process.c revision 1.6 1 1.6 mrg /* $NetBSD: process.c,v 1.6 1998/07/04 19:31:05 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.6 mrg __RCSID("$NetBSD: process.c,v 1.6 1998/07/04 19:31:05 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
78 1.1 cgd rp->vers = TALK_VERSION;
79 1.1 cgd rp->type = mp->type;
80 1.1 cgd rp->id_num = htonl(0);
81 1.1 cgd if (mp->vers != TALK_VERSION) {
82 1.1 cgd syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
83 1.1 cgd rp->answer = BADVERSION;
84 1.1 cgd return;
85 1.1 cgd }
86 1.1 cgd mp->id_num = ntohl(mp->id_num);
87 1.1 cgd mp->addr.sa_family = ntohs(mp->addr.sa_family);
88 1.1 cgd if (mp->addr.sa_family != AF_INET) {
89 1.1 cgd syslog(LOG_WARNING, "Bad address, family %d",
90 1.1 cgd mp->addr.sa_family);
91 1.1 cgd rp->answer = BADADDR;
92 1.1 cgd return;
93 1.1 cgd }
94 1.1 cgd mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
95 1.1 cgd if (mp->ctl_addr.sa_family != AF_INET) {
96 1.1 cgd syslog(LOG_WARNING, "Bad control address, family %d",
97 1.1 cgd mp->ctl_addr.sa_family);
98 1.1 cgd rp->answer = BADCTLADDR;
99 1.1 cgd return;
100 1.1 cgd }
101 1.1 cgd mp->pid = ntohl(mp->pid);
102 1.6 mrg if (debug || logging)
103 1.6 mrg print_request("request", mp);
104 1.1 cgd switch (mp->type) {
105 1.1 cgd
106 1.1 cgd case ANNOUNCE:
107 1.1 cgd do_announce(mp, rp);
108 1.1 cgd break;
109 1.1 cgd
110 1.1 cgd case LEAVE_INVITE:
111 1.1 cgd ptr = find_request(mp);
112 1.1 cgd if (ptr != (CTL_MSG *)0) {
113 1.1 cgd rp->id_num = htonl(ptr->id_num);
114 1.1 cgd rp->answer = SUCCESS;
115 1.1 cgd } else
116 1.1 cgd insert_table(mp, rp);
117 1.1 cgd break;
118 1.1 cgd
119 1.1 cgd case LOOK_UP:
120 1.1 cgd ptr = find_match(mp);
121 1.1 cgd if (ptr != (CTL_MSG *)0) {
122 1.1 cgd rp->id_num = htonl(ptr->id_num);
123 1.1 cgd rp->addr = ptr->addr;
124 1.1 cgd rp->addr.sa_family = htons(ptr->addr.sa_family);
125 1.1 cgd rp->answer = SUCCESS;
126 1.1 cgd } else
127 1.1 cgd rp->answer = NOT_HERE;
128 1.1 cgd break;
129 1.1 cgd
130 1.1 cgd case DELETE:
131 1.1 cgd rp->answer = delete_invite(mp->id_num);
132 1.1 cgd break;
133 1.1 cgd
134 1.1 cgd default:
135 1.1 cgd rp->answer = UNKNOWN_REQUEST;
136 1.1 cgd break;
137 1.1 cgd }
138 1.1 cgd if (debug)
139 1.6 mrg print_response("process_request done", rp);
140 1.1 cgd }
141 1.1 cgd
142 1.4 christos void
143 1.1 cgd do_announce(mp, rp)
144 1.4 christos CTL_MSG *mp;
145 1.1 cgd CTL_RESPONSE *rp;
146 1.1 cgd {
147 1.1 cgd struct hostent *hp;
148 1.1 cgd CTL_MSG *ptr;
149 1.1 cgd int result;
150 1.1 cgd
151 1.1 cgd /* see if the user is logged */
152 1.1 cgd result = find_user(mp->r_name, mp->r_tty);
153 1.1 cgd if (result != SUCCESS) {
154 1.1 cgd rp->answer = result;
155 1.1 cgd return;
156 1.1 cgd }
157 1.1 cgd #define satosin(sa) ((struct sockaddr_in *)(sa))
158 1.1 cgd hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
159 1.1 cgd sizeof (struct in_addr), AF_INET);
160 1.1 cgd if (hp == (struct hostent *)0) {
161 1.1 cgd rp->answer = MACHINE_UNKNOWN;
162 1.1 cgd return;
163 1.1 cgd }
164 1.1 cgd ptr = find_request(mp);
165 1.1 cgd if (ptr == (CTL_MSG *) 0) {
166 1.1 cgd insert_table(mp, rp);
167 1.1 cgd rp->answer = announce(mp, hp->h_name);
168 1.1 cgd return;
169 1.1 cgd }
170 1.1 cgd if (mp->id_num > ptr->id_num) {
171 1.1 cgd /*
172 1.1 cgd * This is an explicit re-announce, so update the id_num
173 1.1 cgd * field to avoid duplicates and re-announce the talk.
174 1.1 cgd */
175 1.1 cgd ptr->id_num = new_id();
176 1.1 cgd rp->id_num = htonl(ptr->id_num);
177 1.1 cgd rp->answer = announce(mp, hp->h_name);
178 1.1 cgd } else {
179 1.1 cgd /* a duplicated request, so ignore it */
180 1.1 cgd rp->id_num = htonl(ptr->id_num);
181 1.1 cgd rp->answer = SUCCESS;
182 1.1 cgd }
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd /*
186 1.1 cgd * Search utmp for the local user
187 1.1 cgd */
188 1.4 christos int
189 1.1 cgd find_user(name, tty)
190 1.1 cgd char *name, *tty;
191 1.1 cgd {
192 1.1 cgd struct utmp ubuf;
193 1.1 cgd int status;
194 1.1 cgd FILE *fd;
195 1.1 cgd struct stat statb;
196 1.3 christos char line[sizeof(ubuf.ut_line) + 1];
197 1.3 christos char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
198 1.4 christos time_t atime = 0;
199 1.4 christos int anytty = 0;
200 1.1 cgd
201 1.1 cgd if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
202 1.1 cgd fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
203 1.1 cgd return (FAILED);
204 1.1 cgd }
205 1.1 cgd #define SCMPN(a, b) strncmp(a, b, sizeof (a))
206 1.1 cgd status = NOT_HERE;
207 1.1 cgd (void) strcpy(ftty, _PATH_DEV);
208 1.4 christos
209 1.4 christos if (*tty == '\0')
210 1.4 christos anytty = 1;
211 1.4 christos
212 1.4 christos while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
213 1.4 christos if (SCMPN(ubuf.ut_name, name) != 0)
214 1.4 christos continue;
215 1.4 christos (void)strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
216 1.4 christos line[sizeof(ubuf.ut_line)] = '\0';
217 1.4 christos if (anytty) {
218 1.4 christos /* no particular tty was requested */
219 1.5 mrg /* XXX strcpy is safe */
220 1.4 christos (void)strcpy(ftty + sizeof(_PATH_DEV) - 1, line);
221 1.4 christos if (stat(ftty, &statb) == 0) {
222 1.5 mrg if (!(statb.st_mode & S_IWGRP)) {
223 1.4 christos if (status != SUCCESS)
224 1.4 christos status = PERMISSION_DENIED;
225 1.4 christos continue;
226 1.4 christos }
227 1.4 christos if (statb.st_atime > atime) {
228 1.4 christos atime = statb.st_atime;
229 1.5 mrg (void)strcpy(tty, line);
230 1.1 cgd status = SUCCESS;
231 1.1 cgd }
232 1.1 cgd }
233 1.4 christos } else if (strcmp(line, tty) == 0) {
234 1.4 christos status = SUCCESS;
235 1.4 christos break;
236 1.1 cgd }
237 1.4 christos }
238 1.4 christos (void)fclose(fd);
239 1.1 cgd return (status);
240 1.1 cgd }
241