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