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