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