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 2005 Sun Microsystems, Inc.  All rights reserved.
23*0Sstevel@tonic-gate  * Use is subject to license terms.
24*0Sstevel@tonic-gate  *
25*0Sstevel@tonic-gate  * Copyright (c) 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
26*0Sstevel@tonic-gate  * All Rights Reserved
27*0Sstevel@tonic-gate  *
28*0Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
29*0Sstevel@tonic-gate  * 4.3 BSD under license from the Regents of the University of
30*0Sstevel@tonic-gate  * California.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
34*0Sstevel@tonic-gate /*	from kerbd_handle.c	1.3	92/01/29 SMI */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate /*
37*0Sstevel@tonic-gate  * gssd_handle.c, Interface to gssd
38*0Sstevel@tonic-gate  *
39*0Sstevel@tonic-gate  */
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #include <unistd.h>
42*0Sstevel@tonic-gate #include <rpc/rpc.h>
43*0Sstevel@tonic-gate #include <rpc/clnt.h>
44*0Sstevel@tonic-gate #include <stdio.h>
45*0Sstevel@tonic-gate #include <string.h>
46*0Sstevel@tonic-gate #include <netconfig.h>
47*0Sstevel@tonic-gate #include <sys/utsname.h>
48*0Sstevel@tonic-gate #include "gssd.h"
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #ifdef DEBUG
51*0Sstevel@tonic-gate #define	dprt(msg)	(void) fprintf(stderr, "%s\n", msg);
52*0Sstevel@tonic-gate #else
53*0Sstevel@tonic-gate #define	dprt(msg)
54*0Sstevel@tonic-gate #endif /* DEBUG */
55*0Sstevel@tonic-gate 
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * Keep the handle cached.  This call may be made quite often.
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate CLIENT *
62*0Sstevel@tonic-gate getgssd_handle()
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate 	void *localhandle;
65*0Sstevel@tonic-gate 	struct netconfig *nconf;
66*0Sstevel@tonic-gate 	struct netconfig *tpconf;
67*0Sstevel@tonic-gate 	static CLIENT *clnt;
68*0Sstevel@tonic-gate 	struct timeval wait_time;
69*0Sstevel@tonic-gate 	struct utsname u;
70*0Sstevel@tonic-gate 	static char *hostname;
71*0Sstevel@tonic-gate 	static bool_t first_time = TRUE;
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate #define	TOTAL_TIMEOUT	1000	/* total timeout talking to gssd */
74*0Sstevel@tonic-gate #define	TOTAL_TRIES	1	/* Number of tries */
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if (clnt)
77*0Sstevel@tonic-gate 		return (clnt);
78*0Sstevel@tonic-gate 	if (!(localhandle = setnetconfig()))
79*0Sstevel@tonic-gate 		return (NULL);
80*0Sstevel@tonic-gate 	tpconf = NULL;
81*0Sstevel@tonic-gate 	if (first_time == TRUE) {
82*0Sstevel@tonic-gate 		if (uname(&u) == -1)
83*0Sstevel@tonic-gate 			return ((CLIENT *) NULL);
84*0Sstevel@tonic-gate 		if ((hostname = strdup(u.nodename)) == (char *) NULL)
85*0Sstevel@tonic-gate 			return ((CLIENT *) NULL);
86*0Sstevel@tonic-gate 		first_time = FALSE;
87*0Sstevel@tonic-gate 	}
88*0Sstevel@tonic-gate 	while (nconf = getnetconfig(localhandle)) {
89*0Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
90*0Sstevel@tonic-gate 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
91*0Sstevel@tonic-gate 				clnt = clnt_tp_create(hostname,
92*0Sstevel@tonic-gate 					GSSPROG, GSSVERS, nconf);
93*0Sstevel@tonic-gate 				if (clnt) {
94*0Sstevel@tonic-gate 					dprt("got COTS_ORD\n");
95*0Sstevel@tonic-gate 					break;
96*0Sstevel@tonic-gate 				}
97*0Sstevel@tonic-gate 			} else {
98*0Sstevel@tonic-gate 				tpconf = nconf;
99*0Sstevel@tonic-gate 			}
100*0Sstevel@tonic-gate 		}
101*0Sstevel@tonic-gate 	}
102*0Sstevel@tonic-gate 	if ((clnt == NULL) && (tpconf)) {
103*0Sstevel@tonic-gate 
104*0Sstevel@tonic-gate 		/* Now, try the connection-oriented loopback transport */
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate 		clnt = clnt_tp_create(hostname, GSSPROG, GSSVERS, tpconf);
107*0Sstevel@tonic-gate #ifdef DEBUG
108*0Sstevel@tonic-gate 		if (clnt) {
109*0Sstevel@tonic-gate 			dprt("got COTS\n");
110*0Sstevel@tonic-gate 		}
111*0Sstevel@tonic-gate #endif DEBUG
112*0Sstevel@tonic-gate 	}
113*0Sstevel@tonic-gate 	endnetconfig(localhandle);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 	/*
116*0Sstevel@tonic-gate 	 * This bit of code uses an as yet unimplemented argument to
117*0Sstevel@tonic-gate 	 * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
118*0Sstevel@tonic-gate 	 * loopback transport should be checked to ensure it is
119*0Sstevel@tonic-gate 	 * connected to a process running as root. If so, the clnt_control()
120*0Sstevel@tonic-gate 	 * call returns TRUE. If not, it returns FALSE.
121*0Sstevel@tonic-gate 	 */
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate #ifdef CLSET_SVC_PRIV
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate 	if (clnt_control(clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
126*0Sstevel@tonic-gate 		clnt_destroy(clnt);
127*0Sstevel@tonic-gate 		clnt = NULL;
128*0Sstevel@tonic-gate 		return (NULL);
129*0Sstevel@tonic-gate 	{
130*0Sstevel@tonic-gate #endif
131*0Sstevel@tonic-gate 	if (clnt == NULL)
132*0Sstevel@tonic-gate 		return (NULL);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
135*0Sstevel@tonic-gate 	if (clnt->cl_auth == NULL) {
136*0Sstevel@tonic-gate 		clnt_destroy(clnt);
137*0Sstevel@tonic-gate 		clnt = NULL;
138*0Sstevel@tonic-gate 		return (NULL);
139*0Sstevel@tonic-gate 	}
140*0Sstevel@tonic-gate 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
141*0Sstevel@tonic-gate 	wait_time.tv_usec = 0;
142*0Sstevel@tonic-gate 	(void) clnt_control(clnt, CLSET_RETRY_TIMEOUT, (char *)&wait_time);
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate 	return (clnt);
145*0Sstevel@tonic-gate }
146