blob: fcb4c8db0050db6f4c39bb9f3dd4fa433662f7be [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 Welte3fb0b6f2010-05-19 19:02:52 +020062 .when = BSC_FD_READ,
63 .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
70 * \param[in] port UDP port number
71 */
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, ...)
81 * \param[in] port UDP port number
82 */
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 Freyther2e228fc2010-09-11 13:41:41 +0800108extern struct host host;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200109
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200110/*! close a telnet connection */
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200111int telnet_close_client(struct osmo_fd *fd)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200112{
113 struct telnet_connection *conn = (struct telnet_connection*)fd->data;
114
115 close(fd->fd);
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200116 osmo_fd_unregister(fd);
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200117
118 if (conn->dbg) {
119 log_del_target(conn->dbg);
120 talloc_free(conn->dbg);
121 }
122
123 llist_del(&conn->entry);
124 talloc_free(conn);
125 return 0;
126}
127
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200128static int client_data(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200129{
130 struct telnet_connection *conn = fd->data;
131 int rc = 0;
132
133 if (what & BSC_FD_READ) {
134 conn->fd.when &= ~BSC_FD_READ;
135 rc = vty_read(conn->vty);
136 }
137
138 /* vty might have been closed from vithin vty_read() */
Holger Hans Peter Freythereb55e6a2014-07-01 19:39:26 +0200139 if (rc == -EBADF)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200140 return rc;
141
142 if (what & BSC_FD_WRITE) {
143 rc = buffer_flush_all(conn->vty->obuf, fd->fd);
144 if (rc == BUFFER_EMPTY)
145 conn->fd.when &= ~BSC_FD_WRITE;
146 }
147
148 return rc;
149}
150
Pablo Neira Ayusof7f89d02011-05-07 12:42:40 +0200151static int telnet_new_connection(struct osmo_fd *fd, unsigned int what)
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200152{
153 struct telnet_connection *connection;
154 struct sockaddr_in sockaddr;
155 socklen_t len = sizeof(sockaddr);
156 int new_connection = accept(fd->fd, (struct sockaddr*)&sockaddr, &len);
Harald Welte4a1cb092016-11-26 00:11:53 +0100157 int rc;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200158
159 if (new_connection < 0) {
160 LOGP(0, LOGL_ERROR, "telnet accept failed\n");
161 return new_connection;
162 }
163
164 connection = talloc_zero(tall_telnet_ctx, struct telnet_connection);
165 connection->priv = fd->data;
166 connection->fd.data = connection;
167 connection->fd.fd = new_connection;
168 connection->fd.when = BSC_FD_READ;
169 connection->fd.cb = client_data;
Harald Welte4a1cb092016-11-26 00:11:53 +0100170 rc = osmo_fd_register(&connection->fd);
171 if (rc < 0) {
172 talloc_free(connection);
173 return rc;
174 }
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200175 llist_add_tail(&connection->entry, &active_connections);
176
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200177 connection->vty = vty_create(new_connection, connection);
178 if (!connection->vty) {
179 LOGP(0, LOGL_ERROR, "couldn't create VTY\n");
Harald Weltee8e43222018-10-21 13:22:31 +0200180 /* vty_create() is already closing the fd if it returns NULL */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200181 talloc_free(connection);
182 return -1;
183 }
184
185 return 0;
186}
187
Neels Hofmeyr87e45502017-06-20 00:17:59 +0200188/*! callback from core VTY code about VTY related events */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200189void vty_event(enum event event, int sock, struct vty *vty)
190{
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200191 struct vty_signal_data sig_data = { 0, };
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200192 struct telnet_connection *connection = vty->priv;
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200193 struct osmo_fd *bfd;
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200194
195 if (vty->type != VTY_TERM)
196 return;
197
Holger Hans Peter Freyther2c9168c2013-10-10 20:21:33 +0200198 sig_data.event = event;
199 sig_data.sock = sock;
200 sig_data.vty = vty;
201 osmo_signal_dispatch(SS_L_VTY, S_VTY_EVENT, &sig_data);
202
203 if (!connection)
204 return;
205
206 bfd = &connection->fd;
207
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200208 switch (event) {
209 case VTY_READ:
210 bfd->when |= BSC_FD_READ;
211 break;
212 case VTY_WRITE:
213 bfd->when |= BSC_FD_WRITE;
214 break;
215 case VTY_CLOSED:
216 /* vty layer is about to free() vty */
Harald Welte3fb0b6f2010-05-19 19:02:52 +0200217 telnet_close_client(bfd);
218 break;
219 default:
220 break;
221 }
222}
223
Harald Welte8c648252017-10-16 15:17:03 +0200224/*! Close all telnet connections and release the telnet socket */
Andreas.Eversbergdc3be0a2011-11-06 20:09:28 +0100225void telnet_exit(void)
226{
227 struct telnet_connection *tc, *tc2;
228
229 llist_for_each_entry_safe(tc, tc2, &active_connections, entry)
230 telnet_close_client(&tc->fd);
231
232 osmo_fd_unregister(&server_socket);
233 close(server_socket.fd);
234 talloc_free(tall_telnet_ctx);
235}