xref: /freebsd/libexec/talkd/process.c (revision a6fe717c2a876105123214c05176cd74106fb94b)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
4ea022d16SRodney W. Grimes  * Copyright (c) 1983, 1993
5ea022d16SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6ea022d16SRodney W. Grimes  *
7ea022d16SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
8ea022d16SRodney W. Grimes  * modification, are permitted provided that the following conditions
9ea022d16SRodney W. Grimes  * are met:
10ea022d16SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
11ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
12ea022d16SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
13ea022d16SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
14ea022d16SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
155efaea4cSChristian Brueffer  * 3. Neither the name of the University nor the names of its contributors
16ea022d16SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
17ea022d16SRodney W. Grimes  *    without specific prior written permission.
18ea022d16SRodney W. Grimes  *
19ea022d16SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20ea022d16SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21ea022d16SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22ea022d16SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23ea022d16SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24ea022d16SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25ea022d16SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26ea022d16SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27ea022d16SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28ea022d16SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29ea022d16SRodney W. Grimes  * SUCH DAMAGE.
30ea022d16SRodney W. Grimes  */
31ea022d16SRodney W. Grimes 
32ea022d16SRodney W. Grimes /*
33ea022d16SRodney W. Grimes  * process.c handles the requests, which can be of three types:
34ea022d16SRodney W. Grimes  *	ANNOUNCE - announce to a user that a talk is wanted
35ea022d16SRodney W. Grimes  *	LEAVE_INVITE - insert the request into the table
36ea022d16SRodney W. Grimes  *	LOOK_UP - look up to see if a request is waiting in
37ea022d16SRodney W. Grimes  *		  in the table for the local user
38ea022d16SRodney W. Grimes  *	DELETE - delete invitation
39ea022d16SRodney W. Grimes  */
40ea022d16SRodney W. Grimes #include <sys/param.h>
41ea022d16SRodney W. Grimes #include <sys/stat.h>
42ea022d16SRodney W. Grimes #include <sys/socket.h>
43ea022d16SRodney W. Grimes #include <netinet/in.h>
44ea022d16SRodney W. Grimes #include <protocols/talkd.h>
45a846453cSPhilippe Charnier #include <ctype.h>
46a846453cSPhilippe Charnier #include <err.h>
47ea022d16SRodney W. Grimes #include <netdb.h>
48a846453cSPhilippe Charnier #include <paths.h>
49ea022d16SRodney W. Grimes #include <stdio.h>
50ea022d16SRodney W. Grimes #include <string.h>
51a846453cSPhilippe Charnier #include <syslog.h>
52006ab5b3SEd Schouten #include <utmpx.h>
53ea022d16SRodney W. Grimes 
540b67b493SWarner Losh #include "extern.h"
55ea022d16SRodney W. Grimes 
56a846453cSPhilippe Charnier void
process_request(CTL_MSG * mp,CTL_RESPONSE * rp)570b67b493SWarner Losh process_request(CTL_MSG *mp, CTL_RESPONSE *rp)
58ea022d16SRodney W. Grimes {
590b67b493SWarner Losh 	CTL_MSG *ptr;
605a216204SAndrey A. Chernov 	char *s;
61ea022d16SRodney W. Grimes 
62ea022d16SRodney W. Grimes 	rp->vers = TALK_VERSION;
63ea022d16SRodney W. Grimes 	rp->type = mp->type;
64ea022d16SRodney W. Grimes 	rp->id_num = htonl(0);
65ea022d16SRodney W. Grimes 	if (mp->vers != TALK_VERSION) {
66a846453cSPhilippe Charnier 		syslog(LOG_WARNING, "bad protocol version %d", mp->vers);
67ea022d16SRodney W. Grimes 		rp->answer = BADVERSION;
68ea022d16SRodney W. Grimes 		return;
69ea022d16SRodney W. Grimes 	}
70ea022d16SRodney W. Grimes 	mp->id_num = ntohl(mp->id_num);
71ea022d16SRodney W. Grimes 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
72ea022d16SRodney W. Grimes 	if (mp->addr.sa_family != AF_INET) {
73a846453cSPhilippe Charnier 		syslog(LOG_WARNING, "bad address, family %d",
74ea022d16SRodney W. Grimes 		    mp->addr.sa_family);
75ea022d16SRodney W. Grimes 		rp->answer = BADADDR;
76ea022d16SRodney W. Grimes 		return;
77ea022d16SRodney W. Grimes 	}
78ea022d16SRodney W. Grimes 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
79ea022d16SRodney W. Grimes 	if (mp->ctl_addr.sa_family != AF_INET) {
80a846453cSPhilippe Charnier 		syslog(LOG_WARNING, "bad control address, family %d",
81ea022d16SRodney W. Grimes 		    mp->ctl_addr.sa_family);
82ea022d16SRodney W. Grimes 		rp->answer = BADCTLADDR;
83ea022d16SRodney W. Grimes 		return;
84ea022d16SRodney W. Grimes 	}
855a216204SAndrey A. Chernov 	for (s = mp->l_name; *s; s++)
865a216204SAndrey A. Chernov 		if (!isprint(*s)) {
87a846453cSPhilippe Charnier 			syslog(LOG_NOTICE, "illegal user name. Aborting");
88b6cbdb1dSPoul-Henning Kamp 			rp->answer = FAILED;
89b6cbdb1dSPoul-Henning Kamp 			return;
90b6cbdb1dSPoul-Henning Kamp 		}
91ea022d16SRodney W. Grimes 	mp->pid = ntohl(mp->pid);
92ea022d16SRodney W. Grimes 	if (debug)
93ea022d16SRodney W. Grimes 		print_request("process_request", mp);
94ea022d16SRodney W. Grimes 	switch (mp->type) {
95ea022d16SRodney W. Grimes 
96ea022d16SRodney W. Grimes 	case ANNOUNCE:
97ea022d16SRodney W. Grimes 		do_announce(mp, rp);
98ea022d16SRodney W. Grimes 		break;
99ea022d16SRodney W. Grimes 
100ea022d16SRodney W. Grimes 	case LEAVE_INVITE:
101ea022d16SRodney W. Grimes 		ptr = find_request(mp);
102ea022d16SRodney W. Grimes 		if (ptr != (CTL_MSG *)0) {
103ea022d16SRodney W. Grimes 			rp->id_num = htonl(ptr->id_num);
104ea022d16SRodney W. Grimes 			rp->answer = SUCCESS;
105ea022d16SRodney W. Grimes 		} else
106ea022d16SRodney W. Grimes 			insert_table(mp, rp);
107ea022d16SRodney W. Grimes 		break;
108ea022d16SRodney W. Grimes 
109ea022d16SRodney W. Grimes 	case LOOK_UP:
110ea022d16SRodney W. Grimes 		ptr = find_match(mp);
111ea022d16SRodney W. Grimes 		if (ptr != (CTL_MSG *)0) {
112ea022d16SRodney W. Grimes 			rp->id_num = htonl(ptr->id_num);
113ea022d16SRodney W. Grimes 			rp->addr = ptr->addr;
114ea022d16SRodney W. Grimes 			rp->addr.sa_family = htons(ptr->addr.sa_family);
115ea022d16SRodney W. Grimes 			rp->answer = SUCCESS;
116ea022d16SRodney W. Grimes 		} else
117ea022d16SRodney W. Grimes 			rp->answer = NOT_HERE;
118ea022d16SRodney W. Grimes 		break;
119ea022d16SRodney W. Grimes 
120ea022d16SRodney W. Grimes 	case DELETE:
121ea022d16SRodney W. Grimes 		rp->answer = delete_invite(mp->id_num);
122ea022d16SRodney W. Grimes 		break;
123ea022d16SRodney W. Grimes 
124ea022d16SRodney W. Grimes 	default:
125ea022d16SRodney W. Grimes 		rp->answer = UNKNOWN_REQUEST;
126ea022d16SRodney W. Grimes 		break;
127ea022d16SRodney W. Grimes 	}
128ea022d16SRodney W. Grimes 	if (debug)
129ea022d16SRodney W. Grimes 		print_response("process_request", rp);
130ea022d16SRodney W. Grimes }
131ea022d16SRodney W. Grimes 
132a846453cSPhilippe Charnier void
do_announce(CTL_MSG * mp,CTL_RESPONSE * rp)1330b67b493SWarner Losh do_announce(CTL_MSG *mp, CTL_RESPONSE *rp)
134ea022d16SRodney W. Grimes {
135ea022d16SRodney W. Grimes 	struct hostent *hp;
136ea022d16SRodney W. Grimes 	CTL_MSG *ptr;
137ea022d16SRodney W. Grimes 	int result;
138ea022d16SRodney W. Grimes 
139ea022d16SRodney W. Grimes 	/* see if the user is logged */
140ea022d16SRodney W. Grimes 	result = find_user(mp->r_name, mp->r_tty);
141ea022d16SRodney W. Grimes 	if (result != SUCCESS) {
142ea022d16SRodney W. Grimes 		rp->answer = result;
143ea022d16SRodney W. Grimes 		return;
144ea022d16SRodney W. Grimes 	}
145af5bd8feSEd Schouten #define	satosin(sa)	((struct sockaddr_in *)(void *)(sa))
146af5bd8feSEd Schouten 	hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr,
147ea022d16SRodney W. Grimes 		sizeof (struct in_addr), AF_INET);
148ea022d16SRodney W. Grimes 	if (hp == (struct hostent *)0) {
149ea022d16SRodney W. Grimes 		rp->answer = MACHINE_UNKNOWN;
150ea022d16SRodney W. Grimes 		return;
151ea022d16SRodney W. Grimes 	}
152ea022d16SRodney W. Grimes 	ptr = find_request(mp);
153ea022d16SRodney W. Grimes 	if (ptr == (CTL_MSG *) 0) {
154ea022d16SRodney W. Grimes 		insert_table(mp, rp);
155ea022d16SRodney W. Grimes 		rp->answer = announce(mp, hp->h_name);
156ea022d16SRodney W. Grimes 		return;
157ea022d16SRodney W. Grimes 	}
158ea022d16SRodney W. Grimes 	if (mp->id_num > ptr->id_num) {
159ea022d16SRodney W. Grimes 		/*
160ea022d16SRodney W. Grimes 		 * This is an explicit re-announce, so update the id_num
161ea022d16SRodney W. Grimes 		 * field to avoid duplicates and re-announce the talk.
162ea022d16SRodney W. Grimes 		 */
163ea022d16SRodney W. Grimes 		ptr->id_num = new_id();
164ea022d16SRodney W. Grimes 		rp->id_num = htonl(ptr->id_num);
165ea022d16SRodney W. Grimes 		rp->answer = announce(mp, hp->h_name);
166ea022d16SRodney W. Grimes 	} else {
167ea022d16SRodney W. Grimes 		/* a duplicated request, so ignore it */
168ea022d16SRodney W. Grimes 		rp->id_num = htonl(ptr->id_num);
169ea022d16SRodney W. Grimes 		rp->answer = SUCCESS;
170ea022d16SRodney W. Grimes 	}
171ea022d16SRodney W. Grimes }
172ea022d16SRodney W. Grimes 
173ea022d16SRodney W. Grimes /*
174ea022d16SRodney W. Grimes  * Search utmp for the local user
175ea022d16SRodney W. Grimes  */
176a846453cSPhilippe Charnier int
find_user(const char * name,char * tty)1770b67b493SWarner Losh find_user(const char *name, char *tty)
178ea022d16SRodney W. Grimes {
1793eb56f7eSEd Schouten 	struct utmpx *ut;
180ea022d16SRodney W. Grimes 	int status;
181ea022d16SRodney W. Grimes 	struct stat statb;
182a7939d5aSJordan K. Hubbard 	time_t best = 0;
1833eb56f7eSEd Schouten 	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(ut->ut_line)];
184ea022d16SRodney W. Grimes 
1853eb56f7eSEd Schouten 	setutxent();
186ea022d16SRodney W. Grimes 	status = NOT_HERE;
187ea022d16SRodney W. Grimes 	(void) strcpy(ftty, _PATH_DEV);
1883eb56f7eSEd Schouten 	while ((ut = getutxent()) != NULL)
1893eb56f7eSEd Schouten 		if (ut->ut_type == USER_PROCESS &&
1903eb56f7eSEd Schouten 		    strcmp(ut->ut_user, name) == 0) {
191a7939d5aSJordan K. Hubbard 			if (*tty == '\0' || best != 0) {
192a7939d5aSJordan K. Hubbard 				if (best == 0)
193ea022d16SRodney W. Grimes 					status = PERMISSION_DENIED;
194ea022d16SRodney W. Grimes 				/* no particular tty was requested */
195ea022d16SRodney W. Grimes 				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
1963eb56f7eSEd Schouten 				    ut->ut_line);
197ea022d16SRodney W. Grimes 				if (stat(ftty, &statb) == 0) {
198ea022d16SRodney W. Grimes 					if (!(statb.st_mode & 020))
199ea022d16SRodney W. Grimes 						continue;
200a7939d5aSJordan K. Hubbard 					if (statb.st_atime > best) {
201a7939d5aSJordan K. Hubbard 						best = statb.st_atime;
2023eb56f7eSEd Schouten 						(void) strcpy(tty, ut->ut_line);
203ea022d16SRodney W. Grimes 						status = SUCCESS;
204a7939d5aSJordan K. Hubbard 						continue;
205a7939d5aSJordan K. Hubbard 					}
206ea022d16SRodney W. Grimes 				}
207ea022d16SRodney W. Grimes 			}
2083eb56f7eSEd Schouten 			if (strcmp(ut->ut_line, tty) == 0) {
209ea022d16SRodney W. Grimes 				status = SUCCESS;
210ea022d16SRodney W. Grimes 				break;
211ea022d16SRodney W. Grimes 			}
212ea022d16SRodney W. Grimes 		}
2133eb56f7eSEd Schouten 	endutxent();
214ea022d16SRodney W. Grimes 	return (status);
215ea022d16SRodney W. Grimes }
216