10Sstevel@tonic-gate /*
2*8438SShawn.Emery@Sun.COM * CDDL HEADER START
3*8438SShawn.Emery@Sun.COM *
4*8438SShawn.Emery@Sun.COM * The contents of this file are subject to the terms of the
5*8438SShawn.Emery@Sun.COM * Common Development and Distribution License (the "License").
6*8438SShawn.Emery@Sun.COM * You may not use this file except in compliance with the License.
7*8438SShawn.Emery@Sun.COM *
8*8438SShawn.Emery@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*8438SShawn.Emery@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*8438SShawn.Emery@Sun.COM * See the License for the specific language governing permissions
11*8438SShawn.Emery@Sun.COM * and limitations under the License.
12*8438SShawn.Emery@Sun.COM *
13*8438SShawn.Emery@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*8438SShawn.Emery@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*8438SShawn.Emery@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*8438SShawn.Emery@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*8438SShawn.Emery@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*8438SShawn.Emery@Sun.COM *
19*8438SShawn.Emery@Sun.COM * CDDL HEADER END
20*8438SShawn.Emery@Sun.COM */
21*8438SShawn.Emery@Sun.COM
22*8438SShawn.Emery@Sun.COM /*
23*8438SShawn.Emery@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* from kerbd_handle.c 1.3 92/01/29 SMI */
280Sstevel@tonic-gate
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate * kwarnd_handle.c, Interface to kwarnd
310Sstevel@tonic-gate *
320Sstevel@tonic-gate */
330Sstevel@tonic-gate
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <rpc/rpc.h>
360Sstevel@tonic-gate #include <rpc/clnt.h>
370Sstevel@tonic-gate #include <stdio.h>
380Sstevel@tonic-gate #include <string.h>
390Sstevel@tonic-gate #include <netconfig.h>
400Sstevel@tonic-gate #include <sys/utsname.h>
410Sstevel@tonic-gate #include "kwarnd.h"
420Sstevel@tonic-gate
430Sstevel@tonic-gate #ifdef DEBUG
440Sstevel@tonic-gate #define dprt(msg)
450Sstevel@tonic-gate #else
460Sstevel@tonic-gate #define dprt(msg)
470Sstevel@tonic-gate #endif /* DEBUG */
480Sstevel@tonic-gate
49*8438SShawn.Emery@Sun.COM CLIENT *kwarn_clnt;
500Sstevel@tonic-gate
510Sstevel@tonic-gate /*
520Sstevel@tonic-gate * Keep the handle cached. This call may be made quite often.
530Sstevel@tonic-gate */
540Sstevel@tonic-gate
550Sstevel@tonic-gate CLIENT *
getkwarnd_handle(void)560Sstevel@tonic-gate getkwarnd_handle(void)
570Sstevel@tonic-gate {
580Sstevel@tonic-gate void *localhandle;
590Sstevel@tonic-gate struct netconfig *nconf;
600Sstevel@tonic-gate struct netconfig *tpconf;
610Sstevel@tonic-gate struct timeval wait_time;
620Sstevel@tonic-gate struct utsname u;
630Sstevel@tonic-gate static char *hostname;
640Sstevel@tonic-gate static bool_t first_time = TRUE;
650Sstevel@tonic-gate
66*8438SShawn.Emery@Sun.COM /*
67*8438SShawn.Emery@Sun.COM * Total timeout (in seconds) talking to kwarnd.
68*8438SShawn.Emery@Sun.COM */
69*8438SShawn.Emery@Sun.COM #define TOTAL_TIMEOUT 5
700Sstevel@tonic-gate
71*8438SShawn.Emery@Sun.COM if (kwarn_clnt)
72*8438SShawn.Emery@Sun.COM return (kwarn_clnt);
730Sstevel@tonic-gate if (!(localhandle = setnetconfig()))
740Sstevel@tonic-gate return (NULL);
750Sstevel@tonic-gate tpconf = NULL;
760Sstevel@tonic-gate if (first_time == TRUE) {
770Sstevel@tonic-gate if (uname(&u) == -1) {
780Sstevel@tonic-gate (void) endnetconfig(localhandle);
790Sstevel@tonic-gate return ((CLIENT *)NULL);
800Sstevel@tonic-gate }
810Sstevel@tonic-gate if ((hostname = strdup(u.nodename)) == (char *)NULL) {
820Sstevel@tonic-gate (void) endnetconfig(localhandle);
830Sstevel@tonic-gate return ((CLIENT *)NULL);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate first_time = FALSE;
860Sstevel@tonic-gate }
870Sstevel@tonic-gate while (nconf = getnetconfig(localhandle)) {
880Sstevel@tonic-gate if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
890Sstevel@tonic-gate if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
90*8438SShawn.Emery@Sun.COM kwarn_clnt = clnt_tp_create(hostname,
91*8438SShawn.Emery@Sun.COM KWARNPROG, KWARNVERS, nconf);
92*8438SShawn.Emery@Sun.COM if (kwarn_clnt) {
930Sstevel@tonic-gate dprt("got COTS_ORD\n");
940Sstevel@tonic-gate break;
950Sstevel@tonic-gate }
960Sstevel@tonic-gate } else {
970Sstevel@tonic-gate tpconf = nconf;
980Sstevel@tonic-gate }
990Sstevel@tonic-gate }
1000Sstevel@tonic-gate }
101*8438SShawn.Emery@Sun.COM if ((kwarn_clnt == NULL) && (tpconf)) {
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate /* Now, try the connection-oriented loopback transport */
1040Sstevel@tonic-gate
105*8438SShawn.Emery@Sun.COM kwarn_clnt = clnt_tp_create(hostname, KWARNPROG, KWARNVERS,
106*8438SShawn.Emery@Sun.COM tpconf);
1070Sstevel@tonic-gate #ifdef DEBUG
108*8438SShawn.Emery@Sun.COM if (kwarn_clnt) {
1090Sstevel@tonic-gate dprt("got COTS\n");
1100Sstevel@tonic-gate }
1110Sstevel@tonic-gate #endif /* DEBUG */
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate (void) endnetconfig(localhandle);
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate /*
1160Sstevel@tonic-gate * This bit of code uses an as yet unimplemented argument to
1170Sstevel@tonic-gate * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
1180Sstevel@tonic-gate * loopback transport should be checked to ensure it is
1190Sstevel@tonic-gate * connected to a process running as root. If so, the clnt_control()
1200Sstevel@tonic-gate * call returns TRUE. If not, it returns FALSE.
1210Sstevel@tonic-gate */
1220Sstevel@tonic-gate
1230Sstevel@tonic-gate #ifdef CLSET_SVC_PRIV
1240Sstevel@tonic-gate
125*8438SShawn.Emery@Sun.COM if (clnt_control(kwarn_clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
126*8438SShawn.Emery@Sun.COM clnt_destroy(kwarn_clnt);
127*8438SShawn.Emery@Sun.COM kwarn_clnt = NULL;
1280Sstevel@tonic-gate return (NULL);
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate #endif
131*8438SShawn.Emery@Sun.COM if (kwarn_clnt == NULL)
1320Sstevel@tonic-gate return (NULL);
1330Sstevel@tonic-gate
134*8438SShawn.Emery@Sun.COM kwarn_clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
135*8438SShawn.Emery@Sun.COM if (kwarn_clnt->cl_auth == NULL) {
136*8438SShawn.Emery@Sun.COM clnt_destroy(kwarn_clnt);
137*8438SShawn.Emery@Sun.COM kwarn_clnt = NULL;
1380Sstevel@tonic-gate return (NULL);
1390Sstevel@tonic-gate }
140*8438SShawn.Emery@Sun.COM wait_time.tv_sec = TOTAL_TIMEOUT;
1410Sstevel@tonic-gate wait_time.tv_usec = 0;
142*8438SShawn.Emery@Sun.COM (void) clnt_control(kwarn_clnt, CLSET_TIMEOUT, (char *)&wait_time);
143*8438SShawn.Emery@Sun.COM
144*8438SShawn.Emery@Sun.COM return (kwarn_clnt);
145*8438SShawn.Emery@Sun.COM }
1460Sstevel@tonic-gate
147*8438SShawn.Emery@Sun.COM void
148*8438SShawn.Emery@Sun.COM resetkwarnd_handle(void)
149*8438SShawn.Emery@Sun.COM {
150*8438SShawn.Emery@Sun.COM auth_destroy(kwarn_clnt->cl_auth);
151*8438SShawn.Emery@Sun.COM clnt_destroy(kwarn_clnt);
152*8438SShawn.Emery@Sun.COM kwarn_clnt = NULL;
1530Sstevel@tonic-gate }
154