xref: /illumos-gate/usr/src/cmd/gss/gssd/gssd_handle.c (revision 8b80e8cb6855118d46f605e91b5ed4ce83417395)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  *
22  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  *
25  * Copyright (c) 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
26  * All Rights Reserved
27  *
28  * Portions of this source code were derived from Berkeley
29  * 4.3 BSD under license from the Regents of the University of
30  * California.
31  */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 /*	from kerbd_handle.c	1.3	92/01/29 SMI */
35 
36 /*
37  * gssd_handle.c, Interface to gssd
38  *
39  */
40 
41 #include <unistd.h>
42 #include <rpc/rpc.h>
43 #include <rpc/clnt.h>
44 #include <stdio.h>
45 #include <string.h>
46 #include <netconfig.h>
47 #include <sys/utsname.h>
48 #include "gssd.h"
49 
50 #ifdef DEBUG
51 #define	dprt(msg)	(void) fprintf(stderr, "%s\n", msg);
52 #else
53 #define	dprt(msg)
54 #endif /* DEBUG */
55 
56 
57 /*
58  * Keep the handle cached.  This call may be made quite often.
59  */
60 
61 CLIENT *
62 getgssd_handle()
63 {
64 	void *localhandle;
65 	struct netconfig *nconf;
66 	struct netconfig *tpconf;
67 	static CLIENT *clnt;
68 	struct timeval wait_time;
69 	struct utsname u;
70 	static char *hostname;
71 	static bool_t first_time = TRUE;
72 
73 #define	TOTAL_TIMEOUT	1000	/* total timeout talking to gssd */
74 #define	TOTAL_TRIES	1	/* Number of tries */
75 
76 	if (clnt)
77 		return (clnt);
78 	if (!(localhandle = setnetconfig()))
79 		return (NULL);
80 	tpconf = NULL;
81 	if (first_time == TRUE) {
82 		if (uname(&u) == -1)
83 			return ((CLIENT *) NULL);
84 		if ((hostname = strdup(u.nodename)) == (char *)NULL)
85 			return ((CLIENT *) NULL);
86 		first_time = FALSE;
87 	}
88 	while (nconf = getnetconfig(localhandle)) {
89 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
90 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
91 				clnt = clnt_tp_create(hostname,
92 					GSSPROG, GSSVERS, nconf);
93 				if (clnt) {
94 					dprt("got COTS_ORD\n");
95 					break;
96 				}
97 			} else {
98 				tpconf = nconf;
99 			}
100 		}
101 	}
102 	if ((clnt == NULL) && (tpconf)) {
103 
104 		/* Now, try the connection-oriented loopback transport */
105 
106 		clnt = clnt_tp_create(hostname, GSSPROG, GSSVERS, tpconf);
107 #ifdef DEBUG
108 		if (clnt) {
109 			dprt("got COTS\n");
110 		}
111 #endif /* DEBUG */
112 	}
113 	endnetconfig(localhandle);
114 
115 	/*
116 	 * This bit of code uses an as yet unimplemented argument to
117 	 * clnt_control(). CLSET_SVC_PRIV specifies that the underlying
118 	 * loopback transport should be checked to ensure it is
119 	 * connected to a process running as root. If so, the clnt_control()
120 	 * call returns TRUE. If not, it returns FALSE.
121 	 */
122 
123 #ifdef CLSET_SVC_PRIV
124 
125 	if (clnt_control(clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
126 		clnt_destroy(clnt);
127 		clnt = NULL;
128 		return (NULL);
129 	{
130 #endif
131 	if (clnt == NULL)
132 		return (NULL);
133 
134 	clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
135 	if (clnt->cl_auth == NULL) {
136 		clnt_destroy(clnt);
137 		clnt = NULL;
138 		return (NULL);
139 	}
140 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
141 	wait_time.tv_usec = 0;
142 	(void) clnt_control(clnt, CLSET_RETRY_TIMEOUT, (char *)&wait_time);
143 
144 	return (clnt);
145 }
146