Annotation of src/usr.bin/cu/cu.c, Revision 1.18
1.18 ! nicm 1: /* $OpenBSD: cu.c,v 1.17 2014/03/31 09:09:19 nicm Exp $ */
1.1 nicm 2:
3: /*
4: * Copyright (c) 2012 Nicholas Marriott <nicm@openbsd.org>
5: *
6: * Permission to use, copy, modify, and distribute this software for any
7: * purpose with or without fee is hereby granted, provided that the above
8: * copyright notice and this permission notice appear in all copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14: * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
15: * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16: * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17: */
18:
19: #include <sys/param.h>
20: #include <sys/ioctl.h>
21:
1.11 halex 22: #include <ctype.h>
1.1 nicm 23: #include <err.h>
24: #include <event.h>
25: #include <fcntl.h>
26: #include <getopt.h>
27: #include <paths.h>
28: #include <pwd.h>
29: #include <signal.h>
30: #include <stdio.h>
31: #include <stdlib.h>
32: #include <string.h>
33: #include <termios.h>
34: #include <unistd.h>
35:
36: #include "cu.h"
37:
38: extern char *__progname;
39:
1.6 nicm 40: FILE *record_file;
1.1 nicm 41: struct termios saved_tio;
42: struct bufferevent *input_ev;
43: struct bufferevent *output_ev;
1.16 nicm 44: const char *line_path = NULL;
45: int line_speed = -1;
1.1 nicm 46: int line_fd;
1.12 nicm 47: struct termios line_tio;
1.1 nicm 48: struct bufferevent *line_ev;
49: struct event sigterm_ev;
1.8 nicm 50: struct event sighup_ev;
1.1 nicm 51: enum {
52: STATE_NONE,
53: STATE_NEWLINE,
54: STATE_TILDE
55: } last_state = STATE_NEWLINE;
56:
57: __dead void usage(void);
58: void signal_event(int, short, void *);
59: void stream_read(struct bufferevent *, void *);
60: void stream_error(struct bufferevent *, short, void *);
61: void line_read(struct bufferevent *, void *);
62: void line_error(struct bufferevent *, short, void *);
1.17 nicm 63: void try_remote(const char *, const char *, const char *);
1.1 nicm 64:
65: __dead void
66: usage(void)
67: {
1.16 nicm 68: fprintf(stderr, "usage: %s [-l line] [-s speed | -speed] [host]\n",
1.7 nicm 69: __progname);
1.1 nicm 70: exit(1);
71: }
72:
73: int
74: main(int argc, char **argv)
75: {
1.16 nicm 76: const char *errstr;
1.17 nicm 77: char *tmp, *s, *host;
1.16 nicm 78: int opt, i;
1.1 nicm 79:
1.16 nicm 80: if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
81: err(1, "tcgetattr");
1.1 nicm 82:
83: /*
84: * Convert obsolescent -### speed to modern -s### syntax which getopt()
85: * can handle.
86: */
87: for (i = 1; i < argc; i++) {
1.10 dlg 88: if (strcmp("--", argv[i]) == 0)
89: break;
1.15 deraadt 90: if (argv[i][0] != '-' || !isdigit((unsigned char)argv[i][1]))
1.10 dlg 91: continue;
92:
93: if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
94: errx(1, "speed asprintf");
1.1 nicm 95: }
96:
97: while ((opt = getopt(argc, argv, "l:s:")) != -1) {
98: switch (opt) {
99: case 'l':
1.16 nicm 100: line_path = optarg;
1.1 nicm 101: break;
102: case 's':
1.16 nicm 103: line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
1.1 nicm 104: if (errstr != NULL)
105: errx(1, "speed is %s: %s", errstr, optarg);
106: break;
107: default:
108: usage();
109: }
110: }
111: argc -= optind;
112: argv += optind;
1.16 nicm 113: if (argc != 0 && argc != 1)
1.1 nicm 114: usage();
115:
1.17 nicm 116: if (argc == 1)
117: host = argv[0];
118: else
119: host = getenv("HOST");
120: if (host != NULL && *host != '\0') {
1.18 ! nicm 121: if (*host == '/')
! 122: line_path = host;
! 123: else {
! 124: s = getenv("REMOTE");
! 125: if (s != NULL && *s == '/')
! 126: try_remote(host, s, NULL);
! 127: else
! 128: try_remote(host, NULL, s);
! 129: }
1.17 nicm 130: }
1.16 nicm 131:
132: if (line_path == NULL)
133: line_path = "/dev/cua00";
134: if (line_speed == -1)
135: line_speed = 9600;
136:
137: if (strchr(line_path, '/') == NULL) {
138: if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
1.1 nicm 139: err(1, "asprintf");
1.16 nicm 140: line_path = tmp;
1.1 nicm 141: }
142:
1.16 nicm 143: line_fd = open(line_path, O_RDWR);
1.1 nicm 144: if (line_fd < 0)
1.16 nicm 145: err(1, "open(\"%s\")", line_path);
1.1 nicm 146: if (ioctl(line_fd, TIOCEXCL) != 0)
147: err(1, "ioctl(TIOCEXCL)");
1.12 nicm 148: if (tcgetattr(line_fd, &line_tio) != 0)
149: err(1, "tcgetattr");
1.16 nicm 150: if (set_line(line_speed) != 0)
1.4 nicm 151: err(1, "tcsetattr");
1.1 nicm 152:
153: event_init();
1.3 nicm 154:
1.1 nicm 155: signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
156: signal_add(&sigterm_ev, NULL);
1.8 nicm 157: signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
158: signal_add(&sighup_ev, NULL);
1.3 nicm 159: if (signal(SIGINT, SIG_IGN) == SIG_ERR)
160: err(1, "signal");
161: if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
162: err(1, "signal");
1.1 nicm 163:
1.4 nicm 164: set_termios(); /* after this use cu_err and friends */
1.1 nicm 165:
166: /* stdin and stdout get separate events */
167: input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
168: stream_error, NULL);
169: bufferevent_enable(input_ev, EV_READ);
170: output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
171: NULL);
172: bufferevent_enable(output_ev, EV_WRITE);
173:
174: line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
175: NULL);
176: bufferevent_enable(line_ev, EV_READ|EV_WRITE);
177:
1.16 nicm 178: printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
1.1 nicm 179: event_dispatch();
180:
1.5 nicm 181: restore_termios();
1.1 nicm 182: printf("\r\n[EOT]\n");
183:
184: exit(0);
185: }
186:
187: void
188: signal_event(int fd, short events, void *data)
189: {
1.5 nicm 190: restore_termios();
1.1 nicm 191: printf("\r\n[SIG%s]\n", sys_signame[fd]);
192:
193: exit(0);
194: }
195:
196: void
197: set_termios(void)
198: {
199: struct termios tio;
200:
201: if (!isatty(STDIN_FILENO))
202: return;
203:
204: memcpy(&tio, &saved_tio, sizeof(tio));
205: tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
206: tio.c_iflag &= ~(INPCK|ICRNL);
207: tio.c_oflag &= ~OPOST;
208: tio.c_cc[VMIN] = 1;
209: tio.c_cc[VTIME] = 0;
210: tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
211: tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
212: tio.c_cc[VINTR] = _POSIX_VDISABLE;
213: tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
214: tio.c_cc[VQUIT] = _POSIX_VDISABLE;
215: tio.c_cc[VSUSP] = _POSIX_VDISABLE;
216: if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4 nicm 217: cu_err(1, "tcsetattr");
1.1 nicm 218: }
219:
220: void
221: restore_termios(void)
222: {
1.4 nicm 223: if (isatty(STDIN_FILENO))
224: tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2 nicm 225: }
226:
227: int
228: set_line(int speed)
229: {
230: struct termios tio;
231:
1.12 nicm 232: memcpy(&tio, &line_tio, sizeof(tio));
233: tio.c_iflag &= ~(ISTRIP|ICRNL);
234: tio.c_oflag &= ~OPOST;
235: tio.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
236: tio.c_cflag &= ~(CSIZE|PARENB);
237: tio.c_cflag |= CREAD|CS8|CLOCAL;
1.2 nicm 238: tio.c_cc[VMIN] = 1;
239: tio.c_cc[VTIME] = 0;
240: cfsetspeed(&tio, speed);
1.4 nicm 241: if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2 nicm 242: return (-1);
243: return (0);
1.1 nicm 244: }
245:
246: void
247: stream_read(struct bufferevent *bufev, void *data)
248: {
249: char *new_data, *ptr;
250: size_t new_size;
251: int state_change;
252:
253: new_data = EVBUFFER_DATA(input_ev->input);
254: new_size = EVBUFFER_LENGTH(input_ev->input);
255: if (new_size == 0)
256: return;
257:
258: state_change = isatty(STDIN_FILENO);
259: for (ptr = new_data; ptr < new_data + new_size; ptr++) {
260: switch (last_state) {
261: case STATE_NONE:
262: if (state_change && *ptr == '\r')
263: last_state = STATE_NEWLINE;
264: break;
265: case STATE_NEWLINE:
266: if (state_change && *ptr == '~') {
267: last_state = STATE_TILDE;
268: continue;
269: }
270: if (*ptr != '\r')
271: last_state = STATE_NONE;
272: break;
273: case STATE_TILDE:
274: do_command(*ptr);
275: last_state = STATE_NEWLINE;
276: continue;
277: }
278:
279: bufferevent_write(line_ev, ptr, 1);
280: }
281:
282: evbuffer_drain(input_ev->input, new_size);
283: }
284:
285: void
286: stream_error(struct bufferevent *bufev, short what, void *data)
287: {
288: event_loopexit(NULL);
289: }
290:
291: void
292: line_read(struct bufferevent *bufev, void *data)
293: {
294: char *new_data;
295: size_t new_size;
296:
297: new_data = EVBUFFER_DATA(line_ev->input);
298: new_size = EVBUFFER_LENGTH(line_ev->input);
299: if (new_size == 0)
300: return;
301:
1.6 nicm 302: if (record_file != NULL)
303: fwrite(new_data, 1, new_size, record_file);
1.1 nicm 304: bufferevent_write(output_ev, new_data, new_size);
305:
306: evbuffer_drain(line_ev->input, new_size);
307: }
308:
309: void
310: line_error(struct bufferevent *bufev, short what, void *data)
311: {
312: event_loopexit(NULL);
1.16 nicm 313: }
314:
315: void
1.17 nicm 316: try_remote(const char *host, const char *path, const char *entry)
1.16 nicm 317: {
318: const char *paths[] = { "/etc/remote", NULL, NULL };
319: char *cp, *s;
320: long l;
321: int error;
322:
323: if (path != NULL) {
324: paths[0] = path;
325: paths[1] = "/etc/remote";
326: }
327:
1.17 nicm 328: if (entry != NULL && cgetset(entry) != 0)
329: cu_errx(1, "cgetset failed");
1.16 nicm 330: error = cgetent(&cp, (char**)paths, (char*)host);
331: if (error < 0) {
332: switch (error) {
333: case -1:
334: cu_errx(1, "unknown host %s", host);
335: case -2:
336: cu_errx(1, "can't open remote file");
337: case -3:
338: cu_errx(1, "loop in remote file");
339: default:
340: cu_errx(1, "unknown error in remote file");
341: }
342: }
343:
344: if (line_path == NULL && cgetstr(cp, "dv", &s) >= 0)
345: line_path = s;
346:
347: if (line_speed == -1 && cgetnum(cp, "br", &l) >= 0) {
348: if (l < 0 || l > INT_MAX)
349: cu_errx(1, "speed out of range");
350: line_speed = l;
351: }
1.1 nicm 352: }
353:
354: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
355: char *
356: tilde_expand(const char *filename1)
357: {
1.14 tedu 358: const char *filename, *path, *sep;
359: char user[128], *out;
1.1 nicm 360: struct passwd *pw;
361: u_int len, slash;
1.14 tedu 362: int rv;
1.1 nicm 363:
364: if (*filename1 != '~')
365: goto no_change;
366: filename = filename1 + 1;
367:
368: path = strchr(filename, '/');
369: if (path != NULL && path > filename) { /* ~user/path */
370: slash = path - filename;
371: if (slash > sizeof(user) - 1)
372: goto no_change;
373: memcpy(user, filename, slash);
374: user[slash] = '\0';
375: if ((pw = getpwnam(user)) == NULL)
376: goto no_change;
377: } else if ((pw = getpwuid(getuid())) == NULL) /* ~/path */
378: goto no_change;
379:
380: /* Make sure directory has a trailing '/' */
381: len = strlen(pw->pw_dir);
1.14 tedu 382: if (len == 0 || pw->pw_dir[len - 1] != '/')
383: sep = "/";
384: else
385: sep = "";
1.1 nicm 386:
387: /* Skip leading '/' from specified path */
388: if (path != NULL)
389: filename = path + 1;
1.14 tedu 390:
391: if ((rv = asprintf(&out, "%s%s%s", pw->pw_dir, sep, filename)) == -1)
392: cu_err(1, "asprintf");
393: if (rv >= MAXPATHLEN) {
394: free(out);
1.1 nicm 395: goto no_change;
1.14 tedu 396: }
1.1 nicm 397:
398: return (out);
399:
400: no_change:
401: out = strdup(filename1);
402: if (out == NULL)
1.4 nicm 403: cu_err(1, "strdup");
1.1 nicm 404: return (out);
405: }