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