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