process.c revision 14783
1/*
2 * Copyright (c) 1983, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *	This product includes software developed by the University of
16 *	California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#ifndef lint
35static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
36#endif /* not lint */
37
38/*
39 * process.c handles the requests, which can be of three types:
40 *	ANNOUNCE - announce to a user that a talk is wanted
41 *	LEAVE_INVITE - insert the request into the table
42 *	LOOK_UP - look up to see if a request is waiting in
43 *		  in the table for the local user
44 *	DELETE - delete invitation
45 */
46#include <sys/param.h>
47#include <sys/stat.h>
48#include <sys/socket.h>
49#include <netinet/in.h>
50#include <protocols/talkd.h>
51#include <netdb.h>
52#include <syslog.h>
53#include <stdio.h>
54#include <string.h>
55#include <ctype.h>
56#include <paths.h>
57
58CTL_MSG *find_request();
59CTL_MSG *find_match();
60
61process_request(mp, rp)
62	register CTL_MSG *mp;
63	register CTL_RESPONSE *rp;
64{
65	register CTL_MSG *ptr;
66	extern int debug;
67	char *s;
68
69	rp->vers = TALK_VERSION;
70	rp->type = mp->type;
71	rp->id_num = htonl(0);
72	if (mp->vers != TALK_VERSION) {
73		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
74		rp->answer = BADVERSION;
75		return;
76	}
77	mp->id_num = ntohl(mp->id_num);
78	mp->addr.sa_family = ntohs(mp->addr.sa_family);
79	if (mp->addr.sa_family != AF_INET) {
80		syslog(LOG_WARNING, "Bad address, family %d",
81		    mp->addr.sa_family);
82		rp->answer = BADADDR;
83		return;
84	}
85	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
86	if (mp->ctl_addr.sa_family != AF_INET) {
87		syslog(LOG_WARNING, "Bad control address, family %d",
88		    mp->ctl_addr.sa_family);
89		rp->answer = BADCTLADDR;
90		return;
91	}
92	for (s = mp->l_name; *s; s++)
93		if (!isprint(*s)) {
94			syslog(LOG_NOTICE, "Illegal user name. Aborting");
95			rp->answer = FAILED;
96			return;
97		}
98	mp->pid = ntohl(mp->pid);
99	if (debug)
100		print_request("process_request", mp);
101	switch (mp->type) {
102
103	case ANNOUNCE:
104		do_announce(mp, rp);
105		break;
106
107	case LEAVE_INVITE:
108		ptr = find_request(mp);
109		if (ptr != (CTL_MSG *)0) {
110			rp->id_num = htonl(ptr->id_num);
111			rp->answer = SUCCESS;
112		} else
113			insert_table(mp, rp);
114		break;
115
116	case LOOK_UP:
117		ptr = find_match(mp);
118		if (ptr != (CTL_MSG *)0) {
119			rp->id_num = htonl(ptr->id_num);
120			rp->addr = ptr->addr;
121			rp->addr.sa_family = htons(ptr->addr.sa_family);
122			rp->answer = SUCCESS;
123		} else
124			rp->answer = NOT_HERE;
125		break;
126
127	case DELETE:
128		rp->answer = delete_invite(mp->id_num);
129		break;
130
131	default:
132		rp->answer = UNKNOWN_REQUEST;
133		break;
134	}
135	if (debug)
136		print_response("process_request", rp);
137}
138
139do_announce(mp, rp)
140	register CTL_MSG *mp;
141	CTL_RESPONSE *rp;
142{
143	struct hostent *hp;
144	CTL_MSG *ptr;
145	int result;
146
147	/* see if the user is logged */
148	result = find_user(mp->r_name, mp->r_tty);
149	if (result != SUCCESS) {
150		rp->answer = result;
151		return;
152	}
153#define	satosin(sa)	((struct sockaddr_in *)(sa))
154	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
155		sizeof (struct in_addr), AF_INET);
156	if (hp == (struct hostent *)0) {
157		rp->answer = MACHINE_UNKNOWN;
158		return;
159	}
160	ptr = find_request(mp);
161	if (ptr == (CTL_MSG *) 0) {
162		insert_table(mp, rp);
163		rp->answer = announce(mp, hp->h_name);
164		return;
165	}
166	if (mp->id_num > ptr->id_num) {
167		/*
168		 * This is an explicit re-announce, so update the id_num
169		 * field to avoid duplicates and re-announce the talk.
170		 */
171		ptr->id_num = new_id();
172		rp->id_num = htonl(ptr->id_num);
173		rp->answer = announce(mp, hp->h_name);
174	} else {
175		/* a duplicated request, so ignore it */
176		rp->id_num = htonl(ptr->id_num);
177		rp->answer = SUCCESS;
178	}
179}
180
181#include <utmp.h>
182
183/*
184 * Search utmp for the local user
185 */
186find_user(name, tty)
187	char *name, *tty;
188{
189	struct utmp ubuf;
190	int status;
191	FILE *fd;
192	struct stat statb;
193	time_t best = 0;
194	char line[sizeof(ubuf.ut_line) + 1];
195	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
196
197	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
198		fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
199		return (FAILED);
200	}
201#define SCMPN(a, b)	strncmp(a, b, sizeof (a))
202	status = NOT_HERE;
203	(void) strcpy(ftty, _PATH_DEV);
204	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
205		if (SCMPN(ubuf.ut_name, name) == 0) {
206			strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
207			line[sizeof(ubuf.ut_line)] = '\0';
208			if (*tty == '\0' || best != 0) {
209				if (best == 0)
210					status = PERMISSION_DENIED;
211				/* no particular tty was requested */
212				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
213				    line);
214				if (stat(ftty, &statb) == 0) {
215					if (!(statb.st_mode & 020))
216						continue;
217					if (statb.st_atime > best) {
218						best = statb.st_atime;
219						(void) strcpy(tty, line);
220						status = SUCCESS;
221						continue;
222					}
223				}
224			}
225			if (strcmp(line, tty) == 0) {
226				status = SUCCESS;
227				break;
228			}
229		}
230	fclose(fd);
231	return (status);
232}
233