blob: 6baeb0ba70aa8586168c43c9475d1296f6b32cd7 [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>
William A. Kennington III15691c82019-07-24 23:11:18 -070018#include <getopt.h>
Jeremy Kerr2bd05182016-03-10 16:59:43 +080019#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 III8a154352019-07-24 23:01:47 -070032#define EXIT_ESCAPE 2
33
Jeremy Kerr2bd05182016-03-10 16:59:43 +080034enum process_rc {
35 PROCESS_OK = 0,
36 PROCESS_ERR,
37 PROCESS_EXIT,
William A. Kennington III8a154352019-07-24 23:01:47 -070038 PROCESS_ESC,
Jeremy Kerr2bd05182016-03-10 16:59:43 +080039};
40
William A. Kennington IIIff569832019-07-24 23:07:15 -070041enum esc_type {
42 ESC_TYPE_SSH,
William A. Kennington III15691c82019-07-24 23:11:18 -070043 ESC_TYPE_STR,
William A. Kennington IIIff569832019-07-24 23:07:15 -070044};
45
46struct ssh_esc_state {
47 uint8_t state;
48};
49
William A. Kennington III15691c82019-07-24 23:11:18 -070050struct str_esc_state {
51 const uint8_t *str;
52 size_t pos;
53};
54
Jeremy Kerr2bd05182016-03-10 16:59:43 +080055struct 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 IIIff569832019-07-24 23:07:15 -070061 enum esc_type esc_type;
62 union {
63 struct ssh_esc_state ssh;
William A. Kennington III15691c82019-07-24 23:11:18 -070064 struct str_esc_state str;
William A. Kennington IIIff569832019-07-24 23:07:15 -070065 } esc_state;
Jeremy Kerr2bd05182016-03-10 16:59:43 +080066};
67
William A. Kennington IIIff569832019-07-24 23:07:15 -070068static 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 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 */
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 Kerr2bd05182016-03-10 16:59:43 +0800108
William A. Kennington III15691c82019-07-24 23:11:18 -0700109static 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 III8a154352019-07-24 23:01:47 -0700123 prc = PROCESS_ESC;
William A. Kennington III15691c82019-07-24 23:11:18 -0700124 break;
125 }
126 }
127
128 if (write_buf_to_fd(client->console_sd, buf, i) < 0)
129 return PROCESS_ERR;
130 return prc;
131}
132
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800133static enum process_rc process_tty(struct console_client *client)
134{
William A. Kennington IIIff569832019-07-24 23:07:15 -0700135 uint8_t buf[4096];
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800136 ssize_t len;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800137
138 len = read(client->fd_in, buf, sizeof(buf));
139 if (len < 0)
140 return PROCESS_ERR;
141 if (len == 0)
142 return PROCESS_EXIT;
143
William A. Kennington IIIff569832019-07-24 23:07:15 -0700144 switch (client->esc_type)
145 {
146 case ESC_TYPE_SSH:
147 return process_ssh_tty(client, buf, len);
William A. Kennington III15691c82019-07-24 23:11:18 -0700148 case ESC_TYPE_STR:
149 return process_str_tty(client, buf, len);
William A. Kennington IIIff569832019-07-24 23:07:15 -0700150 default:
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800151 return PROCESS_ERR;
William A. Kennington IIIff569832019-07-24 23:07:15 -0700152 }
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800153}
154
155
156static int process_console(struct console_client *client)
157{
158 uint8_t buf[4096];
159 int len, rc;
160
161 len = read(client->console_sd, buf, sizeof(buf));
162 if (len < 0) {
163 warn("Can't read from server");
164 return PROCESS_ERR;
165 }
166 if (len == 0) {
167 fprintf(stderr, "Connection closed\n");
168 return PROCESS_EXIT;
169 }
170
171 rc = write_buf_to_fd(client->fd_out, buf, len);
172 return rc ? PROCESS_ERR : PROCESS_OK;
173}
174
175/*
176 * Setup our local file descriptors for IO: use stdin/stdout, and if we're on a
177 * TTY, put it in canonical mode
178 */
179static int client_tty_init(struct console_client *client)
180{
181 struct termios termios;
182 int rc;
183
184 client->fd_in = STDIN_FILENO;
185 client->fd_out = STDOUT_FILENO;
186 client->is_tty = isatty(client->fd_in);
187
188 if (!client->is_tty)
189 return 0;
190
191 rc = tcgetattr(client->fd_in, &termios);
192 if (rc) {
193 warn("Can't get terminal attributes for console");
194 return -1;
195 }
196 memcpy(&client->orig_termios, &termios, sizeof(client->orig_termios));
197 cfmakeraw(&termios);
198
199 rc = tcsetattr(client->fd_in, TCSANOW, &termios);
200 if (rc) {
201 warn("Can't set terminal attributes for console");
202 return -1;
203 }
204
205 return 0;
206}
207
208static int client_init(struct console_client *client)
209{
210 struct sockaddr_un addr;
211 int rc;
212
213 client->console_sd = socket(AF_UNIX, SOCK_STREAM, 0);
214 if (!client->console_sd) {
215 warn("Can't open socket");
216 return -1;
217 }
218
219 memset(&addr, 0, sizeof(addr));
220 addr.sun_family = AF_UNIX;
Jeremy Kerr0cff6522016-03-18 09:57:01 +0800221 memcpy(&addr.sun_path, &console_socket_path, console_socket_path_len);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800222
223 rc = connect(client->console_sd, (struct sockaddr *)&addr,
Vernon Maueryfcf85412018-12-18 15:16:38 -0800224 sizeof(addr) - sizeof(addr.sun_path) + console_socket_path_len);
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800225 if (rc) {
226 warn("Can't connect to console server");
227 close(client->console_sd);
228 return -1;
229 }
230
231 return 0;
232}
233
234static void client_fini(struct console_client *client)
235{
236 if (client->is_tty)
237 tcsetattr(client->fd_in, TCSANOW, &client->orig_termios);
238 close(client->console_sd);
239}
240
William A. Kennington III15691c82019-07-24 23:11:18 -0700241int main(int argc, char *argv[])
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800242{
243 struct console_client _client, *client;
244 struct pollfd pollfds[2];
William A. Kennington III8a154352019-07-24 23:01:47 -0700245 enum process_rc prc = PROCESS_OK;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800246 int rc;
247
248 client = &_client;
249 memset(client, 0, sizeof(*client));
William A. Kennington IIIff569832019-07-24 23:07:15 -0700250 client->esc_type = ESC_TYPE_SSH;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800251
William A. Kennington III15691c82019-07-24 23:11:18 -0700252 for (;;) {
253 rc = getopt(argc, argv, "e:");
254 if (rc == -1)
255 break;
256
257 switch (rc) {
258 case 'e':
259 if (optarg[0] == '\0') {
260 fprintf(stderr, "Escape str cannot be empty\n");
261 return EXIT_FAILURE;
262 }
263 client->esc_type = ESC_TYPE_STR;
264 client->esc_state.str.str = (const uint8_t*)optarg;
265 break;
266 default:
267 fprintf(stderr,
268 "Usage: %s "
269 "[-e <escape sequence>]\n",
270 argv[0]);
271 return EXIT_FAILURE;
272 }
273 }
274
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800275 rc = client_init(client);
276 if (rc)
277 return EXIT_FAILURE;
278
279 rc = client_tty_init(client);
280 if (rc)
281 goto out_fini;
282
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800283 for (;;) {
284 pollfds[0].fd = client->fd_in;
285 pollfds[0].events = POLLIN;
286 pollfds[1].fd = client->console_sd;
287 pollfds[1].events = POLLIN;
288
289 rc = poll(pollfds, 2, -1);
290 if (rc < 0) {
291 warn("Poll failure");
292 break;
293 }
294
295 if (pollfds[0].revents)
296 prc = process_tty(client);
297
298 if (prc == PROCESS_OK && pollfds[1].revents)
299 prc = process_console(client);
300
301 rc = (prc == PROCESS_ERR) ? -1 : 0;
302 if (prc != PROCESS_OK)
303 break;
304 }
305
306out_fini:
307 client_fini(client);
William A. Kennington III8a154352019-07-24 23:01:47 -0700308 if (prc == PROCESS_ESC)
309 return EXIT_ESCAPE;
Jeremy Kerr2bd05182016-03-10 16:59:43 +0800310 return rc ? EXIT_FAILURE : EXIT_SUCCESS;
311}
312