Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 1 | /* (C) 2009-2010 by Holger Hans Peter Freyther <zecke@selfish.org> |
| 2 | * (C) 2010 by Sylvain Munaut <tnt@246tNt.com> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 3 | * All Rights Reserved |
| 4 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | * |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 7 | * 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 Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <netinet/in.h> |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 21 | #include <errno.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 27 | #include <osmocom/core/msgb.h> |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 28 | #include <osmocom/core/socket.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 29 | #include <osmocom/core/talloc.h> |
| 30 | #include <osmocom/core/logging.h> |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 31 | #include <osmocom/core/signal.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 32 | |
| 33 | #include <osmocom/vty/telnet_interface.h> |
| 34 | #include <osmocom/vty/buffer.h> |
Holger Hans Peter Freyther | 2e228fc | 2010-09-11 13:41:41 +0800 | [diff] [blame] | 35 | #include <osmocom/vty/command.h> |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 36 | |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 37 | /*! \file telnet_interface.c |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 38 | * Telnet interface towards Osmocom VTY |
Harald Welte | 96e2a00 | 2017-06-12 21:44:18 +0200 | [diff] [blame] | 39 | * |
| 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 Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 44 | * |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 45 | * You typically call \ref telnet_init or \ref telnet_init_dynif once |
| 46 | * from your application code to enable this. |
| 47 | */ |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 48 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 49 | /* per connection data */ |
| 50 | LLIST_HEAD(active_connections); |
| 51 | |
| 52 | static void *tall_telnet_ctx; |
| 53 | |
| 54 | /* per network data */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 55 | static int telnet_new_connection(struct osmo_fd *fd, unsigned int what); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 56 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 57 | static struct osmo_fd server_socket = { |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 58 | .when = OSMO_FD_READ, |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 59 | .cb = telnet_new_connection, |
| 60 | .priv_nr = 0, |
| 61 | }; |
| 62 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 63 | /*! Initialize telnet based VTY interface listening to 127.0.0.1 |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 64 | * \param[in] tall_ctx \ref talloc context |
| 65 | * \param[in] priv private data to be passed to callback |
Holger Hans Peter Freyther | d8d0ef6 | 2018-12-22 03:23:43 +0000 | [diff] [blame] | 66 | * \param[in] port TCP port number to bind to |
Harald Welte | 7acb30c | 2011-08-17 17:13:48 +0200 | [diff] [blame] | 67 | */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 68 | int telnet_init(void *tall_ctx, void *priv, int port) |
| 69 | { |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 70 | return telnet_init_dynif(tall_ctx, priv, "127.0.0.1", port); |
| 71 | } |
| 72 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 73 | /*! Initialize telnet based VTY interface |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 74 | * \param[in] tall_ctx \ref talloc context |
| 75 | * \param[in] priv private data to be passed to callback |
| 76 | * \param[in] ip IP to listen to ('::1' for localhost, '::0' for all, ...) |
Holger Hans Peter Freyther | d8d0ef6 | 2018-12-22 03:23:43 +0000 | [diff] [blame] | 77 | * \param[in] port TCP port number to bind to |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 78 | */ |
| 79 | int telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port) |
| 80 | { |
| 81 | int rc; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 82 | |
Max | af35eb2 | 2022-12-03 17:10:42 +0300 | [diff] [blame] | 83 | if (port < 0) |
| 84 | return -EINVAL; |
| 85 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 86 | tall_telnet_ctx = talloc_named_const(tall_ctx, 1, |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 87 | "telnet_connection"); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 88 | |
Sylvain Munaut | a9efc12 | 2012-03-01 20:41:40 +0100 | [diff] [blame] | 89 | rc = osmo_sock_init_ofd( |
| 90 | &server_socket, |
| 91 | AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP, |
| 92 | ip, port, OSMO_SOCK_F_BIND |
| 93 | ); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 94 | |
| 95 | server_socket.data = priv; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 96 | |
Neels Hofmeyr | 55dc2ed | 2016-09-19 14:10:25 +0200 | [diff] [blame] | 97 | if (rc < 0) { |
| 98 | LOGP(DLGLOBAL, LOGL_ERROR, "Cannot bind telnet at %s %d\n", |
| 99 | ip, port); |
Max | 6d26134 | 2022-12-03 17:06:51 +0300 | [diff] [blame] | 100 | return rc; |
Neels Hofmeyr | 55dc2ed | 2016-09-19 14:10:25 +0200 | [diff] [blame] | 101 | } |
| 102 | |
Max | 7a2ec6e | 2018-10-24 11:29:31 +0200 | [diff] [blame] | 103 | LOGP(DLGLOBAL, LOGL_NOTICE, "Available via telnet %s %d\n", ip, port); |
Neels Hofmeyr | 55dc2ed | 2016-09-19 14:10:25 +0200 | [diff] [blame] | 104 | return 0; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 105 | } |
| 106 | |
Holger Hans Peter Freyther | 99ae401 | 2018-12-15 17:36:41 +0000 | [diff] [blame] | 107 | /*! Initializes telnet based VTY interface using the configured bind addr/port. |
| 108 | * \param[in] tall_ctx \ref talloc context |
| 109 | * \param[in] priv private data to be passed to callback |
Max | 5b37642 | 2022-12-03 20:22:08 +0300 | [diff] [blame] | 110 | * \param[in] default_port TCP port number to bind to if not explicitly configured |
Holger Hans Peter Freyther | 99ae401 | 2018-12-15 17:36:41 +0000 | [diff] [blame] | 111 | */ |
| 112 | int telnet_init_default(void *tall_ctx, void *priv, int default_port) |
| 113 | { |
| 114 | return telnet_init_dynif(tall_ctx, priv, vty_get_bind_addr(), |
| 115 | vty_get_bind_port(default_port)); |
| 116 | } |
| 117 | |
| 118 | |
Holger Hans Peter Freyther | 2e228fc | 2010-09-11 13:41:41 +0800 | [diff] [blame] | 119 | extern struct host host; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 120 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 121 | /*! close a telnet connection */ |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 122 | int telnet_close_client(struct osmo_fd *fd) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 123 | { |
| 124 | struct telnet_connection *conn = (struct telnet_connection*)fd->data; |
Vadim Yanitskiy | 74b6ff0 | 2019-07-28 00:28:36 +0700 | [diff] [blame] | 125 | char sock_name_buf[OSMO_SOCK_NAME_MAXLEN]; |
| 126 | int rc; |
| 127 | |
| 128 | /* FIXME: getsockname() always fails: "Bad file descriptor" */ |
| 129 | rc = osmo_sock_get_name_buf(sock_name_buf, OSMO_SOCK_NAME_MAXLEN, fd->fd); |
| 130 | LOGP(DLGLOBAL, LOGL_INFO, "Closing telnet connection %s\n", |
| 131 | (rc <= 0) ? "r=NULL<->l=NULL" : sock_name_buf); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 132 | |
| 133 | close(fd->fd); |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 134 | osmo_fd_unregister(fd); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 135 | |
| 136 | if (conn->dbg) { |
| 137 | log_del_target(conn->dbg); |
| 138 | talloc_free(conn->dbg); |
| 139 | } |
| 140 | |
| 141 | llist_del(&conn->entry); |
| 142 | talloc_free(conn); |
| 143 | return 0; |
| 144 | } |
| 145 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 146 | static int client_data(struct osmo_fd *fd, unsigned int what) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 147 | { |
| 148 | struct telnet_connection *conn = fd->data; |
| 149 | int rc = 0; |
| 150 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 151 | if (what & OSMO_FD_READ) { |
| 152 | conn->fd.when &= ~OSMO_FD_READ; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 153 | rc = vty_read(conn->vty); |
| 154 | } |
| 155 | |
| 156 | /* vty might have been closed from vithin vty_read() */ |
Holger Hans Peter Freyther | eb55e6a | 2014-07-01 19:39:26 +0200 | [diff] [blame] | 157 | if (rc == -EBADF) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 158 | return rc; |
| 159 | |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 160 | if (what & OSMO_FD_WRITE) { |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 161 | rc = buffer_flush_all(conn->vty->obuf, fd->fd); |
| 162 | if (rc == BUFFER_EMPTY) |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 163 | conn->fd.when &= ~OSMO_FD_WRITE; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | return rc; |
| 167 | } |
| 168 | |
Pablo Neira Ayuso | f7f89d0 | 2011-05-07 12:42:40 +0200 | [diff] [blame] | 169 | static int telnet_new_connection(struct osmo_fd *fd, unsigned int what) |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 170 | { |
| 171 | struct telnet_connection *connection; |
| 172 | struct sockaddr_in sockaddr; |
| 173 | socklen_t len = sizeof(sockaddr); |
| 174 | int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len); |
Vadim Yanitskiy | 74b6ff0 | 2019-07-28 00:28:36 +0700 | [diff] [blame] | 175 | char sock_name_buf[OSMO_SOCK_NAME_MAXLEN]; |
Harald Welte | 4a1cb09 | 2016-11-26 00:11:53 +0100 | [diff] [blame] | 176 | int rc; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 177 | |
| 178 | if (new_connection < 0) { |
Vadim Yanitskiy | 0ba3573 | 2019-07-27 21:47:59 +0700 | [diff] [blame] | 179 | LOGP(DLGLOBAL, LOGL_ERROR, "telnet accept failed\n"); |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 180 | return new_connection; |
| 181 | } |
| 182 | |
Vadim Yanitskiy | 74b6ff0 | 2019-07-28 00:28:36 +0700 | [diff] [blame] | 183 | rc = osmo_sock_get_name_buf(sock_name_buf, OSMO_SOCK_NAME_MAXLEN, new_connection); |
| 184 | LOGP(DLGLOBAL, LOGL_INFO, "Accept()ed new telnet connection %s\n", |
| 185 | (rc <= 0) ? "r=NULL<->l=NULL" : sock_name_buf); |
| 186 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 187 | connection = talloc_zero(tall_telnet_ctx, struct telnet_connection); |
| 188 | connection->priv = fd->data; |
| 189 | connection->fd.data = connection; |
| 190 | connection->fd.fd = new_connection; |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 191 | connection->fd.when = OSMO_FD_READ; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 192 | connection->fd.cb = client_data; |
Harald Welte | 4a1cb09 | 2016-11-26 00:11:53 +0100 | [diff] [blame] | 193 | rc = osmo_fd_register(&connection->fd); |
| 194 | if (rc < 0) { |
| 195 | talloc_free(connection); |
| 196 | return rc; |
| 197 | } |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 198 | llist_add_tail(&connection->entry, &active_connections); |
| 199 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 200 | connection->vty = vty_create(new_connection, connection); |
| 201 | if (!connection->vty) { |
Vadim Yanitskiy | 0ba3573 | 2019-07-27 21:47:59 +0700 | [diff] [blame] | 202 | LOGP(DLGLOBAL, LOGL_ERROR, "couldn't create VTY\n"); |
Harald Welte | e8e4322 | 2018-10-21 13:22:31 +0200 | [diff] [blame] | 203 | /* vty_create() is already closing the fd if it returns NULL */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 204 | talloc_free(connection); |
| 205 | return -1; |
| 206 | } |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
Neels Hofmeyr | 6305300 | 2019-04-10 02:41:53 +0200 | [diff] [blame] | 211 | bool vty_is_active(struct vty *vty) |
| 212 | { |
| 213 | struct telnet_connection *connection; |
| 214 | llist_for_each_entry(connection, &active_connections, entry) { |
| 215 | if (connection->vty == vty) |
| 216 | return true; |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 221 | /*! callback from core VTY code about VTY related events */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 222 | void vty_event(enum event event, int sock, struct vty *vty) |
| 223 | { |
Vadim Yanitskiy | a9a8ea5 | 2019-07-27 21:58:44 +0700 | [diff] [blame] | 224 | struct vty_signal_data sig_data; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 225 | struct telnet_connection *connection = vty->priv; |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 226 | struct osmo_fd *bfd; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 227 | |
| 228 | if (vty->type != VTY_TERM) |
| 229 | return; |
| 230 | |
Holger Hans Peter Freyther | 2c9168c | 2013-10-10 20:21:33 +0200 | [diff] [blame] | 231 | sig_data.event = event; |
| 232 | sig_data.sock = sock; |
| 233 | sig_data.vty = vty; |
| 234 | osmo_signal_dispatch(SS_L_VTY, S_VTY_EVENT, &sig_data); |
| 235 | |
| 236 | if (!connection) |
| 237 | return; |
| 238 | |
| 239 | bfd = &connection->fd; |
| 240 | |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 241 | switch (event) { |
| 242 | case VTY_READ: |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 243 | bfd->when |= OSMO_FD_READ; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 244 | break; |
| 245 | case VTY_WRITE: |
Harald Welte | 1688699 | 2019-03-20 10:26:39 +0100 | [diff] [blame] | 246 | bfd->when |= OSMO_FD_WRITE; |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 247 | break; |
| 248 | case VTY_CLOSED: |
| 249 | /* vty layer is about to free() vty */ |
Harald Welte | 3fb0b6f | 2010-05-19 19:02:52 +0200 | [diff] [blame] | 250 | telnet_close_client(bfd); |
| 251 | break; |
| 252 | default: |
| 253 | break; |
| 254 | } |
| 255 | } |
| 256 | |
Harald Welte | 8c64825 | 2017-10-16 15:17:03 +0200 | [diff] [blame] | 257 | /*! Close all telnet connections and release the telnet socket */ |
Pau Espin Pedrol | 5bc9f38 | 2020-10-12 15:40:36 +0200 | [diff] [blame] | 258 | void telnet_exit(void) |
Andreas.Eversberg | dc3be0a | 2011-11-06 20:09:28 +0100 | [diff] [blame] | 259 | { |
| 260 | struct telnet_connection *tc, *tc2; |
| 261 | |
| 262 | llist_for_each_entry_safe(tc, tc2, &active_connections, entry) |
| 263 | telnet_close_client(&tc->fd); |
| 264 | |
| 265 | osmo_fd_unregister(&server_socket); |
| 266 | close(server_socket.fd); |
| 267 | talloc_free(tall_telnet_ctx); |
| 268 | } |