11592Srgrimes/*
21592Srgrimes * Copyright (c) 1983, 1993
31592Srgrimes *	The Regents of the University of California.  All rights reserved.
41592Srgrimes *
51592Srgrimes * Redistribution and use in source and binary forms, with or without
61592Srgrimes * modification, are permitted provided that the following conditions
71592Srgrimes * are met:
81592Srgrimes * 1. Redistributions of source code must retain the above copyright
91592Srgrimes *    notice, this list of conditions and the following disclaimer.
101592Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111592Srgrimes *    notice, this list of conditions and the following disclaimer in the
121592Srgrimes *    documentation and/or other materials provided with the distribution.
131592Srgrimes * 3. All advertising materials mentioning features or use of this software
141592Srgrimes *    must display the following acknowledgement:
151592Srgrimes *	This product includes software developed by the University of
161592Srgrimes *	California, Berkeley and its contributors.
171592Srgrimes * 4. Neither the name of the University nor the names of its contributors
181592Srgrimes *    may be used to endorse or promote products derived from this software
191592Srgrimes *    without specific prior written permission.
201592Srgrimes *
211592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311592Srgrimes * SUCH DAMAGE.
321592Srgrimes */
331592Srgrimes
341592Srgrimes#ifndef lint
3531491Scharnierstatic const char copyright[] =
361592Srgrimes"@(#) Copyright (c) 1983, 1993\n\
371592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
381592Srgrimes#endif /* not lint */
391592Srgrimes
401592Srgrimes#ifndef lint
4131491Scharnier#if 0
421592Srgrimesstatic char sccsid[] = "@(#)talkd.c	8.1 (Berkeley) 6/4/93";
4331491Scharnier#endif
4431491Scharnierstatic const char rcsid[] =
4550476Speter  "$FreeBSD$";
461592Srgrimes#endif /* not lint */
471592Srgrimes
481592Srgrimes/*
491592Srgrimes * The top level of the daemon, the format is heavily borrowed
508870Srgrimes * from rwhod.c. Basically: find out who and where you are;
511592Srgrimes * disconnect all descriptors and ttys, and then endless
521592Srgrimes * loop on waiting for and processing requests
531592Srgrimes */
541592Srgrimes#include <sys/types.h>
551592Srgrimes#include <sys/socket.h>
565111Sache#include <sys/param.h>
57112998Sjmallett#include <netinet/in.h>
581592Srgrimes#include <protocols/talkd.h>
5931491Scharnier#include <err.h>
6031491Scharnier#include <errno.h>
6131491Scharnier#include <paths.h>
621592Srgrimes#include <signal.h>
6331491Scharnier#include <stdio.h>
6431491Scharnier#include <stdlib.h>
6531491Scharnier#include <string.h>
661592Srgrimes#include <syslog.h>
671592Srgrimes#include <time.h>
681592Srgrimes#include <unistd.h>
691592Srgrimes
7090261Simp#include "extern.h"
7190261Simp
72241777Sedstatic CTL_MSG		request;
73241777Sedstatic CTL_RESPONSE	response;
741592Srgrimes
75241777Sedint			debug = 0;
76241777Sedstatic long		lastmsgtime;
771592Srgrimes
78241777Sedchar			hostname[MAXHOSTNAMELEN];
791592Srgrimes
801592Srgrimes#define TIMEOUT 30
811592Srgrimes#define MAXIDLE 120
821592Srgrimes
8331491Scharnierint
8490261Simpmain(int argc, char *argv[])
851592Srgrimes{
861592Srgrimes	register CTL_MSG *mp = &request;
871592Srgrimes	int cc;
88120548Stjr	struct sockaddr ctl_addr;
891592Srgrimes
9041440Sdillon#ifdef NOTDEF
9141440Sdillon	/*
9241440Sdillon	 * removed so ntalkd can run in tty sandbox
9341440Sdillon	 */
9431491Scharnier	if (getuid())
9531491Scharnier		errx(1, "getuid: not super-user");
9641440Sdillon#endif
971592Srgrimes	openlog("talkd", LOG_PID, LOG_DAEMON);
9845422Sbrian	if (gethostname(hostname, sizeof(hostname) - 1) < 0) {
991592Srgrimes		syslog(LOG_ERR, "gethostname: %m");
1001592Srgrimes		_exit(1);
1011592Srgrimes	}
10245422Sbrian	hostname[sizeof(hostname) - 1] = '\0';
1031592Srgrimes	if (chdir(_PATH_DEV) < 0) {
1041592Srgrimes		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
1051592Srgrimes		_exit(1);
1061592Srgrimes	}
1071592Srgrimes	if (argc > 1 && strcmp(argv[1], "-d") == 0)
1081592Srgrimes		debug = 1;
1091592Srgrimes	signal(SIGALRM, timeout);
1101592Srgrimes	alarm(TIMEOUT);
1111592Srgrimes	for (;;) {
112130496Sbms		cc = recv(0, (char *)mp, sizeof(*mp), 0);
1131592Srgrimes		if (cc != sizeof (*mp)) {
1141592Srgrimes			if (cc < 0 && errno != EINTR)
1151592Srgrimes				syslog(LOG_WARNING, "recv: %m");
1161592Srgrimes			continue;
1171592Srgrimes		}
1181592Srgrimes		lastmsgtime = time(0);
119120548Stjr		(void)memcpy(&ctl_addr, &mp->ctl_addr, sizeof(ctl_addr));
120120548Stjr		ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
121120548Stjr		ctl_addr.sa_len = sizeof(ctl_addr);
1221592Srgrimes		process_request(mp, &response);
1231592Srgrimes		/* can block here, is this what I want? */
124130495Sbms		cc = sendto(STDIN_FILENO, (char *)&response,
125130496Sbms		    sizeof(response), 0, &ctl_addr, sizeof(ctl_addr));
1261592Srgrimes		if (cc != sizeof (response))
1271592Srgrimes			syslog(LOG_WARNING, "sendto: %m");
1281592Srgrimes	}
1291592Srgrimes}
1301592Srgrimes
1311592Srgrimesvoid
13290261Simptimeout(int sig __unused)
1331592Srgrimes{
13480248Skris	int save_errno = errno;
1351592Srgrimes
1361592Srgrimes	if (time(0) - lastmsgtime >= MAXIDLE)
1371592Srgrimes		_exit(0);
1381592Srgrimes	alarm(TIMEOUT);
13980248Skris	errno = save_errno;
1401592Srgrimes}
141