GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/cu/cu.c Lines: 0 208 0.0 %
Date: 2017-11-07 Branches: 0 154 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: cu.c,v 1.25 2017/08/22 16:32:37 mestre Exp $ */
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
21
#include <ctype.h>
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>
34
#include <limits.h>
35
36
#include "cu.h"
37
38
extern char		*__progname;
39
40
FILE			*record_file;
41
struct termios		 saved_tio;
42
struct bufferevent	*input_ev;
43
struct bufferevent	*output_ev;
44
int			 is_direct = -1;
45
const char		*line_path = NULL;
46
int			 line_speed = -1;
47
int			 line_fd;
48
struct termios		 line_tio;
49
struct bufferevent	*line_ev;
50
struct event		 sigterm_ev;
51
struct event		 sighup_ev;
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 *);
64
void		try_remote(const char *, const char *, const char *);
65
66
__dead void
67
usage(void)
68
{
69
	fprintf(stderr, "usage: %s [-d] [-l line] [-s speed | -speed]\n",
70
	    __progname);
71
	fprintf(stderr, "       %s [host]\n", __progname);
72
	exit(1);
73
}
74
75
int
76
main(int argc, char **argv)
77
{
78
	const char	*errstr;
79
	char		*tmp, *s, *host;
80
	int		 opt, i, flags;
81
82
	if (pledge("stdio rpath wpath cpath getpw proc exec tty",
83
	    NULL) == -1)
84
		err(1, "pledge");
85
86
	if (isatty(STDIN_FILENO) && tcgetattr(STDIN_FILENO, &saved_tio) != 0)
87
		err(1, "tcgetattr");
88
89
	/*
90
	 * Convert obsolescent -### speed to modern -s### syntax which getopt()
91
	 * can handle.
92
	 */
93
	for (i = 1; i < argc; i++) {
94
		if (strcmp("--", argv[i]) == 0)
95
			break;
96
		if (argv[i][0] != '-' || !isdigit((unsigned char)argv[i][1]))
97
			continue;
98
99
		if (asprintf(&argv[i], "-s%s", &argv[i][1]) == -1)
100
			errx(1, "speed asprintf");
101
	}
102
103
	while ((opt = getopt(argc, argv, "dl:s:")) != -1) {
104
		switch (opt) {
105
		case 'd':
106
			is_direct = 1;
107
			break;
108
		case 'l':
109
			line_path = optarg;
110
			break;
111
		case 's':
112
			line_speed = strtonum(optarg, 0, INT_MAX, &errstr);
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;
122
	if (argc != 0 && argc != 1)
123
		usage();
124
125
	if (line_path != NULL || line_speed != -1 || is_direct != -1) {
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 == '/')
135
				line_path = host;
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
			}
143
		}
144
	}
145
146
	if (line_path == NULL)
147
		line_path = "/dev/cua00";
148
	if (line_speed == -1)
149
		line_speed = 9600;
150
	if (is_direct == -1)
151
		is_direct = 0;
152
153
	if (strchr(line_path, '/') == NULL) {
154
		if (asprintf(&tmp, "%s%s", _PATH_DEV, line_path) == -1)
155
			err(1, "asprintf");
156
		line_path = tmp;
157
	}
158
159
	flags = O_RDWR;
160
	if (is_direct)
161
		flags |= O_NONBLOCK;
162
	line_fd = open(line_path, flags);
163
	if (line_fd < 0)
164
		err(1, "open(\"%s\")", line_path);
165
	if (!isatty(line_fd))
166
		err(1, "%s", line_path);
167
	if (ioctl(line_fd, TIOCEXCL) != 0)
168
		err(1, "ioctl(TIOCEXCL)");
169
	if (tcgetattr(line_fd, &line_tio) != 0)
170
		err(1, "tcgetattr");
171
	if (set_line(line_speed) != 0)
172
		err(1, "tcsetattr");
173
174
	event_init();
175
176
	signal_set(&sigterm_ev, SIGTERM, signal_event, NULL);
177
	signal_add(&sigterm_ev, NULL);
178
	signal_set(&sighup_ev, SIGHUP, signal_event, NULL);
179
	signal_add(&sighup_ev, NULL);
180
	if (signal(SIGINT, SIG_IGN) == SIG_ERR)
181
		err(1, "signal");
182
	if (signal(SIGQUIT, SIG_IGN) == SIG_ERR)
183
		err(1, "signal");
184
185
	set_termios(); /* after this use cu_err and friends */
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
195
	set_blocking(line_fd, 0);
196
	line_ev = bufferevent_new(line_fd, line_read, NULL, line_error,
197
	    NULL);
198
	bufferevent_enable(line_ev, EV_READ|EV_WRITE);
199
200
	printf("Connected to %s (speed %d)\r\n", line_path, line_speed);
201
	event_dispatch();
202
203
	restore_termios();
204
	printf("\r\n[EOT]\n");
205
206
	exit(0);
207
}
208
209
void
210
signal_event(int fd, short events, void *data)
211
{
212
	restore_termios();
213
	printf("\r\n[SIG%s]\n", sys_signame[fd]);
214
215
	exit(0);
216
}
217
218
void
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
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)
254
		cu_err(1, "tcsetattr");
255
}
256
257
void
258
restore_termios(void)
259
{
260
	if (isatty(STDIN_FILENO))
261
		tcsetattr(STDIN_FILENO, TCSAFLUSH, &saved_tio);
262
}
263
264
int
265
set_line(int speed)
266
{
267
	struct termios	 tio;
268
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;
275
	tio.c_cc[VMIN] = 1;
276
	tio.c_cc[VTIME] = 0;
277
	cfsetspeed(&tio, speed);
278
	if (tcsetattr(line_fd, TCSAFLUSH, &tio) != 0)
279
		return (-1);
280
	return (0);
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
339
	if (record_file != NULL)
340
		fwrite(new_data, 1, new_size, record_file);
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);
350
}
351
352
void
353
try_remote(const char *host, const char *path, const char *entry)
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
365
	if (entry != NULL && cgetset(entry) != 0)
366
		cu_errx(1, "cgetset failed");
367
	error = cgetent(&cp, (char **)paths, (char *)host);
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
	}
380
381
	if (is_direct == -1 && cgetcap(cp, "dc", ':') != NULL)
382
		is_direct = 1;
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
	}
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
{
398
	const char	*filename, *path, *sep;
399
	char		 user[128], *out;
400
	struct passwd	*pw;
401
	u_int		 len, slash;
402
	int		 rv;
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);
422
	if (len == 0 || pw->pw_dir[len - 1] != '/')
423
		sep = "/";
424
	else
425
		sep = "";
426
427
	/* Skip leading '/' from specified path */
428
	if (path != NULL)
429
		filename = path + 1;
430
431
	if ((rv = asprintf(&out, "%s%s%s", pw->pw_dir, sep, filename)) == -1)
432
		cu_err(1, "asprintf");
433
	if (rv >= PATH_MAX) {
434
		free(out);
435
		goto no_change;
436
	}
437
438
	return (out);
439
440
no_change:
441
	out = strdup(filename1);
442
	if (out == NULL)
443
		cu_err(1, "strdup");
444
	return (out);
445
}