blob: c16b0b168d4bb5773acd4e2bec57de5e094ab3fb [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 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <sys/socket.h>
24#include <netinet/in.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010025#include <errno.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020026#include <stdlib.h>
27#include <stdio.h>
28#include <string.h>
29#include <unistd.h>
30
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/msgb.h>
Sylvain Munauta9efc122012-03-01 20:41:40 +010032#include <osmocom/core/socket.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010033#include <osmocom/core/talloc.h>
34#include <osmocom/core/logging.h>
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +020035#include <osmocom/core/signal.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020036
37#include <osmocom/vty/telnet_interface.h>
38#include <osmocom/vty/buffer.h>
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +080039#include <osmocom/vty/command.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020040
Harald Welte8c648252017-10-16 15:17:03 +020041/*! \file telnet_interface.c
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 *
Harald Welte8c648252017-10-16 15:17:03 +020049 * You typically call \ref telnet_init or \ref telnet_init_dynif once
50 * from your application code to enable this.
51 */
Harald Welte7acb30c2011-08-17 17:13:48 +020052
Harald Welte3fb0b6f2010-05-19 19:02:52 +020053/* per connection data */
54LLIST_HEAD(active_connections);
55
56static void *tall_telnet_ctx;
57
58/* per network data */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020059static int telnet_new_connection(struct osmo_fd *fd, unsigned int what);
Harald Welte3fb0b6f2010-05-19 19:02:52 +020060
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +020061static struct osmo_fd server_socket = {
Harald Welte16886992019-03-20 10:26:39 +010062 .when = OSMO_FD_READ,
Harald Welte3fb0b6f2010-05-19 19:02:52 +020063 .cb = telnet_new_connection,
64 .priv_nr = 0,
65};
66
Neels Hofmeyr87e45502017-06-20 00:17:59 +020067/*! Initialize telnet based VTY interface listening to 127.0.0.1
Harald Welte7acb30c2011-08-17 17:13:48 +020068 * \param[in] tall_ctx \ref talloc context
69 * \param[in] priv private data to be passed to callback
Holger Hans Peter Freytherd8d0ef62018-12-22 03:23:43 +000070 * \param[in] port TCP port number to bind to
Harald Welte7acb30c2011-08-17 17:13:48 +020071 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020072int telnet_init(void *tall_ctx, void *priv, int port)
73{
Sylvain Munauta9efc122012-03-01 20:41:40 +010074 return telnet_init_dynif(tall_ctx, priv, "127.0.0.1", port);
75}
76
Neels Hofmeyr87e45502017-06-20 00:17:59 +020077/*! Initialize telnet based VTY interface
Sylvain Munauta9efc122012-03-01 20:41:40 +010078 * \param[in] tall_ctx \ref talloc context
79 * \param[in] priv private data to be passed to callback
80 * \param[in] ip IP to listen to ('::1' for localhost, '::0' for all, ...)
Holger Hans Peter Freytherd8d0ef62018-12-22 03:23:43 +000081 * \param[in] port TCP port number to bind to
Sylvain Munauta9efc122012-03-01 20:41:40 +010082 */
83int telnet_init_dynif(void *tall_ctx, void *priv, const char *ip, int port)
84{
85 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020086
87 tall_telnet_ctx = talloc_named_const(tall_ctx, 1,
Sylvain Munauta9efc122012-03-01 20:41:40 +010088 "telnet_connection");
Harald Welte3fb0b6f2010-05-19 19:02:52 +020089
Sylvain Munauta9efc122012-03-01 20:41:40 +010090 rc = osmo_sock_init_ofd(
91 &server_socket,
92 AF_UNSPEC, SOCK_STREAM, IPPROTO_TCP,
93 ip, port, OSMO_SOCK_F_BIND
94 );
Harald Welte3fb0b6f2010-05-19 19:02:52 +020095
96 server_socket.data = priv;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020097
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +020098 if (rc < 0) {
99 LOGP(DLGLOBAL, LOGL_ERROR, "Cannot bind telnet at %s %d\n",
100 ip, port);
101 return -1;
102 }
103
Max7a2ec6e2018-10-24 11:29:31 +0200104 LOGP(DLGLOBAL, LOGL_NOTICE, "Available via telnet %s %d\n", ip, port);
Neels Hofmeyr55dc2ed2016-09-19 14:10:25 +0200105 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200106}
107
Holger Hans Peter Freyther99ae4012018-12-15 17:36:41 +0000108/*! Initializes telnet based VTY interface using the configured bind addr/port.
109 * \param[in] tall_ctx \ref talloc context
110 * \param[in] priv private data to be passed to callback
111 * \param[in] default_port TCP port number to bind to if not explicitely configured
112 */
113int telnet_init_default(void *tall_ctx, void *priv, int default_port)
114{
115 return telnet_init_dynif(tall_ctx, priv, vty_get_bind_addr(),
116 vty_get_bind_port(default_port));
117}
118
119
Holger Hans Peter Freyther2e228fc2010-09-11 13:41:41 +0800120extern struct host host;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200121
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200122/*! close a telnet connection */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200123int telnet_close_client(struct osmo_fd *fd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200124{
125 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
126
127 close(fd->fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200128 osmo_fd_unregister(fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129
130 if (conn->dbg) {
131 log_del_target(conn->dbg);
132 talloc_free(conn->dbg);
133 }
134
135 llist_del(&conn->entry);
136 talloc_free(conn);
137 return 0;
138}
139
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200140static int client_data(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200141{
142 struct telnet_connection *conn = fd->data;
143 int rc = 0;
144
Harald Welte16886992019-03-20 10:26:39 +0100145 if (what & OSMO_FD_READ) {
146 conn->fd.when &= ~OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200147 rc = vty_read(conn->vty);
148 }
149
150 /* vty might have been closed from vithin vty_read() */
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +0200151 if (rc == -EBADF)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152 return rc;
153
Harald Welte16886992019-03-20 10:26:39 +0100154 if (what & OSMO_FD_WRITE) {
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200155 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
156 if (rc == BUFFER_EMPTY)
Harald Welte16886992019-03-20 10:26:39 +0100157 conn->fd.when &= ~OSMO_FD_WRITE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200158 }
159
160 return rc;
161}
162
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200163static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200164{
165 struct telnet_connection *connection;
166 struct sockaddr_in sockaddr;
167 socklen_t len = sizeof(sockaddr);
168 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
Harald Welte4a1cb092016-11-26 00:11:53 +0100169 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200170
171 if (new_connection < 0) {
Vadim Yanitskiy0ba35732019-07-27 21:47:59 +0700172 LOGP(DLGLOBAL, LOGL_ERROR, "telnet accept failed\n");
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200173 return new_connection;
174 }
175
176 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
177 connection->priv = fd->data;
178 connection->fd.data = connection;
179 connection->fd.fd = new_connection;
Harald Welte16886992019-03-20 10:26:39 +0100180 connection->fd.when = OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200181 connection->fd.cb = client_data;
Harald Welte4a1cb092016-11-26 00:11:53 +0100182 rc = osmo_fd_register(&connection->fd);
183 if (rc < 0) {
184 talloc_free(connection);
185 return rc;
186 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200187 llist_add_tail(&connection->entry, &active_connections);
188
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200189 connection->vty = vty_create(new_connection, connection);
190 if (!connection->vty) {
Vadim Yanitskiy0ba35732019-07-27 21:47:59 +0700191 LOGP(DLGLOBAL, LOGL_ERROR, "couldn't create VTY\n");
Harald Weltee8e43222018-10-21 13:22:31 +0200192 /* vty_create() is already closing the fd if it returns NULL */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200193 talloc_free(connection);
194 return -1;
195 }
196
197 return 0;
198}
199
Neels Hofmeyr63053002019-04-10 02:41:53 +0200200bool vty_is_active(struct vty *vty)
201{
202 struct telnet_connection *connection;
203 llist_for_each_entry(connection, &active_connections, entry) {
204 if (connection->vty == vty)
205 return true;
206 }
207 return false;
208}
209
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200210/*! callback from core VTY code about VTY related events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200211void vty_event(enum event event, int sock, struct vty *vty)
212{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200213 struct vty_signal_data sig_data = { 0, };
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200214 struct telnet_connection *connection = vty->priv;
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200215 struct osmo_fd *bfd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200216
217 if (vty->type != VTY_TERM)
218 return;
219
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200220 sig_data.event = event;
221 sig_data.sock = sock;
222 sig_data.vty = vty;
223 osmo_signal_dispatch(SS_L_VTY, S_VTY_EVENT, &sig_data);
224
225 if (!connection)
226 return;
227
228 bfd = &connection->fd;
229
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200230 switch (event) {
231 case VTY_READ:
Harald Welte16886992019-03-20 10:26:39 +0100232 bfd->when |= OSMO_FD_READ;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200233 break;
234 case VTY_WRITE:
Harald Welte16886992019-03-20 10:26:39 +0100235 bfd->when |= OSMO_FD_WRITE;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200236 break;
237 case VTY_CLOSED:
238 /* vty layer is about to free() vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200239 telnet_close_client(bfd);
240 break;
241 default:
242 break;
243 }
244}
245
Harald Welte8c648252017-10-16 15:17:03 +0200246/*! Close all telnet connections and release the telnet socket */
Andreas.Eversbergdc3be0a2011-11-06 20:09:28 +0100247void telnet_exit(void)
248{
249 struct telnet_connection *tc, *tc2;
250
251 llist_for_each_entry_safe(tc, tc2, &active_connections, entry)
252 telnet_close_client(&tc->fd);
253
254 osmo_fd_unregister(&server_socket);
255 close(server_socket.fd);
256 talloc_free(tall_telnet_ctx);
257}