2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93";
34 static const char rcsid[] =
39 * process.c handles the requests, which can be of three types:
40 * ANNOUNCE - announce to a user that a talk is wanted
41 * LEAVE_INVITE - insert the request into the table
42 * LOOK_UP - look up to see if a request is waiting in
43 * in the table for the local user
44 * DELETE - delete invitation
46 #include <sys/param.h>
48 #include <sys/socket.h>
49 #include <netinet/in.h>
50 #include <protocols/talkd.h>
63 process_request(CTL_MSG *mp, CTL_RESPONSE *rp)
68 rp->vers = TALK_VERSION;
70 rp->id_num = htonl(0);
71 if (mp->vers != TALK_VERSION) {
72 syslog(LOG_WARNING, "bad protocol version %d", mp->vers);
73 rp->answer = BADVERSION;
76 mp->id_num = ntohl(mp->id_num);
77 mp->addr.sa_family = ntohs(mp->addr.sa_family);
78 if (mp->addr.sa_family != AF_INET) {
79 syslog(LOG_WARNING, "bad address, family %d",
84 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
85 if (mp->ctl_addr.sa_family != AF_INET) {
86 syslog(LOG_WARNING, "bad control address, family %d",
87 mp->ctl_addr.sa_family);
88 rp->answer = BADCTLADDR;
91 for (s = mp->l_name; *s; s++)
93 syslog(LOG_NOTICE, "illegal user name. Aborting");
97 mp->pid = ntohl(mp->pid);
99 print_request("process_request", mp);
107 ptr = find_request(mp);
108 if (ptr != (CTL_MSG *)0) {
109 rp->id_num = htonl(ptr->id_num);
110 rp->answer = SUCCESS;
112 insert_table(mp, rp);
116 ptr = find_match(mp);
117 if (ptr != (CTL_MSG *)0) {
118 rp->id_num = htonl(ptr->id_num);
119 rp->addr = ptr->addr;
120 rp->addr.sa_family = htons(ptr->addr.sa_family);
121 rp->answer = SUCCESS;
123 rp->answer = NOT_HERE;
127 rp->answer = delete_invite(mp->id_num);
131 rp->answer = UNKNOWN_REQUEST;
135 print_response("process_request", rp);
139 do_announce(CTL_MSG *mp, CTL_RESPONSE *rp)
145 /* see if the user is logged */
146 result = find_user(mp->r_name, mp->r_tty);
147 if (result != SUCCESS) {
151 #define satosin(sa) ((struct sockaddr_in *)(void *)(sa))
152 hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr,
153 sizeof (struct in_addr), AF_INET);
154 if (hp == (struct hostent *)0) {
155 rp->answer = MACHINE_UNKNOWN;
158 ptr = find_request(mp);
159 if (ptr == (CTL_MSG *) 0) {
160 insert_table(mp, rp);
161 rp->answer = announce(mp, hp->h_name);
164 if (mp->id_num > ptr->id_num) {
166 * This is an explicit re-announce, so update the id_num
167 * field to avoid duplicates and re-announce the talk.
169 ptr->id_num = new_id();
170 rp->id_num = htonl(ptr->id_num);
171 rp->answer = announce(mp, hp->h_name);
173 /* a duplicated request, so ignore it */
174 rp->id_num = htonl(ptr->id_num);
175 rp->answer = SUCCESS;
180 * Search utmp for the local user
183 find_user(const char *name, char *tty)
189 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(ut->ut_line)];
193 (void) strcpy(ftty, _PATH_DEV);
194 while ((ut = getutxent()) != NULL)
195 if (ut->ut_type == USER_PROCESS &&
196 strcmp(ut->ut_user, name) == 0) {
197 if (*tty == '\0' || best != 0) {
199 status = PERMISSION_DENIED;
200 /* no particular tty was requested */
201 (void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
203 if (stat(ftty, &statb) == 0) {
204 if (!(statb.st_mode & 020))
206 if (statb.st_atime > best) {
207 best = statb.st_atime;
208 (void) strcpy(tty, ut->ut_line);
214 if (strcmp(ut->ut_line, tty) == 0) {