xref: /onnv-gate/usr/src/cmd/gss/gssd/gssd_handle.c (revision 1047:85e0466f58d4)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  *
220Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  *
250Sstevel@tonic-gate  * Copyright (c) 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T
260Sstevel@tonic-gate  * All Rights Reserved
270Sstevel@tonic-gate  *
280Sstevel@tonic-gate  * Portions of this source code were derived from Berkeley
29*1047Sgtb  * 4.3 BSD under license from the Regents of the University of
300Sstevel@tonic-gate  * California.
310Sstevel@tonic-gate  */
320Sstevel@tonic-gate 
330Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate /*	from kerbd_handle.c	1.3	92/01/29 SMI */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate /*
370Sstevel@tonic-gate  * gssd_handle.c, Interface to gssd
380Sstevel@tonic-gate  *
390Sstevel@tonic-gate  */
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #include <unistd.h>
420Sstevel@tonic-gate #include <rpc/rpc.h>
430Sstevel@tonic-gate #include <rpc/clnt.h>
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <string.h>
460Sstevel@tonic-gate #include <netconfig.h>
470Sstevel@tonic-gate #include <sys/utsname.h>
480Sstevel@tonic-gate #include "gssd.h"
490Sstevel@tonic-gate 
500Sstevel@tonic-gate #ifdef DEBUG
510Sstevel@tonic-gate #define	dprt(msg)	(void) fprintf(stderr, "%s\n", msg);
520Sstevel@tonic-gate #else
530Sstevel@tonic-gate #define	dprt(msg)
540Sstevel@tonic-gate #endif /* DEBUG */
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 
570Sstevel@tonic-gate /*
580Sstevel@tonic-gate  * Keep the handle cached.  This call may be made quite often.
590Sstevel@tonic-gate  */
600Sstevel@tonic-gate 
610Sstevel@tonic-gate CLIENT *
getgssd_handle()620Sstevel@tonic-gate getgssd_handle()
630Sstevel@tonic-gate {
640Sstevel@tonic-gate 	void *localhandle;
650Sstevel@tonic-gate 	struct netconfig *nconf;
660Sstevel@tonic-gate 	struct netconfig *tpconf;
670Sstevel@tonic-gate 	static CLIENT *clnt;
680Sstevel@tonic-gate 	struct timeval wait_time;
690Sstevel@tonic-gate 	struct utsname u;
700Sstevel@tonic-gate 	static char *hostname;
710Sstevel@tonic-gate 	static bool_t first_time = TRUE;
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #define	TOTAL_TIMEOUT	1000	/* total timeout talking to gssd */
740Sstevel@tonic-gate #define	TOTAL_TRIES	1	/* Number of tries */
750Sstevel@tonic-gate 
760Sstevel@tonic-gate 	if (clnt)
770Sstevel@tonic-gate 		return (clnt);
780Sstevel@tonic-gate 	if (!(localhandle = setnetconfig()))
790Sstevel@tonic-gate 		return (NULL);
800Sstevel@tonic-gate 	tpconf = NULL;
810Sstevel@tonic-gate 	if (first_time == TRUE) {
820Sstevel@tonic-gate 		if (uname(&u) == -1)
830Sstevel@tonic-gate 			return ((CLIENT *) NULL);
84*1047Sgtb 		if ((hostname = strdup(u.nodename)) == (char *)NULL)
850Sstevel@tonic-gate 			return ((CLIENT *) NULL);
860Sstevel@tonic-gate 		first_time = FALSE;
870Sstevel@tonic-gate 	}
880Sstevel@tonic-gate 	while (nconf = getnetconfig(localhandle)) {
890Sstevel@tonic-gate 		if (strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) {
900Sstevel@tonic-gate 			if (nconf->nc_semantics == NC_TPI_COTS_ORD) {
910Sstevel@tonic-gate 				clnt = clnt_tp_create(hostname,
920Sstevel@tonic-gate 					GSSPROG, GSSVERS, nconf);
930Sstevel@tonic-gate 				if (clnt) {
940Sstevel@tonic-gate 					dprt("got COTS_ORD\n");
950Sstevel@tonic-gate 					break;
960Sstevel@tonic-gate 				}
970Sstevel@tonic-gate 			} else {
980Sstevel@tonic-gate 				tpconf = nconf;
990Sstevel@tonic-gate 			}
1000Sstevel@tonic-gate 		}
1010Sstevel@tonic-gate 	}
1020Sstevel@tonic-gate 	if ((clnt == NULL) && (tpconf)) {
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		/* Now, try the connection-oriented loopback transport */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 		clnt = clnt_tp_create(hostname, GSSPROG, GSSVERS, tpconf);
1070Sstevel@tonic-gate #ifdef DEBUG
1080Sstevel@tonic-gate 		if (clnt) {
1090Sstevel@tonic-gate 			dprt("got COTS\n");
1100Sstevel@tonic-gate 		}
111*1047Sgtb #endif /* DEBUG */
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	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 
1250Sstevel@tonic-gate 	if (clnt_control(clnt, CLSET_SVC_PRIV, NULL) != TRUE) {
1260Sstevel@tonic-gate 		clnt_destroy(clnt);
1270Sstevel@tonic-gate 		clnt = NULL;
1280Sstevel@tonic-gate 		return (NULL);
1290Sstevel@tonic-gate 	{
1300Sstevel@tonic-gate #endif
1310Sstevel@tonic-gate 	if (clnt == NULL)
1320Sstevel@tonic-gate 		return (NULL);
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	clnt->cl_auth = authsys_create("", getuid(), 0, 0, NULL);
1350Sstevel@tonic-gate 	if (clnt->cl_auth == NULL) {
1360Sstevel@tonic-gate 		clnt_destroy(clnt);
1370Sstevel@tonic-gate 		clnt = NULL;
1380Sstevel@tonic-gate 		return (NULL);
1390Sstevel@tonic-gate 	}
1400Sstevel@tonic-gate 	wait_time.tv_sec = TOTAL_TIMEOUT/TOTAL_TRIES;
1410Sstevel@tonic-gate 	wait_time.tv_usec = 0;
1420Sstevel@tonic-gate 	(void) clnt_control(clnt, CLSET_RETRY_TIMEOUT, (char *)&wait_time);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	return (clnt);
1450Sstevel@tonic-gate }
146