17c478bd9Sstevel@tonic-gate /*
2*a5128709SShawn Emery  * CDDL HEADER START
3*a5128709SShawn Emery  *
4*a5128709SShawn Emery  * The contents of this file are subject to the terms of the
5*a5128709SShawn Emery  * Common Development and Distribution License (the "License").
6*a5128709SShawn Emery  * You may not use this file except in compliance with the License.
7*a5128709SShawn Emery  *
8*a5128709SShawn Emery  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*a5128709SShawn Emery  * or http://www.opensolaris.org/os/licensing.
10*a5128709SShawn Emery  * See the License for the specific language governing permissions
11*a5128709SShawn Emery  * and limitations under the License.
12*a5128709SShawn Emery  *
13*a5128709SShawn Emery  * When distributing Covered Code, include this CDDL HEADER in each
14*a5128709SShawn Emery  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*a5128709SShawn Emery  * If applicable, add the following below this CDDL HEADER, with the
16*a5128709SShawn Emery  * fields enclosed by brackets "[]" replaced with your own identifying
17*a5128709SShawn Emery  * information: Portions Copyright [yyyy] [name of copyright owner]
18*a5128709SShawn Emery  *
19*a5128709SShawn Emery  * CDDL HEADER END
20*a5128709SShawn Emery  */
21*a5128709SShawn Emery 
22*a5128709SShawn Emery /*
23*a5128709SShawn Emery  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	from kerbd_handle.c	1.3	92/01/29 SMI */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate /*
307c478bd9Sstevel@tonic-gate  * kwarnd_handle.c, Interface to kwarnd
317c478bd9Sstevel@tonic-gate  *
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <unistd.h>
357c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
367c478bd9Sstevel@tonic-gate #include <rpc/clnt.h>
377c478bd9Sstevel@tonic-gate #include <stdio.h>
387c478bd9Sstevel@tonic-gate #include <string.h>
397c478bd9Sstevel@tonic-gate #include <netconfig.h>
407c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
417c478bd9Sstevel@tonic-gate #include "kwarnd.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #ifdef DEBUG
447c478bd9Sstevel@tonic-gate #define	dprt(msg)
457c478bd9Sstevel@tonic-gate #else
467c478bd9Sstevel@tonic-gate #define	dprt(msg)
477c478bd9Sstevel@tonic-gate #endif /* DEBUG */
487c478bd9Sstevel@tonic-gate 
49*a5128709SShawn Emery CLIENT *kwarn_clnt;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate /*
527c478bd9Sstevel@tonic-gate  * Keep the handle cached.  This call may be made quite often.
537c478bd9Sstevel@tonic-gate  */
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate CLIENT *
getkwarnd_handle(void)567c478bd9Sstevel@tonic-gate getkwarnd_handle(void)
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	void *localhandle;
597c478bd9Sstevel@tonic-gate 	struct netconfig *nconf;
607c478bd9Sstevel@tonic-gate 	struct netconfig *tpconf;
617c478bd9Sstevel@tonic-gate 	struct timeval wait_time;
627c478bd9Sstevel@tonic-gate 	struct utsname u;
637c478bd9Sstevel@tonic-gate 	static char *hostname;
647c478bd9Sstevel@tonic-gate 	static bool_t first_time = TRUE;
657c478bd9Sstevel@tonic-gate 
66*a5128709SShawn Emery /*
67*a5128709SShawn Emery  * Total timeout (in seconds) talking to kwarnd.
68*a5128709SShawn Emery  */
69*a5128709SShawn Emery #define	TOTAL_TIMEOUT	5
707c478bd9Sstevel@tonic-gate 
71*a5128709SShawn Emery 	if (kwarn_clnt)
72*a5128709SShawn Emery 		return (kwarn_clnt);
737c478bd9Sstevel@tonic-gate 	if (!(localhandle = setnetconfig()))
747c478bd9Sstevel@tonic-gate 		return (NULL);
757c478bd9Sstevel@tonic-gate 	tpconf = NULL;
767c478bd9Sstevel@tonic-gate 	if (first_time == TRUE) {
777c478bd9Sstevel@tonic-gate 		if (uname(&u) == -1) {
787c478bd9Sstevel@tonic-gate 			(void) endnetconfig(localhandle);
797c478bd9Sstevel@tonic-gate 			return ((CLIENT *)NULL);
807c478bd9Sstevel@tonic-gate 		}
817c478bd9Sstevel@tonic-gate 		if ((hostname = strdup(u.nodename)) == (char *)NULL) {
827c478bd9Sstevel@tonic-gate 			(void) endnetconfig(localhandle);
837c478bd9Sstevel@tonic-gate 			return ((CLIENT *)NULL);
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		first_time = FALSE;
867c478bd9Sstevel@tonic-gate 	}
877c478bd9Sstevel@tonic-gate 	while (nconf = getnetconfig(localhandle)) {
887c478bd9Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
897c478bd9Sstevel@tonic-gate 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
90*a5128709SShawn Emery 				kwarn_clnt = clnt_tp_create(hostname,
91*a5128709SShawn Emery 				    KWARNPROG, KWARNVERS, nconf);
92*a5128709SShawn Emery 				if (kwarn_clnt) {
937c478bd9Sstevel@tonic-gate 					dprt("got COTS_ORD\n");
947c478bd9Sstevel@tonic-gate 					break;
957c478bd9Sstevel@tonic-gate 				}
967c478bd9Sstevel@tonic-gate 			} else {
977c478bd9Sstevel@tonic-gate 				tpconf = nconf;
987c478bd9Sstevel@tonic-gate 			}
997c478bd9Sstevel@tonic-gate 		}
1007c478bd9Sstevel@tonic-gate 	}
101*a5128709SShawn Emery 	if ((kwarn_clnt == NULL) && (tpconf)) {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 		/* Now, try the connection-oriented loopback transport */
1047c478bd9Sstevel@tonic-gate 
105*a5128709SShawn Emery 		kwarn_clnt = clnt_tp_create(hostname, KWARNPROG, KWARNVERS,
106*a5128709SShawn Emery 		    tpconf);
1077c478bd9Sstevel@tonic-gate #ifdef DEBUG
108*a5128709SShawn Emery 		if (kwarn_clnt) {
1097c478bd9Sstevel@tonic-gate 			dprt("got COTS\n");
1107c478bd9Sstevel@tonic-gate 		}
1117c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
1127c478bd9Sstevel@tonic-gate 	}
1137c478bd9Sstevel@tonic-gate 	(void) endnetconfig(localhandle);
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	/*
1167c478bd9Sstevel@tonic-gate 	 * This bit of code uses an as yet unimplemented argument to
1177c478bd9Sstevel@tonic-gate 	 * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
1187c478bd9Sstevel@tonic-gate 	 * loopback transport should be checked to ensure it is
1197c478bd9Sstevel@tonic-gate 	 * connected to a process running as root. If so, the clnt_control()
1207c478bd9Sstevel@tonic-gate 	 * call returns TRUE. If not, it returns FALSE.
1217c478bd9Sstevel@tonic-gate 	 */
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate #ifdef CLSET_SVC_PRIV
1247c478bd9Sstevel@tonic-gate 
125*a5128709SShawn Emery 	if (clnt_control(kwarn_clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
126*a5128709SShawn Emery 		clnt_destroy(kwarn_clnt);
127*a5128709SShawn Emery 		kwarn_clnt = NULL;
1287c478bd9Sstevel@tonic-gate 		return (NULL);
1297c478bd9Sstevel@tonic-gate 	{
1307c478bd9Sstevel@tonic-gate #endif
131*a5128709SShawn Emery 	if (kwarn_clnt == NULL)
1327c478bd9Sstevel@tonic-gate 		return (NULL);
1337c478bd9Sstevel@tonic-gate 
134*a5128709SShawn Emery 	kwarn_clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
135*a5128709SShawn Emery 	if (kwarn_clnt->cl_auth == NULL) {
136*a5128709SShawn Emery 		clnt_destroy(kwarn_clnt);
137*a5128709SShawn Emery 		kwarn_clnt = NULL;
1387c478bd9Sstevel@tonic-gate 		return (NULL);
1397c478bd9Sstevel@tonic-gate 	}
140*a5128709SShawn Emery 	wait_time.tv_sec = TOTAL_TIMEOUT;
1417c478bd9Sstevel@tonic-gate 	wait_time.tv_usec = 0;
142*a5128709SShawn Emery 	(void) clnt_control(kwarn_clnt, CLSET_TIMEOUT, (char *)&wait_time);
1437c478bd9Sstevel@tonic-gate 
144*a5128709SShawn Emery 	return (kwarn_clnt);
145*a5128709SShawn Emery }
146*a5128709SShawn Emery 
147*a5128709SShawn Emery void
148*a5128709SShawn Emery resetkwarnd_handle(void)
149*a5128709SShawn Emery {
150*a5128709SShawn Emery 	auth_destroy(kwarn_clnt->cl_auth);
151*a5128709SShawn Emery 	clnt_destroy(kwarn_clnt);
152*a5128709SShawn Emery 	kwarn_clnt = NULL;
1537c478bd9Sstevel@tonic-gate }
154