Home | History | Annotate | Line # | Download | only in talkd
process.c revision 1.6
      1 /*	$NetBSD: process.c,v 1.6 1998/07/04 19:31:05 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1983, 1993
      5  *	The Regents of the University of California.  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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)process.c	8.2 (Berkeley) 11/16/93";
     40 #else
     41 __RCSID("$NetBSD: process.c,v 1.6 1998/07/04 19:31:05 mrg Exp $");
     42 #endif
     43 #endif /* not lint */
     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 
     54 #include <sys/param.h>
     55 #include <sys/stat.h>
     56 #include <sys/socket.h>
     57 
     58 #include <netinet/in.h>
     59 
     60 #include <protocols/talkd.h>
     61 
     62 #include <netdb.h>
     63 #include <syslog.h>
     64 #include <stdio.h>
     65 #include <string.h>
     66 #include <paths.h>
     67 #include <utmp.h>
     68 
     69 #include "extern.h"
     70 
     71 void
     72 process_request(mp, rp)
     73 	CTL_MSG *mp;
     74 	CTL_RESPONSE *rp;
     75 {
     76 	CTL_MSG *ptr;
     77 
     78 	rp->vers = TALK_VERSION;
     79 	rp->type = mp->type;
     80 	rp->id_num = htonl(0);
     81 	if (mp->vers != TALK_VERSION) {
     82 		syslog(LOG_WARNING, "Bad protocol version %d", mp->vers);
     83 		rp->answer = BADVERSION;
     84 		return;
     85 	}
     86 	mp->id_num = ntohl(mp->id_num);
     87 	mp->addr.sa_family = ntohs(mp->addr.sa_family);
     88 	if (mp->addr.sa_family != AF_INET) {
     89 		syslog(LOG_WARNING, "Bad address, family %d",
     90 		    mp->addr.sa_family);
     91 		rp->answer = BADADDR;
     92 		return;
     93 	}
     94 	mp->ctl_addr.sa_family = ntohs(mp->ctl_addr.sa_family);
     95 	if (mp->ctl_addr.sa_family != AF_INET) {
     96 		syslog(LOG_WARNING, "Bad control address, family %d",
     97 		    mp->ctl_addr.sa_family);
     98 		rp->answer = BADCTLADDR;
     99 		return;
    100 	}
    101 	mp->pid = ntohl(mp->pid);
    102 	if (debug || logging)
    103 		print_request("request", mp);
    104 	switch (mp->type) {
    105 
    106 	case ANNOUNCE:
    107 		do_announce(mp, rp);
    108 		break;
    109 
    110 	case LEAVE_INVITE:
    111 		ptr = find_request(mp);
    112 		if (ptr != (CTL_MSG *)0) {
    113 			rp->id_num = htonl(ptr->id_num);
    114 			rp->answer = SUCCESS;
    115 		} else
    116 			insert_table(mp, rp);
    117 		break;
    118 
    119 	case LOOK_UP:
    120 		ptr = find_match(mp);
    121 		if (ptr != (CTL_MSG *)0) {
    122 			rp->id_num = htonl(ptr->id_num);
    123 			rp->addr = ptr->addr;
    124 			rp->addr.sa_family = htons(ptr->addr.sa_family);
    125 			rp->answer = SUCCESS;
    126 		} else
    127 			rp->answer = NOT_HERE;
    128 		break;
    129 
    130 	case DELETE:
    131 		rp->answer = delete_invite(mp->id_num);
    132 		break;
    133 
    134 	default:
    135 		rp->answer = UNKNOWN_REQUEST;
    136 		break;
    137 	}
    138 	if (debug)
    139 		print_response("process_request done", rp);
    140 }
    141 
    142 void
    143 do_announce(mp, rp)
    144 	CTL_MSG *mp;
    145 	CTL_RESPONSE *rp;
    146 {
    147 	struct hostent *hp;
    148 	CTL_MSG *ptr;
    149 	int result;
    150 
    151 	/* see if the user is logged */
    152 	result = find_user(mp->r_name, mp->r_tty);
    153 	if (result != SUCCESS) {
    154 		rp->answer = result;
    155 		return;
    156 	}
    157 #define	satosin(sa)	((struct sockaddr_in *)(sa))
    158 	hp = gethostbyaddr((char *)&satosin(&mp->ctl_addr)->sin_addr,
    159 		sizeof (struct in_addr), AF_INET);
    160 	if (hp == (struct hostent *)0) {
    161 		rp->answer = MACHINE_UNKNOWN;
    162 		return;
    163 	}
    164 	ptr = find_request(mp);
    165 	if (ptr == (CTL_MSG *) 0) {
    166 		insert_table(mp, rp);
    167 		rp->answer = announce(mp, hp->h_name);
    168 		return;
    169 	}
    170 	if (mp->id_num > ptr->id_num) {
    171 		/*
    172 		 * This is an explicit re-announce, so update the id_num
    173 		 * field to avoid duplicates and re-announce the talk.
    174 		 */
    175 		ptr->id_num = new_id();
    176 		rp->id_num = htonl(ptr->id_num);
    177 		rp->answer = announce(mp, hp->h_name);
    178 	} else {
    179 		/* a duplicated request, so ignore it */
    180 		rp->id_num = htonl(ptr->id_num);
    181 		rp->answer = SUCCESS;
    182 	}
    183 }
    184 
    185 /*
    186  * Search utmp for the local user
    187  */
    188 int
    189 find_user(name, tty)
    190 	char *name, *tty;
    191 {
    192 	struct utmp ubuf;
    193 	int status;
    194 	FILE *fd;
    195 	struct stat statb;
    196 	char line[sizeof(ubuf.ut_line) + 1];
    197 	char ftty[sizeof(_PATH_DEV) - 1 + sizeof(line)];
    198 	time_t atime = 0;
    199 	int anytty = 0;
    200 
    201 	if ((fd = fopen(_PATH_UTMP, "r")) == NULL) {
    202 		fprintf(stderr, "talkd: can't read %s.\n", _PATH_UTMP);
    203 		return (FAILED);
    204 	}
    205 #define SCMPN(a, b)	strncmp(a, b, sizeof (a))
    206 	status = NOT_HERE;
    207 	(void) strcpy(ftty, _PATH_DEV);
    208 
    209 	if (*tty == '\0')
    210 		anytty = 1;
    211 
    212 	while (fread((char *) &ubuf, sizeof ubuf, 1, fd) == 1) {
    213 		if (SCMPN(ubuf.ut_name, name) != 0)
    214 			continue;
    215 		(void)strncpy(line, ubuf.ut_line, sizeof(ubuf.ut_line));
    216 		line[sizeof(ubuf.ut_line)] = '\0';
    217 		if (anytty) {
    218 			/* no particular tty was requested */
    219 			/* XXX strcpy is safe */
    220 			(void)strcpy(ftty + sizeof(_PATH_DEV) - 1, line);
    221 			if (stat(ftty, &statb) == 0) {
    222 				if (!(statb.st_mode & S_IWGRP)) {
    223 					if (status != SUCCESS)
    224 						status = PERMISSION_DENIED;
    225 					continue;
    226 				}
    227 				if (statb.st_atime > atime) {
    228 					atime = statb.st_atime;
    229 					(void)strcpy(tty, line);
    230 					status = SUCCESS;
    231 				}
    232 			}
    233 		} else if (strcmp(line, tty) == 0) {
    234 			status = SUCCESS;
    235 			break;
    236 		}
    237 	}
    238 	(void)fclose(fd);
    239 	return (status);
    240 }
    241