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