process.c revision 1.3 1 1.3 christos /* $NetBSD: process.c,v 1.3 1997/06/29 18:01:14 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.3 christos __RCSID("$NetBSD: process.c,v 1.3 1997/06/29 18:01:14 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.1 cgd
64 1.1 cgd CTL_MSG *find_request();
65 1.1 cgd CTL_MSG *find_match();
66 1.1 cgd
67 1.1 cgd process_request(mp, rp)
68 1.1 cgd register CTL_MSG *mp;
69 1.1 cgd register CTL_RESPONSE *rp;
70 1.1 cgd {
71 1.1 cgd register CTL_MSG *ptr;
72 1.1 cgd extern int debug;
73 1.1 cgd
74 1.1 cgd rp->vers = TALK_VERSION;
75 1.1 cgd rp->type = mp->type;
76 1.1 cgd rp->id_num = htonl(0);
77 1.1 cgd if (mp->vers != TALK_VERSION) {
78 1.1 cgd syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
79 1.1 cgd rp->answer = BADVERSION;
80 1.1 cgd return;
81 1.1 cgd }
82 1.1 cgd mp->id_num = ntohl(mp->id_num);
83 1.1 cgd mp->addr.sa_family = ntohs(mp->addr.sa_family);
84 1.1 cgd if (mp->addr.sa_family != AF_INET) {
85 1.1 cgd syslog(LOG_WARNING, "Bad address, family %d",
86 1.1 cgd mp->addr.sa_family);
87 1.1 cgd rp->answer = BADADDR;
88 1.1 cgd return;
89 1.1 cgd }
90 1.1 cgd mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
91 1.1 cgd if (mp->ctl_addr.sa_family != AF_INET) {
92 1.1 cgd syslog(LOG_WARNING, "Bad control address, family %d",
93 1.1 cgd mp->ctl_addr.sa_family);
94 1.1 cgd rp->answer = BADCTLADDR;
95 1.1 cgd return;
96 1.1 cgd }
97 1.1 cgd mp->pid = ntohl(mp->pid);
98 1.1 cgd if (debug)
99 1.1 cgd print_request("process_request", mp);
100 1.1 cgd switch (mp->type) {
101 1.1 cgd
102 1.1 cgd case ANNOUNCE:
103 1.1 cgd do_announce(mp, rp);
104 1.1 cgd break;
105 1.1 cgd
106 1.1 cgd case LEAVE_INVITE:
107 1.1 cgd ptr = find_request(mp);
108 1.1 cgd if (ptr != (CTL_MSG *)0) {
109 1.1 cgd rp->id_num = htonl(ptr->id_num);
110 1.1 cgd rp->answer = SUCCESS;
111 1.1 cgd } else
112 1.1 cgd insert_table(mp, rp);
113 1.1 cgd break;
114 1.1 cgd
115 1.1 cgd case LOOK_UP:
116 1.1 cgd ptr = find_match(mp);
117 1.1 cgd if (ptr != (CTL_MSG *)0) {
118 1.1 cgd rp->id_num = htonl(ptr->id_num);
119 1.1 cgd rp->addr = ptr->addr;
120 1.1 cgd rp->addr.sa_family = htons(ptr->addr.sa_family);
121 1.1 cgd rp->answer = SUCCESS;
122 1.1 cgd } else
123 1.1 cgd rp->answer = NOT_HERE;
124 1.1 cgd break;
125 1.1 cgd
126 1.1 cgd case DELETE:
127 1.1 cgd rp->answer = delete_invite(mp->id_num);
128 1.1 cgd break;
129 1.1 cgd
130 1.1 cgd default:
131 1.1 cgd rp->answer = UNKNOWN_REQUEST;
132 1.1 cgd break;
133 1.1 cgd }
134 1.1 cgd if (debug)
135 1.1 cgd print_response("process_request", rp);
136 1.1 cgd }
137 1.1 cgd
138 1.1 cgd do_announce(mp, rp)
139 1.1 cgd register 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.1 cgd find_user(name, tty)
186 1.1 cgd char *name, *tty;
187 1.1 cgd {
188 1.1 cgd struct utmp ubuf;
189 1.1 cgd int status;
190 1.1 cgd FILE *fd;
191 1.1 cgd struct stat statb;
192 1.3 christos char line[sizeof(ubuf.ut_line) + 1];
193 1.3 christos char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
194 1.1 cgd
195 1.1 cgd if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
196 1.1 cgd fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
197 1.1 cgd return (FAILED);
198 1.1 cgd }
199 1.1 cgd #define SCMPN(a, b) strncmp(a, b, sizeof (a))
200 1.1 cgd status = NOT_HERE;
201 1.1 cgd (void) strcpy(ftty, _PATH_DEV);
202 1.1 cgd while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1)
203 1.1 cgd if (SCMPN(ubuf.ut_name, name) == 0) {
204 1.3 christos (void)strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
205 1.3 christos line[sizeof(ubuf.ut_line)] = '\0';
206 1.1 cgd if (*tty == '\0') {
207 1.1 cgd status = PERMISSION_DENIED;
208 1.1 cgd /* no particular tty was requested */
209 1.3 christos (void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
210 1.3 christos line);
211 1.3 christos if (stat(ftty, &statb) == 0) {
212 1.1 cgd if (!(statb.st_mode & 020))
213 1.1 cgd continue;
214 1.3 christos (void) strcpy(tty, line);
215 1.1 cgd status = SUCCESS;
216 1.1 cgd break;
217 1.1 cgd }
218 1.1 cgd }
219 1.3 christos if (strcmp(line, tty) == 0) {
220 1.1 cgd status = SUCCESS;
221 1.1 cgd break;
222 1.1 cgd }
223 1.1 cgd }
224 1.1 cgd fclose(fd);
225 1.1 cgd return (status);
226 1.1 cgd }
227