process.c revision 3791
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 <paths.h>
56
57CTL_MSG *find_request();
58CTL_MSG *find_match();
59
60process_request(mp, rp)
61	register CTL_MSG *mp;
62	register CTL_RESPONSE *rp;
63{
64	register CTL_MSG *ptr;
65	extern int debug;
66
67	rp->vers = TALK_VERSION;
68	rp->type = mp->type;
69	rp->id_num = htonl(0);
70	if (mp->vers != TALK_VERSION) {
71		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
72		rp->answer = BADVERSION;
73		return;
74	}
75	mp->id_num = ntohl(mp->id_num);
76	mp->addr.sa_family = ntohs(mp->addr.sa_family);
77	if (mp->addr.sa_family != AF_INET) {
78		syslog(LOG_WARNING, "Bad address, family %d",
79		    mp->addr.sa_family);
80		rp->answer = BADADDR;
81		return;
82	}
83	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
84	if (mp->ctl_addr.sa_family != AF_INET) {
85		syslog(LOG_WARNING, "Bad control address, family %d",
86		    mp->ctl_addr.sa_family);
87		rp->answer = BADCTLADDR;
88		return;
89	}
90	if (strchr(mp->l_name, 27)) {
91		syslog(LOG_NOTICE, "Illegal user name. Aborting");
92		rp->answer = FAILED;
93		return;
94	}
95	mp->pid = ntohl(mp->pid);
96	if (debug)
97		print_request("process_request", mp);
98	switch (mp->type) {
99
100	case ANNOUNCE:
101		do_announce(mp, rp);
102		break;
103
104	case LEAVE_INVITE:
105		ptr = find_request(mp);
106		if (ptr != (CTL_MSG *)0) {
107			rp->id_num = htonl(ptr->id_num);
108			rp->answer = SUCCESS;
109		} else
110			insert_table(mp, rp);
111		break;
112
113	case LOOK_UP:
114		ptr = find_match(mp);
115		if (ptr != (CTL_MSG *)0) {
116			rp->id_num = htonl(ptr->id_num);
117			rp->addr = ptr->addr;
118			rp->addr.sa_family = htons(ptr->addr.sa_family);
119			rp->answer = SUCCESS;
120		} else
121			rp->answer = NOT_HERE;
122		break;
123
124	case DELETE:
125		rp->answer = delete_invite(mp->id_num);
126		break;
127
128	default:
129		rp->answer = UNKNOWN_REQUEST;
130		break;
131	}
132	if (debug)
133		print_response("process_request", rp);
134}
135
136do_announce(mp, rp)
137	register CTL_MSG *mp;
138	CTL_RESPONSE *rp;
139{
140	struct hostent *hp;
141	CTL_MSG *ptr;
142	int result;
143
144	/* see if the user is logged */
145	result = find_user(mp->r_name, mp->r_tty);
146	if (result != SUCCESS) {
147		rp->answer = result;
148		return;
149	}
150#define	satosin(sa)	((struct sockaddr_in *)(sa))
151	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
152		sizeof (struct in_addr), AF_INET);
153	if (hp == (struct hostent *)0) {
154		rp->answer = MACHINE_UNKNOWN;
155		return;
156	}
157	ptr = find_request(mp);
158	if (ptr == (CTL_MSG *) 0) {
159		insert_table(mp, rp);
160		rp->answer = announce(mp, hp->h_name);
161		return;
162	}
163	if (mp->id_num > ptr->id_num) {
164		/*
165		 * This is an explicit re-announce, so update the id_num
166		 * field to avoid duplicates and re-announce the talk.
167		 */
168		ptr->id_num = new_id();
169		rp->id_num = htonl(ptr->id_num);
170		rp->answer = announce(mp, hp->h_name);
171	} else {
172		/* a duplicated request, so ignore it */
173		rp->id_num = htonl(ptr->id_num);
174		rp->answer = SUCCESS;
175	}
176}
177
178#include <utmp.h>
179
180/*
181 * Search utmp for the local user
182 */
183find_user(name, tty)
184	char *name, *tty;
185{
186	struct utmp ubuf;
187	int status;
188	FILE *fd;
189	struct stat statb;
190	char line[sizeof(ubuf.ut_line) + 1];
191	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
192
193	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
194		fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
195		return (FAILED);
196	}
197#define SCMPN(a, b)	strncmp(a, b, sizeof (a))
198	status = NOT_HERE;
199	(void) strcpy(ftty, _PATH_DEV);
200	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
201		if (SCMPN(ubuf.ut_name, name) == 0) {
202			strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
203			line[sizeof(ubuf.ut_line)] = '\0';
204			if (*tty == '\0') {
205				status = PERMISSION_DENIED;
206				/* no particular tty was requested */
207				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
208				    line);
209				if (stat(ftty, &statb) == 0) {
210					if (!(statb.st_mode & 020))
211						continue;
212					(void) strcpy(tty, line);
213					status = SUCCESS;
214					break;
215				}
216			}
217			if (strcmp(line, tty) == 0) {
218				status = SUCCESS;
219				break;
220			}
221		}
222	fclose(fd);
223	return (status);
224}
225