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