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