]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - bin/cat/cat.c
ssh: Update to OpenSSH 9.5p1
[FreeBSD/FreeBSD.git] / bin / cat / cat.c
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                                         raw_cat(fd);
286                                 else
287                                         err(1, "stdout");
288                         }
289 #else
290                         raw_cat(fd);
291 #endif
292                         if (fd != STDIN_FILENO)
293                                 close(fd);
294                 }
295                 if (path == NULL)
296                         break;
297                 ++i;
298         }
299 }
300
301 #ifndef BOOTSTRAP_CAT
302 static void
303 cook_cat(FILE *fp)
304 {
305         int ch, gobble, line, prev;
306         wint_t wch;
307
308         /* Reset EOF condition on stdin. */
309         if (fp == stdin && feof(stdin))
310                 clearerr(stdin);
311
312         line = gobble = 0;
313         for (prev = '\n'; (ch = getc(fp)) != EOF; prev = ch) {
314                 if (prev == '\n') {
315                         if (sflag) {
316                                 if (ch == '\n') {
317                                         if (gobble)
318                                                 continue;
319                                         gobble = 1;
320                                 } else
321                                         gobble = 0;
322                         }
323                         if (nflag) {
324                                 if (!bflag || ch != '\n') {
325                                         (void)fprintf(stdout, "%6d\t", ++line);
326                                         if (ferror(stdout))
327                                                 break;
328                                 } else if (eflag) {
329                                         (void)fprintf(stdout, "%6s\t", "");
330                                         if (ferror(stdout))
331                                                 break;
332                                 }
333                         }
334                 }
335                 if (ch == '\n') {
336                         if (eflag && putchar('$') == EOF)
337                                 break;
338                 } else if (ch == '\t') {
339                         if (tflag) {
340                                 if (putchar('^') == EOF || putchar('I') == EOF)
341                                         break;
342                                 continue;
343                         }
344                 } else if (vflag) {
345                         (void)ungetc(ch, fp);
346                         /*
347                          * Our getwc(3) doesn't change file position
348                          * on error.
349                          */
350                         if ((wch = getwc(fp)) == WEOF) {
351                                 if (ferror(fp) && errno == EILSEQ) {
352                                         clearerr(fp);
353                                         /* Resync attempt. */
354                                         memset(&fp->_mbstate, 0, sizeof(mbstate_t));
355                                         if ((ch = getc(fp)) == EOF)
356                                                 break;
357                                         wch = ch;
358                                         goto ilseq;
359                                 } else
360                                         break;
361                         }
362                         if (!iswascii(wch) && !iswprint(wch)) {
363 ilseq:
364                                 if (putchar('M') == EOF || putchar('-') == EOF)
365                                         break;
366                                 wch = toascii(wch);
367                         }
368                         if (iswcntrl(wch)) {
369                                 ch = toascii(wch);
370                                 ch = (ch == '\177') ? '?' : (ch | 0100);
371                                 if (putchar('^') == EOF || putchar(ch) == EOF)
372                                         break;
373                                 continue;
374                         }
375                         if (putwchar(wch) == WEOF)
376                                 break;
377                         ch = -1;
378                         continue;
379                 }
380                 if (putchar(ch) == EOF)
381                         break;
382         }
383         if (ferror(fp)) {
384                 warn("%s", filename);
385                 rval = 1;
386                 clearerr(fp);
387         }
388         if (ferror(stdout))
389                 err(1, "stdout");
390 }
391
392 static ssize_t
393 in_kernel_copy(int rfd)
394 {
395         int wfd;
396         ssize_t ret;
397
398         wfd = fileno(stdout);
399         ret = 1;
400
401         while (ret > 0)
402                 ret = copy_file_range(rfd, NULL, wfd, NULL, SSIZE_MAX, 0);
403
404         return (ret);
405 }
406 #endif /* BOOTSTRAP_CAT */
407
408 static void
409 raw_cat(int rfd)
410 {
411         long pagesize;
412         int off, wfd;
413         ssize_t nr, nw;
414         static size_t bsize;
415         static char *buf = NULL;
416         struct stat sbuf;
417
418         wfd = fileno(stdout);
419         if (buf == NULL) {
420                 if (fstat(wfd, &sbuf))
421                         err(1, "stdout");
422                 if (S_ISREG(sbuf.st_mode)) {
423                         /* If there's plenty of RAM, use a large copy buffer */
424                         if (sysconf(_SC_PHYS_PAGES) > PHYSPAGES_THRESHOLD)
425                                 bsize = MIN(BUFSIZE_MAX, MAXPHYS * 8);
426                         else
427                                 bsize = BUFSIZE_SMALL;
428                 } else {
429                         bsize = sbuf.st_blksize;
430                         pagesize = sysconf(_SC_PAGESIZE);
431                         if (pagesize > 0)
432                                 bsize = MAX(bsize, (size_t)pagesize);
433                 }
434                 if ((buf = malloc(bsize)) == NULL)
435                         err(1, "malloc() failure of IO buffer");
436         }
437         while ((nr = read(rfd, buf, bsize)) > 0)
438                 for (off = 0; nr; nr -= nw, off += nw)
439                         if ((nw = write(wfd, buf + off, (size_t)nr)) < 0)
440                                 err(1, "stdout");
441         if (nr < 0) {
442                 warn("%s", filename);
443                 rval = 1;
444         }
445 }
446
447 #ifndef NO_UDOM_SUPPORT
448
449 static int
450 udom_open(const char *path, int flags)
451 {
452         struct addrinfo hints, *res, *res0;
453         char rpath[PATH_MAX];
454         int error, fd, serrno;
455         cap_rights_t rights;
456
457         /*
458          * Construct the unix domain socket address and attempt to connect.
459          */
460         bzero(&hints, sizeof(hints));
461         hints.ai_family = AF_LOCAL;
462
463         if (fileargs_realpath(fa, path, rpath) == NULL)
464                 return (-1);
465
466         error = cap_getaddrinfo(capnet, rpath, NULL, &hints, &res0);
467         if (error) {
468                 warn("%s", gai_strerror(error));
469                 errno = EINVAL;
470                 return (-1);
471         }
472         cap_rights_init(&rights, CAP_CONNECT, CAP_READ, CAP_WRITE,
473             CAP_SHUTDOWN, CAP_FSTAT, CAP_FCNTL);
474
475         /* Default error if something goes wrong. */
476         serrno = EINVAL;
477
478         for (res = res0; res != NULL; res = res->ai_next) {
479                 fd = socket(res->ai_family, res->ai_socktype,
480                     res->ai_protocol);
481                 if (fd < 0) {
482                         serrno = errno;
483                         freeaddrinfo(res0);
484                         errno = serrno;
485                         return (-1);
486                 }
487                 if (caph_rights_limit(fd, &rights) < 0) {
488                         serrno = errno;
489                         close(fd);
490                         freeaddrinfo(res0);
491                         errno = serrno;
492                         return (-1);
493                 }
494                 error = cap_connect(capnet, fd, res->ai_addr, res->ai_addrlen);
495                 if (error == 0)
496                         break;
497                 else {
498                         serrno = errno;
499                         close(fd);
500                 }
501         }
502         freeaddrinfo(res0);
503
504         if (res == NULL) {
505                 errno = serrno;
506                 return (-1);
507         }
508
509         /*
510          * handle the open flags by shutting down appropriate directions
511          */
512
513         switch (flags & O_ACCMODE) {
514         case O_RDONLY:
515                 cap_rights_clear(&rights, CAP_WRITE);
516                 if (shutdown(fd, SHUT_WR) == -1)
517                         warn(NULL);
518                 break;
519         case O_WRONLY:
520                 cap_rights_clear(&rights, CAP_READ);
521                 if (shutdown(fd, SHUT_RD) == -1)
522                         warn(NULL);
523                 break;
524         default:
525                 break;
526         }
527
528         cap_rights_clear(&rights, CAP_CONNECT, CAP_SHUTDOWN);
529         if (caph_rights_limit(fd, &rights) < 0) {
530                 serrno = errno;
531                 close(fd);
532                 errno = serrno;
533                 return (-1);
534         }
535         return (fd);
536 }
537
538 #endif