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