1/*	$OpenBSD: ttymsg.c,v 1.21 2023/05/02 09:51:22 tb Exp $	*/
2/*	$NetBSD: ttymsg.c,v 1.3 1994/11/17 07:17:55 jtc Exp $	*/
3
4/*
5 * Copyright (c) 1989, 1993
6 *	The Regents of the University of California.  All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 *    may be used to endorse or promote products derived from this software
18 *    without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 */
32
33#include <sys/types.h>
34#include <sys/stat.h>
35#include <sys/uio.h>
36#include <signal.h>
37#include <fcntl.h>
38#include <dirent.h>
39#include <errno.h>
40#include <paths.h>
41#include <unistd.h>
42#include <stdio.h>
43#include <string.h>
44#include <stdlib.h>
45#include <err.h>
46
47char *ttymsg(struct iovec *, int, char *, int);
48
49/*
50 * Display the contents of a uio structure on a terminal.  Used by wall(1)
51 * and talkd(8).  Forks and finishes in child if write would block,
52 * waiting up to tmout seconds.  Returns pointer to error string on unexpected
53 * error; string is not newline-terminated.  Various "normal" errors are
54 * ignored (exclusive-use, lack of permission, etc.).
55 */
56char *
57ttymsg(struct iovec *iov, int iovcnt, char *line, int tmout)
58{
59	static char device[MAXNAMLEN] = _PATH_DEV;
60	static char errbuf[1024];
61	int cnt, fd, left, wret;
62	struct iovec localiov[6];
63	int forked = 0;
64	struct stat st;
65	sigset_t mask;
66
67	if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
68		return ("too many iov's (change code in wall/ttymsg.c)");
69
70	/*
71	 * Ignore lines that start with "ftp" or "uucp".
72	 */
73	if ((strncmp(line, "ftp", 3) == 0) ||
74	    (strncmp(line, "uucp", 4) == 0))
75		return (NULL);
76
77	(void) strlcpy(device + sizeof(_PATH_DEV) - 1, line,
78	    sizeof(device) - (sizeof(_PATH_DEV) - 1));
79	if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
80		/* A slash is an attempt to break security... */
81		(void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
82		    device);
83		return (errbuf);
84	}
85
86	/*
87	 * open will fail on slip lines or exclusive-use lines
88	 * if not running as root; not an error.
89	 */
90	if ((fd = open(device, O_WRONLY|O_NONBLOCK)) == -1) {
91		if (errno == EBUSY || errno == EACCES)
92			return (NULL);
93		(void) snprintf(errbuf, sizeof(errbuf),
94		    "%s: %s", device, strerror(errno));
95		return (errbuf);
96	}
97
98	if (getuid()) {
99		if (fstat(fd, &st) == -1 ||
100		    (st.st_mode & S_IWGRP) == 0) {
101			close(fd);
102			return (NULL);
103		}
104	}
105
106	for (cnt = left = 0; cnt < iovcnt; ++cnt)
107		left += iov[cnt].iov_len;
108
109	for (;;) {
110		wret = writev(fd, iov, iovcnt);
111		if (wret >= left)
112			break;
113		if (wret >= 0) {
114			left -= wret;
115			if (iov != localiov) {
116				bcopy(iov, localiov,
117				    iovcnt * sizeof(struct iovec));
118				iov = localiov;
119			}
120			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
121				wret -= iov->iov_len;
122				++iov;
123				--iovcnt;
124			}
125			if (wret) {
126				char *base = iov->iov_base;
127
128				iov->iov_base = base + wret;
129				iov->iov_len -= wret;
130			}
131			continue;
132		}
133		if (errno == EWOULDBLOCK) {
134			int off = 0;
135			pid_t cpid;
136
137			if (forked) {
138				(void) close(fd);
139				_exit(1);
140			}
141			cpid = fork();
142			if (cpid == -1) {
143				(void) snprintf(errbuf, sizeof(errbuf),
144				    "fork: %s", strerror(errno));
145				(void) close(fd);
146				return (errbuf);
147			}
148			if (cpid) {	/* parent */
149				(void) close(fd);
150				return (NULL);
151			}
152
153			if (pledge("stdio", NULL) == -1)
154				err(1, "pledge");
155
156			forked++;
157			/* wait at most tmout seconds */
158			(void) signal(SIGALRM, SIG_DFL);
159			(void) signal(SIGTERM, SIG_DFL); /* XXX */
160			(void) sigemptyset(&mask);
161			(void) sigprocmask(SIG_SETMASK, &mask, NULL);
162			(void) alarm((u_int)tmout);
163			(void) fcntl(fd, O_NONBLOCK, &off);
164			continue;
165		}
166		/*
167		 * We get ENODEV on a slip line if we're running as root,
168		 * and EIO if the line just went away.
169		 */
170		if (errno == ENODEV || errno == EIO)
171			break;
172		(void) close(fd);
173		if (forked)
174			_exit(1);
175		(void) snprintf(errbuf, sizeof(errbuf),
176		    "%s: %s", device, strerror(errno));
177		return (errbuf);
178	}
179
180	(void) close(fd);
181	if (forked)
182		_exit(0);
183	return (NULL);
184}
185