Annotation of src/usr.bin/cu/cu.c, Revision 1.25
1.25 ! mestre 1: /* $OpenBSD: cu.c,v 1.24 2015/10/16 07:01:53 deraadt 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/ioctl.h>
20:
1.11 halex 21: #include <ctype.h>
1.1 nicm 22: #include <err.h>
23: #include <event.h>
24: #include <fcntl.h>
25: #include <getopt.h>
26: #include <paths.h>
27: #include <pwd.h>
28: #include <signal.h>
29: #include <stdio.h>
30: #include <stdlib.h>
31: #include <string.h>
32: #include <termios.h>
33: #include <unistd.h>
1.20 deraadt 34: #include <limits.h>
1.1 nicm 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.22 nicm 44: int is_direct = -1;
1.16 nicm 45: const char *line_path = NULL;
46: int line_speed = -1;
1.1 nicm 47: int line_fd;
1.12 nicm 48: struct termios line_tio;
1.1 nicm 49: struct bufferevent *line_ev;
50: struct event sigterm_ev;
1.8 nicm 51: struct event sighup_ev;
1.1 nicm 52: enum {
53: STATE_NONE,
54: STATE_NEWLINE,
55: STATE_TILDE
56: } last_state = STATE_NEWLINE;
57:
58: __dead void usage(void);
59: void signal_event(int, short, void *);
60: void stream_read(struct bufferevent *, void *);
61: void stream_error(struct bufferevent *, short, void *);
62: void line_read(struct bufferevent *, void *);
63: void line_error(struct bufferevent *, short, void *);
1.17 nicm 64: void try_remote(const char *, const char *, const char *);
1.1 nicm 65:
66: __dead void
67: usage(void)
68: {
1.22 nicm 69: fprintf(stderr, "usage: %s [-d] [-l line] [-s speed | -speed]\n",
1.7 nicm 70: __progname);
1.21 nicm 71: fprintf(stderr, " %s [host]\n", __progname);
1.1 nicm 72: exit(1);
73: }
74:
75: int
76: main(int argc, char **argv)
77: {
1.16 nicm 78: const char *errstr;
1.17 nicm 79: char *tmp, *s, *host;
1.22 nicm 80: int opt, i, flags;
1.24 deraadt 81:
82: if (pledge("stdio rpath wpath cpath getpw proc exec tty",
83: NULL) == -1)
84: err(1, "pledge");
1.1 nicm 85:
1.16 nicm 86: if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
87: err(1, "tcgetattr");
1.1 nicm 88:
89: /*
90: * Convert obsolescent -### speed to modern -s### syntax which getopt()
91: * can handle.
92: */
93: for (i = 1; i < argc; i++) {
1.10 dlg 94: if (strcmp("--", argv[i]) == 0)
95: break;
1.15 deraadt 96: if (argv[i][0] != '-' || !isdigit((unsigned char)argv[i][1]))
1.10 dlg 97: continue;
98:
99: if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
100: errx(1, "speed asprintf");
1.1 nicm 101: }
102:
1.22 nicm 103: while ((opt = getopt(argc, argv, "dl:s:")) != -1) {
1.1 nicm 104: switch (opt) {
1.22 nicm 105: case 'd':
106: is_direct = 1;
107: break;
1.1 nicm 108: case 'l':
1.16 nicm 109: line_path = optarg;
1.1 nicm 110: break;
111: case 's':
1.16 nicm 112: line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
1.1 nicm 113: if (errstr != NULL)
114: errx(1, "speed is %s: %s", errstr, optarg);
115: break;
116: default:
117: usage();
118: }
119: }
120: argc -= optind;
121: argv += optind;
1.16 nicm 122: if (argc != 0 && argc != 1)
1.1 nicm 123: usage();
124:
1.22 nicm 125: if (line_path != NULL || line_speed != -1 || is_direct != -1) {
1.21 nicm 126: if (argc != 0)
127: usage();
128: } else {
129: if (argc == 1)
130: host = argv[0];
131: else
132: host = getenv("HOST");
133: if (host != NULL && *host != '\0') {
134: if (*host == '/')
1.19 nicm 135: line_path = host;
1.21 nicm 136: else {
137: s = getenv("REMOTE");
138: if (s != NULL && *s == '/')
139: try_remote(host, s, NULL);
140: else
141: try_remote(host, NULL, s);
142: }
1.18 nicm 143: }
1.17 nicm 144: }
1.16 nicm 145:
146: if (line_path == NULL)
147: line_path = "/dev/cua00";
148: if (line_speed == -1)
149: line_speed = 9600;
1.22 nicm 150: if (is_direct == -1)
151: is_direct = 0;
1.16 nicm 152:
153: if (strchr(line_path, '/') == NULL) {
154: if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
1.1 nicm 155: err(1, "asprintf");
1.16 nicm 156: line_path = tmp;
1.1 nicm 157: }
158:
1.22 nicm 159: flags = O_RDWR;
160: if (is_direct)
161: flags |= O_NONBLOCK;
162: line_fd = open(line_path, flags);
1.1 nicm 163: if (line_fd < 0)
1.16 nicm 164: err(1, "open(\"%s\")", line_path);
1.25 ! mestre 165: if (!isatty(line_fd))
! 166: err(1, "%s", line_path);
1.1 nicm 167: if (ioctl(line_fd, TIOCEXCL) != 0)
168: err(1, "ioctl(TIOCEXCL)");
1.12 nicm 169: if (tcgetattr(line_fd, &line_tio) != 0)
170: err(1, "tcgetattr");
1.16 nicm 171: if (set_line(line_speed) != 0)
1.4 nicm 172: err(1, "tcsetattr");
1.1 nicm 173:
174: event_init();
1.3 nicm 175:
1.1 nicm 176: signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
177: signal_add(&sigterm_ev, NULL);
1.8 nicm 178: signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
179: signal_add(&sighup_ev, NULL);
1.3 nicm 180: if (signal(SIGINT, SIG_IGN) == SIG_ERR)
181: err(1, "signal");
182: if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
183: err(1, "signal");
1.1 nicm 184:
1.4 nicm 185: set_termios(); /* after this use cu_err and friends */
1.1 nicm 186:
187: /* stdin and stdout get separate events */
188: input_ev = bufferevent_new(STDIN_FILENO, stream_read, NULL,
189: stream_error, NULL);
190: bufferevent_enable(input_ev, EV_READ);
191: output_ev = bufferevent_new(STDOUT_FILENO, NULL, NULL, stream_error,
192: NULL);
193: bufferevent_enable(output_ev, EV_WRITE);
194:
1.23 nicm 195: set_blocking(line_fd, 0);
1.1 nicm 196: line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
197: NULL);
198: bufferevent_enable(line_ev, EV_READ|EV_WRITE);
199:
1.16 nicm 200: printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
1.1 nicm 201: event_dispatch();
202:
1.5 nicm 203: restore_termios();
1.1 nicm 204: printf("\r\n[EOT]\n");
205:
206: exit(0);
207: }
208:
209: void
210: signal_event(int fd, short events, void *data)
211: {
1.5 nicm 212: restore_termios();
1.1 nicm 213: printf("\r\n[SIG%s]\n", sys_signame[fd]);
214:
215: exit(0);
216: }
217:
218: void
1.23 nicm 219: set_blocking(int fd, int state)
220: {
221: int mode;
222:
223: state = state ? 0 : O_NONBLOCK;
224: if ((mode = fcntl(fd, F_GETFL)) == -1)
225: cu_err(1, "fcntl");
226: if ((mode & O_NONBLOCK) != state) {
227: mode = (mode & ~O_NONBLOCK) | state;
228: if (fcntl(fd, F_SETFL, mode) == -1)
229: cu_err(1, "fcntl");
230: }
231: }
232:
233: void
1.1 nicm 234: set_termios(void)
235: {
236: struct termios tio;
237:
238: if (!isatty(STDIN_FILENO))
239: return;
240:
241: memcpy(&tio, &saved_tio, sizeof(tio));
242: tio.c_lflag &= ~(ICANON|IEXTEN|ECHO);
243: tio.c_iflag &= ~(INPCK|ICRNL);
244: tio.c_oflag &= ~OPOST;
245: tio.c_cc[VMIN] = 1;
246: tio.c_cc[VTIME] = 0;
247: tio.c_cc[VDISCARD] = _POSIX_VDISABLE;
248: tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
249: tio.c_cc[VINTR] = _POSIX_VDISABLE;
250: tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
251: tio.c_cc[VQUIT] = _POSIX_VDISABLE;
252: tio.c_cc[VSUSP] = _POSIX_VDISABLE;
253: if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &tio) != 0)
1.4 nicm 254: cu_err(1, "tcsetattr");
1.1 nicm 255: }
256:
257: void
258: restore_termios(void)
259: {
1.4 nicm 260: if (isatty(STDIN_FILENO))
261: tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
1.2 nicm 262: }
263:
264: int
265: set_line(int speed)
266: {
267: struct termios tio;
268:
1.12 nicm 269: memcpy(&tio, &line_tio, sizeof(tio));
270: tio.c_iflag &= ~(ISTRIP|ICRNL);
271: tio.c_oflag &= ~OPOST;
272: tio.c_lflag &= ~(ICANON|ISIG|IEXTEN|ECHO);
273: tio.c_cflag &= ~(CSIZE|PARENB);
274: tio.c_cflag |= CREAD|CS8|CLOCAL;
1.2 nicm 275: tio.c_cc[VMIN] = 1;
276: tio.c_cc[VTIME] = 0;
277: cfsetspeed(&tio, speed);
1.4 nicm 278: if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
1.2 nicm 279: return (-1);
280: return (0);
1.1 nicm 281: }
282:
283: void
284: stream_read(struct bufferevent *bufev, void *data)
285: {
286: char *new_data, *ptr;
287: size_t new_size;
288: int state_change;
289:
290: new_data = EVBUFFER_DATA(input_ev->input);
291: new_size = EVBUFFER_LENGTH(input_ev->input);
292: if (new_size == 0)
293: return;
294:
295: state_change = isatty(STDIN_FILENO);
296: for (ptr = new_data; ptr < new_data + new_size; ptr++) {
297: switch (last_state) {
298: case STATE_NONE:
299: if (state_change && *ptr == '\r')
300: last_state = STATE_NEWLINE;
301: break;
302: case STATE_NEWLINE:
303: if (state_change && *ptr == '~') {
304: last_state = STATE_TILDE;
305: continue;
306: }
307: if (*ptr != '\r')
308: last_state = STATE_NONE;
309: break;
310: case STATE_TILDE:
311: do_command(*ptr);
312: last_state = STATE_NEWLINE;
313: continue;
314: }
315:
316: bufferevent_write(line_ev, ptr, 1);
317: }
318:
319: evbuffer_drain(input_ev->input, new_size);
320: }
321:
322: void
323: stream_error(struct bufferevent *bufev, short what, void *data)
324: {
325: event_loopexit(NULL);
326: }
327:
328: void
329: line_read(struct bufferevent *bufev, void *data)
330: {
331: char *new_data;
332: size_t new_size;
333:
334: new_data = EVBUFFER_DATA(line_ev->input);
335: new_size = EVBUFFER_LENGTH(line_ev->input);
336: if (new_size == 0)
337: return;
338:
1.6 nicm 339: if (record_file != NULL)
340: fwrite(new_data, 1, new_size, record_file);
1.1 nicm 341: bufferevent_write(output_ev, new_data, new_size);
342:
343: evbuffer_drain(line_ev->input, new_size);
344: }
345:
346: void
347: line_error(struct bufferevent *bufev, short what, void *data)
348: {
349: event_loopexit(NULL);
1.16 nicm 350: }
351:
352: void
1.17 nicm 353: try_remote(const char *host, const char *path, const char *entry)
1.16 nicm 354: {
355: const char *paths[] = { "/etc/remote", NULL, NULL };
356: char *cp, *s;
357: long l;
358: int error;
359:
360: if (path != NULL) {
361: paths[0] = path;
362: paths[1] = "/etc/remote";
363: }
364:
1.17 nicm 365: if (entry != NULL && cgetset(entry) != 0)
366: cu_errx(1, "cgetset failed");
1.23 nicm 367: error = cgetent(&cp, (char **)paths, (char *)host);
1.16 nicm 368: if (error < 0) {
369: switch (error) {
370: case -1:
371: cu_errx(1, "unknown host %s", host);
372: case -2:
373: cu_errx(1, "can't open remote file");
374: case -3:
375: cu_errx(1, "loop in remote file");
376: default:
377: cu_errx(1, "unknown error in remote file");
378: }
379: }
1.22 nicm 380:
381: if (is_direct == -1 && cgetcap(cp, "dc", ':') != NULL)
382: is_direct = 1;
1.16 nicm 383:
384: if (line_path == NULL && cgetstr(cp, "dv", &s) >= 0)
385: line_path = s;
386:
387: if (line_speed == -1 && cgetnum(cp, "br", &l) >= 0) {
388: if (l < 0 || l > INT_MAX)
389: cu_errx(1, "speed out of range");
390: line_speed = l;
391: }
1.1 nicm 392: }
393:
394: /* Expands tildes in the file name. Based on code from ssh/misc.c. */
395: char *
396: tilde_expand(const char *filename1)
397: {
1.14 tedu 398: const char *filename, *path, *sep;
399: char user[128], *out;
1.1 nicm 400: struct passwd *pw;
401: u_int len, slash;
1.14 tedu 402: int rv;
1.1 nicm 403:
404: if (*filename1 != '~')
405: goto no_change;
406: filename = filename1 + 1;
407:
408: path = strchr(filename, '/');
409: if (path != NULL && path > filename) { /* ~user/path */
410: slash = path - filename;
411: if (slash > sizeof(user) - 1)
412: goto no_change;
413: memcpy(user, filename, slash);
414: user[slash] = '\0';
415: if ((pw = getpwnam(user)) == NULL)
416: goto no_change;
417: } else if ((pw = getpwuid(getuid())) == NULL) /* ~/path */
418: goto no_change;
419:
420: /* Make sure directory has a trailing '/' */
421: len = strlen(pw->pw_dir);
1.14 tedu 422: if (len == 0 || pw->pw_dir[len - 1] != '/')
423: sep = "/";
424: else
425: sep = "";
1.1 nicm 426:
427: /* Skip leading '/' from specified path */
428: if (path != NULL)
429: filename = path + 1;
1.14 tedu 430:
431: if ((rv = asprintf(&out, "%s%s%s", pw->pw_dir, sep, filename)) == -1)
432: cu_err(1, "asprintf");
1.20 deraadt 433: if (rv >= PATH_MAX) {
1.14 tedu 434: free(out);
1.1 nicm 435: goto no_change;
1.14 tedu 436: }
1.1 nicm 437:
438: return (out);
439:
440: no_change:
441: out = strdup(filename1);
442: if (out == NULL)
1.4 nicm 443: cu_err(1, "strdup");
1.1 nicm 444: return (out);
445: }