xref: /openbsd-src/usr.bin/wall/ttymsg.c (revision d13be5d47e4149db2549a9828e244d59dbc43f15)
1 /*	$OpenBSD: ttymsg.c,v 1.16 2009/10/27 23:59:49 deraadt 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/uio.h>
35 #include <signal.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <paths.h>
40 #include <unistd.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <stdlib.h>
44 #include <sys/stat.h>
45 
46 char *ttymsg(struct iovec *, int, char *, int);
47 
48 /*
49  * Display the contents of a uio structure on a terminal.  Used by wall(1)
50  * and talkd(8).  Forks and finishes in child if write would block,
51  * waiting up to tmout seconds.  Returns pointer to error string on unexpected
52  * error; string is not newline-terminated.  Various "normal" errors are
53  * ignored (exclusive-use, lack of permission, etc.).
54  */
55 char *
56 ttymsg(iov, iovcnt, line, tmout)
57 	struct iovec *iov;
58 	int iovcnt;
59 	char *line;
60 	int tmout;
61 {
62 	static char device[MAXNAMLEN] = _PATH_DEV;
63 	static char errbuf[1024];
64 	int cnt, fd, left, wret;
65 	struct iovec localiov[6];
66 	int forked = 0;
67 	struct stat st;
68 	sigset_t mask;
69 
70 	if (iovcnt > sizeof(localiov) / sizeof(localiov[0]))
71 		return ("too many iov's (change code in wall/ttymsg.c)");
72 
73 	/*
74 	 * Ignore lines that start with "ftp" or "uucp".
75 	 */
76 	if ((strncmp(line, "ftp", 3) == 0) ||
77 	    (strncmp(line, "uucp", 4) == 0))
78 		return (NULL);
79 
80 	(void) strlcpy(device + sizeof(_PATH_DEV) - 1, line,
81 	    sizeof(device) - (sizeof(_PATH_DEV) - 1));
82 	if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
83 		/* A slash is an attempt to break security... */
84 		(void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"",
85 		    device);
86 		return (errbuf);
87 	}
88 
89 	if (getuid()) {
90 		if (stat(device, &st) < 0)
91 			return (NULL);
92 		if ((st.st_mode & S_IWGRP) == 0)
93 			return (NULL);
94 	}
95 
96 	/*
97 	 * open will fail on slip lines or exclusive-use lines
98 	 * if not running as root; not an error.
99 	 */
100 	if ((fd = open(device, O_WRONLY|O_NONBLOCK, 0)) < 0) {
101 		if (errno == EBUSY || errno == EACCES)
102 			return (NULL);
103 		(void) snprintf(errbuf, sizeof(errbuf),
104 		    "%s: %s", device, strerror(errno));
105 		return (errbuf);
106 	}
107 
108 	for (cnt = left = 0; cnt < iovcnt; ++cnt)
109 		left += iov[cnt].iov_len;
110 
111 	for (;;) {
112 		wret = writev(fd, iov, iovcnt);
113 		if (wret >= left)
114 			break;
115 		if (wret >= 0) {
116 			left -= wret;
117 			if (iov != localiov) {
118 				bcopy(iov, localiov,
119 				    iovcnt * sizeof(struct iovec));
120 				iov = localiov;
121 			}
122 			for (cnt = 0; wret >= iov->iov_len; ++cnt) {
123 				wret -= iov->iov_len;
124 				++iov;
125 				--iovcnt;
126 			}
127 			if (wret) {
128 				char *base = iov->iov_base;
129 
130 				iov->iov_base = base + wret;
131 				iov->iov_len -= wret;
132 			}
133 			continue;
134 		}
135 		if (errno == EWOULDBLOCK) {
136 			int off = 0;
137 			pid_t cpid;
138 
139 			if (forked) {
140 				(void) close(fd);
141 				_exit(1);
142 			}
143 			cpid = fork();
144 			if (cpid < 0) {
145 				(void) snprintf(errbuf, sizeof(errbuf),
146 				    "fork: %s", strerror(errno));
147 				(void) close(fd);
148 				return (errbuf);
149 			}
150 			if (cpid) {	/* parent */
151 				(void) close(fd);
152 				return (NULL);
153 			}
154 			forked++;
155 			/* wait at most tmout seconds */
156 			(void) signal(SIGALRM, SIG_DFL);
157 			(void) signal(SIGTERM, SIG_DFL); /* XXX */
158 			(void) sigemptyset(&mask);
159 			(void) sigprocmask(SIG_SETMASK, &mask, NULL);
160 			(void) alarm((u_int)tmout);
161 			(void) fcntl(fd, O_NONBLOCK, &off);
162 			continue;
163 		}
164 		/*
165 		 * We get ENODEV on a slip line if we're running as root,
166 		 * and EIO if the line just went away.
167 		 */
168 		if (errno == ENODEV || errno == EIO)
169 			break;
170 		(void) close(fd);
171 		if (forked)
172 			_exit(1);
173 		(void) snprintf(errbuf, sizeof(errbuf),
174 		    "%s: %s", device, strerror(errno));
175 		return (errbuf);
176 	}
177 
178 	(void) close(fd);
179 	if (forked)
180 		_exit(0);
181 	return (NULL);
182 }
183