1 /* $OpenBSD: talkd.c,v 1.21 2004/08/08 19:32:45 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1983 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 char copyright[] = 34 "@(#) Copyright (c) 1983 Regents of the University of California.\n\ 35 All rights reserved.\n"; 36 #endif /* not lint */ 37 38 #ifndef lint 39 /*static char sccsid[] = "from: @(#)talkd.c 5.8 (Berkeley) 2/26/91";*/ 40 static char rcsid[] = "$Id: talkd.c,v 1.21 2004/08/08 19:32:45 deraadt Exp $"; 41 #endif /* not lint */ 42 43 /* 44 * The top level of the daemon, the format is heavily borrowed 45 * from rwhod.c. Basically: find out who and where you are; 46 * disconnect all descriptors and ttys, and then endless 47 * loop on waiting for and processing requests 48 */ 49 #include <sys/param.h> 50 #include <sys/types.h> 51 #include <sys/socket.h> 52 #include <protocols/talkd.h> 53 #include <signal.h> 54 #include <syslog.h> 55 #include <time.h> 56 #include <errno.h> 57 #include <unistd.h> 58 #include <stdio.h> 59 #include <stdlib.h> 60 #include <string.h> 61 #include <paths.h> 62 #include "talkd.h" 63 64 int debug = 0; 65 void timeout(int); 66 long lastmsgtime; 67 68 char hostname[MAXHOSTNAMELEN]; 69 70 #define TIMEOUT 30 71 #define MAXIDLE 120 72 73 int 74 main(int argc, char *argv[]) 75 { 76 if (getuid() != 0) { 77 fprintf(stderr, "%s: getuid: not super-user\n", argv[0]); 78 exit(1); 79 } 80 openlog("talkd", LOG_PID, LOG_DAEMON); 81 if (gethostname(hostname, sizeof(hostname)) < 0) { 82 syslog(LOG_ERR, "gethostname: %m"); 83 _exit(1); 84 } 85 if (chdir(_PATH_DEV) < 0) { 86 syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV); 87 _exit(1); 88 } 89 if (argc > 1 && strcmp(argv[1], "-d") == 0) 90 debug = 1; 91 init_table(); 92 signal(SIGALRM, timeout); 93 alarm(TIMEOUT); 94 95 for (;;) { 96 CTL_RESPONSE response; 97 socklen_t len = sizeof(response.addr); 98 CTL_MSG request; 99 int cc; 100 struct sockaddr ctl_addr; 101 102 memset(&response, 0, sizeof(response)); 103 cc = recvfrom(STDIN_FILENO, (char *)&request, 104 sizeof(request), 0, (struct sockaddr *)&response.addr, 105 &len); 106 if (cc != sizeof(request)) { 107 if (cc < 0 && errno != EINTR) 108 syslog(LOG_WARNING, "recvfrom: %m"); 109 continue; 110 } 111 112 /* Force NUL termination */ 113 request.l_name[sizeof(request.l_name) - 1] = '\0'; 114 request.r_name[sizeof(request.r_name) - 1] = '\0'; 115 request.r_tty[sizeof(request.r_tty) - 1] = '\0'; 116 117 memcpy(&ctl_addr, &request.ctl_addr, sizeof(ctl_addr)); 118 ctl_addr.sa_family = ntohs(request.ctl_addr.sa_family); 119 ctl_addr.sa_len = sizeof(ctl_addr); 120 if (ctl_addr.sa_family != AF_INET) 121 continue; 122 123 lastmsgtime = time(0); 124 process_request(&request, &response); 125 /* can block here, is this what I want? */ 126 cc = sendto(STDOUT_FILENO, (char *)&response, 127 sizeof(response), 0, &ctl_addr, sizeof(ctl_addr)); 128 if (cc != sizeof(response)) 129 syslog(LOG_WARNING, "sendto: %m"); 130 } 131 } 132 133 void 134 timeout(int signo) 135 { 136 int save_errno = errno; 137 138 if (time(0) - lastmsgtime >= MAXIDLE) 139 _exit(0); 140 alarm(TIMEOUT); 141 errno = save_errno; 142 } 143