1331722Seadler/*
21590Srgrimes * Copyright (c) 1988, 1993
31590Srgrimes *	The Regents of the University of California.  All rights reserved.
41590Srgrimes *
51590Srgrimes * Redistribution and use in source and binary forms, with or without
61590Srgrimes * modification, are permitted provided that the following conditions
71590Srgrimes * are met:
81590Srgrimes * 1. Redistributions of source code must retain the above copyright
91590Srgrimes *    notice, this list of conditions and the following disclaimer.
101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111590Srgrimes *    notice, this list of conditions and the following disclaimer in the
121590Srgrimes *    documentation and/or other materials provided with the distribution.
131590Srgrimes * 4. Neither the name of the University nor the names of its contributors
141590Srgrimes *    may be used to endorse or promote products derived from this software
151590Srgrimes *    without specific prior written permission.
161590Srgrimes *
171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201590Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271590Srgrimes * SUCH DAMAGE.
281590Srgrimes */
291590Srgrimes
301590Srgrimes#ifndef lint
3128199Scharnierstatic const char copyright[] =
321590Srgrimes"@(#) Copyright (c) 1988, 1993\n\
331590Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341590Srgrimes#endif /* not lint */
351590Srgrimes
361590Srgrimes#ifndef lint
3728199Scharnier#if 0
381590Srgrimesstatic char sccsid[] = "@(#)tee.c	8.1 (Berkeley) 6/6/93";
3928199Scharnier#endif
4028199Scharnierstatic const char rcsid[] =
4150477Speter  "$FreeBSD: stable/11/usr.bin/tee/tee.c 307773 2016-10-22 13:20:02Z bapt $";
421590Srgrimes#endif /* not lint */
431590Srgrimes
44307773Sbapt#include <sys/capsicum.h>
45307773Sbapt#include <sys/stat.h>
461590Srgrimes#include <sys/types.h>
47307773Sbapt
4828199Scharnier#include <err.h>
49307773Sbapt#include <errno.h>
5028199Scharnier#include <fcntl.h>
511590Srgrimes#include <signal.h>
521590Srgrimes#include <stdio.h>
531590Srgrimes#include <stdlib.h>
54200462Sdelphij#include <string.h>
55307773Sbapt#include <termios.h>
5628199Scharnier#include <unistd.h>
571590Srgrimes
581590Srgrimestypedef struct _list {
591590Srgrimes	struct _list *next;
601590Srgrimes	int fd;
6187303Sdwmalone	const char *name;
621590Srgrimes} LIST;
63227187Sedstatic LIST *head;
641590Srgrimes
65227187Sedstatic void add(int, const char *);
6692922Simpstatic void usage(void);
671590Srgrimes
681590Srgrimesint
69102944Sdwmalonemain(int argc, char *argv[])
701590Srgrimes{
71102944Sdwmalone	LIST *p;
72102944Sdwmalone	int n, fd, rval, wval;
73102944Sdwmalone	char *bp;
741590Srgrimes	int append, ch, exitval;
751590Srgrimes	char *buf;
76307773Sbapt	cap_rights_t rights;
77307773Sbapt	unsigned long cmd;
781590Srgrimes#define	BSIZE (8 * 1024)
791590Srgrimes
801590Srgrimes	append = 0;
8124360Simp	while ((ch = getopt(argc, argv, "ai")) != -1)
821590Srgrimes		switch((char)ch) {
831590Srgrimes		case 'a':
841590Srgrimes			append = 1;
851590Srgrimes			break;
861590Srgrimes		case 'i':
871590Srgrimes			(void)signal(SIGINT, SIG_IGN);
881590Srgrimes			break;
891590Srgrimes		case '?':
901590Srgrimes		default:
9128199Scharnier			usage();
921590Srgrimes		}
931590Srgrimes	argv += optind;
941590Srgrimes	argc -= optind;
951590Srgrimes
9696776Sjmallett	if ((buf = malloc(BSIZE)) == NULL)
9799433Stjr		err(1, "malloc");
981590Srgrimes
99307773Sbapt	cap_rights_init(&rights, CAP_READ, CAP_FSTAT);
100307773Sbapt	if (cap_rights_limit(STDIN_FILENO, &rights) < 0 && errno != ENOSYS)
101307773Sbapt		err(EXIT_FAILURE, "unable to limit rights for stdin");
102307773Sbapt	cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL);
103307773Sbapt	if (cap_rights_limit(STDERR_FILENO, &rights) < 0 && errno != ENOSYS)
104307773Sbapt		err(EXIT_FAILURE, "unable to limit rights for stderr");
105307773Sbapt	cmd = TIOCGETA;
106307773Sbapt	if (cap_ioctls_limit(STDERR_FILENO, &cmd, 1) < 0 && errno != ENOSYS)
107307773Sbapt		err(EXIT_FAILURE, "unable to limit ioctls for stderr");
108307773Sbapt
1091590Srgrimes	add(STDOUT_FILENO, "stdout");
1101590Srgrimes
1111590Srgrimes	for (exitval = 0; *argv; ++argv)
1121590Srgrimes		if ((fd = open(*argv, append ? O_WRONLY|O_CREAT|O_APPEND :
1131590Srgrimes		    O_WRONLY|O_CREAT|O_TRUNC, DEFFILEMODE)) < 0) {
11428199Scharnier			warn("%s", *argv);
1151590Srgrimes			exitval = 1;
1161590Srgrimes		} else
1171590Srgrimes			add(fd, *argv);
1181590Srgrimes
119307773Sbapt	if (cap_enter() < 0 && errno != ENOSYS)
120307773Sbapt		err(EXIT_FAILURE, "unable to enter capability mode");
1211590Srgrimes	while ((rval = read(STDIN_FILENO, buf, BSIZE)) > 0)
1221590Srgrimes		for (p = head; p; p = p->next) {
1231590Srgrimes			n = rval;
1241590Srgrimes			bp = buf;
1251590Srgrimes			do {
1261590Srgrimes				if ((wval = write(p->fd, bp, n)) == -1) {
127231524Scracauer					warn("%s", p->name);
128231524Scracauer					exitval = 1;
129231524Scracauer					break;
1301590Srgrimes				}
1311590Srgrimes				bp += wval;
1321590Srgrimes			} while (n -= wval);
1331590Srgrimes		}
1341590Srgrimes	if (rval < 0)
13528199Scharnier		err(1, "read");
13696803Sjmallett	exit(exitval);
1371590Srgrimes}
1381590Srgrimes
13928199Scharnierstatic void
140102944Sdwmaloneusage(void)
14128199Scharnier{
14228199Scharnier	(void)fprintf(stderr, "usage: tee [-ai] [file ...]\n");
14328199Scharnier	exit(1);
14428199Scharnier}
14528199Scharnier
146227187Sedstatic void
147102944Sdwmaloneadd(int fd, const char *name)
1481590Srgrimes{
1491590Srgrimes	LIST *p;
150307773Sbapt	cap_rights_t rights;
151307773Sbapt	unsigned long cmd;
1521590Srgrimes
153307773Sbapt	if (fd == STDOUT_FILENO)
154307773Sbapt		cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT, CAP_IOCTL);
155307773Sbapt	else
156307773Sbapt		cap_rights_init(&rights, CAP_WRITE, CAP_FSTAT);
157307773Sbapt	if (cap_rights_limit(fd, &rights) < 0 && errno != ENOSYS)
158307773Sbapt		err(EXIT_FAILURE, "unable to limit rights");
159307773Sbapt
160307773Sbapt	if (fd == STDOUT_FILENO) {
161307773Sbapt		cmd = TIOCGETA;
162307773Sbapt		if (cap_ioctls_limit(fd, &cmd, 1) < 0 && errno != ENOSYS)
163307773Sbapt			err(EXIT_FAILURE, "unable to limit ioctls for stdout");
164307773Sbapt	}
165307773Sbapt
16696776Sjmallett	if ((p = malloc(sizeof(LIST))) == NULL)
16799433Stjr		err(1, "malloc");
1681590Srgrimes	p->fd = fd;
1691590Srgrimes	p->name = name;
1701590Srgrimes	p->next = head;
1711590Srgrimes	head = p;
1721590Srgrimes}
173