70ec159c68be579191b7679029fe3ba98213b2eb
[dragonfly.git] / libexec / talkd / process.c
1 /*
2  * Copyright (c) 1983, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * @(#)process.c        8.2 (Berkeley) 11/16/93
34  * $FreeBSD: src/libexec/talkd/process.c,v 1.9 1999/08/28 00:10:16 peter Exp $
35  */
36
37 /*
38  * process.c handles the requests, which can be of three types:
39  *      ANNOUNCE - announce to a user that a talk is wanted
40  *      LEAVE_INVITE - insert the request into the table
41  *      LOOK_UP - look up to see if a request is waiting in
42  *                in the table for the local user
43  *      DELETE - delete invitation
44  */
45 #include <sys/param.h>
46 #include <sys/stat.h>
47 #include <sys/socket.h>
48 #include <netinet/in.h>
49 #include <protocols/talkd.h>
50 #include <ctype.h>
51 #include <err.h>
52 #include <netdb.h>
53 #include <paths.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <syslog.h>
57 #include "utmpentry.h"
58
59 #include "extern.h"
60
61 extern int debug;
62
63 void
64 process_request(CTL_MSG *mp, CTL_RESPONSE *rp)
65 {
66         CTL_MSG *ptr;
67         char *s;
68
69         rp->vers = TALK_VERSION;
70         rp->type = mp->type;
71         rp->id_num = htonl(0);
72         if (mp->vers != TALK_VERSION) {
73                 syslog(LOG_WARNING, "bad protocol version %d", mp->vers);
74                 rp->answer = BADVERSION;
75                 return;
76         }
77         mp->id_num = ntohl(mp->id_num);
78         mp->addr.sa_family = ntohs(mp->addr.sa_family);
79         if (mp->addr.sa_family != AF_INET) {
80                 syslog(LOG_WARNING, "bad address, family %d",
81                     mp->addr.sa_family);
82                 rp->answer = BADADDR;
83                 return;
84         }
85         mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
86         if (mp->ctl_addr.sa_family != AF_INET) {
87                 syslog(LOG_WARNING, "bad control address, family %d",
88                     mp->ctl_addr.sa_family);
89                 rp->answer = BADCTLADDR;
90                 return;
91         }
92         for (s = mp->l_name; *s; s++)
93                 if (!isprint(*s)) {
94                         syslog(LOG_NOTICE, "illegal user name. Aborting");
95                         rp->answer = FAILED;
96                         return;
97                 }
98         mp->pid = ntohl(mp->pid);
99         if (debug)
100                 print_request("process_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 != NULL) {
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 != NULL) {
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", rp);
137 }
138
139 void
140 do_announce(CTL_MSG *mp, CTL_RESPONSE *rp)
141 {
142         struct hostent *hp;
143         CTL_MSG *ptr;
144         int result;
145
146         /* see if the user is logged */
147         result = find_user(mp->r_name, mp->r_tty);
148         if (result != SUCCESS) {
149                 rp->answer = result;
150                 return;
151         }
152 #define satosin(sa)     ((struct sockaddr_in *)(void *)(sa))
153         hp = gethostbyaddr(&satosin(&mp->ctl_addr)->sin_addr,
154                 sizeof (struct in_addr), AF_INET);
155         if (hp == NULL) {
156                 rp->answer = MACHINE_UNKNOWN;
157                 return;
158         }
159         ptr = find_request(mp);
160         if (ptr == NULL) {
161                 insert_table(mp, rp);
162                 rp->answer = announce(mp, hp->h_name);
163                 return;
164         }
165         if (mp->id_num > ptr->id_num) {
166                 /*
167                  * This is an explicit re-announce, so update the id_num
168                  * field to avoid duplicates and re-announce the talk.
169                  */
170                 ptr->id_num = new_id();
171                 rp->id_num = htonl(ptr->id_num);
172                 rp->answer = announce(mp, hp->h_name);
173         } else {
174                 /* a duplicated request, so ignore it */
175                 rp->id_num = htonl(ptr->id_num);
176                 rp->answer = SUCCESS;
177         }
178 }
179
180 /*
181  * Search utmp for the local user
182  */
183 int
184 find_user(const char *name, char *tty)
185 {
186         struct utmpentry *ep;
187         int status;
188         struct stat statb;
189         time_t best = 0;
190         char ftty[sizeof(_PATH_DEV) + sizeof(ep->line)];
191
192         getutentries(NULL, &ep);
193
194 #define SCMPN(a, b)     strncmp(a, b, sizeof (a))
195         status = NOT_HERE;
196         (void) strcpy(ftty, _PATH_DEV);
197         for (; ep; ep = ep->next)
198                 if (SCMPN(ep->name, name) == 0) {
199                         if (*tty == '\0' || best != 0) {
200                                 if (best == 0)
201                                         status = PERMISSION_DENIED;
202                                 /* no particular tty was requested */
203                                 (void) strcpy(ftty + sizeof(_PATH_DEV) - 1,
204                                     ep->line);
205                                 if (stat(ftty, &statb) == 0) {
206                                         if (!(statb.st_mode & 020))
207                                                 continue;
208                                         if (statb.st_atime > best) {
209                                                 best = statb.st_atime;
210                                                 (void) strcpy(tty, ep->line);
211                                                 status = SUCCESS;
212                                                 continue;
213                                         }
214                                 }
215                         }
216                         if (strcmp(ep->line, tty) == 0) {
217                                 status = SUCCESS;
218                                 break;
219                         }
220                 }
221
222         return (status);
223 }