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 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)process.c 8.2 (Berkeley) 11/16/93"; 37 #endif 38 static const char rcsid[] = 39 "$FreeBSD: src/libexec/talkd/process.c,v 1.9 1999/08/28 00:10:16 peter Exp $"; 40 #endif /* not lint */ 41 42 /* 43 * process.c handles the requests, which can be of three types: 44 * ANNOUNCE - announce to a user that a talk is wanted 45 * LEAVE_INVITE - insert the request into the table 46 * LOOK_UP - look up to see if a request is waiting in 47 * in the table for the local user 48 * DELETE - delete invitation 49 */ 50 #include <sys/param.h> 51 #include <sys/stat.h> 52 #include <sys/socket.h> 53 #include <netinet/in.h> 54 #include <protocols/talkd.h> 55 #include <ctype.h> 56 #include <err.h> 57 #include <netdb.h> 58 #include <paths.h> 59 #include <stdio.h> 60 #include <string.h> 61 #include <syslog.h> 62 63 int announce __P((CTL_MSG *, char *)); 64 int delete_invite __P((int)); 65 void do_announce __P((CTL_MSG *, CTL_RESPONSE *)); 66 CTL_MSG *find_request(); 67 CTL_MSG *find_match(); 68 int find_user __P((char *, char *)); 69 void insert_table __P((CTL_MSG *, CTL_RESPONSE *)); 70 int new_id __P((void)); 71 void print_request __P((char *, CTL_MSG *)); 72 void print_response __P((char *, CTL_RESPONSE *)); 73 74 void 75 process_request(mp, rp) 76 register CTL_MSG *mp; 77 register CTL_RESPONSE *rp; 78 { 79 register CTL_MSG *ptr; 80 extern int debug; 81 char *s; 82 83 rp->vers = TALK_VERSION; 84 rp->type = mp->type; 85 rp->id_num = htonl(0); 86 if (mp->vers != TALK_VERSION) { 87 syslog(LOG_WARNING, "bad protocol version %d", mp->vers); 88 rp->answer = BADVERSION; 89 return; 90 } 91 mp->id_num = ntohl(mp->id_num); 92 mp->addr.sa_family = ntohs(mp->addr.sa_family); 93 if (mp->addr.sa_family != AF_INET) { 94 syslog(LOG_WARNING, "bad address, family %d", 95 mp->addr.sa_family); 96 rp->answer = BADADDR; 97 return; 98 } 99 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family); 100 if (mp->ctl_addr.sa_family != AF_INET) { 101 syslog(LOG_WARNING, "bad control address, family %d", 102 mp->ctl_addr.sa_family); 103 rp->answer = BADCTLADDR; 104 return; 105 } 106 for (s = mp->l_name; *s; s++) 107 if (!isprint(*s)) { 108 syslog(LOG_NOTICE, "illegal user name. Aborting"); 109 rp->answer = FAILED; 110 return; 111 } 112 mp->pid = ntohl(mp->pid); 113 if (debug) 114 print_request("process_request", mp); 115 switch (mp->type) { 116 117 case ANNOUNCE: 118 do_announce(mp, rp); 119 break; 120 121 case LEAVE_INVITE: 122 ptr = find_request(mp); 123 if (ptr != (CTL_MSG *)0) { 124 rp->id_num = htonl(ptr->id_num); 125 rp->answer = SUCCESS; 126 } else 127 insert_table(mp, rp); 128 break; 129 130 case LOOK_UP: 131 ptr = find_match(mp); 132 if (ptr != (CTL_MSG *)0) { 133 rp->id_num = htonl(ptr->id_num); 134 rp->addr = ptr->addr; 135 rp->addr.sa_family = htons(ptr->addr.sa_family); 136 rp->answer = SUCCESS; 137 } else 138 rp->answer = NOT_HERE; 139 break; 140 141 case DELETE: 142 rp->answer = delete_invite(mp->id_num); 143 break; 144 145 default: 146 rp->answer = UNKNOWN_REQUEST; 147 break; 148 } 149 if (debug) 150 print_response("process_request", rp); 151 } 152 153 void 154 do_announce(mp, rp) 155 register CTL_MSG *mp; 156 CTL_RESPONSE *rp; 157 { 158 struct hostent *hp; 159 CTL_MSG *ptr; 160 int result; 161 162 /* see if the user is logged */ 163 result = find_user(mp->r_name, mp->r_tty); 164 if (result != SUCCESS) { 165 rp->answer = result; 166 return; 167 } 168 #define satosin(sa) ((struct sockaddr_in *)(sa)) 169 hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr, 170 sizeof (struct in_addr), AF_INET); 171 if (hp == (struct hostent *)0) { 172 rp->answer = MACHINE_UNKNOWN; 173 return; 174 } 175 ptr = find_request(mp); 176 if (ptr == (CTL_MSG *) 0) { 177 insert_table(mp, rp); 178 rp->answer = announce(mp, hp->h_name); 179 return; 180 } 181 if (mp->id_num > ptr->id_num) { 182 /* 183 * This is an explicit re-announce, so update the id_num 184 * field to avoid duplicates and re-announce the talk. 185 */ 186 ptr->id_num = new_id(); 187 rp->id_num = htonl(ptr->id_num); 188 rp->answer = announce(mp, hp->h_name); 189 } else { 190 /* a duplicated request, so ignore it */ 191 rp->id_num = htonl(ptr->id_num); 192 rp->answer = SUCCESS; 193 } 194 } 195 196 #include <utmp.h> 197 198 /* 199 * Search utmp for the local user 200 */ 201 int 202 find_user(name, tty) 203 char *name, *tty; 204 { 205 struct utmp ubuf; 206 int status; 207 FILE *fd; 208 struct stat statb; 209 time_t best = 0; 210 char line[sizeof(ubuf.ut_line) + 1]; 211 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)]; 212 213 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) { 214 warnx("can't read %s", _PATH_UTMP); 215 return (FAILED); 216 } 217 #define SCMPN(a, b) strncmp(a, b, sizeof (a)) 218 status = NOT_HERE; 219 (void) strcpy(ftty, _PATH_DEV); 220 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) 221 if (SCMPN(ubuf.ut_name, name) == 0) { 222 strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line)); 223 line[sizeof(ubuf.ut_line)] = '\0'; 224 if (*tty == '\0' || best != 0) { 225 if (best == 0) 226 status = PERMISSION_DENIED; 227 /* no particular tty was requested */ 228 (void) strcpy(ftty + sizeof(_PATH_DEV) - 1, 229 line); 230 if (stat(ftty, &statb) == 0) { 231 if (!(statb.st_mode & 020)) 232 continue; 233 if (statb.st_atime > best) { 234 best = statb.st_atime; 235 (void) strcpy(tty, line); 236 status = SUCCESS; 237 continue; 238 } 239 } 240 } 241 if (strcmp(line, tty) == 0) { 242 status = SUCCESS; 243 break; 244 } 245 } 246 fclose(fd); 247 return (status); 248 } 249