xref: /freebsd/libexec/talkd/process.c (revision e627b39baccd1ec9129690167cf5e6d860509655)
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  *	$Id$
34  */
35 
36 #ifndef lint
37 static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
38 #endif /* not lint */
39 
40 /*
41  * process.c handles the requests, which can be of three types:
42  *	ANNOUNCE - announce to a user that a talk is wanted
43  *	LEAVE_INVITE - insert the request into the table
44  *	LOOK_UP - look up to see if a request is waiting in
45  *		  in the table for the local user
46  *	DELETE - delete invitation
47  */
48 #include <sys/param.h>
49 #include <sys/stat.h>
50 #include <sys/socket.h>
51 #include <netinet/in.h>
52 #include <protocols/talkd.h>
53 #include <netdb.h>
54 #include <syslog.h>
55 #include <stdio.h>
56 #include <string.h>
57 #include <ctype.h>
58 #include <paths.h>
59 
60 CTL_MSG *find_request();
61 CTL_MSG *find_match();
62 
63 process_request(mp, rp)
64 	register CTL_MSG *mp;
65 	register CTL_RESPONSE *rp;
66 {
67 	register CTL_MSG *ptr;
68 	extern int debug;
69 	char *s;
70 
71 	rp->vers = TALK_VERSION;
72 	rp->type = mp->type;
73 	rp->id_num = htonl(0);
74 	if (mp->vers != TALK_VERSION) {
75 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
76 		rp->answer = BADVERSION;
77 		return;
78 	}
79 	mp->id_num = ntohl(mp->id_num);
80 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
81 	if (mp->addr.sa_family != AF_INET) {
82 		syslog(LOG_WARNING, "Bad address, family %d",
83 		    mp->addr.sa_family);
84 		rp->answer = BADADDR;
85 		return;
86 	}
87 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
88 	if (mp->ctl_addr.sa_family != AF_INET) {
89 		syslog(LOG_WARNING, "Bad control address, family %d",
90 		    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 	mp->pid = ntohl(mp->pid);
101 	if (debug)
102 		print_request("process_request", mp);
103 	switch (mp->type) {
104 
105 	case ANNOUNCE:
106 		do_announce(mp, rp);
107 		break;
108 
109 	case LEAVE_INVITE:
110 		ptr = find_request(mp);
111 		if (ptr != (CTL_MSG *)0) {
112 			rp->id_num = htonl(ptr->id_num);
113 			rp->answer = SUCCESS;
114 		} else
115 			insert_table(mp, rp);
116 		break;
117 
118 	case LOOK_UP:
119 		ptr = find_match(mp);
120 		if (ptr != (CTL_MSG *)0) {
121 			rp->id_num = htonl(ptr->id_num);
122 			rp->addr = ptr->addr;
123 			rp->addr.sa_family = htons(ptr->addr.sa_family);
124 			rp->answer = SUCCESS;
125 		} else
126 			rp->answer = NOT_HERE;
127 		break;
128 
129 	case DELETE:
130 		rp->answer = delete_invite(mp->id_num);
131 		break;
132 
133 	default:
134 		rp->answer = UNKNOWN_REQUEST;
135 		break;
136 	}
137 	if (debug)
138 		print_response("process_request", rp);
139 }
140 
141 do_announce(mp, rp)
142 	register CTL_MSG *mp;
143 	CTL_RESPONSE *rp;
144 {
145 	struct hostent *hp;
146 	CTL_MSG *ptr;
147 	int result;
148 
149 	/* see if the user is logged */
150 	result = find_user(mp->r_name, mp->r_tty);
151 	if (result != SUCCESS) {
152 		rp->answer = result;
153 		return;
154 	}
155 #define	satosin(sa)	((struct sockaddr_in *)(sa))
156 	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
157 		sizeof (struct in_addr), AF_INET);
158 	if (hp == (struct hostent *)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, hp->h_name);
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, hp->h_name);
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 #include <utmp.h>
184 
185 /*
186  * Search utmp for the local user
187  */
188 find_user(name, tty)
189 	char *name, *tty;
190 {
191 	struct utmp ubuf;
192 	int status;
193 	FILE *fd;
194 	struct stat statb;
195 	time_t best = 0;
196 	char line[sizeof(ubuf.ut_line) + 1];
197 	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
198 
199 	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
200 		fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
201 		return (FAILED);
202 	}
203 #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
204 	status = NOT_HERE;
205 	(void) strcpy(ftty, _PATH_DEV);
206 	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
207 		if (SCMPN(ubuf.ut_name, name) == 0) {
208 			strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
209 			line[sizeof(ubuf.ut_line)] = '\0';
210 			if (*tty == '\0' || best != 0) {
211 				if (best == 0)
212 					status = PERMISSION_DENIED;
213 				/* no particular tty was requested */
214 				(void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
215 				    line);
216 				if (stat(ftty, &statb) == 0) {
217 					if (!(statb.st_mode & 020))
218 						continue;
219 					if (statb.st_atime > best) {
220 						best = statb.st_atime;
221 						(void) strcpy(tty, line);
222 						status = SUCCESS;
223 						continue;
224 					}
225 				}
226 			}
227 			if (strcmp(line, tty) == 0) {
228 				status = SUCCESS;
229 				break;
230 			}
231 		}
232 	fclose(fd);
233 	return (status);
234 }
235