Jeremy Kerr | 9326d77 | 2016-03-17 17:15:02 +0800 | [diff] [blame] | 1 | /** |
| 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 Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 16 | |
| 17 | #include <err.h> |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 18 | #include <getopt.h> |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 19 | #include <stdbool.h> |
| 20 | #include <stdint.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <termios.h> |
| 25 | #include <unistd.h> |
| 26 | |
| 27 | #include <sys/socket.h> |
| 28 | #include <sys/un.h> |
| 29 | |
| 30 | #include "console-server.h" |
| 31 | |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 32 | #define EXIT_ESCAPE 2 |
| 33 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 34 | enum process_rc { |
| 35 | PROCESS_OK = 0, |
| 36 | PROCESS_ERR, |
| 37 | PROCESS_EXIT, |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 38 | PROCESS_ESC, |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 39 | }; |
| 40 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 41 | enum esc_type { |
| 42 | ESC_TYPE_SSH, |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 43 | ESC_TYPE_STR, |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct ssh_esc_state { |
| 47 | uint8_t state; |
| 48 | }; |
| 49 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 50 | struct str_esc_state { |
| 51 | const uint8_t *str; |
| 52 | size_t pos; |
| 53 | }; |
| 54 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 55 | struct console_client { |
| 56 | int console_sd; |
| 57 | int fd_in; |
| 58 | int fd_out; |
| 59 | bool is_tty; |
| 60 | struct termios orig_termios; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 61 | enum esc_type esc_type; |
| 62 | union { |
| 63 | struct ssh_esc_state ssh; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 64 | struct str_esc_state str; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 65 | } esc_state; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 66 | }; |
| 67 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 68 | static enum process_rc process_ssh_tty( |
| 69 | struct console_client *client, const uint8_t *buf, size_t len) |
| 70 | { |
| 71 | struct ssh_esc_state *esc_state = &client->esc_state.ssh; |
| 72 | const uint8_t *out_buf = buf; |
| 73 | int rc; |
| 74 | |
| 75 | for (size_t i = 0; i < len; ++i) { |
| 76 | switch (buf[i]) |
| 77 | { |
| 78 | case '.': |
| 79 | if (esc_state->state != '~') { |
| 80 | esc_state->state = '\0'; |
| 81 | break; |
| 82 | } |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 83 | return PROCESS_ESC; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 84 | 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 */ |
| 91 | rc = write_buf_to_fd( |
| 92 | client->console_sd, out_buf, i-(out_buf-buf)); |
| 93 | if (rc < 0) |
| 94 | return PROCESS_ERR; |
| 95 | out_buf = &buf[i+1]; |
| 96 | break; |
| 97 | case '\r': |
| 98 | esc_state->state = '\r'; |
| 99 | break; |
| 100 | default: |
| 101 | esc_state->state = '\0'; |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | rc = write_buf_to_fd(client->console_sd, out_buf, len-(out_buf-buf)); |
| 106 | return rc < 0 ? PROCESS_ERR : PROCESS_OK; |
| 107 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 108 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 109 | static enum process_rc process_str_tty( |
| 110 | struct console_client *client, const uint8_t *buf, size_t len) |
| 111 | { |
| 112 | struct str_esc_state *esc_state = &client->esc_state.str; |
| 113 | enum process_rc prc = PROCESS_OK; |
| 114 | size_t i; |
| 115 | |
| 116 | for (i = 0; i < len; ++i) { |
| 117 | if (buf[i] == esc_state->str[esc_state->pos]) |
| 118 | esc_state->pos++; |
| 119 | else |
| 120 | esc_state->pos = 0; |
| 121 | |
| 122 | if (esc_state->str[esc_state->pos] == '\0') { |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 123 | prc = PROCESS_ESC; |
William A. Kennington III | 5b16dc8 | 2019-07-26 01:18:05 -0700 | [diff] [blame] | 124 | ++i; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 125 | break; |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | if (write_buf_to_fd(client->console_sd, buf, i) < 0) |
| 130 | return PROCESS_ERR; |
| 131 | return prc; |
| 132 | } |
| 133 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 134 | static enum process_rc process_tty(struct console_client *client) |
| 135 | { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 136 | uint8_t buf[4096]; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 137 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 138 | |
| 139 | len = read(client->fd_in, buf, sizeof(buf)); |
| 140 | if (len < 0) |
| 141 | return PROCESS_ERR; |
| 142 | if (len == 0) |
| 143 | return PROCESS_EXIT; |
| 144 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 145 | switch (client->esc_type) |
| 146 | { |
| 147 | case ESC_TYPE_SSH: |
| 148 | return process_ssh_tty(client, buf, len); |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 149 | case ESC_TYPE_STR: |
| 150 | return process_str_tty(client, buf, len); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 151 | default: |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 152 | return PROCESS_ERR; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 153 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
| 157 | static int process_console(struct console_client *client) |
| 158 | { |
| 159 | uint8_t buf[4096]; |
| 160 | int len, rc; |
| 161 | |
| 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 | */ |
| 180 | static 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 | |
| 209 | static int client_init(struct console_client *client) |
| 210 | { |
| 211 | struct sockaddr_un addr; |
| 212 | int rc; |
| 213 | |
| 214 | client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 215 | if (!client->console_sd) { |
| 216 | warn("Can't open socket"); |
| 217 | return -1; |
| 218 | } |
| 219 | |
| 220 | memset(&addr, 0, sizeof(addr)); |
| 221 | addr.sun_family = AF_UNIX; |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 222 | memcpy(&addr.sun_path, &console_socket_path, console_socket_path_len); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 223 | |
| 224 | rc = connect(client->console_sd, (struct sockaddr *)&addr, |
Vernon Mauery | fcf8541 | 2018-12-18 15:16:38 -0800 | [diff] [blame] | 225 | sizeof(addr) - sizeof(addr.sun_path) + console_socket_path_len); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 226 | if (rc) { |
| 227 | warn("Can't connect to console server"); |
| 228 | close(client->console_sd); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static void client_fini(struct console_client *client) |
| 236 | { |
| 237 | if (client->is_tty) |
| 238 | tcsetattr(client->fd_in, TCSANOW, &client->orig_termios); |
| 239 | close(client->console_sd); |
| 240 | } |
| 241 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 242 | int main(int argc, char *argv[]) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 243 | { |
| 244 | struct console_client _client, *client; |
| 245 | struct pollfd pollfds[2]; |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 246 | enum process_rc prc = PROCESS_OK; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 247 | int rc; |
| 248 | |
| 249 | client = &_client; |
| 250 | memset(client, 0, sizeof(*client)); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 251 | client->esc_type = ESC_TYPE_SSH; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 252 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame] | 253 | for (;;) { |
| 254 | rc = getopt(argc, argv, "e:"); |
| 255 | if (rc == -1) |
| 256 | break; |
| 257 | |
| 258 | switch (rc) { |
| 259 | case 'e': |
| 260 | if (optarg[0] == '\0') { |
| 261 | fprintf(stderr, "Escape str cannot be empty\n"); |
| 262 | return EXIT_FAILURE; |
| 263 | } |
| 264 | client->esc_type = ESC_TYPE_STR; |
| 265 | client->esc_state.str.str = (const uint8_t*)optarg; |
| 266 | break; |
| 267 | default: |
| 268 | fprintf(stderr, |
| 269 | "Usage: %s " |
| 270 | "[-e <escape sequence>]\n", |
| 271 | argv[0]); |
| 272 | return EXIT_FAILURE; |
| 273 | } |
| 274 | } |
| 275 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 276 | rc = client_init(client); |
| 277 | if (rc) |
| 278 | return EXIT_FAILURE; |
| 279 | |
| 280 | rc = client_tty_init(client); |
| 281 | if (rc) |
| 282 | goto out_fini; |
| 283 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 284 | for (;;) { |
| 285 | pollfds[0].fd = client->fd_in; |
| 286 | pollfds[0].events = POLLIN; |
| 287 | pollfds[1].fd = client->console_sd; |
| 288 | pollfds[1].events = POLLIN; |
| 289 | |
| 290 | rc = poll(pollfds, 2, -1); |
| 291 | if (rc < 0) { |
| 292 | warn("Poll failure"); |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | if (pollfds[0].revents) |
| 297 | prc = process_tty(client); |
| 298 | |
| 299 | if (prc == PROCESS_OK && pollfds[1].revents) |
| 300 | prc = process_console(client); |
| 301 | |
| 302 | rc = (prc == PROCESS_ERR) ? -1 : 0; |
| 303 | if (prc != PROCESS_OK) |
| 304 | break; |
| 305 | } |
| 306 | |
| 307 | out_fini: |
| 308 | client_fini(client); |
William A. Kennington III | 8a15435 | 2019-07-24 23:01:47 -0700 | [diff] [blame] | 309 | if (prc == PROCESS_ESC) |
| 310 | return EXIT_ESCAPE; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 311 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 312 | } |
| 313 | |