xref: /netbsd-src/libexec/talkd/process.c (revision 08c81a9c2dc8c7300e893321eb65c0925d60871c)
1 /*	$NetBSD: process.c,v 1.7 2002/08/20 13:56:50 christos Exp $	*/
2 
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. 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 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
40 #else
41 __RCSID("$NetBSD: process.c,v 1.7 2002/08/20 13:56:50 christos Exp $");
42 #endif
43 #endif /* not lint */
44 
45 /*
46  * process.c handles the requests, which can be of three types:
47  *	ANNOUNCE - announce to a user that a talk is wanted
48  *	LEAVE_INVITE - insert the request into the table
49  *	LOOK_UP - look up to see if a request is waiting in
50  *		  in the table for the local user
51  *	DELETE - delete invitation
52  */
53 
54 #include <sys/param.h>
55 #include <sys/stat.h>
56 #include <sys/socket.h>
57 
58 #include <netinet/in.h>
59 
60 #include <protocols/talkd.h>
61 
62 #include <netdb.h>
63 #include <syslog.h>
64 #include <stdio.h>
65 #include <string.h>
66 #include <paths.h>
67 
68 #include "extern.h"
69 
70 #include "utmpentry.h"
71 
72 void
73 process_request(mp, rp)
74 	CTL_MSG *mp;
75 	CTL_RESPONSE *rp;
76 {
77 	CTL_MSG *ptr;
78 
79 	rp->vers = TALK_VERSION;
80 	rp->type = mp->type;
81 	rp->id_num = htonl(0);
82 	if (mp->vers != TALK_VERSION) {
83 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
84 		rp->answer = BADVERSION;
85 		return;
86 	}
87 	mp->id_num = ntohl(mp->id_num);
88 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
89 	if (mp->addr.sa_family != AF_INET) {
90 		syslog(LOG_WARNING, "Bad address, family %d",
91 		    mp->addr.sa_family);
92 		rp->answer = BADADDR;
93 		return;
94 	}
95 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
96 	if (mp->ctl_addr.sa_family != AF_INET) {
97 		syslog(LOG_WARNING, "Bad control address, family %d",
98 		    mp->ctl_addr.sa_family);
99 		rp->answer = BADCTLADDR;
100 		return;
101 	}
102 	mp->pid = ntohl(mp->pid);
103 	if (debug || logging)
104 		print_request("request", mp);
105 	switch (mp->type) {
106 
107 	case ANNOUNCE:
108 		do_announce(mp, rp);
109 		break;
110 
111 	case LEAVE_INVITE:
112 		ptr = find_request(mp);
113 		if (ptr != (CTL_MSG *)0) {
114 			rp->id_num = htonl(ptr->id_num);
115 			rp->answer = SUCCESS;
116 		} else
117 			insert_table(mp, rp);
118 		break;
119 
120 	case LOOK_UP:
121 		ptr = find_match(mp);
122 		if (ptr != (CTL_MSG *)0) {
123 			rp->id_num = htonl(ptr->id_num);
124 			rp->addr = ptr->addr;
125 			rp->addr.sa_family = htons(ptr->addr.sa_family);
126 			rp->answer = SUCCESS;
127 		} else
128 			rp->answer = NOT_HERE;
129 		break;
130 
131 	case DELETE:
132 		rp->answer = delete_invite(mp->id_num);
133 		break;
134 
135 	default:
136 		rp->answer = UNKNOWN_REQUEST;
137 		break;
138 	}
139 	if (debug)
140 		print_response("process_request done", rp);
141 }
142 
143 void
144 do_announce(mp, rp)
145 	CTL_MSG *mp;
146 	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 *)(sa))
159 	hp = gethostbyaddr((char *)&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  */
189 int
190 find_user(name, tty)
191 	char *name, *tty;
192 {
193 	int status;
194 	struct stat statb;
195 	struct utmpentry *ep;
196 	char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
197 	time_t atime = 0;
198 	int anytty = 0;
199 
200 	(void)getutentries(NULL, &ep);
201 
202 	status = NOT_HERE;
203 	(void) strcpy(ftty, _PATH_DEV);
204 
205 	if (*tty == '\0')
206 		anytty = 1;
207 
208 	for (; ep; ep = ep->next) {
209 		if (strcmp(ep->name, name) != 0)
210 			continue;
211 		if (anytty) {
212 			/* no particular tty was requested */
213 			/* XXX strcpy is safe */
214 			(void)strcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line);
215 			if (stat(ftty, &statb) == 0) {
216 				if (!(statb.st_mode & S_IWGRP)) {
217 					if (status != SUCCESS)
218 						status = PERMISSION_DENIED;
219 					continue;
220 				}
221 				if (statb.st_atime > atime) {
222 					atime = statb.st_atime;
223 					(void)strcpy(tty, ep->line);
224 					status = SUCCESS;
225 				}
226 			}
227 		} else if (strcmp(ep->line, tty) == 0) {
228 			status = SUCCESS;
229 			break;
230 		}
231 	}
232 	return (status);
233 }
234