xref: /freebsd/libexec/talkd/talkd.c (revision 0b8224d1cc9dc6c9778ba04a75b2c8d47e5d7481)
1ea022d16SRodney W. Grimes /*
28a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni  *
4ea022d16SRodney W. Grimes  * Copyright (c) 1983, 1993
5ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6ea022d16SRodney W. Grimes  *
7ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
9ea022d16SRodney W. Grimes  * are met:
10ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
155efaea4cSChristian Brueffer  * 3. Neither the name of the University nor the names of its contributors
16ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17ea022d16SRodney W. Grimes  *    without specific prior written permission.
18ea022d16SRodney W. Grimes  *
19ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ea022d16SRodney W. Grimes  * SUCH DAMAGE.
30ea022d16SRodney W. Grimes  */
31ea022d16SRodney W. Grimes 
32ea022d16SRodney W. Grimes /*
33ea022d16SRodney W. Grimes  * The top level of the daemon, the format is heavily borrowed
34ea022d16SRodney W. Grimes  * from rwhod.c. Basically: find out who and where you are;
35ea022d16SRodney W. Grimes  * disconnect all descriptors and ttys, and then endless
36ea022d16SRodney W. Grimes  * loop on waiting for and processing requests
37ea022d16SRodney W. Grimes  */
38ea022d16SRodney W. Grimes #include <sys/types.h>
39ea022d16SRodney W. Grimes #include <sys/socket.h>
4058328669SAndrey A. Chernov #include <sys/param.h>
4102a0965eSJuli Mallett #include <netinet/in.h>
42ea022d16SRodney W. Grimes #include <protocols/talkd.h>
43a846453cSPhilippe Charnier #include <err.h>
44ea022d16SRodney W. Grimes #include <errno.h>
45a846453cSPhilippe Charnier #include <paths.h>
46a846453cSPhilippe Charnier #include <signal.h>
47ea022d16SRodney W. Grimes #include <stdio.h>
48ea022d16SRodney W. Grimes #include <stdlib.h>
49ea022d16SRodney W. Grimes #include <string.h>
50a846453cSPhilippe Charnier #include <syslog.h>
51a846453cSPhilippe Charnier #include <time.h>
52a846453cSPhilippe Charnier #include <unistd.h>
53ea022d16SRodney W. Grimes 
540b67b493SWarner Losh #include "extern.h"
550b67b493SWarner Losh 
56eccad222SEd Schouten static CTL_MSG		request;
57eccad222SEd Schouten static CTL_RESPONSE	response;
58ea022d16SRodney W. Grimes 
59ea022d16SRodney W. Grimes int			debug = 0;
60eccad222SEd Schouten static long		lastmsgtime;
61ea022d16SRodney W. Grimes 
629e9a43bdSBrian Somers char			hostname[MAXHOSTNAMELEN];
63ea022d16SRodney W. Grimes 
64ea022d16SRodney W. Grimes #define TIMEOUT 30
65ea022d16SRodney W. Grimes #define MAXIDLE 120
66ea022d16SRodney W. Grimes 
67a846453cSPhilippe Charnier int
main(int argc,char * argv[])680b67b493SWarner Losh main(int argc, char *argv[])
69ea022d16SRodney W. Grimes {
70ea022d16SRodney W. Grimes 	register CTL_MSG *mp = &request;
71ea022d16SRodney W. Grimes 	int cc;
7299d21d50STim J. Robbins 	struct sockaddr ctl_addr;
73ea022d16SRodney W. Grimes 
74386794daSMatthew Dillon #ifdef NOTDEF
75386794daSMatthew Dillon 	/*
76386794daSMatthew Dillon 	 * removed so ntalkd can run in tty sandbox
77386794daSMatthew Dillon 	 */
78a846453cSPhilippe Charnier 	if (getuid())
79a846453cSPhilippe Charnier 		errx(1, "getuid: not super-user");
80386794daSMatthew Dillon #endif
81ea022d16SRodney W. Grimes 	openlog("talkd", LOG_PID, LOG_DAEMON);
82ea022d16SRodney W. Grimes 	if (gethostname(hostname, sizeof(hostname) - 1) < 0) {
83ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "gethostname: %m");
84ea022d16SRodney W. Grimes 		_exit(1);
85ea022d16SRodney W. Grimes 	}
869e9a43bdSBrian Somers 	hostname[sizeof(hostname) - 1] = '\0';
87ea022d16SRodney W. Grimes 	if (chdir(_PATH_DEV) < 0) {
88ea022d16SRodney W. Grimes 		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
89ea022d16SRodney W. Grimes 		_exit(1);
90ea022d16SRodney W. Grimes 	}
91ea022d16SRodney W. Grimes 	if (argc > 1 && strcmp(argv[1], "-d") == 0)
92ea022d16SRodney W. Grimes 		debug = 1;
93ea022d16SRodney W. Grimes 	signal(SIGALRM, timeout);
94ea022d16SRodney W. Grimes 	alarm(TIMEOUT);
95ea022d16SRodney W. Grimes 	for (;;) {
96ea022d16SRodney W. Grimes 		cc = recv(0, (char *)mp, sizeof(*mp), 0);
97ea022d16SRodney W. Grimes 		if (cc != sizeof (*mp)) {
98ea022d16SRodney W. Grimes 			if (cc < 0 && errno != EINTR)
99ea022d16SRodney W. Grimes 				syslog(LOG_WARNING, "recv: %m");
100ea022d16SRodney W. Grimes 			continue;
101ea022d16SRodney W. Grimes 		}
102ea022d16SRodney W. Grimes 		lastmsgtime = time(0);
103*e4478d7eSBrooks Davis 		(void)memcpy(&ctl_addr.sa_data, &mp->ctl_addr.sa_data,
104*e4478d7eSBrooks Davis 		    sizeof(ctl_addr.sa_data));
10599d21d50STim J. Robbins 		ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
10699d21d50STim J. Robbins 		ctl_addr.sa_len = sizeof(ctl_addr);
107ea022d16SRodney W. Grimes 		process_request(mp, &response);
108ea022d16SRodney W. Grimes 		/* can block here, is this what I want? */
10917d79768SBruce M Simpson 		cc = sendto(STDIN_FILENO, (char *)&response,
11017d79768SBruce M Simpson 		    sizeof(response), 0, &ctl_addr, sizeof(ctl_addr));
111ea022d16SRodney W. Grimes 		if (cc != sizeof (response))
112ea022d16SRodney W. Grimes 			syslog(LOG_WARNING, "sendto: %m");
113ea022d16SRodney W. Grimes 	}
114ea022d16SRodney W. Grimes }
115ea022d16SRodney W. Grimes 
116ea022d16SRodney W. Grimes void
timeout(int sig __unused)1170b67b493SWarner Losh timeout(int sig __unused)
118ea022d16SRodney W. Grimes {
1195007eafaSKris Kennaway 	int save_errno = errno;
120ea022d16SRodney W. Grimes 
121ea022d16SRodney W. Grimes 	if (time(0) - lastmsgtime >= MAXIDLE)
122ea022d16SRodney W. Grimes 		_exit(0);
123ea022d16SRodney W. Grimes 	alarm(TIMEOUT);
1245007eafaSKris Kennaway 	errno = save_errno;
125ea022d16SRodney W. Grimes }
126