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