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.
13262435Sbrueffer * 3. Neither the name of the University nor the names of its contributors
141592Srgrimes *    may be used to endorse or promote products derived from this software
151592Srgrimes *    without specific prior written permission.
161592Srgrimes *
171592Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181592Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191592Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201592Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211592Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221592Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231592Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241592Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251592Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261592Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271592Srgrimes * SUCH DAMAGE.
281592Srgrimes */
291592Srgrimes
301592Srgrimes#ifndef lint
3131491Scharnierstatic const char copyright[] =
321592Srgrimes"@(#) Copyright (c) 1983, 1993\n\
331592Srgrimes	The Regents of the University of California.  All rights reserved.\n";
341592Srgrimes#endif /* not lint */
351592Srgrimes
361592Srgrimes#ifndef lint
3731491Scharnier#if 0
381592Srgrimesstatic char sccsid[] = "@(#)talkd.c	8.1 (Berkeley) 6/4/93";
3931491Scharnier#endif
4031491Scharnierstatic const char rcsid[] =
4150476Speter  "$FreeBSD$";
421592Srgrimes#endif /* not lint */
431592Srgrimes
441592Srgrimes/*
451592Srgrimes * The top level of the daemon, the format is heavily borrowed
468870Srgrimes * from rwhod.c. Basically: find out who and where you are;
471592Srgrimes * disconnect all descriptors and ttys, and then endless
481592Srgrimes * loop on waiting for and processing requests
491592Srgrimes */
501592Srgrimes#include <sys/types.h>
511592Srgrimes#include <sys/socket.h>
525111Sache#include <sys/param.h>
53112998Sjmallett#include <netinet/in.h>
541592Srgrimes#include <protocols/talkd.h>
5531491Scharnier#include <err.h>
5631491Scharnier#include <errno.h>
5731491Scharnier#include <paths.h>
581592Srgrimes#include <signal.h>
5931491Scharnier#include <stdio.h>
6031491Scharnier#include <stdlib.h>
6131491Scharnier#include <string.h>
621592Srgrimes#include <syslog.h>
631592Srgrimes#include <time.h>
641592Srgrimes#include <unistd.h>
651592Srgrimes
6690261Simp#include "extern.h"
6790261Simp
68241777Sedstatic CTL_MSG		request;
69241777Sedstatic CTL_RESPONSE	response;
701592Srgrimes
71241777Sedint			debug = 0;
72241777Sedstatic long		lastmsgtime;
731592Srgrimes
74241777Sedchar			hostname[MAXHOSTNAMELEN];
751592Srgrimes
761592Srgrimes#define TIMEOUT 30
771592Srgrimes#define MAXIDLE 120
781592Srgrimes
7931491Scharnierint
8090261Simpmain(int argc, char *argv[])
811592Srgrimes{
821592Srgrimes	register CTL_MSG *mp = &request;
831592Srgrimes	int cc;
84120548Stjr	struct sockaddr ctl_addr;
851592Srgrimes
8641440Sdillon#ifdef NOTDEF
8741440Sdillon	/*
8841440Sdillon	 * removed so ntalkd can run in tty sandbox
8941440Sdillon	 */
9031491Scharnier	if (getuid())
9131491Scharnier		errx(1, "getuid: not super-user");
9241440Sdillon#endif
931592Srgrimes	openlog("talkd", LOG_PID, LOG_DAEMON);
9445422Sbrian	if (gethostname(hostname, sizeof(hostname) - 1) < 0) {
951592Srgrimes		syslog(LOG_ERR, "gethostname: %m");
961592Srgrimes		_exit(1);
971592Srgrimes	}
9845422Sbrian	hostname[sizeof(hostname) - 1] = '\0';
991592Srgrimes	if (chdir(_PATH_DEV) < 0) {
1001592Srgrimes		syslog(LOG_ERR, "chdir: %s: %m", _PATH_DEV);
1011592Srgrimes		_exit(1);
1021592Srgrimes	}
1031592Srgrimes	if (argc > 1 && strcmp(argv[1], "-d") == 0)
1041592Srgrimes		debug = 1;
1051592Srgrimes	signal(SIGALRM, timeout);
1061592Srgrimes	alarm(TIMEOUT);
1071592Srgrimes	for (;;) {
108130496Sbms		cc = recv(0, (char *)mp, sizeof(*mp), 0);
1091592Srgrimes		if (cc != sizeof (*mp)) {
1101592Srgrimes			if (cc < 0 && errno != EINTR)
1111592Srgrimes				syslog(LOG_WARNING, "recv: %m");
1121592Srgrimes			continue;
1131592Srgrimes		}
1141592Srgrimes		lastmsgtime = time(0);
115120548Stjr		(void)memcpy(&ctl_addr, &mp->ctl_addr, sizeof(ctl_addr));
116120548Stjr		ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
117120548Stjr		ctl_addr.sa_len = sizeof(ctl_addr);
1181592Srgrimes		process_request(mp, &response);
1191592Srgrimes		/* can block here, is this what I want? */
120130495Sbms		cc = sendto(STDIN_FILENO, (char *)&response,
121130496Sbms		    sizeof(response), 0, &ctl_addr, sizeof(ctl_addr));
1221592Srgrimes		if (cc != sizeof (response))
1231592Srgrimes			syslog(LOG_WARNING, "sendto: %m");
1241592Srgrimes	}
1251592Srgrimes}
1261592Srgrimes
1271592Srgrimesvoid
12890261Simptimeout(int sig __unused)
1291592Srgrimes{
13080248Skris	int save_errno = errno;
1311592Srgrimes
1321592Srgrimes	if (time(0) - lastmsgtime >= MAXIDLE)
1331592Srgrimes		_exit(0);
1341592Srgrimes	alarm(TIMEOUT);
13580248Skris	errno = save_errno;
1361592Srgrimes}
137