xref: /netbsd/libexec/talkd/process.c (revision c4a72b64)
1 /*	$NetBSD: process.c,v 1.8 2002/09/19 14:39:51 itojun 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.8 2002/09/19 14:39:51 itojun 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 	mp->id_num = ntohl(mp->id_num);
83 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
84 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
85 	mp->pid = ntohl(mp->pid);
86 	if (mp->vers != TALK_VERSION) {
87 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
88 		rp->answer = BADVERSION;
89 		return;
90 	}
91 	if (mp->addr.sa_family != AF_INET) {
92 		syslog(LOG_WARNING, "Bad address, family %d",
93 		    mp->addr.sa_family);
94 		rp->answer = BADADDR;
95 		return;
96 	}
97 	if (mp->ctl_addr.sa_family != AF_INET) {
98 		syslog(LOG_WARNING, "Bad control address, family %d",
99 		    mp->ctl_addr.sa_family);
100 		rp->answer = BADCTLADDR;
101 		return;
102 	}
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, sizeof(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, ttysize)
191 	char *name, *tty;
192 	size_t ttysize;
193 {
194 	int status;
195 	struct stat statb;
196 	struct utmpentry *ep;
197 	char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
198 	time_t atime = 0;
199 	int anytty = 0;
200 
201 	(void)getutentries(NULL, &ep);
202 
203 	status = NOT_HERE;
204 	(void) strlcpy(ftty, _PATH_DEV, sizeof(ftty));
205 
206 	if (*tty == '\0')
207 		anytty = 1;
208 
209 	for (; ep; ep = ep->next) {
210 		if (strcmp(ep->name, name) != 0)
211 			continue;
212 		if (anytty) {
213 			/* no particular tty was requested */
214 			/* XXX strcpy is safe */
215 			(void)strcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line);
216 			if (stat(ftty, &statb) != 0)
217 				continue;
218 
219 			if (!(statb.st_mode & S_IWGRP)) {
220 				if (status != SUCCESS)
221 					status = PERMISSION_DENIED;
222 				continue;
223 			}
224 			if (statb.st_atime > atime &&
225 			    strlcpy(tty, ep->line, ttysize) < ttysize) {
226 				atime = statb.st_atime;
227 				status = SUCCESS;
228 			}
229 		} else if (strcmp(ep->line, tty) == 0) {
230 			status = SUCCESS;
231 			break;
232 		}
233 	}
234 	return (status);
235 }
236