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