xref: /openbsd/libexec/talkd/talkd.c (revision a6445c1d)
1 /*	$OpenBSD: talkd.c,v 1.22 2009/10/27 23:59:31 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 /*
33  * The top level of the daemon, the format is heavily borrowed
34  * from rwhod.c. Basically: find out who and where you are;
35  * disconnect all descriptors and ttys, and then endless
36  * loop on waiting for and processing requests
37  */
38 #include <sys/param.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <protocols/talkd.h>
42 #include <signal.h>
43 #include <syslog.h>
44 #include <time.h>
45 #include <errno.h>
46 #include <unistd.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <paths.h>
51 #include "talkd.h"
52 
53 int	debug = 0;
54 void	timeout(int);
55 long	lastmsgtime;
56 
57 char	hostname[MAXHOSTNAMELEN];
58 
59 #define TIMEOUT 30
60 #define MAXIDLE 120
61 
62 int
63 main(int argc, char *argv[])
64 {
65 	if (getuid() != 0) {
66 		fprintf(stderr, "%s: getuid: not super-user\n", argv[0]);
67 		exit(1);
68 	}
69 	openlog("talkd", LOG_PID, LOG_DAEMON);
70 	if (gethostname(hostname, sizeof(hostname)) < 0) {
71 		syslog(LOG_ERR, "gethostname: %m");
72 		_exit(1);
73 	}
74 	if (chdir(_PATH_DEV) < 0) {
75 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
76 		_exit(1);
77 	}
78 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
79 		debug = 1;
80 	init_table();
81 	signal(SIGALRM, timeout);
82 	alarm(TIMEOUT);
83 
84 	for (;;) {
85 		CTL_RESPONSE response;
86 		socklen_t len = sizeof(response.addr);
87 		CTL_MSG	request;
88 		int cc;
89 		struct sockaddr ctl_addr;
90 
91 		memset(&response, 0, sizeof(response));
92 		cc = recvfrom(STDIN_FILENO, (char *)&request,
93 		    sizeof(request), 0, (struct sockaddr *)&response.addr,
94 		    &len);
95 		if (cc != sizeof(request)) {
96 			if (cc < 0 && errno != EINTR)
97 				syslog(LOG_WARNING, "recvfrom: %m");
98 			continue;
99 		}
100 
101 		/* Force NUL termination */
102 		request.l_name[sizeof(request.l_name) - 1] = '\0';
103 		request.r_name[sizeof(request.r_name) - 1] = '\0';
104 		request.r_tty[sizeof(request.r_tty) - 1] = '\0';
105 
106 		memcpy(&ctl_addr, &request.ctl_addr, sizeof(ctl_addr));
107 		ctl_addr.sa_family = ntohs(request.ctl_addr.sa_family);
108 		ctl_addr.sa_len = sizeof(ctl_addr);
109 		if (ctl_addr.sa_family != AF_INET)
110 			continue;
111 
112 		lastmsgtime = time(0);
113 		process_request(&request, &response);
114 		/* can block here, is this what I want? */
115 		cc = sendto(STDOUT_FILENO, (char *)&response,
116 		    sizeof(response), 0, &ctl_addr, sizeof(ctl_addr));
117 		if (cc != sizeof(response))
118 			syslog(LOG_WARNING, "sendto: %m");
119 	}
120 }
121 
122 void
123 timeout(int signo)
124 {
125 	int save_errno = errno;
126 
127 	if (time(0) - lastmsgtime >= MAXIDLE)
128 		_exit(0);
129 	alarm(TIMEOUT);
130 	errno = save_errno;
131 }
132