xref: /freebsd/bin/cat/cat.c (revision dc36d6f9bb1753f3808552f3afd30eda9a7b206a)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1989, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Kevin Fall.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 #if 0
36 #ifndef lint
37 static char const copyright[] =
38 "@(#) Copyright (c) 1989, 1993\n\
39 	The Regents of the University of California.  All rights reserved.\n";
40 #endif /* not lint */
41 #endif
42 
43 #ifndef lint
44 #endif /* not lint */
45 #include <sys/cdefs.h>
46 #include <sys/capsicum.h>
47 #include <sys/param.h>
48 #include <sys/stat.h>
49 #ifndef NO_UDOM_SUPPORT
50 #include <sys/socket.h>
51 #include <sys/un.h>
52 #include <netdb.h>
53 #endif
54 
55 #include <capsicum_helpers.h>
56 #include <ctype.h>
57 #include <err.h>
58 #include <errno.h>
59 #include <fcntl.h>
60 #include <locale.h>
61 #include <stdio.h>
62 #include <stdlib.h>
63 #include <string.h>
64 #include <unistd.h>
65 #include <wchar.h>
66 #include <wctype.h>
67 
68 #include <libcasper.h>
69 #include <casper/cap_fileargs.h>
70 #include <casper/cap_net.h>
71 
72 static int bflag, eflag, lflag, nflag, sflag, tflag, vflag;
73 static int rval;
74 static const char *filename;
75 static fileargs_t *fa;
76 
77 static void usage(void) __dead2;
78 static void scanfiles(char *argv[], int cooked);
79 #ifndef BOOTSTRAP_CAT
80 static void cook_cat(FILE *);
81 static ssize_t in_kernel_copy(int);
82 #endif
83 static void raw_cat(int);
84 
85 #ifndef NO_UDOM_SUPPORT
86 static cap_channel_t *capnet;
87 
88 static int udom_open(const char *path, int flags);
89 #endif
90 
91 /*
92  * Memory strategy threshold, in pages: if physmem is larger than this,
93  * use a large buffer.
94  */
95 #define	PHYSPAGES_THRESHOLD (32 * 1024)
96 
97 /* Maximum buffer size in bytes - do not allow it to grow larger than this. */
98 #define	BUFSIZE_MAX (2 * 1024 * 1024)
99 
100 /*
101  * Small (default) buffer size in bytes. It's inefficient for this to be
102  * smaller than MAXPHYS.
103  */
104 #define	BUFSIZE_SMALL (MAXPHYS)
105 
106 
107 /*
108  * For the bootstrapped cat binary (needed for locked appending to METALOG), we
109  * disable all flags except -l and -u to avoid non-portable function calls.
110  * In the future we may instead want to write a small portable bootstrap tool
111  * that locks the output file before writing to it. However, for now
112  * bootstrapping cat without multibyte support is the simpler solution.
113  */
114 #ifdef BOOTSTRAP_CAT
115 #define SUPPORTED_FLAGS "lu"
116 #else
117 #define SUPPORTED_FLAGS "belnstuv"
118 #endif
119 
120 #ifndef NO_UDOM_SUPPORT
121 static void
122 init_casper_net(cap_channel_t *casper)
123 {
124 	cap_net_limit_t *limit;
125 	int familylimit;
126 
127 	capnet = cap_service_open(casper, "system.net");
128 	if (capnet == NULL)
129 		err(EXIT_FAILURE, "unable to create network service");
130 
131 	limit = cap_net_limit_init(capnet, CAPNET_NAME2ADDR |
132 	    CAPNET_CONNECTDNS);
133 	if (limit == NULL)
134 		err(EXIT_FAILURE, "unable to create limits");
135 
136 	familylimit = AF_LOCAL;
137 	cap_net_limit_name2addr_family(limit, &familylimit, 1);
138 
139 	if (cap_net_limit(limit) < 0)
140 		err(EXIT_FAILURE, "unable to apply limits");
141 }
142 #endif
143 
144 static void
145 init_casper(int argc, char *argv[])
146 {
147 	cap_channel_t *casper;
148 	cap_rights_t rights;
149 
150 	casper = cap_init();
151 	if (casper == NULL)
152 		err(EXIT_FAILURE, "unable to create Casper");
153 
154 	fa = fileargs_cinit(casper, argc, argv, O_RDONLY, 0,
155 	    cap_rights_init(&rights, CAP_READ | CAP_FSTAT | CAP_FCNTL | CAP_SEEK),
156 	    FA_OPEN | FA_REALPATH);
157 	if (fa == NULL)
158 		err(EXIT_FAILURE, "unable to create fileargs");
159 
160 #ifndef NO_UDOM_SUPPORT
161 	init_casper_net(casper);
162 #endif
163 
164 	cap_close(casper);
165 }
166 
167 int
168 main(int argc, char *argv[])
169 {
170 	int ch;
171 	struct flock stdout_lock;
172 
173 	setlocale(LC_CTYPE, "");
174 
175 	while ((ch = getopt(argc, argv, SUPPORTED_FLAGS)) != -1)
176 		switch (ch) {
177 		case 'b':
178 			bflag = nflag = 1;	/* -b implies -n */
179 			break;
180 		case 'e':
181 			eflag = vflag = 1;	/* -e implies -v */
182 			break;
183 		case 'l':
184 			lflag = 1;
185 			break;
186 		case 'n':
187 			nflag = 1;
188 			break;
189 		case 's':
190 			sflag = 1;
191 			break;
192 		case 't':
193 			tflag = vflag = 1;	/* -t implies -v */
194 			break;
195 		case 'u':
196 			setbuf(stdout, NULL);
197 			break;
198 		case 'v':
199 			vflag = 1;
200 			break;
201 		default:
202 			usage();
203 		}
204 	argv += optind;
205 	argc -= optind;
206 
207 	if (lflag) {
208 		stdout_lock.l_len = 0;
209 		stdout_lock.l_start = 0;
210 		stdout_lock.l_type = F_WRLCK;
211 		stdout_lock.l_whence = SEEK_SET;
212 		if (fcntl(STDOUT_FILENO, F_SETLKW, &stdout_lock) == -1)
213 			err(EXIT_FAILURE, "stdout");
214 	}
215 
216 	init_casper(argc, argv);
217 
218 	caph_cache_catpages();
219 
220 	if (caph_enter_casper() < 0)
221 		err(EXIT_FAILURE, "capsicum");
222 
223 	if (bflag || eflag || nflag || sflag || tflag || vflag)
224 		scanfiles(argv, 1);
225 	else
226 		scanfiles(argv, 0);
227 	if (fclose(stdout))
228 		err(1, "stdout");
229 	exit(rval);
230 	/* NOTREACHED */
231 }
232 
233 static void
234 usage(void)
235 {
236 
237 	fprintf(stderr, "usage: cat [-" SUPPORTED_FLAGS "] [file ...]\n");
238 	exit(1);
239 	/* NOTREACHED */
240 }
241 
242 static void
243 scanfiles(char *argv[], int cooked __unused)
244 {
245 	int fd, i;
246 	char *path;
247 #ifndef BOOTSTRAP_CAT
248 	FILE *fp;
249 #endif
250 
251 	i = 0;
252 	fd = -1;
253 	while ((path = argv[i]) != NULL || i == 0) {
254 		if (path == NULL || strcmp(path, "-") == 0) {
255 			filename = "stdin";
256 			fd = STDIN_FILENO;
257 		} else {
258 			filename = path;
259 			fd = fileargs_open(fa, path);
260 #ifndef NO_UDOM_SUPPORT
261 			if (fd < 0 && errno == EOPNOTSUPP)
262 				fd = udom_open(path, O_RDONLY);
263 #endif
264 		}
265 		if (fd < 0) {
266 			warn("%s", path);
267 			rval = 1;
268 #ifndef BOOTSTRAP_CAT
269 		} else if (cooked) {
270 			if (fd == STDIN_FILENO)
271 				cook_cat(stdin);
272 			else {
273 				fp = fdopen(fd, "r");
274 				cook_cat(fp);
275 				fclose(fp);
276 			}
277 #endif
278 		} else {
279 #ifndef BOOTSTRAP_CAT
280 			if (in_kernel_copy(fd) == -1) {
281 				if (errno == EINVAL || errno == EBADF ||
282 				    errno == EISDIR)
283 					raw_cat(fd);
284 				else
285 					err(1, "stdout");
286 			}
287 #else
288 			raw_cat(fd);
289 #endif
290 			if (fd != STDIN_FILENO)
291 				close(fd);
292 		}
293 		if (path == NULL)
294 			break;
295 		++i;
296 	}
297 }
298 
299 #ifndef BOOTSTRAP_CAT
300 static void
301 cook_cat(FILE *fp)
302 {
303 	int ch, gobble, line, prev;
304 	wint_t wch;
305 
306 	/* Reset EOF condition on stdin. */
307 	if (fp == stdin && feof(stdin))
308 		clearerr(stdin);
309 
310 	line = gobble = 0;
311 	for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
312 		if (prev == '\n') {
313 			if (sflag) {
314 				if (ch == '\n') {
315 					if (gobble)
316 						continue;
317 					gobble = 1;
318 				} else
319 					gobble = 0;
320 			}
321 			if (nflag) {
322 				if (!bflag || ch != '\n') {
323 					(void)fprintf(stdout, "%6d\t", ++line);
324 					if (ferror(stdout))
325 						break;
326 				} else if (eflag) {
327 					(void)fprintf(stdout, "%6s\t", "");
328 					if (ferror(stdout))
329 						break;
330 				}
331 			}
332 		}
333 		if (ch == '\n') {
334 			if (eflag && putchar('$') == EOF)
335 				break;
336 		} else if (ch == '\t') {
337 			if (tflag) {
338 				if (putchar('^') == EOF || putchar('I') == EOF)
339 					break;
340 				continue;
341 			}
342 		} else if (vflag) {
343 			(void)ungetc(ch, fp);
344 			/*
345 			 * Our getwc(3) doesn't change file position
346 			 * on error.
347 			 */
348 			if ((wch = getwc(fp)) == WEOF) {
349 				if (ferror(fp) && errno == EILSEQ) {
350 					clearerr(fp);
351 					/* Resync attempt. */
352 					memset(&fp->_mbstate, 0, sizeof(mbstate_t));
353 					if ((ch = getc(fp)) == EOF)
354 						break;
355 					wch = ch;
356 					goto ilseq;
357 				} else
358 					break;
359 			}
360 			if (!iswascii(wch) && !iswprint(wch)) {
361 ilseq:
362 				if (putchar('M') == EOF || putchar('-') == EOF)
363 					break;
364 				wch = toascii(wch);
365 			}
366 			if (iswcntrl(wch)) {
367 				ch = toascii(wch);
368 				ch = (ch == '\177') ? '?' : (ch | 0100);
369 				if (putchar('^') == EOF || putchar(ch) == EOF)
370 					break;
371 				continue;
372 			}
373 			if (putwchar(wch) == WEOF)
374 				break;
375 			ch = -1;
376 			continue;
377 		}
378 		if (putchar(ch) == EOF)
379 			break;
380 	}
381 	if (ferror(fp)) {
382 		warn("%s", filename);
383 		rval = 1;
384 		clearerr(fp);
385 	}
386 	if (ferror(stdout))
387 		err(1, "stdout");
388 }
389 
390 static ssize_t
391 in_kernel_copy(int rfd)
392 {
393 	int wfd;
394 	ssize_t ret;
395 
396 	wfd = fileno(stdout);
397 	ret = 1;
398 
399 	while (ret > 0)
400 		ret = copy_file_range(rfd, NULL, wfd, NULL, SSIZE_MAX, 0);
401 
402 	return (ret);
403 }
404 #endif /* BOOTSTRAP_CAT */
405 
406 static void
407 raw_cat(int rfd)
408 {
409 	long pagesize;
410 	int off, wfd;
411 	ssize_t nr, nw;
412 	static size_t bsize;
413 	static char *buf = NULL;
414 	struct stat sbuf;
415 
416 	wfd = fileno(stdout);
417 	if (buf == NULL) {
418 		if (fstat(wfd, &sbuf))
419 			err(1, "stdout");
420 		if (S_ISREG(sbuf.st_mode)) {
421 			/* If there's plenty of RAM, use a large copy buffer */
422 			if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
423 				bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
424 			else
425 				bsize = BUFSIZE_SMALL;
426 		} else {
427 			bsize = sbuf.st_blksize;
428 			pagesize = sysconf(_SC_PAGESIZE);
429 			if (pagesize > 0)
430 				bsize = MAX(bsize, (size_t)pagesize);
431 		}
432 		if ((buf = malloc(bsize)) == NULL)
433 			err(1, "malloc() failure of IO buffer");
434 	}
435 	while ((nr = read(rfd, buf, bsize)) > 0)
436 		for (off = 0; nr; nr -= nw, off += nw)
437 			if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
438 				err(1, "stdout");
439 	if (nr < 0) {
440 		warn("%s", filename);
441 		rval = 1;
442 	}
443 }
444 
445 #ifndef NO_UDOM_SUPPORT
446 
447 static int
448 udom_open(const char *path, int flags)
449 {
450 	struct addrinfo hints, *res, *res0;
451 	char rpath[PATH_MAX];
452 	int error, fd, serrno;
453 	cap_rights_t rights;
454 
455 	/*
456 	 * Construct the unix domain socket address and attempt to connect.
457 	 */
458 	bzero(&hints, sizeof(hints));
459 	hints.ai_family = AF_LOCAL;
460 
461 	if (fileargs_realpath(fa, path, rpath) == NULL)
462 		return (-1);
463 
464 	error = cap_getaddrinfo(capnet, rpath, NULL, &hints, &res0);
465 	if (error) {
466 		warn("%s", gai_strerror(error));
467 		errno = EINVAL;
468 		return (-1);
469 	}
470 	cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE,
471 	    CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL);
472 
473 	/* Default error if something goes wrong. */
474 	serrno = EINVAL;
475 
476 	for (res = res0; res != NULL; res = res->ai_next) {
477 		fd = socket(res->ai_family, res->ai_socktype,
478 		    res->ai_protocol);
479 		if (fd < 0) {
480 			serrno = errno;
481 			freeaddrinfo(res0);
482 			errno = serrno;
483 			return (-1);
484 		}
485 		if (caph_rights_limit(fd, &rights) < 0) {
486 			serrno = errno;
487 			close(fd);
488 			freeaddrinfo(res0);
489 			errno = serrno;
490 			return (-1);
491 		}
492 		error = cap_connect(capnet, fd, res->ai_addr, res->ai_addrlen);
493 		if (error == 0)
494 			break;
495 		else {
496 			serrno = errno;
497 			close(fd);
498 		}
499 	}
500 	freeaddrinfo(res0);
501 
502 	if (res == NULL) {
503 		errno = serrno;
504 		return (-1);
505 	}
506 
507 	/*
508 	 * handle the open flags by shutting down appropriate directions
509 	 */
510 
511 	switch (flags & O_ACCMODE) {
512 	case O_RDONLY:
513 		cap_rights_clear(&rights, CAP_WRITE);
514 		if (shutdown(fd, SHUT_WR) == -1)
515 			warn(NULL);
516 		break;
517 	case O_WRONLY:
518 		cap_rights_clear(&rights, CAP_READ);
519 		if (shutdown(fd, SHUT_RD) == -1)
520 			warn(NULL);
521 		break;
522 	default:
523 		break;
524 	}
525 
526 	cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN);
527 	if (caph_rights_limit(fd, &rights) < 0) {
528 		serrno = errno;
529 		close(fd);
530 		errno = serrno;
531 		return (-1);
532 	}
533 	return (fd);
534 }
535 
536 #endif
537