1 /* $OpenBSD: ttymsg.c,v 1.4 2009/10/27 23:59:54 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 #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, wret; 61 struct iovec localiov[6]; 62 int forked = 0; 63 sigset_t mask; 64 65 if (iovcnt > sizeof(localiov) / sizeof(localiov[0])) 66 return ("too many iov's (change code in syslogd/ttymsg.c)"); 67 68 /* 69 * Ignore lines that start with "ftp" or "uucp". 70 */ 71 if ((strncmp(line, "ftp", 3) == 0) || 72 (strncmp(line, "uucp", 4) == 0)) 73 return (NULL); 74 75 (void) strlcpy(device + sizeof(_PATH_DEV) - 1, line, 76 sizeof(device) - (sizeof(_PATH_DEV) - 1)); 77 if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) { 78 /* A slash is an attempt to break security... */ 79 (void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"", 80 device); 81 return (errbuf); 82 } 83 84 /* 85 * open will fail on slip lines or exclusive-use lines 86 * if not running as root; not an error. 87 */ 88 if ((fd = priv_open_tty(device)) < 0) { 89 if (errno == EBUSY || errno == EACCES) 90 return (NULL); 91 (void) snprintf(errbuf, sizeof(errbuf), 92 "%s: %s", device, strerror(errno)); 93 return (errbuf); 94 } 95 96 for (cnt = left = 0; cnt < iovcnt; ++cnt) 97 left += iov[cnt].iov_len; 98 99 for (;;) { 100 wret = writev(fd, iov, iovcnt); 101 if (wret >= left) 102 break; 103 if (wret >= 0) { 104 left -= wret; 105 if (iov != localiov) { 106 bcopy(iov, localiov, 107 iovcnt * sizeof(struct iovec)); 108 iov = localiov; 109 } 110 for (cnt = 0; wret >= iov->iov_len; ++cnt) { 111 wret -= iov->iov_len; 112 ++iov; 113 --iovcnt; 114 } 115 if (wret) { 116 iov->iov_base = (char *)iov->iov_base + wret; 117 iov->iov_len -= wret; 118 } 119 continue; 120 } 121 if (errno == EWOULDBLOCK) { 122 int off = 0; 123 pid_t cpid; 124 125 if (forked) { 126 (void) close(fd); 127 _exit(1); 128 } 129 cpid = fork(); 130 if (cpid < 0) { 131 (void) snprintf(errbuf, sizeof(errbuf), 132 "fork: %s", strerror(errno)); 133 (void) close(fd); 134 return (errbuf); 135 } 136 if (cpid) { /* parent */ 137 (void) close(fd); 138 return (NULL); 139 } 140 forked++; 141 /* wait at most tmout seconds */ 142 (void) signal(SIGALRM, SIG_DFL); 143 (void) signal(SIGTERM, SIG_DFL); /* XXX */ 144 (void) sigemptyset(&mask); 145 (void) sigprocmask(SIG_SETMASK, &mask, NULL); 146 (void) alarm((u_int)tmout); 147 (void) fcntl(fd, O_NONBLOCK, &off); 148 continue; 149 } 150 /* 151 * We get ENODEV on a slip line if we're running as root, 152 * and EIO if the line just went away. 153 */ 154 if (errno == ENODEV || errno == EIO) 155 break; 156 (void) close(fd); 157 if (forked) 158 _exit(1); 159 (void) snprintf(errbuf, sizeof(errbuf), 160 "%s: %s", device, strerror(errno)); 161 return (errbuf); 162 } 163 164 (void) close(fd); 165 if (forked) 166 _exit(0); 167 return (NULL); 168 } 169