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