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