blob: c63677a763ad0856ddd316ec319bd97e7e70bc80 [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));
William A. Kennington IIIff569832019-07-24 23:07:15 -070093 if (rc < 0)
94 return PROCESS_ERR;
Andrew Jefferya72711a2023-04-18 18:19:41 +093095 out_buf = &buf[i + 1];
William A. Kennington IIIff569832019-07-24 23:07:15 -070096 break;
97 case '\r':
98 esc_state->state = '\r';
99 break;
100 default:
101 esc_state->state = '\0';
102 }
103 }
104
Andrew Jefferya72711a2023-04-18 18:19:41 +0930105 rc = write_buf_to_fd(client->console_sd, out_buf,
106 len - (out_buf - buf));
William A. Kennington IIIff569832019-07-24 23:07:15 -0700107 return rc < 0 ? PROCESS_ERR : PROCESS_OK;
108}
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800109
Andrew Jefferya72711a2023-04-18 18:19:41 +0930110static enum process_rc process_str_tty(struct console_client *client,
111 const uint8_t *buf, size_t len)
William A. Kennington III15691c82019-07-24 23:11:18 -0700112{
113 struct str_esc_state *esc_state = &client->esc_state.str;
114 enum process_rc prc = PROCESS_OK;
115 size_t i;
116
117 for (i = 0; i < len; ++i) {
118 if (buf[i] == esc_state->str[esc_state->pos])
119 esc_state->pos++;
120 else
121 esc_state->pos = 0;
122
123 if (esc_state->str[esc_state->pos] == '\0') {
William A. Kennington III8a154352019-07-24 23:01:47 -0700124 prc = PROCESS_ESC;
William A. Kennington III5b16dc82019-07-26 01:18:05 -0700125 ++i;
William A. Kennington III15691c82019-07-24 23:11:18 -0700126 break;
127 }
128 }
129
130 if (write_buf_to_fd(client->console_sd, buf, i) < 0)
131 return PROCESS_ERR;
132 return prc;
133}
134
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800135static enum process_rc process_tty(struct console_client *client)
136{
William A. Kennington IIIff569832019-07-24 23:07:15 -0700137 uint8_t buf[4096];
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800138 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800139
140 len = read(client->fd_in, buf, sizeof(buf));
141 if (len < 0)
142 return PROCESS_ERR;
143 if (len == 0)
144 return PROCESS_EXIT;
145
Andrew Jefferya72711a2023-04-18 18:19:41 +0930146 switch (client->esc_type) {
William A. Kennington IIIff569832019-07-24 23:07:15 -0700147 case ESC_TYPE_SSH:
148 return process_ssh_tty(client, buf, len);
William A. Kennington III15691c82019-07-24 23:11:18 -0700149 case ESC_TYPE_STR:
150 return process_str_tty(client, buf, len);
William A. Kennington IIIff569832019-07-24 23:07:15 -0700151 default:
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800152 return PROCESS_ERR;
William A. Kennington IIIff569832019-07-24 23:07:15 -0700153 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800154}
155
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800156static int process_console(struct console_client *client)
157{
158 uint8_t buf[4096];
Andrew Jeffery5c359cc2023-04-18 22:50:07 +0930159 ssize_t len;
160 int rc;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800161
162 len = read(client->console_sd, buf, sizeof(buf));
163 if (len < 0) {
164 warn("Can't read from server");
165 return PROCESS_ERR;
166 }
167 if (len == 0) {
168 fprintf(stderr, "Connection closed\n");
169 return PROCESS_EXIT;
170 }
171
172 rc = write_buf_to_fd(client->fd_out, buf, len);
173 return rc ? PROCESS_ERR : PROCESS_OK;
174}
175
176/*
177 * Setup our local file descriptors for IO: use stdin/stdout, and if we're on a
178 * TTY, put it in canonical mode
179 */
180static int client_tty_init(struct console_client *client)
181{
182 struct termios termios;
183 int rc;
184
185 client->fd_in = STDIN_FILENO;
186 client->fd_out = STDOUT_FILENO;
187 client->is_tty = isatty(client->fd_in);
188
189 if (!client->is_tty)
190 return 0;
191
192 rc = tcgetattr(client->fd_in, &termios);
193 if (rc) {
194 warn("Can't get terminal attributes for console");
195 return -1;
196 }
197 memcpy(&client->orig_termios, &termios, sizeof(client->orig_termios));
198 cfmakeraw(&termios);
199
200 rc = tcsetattr(client->fd_in, TCSANOW, &termios);
201 if (rc) {
202 warn("Can't set terminal attributes for console");
203 return -1;
204 }
205
206 return 0;
207}
208
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030209static int client_init(struct console_client *client, const char *socket_id)
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800210{
211 struct sockaddr_un addr;
Andrew Jeffery6ed0e4e2020-02-21 14:35:39 +1030212 socket_path_t path;
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030213 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800214 int rc;
215
216 client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0);
217 if (!client->console_sd) {
218 warn("Can't open socket");
219 return -1;
220 }
221
222 memset(&addr, 0, sizeof(addr));
223 addr.sun_family = AF_UNIX;
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030224 len = console_socket_path(&addr, socket_id);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030225 if (len < 0) {
226 if (errno)
227 warn("Failed to configure socket: %s", strerror(errno));
228 else
229 warn("Socket name length exceeds buffer limits");
230 goto cleanup;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800231 }
232
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030233 rc = connect(client->console_sd, (struct sockaddr *)&addr,
Andrew Jefferya72711a2023-04-18 18:19:41 +0930234 sizeof(addr) - sizeof(addr.sun_path) + len);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030235 if (!rc)
236 return 0;
237
Andrew Jeffery6ed0e4e2020-02-21 14:35:39 +1030238 console_socket_path_readable(&addr, len, path);
239 warn("Can't connect to console server '@%s'", path);
Andrew Jeffery5e7c0782020-02-10 12:12:36 +1030240cleanup:
241 close(client->console_sd);
242 return -1;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800243}
244
245static void client_fini(struct console_client *client)
246{
247 if (client->is_tty)
248 tcsetattr(client->fd_in, TCSANOW, &client->orig_termios);
249 close(client->console_sd);
250}
251
William A. Kennington III15691c82019-07-24 23:11:18 -0700252int main(int argc, char *argv[])
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800253{
254 struct console_client _client, *client;
255 struct pollfd pollfds[2];
William A. Kennington III8a154352019-07-24 23:01:47 -0700256 enum process_rc prc = PROCESS_OK;
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030257 const char *config_path = NULL;
258 struct config *config = NULL;
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030259 const char *socket_id = NULL;
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030260 const uint8_t *esc = NULL;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800261 int rc;
262
263 client = &_client;
264 memset(client, 0, sizeof(*client));
William A. Kennington IIIff569832019-07-24 23:07:15 -0700265 client->esc_type = ESC_TYPE_SSH;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800266
William A. Kennington III15691c82019-07-24 23:11:18 -0700267 for (;;) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030268 rc = getopt(argc, argv, "c:e:i:");
William A. Kennington III15691c82019-07-24 23:11:18 -0700269 if (rc == -1)
270 break;
271
272 switch (rc) {
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030273 case 'c':
274 if (optarg[0] == '\0') {
275 fprintf(stderr, "Config str cannot be empty\n");
276 return EXIT_FAILURE;
277 }
278 config_path = optarg;
279 break;
William A. Kennington III15691c82019-07-24 23:11:18 -0700280 case 'e':
281 if (optarg[0] == '\0') {
282 fprintf(stderr, "Escape str cannot be empty\n");
283 return EXIT_FAILURE;
284 }
Andrew Jefferya72711a2023-04-18 18:19:41 +0930285 esc = (const uint8_t *)optarg;
William A. Kennington III15691c82019-07-24 23:11:18 -0700286 break;
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030287 case 'i':
288 if (optarg[0] == '\0') {
Andrew Jefferya72711a2023-04-18 18:19:41 +0930289 fprintf(stderr,
290 "Socket ID str cannot be empty\n");
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030291 return EXIT_FAILURE;
292 }
293 socket_id = optarg;
294 break;
William A. Kennington III15691c82019-07-24 23:11:18 -0700295 default:
296 fprintf(stderr,
297 "Usage: %s "
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030298 "[-e <escape sequence>]"
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030299 "[-i <socket ID>]"
300 "[-c <config>]\n",
William A. Kennington III15691c82019-07-24 23:11:18 -0700301 argv[0]);
302 return EXIT_FAILURE;
303 }
304 }
305
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030306 if (config_path) {
307 config = config_init(config_path);
308 if (!config) {
309 warnx("Can't read configuration, exiting.");
310 return EXIT_FAILURE;
311 }
312
313 if (!esc)
Andrew Jefferya72711a2023-04-18 18:19:41 +0930314 esc = (const uint8_t *)config_get_value(
315 config, "escape-sequence");
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030316
317 if (!socket_id)
318 socket_id = config_get_value(config, "socket-id");
319 }
320
321 if (esc) {
322 client->esc_type = ESC_TYPE_STR;
323 client->esc_state.str.str = esc;
324 }
325
Andrew Jefferyddf2ab72020-02-10 12:36:09 +1030326 rc = client_init(client, socket_id);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800327 if (rc)
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030328 goto out_config_fini;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800329
330 rc = client_tty_init(client);
331 if (rc)
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030332 goto out_client_fini;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800333
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800334 for (;;) {
335 pollfds[0].fd = client->fd_in;
336 pollfds[0].events = POLLIN;
337 pollfds[1].fd = client->console_sd;
338 pollfds[1].events = POLLIN;
339
340 rc = poll(pollfds, 2, -1);
341 if (rc < 0) {
342 warn("Poll failure");
343 break;
344 }
345
346 if (pollfds[0].revents)
347 prc = process_tty(client);
348
349 if (prc == PROCESS_OK && pollfds[1].revents)
350 prc = process_console(client);
351
352 rc = (prc == PROCESS_ERR) ? -1 : 0;
353 if (prc != PROCESS_OK)
354 break;
355 }
356
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030357out_client_fini:
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800358 client_fini(client);
Andrew Jeffery71e7a242020-02-12 22:58:16 +1030359
360out_config_fini:
361 if (config_path)
362 config_fini(config);
363
William A. Kennington III8a154352019-07-24 23:01:47 -0700364 if (prc == PROCESS_ESC)
365 return EXIT_ESCAPE;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800366 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
367}