xref: /netbsd-src/libexec/talkd/process.c (revision 404fbe5fb94ca1e054339640cabb2801ce52dd30)
1 /*	$NetBSD: process.c,v 1.12 2007/01/08 17:51:34 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. 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 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
36 #else
37 __RCSID("$NetBSD: process.c,v 1.12 2007/01/08 17:51:34 christos Exp $");
38 #endif
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 
50 #include <sys/param.h>
51 #include <sys/stat.h>
52 #include <sys/socket.h>
53 
54 #include <netinet/in.h>
55 
56 #include <protocols/talkd.h>
57 
58 #include <netdb.h>
59 #include <syslog.h>
60 #include <stdio.h>
61 #include <string.h>
62 #include <paths.h>
63 
64 #include "extern.h"
65 
66 #include "utmpentry.h"
67 
68 void
69 process_request(mp, rp)
70 	CTL_MSG *mp;
71 	CTL_RESPONSE *rp;
72 {
73 	CTL_MSG *ptr;
74 
75 	rp->vers = TALK_VERSION;
76 	rp->type = mp->type;
77 	rp->id_num = htonl(0);
78 	mp->id_num = ntohl(mp->id_num);
79 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
80 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
81 	mp->pid = ntohl(mp->pid);
82 	if (mp->vers != TALK_VERSION) {
83 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
84 		rp->answer = BADVERSION;
85 		return;
86 	}
87 	if (mp->addr.sa_family != AF_INET) {
88 		syslog(LOG_WARNING, "Bad address, family %d",
89 		    mp->addr.sa_family);
90 		rp->answer = BADADDR;
91 		return;
92 	}
93 	if (mp->ctl_addr.sa_family != AF_INET) {
94 		syslog(LOG_WARNING, "Bad control address, family %d",
95 		    mp->ctl_addr.sa_family);
96 		rp->answer = BADCTLADDR;
97 		return;
98 	}
99 	if (debug || logging)
100 		print_request("request", mp);
101 	switch (mp->type) {
102 
103 	case ANNOUNCE:
104 		do_announce(mp, rp);
105 		break;
106 
107 	case LEAVE_INVITE:
108 		ptr = find_request(mp);
109 		if (ptr != (CTL_MSG *)0) {
110 			rp->id_num = htonl(ptr->id_num);
111 			rp->answer = SUCCESS;
112 		} else
113 			insert_table(mp, rp);
114 		break;
115 
116 	case LOOK_UP:
117 		ptr = find_match(mp);
118 		if (ptr != (CTL_MSG *)0) {
119 			rp->id_num = htonl(ptr->id_num);
120 			rp->addr = ptr->addr;
121 			rp->addr.sa_family = htons(ptr->addr.sa_family);
122 			rp->answer = SUCCESS;
123 		} else
124 			rp->answer = NOT_HERE;
125 		break;
126 
127 	case DELETE:
128 		rp->answer = delete_invite(mp->id_num);
129 		break;
130 
131 	default:
132 		rp->answer = UNKNOWN_REQUEST;
133 		break;
134 	}
135 	if (debug)
136 		print_response("process_request done", rp);
137 }
138 
139 void
140 do_announce(mp, rp)
141 	CTL_MSG *mp;
142 	CTL_RESPONSE *rp;
143 {
144 	CTL_MSG *ptr;
145 	int result;
146 	char hostname[NI_MAXHOST];
147 	struct sockaddr sa;
148 
149 	tsa2sa(&sa, &mp->ctl_addr);
150 
151 	/* see if the user is logged */
152 	result = find_user(mp->r_name, mp->r_tty, sizeof(mp->r_tty));
153 	if (result != SUCCESS) {
154 		rp->answer = result;
155 		return;
156 	}
157 	if (getnameinfo(&sa, sa.sa_len, hostname, sizeof(hostname), NULL,
158 	    0, 0)) {
159 		rp->answer = MACHINE_UNKNOWN;
160 		return;
161 	}
162 	ptr = find_request(mp);
163 	if (ptr == (CTL_MSG *) 0) {
164 		insert_table(mp, rp);
165 		rp->answer = announce(mp, hostname);
166 		return;
167 	}
168 	if (mp->id_num > ptr->id_num) {
169 		/*
170 		 * This is an explicit re-announce, so update the id_num
171 		 * field to avoid duplicates and re-announce the talk.
172 		 */
173 		ptr->id_num = new_id();
174 		rp->id_num = htonl(ptr->id_num);
175 		rp->answer = announce(mp, hostname);
176 	} else {
177 		/* a duplicated request, so ignore it */
178 		rp->id_num = htonl(ptr->id_num);
179 		rp->answer = SUCCESS;
180 	}
181 }
182 
183 /*
184  * Search utmp for the local user
185  */
186 int
187 find_user(name, tty, ttysize)
188 	char *name, *tty;
189 	size_t ttysize;
190 {
191 	int status;
192 	struct stat statb;
193 	struct utmpentry *ep;
194 	char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
195 	time_t atime = 0;
196 	int anytty = 0;
197 
198 	(void)getutentries(NULL, &ep);
199 
200 	status = NOT_HERE;
201 	(void) strlcpy(ftty, _PATH_DEV, sizeof(ftty));
202 
203 	if (*tty == '\0')
204 		anytty = 1;
205 
206 	for (; ep; ep = ep->next) {
207 		if (strcmp(ep->name, name) != 0)
208 			continue;
209 		if (anytty) {
210 			/* no particular tty was requested */
211 			(void)strlcpy(ftty + sizeof(_PATH_DEV) - 1, ep->line,
212 			     sizeof(ftty) - sizeof(_PATH_DEV) + 1);
213 			if (stat(ftty, &statb) != 0)
214 				continue;
215 
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 			    strlcpy(tty, ep->line, ttysize) < ttysize) {
223 				atime = statb.st_atime;
224 				status = SUCCESS;
225 			}
226 		} else if (strcmp(ep->line, tty) == 0) {
227 			status = SUCCESS;
228 			break;
229 		}
230 	}
231 	return (status);
232 }
233