xref: /dragonfly/libexec/talkd/process.c (revision 17b61719)
1 /*
2  * Copyright (c) 1983, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)process.c	8.2 (Berkeley) 11/16/93
34  * $FreeBSD: src/libexec/talkd/process.c,v 1.9 1999/08/28 00:10:16 peter Exp $
35  * $DragonFly: src/libexec/talkd/process.c,v 1.3 2003/11/14 03:54:31 dillon Exp $
36  */
37 
38 /*
39  * process.c handles the requests, which can be of three types:
40  *	ANNOUNCE - announce to a user that a talk is wanted
41  *	LEAVE_INVITE - insert the request into the table
42  *	LOOK_UP - look up to see if a request is waiting in
43  *		  in the table for the local user
44  *	DELETE - delete invitation
45  */
46 #include <sys/param.h>
47 #include <sys/stat.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <protocols/talkd.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <netdb.h>
54 #include <paths.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <syslog.h>
58 
59 int announce (CTL_MSG *, char *);
60 int delete_invite (int);
61 void do_announce (CTL_MSG *, CTL_RESPONSE *);
62 CTL_MSG *find_request();
63 CTL_MSG *find_match();
64 int find_user (char *, char *);
65 void insert_table (CTL_MSG *, CTL_RESPONSE *);
66 int new_id (void);
67 void print_request (char *, CTL_MSG *);
68 void print_response (char *, CTL_RESPONSE *);
69 
70 void
71 process_request(mp, rp)
72 	register CTL_MSG *mp;
73 	register CTL_RESPONSE *rp;
74 {
75 	register CTL_MSG *ptr;
76 	extern int debug;
77 	char *s;
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 	for (s = mp->l_name; *s; s++)
103 		if (!isprint(*s)) {
104 			syslog(LOG_NOTICE, "illegal user name. Aborting");
105 			rp->answer = FAILED;
106 			return;
107 		}
108 	mp->pid = ntohl(mp->pid);
109 	if (debug)
110 		print_request("process_request", mp);
111 	switch (mp->type) {
112 
113 	case ANNOUNCE:
114 		do_announce(mp, rp);
115 		break;
116 
117 	case LEAVE_INVITE:
118 		ptr = find_request(mp);
119 		if (ptr != (CTL_MSG *)0) {
120 			rp->id_num = htonl(ptr->id_num);
121 			rp->answer = SUCCESS;
122 		} else
123 			insert_table(mp, rp);
124 		break;
125 
126 	case LOOK_UP:
127 		ptr = find_match(mp);
128 		if (ptr != (CTL_MSG *)0) {
129 			rp->id_num = htonl(ptr->id_num);
130 			rp->addr = ptr->addr;
131 			rp->addr.sa_family = htons(ptr->addr.sa_family);
132 			rp->answer = SUCCESS;
133 		} else
134 			rp->answer = NOT_HERE;
135 		break;
136 
137 	case DELETE:
138 		rp->answer = delete_invite(mp->id_num);
139 		break;
140 
141 	default:
142 		rp->answer = UNKNOWN_REQUEST;
143 		break;
144 	}
145 	if (debug)
146 		print_response("process_request", rp);
147 }
148 
149 void
150 do_announce(mp, rp)
151 	register CTL_MSG *mp;
152 	CTL_RESPONSE *rp;
153 {
154 	struct hostent *hp;
155 	CTL_MSG *ptr;
156 	int result;
157 
158 	/* see if the user is logged */
159 	result = find_user(mp->r_name, mp->r_tty);
160 	if (result != SUCCESS) {
161 		rp->answer = result;
162 		return;
163 	}
164 #define	satosin(sa)	((struct sockaddr_in *)(sa))
165 	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
166 		sizeof (struct in_addr), AF_INET);
167 	if (hp == (struct hostent *)0) {
168 		rp->answer = MACHINE_UNKNOWN;
169 		return;
170 	}
171 	ptr = find_request(mp);
172 	if (ptr == (CTL_MSG *) 0) {
173 		insert_table(mp, rp);
174 		rp->answer = announce(mp, hp->h_name);
175 		return;
176 	}
177 	if (mp->id_num > ptr->id_num) {
178 		/*
179 		 * This is an explicit re-announce, so update the id_num
180 		 * field to avoid duplicates and re-announce the talk.
181 		 */
182 		ptr->id_num = new_id();
183 		rp->id_num = htonl(ptr->id_num);
184 		rp->answer = announce(mp, hp->h_name);
185 	} else {
186 		/* a duplicated request, so ignore it */
187 		rp->id_num = htonl(ptr->id_num);
188 		rp->answer = SUCCESS;
189 	}
190 }
191 
192 #include <utmp.h>
193 
194 /*
195  * Search utmp for the local user
196  */
197 int
198 find_user(name, tty)
199 	char *name, *tty;
200 {
201 	struct utmp ubuf;
202 	int status;
203 	FILE *fd;
204 	struct stat statb;
205 	time_t best = 0;
206 	char line[sizeof(ubuf.ut_line) + 1];
207 	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
208 
209 	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
210 		warnx("can't read %s", _PATH_UTMP);
211 		return (FAILED);
212 	}
213 #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
214 	status = NOT_HERE;
215 	(void) strcpy(ftty, _PATH_DEV);
216 	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
217 		if (SCMPN(ubuf.ut_name, name) == 0) {
218 			strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
219 			line[sizeof(ubuf.ut_line)] = '\0';
220 			if (*tty == '\0' || best != 0) {
221 				if (best == 0)
222 					status = PERMISSION_DENIED;
223 				/* no particular tty was requested */
224 				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
225 				    line);
226 				if (stat(ftty, &statb) == 0) {
227 					if (!(statb.st_mode & 020))
228 						continue;
229 					if (statb.st_atime > best) {
230 						best = statb.st_atime;
231 						(void) strcpy(tty, line);
232 						status = SUCCESS;
233 						continue;
234 					}
235 				}
236 			}
237 			if (strcmp(line, tty) == 0) {
238 				status = SUCCESS;
239 				break;
240 			}
241 		}
242 	fclose(fd);
243 	return (status);
244 }
245