1 | /*
|
---|
2 | * Copyright (c) 2023 Jiri Svoboda
|
---|
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>
|
---|
38 | #include <io/con_srv.h>
|
---|
39 | #include <stdio.h>
|
---|
40 | #include <stdlib.h>
|
---|
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>
|
---|
47 | #include <inet/addr.h>
|
---|
48 | #include <inet/endpoint.h>
|
---|
49 | #include <inet/tcp.h>
|
---|
50 | #include <io/console.h>
|
---|
51 | #include <inttypes.h>
|
---|
52 | #include <str.h>
|
---|
53 | #include "telnet.h"
|
---|
54 | #include "user.h"
|
---|
55 |
|
---|
56 | #define APP_GETTERM "/app/getterm"
|
---|
57 | #define APP_SHELL "/app/bdsh"
|
---|
58 |
|
---|
59 | /** Telnet commands to force character mode
|
---|
60 | * (redundant to be on the safe side).
|
---|
61 | * See
|
---|
62 | * http://stackoverflow.com/questions/273261/force-telnet-user-into-character-mode
|
---|
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 | };
|
---|
70 |
|
---|
71 | static const size_t telnet_force_character_mode_command_count =
|
---|
72 | sizeof(telnet_force_character_mode_command) / sizeof(telnet_cmd_t);
|
---|
73 |
|
---|
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 *);
|
---|
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);
|
---|
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 *);
|
---|
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 | };
|
---|
102 |
|
---|
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 |
|
---|
113 | static loc_srv_t *remcons_srv;
|
---|
114 |
|
---|
115 | static telnet_user_t *srv_to_user(con_srv_t *srv)
|
---|
116 | {
|
---|
117 | return srv->srvs->sarg;
|
---|
118 | }
|
---|
119 |
|
---|
120 | static errno_t remcons_open(con_srvs_t *srvs, con_srv_t *srv)
|
---|
121 | {
|
---|
122 | telnet_user_t *user = srv_to_user(srv);
|
---|
123 |
|
---|
124 | telnet_user_log(user, "New client connected (%p).", srv);
|
---|
125 |
|
---|
126 | /* Force character mode. */
|
---|
127 | (void) tcp_conn_send(user->conn, (void *)telnet_force_character_mode_command,
|
---|
128 | telnet_force_character_mode_command_count);
|
---|
129 |
|
---|
130 | return EOK;
|
---|
131 | }
|
---|
132 |
|
---|
133 | static errno_t remcons_close(con_srv_t *srv)
|
---|
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 |
|
---|
143 | static errno_t remcons_write(con_srv_t *srv, void *data, size_t size, size_t *nwritten)
|
---|
144 | {
|
---|
145 | telnet_user_t *user = srv_to_user(srv);
|
---|
146 | errno_t rc;
|
---|
147 |
|
---|
148 | rc = telnet_user_send_data(user, data, size);
|
---|
149 | if (rc != EOK)
|
---|
150 | return rc;
|
---|
151 |
|
---|
152 | *nwritten = size;
|
---|
153 | return EOK;
|
---|
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 |
|
---|
173 | static errno_t remcons_get_pos(con_srv_t *srv, sysarg_t *col, sysarg_t *row)
|
---|
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 |
|
---|
183 | static errno_t remcons_get_size(con_srv_t *srv, sysarg_t *cols, sysarg_t *rows)
|
---|
184 | {
|
---|
185 | (void) srv;
|
---|
186 |
|
---|
187 | *cols = 100;
|
---|
188 | *rows = 1;
|
---|
189 |
|
---|
190 | return EOK;
|
---|
191 | }
|
---|
192 |
|
---|
193 | static errno_t remcons_get_color_cap(con_srv_t *srv, console_caps_t *ccaps)
|
---|
194 | {
|
---|
195 | (void) srv;
|
---|
196 | *ccaps = CONSOLE_CAP_NONE;
|
---|
197 |
|
---|
198 | return EOK;
|
---|
199 | }
|
---|
200 |
|
---|
201 | static errno_t remcons_get_event(con_srv_t *srv, cons_event_t *event)
|
---|
202 | {
|
---|
203 | telnet_user_t *user = srv_to_user(srv);
|
---|
204 | kbd_event_t kevent;
|
---|
205 | errno_t rc;
|
---|
206 |
|
---|
207 | rc = telnet_user_get_next_keyboard_event(user, &kevent);
|
---|
208 | if (rc != EOK) {
|
---|
209 | /* XXX What? */
|
---|
210 | memset(event, 0, sizeof(*event));
|
---|
211 | return EOK;
|
---|
212 | }
|
---|
213 |
|
---|
214 | event->type = CEV_KEY;
|
---|
215 | event->ev.key = kevent;
|
---|
216 |
|
---|
217 | return EOK;
|
---|
218 | }
|
---|
219 |
|
---|
220 | /** Callback when client connects to a telnet terminal. */
|
---|
221 | static void client_connection(ipc_call_t *icall, void *arg)
|
---|
222 | {
|
---|
223 | /* Find the user. */
|
---|
224 | telnet_user_t *user = telnet_user_get_for_client_connection(ipc_get_arg2(icall));
|
---|
225 | if (user == NULL) {
|
---|
226 | async_answer_0(icall, ENOENT);
|
---|
227 | return;
|
---|
228 | }
|
---|
229 |
|
---|
230 | /* Handle messages. */
|
---|
231 | con_conn(icall, &user->srvs);
|
---|
232 | }
|
---|
233 |
|
---|
234 | /** Fibril for spawning the task running after user connects.
|
---|
235 | *
|
---|
236 | * @param arg Corresponding @c telnet_user_t structure.
|
---|
237 | */
|
---|
238 | static errno_t spawn_task_fibril(void *arg)
|
---|
239 | {
|
---|
240 | telnet_user_t *user = arg;
|
---|
241 |
|
---|
242 | task_id_t task;
|
---|
243 | task_wait_t wait;
|
---|
244 | errno_t rc = task_spawnl(&task, &wait, APP_GETTERM, APP_GETTERM, user->service_name,
|
---|
245 | "/loc", "--msg", "--", APP_SHELL, NULL);
|
---|
246 | if (rc != EOK) {
|
---|
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));
|
---|
250 | fibril_mutex_lock(&user->guard);
|
---|
251 | user->task_finished = true;
|
---|
252 | user->srvs.aborted = true;
|
---|
253 | fibril_condvar_signal(&user->refcount_cv);
|
---|
254 | fibril_mutex_unlock(&user->guard);
|
---|
255 | return EOK;
|
---|
256 | }
|
---|
257 |
|
---|
258 | fibril_mutex_lock(&user->guard);
|
---|
259 | user->task_id = task;
|
---|
260 | fibril_mutex_unlock(&user->guard);
|
---|
261 |
|
---|
262 | task_exit_t task_exit;
|
---|
263 | int task_retval;
|
---|
264 | task_wait(&wait, &task_exit, &task_retval);
|
---|
265 | telnet_user_log(user, "%s terminated %s, exit code %d.", APP_GETTERM,
|
---|
266 | task_exit == TASK_EXIT_NORMAL ? "normally" : "unexpectedly",
|
---|
267 | task_retval);
|
---|
268 |
|
---|
269 | /* Announce destruction. */
|
---|
270 | fibril_mutex_lock(&user->guard);
|
---|
271 | user->task_finished = true;
|
---|
272 | user->srvs.aborted = true;
|
---|
273 | fibril_condvar_signal(&user->refcount_cv);
|
---|
274 | fibril_mutex_unlock(&user->guard);
|
---|
275 |
|
---|
276 | return EOK;
|
---|
277 | }
|
---|
278 |
|
---|
279 | /** Tell whether given user can be destroyed (has no active clients).
|
---|
280 | *
|
---|
281 | * @param user The telnet user in question.
|
---|
282 | */
|
---|
283 | static bool user_can_be_destroyed_no_lock(telnet_user_t *user)
|
---|
284 | {
|
---|
285 | return user->task_finished && user->socket_closed &&
|
---|
286 | (user->locsrv_connection_count == 0);
|
---|
287 | }
|
---|
288 |
|
---|
289 | /** Handle network connection.
|
---|
290 | *
|
---|
291 | * @param lst Listener
|
---|
292 | * @param conn Connection
|
---|
293 | */
|
---|
294 | static void remcons_new_conn(tcp_listener_t *lst, tcp_conn_t *conn)
|
---|
295 | {
|
---|
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);
|
---|
305 |
|
---|
306 | errno_t rc = loc_service_register(remcons_srv, user->service_name,
|
---|
307 | &user->service_id);
|
---|
308 | if (rc != EOK) {
|
---|
309 | telnet_user_error(user, "Unable to register %s with loc: %s.",
|
---|
310 | user->service_name, str_error(rc));
|
---|
311 | return;
|
---|
312 | }
|
---|
313 |
|
---|
314 | telnet_user_log(user, "Service %s registerd with id %" PRIun ".",
|
---|
315 | user->service_name, user->service_id);
|
---|
316 |
|
---|
317 | fid_t spawn_fibril = fibril_create(spawn_task_fibril, user);
|
---|
318 | assert(spawn_fibril);
|
---|
319 | fibril_add_ready(spawn_fibril);
|
---|
320 |
|
---|
321 | /* Wait for all clients to exit. */
|
---|
322 | fibril_mutex_lock(&user->guard);
|
---|
323 | while (!user_can_be_destroyed_no_lock(user)) {
|
---|
324 | if (user->task_finished) {
|
---|
325 | user->conn = NULL;
|
---|
326 | user->socket_closed = true;
|
---|
327 | user->srvs.aborted = true;
|
---|
328 | continue;
|
---|
329 | } else if (user->socket_closed) {
|
---|
330 | if (user->task_id != 0) {
|
---|
331 | task_kill(user->task_id);
|
---|
332 | }
|
---|
333 | }
|
---|
334 | fibril_condvar_wait_timeout(&user->refcount_cv, &user->guard, 1000);
|
---|
335 | }
|
---|
336 | fibril_mutex_unlock(&user->guard);
|
---|
337 |
|
---|
338 | rc = loc_service_unregister(remcons_srv, user->service_id);
|
---|
339 | if (rc != EOK) {
|
---|
340 | telnet_user_error(user,
|
---|
341 | "Unable to unregister %s from loc: %s (ignored).",
|
---|
342 | user->service_name, str_error(rc));
|
---|
343 | }
|
---|
344 |
|
---|
345 | telnet_user_log(user, "Destroying...");
|
---|
346 | telnet_user_destroy(user);
|
---|
347 | }
|
---|
348 |
|
---|
349 | int main(int argc, char *argv[])
|
---|
350 | {
|
---|
351 | errno_t rc;
|
---|
352 | tcp_listener_t *lst;
|
---|
353 | tcp_t *tcp;
|
---|
354 | inet_ep_t ep;
|
---|
355 |
|
---|
356 | async_set_fallback_port_handler(client_connection, NULL);
|
---|
357 | rc = loc_server_register(NAME, &remcons_srv);
|
---|
358 | if (rc != EOK) {
|
---|
359 | fprintf(stderr, "%s: Unable to register server\n", NAME);
|
---|
360 | return rc;
|
---|
361 | }
|
---|
362 |
|
---|
363 | rc = tcp_create(&tcp);
|
---|
364 | if (rc != EOK) {
|
---|
365 | fprintf(stderr, "%s: Error initializing TCP.\n", NAME);
|
---|
366 | return rc;
|
---|
367 | }
|
---|
368 |
|
---|
369 | inet_ep_init(&ep);
|
---|
370 | ep.port = 2223;
|
---|
371 |
|
---|
372 | rc = tcp_listener_create(tcp, &ep, &listen_cb, NULL, &conn_cb, NULL,
|
---|
373 | &lst);
|
---|
374 | if (rc != EOK) {
|
---|
375 | fprintf(stderr, "%s: Error creating listener.\n", NAME);
|
---|
376 | return rc;
|
---|
377 | }
|
---|
378 |
|
---|
379 | printf("%s: HelenOS Remote console service\n", NAME);
|
---|
380 | task_retval(0);
|
---|
381 | async_manager();
|
---|
382 |
|
---|
383 | /* Not reached */
|
---|
384 | return 0;
|
---|
385 | }
|
---|
386 |
|
---|
387 | /** @}
|
---|
388 | */
|
---|