10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*5720Sblu * Common Development and Distribution License (the "License").
6*5720Sblu * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate *
21*5720Sblu * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
220Sstevel@tonic-gate * Use is subject to license terms.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate /*
260Sstevel@tonic-gate * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
270Sstevel@tonic-gate * All Rights Reserved.
280Sstevel@tonic-gate */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
320Sstevel@tonic-gate * The Regents of the University of California.
330Sstevel@tonic-gate * All Rights Reserved.
340Sstevel@tonic-gate *
350Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
360Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
370Sstevel@tonic-gate * contributors.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate
400Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
410Sstevel@tonic-gate
420Sstevel@tonic-gate
430Sstevel@tonic-gate /*
440Sstevel@tonic-gate * process.c handles the requests, which can be of three types:
450Sstevel@tonic-gate *
460Sstevel@tonic-gate * ANNOUNCE - announce to a user that a talk is wanted
470Sstevel@tonic-gate *
480Sstevel@tonic-gate * LEAVE_INVITE - insert the request into the table
490Sstevel@tonic-gate *
500Sstevel@tonic-gate * LOOK_UP - look up to see if a request is waiting in
510Sstevel@tonic-gate * in the table for the local user
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * DELETE - delete invitation
540Sstevel@tonic-gate *
550Sstevel@tonic-gate */
560Sstevel@tonic-gate
570Sstevel@tonic-gate #include <sys/types.h>
580Sstevel@tonic-gate #include <sys/stat.h>
590Sstevel@tonic-gate #include <fcntl.h>
600Sstevel@tonic-gate #include <syslog.h>
610Sstevel@tonic-gate #include <string.h>
620Sstevel@tonic-gate #include <utmpx.h>
630Sstevel@tonic-gate #include <unistd.h>
640Sstevel@tonic-gate #include <stdlib.h>
650Sstevel@tonic-gate #include <stdio.h>
660Sstevel@tonic-gate #include "talkd_impl.h"
670Sstevel@tonic-gate
680Sstevel@tonic-gate static void do_announce(CTL_MSG *request, CTL_RESPONSE *response);
690Sstevel@tonic-gate static int find_user(char *name, char *tty);
700Sstevel@tonic-gate
710Sstevel@tonic-gate void
process_request(CTL_MSG * request,CTL_RESPONSE * response)720Sstevel@tonic-gate process_request(CTL_MSG *request, CTL_RESPONSE *response)
730Sstevel@tonic-gate {
740Sstevel@tonic-gate CTL_MSG *ptr;
750Sstevel@tonic-gate
760Sstevel@tonic-gate response->type = request->type;
770Sstevel@tonic-gate response->id_num = 0;
780Sstevel@tonic-gate
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate * Check if any of the strings within the request structure aren't
810Sstevel@tonic-gate * NUL terminated, and if so don't bother processing the request
820Sstevel@tonic-gate * further.
830Sstevel@tonic-gate */
840Sstevel@tonic-gate if ((memchr(request->l_name, '\0', sizeof (request->l_name)) == NULL) ||
850Sstevel@tonic-gate (memchr(request->r_name, '\0', sizeof (request->r_name)) == NULL) ||
860Sstevel@tonic-gate (memchr(request->r_tty, '\0', sizeof (request->r_tty)) == NULL)) {
870Sstevel@tonic-gate response->answer = FAILED;
880Sstevel@tonic-gate openlog("talk", 0, LOG_AUTH);
890Sstevel@tonic-gate syslog(LOG_CRIT, "malformed talk request\n");
900Sstevel@tonic-gate closelog();
910Sstevel@tonic-gate return;
920Sstevel@tonic-gate }
930Sstevel@tonic-gate
940Sstevel@tonic-gate switch (request->type) {
950Sstevel@tonic-gate
960Sstevel@tonic-gate case ANNOUNCE :
970Sstevel@tonic-gate
980Sstevel@tonic-gate do_announce(request, response);
990Sstevel@tonic-gate break;
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate case LEAVE_INVITE :
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate ptr = find_request(request);
1040Sstevel@tonic-gate if (ptr != NULL) {
1050Sstevel@tonic-gate response->id_num = ptr->id_num;
1060Sstevel@tonic-gate response->answer = SUCCESS;
1070Sstevel@tonic-gate } else {
1080Sstevel@tonic-gate insert_table(request, response);
1090Sstevel@tonic-gate }
1100Sstevel@tonic-gate break;
1110Sstevel@tonic-gate
1120Sstevel@tonic-gate case LOOK_UP :
1130Sstevel@tonic-gate
1140Sstevel@tonic-gate ptr = find_match(request);
1150Sstevel@tonic-gate if (ptr != NULL) {
1160Sstevel@tonic-gate response->id_num = ptr->id_num;
1170Sstevel@tonic-gate response->addr = ptr->addr;
1180Sstevel@tonic-gate response->answer = SUCCESS;
1190Sstevel@tonic-gate } else {
1200Sstevel@tonic-gate response->answer = NOT_HERE;
1210Sstevel@tonic-gate }
1220Sstevel@tonic-gate break;
1230Sstevel@tonic-gate
1240Sstevel@tonic-gate case DELETE :
1250Sstevel@tonic-gate
1260Sstevel@tonic-gate response->answer = delete_invite(request->id_num);
1270Sstevel@tonic-gate break;
1280Sstevel@tonic-gate
1290Sstevel@tonic-gate default :
1300Sstevel@tonic-gate
1310Sstevel@tonic-gate response->answer = UNKNOWN_REQUEST;
1320Sstevel@tonic-gate break;
1330Sstevel@tonic-gate }
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate
1360Sstevel@tonic-gate static void
do_announce(CTL_MSG * request,CTL_RESPONSE * response)1370Sstevel@tonic-gate do_announce(CTL_MSG *request, CTL_RESPONSE *response)
1380Sstevel@tonic-gate {
1390Sstevel@tonic-gate struct hostent *hp;
1400Sstevel@tonic-gate CTL_MSG *ptr;
1410Sstevel@tonic-gate int result;
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate /*
1440Sstevel@tonic-gate * See if the user is logged.
1450Sstevel@tonic-gate */
1460Sstevel@tonic-gate result = find_user(request->r_name, request->r_tty);
1470Sstevel@tonic-gate if (result != SUCCESS) {
1480Sstevel@tonic-gate response->answer = result;
1490Sstevel@tonic-gate return;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate
1520Sstevel@tonic-gate hp = gethostbyaddr((const char *)&request->ctl_addr.sin_addr,
1530Sstevel@tonic-gate sizeof (struct in_addr), AF_INET);
1540Sstevel@tonic-gate if (hp == NULL) {
1550Sstevel@tonic-gate response->answer = MACHINE_UNKNOWN;
1560Sstevel@tonic-gate return;
1570Sstevel@tonic-gate }
1580Sstevel@tonic-gate
1590Sstevel@tonic-gate ptr = find_request(request);
1600Sstevel@tonic-gate if (ptr == NULL) {
1610Sstevel@tonic-gate insert_table(request, response);
1620Sstevel@tonic-gate response->answer = announce(request, hp->h_name);
1630Sstevel@tonic-gate } else if (request->id_num > ptr->id_num) {
1640Sstevel@tonic-gate /*
1650Sstevel@tonic-gate * This is an explicit re-announce, so update the id_num
1660Sstevel@tonic-gate * field to avoid duplicates and re-announce the talk.
1670Sstevel@tonic-gate */
1680Sstevel@tonic-gate ptr->id_num = response->id_num = new_id();
1690Sstevel@tonic-gate response->answer = announce(request, hp->h_name);
1700Sstevel@tonic-gate } else {
1710Sstevel@tonic-gate /* a duplicated request, so ignore it */
1720Sstevel@tonic-gate response->id_num = ptr->id_num;
1730Sstevel@tonic-gate response->answer = SUCCESS;
1740Sstevel@tonic-gate }
1750Sstevel@tonic-gate }
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate /*
1780Sstevel@tonic-gate * Search utmp for the local user.
1790Sstevel@tonic-gate */
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate static int
find_user(char * name,char * tty)1820Sstevel@tonic-gate find_user(char *name, char *tty)
1830Sstevel@tonic-gate {
1840Sstevel@tonic-gate struct utmpx *ubuf;
1850Sstevel@tonic-gate int tfd;
1860Sstevel@tonic-gate char dev[MAXPATHLEN];
187*5720Sblu struct stat stbuf;
188*5720Sblu int problem = NOT_HERE;
1890Sstevel@tonic-gate
1900Sstevel@tonic-gate setutxent(); /* reset the utmpx file */
1910Sstevel@tonic-gate
1920Sstevel@tonic-gate while (ubuf = getutxent()) {
1930Sstevel@tonic-gate if (ubuf->ut_type == USER_PROCESS &&
1940Sstevel@tonic-gate strncmp(ubuf->ut_user, name, sizeof (ubuf->ut_user)) == 0) {
1950Sstevel@tonic-gate /*
1960Sstevel@tonic-gate * Check if this entry is really a tty.
1970Sstevel@tonic-gate */
1980Sstevel@tonic-gate (void) snprintf(dev, sizeof (dev), "/dev/%.*s",
1990Sstevel@tonic-gate sizeof (ubuf->ut_line), ubuf->ut_line);
2000Sstevel@tonic-gate if ((tfd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
2010Sstevel@tonic-gate continue;
2020Sstevel@tonic-gate }
2030Sstevel@tonic-gate if (!isatty(tfd)) {
2040Sstevel@tonic-gate (void) close(tfd);
2050Sstevel@tonic-gate openlog("talk", 0, LOG_AUTH);
2060Sstevel@tonic-gate syslog(LOG_CRIT, "%.*s in utmp is not a tty\n",
2070Sstevel@tonic-gate sizeof (ubuf->ut_line), ubuf->ut_line);
2080Sstevel@tonic-gate closelog();
2090Sstevel@tonic-gate continue;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate if (*tty == '\0') {
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * No particular tty was requested.
2140Sstevel@tonic-gate */
215*5720Sblu if (fstat(tfd, &stbuf) < 0 ||
216*5720Sblu (stbuf.st_mode&020) == 0) {
217*5720Sblu (void) close(tfd);
218*5720Sblu problem = PERMISSION_DENIED;
219*5720Sblu continue;
220*5720Sblu }
221*5720Sblu (void) close(tfd);
2220Sstevel@tonic-gate (void) strlcpy(tty, ubuf->ut_line, TTY_SIZE);
2230Sstevel@tonic-gate endutxent(); /* close the utmpx file */
2240Sstevel@tonic-gate return (SUCCESS);
225*5720Sblu }
226*5720Sblu (void) close(tfd);
227*5720Sblu if (strcmp(ubuf->ut_line, tty) == 0) {
2280Sstevel@tonic-gate endutxent(); /* close the utmpx file */
2290Sstevel@tonic-gate return (SUCCESS);
2300Sstevel@tonic-gate }
2310Sstevel@tonic-gate }
2320Sstevel@tonic-gate }
2330Sstevel@tonic-gate
2340Sstevel@tonic-gate endutxent(); /* close the utmpx file */
235*5720Sblu return (problem);
2360Sstevel@tonic-gate }
237