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 #pragma ident "%Z%%M% %I% %E% SMI"
417c478bd9Sstevel@tonic-gate
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate * process.c handles the requests, which can be of three types:
457c478bd9Sstevel@tonic-gate *
467c478bd9Sstevel@tonic-gate * ANNOUNCE - announce to a user that a talk is wanted
477c478bd9Sstevel@tonic-gate *
487c478bd9Sstevel@tonic-gate * LEAVE_INVITE - insert the request into the table
497c478bd9Sstevel@tonic-gate *
507c478bd9Sstevel@tonic-gate * LOOK_UP - look up to see if a request is waiting in
517c478bd9Sstevel@tonic-gate * in the table for the local user
527c478bd9Sstevel@tonic-gate *
537c478bd9Sstevel@tonic-gate * DELETE - delete invitation
547c478bd9Sstevel@tonic-gate *
557c478bd9Sstevel@tonic-gate */
567c478bd9Sstevel@tonic-gate
577c478bd9Sstevel@tonic-gate #include <sys/types.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <fcntl.h>
607c478bd9Sstevel@tonic-gate #include <syslog.h>
617c478bd9Sstevel@tonic-gate #include <string.h>
627c478bd9Sstevel@tonic-gate #include <utmpx.h>
637c478bd9Sstevel@tonic-gate #include <unistd.h>
647c478bd9Sstevel@tonic-gate #include <stdlib.h>
657c478bd9Sstevel@tonic-gate #include <stdio.h>
667c478bd9Sstevel@tonic-gate #include "talkd_impl.h"
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static void do_announce(CTL_MSG *request, CTL_RESPONSE *response);
697c478bd9Sstevel@tonic-gate static int find_user(char *name, char *tty);
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate void
process_request(CTL_MSG * request,CTL_RESPONSE * response)727c478bd9Sstevel@tonic-gate process_request(CTL_MSG *request, CTL_RESPONSE *response)
737c478bd9Sstevel@tonic-gate {
747c478bd9Sstevel@tonic-gate CTL_MSG *ptr;
757c478bd9Sstevel@tonic-gate
767c478bd9Sstevel@tonic-gate response->type = request->type;
777c478bd9Sstevel@tonic-gate response->id_num = 0;
787c478bd9Sstevel@tonic-gate
797c478bd9Sstevel@tonic-gate /*
807c478bd9Sstevel@tonic-gate * Check if any of the strings within the request structure aren't
817c478bd9Sstevel@tonic-gate * NUL terminated, and if so don't bother processing the request
827c478bd9Sstevel@tonic-gate * further.
837c478bd9Sstevel@tonic-gate */
847c478bd9Sstevel@tonic-gate if ((memchr(request->l_name, '\0', sizeof (request->l_name)) == NULL) ||
857c478bd9Sstevel@tonic-gate (memchr(request->r_name, '\0', sizeof (request->r_name)) == NULL) ||
867c478bd9Sstevel@tonic-gate (memchr(request->r_tty, '\0', sizeof (request->r_tty)) == NULL)) {
877c478bd9Sstevel@tonic-gate response->answer = FAILED;
887c478bd9Sstevel@tonic-gate openlog("talk", 0, LOG_AUTH);
897c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, "malformed talk request\n");
907c478bd9Sstevel@tonic-gate closelog();
917c478bd9Sstevel@tonic-gate return;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate
947c478bd9Sstevel@tonic-gate switch (request->type) {
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate case ANNOUNCE :
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate do_announce(request, response);
997c478bd9Sstevel@tonic-gate break;
1007c478bd9Sstevel@tonic-gate
1017c478bd9Sstevel@tonic-gate case LEAVE_INVITE :
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate ptr = find_request(request);
1047c478bd9Sstevel@tonic-gate if (ptr != NULL) {
1057c478bd9Sstevel@tonic-gate response->id_num = ptr->id_num;
1067c478bd9Sstevel@tonic-gate response->answer = SUCCESS;
1077c478bd9Sstevel@tonic-gate } else {
1087c478bd9Sstevel@tonic-gate insert_table(request, response);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate break;
1117c478bd9Sstevel@tonic-gate
1127c478bd9Sstevel@tonic-gate case LOOK_UP :
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate ptr = find_match(request);
1157c478bd9Sstevel@tonic-gate if (ptr != NULL) {
1167c478bd9Sstevel@tonic-gate response->id_num = ptr->id_num;
1177c478bd9Sstevel@tonic-gate response->addr = ptr->addr;
1187c478bd9Sstevel@tonic-gate response->answer = SUCCESS;
1197c478bd9Sstevel@tonic-gate } else {
1207c478bd9Sstevel@tonic-gate response->answer = NOT_HERE;
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate break;
1237c478bd9Sstevel@tonic-gate
1247c478bd9Sstevel@tonic-gate case DELETE :
1257c478bd9Sstevel@tonic-gate
1267c478bd9Sstevel@tonic-gate response->answer = delete_invite(request->id_num);
1277c478bd9Sstevel@tonic-gate break;
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate default :
1307c478bd9Sstevel@tonic-gate
1317c478bd9Sstevel@tonic-gate response->answer = UNKNOWN_REQUEST;
1327c478bd9Sstevel@tonic-gate break;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate }
1357c478bd9Sstevel@tonic-gate
1367c478bd9Sstevel@tonic-gate static void
do_announce(CTL_MSG * request,CTL_RESPONSE * response)1377c478bd9Sstevel@tonic-gate do_announce(CTL_MSG *request, CTL_RESPONSE *response)
1387c478bd9Sstevel@tonic-gate {
1397c478bd9Sstevel@tonic-gate struct hostent *hp;
1407c478bd9Sstevel@tonic-gate CTL_MSG *ptr;
1417c478bd9Sstevel@tonic-gate int result;
1427c478bd9Sstevel@tonic-gate
1437c478bd9Sstevel@tonic-gate /*
1447c478bd9Sstevel@tonic-gate * See if the user is logged.
1457c478bd9Sstevel@tonic-gate */
1467c478bd9Sstevel@tonic-gate result = find_user(request->r_name, request->r_tty);
1477c478bd9Sstevel@tonic-gate if (result != SUCCESS) {
1487c478bd9Sstevel@tonic-gate response->answer = result;
1497c478bd9Sstevel@tonic-gate return;
1507c478bd9Sstevel@tonic-gate }
1517c478bd9Sstevel@tonic-gate
1527c478bd9Sstevel@tonic-gate hp = gethostbyaddr((const char *)&request->ctl_addr.sin_addr,
1537c478bd9Sstevel@tonic-gate sizeof (struct in_addr), AF_INET);
1547c478bd9Sstevel@tonic-gate if (hp == NULL) {
1557c478bd9Sstevel@tonic-gate response->answer = MACHINE_UNKNOWN;
1567c478bd9Sstevel@tonic-gate return;
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate
1597c478bd9Sstevel@tonic-gate ptr = find_request(request);
1607c478bd9Sstevel@tonic-gate if (ptr == NULL) {
1617c478bd9Sstevel@tonic-gate insert_table(request, response);
1627c478bd9Sstevel@tonic-gate response->answer = announce(request, hp->h_name);
1637c478bd9Sstevel@tonic-gate } else if (request->id_num > ptr->id_num) {
1647c478bd9Sstevel@tonic-gate /*
1657c478bd9Sstevel@tonic-gate * This is an explicit re-announce, so update the id_num
1667c478bd9Sstevel@tonic-gate * field to avoid duplicates and re-announce the talk.
1677c478bd9Sstevel@tonic-gate */
1687c478bd9Sstevel@tonic-gate ptr->id_num = response->id_num = new_id();
1697c478bd9Sstevel@tonic-gate response->answer = announce(request, hp->h_name);
1707c478bd9Sstevel@tonic-gate } else {
1717c478bd9Sstevel@tonic-gate /* a duplicated request, so ignore it */
1727c478bd9Sstevel@tonic-gate response->id_num = ptr->id_num;
1737c478bd9Sstevel@tonic-gate response->answer = SUCCESS;
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate * Search utmp for the local user.
1797c478bd9Sstevel@tonic-gate */
1807c478bd9Sstevel@tonic-gate
1817c478bd9Sstevel@tonic-gate static int
find_user(char * name,char * tty)1827c478bd9Sstevel@tonic-gate find_user(char *name, char *tty)
1837c478bd9Sstevel@tonic-gate {
1847c478bd9Sstevel@tonic-gate struct utmpx *ubuf;
1857c478bd9Sstevel@tonic-gate int tfd;
1867c478bd9Sstevel@tonic-gate char dev[MAXPATHLEN];
187*a856bf05Sblu struct stat stbuf;
188*a856bf05Sblu int problem = NOT_HERE;
1897c478bd9Sstevel@tonic-gate
1907c478bd9Sstevel@tonic-gate setutxent(); /* reset the utmpx file */
1917c478bd9Sstevel@tonic-gate
1927c478bd9Sstevel@tonic-gate while (ubuf = getutxent()) {
1937c478bd9Sstevel@tonic-gate if (ubuf->ut_type == USER_PROCESS &&
1947c478bd9Sstevel@tonic-gate strncmp(ubuf->ut_user, name, sizeof (ubuf->ut_user)) == 0) {
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate * Check if this entry is really a tty.
1977c478bd9Sstevel@tonic-gate */
1987c478bd9Sstevel@tonic-gate (void) snprintf(dev, sizeof (dev), "/dev/%.*s",
1997c478bd9Sstevel@tonic-gate sizeof (ubuf->ut_line), ubuf->ut_line);
2007c478bd9Sstevel@tonic-gate if ((tfd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
2017c478bd9Sstevel@tonic-gate continue;
2027c478bd9Sstevel@tonic-gate }
2037c478bd9Sstevel@tonic-gate if (!isatty(tfd)) {
2047c478bd9Sstevel@tonic-gate (void) close(tfd);
2057c478bd9Sstevel@tonic-gate openlog("talk", 0, LOG_AUTH);
2067c478bd9Sstevel@tonic-gate syslog(LOG_CRIT, "%.*s in utmp is not a tty\n",
2077c478bd9Sstevel@tonic-gate sizeof (ubuf->ut_line), ubuf->ut_line);
2087c478bd9Sstevel@tonic-gate closelog();
2097c478bd9Sstevel@tonic-gate continue;
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate if (*tty == '\0') {
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate * No particular tty was requested.
2147c478bd9Sstevel@tonic-gate */
215*a856bf05Sblu if (fstat(tfd, &stbuf) < 0 ||
216*a856bf05Sblu (stbuf.st_mode&020) == 0) {
217*a856bf05Sblu (void) close(tfd);
218*a856bf05Sblu problem = PERMISSION_DENIED;
219*a856bf05Sblu continue;
220*a856bf05Sblu }
221*a856bf05Sblu (void) close(tfd);
2227c478bd9Sstevel@tonic-gate (void) strlcpy(tty, ubuf->ut_line, TTY_SIZE);
2237c478bd9Sstevel@tonic-gate endutxent(); /* close the utmpx file */
2247c478bd9Sstevel@tonic-gate return (SUCCESS);
225*a856bf05Sblu }
226*a856bf05Sblu (void) close(tfd);
227*a856bf05Sblu if (strcmp(ubuf->ut_line, tty) == 0) {
2287c478bd9Sstevel@tonic-gate endutxent(); /* close the utmpx file */
2297c478bd9Sstevel@tonic-gate return (SUCCESS);
2307c478bd9Sstevel@tonic-gate }
2317c478bd9Sstevel@tonic-gate }
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate endutxent(); /* close the utmpx file */
235*a856bf05Sblu return (problem);
2367c478bd9Sstevel@tonic-gate }
237