[21a9869] | 1 | /*
|
---|
[4c6fd56] | 2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
[21a9869] | 3 | * Copyright (c) 2012 Vojtech Horky
|
---|
| 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
| 30 | /** @addtogroup remcons
|
---|
| 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <async.h>
|
---|
| 37 | #include <errno.h>
|
---|
[5d94b16c] | 38 | #include <io/con_srv.h>
|
---|
| 39 | #include <stdio.h>
|
---|
| 40 | #include <stdlib.h>
|
---|
[21a9869] | 41 | #include <str_error.h>
|
---|
| 42 | #include <loc.h>
|
---|
| 43 | #include <io/keycode.h>
|
---|
| 44 | #include <align.h>
|
---|
| 45 | #include <fibril_synch.h>
|
---|
| 46 | #include <task.h>
|
---|
[fab2746] | 47 | #include <inet/addr.h>
|
---|
| 48 | #include <inet/endpoint.h>
|
---|
| 49 | #include <inet/tcp.h>
|
---|
[21a9869] | 50 | #include <io/console.h>
|
---|
| 51 | #include <inttypes.h>
|
---|
[1d6dd2a] | 52 | #include <str.h>
|
---|
[3806317] | 53 | #include "telnet.h"
|
---|
[30d4706] | 54 | #include "user.h"
|
---|
[21a9869] | 55 |
|
---|
| 56 | #define APP_GETTERM "/app/getterm"
|
---|
[03e0a244] | 57 | #define APP_SHELL "/app/bdsh"
|
---|
[5576358] | 58 |
|
---|
[3806317] | 59 | /** Telnet commands to force character mode
|
---|
| 60 | * (redundant to be on the safe side).
|
---|
| 61 | * See
|
---|
[787b65b] | 62 | * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
|
---|
[3806317] | 63 | * for discussion.
|
---|
| 64 | */
|
---|
| 65 | static const telnet_cmd_t telnet_force_character_mode_command[] = {
|
---|
| 66 | TELNET_IAC, TELNET_WILL, TELNET_ECHO,
|
---|
| 67 | TELNET_IAC, TELNET_WILL, TELNET_SUPPRESS_GO_AHEAD,
|
---|
| 68 | TELNET_IAC, TELNET_WONT, TELNET_LINEMODE
|
---|
| 69 | };
|
---|
[3123d2a] | 70 |
|
---|
[3806317] | 71 | static const size_t telnet_force_character_mode_command_count =
|
---|
| 72 | sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
|
---|
| 73 |
|
---|
[b7fd2a0] | 74 | static errno_t remcons_open(con_srvs_t *, con_srv_t *);
|
---|
| 75 | static errno_t remcons_close(con_srv_t *);
|
---|
| 76 | static errno_t remcons_write(con_srv_t *, void *, size_t, size_t *);
|
---|
[5d94b16c] | 77 | static void remcons_sync(con_srv_t *);
|
---|
| 78 | static void remcons_clear(con_srv_t *);
|
---|
| 79 | static void remcons_set_pos(con_srv_t *, sysarg_t col, sysarg_t row);
|
---|
[b7fd2a0] | 80 | static errno_t remcons_get_pos(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 81 | static errno_t remcons_get_size(con_srv_t *, sysarg_t *, sysarg_t *);
|
---|
| 82 | static errno_t remcons_get_color_cap(con_srv_t *, console_caps_t *);
|
---|
| 83 | static errno_t remcons_get_event(con_srv_t *, cons_event_t *);
|
---|
[5d94b16c] | 84 |
|
---|
| 85 | static con_ops_t con_ops = {
|
---|
| 86 | .open = remcons_open,
|
---|
| 87 | .close = remcons_close,
|
---|
| 88 | .read = NULL,
|
---|
| 89 | .write = remcons_write,
|
---|
| 90 | .sync = remcons_sync,
|
---|
| 91 | .clear = remcons_clear,
|
---|
| 92 | .set_pos = remcons_set_pos,
|
---|
| 93 | .get_pos = remcons_get_pos,
|
---|
| 94 | .get_size = remcons_get_size,
|
---|
| 95 | .get_color_cap = remcons_get_color_cap,
|
---|
| 96 | .set_style = NULL,
|
---|
| 97 | .set_color = NULL,
|
---|
| 98 | .set_rgb_color = NULL,
|
---|
| 99 | .set_cursor_visibility = NULL,
|
---|
| 100 | .get_event = remcons_get_event
|
---|
| 101 | };
|
---|
[21a9869] | 102 |
|
---|
[fab2746] | 103 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn);
|
---|
| 104 |
|
---|
| 105 | static tcp_listen_cb_t listen_cb = {
|
---|
| 106 | .new_conn = remcons_new_conn
|
---|
| 107 | };
|
---|
| 108 |
|
---|
| 109 | static tcp_cb_t conn_cb = {
|
---|
| 110 | .connected = NULL
|
---|
| 111 | };
|
---|
| 112 |
|
---|
[4c6fd56] | 113 | static loc_srv_t *remcons_srv;
|
---|
| 114 |
|
---|
[5d94b16c] | 115 | static telnet_user_t *srv_to_user(con_srv_t *srv)
|
---|
[21a9869] | 116 | {
|
---|
[5d94b16c] | 117 | return srv->srvs->sarg;
|
---|
| 118 | }
|
---|
[21a9869] | 119 |
|
---|
[b7fd2a0] | 120 | static errno_t remcons_open(con_srvs_t *srvs, con_srv_t *srv)
|
---|
[5d94b16c] | 121 | {
|
---|
| 122 | telnet_user_t *user = srv_to_user(srv);
|
---|
[99c2e9f3] | 123 |
|
---|
[5d94b16c] | 124 | telnet_user_log(user, "New client connected (%p).", srv);
|
---|
[21a9869] | 125 |
|
---|
[5d94b16c] | 126 | /* Force character mode. */
|
---|
[fab2746] | 127 | (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
|
---|
| 128 | telnet_force_character_mode_command_count);
|
---|
[21a9869] | 129 |
|
---|
[5d94b16c] | 130 | return EOK;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
[b7fd2a0] | 133 | static errno_t remcons_close(con_srv_t *srv)
|
---|
[5d94b16c] | 134 | {
|
---|
| 135 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 136 |
|
---|
| 137 | telnet_user_notify_client_disconnected(user);
|
---|
| 138 | telnet_user_log(user, "Client disconnected (%p).", srv);
|
---|
| 139 |
|
---|
| 140 | return EOK;
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[b7fd2a0] | 143 | static errno_t remcons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
|
---|
[5d94b16c] | 144 | {
|
---|
| 145 | telnet_user_t *user = srv_to_user(srv);
|
---|
[b7fd2a0] | 146 | errno_t rc;
|
---|
[5d94b16c] | 147 |
|
---|
| 148 | rc = telnet_user_send_data(user, data, size);
|
---|
| 149 | if (rc != EOK)
|
---|
| 150 | return rc;
|
---|
| 151 |
|
---|
[c8211849] | 152 | *nwritten = size;
|
---|
| 153 | return EOK;
|
---|
[5d94b16c] | 154 | }
|
---|
| 155 |
|
---|
| 156 | static void remcons_sync(con_srv_t *srv)
|
---|
| 157 | {
|
---|
| 158 | (void) srv;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | static void remcons_clear(con_srv_t *srv)
|
---|
| 162 | {
|
---|
| 163 | (void) srv;
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | static void remcons_set_pos(con_srv_t *srv, sysarg_t col, sysarg_t row)
|
---|
| 167 | {
|
---|
| 168 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 169 |
|
---|
| 170 | telnet_user_update_cursor_x(user, col);
|
---|
| 171 | }
|
---|
| 172 |
|
---|
[b7fd2a0] | 173 | static errno_t remcons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
---|
[5d94b16c] | 174 | {
|
---|
| 175 | telnet_user_t *user = srv_to_user(srv);
|
---|
| 176 |
|
---|
| 177 | *col = user->cursor_x;
|
---|
| 178 | *row = 0;
|
---|
| 179 |
|
---|
| 180 | return EOK;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
[b7fd2a0] | 183 | static errno_t remcons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
---|
[5d94b16c] | 184 | {
|
---|
| 185 | (void) srv;
|
---|
| 186 |
|
---|
| 187 | *cols = 100;
|
---|
| 188 | *rows = 1;
|
---|
| 189 |
|
---|
| 190 | return EOK;
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[b7fd2a0] | 193 | static errno_t remcons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
|
---|
[5d94b16c] | 194 | {
|
---|
| 195 | (void) srv;
|
---|
| 196 | *ccaps = CONSOLE_CAP_NONE;
|
---|
| 197 |
|
---|
| 198 | return EOK;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
[b7fd2a0] | 201 | static errno_t remcons_get_event(con_srv_t *srv, cons_event_t *event)
|
---|
[5d94b16c] | 202 | {
|
---|
| 203 | telnet_user_t *user = srv_to_user(srv);
|
---|
[902f0906] | 204 | kbd_event_t kevent;
|
---|
[b7fd2a0] | 205 | errno_t rc;
|
---|
[5d94b16c] | 206 |
|
---|
[902f0906] | 207 | rc = telnet_user_get_next_keyboard_event(user, &kevent);
|
---|
[5d94b16c] | 208 | if (rc != EOK) {
|
---|
| 209 | /* XXX What? */
|
---|
| 210 | memset(event, 0, sizeof(*event));
|
---|
| 211 | return EOK;
|
---|
[21a9869] | 212 | }
|
---|
[5d94b16c] | 213 |
|
---|
[902f0906] | 214 | event->type = CEV_KEY;
|
---|
| 215 | event->ev.key = kevent;
|
---|
| 216 |
|
---|
[5d94b16c] | 217 | return EOK;
|
---|
[21a9869] | 218 | }
|
---|
| 219 |
|
---|
[5923cf82] | 220 | /** Callback when client connects to a telnet terminal. */
|
---|
[984a9ba] | 221 | static void client_connection(ipc_call_t *icall, void *arg)
|
---|
[6f7cd5d] | 222 | {
|
---|
[787b65b] | 223 | /* Find the user. */
|
---|
[fafb8e5] | 224 | telnet_user_t *user = telnet_user_get_for_client_connection(ipc_get_arg2(icall));
|
---|
[787b65b] | 225 | if (user == NULL) {
|
---|
[984a9ba] | 226 | async_answer_0(icall, ENOENT);
|
---|
[6f7cd5d] | 227 | return;
|
---|
| 228 | }
|
---|
| 229 |
|
---|
[99c2e9f3] | 230 | /* Handle messages. */
|
---|
[984a9ba] | 231 | con_conn(icall, &user->srvs);
|
---|
[6f7cd5d] | 232 | }
|
---|
| 233 |
|
---|
[5923cf82] | 234 | /** Fibril for spawning the task running after user connects.
|
---|
| 235 | *
|
---|
| 236 | * @param arg Corresponding @c telnet_user_t structure.
|
---|
| 237 | */
|
---|
[b7fd2a0] | 238 | static errno_t spawn_task_fibril(void *arg)
|
---|
[21a9869] | 239 | {
|
---|
[787b65b] | 240 | telnet_user_t *user = arg;
|
---|
[a35b458] | 241 |
|
---|
[21a9869] | 242 | task_id_t task;
|
---|
[1c635d6] | 243 | task_wait_t wait;
|
---|
[b7fd2a0] | 244 | errno_t rc = task_spawnl(&task, &wait, APP_GETTERM, APP_GETTERM, user->service_name,
|
---|
[593e023] | 245 | "/loc", "--msg", "--", APP_SHELL, NULL);
|
---|
[21a9869] | 246 | if (rc != EOK) {
|
---|
[593e023] | 247 | telnet_user_error(user, "Spawning `%s %s /loc --msg -- %s' "
|
---|
| 248 | "failed: %s.", APP_GETTERM, user->service_name, APP_SHELL,
|
---|
| 249 | str_error(rc));
|
---|
[261bbdc] | 250 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 251 | user->task_finished = true;
|
---|
[5d94b16c] | 252 | user->srvs.aborted = true;
|
---|
[787b65b] | 253 | fibril_condvar_signal(&user->refcount_cv);
|
---|
[261bbdc] | 254 | fibril_mutex_unlock(&user->guard);
|
---|
[21a9869] | 255 | return EOK;
|
---|
| 256 | }
|
---|
| 257 |
|
---|
[261bbdc] | 258 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 259 | user->task_id = task;
|
---|
[261bbdc] | 260 | fibril_mutex_unlock(&user->guard);
|
---|
[1870d81] | 261 |
|
---|
[21a9869] | 262 | task_exit_t task_exit;
|
---|
| 263 | int task_retval;
|
---|
[1c635d6] | 264 | task_wait(&wait, &task_exit, &task_retval);
|
---|
[d545d03] | 265 | telnet_user_log(user, "%s terminated %s, exit code %d.", APP_GETTERM,
|
---|
| 266 | task_exit == TASK_EXIT_NORMAL ? "normally" : "unexpectedly",
|
---|
| 267 | task_retval);
|
---|
[21a9869] | 268 |
|
---|
[7c2bb2c] | 269 | /* Announce destruction. */
|
---|
[261bbdc] | 270 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 271 | user->task_finished = true;
|
---|
[5d94b16c] | 272 | user->srvs.aborted = true;
|
---|
[787b65b] | 273 | fibril_condvar_signal(&user->refcount_cv);
|
---|
[261bbdc] | 274 | fibril_mutex_unlock(&user->guard);
|
---|
[7c2bb2c] | 275 |
|
---|
| 276 | return EOK;
|
---|
| 277 | }
|
---|
| 278 |
|
---|
[5923cf82] | 279 | /** Tell whether given user can be destroyed (has no active clients).
|
---|
| 280 | *
|
---|
| 281 | * @param user The telnet user in question.
|
---|
| 282 | */
|
---|
[787b65b] | 283 | static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
|
---|
[1870d81] | 284 | {
|
---|
[787b65b] | 285 | return user->task_finished && user->socket_closed &&
|
---|
| 286 | (user->locsrv_connection_count == 0);
|
---|
[1870d81] | 287 | }
|
---|
[7c2bb2c] | 288 |
|
---|
[d6ff08a0] | 289 | /** Handle network connection.
|
---|
[5923cf82] | 290 | *
|
---|
[d6ff08a0] | 291 | * @param lst Listener
|
---|
| 292 | * @param conn Connection
|
---|
[5923cf82] | 293 | */
|
---|
[d6ff08a0] | 294 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
|
---|
[7c2bb2c] | 295 | {
|
---|
[d6ff08a0] | 296 | telnet_user_t *user = telnet_user_create(conn);
|
---|
| 297 | assert(user);
|
---|
| 298 |
|
---|
| 299 | con_srvs_init(&user->srvs);
|
---|
| 300 | user->srvs.ops = &con_ops;
|
---|
| 301 | user->srvs.sarg = user;
|
---|
| 302 | user->srvs.abort_timeout = 1000;
|
---|
| 303 |
|
---|
| 304 | telnet_user_add(user);
|
---|
[7c2bb2c] | 305 |
|
---|
[4c6fd56] | 306 | errno_t rc = loc_service_register(remcons_srv, user->service_name,
|
---|
| 307 | &user->service_id);
|
---|
[6f7cd5d] | 308 | if (rc != EOK) {
|
---|
[d545d03] | 309 | telnet_user_error(user, "Unable to register %s with loc: %s.",
|
---|
| 310 | user->service_name, str_error(rc));
|
---|
[d6ff08a0] | 311 | return;
|
---|
[6f7cd5d] | 312 | }
|
---|
[d545d03] | 313 |
|
---|
| 314 | telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
|
---|
| 315 | user->service_name, user->service_id);
|
---|
[d6ff08a0] | 316 |
|
---|
[787b65b] | 317 | fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
|
---|
[7c2bb2c] | 318 | assert(spawn_fibril);
|
---|
| 319 | fibril_add_ready(spawn_fibril);
|
---|
[d6ff08a0] | 320 |
|
---|
[6f7cd5d] | 321 | /* Wait for all clients to exit. */
|
---|
[261bbdc] | 322 | fibril_mutex_lock(&user->guard);
|
---|
[787b65b] | 323 | while (!user_can_be_destroyed_no_lock(user)) {
|
---|
| 324 | if (user->task_finished) {
|
---|
[fab2746] | 325 | user->conn = NULL;
|
---|
[787b65b] | 326 | user->socket_closed = true;
|
---|
[5d94b16c] | 327 | user->srvs.aborted = true;
|
---|
[1870d81] | 328 | continue;
|
---|
[787b65b] | 329 | } else if (user->socket_closed) {
|
---|
| 330 | if (user->task_id != 0) {
|
---|
| 331 | task_kill(user->task_id);
|
---|
[1870d81] | 332 | }
|
---|
| 333 | }
|
---|
[261bbdc] | 334 | fibril_condvar_wait_timeout(&user->refcount_cv, &user->guard, 1000);
|
---|
[6f7cd5d] | 335 | }
|
---|
[261bbdc] | 336 | fibril_mutex_unlock(&user->guard);
|
---|
[d6ff08a0] | 337 |
|
---|
[4c6fd56] | 338 | rc = loc_service_unregister(remcons_srv, user->service_id);
|
---|
[7c2bb2c] | 339 | if (rc != EOK) {
|
---|
[d545d03] | 340 | telnet_user_error(user,
|
---|
| 341 | "Unable to unregister %s from loc: %s (ignored).",
|
---|
| 342 | user->service_name, str_error(rc));
|
---|
[7c2bb2c] | 343 | }
|
---|
| 344 |
|
---|
[d545d03] | 345 | telnet_user_log(user, "Destroying...");
|
---|
[787b65b] | 346 | telnet_user_destroy(user);
|
---|
[fab2746] | 347 | }
|
---|
| 348 |
|
---|
[21a9869] | 349 | int main(int argc, char *argv[])
|
---|
| 350 | {
|
---|
[b7fd2a0] | 351 | errno_t rc;
|
---|
[fab2746] | 352 | tcp_listener_t *lst;
|
---|
| 353 | tcp_t *tcp;
|
---|
| 354 | inet_ep_t ep;
|
---|
| 355 |
|
---|
[b688fd8] | 356 | async_set_fallback_port_handler(client_connection, NULL);
|
---|
[4c6fd56] | 357 | rc = loc_server_register(NAME, &remcons_srv);
|
---|
[3123d2a] | 358 | if (rc != EOK) {
|
---|
| 359 | fprintf(stderr, "%s: Unable to register server\n", NAME);
|
---|
| 360 | return rc;
|
---|
[21a9869] | 361 | }
|
---|
| 362 |
|
---|
[fab2746] | 363 | rc = tcp_create(&tcp);
|
---|
[d6ff08a0] | 364 | if (rc != EOK) {
|
---|
| 365 | fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
|
---|
[fab2746] | 366 | return rc;
|
---|
[21a9869] | 367 | }
|
---|
| 368 |
|
---|
[fab2746] | 369 | inet_ep_init(&ep);
|
---|
[bf7587b0] | 370 | ep.port = 2223;
|
---|
[21a9869] | 371 |
|
---|
[fab2746] | 372 | rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
|
---|
| 373 | &lst);
|
---|
[21a9869] | 374 | if (rc != EOK) {
|
---|
[fab2746] | 375 | fprintf(stderr, "%s: Error creating listener.\n", NAME);
|
---|
| 376 | return rc;
|
---|
[21a9869] | 377 | }
|
---|
| 378 |
|
---|
| 379 | printf("%s: HelenOS Remote console service\n", NAME);
|
---|
[6d5e378] | 380 | task_retval(0);
|
---|
[fab2746] | 381 | async_manager();
|
---|
[21a9869] | 382 |
|
---|
[fab2746] | 383 | /* Not reached */
|
---|
[21a9869] | 384 | return 0;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | /** @}
|
---|
| 388 | */
|
---|