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