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 | |
| 32 | enum process_rc { |
| 33 | PROCESS_OK = 0, |
| 34 | PROCESS_ERR, |
| 35 | PROCESS_EXIT, |
| 36 | }; |
| 37 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 38 | enum esc_type { |
| 39 | ESC_TYPE_SSH, |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 40 | ESC_TYPE_STR, |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | struct ssh_esc_state { |
| 44 | uint8_t state; |
| 45 | }; |
| 46 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 47 | struct str_esc_state { |
| 48 | const uint8_t *str; |
| 49 | size_t pos; |
| 50 | }; |
| 51 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 52 | struct console_client { |
| 53 | int console_sd; |
| 54 | int fd_in; |
| 55 | int fd_out; |
| 56 | bool is_tty; |
| 57 | struct termios orig_termios; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 58 | enum esc_type esc_type; |
| 59 | union { |
| 60 | struct ssh_esc_state ssh; |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 61 | struct str_esc_state str; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 62 | } esc_state; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 63 | }; |
| 64 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 65 | static enum process_rc process_ssh_tty( |
| 66 | struct console_client *client, const uint8_t *buf, size_t len) |
| 67 | { |
| 68 | struct ssh_esc_state *esc_state = &client->esc_state.ssh; |
| 69 | const uint8_t *out_buf = buf; |
| 70 | int rc; |
| 71 | |
| 72 | for (size_t i = 0; i < len; ++i) { |
| 73 | switch (buf[i]) |
| 74 | { |
| 75 | case '.': |
| 76 | if (esc_state->state != '~') { |
| 77 | esc_state->state = '\0'; |
| 78 | break; |
| 79 | } |
| 80 | return PROCESS_EXIT; |
| 81 | case '~': |
| 82 | if (esc_state->state != '\r') { |
| 83 | esc_state->state = '\0'; |
| 84 | break; |
| 85 | } |
| 86 | esc_state->state = '~'; |
| 87 | /* We need to print everything to skip the tilde */ |
| 88 | rc = write_buf_to_fd( |
| 89 | client->console_sd, out_buf, i-(out_buf-buf)); |
| 90 | if (rc < 0) |
| 91 | return PROCESS_ERR; |
| 92 | out_buf = &buf[i+1]; |
| 93 | break; |
| 94 | case '\r': |
| 95 | esc_state->state = '\r'; |
| 96 | break; |
| 97 | default: |
| 98 | esc_state->state = '\0'; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | rc = write_buf_to_fd(client->console_sd, out_buf, len-(out_buf-buf)); |
| 103 | return rc < 0 ? PROCESS_ERR : PROCESS_OK; |
| 104 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 105 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 106 | static enum process_rc process_str_tty( |
| 107 | struct console_client *client, const uint8_t *buf, size_t len) |
| 108 | { |
| 109 | struct str_esc_state *esc_state = &client->esc_state.str; |
| 110 | enum process_rc prc = PROCESS_OK; |
| 111 | size_t i; |
| 112 | |
| 113 | for (i = 0; i < len; ++i) { |
| 114 | if (buf[i] == esc_state->str[esc_state->pos]) |
| 115 | esc_state->pos++; |
| 116 | else |
| 117 | esc_state->pos = 0; |
| 118 | |
| 119 | if (esc_state->str[esc_state->pos] == '\0') { |
| 120 | prc = PROCESS_EXIT; |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (write_buf_to_fd(client->console_sd, buf, i) < 0) |
| 126 | return PROCESS_ERR; |
| 127 | return prc; |
| 128 | } |
| 129 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 130 | static enum process_rc process_tty(struct console_client *client) |
| 131 | { |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 132 | uint8_t buf[4096]; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 133 | ssize_t len; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 134 | |
| 135 | len = read(client->fd_in, buf, sizeof(buf)); |
| 136 | if (len < 0) |
| 137 | return PROCESS_ERR; |
| 138 | if (len == 0) |
| 139 | return PROCESS_EXIT; |
| 140 | |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 141 | switch (client->esc_type) |
| 142 | { |
| 143 | case ESC_TYPE_SSH: |
| 144 | return process_ssh_tty(client, buf, len); |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 145 | case ESC_TYPE_STR: |
| 146 | return process_str_tty(client, buf, len); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 147 | default: |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 148 | return PROCESS_ERR; |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 149 | } |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | |
| 153 | static int process_console(struct console_client *client) |
| 154 | { |
| 155 | uint8_t buf[4096]; |
| 156 | int len, rc; |
| 157 | |
| 158 | len = read(client->console_sd, buf, sizeof(buf)); |
| 159 | if (len < 0) { |
| 160 | warn("Can't read from server"); |
| 161 | return PROCESS_ERR; |
| 162 | } |
| 163 | if (len == 0) { |
| 164 | fprintf(stderr, "Connection closed\n"); |
| 165 | return PROCESS_EXIT; |
| 166 | } |
| 167 | |
| 168 | rc = write_buf_to_fd(client->fd_out, buf, len); |
| 169 | return rc ? PROCESS_ERR : PROCESS_OK; |
| 170 | } |
| 171 | |
| 172 | /* |
| 173 | * Setup our local file descriptors for IO: use stdin/stdout, and if we're on a |
| 174 | * TTY, put it in canonical mode |
| 175 | */ |
| 176 | static int client_tty_init(struct console_client *client) |
| 177 | { |
| 178 | struct termios termios; |
| 179 | int rc; |
| 180 | |
| 181 | client->fd_in = STDIN_FILENO; |
| 182 | client->fd_out = STDOUT_FILENO; |
| 183 | client->is_tty = isatty(client->fd_in); |
| 184 | |
| 185 | if (!client->is_tty) |
| 186 | return 0; |
| 187 | |
| 188 | rc = tcgetattr(client->fd_in, &termios); |
| 189 | if (rc) { |
| 190 | warn("Can't get terminal attributes for console"); |
| 191 | return -1; |
| 192 | } |
| 193 | memcpy(&client->orig_termios, &termios, sizeof(client->orig_termios)); |
| 194 | cfmakeraw(&termios); |
| 195 | |
| 196 | rc = tcsetattr(client->fd_in, TCSANOW, &termios); |
| 197 | if (rc) { |
| 198 | warn("Can't set terminal attributes for console"); |
| 199 | return -1; |
| 200 | } |
| 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | static int client_init(struct console_client *client) |
| 206 | { |
| 207 | struct sockaddr_un addr; |
| 208 | int rc; |
| 209 | |
| 210 | client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0); |
| 211 | if (!client->console_sd) { |
| 212 | warn("Can't open socket"); |
| 213 | return -1; |
| 214 | } |
| 215 | |
| 216 | memset(&addr, 0, sizeof(addr)); |
| 217 | addr.sun_family = AF_UNIX; |
Jeremy Kerr | 0cff652 | 2016-03-18 09:57:01 +0800 | [diff] [blame] | 218 | memcpy(&addr.sun_path, &console_socket_path, console_socket_path_len); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 219 | |
| 220 | rc = connect(client->console_sd, (struct sockaddr *)&addr, |
Vernon Mauery | fcf8541 | 2018-12-18 15:16:38 -0800 | [diff] [blame] | 221 | sizeof(addr) - sizeof(addr.sun_path) + console_socket_path_len); |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 222 | if (rc) { |
| 223 | warn("Can't connect to console server"); |
| 224 | close(client->console_sd); |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | static void client_fini(struct console_client *client) |
| 232 | { |
| 233 | if (client->is_tty) |
| 234 | tcsetattr(client->fd_in, TCSANOW, &client->orig_termios); |
| 235 | close(client->console_sd); |
| 236 | } |
| 237 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 238 | int main(int argc, char *argv[]) |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 239 | { |
| 240 | struct console_client _client, *client; |
| 241 | struct pollfd pollfds[2]; |
| 242 | enum process_rc prc; |
| 243 | int rc; |
| 244 | |
| 245 | client = &_client; |
| 246 | memset(client, 0, sizeof(*client)); |
William A. Kennington III | ff56983 | 2019-07-24 23:07:15 -0700 | [diff] [blame] | 247 | client->esc_type = ESC_TYPE_SSH; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 248 | |
William A. Kennington III | 15691c8 | 2019-07-24 23:11:18 -0700 | [diff] [blame^] | 249 | for (;;) { |
| 250 | rc = getopt(argc, argv, "e:"); |
| 251 | if (rc == -1) |
| 252 | break; |
| 253 | |
| 254 | switch (rc) { |
| 255 | case 'e': |
| 256 | if (optarg[0] == '\0') { |
| 257 | fprintf(stderr, "Escape str cannot be empty\n"); |
| 258 | return EXIT_FAILURE; |
| 259 | } |
| 260 | client->esc_type = ESC_TYPE_STR; |
| 261 | client->esc_state.str.str = (const uint8_t*)optarg; |
| 262 | break; |
| 263 | default: |
| 264 | fprintf(stderr, |
| 265 | "Usage: %s " |
| 266 | "[-e <escape sequence>]\n", |
| 267 | argv[0]); |
| 268 | return EXIT_FAILURE; |
| 269 | } |
| 270 | } |
| 271 | |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 272 | rc = client_init(client); |
| 273 | if (rc) |
| 274 | return EXIT_FAILURE; |
| 275 | |
| 276 | rc = client_tty_init(client); |
| 277 | if (rc) |
| 278 | goto out_fini; |
| 279 | |
Joel Stanley | 8596581 | 2016-03-17 20:38:12 +1030 | [diff] [blame] | 280 | prc = PROCESS_OK; |
Jeremy Kerr | 2bd0518 | 2016-03-10 16:59:43 +0800 | [diff] [blame] | 281 | for (;;) { |
| 282 | pollfds[0].fd = client->fd_in; |
| 283 | pollfds[0].events = POLLIN; |
| 284 | pollfds[1].fd = client->console_sd; |
| 285 | pollfds[1].events = POLLIN; |
| 286 | |
| 287 | rc = poll(pollfds, 2, -1); |
| 288 | if (rc < 0) { |
| 289 | warn("Poll failure"); |
| 290 | break; |
| 291 | } |
| 292 | |
| 293 | if (pollfds[0].revents) |
| 294 | prc = process_tty(client); |
| 295 | |
| 296 | if (prc == PROCESS_OK && pollfds[1].revents) |
| 297 | prc = process_console(client); |
| 298 | |
| 299 | rc = (prc == PROCESS_ERR) ? -1 : 0; |
| 300 | if (prc != PROCESS_OK) |
| 301 | break; |
| 302 | } |
| 303 | |
| 304 | out_fini: |
| 305 | client_fini(client); |
| 306 | return rc ? EXIT_FAILURE : EXIT_SUCCESS; |
| 307 | } |
| 308 | |