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$"; 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 #include "extern.h" 64 65 extern int debug; 66 67 void 68 process_request(CTL_MSG *mp, CTL_RESPONSE *rp) 69 { 70 CTL_MSG *ptr; 71 char *s; 72 73 rp->vers = TALK_VERSION; 74 rp->type = mp->type; 75 rp->id_num = htonl(0); 76 if (mp->vers != TALK_VERSION) { 77 syslog(LOG_WARNING, "bad protocol version %d", mp->vers); 78 rp->answer = BADVERSION; 79 return; 80 } 81 mp->id_num = ntohl(mp->id_num); 82 mp->addr.sa_family = ntohs(mp->addr.sa_family); 83 if (mp->addr.sa_family != AF_INET) { 84 syslog(LOG_WARNING, "bad address, family %d", 85 mp->addr.sa_family); 86 rp->answer = BADADDR; 87 return; 88 } 89 mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family); 90 if (mp->ctl_addr.sa_family != AF_INET) { 91 syslog(LOG_WARNING, "bad control address, family %d", 92 mp->ctl_addr.sa_family); 93 rp->answer = BADCTLADDR; 94 return; 95 } 96 for (s = mp->l_name; *s; s++) 97 if (!isprint(*s)) { 98 syslog(LOG_NOTICE, "illegal user name. Aborting"); 99 rp->answer = FAILED; 100 return; 101 } 102 mp->pid = ntohl(mp->pid); 103 if (debug) 104 print_request("process_request", mp); 105 switch (mp->type) { 106 107 case ANNOUNCE: 108 do_announce(mp, rp); 109 break; 110 111 case LEAVE_INVITE: 112 ptr = find_request(mp); 113 if (ptr != (CTL_MSG *)0) { 114 rp->id_num = htonl(ptr->id_num); 115 rp->answer = SUCCESS; 116 } else 117 insert_table(mp, rp); 118 break; 119 120 case LOOK_UP: 121 ptr = find_match(mp); 122 if (ptr != (CTL_MSG *)0) { 123 rp->id_num = htonl(ptr->id_num); 124 rp->addr = ptr->addr; 125 rp->addr.sa_family = htons(ptr->addr.sa_family); 126 rp->answer = SUCCESS; 127 } else 128 rp->answer = NOT_HERE; 129 break; 130 131 case DELETE: 132 rp->answer = delete_invite(mp->id_num); 133 break; 134 135 default: 136 rp->answer = UNKNOWN_REQUEST; 137 break; 138 } 139 if (debug) 140 print_response("process_request", rp); 141 } 142 143 void 144 do_announce(CTL_MSG *mp, CTL_RESPONSE *rp) 145 { 146 struct hostent *hp; 147 CTL_MSG *ptr; 148 int result; 149 150 /* see if the user is logged */ 151 result = find_user(mp->r_name, mp->r_tty); 152 if (result != SUCCESS) { 153 rp->answer = result; 154 return; 155 } 156 #define satosin(sa) ((struct sockaddr_in *)(sa)) 157 hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr, 158 sizeof (struct in_addr), AF_INET); 159 if (hp == (struct hostent *)0) { 160 rp->answer = MACHINE_UNKNOWN; 161 return; 162 } 163 ptr = find_request(mp); 164 if (ptr == (CTL_MSG *) 0) { 165 insert_table(mp, rp); 166 rp->answer = announce(mp, hp->h_name); 167 return; 168 } 169 if (mp->id_num > ptr->id_num) { 170 /* 171 * This is an explicit re-announce, so update the id_num 172 * field to avoid duplicates and re-announce the talk. 173 */ 174 ptr->id_num = new_id(); 175 rp->id_num = htonl(ptr->id_num); 176 rp->answer = announce(mp, hp->h_name); 177 } else { 178 /* a duplicated request, so ignore it */ 179 rp->id_num = htonl(ptr->id_num); 180 rp->answer = SUCCESS; 181 } 182 } 183 184 #include <utmp.h> 185 186 /* 187 * Search utmp for the local user 188 */ 189 int 190 find_user(const char *name, char *tty) 191 { 192 struct utmp ubuf; 193 int status; 194 FILE *fd; 195 struct stat statb; 196 time_t best = 0; 197 char line[sizeof(ubuf.ut_line) + 1]; 198 char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)]; 199 200 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) { 201 warnx("can't read %s", _PATH_UTMP); 202 return (FAILED); 203 } 204 #define SCMPN(a, b) strncmp(a, b, sizeof (a)) 205 status = NOT_HERE; 206 (void) strcpy(ftty, _PATH_DEV); 207 while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) 208 if (SCMPN(ubuf.ut_name, name) == 0) { 209 strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line)); 210 line[sizeof(ubuf.ut_line)] = '\0'; 211 if (*tty == '\0' || best != 0) { 212 if (best == 0) 213 status = PERMISSION_DENIED; 214 /* no particular tty was requested */ 215 (void) strcpy(ftty + sizeof(_PATH_DEV) - 1, 216 line); 217 if (stat(ftty, &statb) == 0) { 218 if (!(statb.st_mode & 020)) 219 continue; 220 if (statb.st_atime > best) { 221 best = statb.st_atime; 222 (void) strcpy(tty, line); 223 status = SUCCESS; 224 continue; 225 } 226 } 227 } 228 if (strcmp(line, tty) == 0) { 229 status = SUCCESS; 230 break; 231 } 232 } 233 fclose(fd); 234 return (status); 235 } 236