blob: f82a7ff1b88c8a8a75ca530d03a5689d0354b4f3 [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file telnet_interface.c
2 * minimalistic telnet/network interface it might turn into a wire interface */
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003/* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 *
20 */
21
22#include <sys/socket.h>
23#include <netinet/in.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010024#include <errno.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020025#include <stdlib.h>
26#include <stdio.h>
27#include <string.h>
28#include <unistd.h>
29
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010030#include <osmocom/core/msgb.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010031#include <osmocom/core/socket.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010032#include <osmocom/core/talloc.h>
33#include <osmocom/core/logging.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020034#include <osmocom/core/signal.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020035
36#include <osmocom/vty/telnet_interface.h>
37#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +080038#include <osmocom/vty/command.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020039
Harald Weltee881b1b2011-08-17 18:52:30 +020040/*! \addtogroup telnet_interface
Harald Welte7acb30c2011-08-17 17:13:48 +020041 * @{
Neels Hofmeyr87e45502017-06-20 00:17:59 +020042 * Telnet interface towards Osmocom VTY
Harald Welte96e2a002017-06-12 21:44:18 +020043 *
44 * This module contains the code implementing a telnet server for VTY
45 * access. This telnet server gets linked into each libosmovty-using
46 * process in order to enable interactive command-line introspection,
47 * interaction and configuration.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020048 *
49 * \file telnet_interface.c */
Harald Welte7acb30c2011-08-17 17:13:48 +020050
Harald Welte3fb0b6f2010-05-19 19:02:52 +020051/* per connection data */
52LLIST_HEAD(active_connections);
53
54static void *tall_telnet_ctx;
55
56/* per network data */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020057static int telnet_new_connection(struct osmo_fd *fd, unsigned int what);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020058
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020059static struct osmo_fd server_socket = {
Harald Welte3fb0b6f2010-05-19 19:02:52 +020060 .when = BSC_FD_READ,
61 .cb = telnet_new_connection,
62 .priv_nr = 0,
63};
64
Neels Hofmeyr87e45502017-06-20 00:17:59 +020065/*! Initialize telnet based VTY interface listening to 127.0.0.1
Harald Welte7acb30c2011-08-17 17:13:48 +020066 * \param[in] tall_ctx \ref talloc context
67 * \param[in] priv private data to be passed to callback
68 * \param[in] port UDP port number
69 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020070int telnet_init(void *tall_ctx, void *priv, int port)
71{
Sylvain Munauta9efc122012-03-01 20:41:40 +010072 return telnet_init_dynif(tall_ctx, priv, "127.0.0.1", port);
73}
74
Neels Hofmeyr87e45502017-06-20 00:17:59 +020075/*! Initialize telnet based VTY interface
Sylvain Munauta9efc122012-03-01 20:41:40 +010076 * \param[in] tall_ctx \ref talloc context
77 * \param[in] priv private data to be passed to callback
78 * \param[in] ip IP to listen to ('::1' for localhost, '::0' for all, ...)
79 * \param[in] port UDP port number
80 */
81int telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port)
82{
83 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020084
85 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
Sylvain Munauta9efc122012-03-01 20:41:40 +010086 "telnet_connection");
Harald Welte3fb0b6f2010-05-19 19:02:52 +020087
Sylvain Munauta9efc122012-03-01 20:41:40 +010088 rc = osmo_sock_init_ofd(
89 &server_socket,
90 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
91 ip, port, OSMO_SOCK_F_BIND
92 );
Harald Welte3fb0b6f2010-05-19 19:02:52 +020093
94 server_socket.data = priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +020096 if (rc < 0) {
97 LOGP(DLGLOBAL, LOGL_ERROR, "Cannot bind telnet at %s %d\n",
98 ip, port);
99 return -1;
100 }
101
102 LOGP(DLGLOBAL, LOGL_NOTICE, "telnet at %s %d\n", ip, port);
103 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200104}
105
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +0800106extern struct host host;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200107
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200108/*! close a telnet connection */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200109int telnet_close_client(struct osmo_fd *fd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200110{
111 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
112
113 close(fd->fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200114 osmo_fd_unregister(fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200115
116 if (conn->dbg) {
117 log_del_target(conn->dbg);
118 talloc_free(conn->dbg);
119 }
120
121 llist_del(&conn->entry);
122 talloc_free(conn);
123 return 0;
124}
125
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200126static int client_data(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200127{
128 struct telnet_connection *conn = fd->data;
129 int rc = 0;
130
131 if (what & BSC_FD_READ) {
132 conn->fd.when &= ~BSC_FD_READ;
133 rc = vty_read(conn->vty);
134 }
135
136 /* vty might have been closed from vithin vty_read() */
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +0200137 if (rc == -EBADF)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200138 return rc;
139
140 if (what & BSC_FD_WRITE) {
141 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
142 if (rc == BUFFER_EMPTY)
143 conn->fd.when &= ~BSC_FD_WRITE;
144 }
145
146 return rc;
147}
148
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200149static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200150{
151 struct telnet_connection *connection;
152 struct sockaddr_in sockaddr;
153 socklen_t len = sizeof(sockaddr);
154 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
Harald Welte4a1cb092016-11-26 00:11:53 +0100155 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200156
157 if (new_connection < 0) {
158 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
159 return new_connection;
160 }
161
162 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
163 connection->priv = fd->data;
164 connection->fd.data = connection;
165 connection->fd.fd = new_connection;
166 connection->fd.when = BSC_FD_READ;
167 connection->fd.cb = client_data;
Harald Welte4a1cb092016-11-26 00:11:53 +0100168 rc = osmo_fd_register(&connection->fd);
169 if (rc < 0) {
170 talloc_free(connection);
171 return rc;
172 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200173 llist_add_tail(&connection->entry, &active_connections);
174
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200175 connection->vty = vty_create(new_connection, connection);
176 if (!connection->vty) {
177 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
178 close(new_connection);
179 talloc_free(connection);
180 return -1;
181 }
182
183 return 0;
184}
185
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200186/*! callback from core VTY code about VTY related events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200187void vty_event(enum event event, int sock, struct vty *vty)
188{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200189 struct vty_signal_data sig_data = { 0, };
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200190 struct telnet_connection *connection = vty->priv;
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200191 struct osmo_fd *bfd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200192
193 if (vty->type != VTY_TERM)
194 return;
195
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200196 sig_data.event = event;
197 sig_data.sock = sock;
198 sig_data.vty = vty;
199 osmo_signal_dispatch(SS_L_VTY, S_VTY_EVENT, &sig_data);
200
201 if (!connection)
202 return;
203
204 bfd = &connection->fd;
205
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200206 switch (event) {
207 case VTY_READ:
208 bfd->when |= BSC_FD_READ;
209 break;
210 case VTY_WRITE:
211 bfd->when |= BSC_FD_WRITE;
212 break;
213 case VTY_CLOSED:
214 /* vty layer is about to free() vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200215 telnet_close_client(bfd);
216 break;
217 default:
218 break;
219 }
220}
221
Andreas.Eversbergdc3be0a2011-11-06 20:09:28 +0100222void telnet_exit(void)
223{
224 struct telnet_connection *tc, *tc2;
225
226 llist_for_each_entry_safe(tc, tc2, &active_connections, entry)
227 telnet_close_client(&tc->fd);
228
229 osmo_fd_unregister(&server_socket);
230 close(server_socket.fd);
231 talloc_free(tall_telnet_ctx);
232}
233
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200234/*! @} */