1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  *
22*0Sstevel@tonic-gate  * Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
28*0Sstevel@tonic-gate  * All Rights Reserved.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate /*
32*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
33*0Sstevel@tonic-gate  * The Regents of the University of California.
34*0Sstevel@tonic-gate  * All Rights Reserved.
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
37*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
38*0Sstevel@tonic-gate  * contributors.
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate /*
45*0Sstevel@tonic-gate  * process.c handles the requests, which can be of three types:
46*0Sstevel@tonic-gate  *
47*0Sstevel@tonic-gate  * ANNOUNCE - announce to a user that a talk is wanted
48*0Sstevel@tonic-gate  *
49*0Sstevel@tonic-gate  * LEAVE_INVITE - insert the request into the table
50*0Sstevel@tonic-gate  *
51*0Sstevel@tonic-gate  * LOOK_UP - look up to see if a request is waiting in
52*0Sstevel@tonic-gate  * in the table for the local user
53*0Sstevel@tonic-gate  *
54*0Sstevel@tonic-gate  * DELETE - delete invitation
55*0Sstevel@tonic-gate  *
56*0Sstevel@tonic-gate  */
57*0Sstevel@tonic-gate 
58*0Sstevel@tonic-gate #include <sys/types.h>
59*0Sstevel@tonic-gate #include <sys/stat.h>
60*0Sstevel@tonic-gate #include <fcntl.h>
61*0Sstevel@tonic-gate #include <syslog.h>
62*0Sstevel@tonic-gate #include <string.h>
63*0Sstevel@tonic-gate #include <utmpx.h>
64*0Sstevel@tonic-gate #include <unistd.h>
65*0Sstevel@tonic-gate #include <stdlib.h>
66*0Sstevel@tonic-gate #include <stdio.h>
67*0Sstevel@tonic-gate #include "talkd_impl.h"
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate static void do_announce(CTL_MSG *request, CTL_RESPONSE *response);
70*0Sstevel@tonic-gate static int find_user(char *name, char *tty);
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate void
73*0Sstevel@tonic-gate process_request(CTL_MSG *request, CTL_RESPONSE *response)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate 	CTL_MSG *ptr;
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	response->type = request->type;
78*0Sstevel@tonic-gate 	response->id_num = 0;
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 	/*
81*0Sstevel@tonic-gate 	 * Check if any of the strings within the request structure aren't
82*0Sstevel@tonic-gate 	 * NUL terminated, and if so don't bother processing the request
83*0Sstevel@tonic-gate 	 * further.
84*0Sstevel@tonic-gate 	 */
85*0Sstevel@tonic-gate 	if ((memchr(request->l_name, '\0', sizeof (request->l_name)) == NULL) ||
86*0Sstevel@tonic-gate 	    (memchr(request->r_name, '\0', sizeof (request->r_name)) == NULL) ||
87*0Sstevel@tonic-gate 	    (memchr(request->r_tty, '\0', sizeof (request->r_tty)) == NULL)) {
88*0Sstevel@tonic-gate 		response->answer = FAILED;
89*0Sstevel@tonic-gate 		openlog("talk", 0, LOG_AUTH);
90*0Sstevel@tonic-gate 		syslog(LOG_CRIT, "malformed talk request\n");
91*0Sstevel@tonic-gate 		closelog();
92*0Sstevel@tonic-gate 		return;
93*0Sstevel@tonic-gate 	}
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate 	switch (request->type) {
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	    case ANNOUNCE :
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 		do_announce(request, response);
100*0Sstevel@tonic-gate 		break;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	    case LEAVE_INVITE :
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		ptr = find_request(request);
105*0Sstevel@tonic-gate 		if (ptr != NULL) {
106*0Sstevel@tonic-gate 			response->id_num = ptr->id_num;
107*0Sstevel@tonic-gate 			response->answer = SUCCESS;
108*0Sstevel@tonic-gate 		} else {
109*0Sstevel@tonic-gate 			insert_table(request, response);
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate 		break;
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate 	    case LOOK_UP :
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 		ptr = find_match(request);
116*0Sstevel@tonic-gate 		if (ptr != NULL) {
117*0Sstevel@tonic-gate 			response->id_num = ptr->id_num;
118*0Sstevel@tonic-gate 			response->addr = ptr->addr;
119*0Sstevel@tonic-gate 			response->answer = SUCCESS;
120*0Sstevel@tonic-gate 		} else {
121*0Sstevel@tonic-gate 			response->answer = NOT_HERE;
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 		break;
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	    case DELETE :
126*0Sstevel@tonic-gate 
127*0Sstevel@tonic-gate 		response->answer = delete_invite(request->id_num);
128*0Sstevel@tonic-gate 		break;
129*0Sstevel@tonic-gate 
130*0Sstevel@tonic-gate 	    default :
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 		response->answer = UNKNOWN_REQUEST;
133*0Sstevel@tonic-gate 		break;
134*0Sstevel@tonic-gate 	}
135*0Sstevel@tonic-gate }
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static void
138*0Sstevel@tonic-gate do_announce(CTL_MSG *request, CTL_RESPONSE *response)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	struct hostent *hp;
141*0Sstevel@tonic-gate 	CTL_MSG *ptr;
142*0Sstevel@tonic-gate 	int result;
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	/*
145*0Sstevel@tonic-gate 	 * See if the user is logged.
146*0Sstevel@tonic-gate 	 */
147*0Sstevel@tonic-gate 	result = find_user(request->r_name, request->r_tty);
148*0Sstevel@tonic-gate 	if (result != SUCCESS) {
149*0Sstevel@tonic-gate 		response->answer = result;
150*0Sstevel@tonic-gate 		return;
151*0Sstevel@tonic-gate 	}
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate 	hp = gethostbyaddr((const char *)&request->ctl_addr.sin_addr,
154*0Sstevel@tonic-gate 	    sizeof (struct in_addr), AF_INET);
155*0Sstevel@tonic-gate 	if (hp == NULL) {
156*0Sstevel@tonic-gate 		response->answer = MACHINE_UNKNOWN;
157*0Sstevel@tonic-gate 		return;
158*0Sstevel@tonic-gate 	}
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	ptr = find_request(request);
161*0Sstevel@tonic-gate 	if (ptr == NULL) {
162*0Sstevel@tonic-gate 		insert_table(request, response);
163*0Sstevel@tonic-gate 		response->answer = announce(request, hp->h_name);
164*0Sstevel@tonic-gate 	} else if (request->id_num > ptr->id_num) {
165*0Sstevel@tonic-gate 		/*
166*0Sstevel@tonic-gate 		 * This is an explicit re-announce, so update the id_num
167*0Sstevel@tonic-gate 		 * field to avoid duplicates and re-announce the talk.
168*0Sstevel@tonic-gate 		 */
169*0Sstevel@tonic-gate 		ptr->id_num = response->id_num = new_id();
170*0Sstevel@tonic-gate 		response->answer = announce(request, hp->h_name);
171*0Sstevel@tonic-gate 	} else {
172*0Sstevel@tonic-gate 		/* a duplicated request, so ignore it */
173*0Sstevel@tonic-gate 		response->id_num = ptr->id_num;
174*0Sstevel@tonic-gate 		response->answer = SUCCESS;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate }
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate /*
179*0Sstevel@tonic-gate  * Search utmp for the local user.
180*0Sstevel@tonic-gate  */
181*0Sstevel@tonic-gate 
182*0Sstevel@tonic-gate static int
183*0Sstevel@tonic-gate find_user(char *name, char *tty)
184*0Sstevel@tonic-gate {
185*0Sstevel@tonic-gate 	struct utmpx *ubuf;
186*0Sstevel@tonic-gate 	int tfd;
187*0Sstevel@tonic-gate 	char dev[MAXPATHLEN];
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	setutxent();		/* reset the utmpx file */
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 	while (ubuf = getutxent()) {
192*0Sstevel@tonic-gate 		if (ubuf->ut_type == USER_PROCESS &&
193*0Sstevel@tonic-gate 		    strncmp(ubuf->ut_user, name, sizeof (ubuf->ut_user)) == 0) {
194*0Sstevel@tonic-gate 			/*
195*0Sstevel@tonic-gate 			 * Check if this entry is really a tty.
196*0Sstevel@tonic-gate 			 */
197*0Sstevel@tonic-gate 			(void) snprintf(dev, sizeof (dev), "/dev/%.*s",
198*0Sstevel@tonic-gate 			    sizeof (ubuf->ut_line), ubuf->ut_line);
199*0Sstevel@tonic-gate 			if ((tfd = open(dev, O_WRONLY|O_NOCTTY)) == -1) {
200*0Sstevel@tonic-gate 				continue;
201*0Sstevel@tonic-gate 			}
202*0Sstevel@tonic-gate 			if (!isatty(tfd)) {
203*0Sstevel@tonic-gate 				(void) close(tfd);
204*0Sstevel@tonic-gate 				openlog("talk", 0, LOG_AUTH);
205*0Sstevel@tonic-gate 				syslog(LOG_CRIT, "%.*s in utmp is not a tty\n",
206*0Sstevel@tonic-gate 				    sizeof (ubuf->ut_line), ubuf->ut_line);
207*0Sstevel@tonic-gate 				closelog();
208*0Sstevel@tonic-gate 				continue;
209*0Sstevel@tonic-gate 			}
210*0Sstevel@tonic-gate 			(void) close(tfd);
211*0Sstevel@tonic-gate 			if (*tty == '\0') {
212*0Sstevel@tonic-gate 				/*
213*0Sstevel@tonic-gate 				 * No particular tty was requested.
214*0Sstevel@tonic-gate 				 */
215*0Sstevel@tonic-gate 				(void) strlcpy(tty, ubuf->ut_line, TTY_SIZE);
216*0Sstevel@tonic-gate 				endutxent();	/* close the utmpx file */
217*0Sstevel@tonic-gate 				return (SUCCESS);
218*0Sstevel@tonic-gate 			} else if (strcmp(ubuf->ut_line, tty) == 0) {
219*0Sstevel@tonic-gate 				endutxent();	/* close the utmpx file */
220*0Sstevel@tonic-gate 				return (SUCCESS);
221*0Sstevel@tonic-gate 			}
222*0Sstevel@tonic-gate 		}
223*0Sstevel@tonic-gate 	}
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	endutxent();		/* close the utmpx file */
226*0Sstevel@tonic-gate 	return (NOT_HERE);
227*0Sstevel@tonic-gate }
228