blob: 8fa5dbffdb578905838ee8f55024704fdef5cbef [file] [log] [blame]
Harald Weltee08da972017-11-13 01:00:26 +09001/* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org>
2 * (C) 2010 by Sylvain Munaut <tnt@246tNt.com>
Harald Welte3fb0b6f2010-05-19 19:02:52 +02003 * All Rights Reserved
4 *
Harald Weltee08da972017-11-13 01:00:26 +09005 * SPDX-License-Identifier: GPL-2.0+
6 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +02007 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
Harald Welte3fb0b6f2010-05-19 19:02:52 +020017 */
18
19#include <sys/socket.h>
20#include <netinet/in.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010021#include <errno.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020022#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <unistd.h>
26
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/msgb.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010028#include <osmocom/core/socket.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/talloc.h>
30#include <osmocom/core/logging.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020031#include <osmocom/core/signal.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032
33#include <osmocom/vty/telnet_interface.h>
34#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +080035#include <osmocom/vty/command.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020036
Harald Welte8c648252017-10-16 15:17:03 +020037/*! \file telnet_interface.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020038 * Telnet interface towards Osmocom VTY
Harald Welte96e2a002017-06-12 21:44:18 +020039 *
40 * This module contains the code implementing a telnet server for VTY
41 * access. This telnet server gets linked into each libosmovty-using
42 * process in order to enable interactive command-line introspection,
43 * interaction and configuration.
Neels Hofmeyr17518fe2017-06-20 04:35:06 +020044 *
arehbeinee7b8c52022-12-19 21:27:49 +010045 * You typically call telnet_init_default once
Harald Welte8c648252017-10-16 15:17:03 +020046 * from your application code to enable this.
47 */
Harald Welte7acb30c2011-08-17 17:13:48 +020048
Harald Welte3fb0b6f2010-05-19 19:02:52 +020049/* per connection data */
50LLIST_HEAD(active_connections);
51
52static void *tall_telnet_ctx;
53
54/* per network data */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020055static int telnet_new_connection(struct osmo_fd *fd, unsigned int what);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020056
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020057static struct osmo_fd server_socket = {
Harald Welte16886992019-03-20 10:26:39 +010058 .when = OSMO_FD_READ,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020059 .cb = telnet_new_connection,
60 .priv_nr = 0,
61};
62
arehbeinee7b8c52022-12-19 21:27:49 +010063/* Helper for deprecating telnet_init_dynif(), which previously held this code */
64static int _telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port)
Sylvain Munauta9efc122012-03-01 20:41:40 +010065{
66 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020067
Maxaf35eb22022-12-03 17:10:42 +030068 if (port < 0)
69 return -EINVAL;
70
Harald Welte3fb0b6f2010-05-19 19:02:52 +020071 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
Sylvain Munauta9efc122012-03-01 20:41:40 +010072 "telnet_connection");
Harald Welte3fb0b6f2010-05-19 19:02:52 +020073
Sylvain Munauta9efc122012-03-01 20:41:40 +010074 rc = osmo_sock_init_ofd(
75 &server_socket,
76 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
77 ip, port, OSMO_SOCK_F_BIND
78 );
Harald Welte3fb0b6f2010-05-19 19:02:52 +020079
80 server_socket.data = priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020081
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +020082 if (rc < 0) {
83 LOGP(DLGLOBAL, LOGL_ERROR, "Cannot bind telnet at %s %d\n",
84 ip, port);
Max6d261342022-12-03 17:06:51 +030085 return rc;
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +020086 }
87
Max7a2ec6e2018-10-24 11:29:31 +020088 LOGP(DLGLOBAL, LOGL_NOTICE, "Available via telnet %s %d\n", ip, port);
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +020089 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020090}
91
arehbeinee7b8c52022-12-19 21:27:49 +010092/*! Initialize telnet based VTY interface listening to 127.0.0.1
93 * \param[in] tall_ctx \ref talloc context
94 * \param[in] priv private data to be passed to callback
95 * \param[in] port TCP port number to bind to
96 * \deprecated use telnet_init_default() instead
97 */
98int telnet_init(void *tall_ctx, void *priv, int port)
99{
100 return _telnet_init_dynif(tall_ctx, priv, "127.0.0.1", port);
101}
102
103/*! Initialize telnet based VTY interface
104 * \param[in] tall_ctx \ref talloc context
105 * \param[in] priv private data to be passed to callback
106 * \param[in] ip IP to listen to ('::1' for localhost, '::0' for all, ...)
107 * \param[in] port TCP port number to bind to
108 * \deprecated use telnet_init_default() instead
109 */
110int telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port)
111{
112 return _telnet_init_dynif(tall_ctx, priv, ip, port);
113}
114
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000115/*! Initializes telnet based VTY interface using the configured bind addr/port.
116 * \param[in] tall_ctx \ref talloc context
117 * \param[in] priv private data to be passed to callback
Max5b376422022-12-03 20:22:08 +0300118 * \param[in] default_port TCP port number to bind to if not explicitly configured
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000119 */
120int telnet_init_default(void *tall_ctx, void *priv, int default_port)
121{
arehbeinee7b8c52022-12-19 21:27:49 +0100122 return _telnet_init_dynif(tall_ctx, priv, vty_get_bind_addr(),
123 vty_get_bind_port(default_port));
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000124}
125
126
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +0800127extern struct host host;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200128
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200129/*! close a telnet connection */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200130int telnet_close_client(struct osmo_fd *fd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200131{
132 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
Vadim Yanitskiy74b6ff02019-07-28 00:28:36 +0700133 char sock_name_buf[OSMO_SOCK_NAME_MAXLEN];
134 int rc;
135
136 /* FIXME: getsockname() always fails: "Bad file descriptor" */
137 rc = osmo_sock_get_name_buf(sock_name_buf, OSMO_SOCK_NAME_MAXLEN, fd->fd);
138 LOGP(DLGLOBAL, LOGL_INFO, "Closing telnet connection %s\n",
139 (rc <= 0) ? "r=NULL<->l=NULL" : sock_name_buf);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200140
141 close(fd->fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200142 osmo_fd_unregister(fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200143
144 if (conn->dbg) {
145 log_del_target(conn->dbg);
146 talloc_free(conn->dbg);
147 }
148
149 llist_del(&conn->entry);
150 talloc_free(conn);
151 return 0;
152}
153
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200154static int client_data(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200155{
156 struct telnet_connection *conn = fd->data;
157 int rc = 0;
158
Harald Welte16886992019-03-20 10:26:39 +0100159 if (what & OSMO_FD_READ) {
160 conn->fd.when &= ~OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200161 rc = vty_read(conn->vty);
162 }
163
164 /* vty might have been closed from vithin vty_read() */
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +0200165 if (rc == -EBADF)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200166 return rc;
167
Harald Welte16886992019-03-20 10:26:39 +0100168 if (what & OSMO_FD_WRITE) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200169 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
170 if (rc == BUFFER_EMPTY)
Harald Welte16886992019-03-20 10:26:39 +0100171 conn->fd.when &= ~OSMO_FD_WRITE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200172 }
173
174 return rc;
175}
176
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200177static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200178{
179 struct telnet_connection *connection;
180 struct sockaddr_in sockaddr;
181 socklen_t len = sizeof(sockaddr);
182 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
Vadim Yanitskiy74b6ff02019-07-28 00:28:36 +0700183 char sock_name_buf[OSMO_SOCK_NAME_MAXLEN];
Harald Welte4a1cb092016-11-26 00:11:53 +0100184 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200185
186 if (new_connection < 0) {
Vadim Yanitskiy0ba35732019-07-27 21:47:59 +0700187 LOGP(DLGLOBAL, LOGL_ERROR, "telnet accept failed\n");
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200188 return new_connection;
189 }
190
Vadim Yanitskiy74b6ff02019-07-28 00:28:36 +0700191 rc = osmo_sock_get_name_buf(sock_name_buf, OSMO_SOCK_NAME_MAXLEN, new_connection);
192 LOGP(DLGLOBAL, LOGL_INFO, "Accept()ed new telnet connection %s\n",
193 (rc <= 0) ? "r=NULL<->l=NULL" : sock_name_buf);
194
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200195 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
196 connection->priv = fd->data;
197 connection->fd.data = connection;
198 connection->fd.fd = new_connection;
Harald Welte16886992019-03-20 10:26:39 +0100199 connection->fd.when = OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200200 connection->fd.cb = client_data;
Harald Welte4a1cb092016-11-26 00:11:53 +0100201 rc = osmo_fd_register(&connection->fd);
202 if (rc < 0) {
203 talloc_free(connection);
204 return rc;
205 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200206 llist_add_tail(&connection->entry, &active_connections);
207
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200208 connection->vty = vty_create(new_connection, connection);
209 if (!connection->vty) {
Vadim Yanitskiy0ba35732019-07-27 21:47:59 +0700210 LOGP(DLGLOBAL, LOGL_ERROR, "couldn't create VTY\n");
Harald Weltee8e43222018-10-21 13:22:31 +0200211 /* vty_create() is already closing the fd if it returns NULL */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200212 talloc_free(connection);
213 return -1;
214 }
215
216 return 0;
217}
218
Neels Hofmeyr63053002019-04-10 02:41:53 +0200219bool vty_is_active(struct vty *vty)
220{
221 struct telnet_connection *connection;
222 llist_for_each_entry(connection, &active_connections, entry) {
223 if (connection->vty == vty)
224 return true;
225 }
226 return false;
227}
228
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200229/*! callback from core VTY code about VTY related events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200230void vty_event(enum event event, int sock, struct vty *vty)
231{
Vadim Yanitskiya9a8ea52019-07-27 21:58:44 +0700232 struct vty_signal_data sig_data;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200233 struct telnet_connection *connection = vty->priv;
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200234 struct osmo_fd *bfd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200235
236 if (vty->type != VTY_TERM)
237 return;
238
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200239 sig_data.event = event;
240 sig_data.sock = sock;
241 sig_data.vty = vty;
242 osmo_signal_dispatch(SS_L_VTY, S_VTY_EVENT, &sig_data);
243
244 if (!connection)
245 return;
246
247 bfd = &connection->fd;
248
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200249 switch (event) {
250 case VTY_READ:
Harald Welte16886992019-03-20 10:26:39 +0100251 bfd->when |= OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200252 break;
253 case VTY_WRITE:
Harald Welte16886992019-03-20 10:26:39 +0100254 bfd->when |= OSMO_FD_WRITE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200255 break;
256 case VTY_CLOSED:
257 /* vty layer is about to free() vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200258 telnet_close_client(bfd);
259 break;
260 default:
261 break;
262 }
263}
264
Harald Welte8c648252017-10-16 15:17:03 +0200265/*! Close all telnet connections and release the telnet socket */
Pau Espin Pedrol5bc9f382020-10-12 15:40:36 +0200266void telnet_exit(void)
Andreas.Eversbergdc3be0a2011-11-06 20:09:28 +0100267{
268 struct telnet_connection *tc, *tc2;
269
270 llist_for_each_entry_safe(tc, tc2, &active_connections, entry)
271 telnet_close_client(&tc->fd);
272
273 osmo_fd_unregister(&server_socket);
274 close(server_socket.fd);
275 talloc_free(tall_telnet_ctx);
276}