tee.c revision 330897
1/*-
2 * SPDX-License-Identifier: BSD-3-Clause
3 *
4 * Copyright (c) 1988, 1993
5 *	The Regents of the University of California.  All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 *    may be used to endorse or promote products derived from this software
17 *    without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32#ifndef lint
33static const char copyright[] =
34"@(#) Copyright (c) 1988, 1993\n\
35	The Regents of the University of California.  All rights reserved.\n";
36#endif /* not lint */
37
38#ifndef lint
39#if 0
40static char sccsid[] = "@(#)tee.c	8.1 (Berkeley) 6/6/93";
41#endif
42static const char rcsid[] =
43  "$FreeBSD: stable/11/usr.bin/tee/tee.c 330897 2018-03-14 03:19:51Z eadler $";
44#endif /* not lint */
45
46#include <sys/capsicum.h>
47#include <sys/stat.h>
48#include <sys/types.h>
49
50#include <err.h>
51#include <errno.h>
52#include <fcntl.h>
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57#include <termios.h>
58#include <unistd.h>
59
60typedef struct _list {
61	struct _list *next;
62	int fd;
63	const char *name;
64} LIST;
65static LIST *head;
66
67static void add(int, const char *);
68static void usage(void);
69
70int
71main(int argc, char *argv[])
72{
73	LIST *p;
74	int n, fd, rval, wval;
75	char *bp;
76	int append, ch, exitval;
77	char *buf;
78	cap_rights_t rights;
79	unsigned long cmd;
80#define	BSIZE (8 * 1024)
81
82	append = 0;
83	while ((ch = getopt(argc, argv, "ai")) != -1)
84		switch((char)ch) {
85		case 'a':
86			append = 1;
87			break;
88		case 'i':
89			(void)signal(SIGINT, SIG_IGN);
90			break;
91		case '?':
92		default:
93			usage();
94		}
95	argv += optind;
96	argc -= optind;
97
98	if ((buf = malloc(BSIZE)) == NULL)
99		err(1, "malloc");
100
101	cap_rights_init(&rights, CAP_READ, CAP_FSTAT);
102	if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
103		err(EXIT_FAILURE, "unable to limit rights for stdin");
104	cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL);
105	if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
106		err(EXIT_FAILURE, "unable to limit rights for stderr");
107	cmd = TIOCGETA;
108	if (cap_ioctls_limit(STDERR_FILENO, &cmd, 1) < 0 && errno != ENOSYS)
109		err(EXIT_FAILURE, "unable to limit ioctls for stderr");
110
111	add(STDOUT_FILENO, "stdout");
112
113	for (exitval = 0; *argv; ++argv)
114		if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
115		    O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
116			warn("%s", *argv);
117			exitval = 1;
118		} else
119			add(fd, *argv);
120
121	if (cap_enter() < 0 && errno != ENOSYS)
122		err(EXIT_FAILURE, "unable to enter capability mode");
123	while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
124		for (p = head; p; p = p->next) {
125			n = rval;
126			bp = buf;
127			do {
128				if ((wval = write(p->fd, bp, n)) == -1) {
129					warn("%s", p->name);
130					exitval = 1;
131					break;
132				}
133				bp += wval;
134			} while (n -= wval);
135		}
136	if (rval < 0)
137		err(1, "read");
138	exit(exitval);
139}
140
141static void
142usage(void)
143{
144	(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
145	exit(1);
146}
147
148static void
149add(int fd, const char *name)
150{
151	LIST *p;
152	cap_rights_t rights;
153	unsigned long cmd;
154
155	if (fd == STDOUT_FILENO)
156		cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL);
157	else
158		cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT);
159	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
160		err(EXIT_FAILURE, "unable to limit rights");
161
162	if (fd == STDOUT_FILENO) {
163		cmd = TIOCGETA;
164		if (cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
165			err(EXIT_FAILURE, "unable to limit ioctls for stdout");
166	}
167
168	if ((p = malloc(sizeof(LIST))) == NULL)
169		err(1, "malloc");
170	p->fd = fd;
171	p->name = name;
172	p->next = head;
173	head = p;
174}
175