process.c revision 201022
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
35#if 0
36static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
37#endif
38static const char rcsid[] =
39  "$FreeBSD: head/libexec/talkd/process.c 201022 2009-12-26 14:33:55Z ed $";
40#endif /* not lint */
41
42/*
43 * process.c handles the requests, which can be of three types:
44 *	ANNOUNCE - announce to a user that a talk is wanted
45 *	LEAVE_INVITE - insert the request into the table
46 *	LOOK_UP - look up to see if a request is waiting in
47 *		  in the table for the local user
48 *	DELETE - delete invitation
49 */
50#include <sys/param.h>
51#include <sys/stat.h>
52#include <sys/socket.h>
53#include <netinet/in.h>
54#include <protocols/talkd.h>
55#include <ctype.h>
56#include <err.h>
57#include <netdb.h>
58#include <paths.h>
59#include <stdio.h>
60#include <string.h>
61#include <syslog.h>
62#define	_ULOG_POSIX_NAMES
63#include <ulog.h>
64
65#include "extern.h"
66
67extern int debug;
68
69void
70process_request(CTL_MSG *mp, CTL_RESPONSE *rp)
71{
72	CTL_MSG *ptr;
73	char *s;
74
75	rp->vers = TALK_VERSION;
76	rp->type = mp->type;
77	rp->id_num = htonl(0);
78	if (mp->vers != TALK_VERSION) {
79		syslog(LOG_WARNING, "bad protocol version %d", mp->vers);
80		rp->answer = BADVERSION;
81		return;
82	}
83	mp->id_num = ntohl(mp->id_num);
84	mp->addr.sa_family = ntohs(mp->addr.sa_family);
85	if (mp->addr.sa_family != AF_INET) {
86		syslog(LOG_WARNING, "bad address, family %d",
87		    mp->addr.sa_family);
88		rp->answer = BADADDR;
89		return;
90	}
91	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
92	if (mp->ctl_addr.sa_family != AF_INET) {
93		syslog(LOG_WARNING, "bad control address, family %d",
94		    mp->ctl_addr.sa_family);
95		rp->answer = BADCTLADDR;
96		return;
97	}
98	for (s = mp->l_name; *s; s++)
99		if (!isprint(*s)) {
100			syslog(LOG_NOTICE, "illegal user name. Aborting");
101			rp->answer = FAILED;
102			return;
103		}
104	mp->pid = ntohl(mp->pid);
105	if (debug)
106		print_request("process_request", mp);
107	switch (mp->type) {
108
109	case ANNOUNCE:
110		do_announce(mp, rp);
111		break;
112
113	case LEAVE_INVITE:
114		ptr = find_request(mp);
115		if (ptr != (CTL_MSG *)0) {
116			rp->id_num = htonl(ptr->id_num);
117			rp->answer = SUCCESS;
118		} else
119			insert_table(mp, rp);
120		break;
121
122	case LOOK_UP:
123		ptr = find_match(mp);
124		if (ptr != (CTL_MSG *)0) {
125			rp->id_num = htonl(ptr->id_num);
126			rp->addr = ptr->addr;
127			rp->addr.sa_family = htons(ptr->addr.sa_family);
128			rp->answer = SUCCESS;
129		} else
130			rp->answer = NOT_HERE;
131		break;
132
133	case DELETE:
134		rp->answer = delete_invite(mp->id_num);
135		break;
136
137	default:
138		rp->answer = UNKNOWN_REQUEST;
139		break;
140	}
141	if (debug)
142		print_response("process_request", rp);
143}
144
145void
146do_announce(CTL_MSG *mp, CTL_RESPONSE *rp)
147{
148	struct hostent *hp;
149	CTL_MSG *ptr;
150	int result;
151
152	/* see if the user is logged */
153	result = find_user(mp->r_name, mp->r_tty);
154	if (result != SUCCESS) {
155		rp->answer = result;
156		return;
157	}
158#define	satosin(sa)	((struct sockaddr_in *)(void *)(sa))
159	hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr,
160		sizeof (struct in_addr), AF_INET);
161	if (hp == (struct hostent *)0) {
162		rp->answer = MACHINE_UNKNOWN;
163		return;
164	}
165	ptr = find_request(mp);
166	if (ptr == (CTL_MSG *) 0) {
167		insert_table(mp, rp);
168		rp->answer = announce(mp, hp->h_name);
169		return;
170	}
171	if (mp->id_num > ptr->id_num) {
172		/*
173		 * This is an explicit re-announce, so update the id_num
174		 * field to avoid duplicates and re-announce the talk.
175		 */
176		ptr->id_num = new_id();
177		rp->id_num = htonl(ptr->id_num);
178		rp->answer = announce(mp, hp->h_name);
179	} else {
180		/* a duplicated request, so ignore it */
181		rp->id_num = htonl(ptr->id_num);
182		rp->answer = SUCCESS;
183	}
184}
185
186/*
187 * Search utmp for the local user
188 */
189int
190find_user(const char *name, char *tty)
191{
192	struct utmpx *ut;
193	int status;
194	struct stat statb;
195	time_t best = 0;
196	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(ut->ut_line)];
197
198	setutxent();
199	status = NOT_HERE;
200	(void) strcpy(ftty, _PATH_DEV);
201	while ((ut = getutxent()) != NULL)
202		if (ut->ut_type == USER_PROCESS &&
203		    strcmp(ut->ut_user, name) == 0) {
204			if (*tty == '\0' || best != 0) {
205				if (best == 0)
206					status = PERMISSION_DENIED;
207				/* no particular tty was requested */
208				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
209				    ut->ut_line);
210				if (stat(ftty, &statb) == 0) {
211					if (!(statb.st_mode & 020))
212						continue;
213					if (statb.st_atime > best) {
214						best = statb.st_atime;
215						(void) strcpy(tty, ut->ut_line);
216						status = SUCCESS;
217						continue;
218					}
219				}
220			}
221			if (strcmp(ut->ut_line, tty) == 0) {
222				status = SUCCESS;
223				break;
224			}
225		}
226	endutxent();
227	return (status);
228}
229