xref: /openbsd/libexec/talkd/process.c (revision 78b63d65)
1 /*	$OpenBSD: process.c,v 1.10 2001/12/07 18:45:33 mpech Exp $	*/
2 
3 /*
4  * Copyright (c) 1983 Regents of the University of California.
5  * 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. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 /*static char sccsid[] = "from: @(#)process.c	5.10 (Berkeley) 2/26/91";*/
38 static char rcsid[] = "$Id: process.c,v 1.10 2001/12/07 18:45:33 mpech Exp $";
39 #endif /* not lint */
40 
41 /*
42  * process.c handles the requests, which can be of three types:
43  *	ANNOUNCE - announce to a user that a talk is wanted
44  *	LEAVE_INVITE - insert the request into the table
45  *	LOOK_UP - look up to see if a request is waiting in
46  *		  in the table for the local user
47  *	DELETE - delete invitation
48  */
49 #include <sys/param.h>
50 #include <sys/stat.h>
51 #include <sys/socket.h>
52 #include <netinet/in.h>
53 #include <arpa/inet.h>
54 #include <protocols/talkd.h>
55 #include <netdb.h>
56 #include <syslog.h>
57 #include <stdio.h>
58 #include <string.h>
59 #include <ctype.h>
60 #include <paths.h>
61 #include "talkd.h"
62 
63 #define	satosin(sa)	((struct sockaddr_in *)(sa))
64 
65 void
66 process_request(mp, rp)
67 	CTL_MSG *mp;
68 	CTL_RESPONSE *rp;
69 {
70 	CTL_MSG *ptr;
71 	char *s;
72 
73 	rp->vers = TALK_VERSION;
74 	rp->type = mp->type;
75 	rp->id_num = htonl(0);
76 	if (mp->vers != TALK_VERSION) {
77 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
78 		rp->answer = BADVERSION;
79 		return;
80 	}
81 	mp->id_num = ntohl(mp->id_num);
82 	if (ntohs(mp->addr.sa_family) != AF_INET) {
83 		syslog(LOG_WARNING, "Bad address, family %d",
84 		    ntohs(mp->addr.sa_family));
85 		rp->answer = BADADDR;
86 		return;
87 	}
88 	if (ntohs(mp->ctl_addr.sa_family) != AF_INET) {
89 		syslog(LOG_WARNING, "Bad control address, family %d",
90 		    ntohs(mp->ctl_addr.sa_family));
91 		rp->answer = BADCTLADDR;
92 		return;
93 	}
94 	for (s = mp->l_name; *s; s++)
95 		if (!isprint(*s)) {
96 			syslog(LOG_NOTICE, "Illegal user name. Aborting");
97 			rp->answer = FAILED;
98 			return;
99 		}
100 	if (memcmp(&satosin(&rp->addr)->sin_addr,
101 		   &satosin(&mp->ctl_addr)->sin_addr,
102 		   sizeof(struct in_addr))) {
103 		char	buf1[32], buf2[32];
104 		strcpy(buf1, inet_ntoa(satosin(&rp->addr)->sin_addr));
105 		strcpy(buf2, inet_ntoa(satosin(&mp->ctl_addr)->sin_addr));
106 		syslog(LOG_WARNING, "addresses are different, %s != %s",
107 		       buf1, buf2);
108 	}
109 	rp->addr.sa_family = 0;
110 	mp->pid = ntohl(mp->pid);
111 	if (debug)
112 		print_request("process_request", mp);
113 	switch (mp->type) {
114 
115 	case ANNOUNCE:
116 		do_announce(mp, rp);
117 		break;
118 
119 	case LEAVE_INVITE:
120 		ptr = find_request(mp);
121 		if (ptr != (CTL_MSG *)0) {
122 			rp->id_num = htonl(ptr->id_num);
123 			rp->answer = SUCCESS;
124 		} else
125 			insert_table(mp, rp);
126 		break;
127 
128 	case LOOK_UP:
129 		ptr = find_match(mp);
130 		if (ptr != (CTL_MSG *)0) {
131 			rp->id_num = htonl(ptr->id_num);
132 			rp->addr = ptr->addr;
133 			rp->addr.sa_family = ptr->addr.sa_family;
134 			rp->answer = SUCCESS;
135 		} else
136 			rp->answer = NOT_HERE;
137 		break;
138 
139 	case DELETE:
140 		rp->answer = delete_invite(mp->id_num);
141 		break;
142 
143 	default:
144 		rp->answer = UNKNOWN_REQUEST;
145 		break;
146 	}
147 	if (debug)
148 		print_response("process_request", rp);
149 }
150 
151 void
152 do_announce(mp, rp)
153 	CTL_MSG *mp;
154 	CTL_RESPONSE *rp;
155 {
156 	struct hostent *hp;
157 	CTL_MSG *ptr;
158 	int result;
159 
160 	/* see if the user is logged */
161 	result = find_user(mp->r_name, mp->r_tty);
162 	if (result != SUCCESS) {
163 		rp->answer = result;
164 		return;
165 	}
166 	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
167 		sizeof (struct in_addr), AF_INET);
168 	if (hp == (struct hostent *)0) {
169 		rp->answer = MACHINE_UNKNOWN;
170 		return;
171 	}
172 	ptr = find_request(mp);
173 	if (ptr == (CTL_MSG *) 0) {
174 		insert_table(mp, rp);
175 		rp->answer = announce(mp, hp->h_name);
176 		return;
177 	}
178 	if (mp->id_num > ptr->id_num) {
179 		/*
180 		 * This is an explicit re-announce, so update the id_num
181 		 * field to avoid duplicates and re-announce the talk.
182 		 */
183 		ptr->id_num = new_id();
184 		rp->id_num = htonl(ptr->id_num);
185 		rp->answer = announce(mp, hp->h_name);
186 	} else {
187 		/* a duplicated request, so ignore it */
188 		rp->id_num = htonl(ptr->id_num);
189 		rp->answer = SUCCESS;
190 	}
191 }
192 
193 #include <utmp.h>
194 
195 /*
196  * Search utmp for the local user
197  */
198 int
199 find_user(name, tty)
200 	char *name, *tty;
201 {
202 	struct utmp ubuf, ubuf1;
203 	int status;
204 	FILE *fd;
205 	char ftty[20];
206 	time_t	idle, now;
207 
208 	time(&now);
209 	idle = INT_MAX;
210 	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
211 		fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
212 		return (FAILED);
213 	}
214 #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
215 	status = NOT_HERE;
216 	(void) strcpy(ftty, _PATH_DEV);
217 	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
218 		if (SCMPN(ubuf.ut_name, name) == 0) {
219 			if (*tty == '\0') {
220 				/* no particular tty was requested */
221 				struct stat statb;
222 
223 				strcpy(ftty+sizeof(_PATH_DEV)-1, ubuf.ut_line);
224 				if (stat(ftty, &statb) == 0) {
225 					if (!(statb.st_mode & S_IWGRP)) {
226 						if (status == NOT_HERE)
227 							status = PERMISSION_DENIED;
228 					} else if (now - statb.st_atime < idle) {
229 						idle = now - statb.st_atime;
230 						status = SUCCESS;
231 						ubuf1 = ubuf;
232 					}
233 				}
234 			} else if (strcmp(ubuf.ut_line, tty) == 0) {
235 				status = SUCCESS;
236 				break;
237 			}
238 		}
239 	fclose(fd);
240 	if (*tty == '\0' && status == SUCCESS)
241 		strcpy(tty, ubuf1.ut_line);
242 	return (status);
243 }
244