17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*a856bf05Sblu  * Common Development and Distribution License (the "License").
6*a856bf05Sblu  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  *
21*a856bf05Sblu  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
227c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate /*
267c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
277c478bd9Sstevel@tonic-gate  * All Rights Reserved.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California.
337c478bd9Sstevel@tonic-gate  * All Rights Reserved.
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*
417c478bd9Sstevel@tonic-gate  * process.c handles the requests, which can be of three types:
427c478bd9Sstevel@tonic-gate  *
437c478bd9Sstevel@tonic-gate  * ANNOUNCE - announce to a user that a talk is wanted
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * LEAVE_INVITE - insert the request into the table
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * LOOK_UP - look up to see if a request is waiting in
487c478bd9Sstevel@tonic-gate  * in the table for the local user
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  * DELETE - delete invitation
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate #include <sys/types.h>
557c478bd9Sstevel@tonic-gate #include <sys/stat.h>
567c478bd9Sstevel@tonic-gate #include <fcntl.h>
577c478bd9Sstevel@tonic-gate #include <syslog.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <utmpx.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <stdlib.h>
627c478bd9Sstevel@tonic-gate #include <stdio.h>
637c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void do_announce(CTL_MSG *request, CTL_RESPONSE *response);
667c478bd9Sstevel@tonic-gate static int find_user(char *name, char *tty);
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate void
process_request(CTL_MSG * request,CTL_RESPONSE * response)697c478bd9Sstevel@tonic-gate process_request(CTL_MSG *request, CTL_RESPONSE *response)
707c478bd9Sstevel@tonic-gate {
717c478bd9Sstevel@tonic-gate 	CTL_MSG *ptr;
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate 	response->type = request->type;
747c478bd9Sstevel@tonic-gate 	response->id_num = 0;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/*
777c478bd9Sstevel@tonic-gate 	 * Check if any of the strings within the request structure aren't
787c478bd9Sstevel@tonic-gate 	 * NUL terminated, and if so don't bother processing the request
797c478bd9Sstevel@tonic-gate 	 * further.
807c478bd9Sstevel@tonic-gate 	 */
817c478bd9Sstevel@tonic-gate 	if ((memchr(request->l_name, '\0', sizeof (request->l_name)) == NULL) ||
827c478bd9Sstevel@tonic-gate 	    (memchr(request->r_name, '\0', sizeof (request->r_name)) == NULL) ||
837c478bd9Sstevel@tonic-gate 	    (memchr(request->r_tty, '\0', sizeof (request->r_tty)) == NULL)) {
847c478bd9Sstevel@tonic-gate 		response->answer = FAILED;
857c478bd9Sstevel@tonic-gate 		openlog("talk", 0, LOG_AUTH);
867c478bd9Sstevel@tonic-gate 		syslog(LOG_CRIT, "malformed talk request\n");
877c478bd9Sstevel@tonic-gate 		closelog();
887c478bd9Sstevel@tonic-gate 		return;
897c478bd9Sstevel@tonic-gate 	}
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	switch (request->type) {
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate 	    case ANNOUNCE :
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate 		do_announce(request, response);
967c478bd9Sstevel@tonic-gate 		break;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	    case LEAVE_INVITE :
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 		ptr = find_request(request);
1017c478bd9Sstevel@tonic-gate 		if (ptr != NULL) {
1027c478bd9Sstevel@tonic-gate 			response->id_num = ptr->id_num;
1037c478bd9Sstevel@tonic-gate 			response->answer = SUCCESS;
1047c478bd9Sstevel@tonic-gate 		} else {
1057c478bd9Sstevel@tonic-gate 			insert_table(request, response);
1067c478bd9Sstevel@tonic-gate 		}
1077c478bd9Sstevel@tonic-gate 		break;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	    case LOOK_UP :
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate 		ptr = find_match(request);
1127c478bd9Sstevel@tonic-gate 		if (ptr != NULL) {
1137c478bd9Sstevel@tonic-gate 			response->id_num = ptr->id_num;
1147c478bd9Sstevel@tonic-gate 			response->addr = ptr->addr;
1157c478bd9Sstevel@tonic-gate 			response->answer = SUCCESS;
1167c478bd9Sstevel@tonic-gate 		} else {
1177c478bd9Sstevel@tonic-gate 			response->answer = NOT_HERE;
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 		break;
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	    case DELETE :
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 		response->answer = delete_invite(request->id_num);
1247c478bd9Sstevel@tonic-gate 		break;
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	    default :
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 		response->answer = UNKNOWN_REQUEST;
1297c478bd9Sstevel@tonic-gate 		break;
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate }
1327c478bd9Sstevel@tonic-gate 
1337c478bd9Sstevel@tonic-gate static void
do_announce(CTL_MSG * request,CTL_RESPONSE * response)1347c478bd9Sstevel@tonic-gate do_announce(CTL_MSG *request, CTL_RESPONSE *response)
1357c478bd9Sstevel@tonic-gate {
1367c478bd9Sstevel@tonic-gate 	struct hostent *hp;
1377c478bd9Sstevel@tonic-gate 	CTL_MSG *ptr;
1387c478bd9Sstevel@tonic-gate 	int result;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * See if the user is logged.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	result = find_user(request->r_name, request->r_tty);
1447c478bd9Sstevel@tonic-gate 	if (result != SUCCESS) {
1457c478bd9Sstevel@tonic-gate 		response->answer = result;
1467c478bd9Sstevel@tonic-gate 		return;
1477c478bd9Sstevel@tonic-gate 	}
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&request->ctl_addr.sin_addr,
1507c478bd9Sstevel@tonic-gate 	    sizeof (struct in_addr), AF_INET);
1517c478bd9Sstevel@tonic-gate 	if (hp == NULL) {
1527c478bd9Sstevel@tonic-gate 		response->answer = MACHINE_UNKNOWN;
1537c478bd9Sstevel@tonic-gate 		return;
1547c478bd9Sstevel@tonic-gate 	}
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate 	ptr = find_request(request);
1577c478bd9Sstevel@tonic-gate 	if (ptr == NULL) {
1587c478bd9Sstevel@tonic-gate 		insert_table(request, response);
1597c478bd9Sstevel@tonic-gate 		response->answer = announce(request, hp->h_name);
1607c478bd9Sstevel@tonic-gate 	} else if (request->id_num > ptr->id_num) {
1617c478bd9Sstevel@tonic-gate 		/*
1627c478bd9Sstevel@tonic-gate 		 * This is an explicit re-announce, so update the id_num
1637c478bd9Sstevel@tonic-gate 		 * field to avoid duplicates and re-announce the talk.
1647c478bd9Sstevel@tonic-gate 		 */
1657c478bd9Sstevel@tonic-gate 		ptr->id_num = response->id_num = new_id();
1667c478bd9Sstevel@tonic-gate 		response->answer = announce(request, hp->h_name);
1677c478bd9Sstevel@tonic-gate 	} else {
1687c478bd9Sstevel@tonic-gate 		/* a duplicated request, so ignore it */
1697c478bd9Sstevel@tonic-gate 		response->id_num = ptr->id_num;
1707c478bd9Sstevel@tonic-gate 		response->answer = SUCCESS;
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate }
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate /*
1757c478bd9Sstevel@tonic-gate  * Search utmp for the local user.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static int
find_user(char * name,char * tty)1797c478bd9Sstevel@tonic-gate find_user(char *name, char *tty)
1807c478bd9Sstevel@tonic-gate {
1817c478bd9Sstevel@tonic-gate 	struct utmpx *ubuf;
1827c478bd9Sstevel@tonic-gate 	int tfd;
1837c478bd9Sstevel@tonic-gate 	char dev[MAXPATHLEN];
184*a856bf05Sblu 	struct stat stbuf;
185*a856bf05Sblu 	int problem = NOT_HERE;
1867c478bd9Sstevel@tonic-gate 
1877c478bd9Sstevel@tonic-gate 	setutxent();		/* reset the utmpx file */
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	while (ubuf = getutxent()) {
1907c478bd9Sstevel@tonic-gate 		if (ubuf->ut_type == USER_PROCESS &&
1917c478bd9Sstevel@tonic-gate 		    strncmp(ubuf->ut_user, name, sizeof (ubuf->ut_user)) == 0) {
1927c478bd9Sstevel@tonic-gate 			/*
1937c478bd9Sstevel@tonic-gate 			 * Check if this entry is really a tty.
1947c478bd9Sstevel@tonic-gate 			 */
1957c478bd9Sstevel@tonic-gate 			(void) snprintf(dev, sizeof (dev), "/dev/%.*s",
1967c478bd9Sstevel@tonic-gate 			    sizeof (ubuf->ut_line), ubuf->ut_line);
1977c478bd9Sstevel@tonic-gate 			if ((tfd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
1987c478bd9Sstevel@tonic-gate 				continue;
1997c478bd9Sstevel@tonic-gate 			}
2007c478bd9Sstevel@tonic-gate 			if (!isatty(tfd)) {
2017c478bd9Sstevel@tonic-gate 				(void) close(tfd);
2027c478bd9Sstevel@tonic-gate 				openlog("talk", 0, LOG_AUTH);
2037c478bd9Sstevel@tonic-gate 				syslog(LOG_CRIT, "%.*s in utmp is not a tty\n",
2047c478bd9Sstevel@tonic-gate 				    sizeof (ubuf->ut_line), ubuf->ut_line);
2057c478bd9Sstevel@tonic-gate 				closelog();
2067c478bd9Sstevel@tonic-gate 				continue;
2077c478bd9Sstevel@tonic-gate 			}
2087c478bd9Sstevel@tonic-gate 			if (*tty == '\0') {
2097c478bd9Sstevel@tonic-gate 				/*
2107c478bd9Sstevel@tonic-gate 				 * No particular tty was requested.
2117c478bd9Sstevel@tonic-gate 				 */
212*a856bf05Sblu 				if (fstat(tfd, &stbuf) < 0 ||
213*a856bf05Sblu 				    (stbuf.st_mode&020) == 0) {
214*a856bf05Sblu 					(void) close(tfd);
215*a856bf05Sblu 					problem = PERMISSION_DENIED;
216*a856bf05Sblu 					continue;
217*a856bf05Sblu 				}
218*a856bf05Sblu 				(void) close(tfd);
2197c478bd9Sstevel@tonic-gate 				(void) strlcpy(tty, ubuf->ut_line, TTY_SIZE);
2207c478bd9Sstevel@tonic-gate 				endutxent();	/* close the utmpx file */
2217c478bd9Sstevel@tonic-gate 				return (SUCCESS);
222*a856bf05Sblu 			}
223*a856bf05Sblu 			(void) close(tfd);
224*a856bf05Sblu 			if (strcmp(ubuf->ut_line, tty) == 0) {
2257c478bd9Sstevel@tonic-gate 				endutxent();	/* close the utmpx file */
2267c478bd9Sstevel@tonic-gate 				return (SUCCESS);
2277c478bd9Sstevel@tonic-gate 			}
2287c478bd9Sstevel@tonic-gate 		}
2297c478bd9Sstevel@tonic-gate 	}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	endutxent();		/* close the utmpx file */
232*a856bf05Sblu 	return (problem);
2337c478bd9Sstevel@tonic-gate }
234