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