1*a4570f53Sdjm /* $OpenBSD: ttymsg.c,v 1.3 2004/07/03 23:40:44 djm Exp $ */ 238182445Savsm /* $NetBSD: ttymsg.c,v 1.3 1994/11/17 07:17:55 jtc Exp $ */ 338182445Savsm 438182445Savsm /* 538182445Savsm * Copyright (c) 1989, 1993 638182445Savsm * The Regents of the University of California. All rights reserved. 738182445Savsm * 838182445Savsm * Redistribution and use in source and binary forms, with or without 938182445Savsm * modification, are permitted provided that the following conditions 1038182445Savsm * are met: 1138182445Savsm * 1. Redistributions of source code must retain the above copyright 1238182445Savsm * notice, this list of conditions and the following disclaimer. 1338182445Savsm * 2. Redistributions in binary form must reproduce the above copyright 1438182445Savsm * notice, this list of conditions and the following disclaimer in the 1538182445Savsm * documentation and/or other materials provided with the distribution. 1638182445Savsm * 3. Neither the name of the University nor the names of its contributors 1738182445Savsm * may be used to endorse or promote products derived from this software 1838182445Savsm * without specific prior written permission. 1938182445Savsm * 2038182445Savsm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2138182445Savsm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2238182445Savsm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2338182445Savsm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2438182445Savsm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2538182445Savsm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2638182445Savsm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2738182445Savsm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2838182445Savsm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2938182445Savsm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3038182445Savsm * SUCH DAMAGE. 3138182445Savsm */ 3238182445Savsm 3338182445Savsm #ifndef lint 3438182445Savsm #if 0 3538182445Savsm static const char sccsid[] = "@(#)ttymsg.c 8.2 (Berkeley) 11/16/93"; 3638182445Savsm #endif 37*a4570f53Sdjm static const char rcsid[] = "$OpenBSD: ttymsg.c,v 1.3 2004/07/03 23:40:44 djm Exp $"; 3838182445Savsm #endif /* not lint */ 3938182445Savsm 4038182445Savsm #include <sys/types.h> 4138182445Savsm #include <sys/uio.h> 4238182445Savsm #include <signal.h> 4338182445Savsm #include <fcntl.h> 4438182445Savsm #include <dirent.h> 4538182445Savsm #include <errno.h> 4638182445Savsm #include <paths.h> 4738182445Savsm #include <unistd.h> 4838182445Savsm #include <stdio.h> 4938182445Savsm #include <string.h> 5038182445Savsm #include <stdlib.h> 5138182445Savsm #include <sys/stat.h> 5238182445Savsm 5338182445Savsm #include "syslogd.h" 5438182445Savsm 5538182445Savsm /* 5638182445Savsm * Display the contents of a uio structure on a terminal. 5738182445Savsm * Forks and finishes in child if write would block, waiting up to tmout 5838182445Savsm * seconds. Returns pointer to error string on unexpected error; 5938182445Savsm * string is not newline-terminated. Various "normal" errors are ignored 6038182445Savsm * (exclusive-use, lack of permission, etc.). 6138182445Savsm */ 6238182445Savsm char * 6338182445Savsm ttymsg(struct iovec *iov, int iovcnt, char *line, int tmout) 6438182445Savsm { 6538182445Savsm static char device[MAXNAMLEN] = _PATH_DEV; 6638182445Savsm static char errbuf[1024]; 6738182445Savsm int cnt, fd, left, wret; 6838182445Savsm struct iovec localiov[6]; 6938182445Savsm int forked = 0; 7038182445Savsm sigset_t mask; 7138182445Savsm 7238182445Savsm if (iovcnt > sizeof(localiov) / sizeof(localiov[0])) 736c0d07e9Savsm return ("too many iov's (change code in syslogd/ttymsg.c)"); 7438182445Savsm 7538182445Savsm /* 7638182445Savsm * Ignore lines that start with "ftp" or "uucp". 7738182445Savsm */ 7838182445Savsm if ((strncmp(line, "ftp", 3) == 0) || 7938182445Savsm (strncmp(line, "uucp", 4) == 0)) 8038182445Savsm return (NULL); 8138182445Savsm 8238182445Savsm (void) strlcpy(device + sizeof(_PATH_DEV) - 1, line, 8338182445Savsm sizeof(device) - (sizeof(_PATH_DEV) - 1)); 8438182445Savsm if (strchr(device + sizeof(_PATH_DEV) - 1, '/')) { 8538182445Savsm /* A slash is an attempt to break security... */ 8638182445Savsm (void) snprintf(errbuf, sizeof(errbuf), "'/' in \"%s\"", 8738182445Savsm device); 8838182445Savsm return (errbuf); 8938182445Savsm } 9038182445Savsm 9138182445Savsm /* 9238182445Savsm * open will fail on slip lines or exclusive-use lines 9338182445Savsm * if not running as root; not an error. 9438182445Savsm */ 9538182445Savsm if ((fd = priv_open_tty(device)) < 0) { 9638182445Savsm if (errno == EBUSY || errno == EACCES) 9738182445Savsm return (NULL); 9838182445Savsm (void) snprintf(errbuf, sizeof(errbuf), 9938182445Savsm "%s: %s", device, strerror(errno)); 10038182445Savsm return (errbuf); 10138182445Savsm } 10238182445Savsm 10338182445Savsm for (cnt = left = 0; cnt < iovcnt; ++cnt) 10438182445Savsm left += iov[cnt].iov_len; 10538182445Savsm 10638182445Savsm for (;;) { 10738182445Savsm wret = writev(fd, iov, iovcnt); 10838182445Savsm if (wret >= left) 10938182445Savsm break; 11038182445Savsm if (wret >= 0) { 11138182445Savsm left -= wret; 11238182445Savsm if (iov != localiov) { 11338182445Savsm bcopy(iov, localiov, 11438182445Savsm iovcnt * sizeof(struct iovec)); 11538182445Savsm iov = localiov; 11638182445Savsm } 11738182445Savsm for (cnt = 0; wret >= iov->iov_len; ++cnt) { 11838182445Savsm wret -= iov->iov_len; 11938182445Savsm ++iov; 12038182445Savsm --iovcnt; 12138182445Savsm } 12238182445Savsm if (wret) { 123*a4570f53Sdjm iov->iov_base = (char *)iov->iov_base + wret; 12438182445Savsm iov->iov_len -= wret; 12538182445Savsm } 12638182445Savsm continue; 12738182445Savsm } 12838182445Savsm if (errno == EWOULDBLOCK) { 12938182445Savsm int off = 0; 13038182445Savsm pid_t cpid; 13138182445Savsm 13238182445Savsm if (forked) { 13338182445Savsm (void) close(fd); 13438182445Savsm _exit(1); 13538182445Savsm } 13638182445Savsm cpid = fork(); 13738182445Savsm if (cpid < 0) { 13838182445Savsm (void) snprintf(errbuf, sizeof(errbuf), 13938182445Savsm "fork: %s", strerror(errno)); 14038182445Savsm (void) close(fd); 14138182445Savsm return (errbuf); 14238182445Savsm } 14338182445Savsm if (cpid) { /* parent */ 14438182445Savsm (void) close(fd); 14538182445Savsm return (NULL); 14638182445Savsm } 14738182445Savsm forked++; 14838182445Savsm /* wait at most tmout seconds */ 14938182445Savsm (void) signal(SIGALRM, SIG_DFL); 15038182445Savsm (void) signal(SIGTERM, SIG_DFL); /* XXX */ 15138182445Savsm (void) sigemptyset(&mask); 15238182445Savsm (void) sigprocmask(SIG_SETMASK, &mask, NULL); 15338182445Savsm (void) alarm((u_int)tmout); 15438182445Savsm (void) fcntl(fd, O_NONBLOCK, &off); 15538182445Savsm continue; 15638182445Savsm } 15738182445Savsm /* 15838182445Savsm * We get ENODEV on a slip line if we're running as root, 15938182445Savsm * and EIO if the line just went away. 16038182445Savsm */ 16138182445Savsm if (errno == ENODEV || errno == EIO) 16238182445Savsm break; 16338182445Savsm (void) close(fd); 16438182445Savsm if (forked) 16538182445Savsm _exit(1); 16638182445Savsm (void) snprintf(errbuf, sizeof(errbuf), 16738182445Savsm "%s: %s", device, strerror(errno)); 16838182445Savsm return (errbuf); 16938182445Savsm } 17038182445Savsm 17138182445Savsm (void) close(fd); 17238182445Savsm if (forked) 17338182445Savsm _exit(0); 17438182445Savsm return (NULL); 17538182445Savsm } 176