blob: ea66b22f2be97b458baae15934a6fa7a782a19aa [file] [log] [blame]
Jeremy Kerr9326d772016-03-17 17:15:02 +08001/**
2 * Copyright © 2016 IBM Corporation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
Jeremy Kerr2bd05182016-03-10 16:59:43 +080016
17#include <err.h>
Andrew Jeffery5e7c0782020-02-10 12:12:36 +103018#include <errno.h>
William A. Kennington III15691c82019-07-24 23:11:18 -070019#include <getopt.h>
Jeremy Kerr2bd05182016-03-10 16:59:43 +080020#include <stdbool.h>
21#include <stdint.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <termios.h>
26#include <unistd.h>
27
28#include <sys/socket.h>
29#include <sys/un.h>
30
31#include "console-server.h"
32
William A. Kennington III8a154352019-07-24 23:01:47 -070033#define EXIT_ESCAPE 2
34
Jeremy Kerr2bd05182016-03-10 16:59:43 +080035enum process_rc {
36 PROCESS_OK = 0,
37 PROCESS_ERR,
38 PROCESS_EXIT,
William A. Kennington III8a154352019-07-24 23:01:47 -070039 PROCESS_ESC,
Jeremy Kerr2bd05182016-03-10 16:59:43 +080040};
41
William A. Kennington IIIff569832019-07-24 23:07:15 -070042enum esc_type {
43 ESC_TYPE_SSH,
William A. Kennington III15691c82019-07-24 23:11:18 -070044 ESC_TYPE_STR,
William A. Kennington IIIff569832019-07-24 23:07:15 -070045};
46
47struct ssh_esc_state {
48 uint8_t state;
49};
50
William A. Kennington III15691c82019-07-24 23:11:18 -070051struct str_esc_state {
52 const uint8_t *str;
53 size_t pos;
54};
55
Jeremy Kerr2bd05182016-03-10 16:59:43 +080056struct console_client {
Andrew Jefferya72711a2023-04-18 18:19:41 +093057 int console_sd;
58 int fd_in;
59 int fd_out;
60 bool is_tty;
61 struct termios orig_termios;
62 enum esc_type esc_type;
William A. Kennington IIIff569832019-07-24 23:07:15 -070063 union {
64 struct ssh_esc_state ssh;
William A. Kennington III15691c82019-07-24 23:11:18 -070065 struct str_esc_state str;
William A. Kennington IIIff569832019-07-24 23:07:15 -070066 } esc_state;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080067};
68
Andrew Jefferya72711a2023-04-18 18:19:41 +093069static enum process_rc process_ssh_tty(struct console_client *client,
70 const uint8_t *buf, size_t len)
William A. Kennington IIIff569832019-07-24 23:07:15 -070071{
72 struct ssh_esc_state *esc_state = &client->esc_state.ssh;
73 const uint8_t *out_buf = buf;
74 int rc;
75
76 for (size_t i = 0; i < len; ++i) {
Andrew Jefferya72711a2023-04-18 18:19:41 +093077 switch (buf[i]) {
William A. Kennington IIIff569832019-07-24 23:07:15 -070078 case '.':
79 if (esc_state->state != '~') {
80 esc_state->state = '\0';
81 break;
82 }
William A. Kennington III8a154352019-07-24 23:01:47 -070083 return PROCESS_ESC;
William A. Kennington IIIff569832019-07-24 23:07:15 -070084 case '~':
85 if (esc_state->state != '\r') {
86 esc_state->state = '\0';
87 break;
88 }
89 esc_state->state = '~';
90 /* We need to print everything to skip the tilde */
Andrew Jefferya72711a2023-04-18 18:19:41 +093091 rc = write_buf_to_fd(client->console_sd, out_buf,
92 i - (out_buf - buf));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093093 if (rc < 0) {
William A. Kennington IIIff569832019-07-24 23:07:15 -070094 return PROCESS_ERR;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +093095 }
Andrew Jefferya72711a2023-04-18 18:19:41 +093096 out_buf = &buf[i + 1];
William A. Kennington IIIff569832019-07-24 23:07:15 -070097 break;
98 case '\r':
99 esc_state->state = '\r';
100 break;
101 default:
102 esc_state->state = '\0';
103 }
104 }
105
Andrew Jefferya72711a2023-04-18 18:19:41 +0930106 rc = write_buf_to_fd(client->console_sd, out_buf,
107 len - (out_buf - buf));
William A. Kennington IIIff569832019-07-24 23:07:15 -0700108 return rc < 0 ? PROCESS_ERR : PROCESS_OK;
109}
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800110
Andrew Jefferya72711a2023-04-18 18:19:41 +0930111static enum process_rc process_str_tty(struct console_client *client,
112 const uint8_t *buf, size_t len)
William A. Kennington III15691c82019-07-24 23:11:18 -0700113{
114 struct str_esc_state *esc_state = &client->esc_state.str;
115 enum process_rc prc = PROCESS_OK;
116 size_t i;
117
118 for (i = 0; i < len; ++i) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930119 if (buf[i] == esc_state->str[esc_state->pos]) {
William A. Kennington III15691c82019-07-24 23:11:18 -0700120 esc_state->pos++;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930121 } else {
William A. Kennington III15691c82019-07-24 23:11:18 -0700122 esc_state->pos = 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930123 }
William A. Kennington III15691c82019-07-24 23:11:18 -0700124
125 if (esc_state->str[esc_state->pos] == '\0') {
William A. Kennington III8a154352019-07-24 23:01:47 -0700126 prc = PROCESS_ESC;
William A. Kennington III5b16dc82019-07-26 01:18:05 -0700127 ++i;
William A. Kennington III15691c82019-07-24 23:11:18 -0700128 break;
129 }
130 }
131
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930132 if (write_buf_to_fd(client->console_sd, buf, i) < 0) {
William A. Kennington III15691c82019-07-24 23:11:18 -0700133 return PROCESS_ERR;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930134 }
William A. Kennington III15691c82019-07-24 23:11:18 -0700135 return prc;
136}
137
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800138static enum process_rc process_tty(struct console_client *client)
139{
William A. Kennington IIIff569832019-07-24 23:07:15 -0700140 uint8_t buf[4096];
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800141 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800142
143 len = read(client->fd_in, buf, sizeof(buf));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930144 if (len < 0) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800145 return PROCESS_ERR;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930146 }
147 if (len == 0) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800148 return PROCESS_EXIT;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930149 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800150
Andrew Jefferya72711a2023-04-18 18:19:41 +0930151 switch (client->esc_type) {
William A. Kennington IIIff569832019-07-24 23:07:15 -0700152 case ESC_TYPE_SSH:
153 return process_ssh_tty(client, buf, len);
William A. Kennington III15691c82019-07-24 23:11:18 -0700154 case ESC_TYPE_STR:
155 return process_str_tty(client, buf, len);
William A. Kennington IIIff569832019-07-24 23:07:15 -0700156 default:
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800157 return PROCESS_ERR;
William A. Kennington IIIff569832019-07-24 23:07:15 -0700158 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800159}
160
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800161static int process_console(struct console_client *client)
162{
163 uint8_t buf[4096];
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930164 ssize_t len;
165 int rc;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800166
167 len = read(client->console_sd, buf, sizeof(buf));
168 if (len < 0) {
169 warn("Can't read from server");
170 return PROCESS_ERR;
171 }
172 if (len == 0) {
173 fprintf(stderr, "Connection closed\n");
174 return PROCESS_EXIT;
175 }
176
177 rc = write_buf_to_fd(client->fd_out, buf, len);
178 return rc ? PROCESS_ERR : PROCESS_OK;
179}
180
181/*
182 * Setup our local file descriptors for IO: use stdin/stdout, and if we're on a
183 * TTY, put it in canonical mode
184 */
185static int client_tty_init(struct console_client *client)
186{
187 struct termios termios;
188 int rc;
189
190 client->fd_in = STDIN_FILENO;
191 client->fd_out = STDOUT_FILENO;
192 client->is_tty = isatty(client->fd_in);
193
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930194 if (!client->is_tty) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800195 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930196 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800197
198 rc = tcgetattr(client->fd_in, &termios);
199 if (rc) {
200 warn("Can't get terminal attributes for console");
201 return -1;
202 }
203 memcpy(&client->orig_termios, &termios, sizeof(client->orig_termios));
204 cfmakeraw(&termios);
205
206 rc = tcsetattr(client->fd_in, TCSANOW, &termios);
207 if (rc) {
208 warn("Can't set terminal attributes for console");
209 return -1;
210 }
211
212 return 0;
213}
214
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030215static int client_init(struct console_client *client, const char *socket_id)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800216{
217 struct sockaddr_un addr;
Andrew Jeffery6ed0e4e2020-02-21 14:35:39 +1030218 socket_path_t path;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030219 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800220 int rc;
221
222 client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0);
223 if (!client->console_sd) {
224 warn("Can't open socket");
225 return -1;
226 }
227
228 memset(&addr, 0, sizeof(addr));
229 addr.sun_family = AF_UNIX;
Ninad Palsuleb14ca192023-03-31 09:49:00 -0500230 len = console_socket_path(addr.sun_path, socket_id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030231 if (len < 0) {
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930232 if (errno) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030233 warn("Failed to configure socket: %s", strerror(errno));
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930234 } else {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030235 warn("Socket name length exceeds buffer limits");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930236 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030237 goto cleanup;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800238 }
239
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030240 rc = connect(client->console_sd, (struct sockaddr *)&addr,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930241 sizeof(addr) - sizeof(addr.sun_path) + len);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930242 if (!rc) {
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030243 return 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930244 }
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030245
Andrew Jeffery6ed0e4e2020-02-21 14:35:39 +1030246 console_socket_path_readable(&addr, len, path);
247 warn("Can't connect to console server '@%s'", path);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030248cleanup:
249 close(client->console_sd);
250 return -1;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800251}
252
253static void client_fini(struct console_client *client)
254{
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930255 if (client->is_tty) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800256 tcsetattr(client->fd_in, TCSANOW, &client->orig_termios);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930257 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800258 close(client->console_sd);
259}
260
William A. Kennington III15691c82019-07-24 23:11:18 -0700261int main(int argc, char *argv[])
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800262{
Andrew Jefferyb70f8712023-04-19 12:53:34 +0930263 struct console_client _client;
264 struct console_client *client;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800265 struct pollfd pollfds[2];
William A. Kennington III8a154352019-07-24 23:01:47 -0700266 enum process_rc prc = PROCESS_OK;
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030267 const char *config_path = NULL;
268 struct config *config = NULL;
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030269 const char *socket_id = NULL;
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030270 const uint8_t *esc = NULL;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800271 int rc;
272
273 client = &_client;
274 memset(client, 0, sizeof(*client));
William A. Kennington IIIff569832019-07-24 23:07:15 -0700275 client->esc_type = ESC_TYPE_SSH;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800276
William A. Kennington III15691c82019-07-24 23:11:18 -0700277 for (;;) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030278 rc = getopt(argc, argv, "c:e:i:");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930279 if (rc == -1) {
William A. Kennington III15691c82019-07-24 23:11:18 -0700280 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930281 }
William A. Kennington III15691c82019-07-24 23:11:18 -0700282
283 switch (rc) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030284 case 'c':
285 if (optarg[0] == '\0') {
286 fprintf(stderr, "Config str cannot be empty\n");
287 return EXIT_FAILURE;
288 }
289 config_path = optarg;
290 break;
William A. Kennington III15691c82019-07-24 23:11:18 -0700291 case 'e':
292 if (optarg[0] == '\0') {
293 fprintf(stderr, "Escape str cannot be empty\n");
294 return EXIT_FAILURE;
295 }
Andrew Jefferya72711a2023-04-18 18:19:41 +0930296 esc = (const uint8_t *)optarg;
William A. Kennington III15691c82019-07-24 23:11:18 -0700297 break;
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030298 case 'i':
299 if (optarg[0] == '\0') {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930300 fprintf(stderr,
301 "Socket ID str cannot be empty\n");
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030302 return EXIT_FAILURE;
303 }
304 socket_id = optarg;
305 break;
William A. Kennington III15691c82019-07-24 23:11:18 -0700306 default:
307 fprintf(stderr,
308 "Usage: %s "
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030309 "[-e <escape sequence>]"
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030310 "[-i <socket ID>]"
311 "[-c <config>]\n",
William A. Kennington III15691c82019-07-24 23:11:18 -0700312 argv[0]);
313 return EXIT_FAILURE;
314 }
315 }
316
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030317 if (config_path) {
318 config = config_init(config_path);
319 if (!config) {
320 warnx("Can't read configuration, exiting.");
321 return EXIT_FAILURE;
322 }
323
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930324 if (!esc) {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930325 esc = (const uint8_t *)config_get_value(
326 config, "escape-sequence");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930327 }
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030328
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930329 if (!socket_id) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030330 socket_id = config_get_value(config, "socket-id");
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930331 }
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030332 }
333
334 if (esc) {
335 client->esc_type = ESC_TYPE_STR;
336 client->esc_state.str.str = esc;
337 }
338
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030339 rc = client_init(client, socket_id);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930340 if (rc) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030341 goto out_config_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930342 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800343
344 rc = client_tty_init(client);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930345 if (rc) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030346 goto out_client_fini;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930347 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800348
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800349 for (;;) {
350 pollfds[0].fd = client->fd_in;
351 pollfds[0].events = POLLIN;
352 pollfds[1].fd = client->console_sd;
353 pollfds[1].events = POLLIN;
354
355 rc = poll(pollfds, 2, -1);
356 if (rc < 0) {
357 warn("Poll failure");
358 break;
359 }
360
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930361 if (pollfds[0].revents) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800362 prc = process_tty(client);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930363 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800364
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930365 if (prc == PROCESS_OK && pollfds[1].revents) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800366 prc = process_console(client);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930367 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800368
369 rc = (prc == PROCESS_ERR) ? -1 : 0;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930370 if (prc != PROCESS_OK) {
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800371 break;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930372 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800373 }
374
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030375out_client_fini:
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800376 client_fini(client);
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030377
378out_config_fini:
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930379 if (config_path) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030380 config_fini(config);
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930381 }
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030382
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930383 if (prc == PROCESS_ESC) {
William A. Kennington III8a154352019-07-24 23:01:47 -0700384 return EXIT_ESCAPE;
Andrew Jeffery2834c5b2023-04-19 12:47:09 +0930385 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800386 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
387}