process.c revision 1.5 1 /* $NetBSD: process.c,v 1.5 1998/07/03 11:54:08 mrg 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.5 1998/07/03 11:54:08 mrg 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 #include <utmp.h>
68
69 #include "extern.h"
70
71 void
72 process_request(mp, rp)
73 CTL_MSG *mp;
74 CTL_RESPONSE *rp;
75 {
76 CTL_MSG *ptr;
77 extern int debug;
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)
104 print_request("process_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", 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 struct utmp ubuf;
194 int status;
195 FILE *fd;
196 struct stat statb;
197 char line[sizeof(ubuf.ut_line) + 1];
198 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
199 time_t atime = 0;
200 int anytty = 0;
201
202 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
203 fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
204 return (FAILED);
205 }
206 #define SCMPN(a, b) strncmp(a, b, sizeof (a))
207 status = NOT_HERE;
208 (void) strcpy(ftty, _PATH_DEV);
209
210 if (*tty == '\0')
211 anytty = 1;
212
213 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
214 if (SCMPN(ubuf.ut_name, name) != 0)
215 continue;
216 (void)strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
217 line[sizeof(ubuf.ut_line)] = '\0';
218 if (anytty) {
219 /* no particular tty was requested */
220 /* XXX strcpy is safe */
221 (void)strcpy(ftty + sizeof(_PATH_DEV) - 1, line);
222 if (stat(ftty, &statb) == 0) {
223 if (!(statb.st_mode & S_IWGRP)) {
224 if (status != SUCCESS)
225 status = PERMISSION_DENIED;
226 continue;
227 }
228 if (statb.st_atime > atime) {
229 atime = statb.st_atime;
230 (void)strcpy(tty, line);
231 status = SUCCESS;
232 }
233 }
234 } else if (strcmp(line, tty) == 0) {
235 status = SUCCESS;
236 break;
237 }
238 }
239 (void)fclose(fd);
240 return (status);
241 }
242