xref: /onnv-gate/usr/src/cmd/krb5/kwarn/kwarnd_handle.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*0Sstevel@tonic-gate  * Use is subject to license terms.
4*0Sstevel@tonic-gate  */
5*0Sstevel@tonic-gate 
6*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*0Sstevel@tonic-gate /*	from kerbd_handle.c	1.3	92/01/29 SMI */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate /*
10*0Sstevel@tonic-gate  * kwarnd_handle.c, Interface to kwarnd
11*0Sstevel@tonic-gate  *
12*0Sstevel@tonic-gate  */
13*0Sstevel@tonic-gate 
14*0Sstevel@tonic-gate #include <unistd.h>
15*0Sstevel@tonic-gate #include <rpc/rpc.h>
16*0Sstevel@tonic-gate #include <rpc/clnt.h>
17*0Sstevel@tonic-gate #include <stdio.h>
18*0Sstevel@tonic-gate #include <string.h>
19*0Sstevel@tonic-gate #include <netconfig.h>
20*0Sstevel@tonic-gate #include <sys/utsname.h>
21*0Sstevel@tonic-gate #include "kwarnd.h"
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate #ifdef DEBUG
24*0Sstevel@tonic-gate #define	dprt(msg)
25*0Sstevel@tonic-gate #else
26*0Sstevel@tonic-gate #define	dprt(msg)
27*0Sstevel@tonic-gate #endif /* DEBUG */
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * Keep the handle cached.  This call may be made quite often.
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate CLIENT *
35*0Sstevel@tonic-gate getkwarnd_handle(void)
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	void *localhandle;
38*0Sstevel@tonic-gate 	struct netconfig *nconf;
39*0Sstevel@tonic-gate 	struct netconfig *tpconf;
40*0Sstevel@tonic-gate 	static CLIENT *clnt;
41*0Sstevel@tonic-gate 	struct timeval wait_time;
42*0Sstevel@tonic-gate 	struct utsname u;
43*0Sstevel@tonic-gate 	static char *hostname;
44*0Sstevel@tonic-gate 	static bool_t first_time = TRUE;
45*0Sstevel@tonic-gate 
46*0Sstevel@tonic-gate #define	TOTAL_TIMEOUT	1000	/* total timeout talking to kwarnd */
47*0Sstevel@tonic-gate #define	TOTAL_TRIES	1	/* Number of tries */
48*0Sstevel@tonic-gate 
49*0Sstevel@tonic-gate 	if (clnt)
50*0Sstevel@tonic-gate 		return (clnt);
51*0Sstevel@tonic-gate 	if (!(localhandle = setnetconfig()))
52*0Sstevel@tonic-gate 		return (NULL);
53*0Sstevel@tonic-gate 	tpconf = NULL;
54*0Sstevel@tonic-gate 	if (first_time == TRUE) {
55*0Sstevel@tonic-gate 		if (uname(&u) == -1) {
56*0Sstevel@tonic-gate 			(void) endnetconfig(localhandle);
57*0Sstevel@tonic-gate 			return ((CLIENT *)NULL);
58*0Sstevel@tonic-gate 		}
59*0Sstevel@tonic-gate 		if ((hostname = strdup(u.nodename)) == (char *)NULL) {
60*0Sstevel@tonic-gate 			(void) endnetconfig(localhandle);
61*0Sstevel@tonic-gate 			return ((CLIENT *)NULL);
62*0Sstevel@tonic-gate 		}
63*0Sstevel@tonic-gate 		first_time = FALSE;
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate 	while (nconf = getnetconfig(localhandle)) {
66*0Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
67*0Sstevel@tonic-gate 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
68*0Sstevel@tonic-gate 				clnt = clnt_tp_create(hostname,
69*0Sstevel@tonic-gate 					KWARNPROG, KWARNVERS, nconf);
70*0Sstevel@tonic-gate 				if (clnt) {
71*0Sstevel@tonic-gate 					dprt("got COTS_ORD\n");
72*0Sstevel@tonic-gate 					break;
73*0Sstevel@tonic-gate 				}
74*0Sstevel@tonic-gate 			} else {
75*0Sstevel@tonic-gate 				tpconf = nconf;
76*0Sstevel@tonic-gate 			}
77*0Sstevel@tonic-gate 		}
78*0Sstevel@tonic-gate 	}
79*0Sstevel@tonic-gate 	if ((clnt == NULL) && (tpconf)) {
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 		/* Now, try the connection-oriented loopback transport */
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 		clnt = clnt_tp_create(hostname, KWARNPROG, KWARNVERS, tpconf);
84*0Sstevel@tonic-gate #ifdef DEBUG
85*0Sstevel@tonic-gate 		if (clnt) {
86*0Sstevel@tonic-gate 			dprt("got COTS\n");
87*0Sstevel@tonic-gate 		}
88*0Sstevel@tonic-gate #endif	/* DEBUG */
89*0Sstevel@tonic-gate 	}
90*0Sstevel@tonic-gate 	(void) endnetconfig(localhandle);
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	/*
93*0Sstevel@tonic-gate 	 * This bit of code uses an as yet unimplemented argument to
94*0Sstevel@tonic-gate 	 * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
95*0Sstevel@tonic-gate 	 * loopback transport should be checked to ensure it is
96*0Sstevel@tonic-gate 	 * connected to a process running as root. If so, the clnt_control()
97*0Sstevel@tonic-gate 	 * call returns TRUE. If not, it returns FALSE.
98*0Sstevel@tonic-gate 	 */
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate #ifdef CLSET_SVC_PRIV
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	if (clnt_control(clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
103*0Sstevel@tonic-gate 		clnt_destroy(clnt);
104*0Sstevel@tonic-gate 		clnt = NULL;
105*0Sstevel@tonic-gate 		return (NULL);
106*0Sstevel@tonic-gate 	{
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 	if (clnt == NULL)
109*0Sstevel@tonic-gate 		return (NULL);
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 	clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
112*0Sstevel@tonic-gate 	if (clnt->cl_auth == NULL) {
113*0Sstevel@tonic-gate 		clnt_destroy(clnt);
114*0Sstevel@tonic-gate 		clnt = NULL;
115*0Sstevel@tonic-gate 		return (NULL);
116*0Sstevel@tonic-gate 	}
117*0Sstevel@tonic-gate 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
118*0Sstevel@tonic-gate 	wait_time.tv_usec = 0;
119*0Sstevel@tonic-gate 	(void) clnt_control(clnt, CLSET_RETRY_TIMEOUT, (char *)&wait_time);
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate 	return (clnt);
122*0Sstevel@tonic-gate }
123