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