xref: /openbsd-src/usr.sbin/syslogd/ttymsg.c (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1 /*	$OpenBSD: ttymsg.c,v 1.11 2016/08/16 18:41:57 tedu 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/stat.h>
34 
35 #include <dirent.h>
36 #include <errno.h>
37 #include <event.h>
38 #include <paths.h>
39 #include <signal.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <unistd.h>
44 
45 #include "syslogd.h"
46 
47 #ifndef nitems
48 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
49 #endif
50 
51 struct tty_delay {
52 	struct event	 td_event;
53 	size_t		 td_length;
54 	char		 td_line[MAXLINE];
55 };
56 int tty_delayed = 0;
57 void ttycb(int, short, void *);
58 
59 /*
60  * Display the contents of a uio structure on a terminal.
61  * Schedules an event if write would block, waiting up to TTYMSGTIME
62  * seconds.  Returns pointer to error string on unexpected error;
63  * string is not newline-terminated.  Various "normal" errors are ignored
64  * (exclusive-use, lack of permission, etc.).
65  */
66 char *
67 ttymsg(struct iovec *iov, int iovcnt, char *utline)
68 {
69 	static char device[MAXNAMLEN] = _PATH_DEV;
70 	static char ebuf[ERRBUFSIZE];
71 	int cnt, fd;
72 	size_t left;
73 	ssize_t wret;
74 	struct iovec localiov[6];
75 
76 	if (iovcnt < 0 || (size_t)iovcnt > nitems(localiov))
77 		return ("too many iov's (change code in syslogd/ttymsg.c)");
78 
79 	/*
80 	 * Ignore lines that start with "ftp" or "uucp".
81 	 */
82 	if ((strncmp(utline, "ftp", 3) == 0) ||
83 	    (strncmp(utline, "uucp", 4) == 0))
84 		return (NULL);
85 
86 	(void) strlcpy(device + sizeof(_PATH_DEV) - 1, utline,
87 	    sizeof(device) - (sizeof(_PATH_DEV) - 1));
88 	if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) {
89 		/* A slash is an attempt to break security... */
90 		(void) snprintf(ebuf, sizeof(ebuf), "'/' in \"%s\"",
91 		    device);
92 		return (ebuf);
93 	}
94 
95 	/*
96 	 * open will fail on slip lines or exclusive-use lines
97 	 * if not running as root; not an error.
98 	 */
99 	if ((fd = priv_open_tty(device)) < 0) {
100 		if (errno == EBUSY || errno == EACCES)
101 			return (NULL);
102 		(void) snprintf(ebuf, sizeof(ebuf),
103 		    "%s: %s", device, strerror(errno));
104 		return (ebuf);
105 	}
106 
107 	left = 0;
108 	for (cnt = 0; cnt < iovcnt; ++cnt)
109 		left += iov[cnt].iov_len;
110 
111 	for (;;) {
112 		wret = writev(fd, iov, iovcnt);
113 		if (wret >= 0) {
114 			if ((size_t)wret >= left)
115 				break;
116 			left -= wret;
117 			if (iov != localiov) {
118 				bcopy(iov, localiov,
119 				    iovcnt * sizeof(struct iovec));
120 				iov = localiov;
121 			}
122 			while ((size_t)wret >= iov->iov_len) {
123 				wret -= iov->iov_len;
124 				++iov;
125 				--iovcnt;
126 			}
127 			if (wret) {
128 				iov->iov_base = (char *)iov->iov_base + wret;
129 				iov->iov_len -= wret;
130 			}
131 			continue;
132 		}
133 		if (errno == EWOULDBLOCK) {
134 			struct tty_delay	*td;
135 			struct timeval		 to;
136 
137 			if (tty_delayed >= TTYMAXDELAY) {
138 				(void) snprintf(ebuf, sizeof(ebuf),
139 				    "%s: too many delayed writes", device);
140 				return (ebuf);
141 			}
142 			logdebug("ttymsg delayed write\n");
143 			if (iov != localiov) {
144 				bcopy(iov, localiov,
145 				    iovcnt * sizeof(struct iovec));
146 				iov = localiov;
147 			}
148 			if ((td = malloc(sizeof(*td))) == NULL) {
149 				(void) snprintf(ebuf, sizeof(ebuf),
150 				    "%s: malloc: %s", device, strerror(errno));
151 				return (ebuf);
152 			}
153 			td->td_length = 0;
154 			if (left > MAXLINE)
155 				left = MAXLINE;
156 			while (iovcnt && left) {
157 				if (iov->iov_len > left)
158 					iov->iov_len = left;
159 				memcpy(td->td_line + td->td_length,
160 				    iov->iov_base, iov->iov_len);
161 				td->td_length += iov->iov_len;
162 				left -= iov->iov_len;
163 				++iov;
164 				--iovcnt;
165 			}
166 			tty_delayed++;
167 			event_set(&td->td_event, fd, EV_WRITE, ttycb, td);
168 			to.tv_sec = TTYMSGTIME;
169 			to.tv_usec = 0;
170 			event_add(&td->td_event, &to);
171 			return (NULL);
172 		}
173 		/*
174 		 * We get ENODEV on a slip line if we're running as root,
175 		 * and EIO if the line just went away.
176 		 */
177 		if (errno == ENODEV || errno == EIO)
178 			break;
179 		(void) close(fd);
180 		(void) snprintf(ebuf, sizeof(ebuf),
181 		    "%s: %s", device, strerror(errno));
182 		return (ebuf);
183 	}
184 
185 	(void) close(fd);
186 	return (NULL);
187 }
188 
189 void
190 ttycb(int fd, short event, void *arg)
191 {
192 	struct tty_delay	*td = arg;
193 	struct timeval		 to;
194 	ssize_t			 wret;
195 
196 	if (event != EV_WRITE)
197 		goto done;
198 
199 	wret = write(fd, td->td_line, td->td_length);
200 	if (wret < 0 && errno != EINTR && errno != EWOULDBLOCK)
201 		goto done;
202 	if (wret > 0) {
203 		td->td_length -= wret;
204 		if (td->td_length == 0)
205 			goto done;
206 		memmove(td->td_line, td->td_line + wret, td->td_length);
207 	}
208 	to.tv_sec = TTYMSGTIME;
209 	to.tv_usec = 0;
210 	event_add(&td->td_event, &to);
211 	return;
212 
213  done:
214 	tty_delayed--;
215 	close(fd);
216 	free(td);
217 }
218