1 /* $OpenBSD: process.c,v 1.16 2003/06/11 14:24:46 deraadt Exp $ */ 2 3 /* 4 * Copyright (c) 1983 Regents of the University of California. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #ifndef lint 33 /*static char sccsid[] = "from: @(#)process.c 5.10 (Berkeley) 2/26/91";*/ 34 static char rcsid[] = "$Id: process.c,v 1.16 2003/06/11 14:24:46 deraadt Exp $"; 35 #endif /* not lint */ 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 <arpa/inet.h> 50 #include <protocols/talkd.h> 51 #include <netdb.h> 52 #include <syslog.h> 53 #include <stdio.h> 54 #include <string.h> 55 #include <ctype.h> 56 #include <paths.h> 57 #include "talkd.h" 58 59 #define satosin(sa) ((struct sockaddr_in *)(sa)) 60 61 void 62 process_request(CTL_MSG *mp, CTL_RESPONSE *rp) 63 { 64 CTL_MSG *ptr; 65 char *s; 66 67 rp->vers = TALK_VERSION; 68 rp->type = mp->type; 69 rp->id_num = htonl(0); 70 if (mp->vers != TALK_VERSION) { 71 syslog(LOG_WARNING, "Bad protocol version %d", mp->vers); 72 rp->answer = BADVERSION; 73 return; 74 } 75 mp->id_num = ntohl(mp->id_num); 76 if (ntohs(mp->addr.sa_family) != AF_INET) { 77 syslog(LOG_WARNING, "Bad address, family %d", 78 ntohs(mp->addr.sa_family)); 79 rp->answer = BADADDR; 80 return; 81 } 82 if (ntohs(mp->ctl_addr.sa_family) != AF_INET) { 83 syslog(LOG_WARNING, "Bad control address, family %d", 84 ntohs(mp->ctl_addr.sa_family)); 85 rp->answer = BADCTLADDR; 86 return; 87 } 88 for (s = mp->l_name; *s; s++) 89 if (!isprint(*s)) { 90 syslog(LOG_NOTICE, "Illegal user name. Aborting"); 91 rp->answer = FAILED; 92 return; 93 } 94 if (memcmp(&satosin(&rp->addr)->sin_addr, 95 &satosin(&mp->ctl_addr)->sin_addr, 96 sizeof(struct in_addr))) { 97 char buf1[32], buf2[32]; 98 99 strlcpy(buf1, inet_ntoa(satosin(&rp->addr)->sin_addr), 100 sizeof(buf1)); 101 strlcpy(buf2, inet_ntoa(satosin(&mp->ctl_addr)->sin_addr), 102 sizeof(buf2)); 103 syslog(LOG_WARNING, "addresses are different, %s != %s", 104 buf1, buf2); 105 } 106 rp->addr.sa_family = 0; 107 mp->pid = ntohl(mp->pid); 108 if (debug) 109 print_request("process_request", mp); 110 switch (mp->type) { 111 112 case ANNOUNCE: 113 do_announce(mp, rp); 114 break; 115 116 case LEAVE_INVITE: 117 ptr = find_request(mp); 118 if (ptr != (CTL_MSG *)0) { 119 rp->id_num = htonl(ptr->id_num); 120 rp->answer = SUCCESS; 121 } else 122 insert_table(mp, rp); 123 break; 124 125 case LOOK_UP: 126 ptr = find_match(mp); 127 if (ptr != (CTL_MSG *)0) { 128 rp->id_num = htonl(ptr->id_num); 129 rp->addr = ptr->addr; 130 rp->addr.sa_family = ptr->addr.sa_family; 131 rp->answer = SUCCESS; 132 } else 133 rp->answer = NOT_HERE; 134 break; 135 136 case DELETE: 137 rp->answer = delete_invite(mp->id_num); 138 break; 139 140 default: 141 rp->answer = UNKNOWN_REQUEST; 142 break; 143 } 144 if (debug) 145 print_response("process_request", rp); 146 } 147 148 void 149 do_announce(CTL_MSG *mp, CTL_RESPONSE *rp) 150 { 151 struct hostent *hp; 152 CTL_MSG *ptr; 153 int result; 154 155 /* see if the user is logged */ 156 result = find_user(mp->r_name, mp->r_tty, sizeof(mp->r_tty)); 157 if (result != SUCCESS) { 158 rp->answer = result; 159 return; 160 } 161 hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr, 162 sizeof(struct in_addr), AF_INET); 163 if (hp == (struct hostent *)0) { 164 rp->answer = MACHINE_UNKNOWN; 165 return; 166 } 167 ptr = find_request(mp); 168 if (ptr == (CTL_MSG *) 0) { 169 insert_table(mp, rp); 170 rp->answer = announce(mp, hp->h_name); 171 return; 172 } 173 if (mp->id_num > ptr->id_num) { 174 /* 175 * This is an explicit re-announce, so update the id_num 176 * field to avoid duplicates and re-announce the talk. 177 */ 178 ptr->id_num = new_id(); 179 rp->id_num = htonl(ptr->id_num); 180 rp->answer = announce(mp, hp->h_name); 181 } else { 182 /* a duplicated request, so ignore it */ 183 rp->id_num = htonl(ptr->id_num); 184 rp->answer = SUCCESS; 185 } 186 } 187 188 #include <utmp.h> 189 190 /* 191 * Search utmp for the local user 192 */ 193 int 194 find_user(char *name, char *tty, size_t ttyl) 195 { 196 struct utmp ubuf, ubuf1; 197 int status; 198 FILE *fd; 199 char line[UT_LINESIZE+1]; 200 char ftty[MAXPATHLEN]; 201 time_t idle, now; 202 203 time(&now); 204 idle = INT_MAX; 205 if ((fd = fopen(_PATH_UTMP, "r")) == NULL) { 206 fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP); 207 return (FAILED); 208 } 209 #define SCMPN(a, b) strncmp(a, b, sizeof(a)) 210 status = NOT_HERE; 211 (void) strlcpy(ftty, _PATH_DEV, sizeof(ftty)); 212 while (fread((char *) &ubuf, sizeof(ubuf), 1, fd) == 1) 213 if (SCMPN(ubuf.ut_name, name) == 0) { 214 if (*tty == '\0') { 215 /* no particular tty was requested */ 216 struct stat statb; 217 218 memcpy(line, ubuf.ut_line, UT_LINESIZE); 219 line[sizeof(line)-1] = '\0'; 220 ftty[sizeof(_PATH_DEV)-1] = '\0'; 221 strlcat(ftty, line, sizeof(ftty)); 222 if (stat(ftty, &statb) == 0) { 223 if (!(statb.st_mode & S_IWGRP)) { 224 if (status == NOT_HERE) 225 status = PERMISSION_DENIED; 226 } else if (now - statb.st_atime < idle) { 227 idle = now - statb.st_atime; 228 status = SUCCESS; 229 ubuf1 = ubuf; 230 } 231 } 232 } else if (SCMPN(ubuf.ut_line, tty) == 0) { 233 status = SUCCESS; 234 break; 235 } 236 } 237 fclose(fd); 238 if (*tty == '\0' && status == SUCCESS) { 239 memcpy(line, ubuf1.ut_line, UT_LINESIZE); 240 line[sizeof(line)-1] = '\0'; 241 strlcpy(tty, line, ttyl); 242 } 243 return (status); 244 } 245