17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57d85b7b5Srupertk  * Common Development and Distribution License (the "License").
67d85b7b5Srupertk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2161961e0fSrobinson 
227c478bd9Sstevel@tonic-gate /*
2336e852a1SRaja Andra  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This contains miscellaneous functions moved from commands to the library.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #include "mt.h"
327c478bd9Sstevel@tonic-gate #include <stdlib.h>
337c478bd9Sstevel@tonic-gate #include <stdio.h>
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
387c478bd9Sstevel@tonic-gate #include <rpc/rpc.h>
397c478bd9Sstevel@tonic-gate #include <rpcsvc/nis.h>
407c478bd9Sstevel@tonic-gate #include <rpcsvc/nis_dhext.h>
417c478bd9Sstevel@tonic-gate #include <rpc/auth.h>
427c478bd9Sstevel@tonic-gate #include <rpc/auth_sys.h>
437c478bd9Sstevel@tonic-gate #include <rpc/auth_des.h>
447c478bd9Sstevel@tonic-gate #include <rpc/key_prot.h>
457c478bd9Sstevel@tonic-gate #include <netdir.h>
467c478bd9Sstevel@tonic-gate #include <netconfig.h>
477c478bd9Sstevel@tonic-gate #include <sys/socket.h>
487c478bd9Sstevel@tonic-gate #include <netinet/in.h>
497c478bd9Sstevel@tonic-gate #include <netdb.h>
507c478bd9Sstevel@tonic-gate #include <dlfcn.h>
517c478bd9Sstevel@tonic-gate #include <gssapi/gssapi.h>
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate extern int bin2hex(int len, unsigned char *binnum, char *hexnum);
547c478bd9Sstevel@tonic-gate extern int hex2bin(int len, char *hexnum, char *binnum);
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX1	"/usr/lib/"
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate #ifdef  _LP64
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX2	"64/"
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate #else   /* _LP64 */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX2	""
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #endif  /* _LP64 */
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	MECH_LIB_DIR		"gss/"
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #define	MECH_LIB_PREFIX MECH_LIB_PREFIX1 MECH_LIB_PREFIX2
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate #define	MECHDH		MECH_LIB_PREFIX MECH_LIB_DIR "mech_dh.so.1"
737c478bd9Sstevel@tonic-gate #define	LIBGSS		MECH_LIB_PREFIX "libgss.so.1"
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static gss_OID_desc __dh_gss_c_nt_netname = {
767c478bd9Sstevel@tonic-gate 	9,  "\053\006\004\001\052\002\032\001\001"
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate mutex_t gss_load_lock = DEFAULTMUTEX;
807c478bd9Sstevel@tonic-gate static gss_OID GSS_EXPORT_NAME = 0;
817c478bd9Sstevel@tonic-gate static gss_OID DH_NETNAME = &__dh_gss_c_nt_netname;
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate typedef OM_uint32 (*gss_fptr)();
847c478bd9Sstevel@tonic-gate OM_uint32 (*g_import_name)();
857c478bd9Sstevel@tonic-gate OM_uint32 (*g_display_name)();
867c478bd9Sstevel@tonic-gate OM_uint32 (*g_release_name)();
877c478bd9Sstevel@tonic-gate OM_uint32 (*g_release_buffer)();
887c478bd9Sstevel@tonic-gate OM_uint32 (*g_release_oid)();
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate /*
917c478bd9Sstevel@tonic-gate  * gss_OID_load()
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * This routine is called by __nis_gssprin2netname to define values for
947c478bd9Sstevel@tonic-gate  * the gss-api-export-name OID, the Diffie-Hellman netname OID, and
957c478bd9Sstevel@tonic-gate  * the gss support routines that it needs.
967c478bd9Sstevel@tonic-gate  * The reason for this support routine is that libnsl cannot have an
977c478bd9Sstevel@tonic-gate  * explicit dependency on libgss. Callers of __nisgssprin2netname are
987c478bd9Sstevel@tonic-gate  * expected to have loaded libgss through the rpcsec layer. The work around
997c478bd9Sstevel@tonic-gate  * is to dlopen the needed shared objects and grab the symbols with dlsym.
1007c478bd9Sstevel@tonic-gate  * This routine opens libgss RTLD_NOLOAD. If this fails then libgss.so.1
1017c478bd9Sstevel@tonic-gate  * is not loaded and we return error. Otherwise it uses dlsym to
1027c478bd9Sstevel@tonic-gate  * defines GSS_EXPORT_NAME to have the value of GSS_C_NT_EXPORT_NAME and
1037c478bd9Sstevel@tonic-gate  * to assign the above fuction pointers.
1047c478bd9Sstevel@tonic-gate  * If this succeeds then the routine will attempt to load mech_dh.so.1
1057c478bd9Sstevel@tonic-gate  * and over ride DH_NETNAME with the value of __DH_GSS_C_NT_NETNAME from
1067c478bd9Sstevel@tonic-gate  * that shared object. We don't consider it an error if this fails because
1077c478bd9Sstevel@tonic-gate  * its conceivable that another mechanism backend will support the netname
1087c478bd9Sstevel@tonic-gate  * name type and mech_dh.so.1 not be available.
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * Return 0 on failer, 1 on success.
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate static int
gss_OID_load()1147c478bd9Sstevel@tonic-gate gss_OID_load()
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 	void *dh;
1177c478bd9Sstevel@tonic-gate 	gss_OID *OIDptr;
1187c478bd9Sstevel@tonic-gate 	int stat = 0;
1197c478bd9Sstevel@tonic-gate 
12061961e0fSrobinson 	(void) mutex_lock(&gss_load_lock);
1217c478bd9Sstevel@tonic-gate 	if (GSS_EXPORT_NAME) {
12261961e0fSrobinson 		(void) mutex_unlock(&gss_load_lock);
1237c478bd9Sstevel@tonic-gate 		return (0);
1247c478bd9Sstevel@tonic-gate 	}
1257c478bd9Sstevel@tonic-gate 
1267c478bd9Sstevel@tonic-gate 	/* if LIBGSS is not loaded return an error */
1277c478bd9Sstevel@tonic-gate 	if ((dh = dlopen(LIBGSS, RTLD_NOLOAD)) == NULL) {
12861961e0fSrobinson 		(void) mutex_unlock(&gss_load_lock);
1297c478bd9Sstevel@tonic-gate 		return (0);
1307c478bd9Sstevel@tonic-gate 	}
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	OIDptr = (gss_OID *)dlsym(dh, "GSS_C_NT_EXPORT_NAME");
1337c478bd9Sstevel@tonic-gate 	if (OIDptr)
1347c478bd9Sstevel@tonic-gate 		GSS_EXPORT_NAME = *OIDptr;
1357c478bd9Sstevel@tonic-gate 	else
1367c478bd9Sstevel@tonic-gate 		goto Done;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	g_import_name = (gss_fptr)dlsym(dh, "gss_import_name");
1397c478bd9Sstevel@tonic-gate 	if (g_import_name == 0)
1407c478bd9Sstevel@tonic-gate 		goto Done;
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	g_display_name = (gss_fptr)dlsym(dh, "gss_display_name");
1437c478bd9Sstevel@tonic-gate 	if (g_display_name == 0)
1447c478bd9Sstevel@tonic-gate 		goto Done;
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	g_release_name = (gss_fptr)dlsym(dh, "gss_release_name");
1477c478bd9Sstevel@tonic-gate 	if (g_release_name == 0)
1487c478bd9Sstevel@tonic-gate 		goto Done;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 	g_release_buffer = (gss_fptr)dlsym(dh, "gss_release_buffer");
1517c478bd9Sstevel@tonic-gate 	if (g_release_buffer == 0)
1527c478bd9Sstevel@tonic-gate 		goto Done;
1537c478bd9Sstevel@tonic-gate 
1547c478bd9Sstevel@tonic-gate 	g_release_oid = (gss_fptr)dlsym(dh, "gss_release_oid");
1557c478bd9Sstevel@tonic-gate 	if (g_release_oid == 0)
1567c478bd9Sstevel@tonic-gate 		goto Done;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	stat = 1;
1597c478bd9Sstevel@tonic-gate 	/*
1607c478bd9Sstevel@tonic-gate 	 * Try and get the official netname oid from mech_dh.so.
1617c478bd9Sstevel@tonic-gate 	 * If this fails will just keep our default from above.
1627c478bd9Sstevel@tonic-gate 	 */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	if ((dh = dlopen(MECHDH, RTLD_LAZY)) != NULL) {
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 		OIDptr = (gss_OID *)dlsym(dh, "__DH_GSS_C_NT_NETNAME");
1677c478bd9Sstevel@tonic-gate 		if (OIDptr)
1687c478bd9Sstevel@tonic-gate 			DH_NETNAME = *OIDptr;
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate Done:
17261961e0fSrobinson 	(void) mutex_unlock(&gss_load_lock);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	if (stat == 0)
1757c478bd9Sstevel@tonic-gate 		GSS_EXPORT_NAME = 0;
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	return (stat);
1787c478bd9Sstevel@tonic-gate }
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * int
1837c478bd9Sstevel@tonic-gate  * __nis_gssprin2netname(rpc_gss_principal_t prin,
1847c478bd9Sstevel@tonic-gate  *			 char netname[MAXNETNAMELEN+1])
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * This routine attempts to extract the netname from an rpc_gss_principal_t
1877c478bd9Sstevel@tonic-gate  * which is in { gss-api-exorted-name } format. Return 0 if a netname was
1887c478bd9Sstevel@tonic-gate  * found, else return -1.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate /*
1927c478bd9Sstevel@tonic-gate  * This routine has a dependency on libgss.so. So we will pragma weak
1937c478bd9Sstevel@tonic-gate  * the interfaces that we need. When this routine is called libgss
1947c478bd9Sstevel@tonic-gate  * should have been loaded by the rpcsec layer. We will call gss_OID_load
1957c478bd9Sstevel@tonic-gate  * to get the value for GSS_EXPORT_NAME. If gss_OID_load failes return -1.
1967c478bd9Sstevel@tonic-gate  */
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate #define	OID_IS_EQUAL(o1, o2) ((o1) && (o2) && \
1997c478bd9Sstevel@tonic-gate 	((o1)->length == (o2)->length) && \
2007c478bd9Sstevel@tonic-gate 	(memcmp((o1)->elements, (o2)->elements, (o1)->length) == 0))
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate int
__nis_gssprin2netname(rpc_gss_principal_t prin,char netname[])203*89fbfe0dSToomas Soome __nis_gssprin2netname(rpc_gss_principal_t prin, char netname[])
2047c478bd9Sstevel@tonic-gate {
2057c478bd9Sstevel@tonic-gate 	gss_buffer_desc display_name;
2067c478bd9Sstevel@tonic-gate 	gss_name_t name;
2077c478bd9Sstevel@tonic-gate 	gss_OID name_type;
2087c478bd9Sstevel@tonic-gate 	gss_buffer_desc expName;
2097c478bd9Sstevel@tonic-gate 	int stat = -1;
2107c478bd9Sstevel@tonic-gate 	OM_uint32 major, minor;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	/* See if we already got the OID */
2137c478bd9Sstevel@tonic-gate 	if (GSS_EXPORT_NAME == 0) {
2147c478bd9Sstevel@tonic-gate 		/* Nope. See if GSS is loaded and get the OIDs */
2157c478bd9Sstevel@tonic-gate 		if (!gss_OID_load())
2167c478bd9Sstevel@tonic-gate 			return (-1);	/* if libgss.so.1 isn't loaded */
2177c478bd9Sstevel@tonic-gate 	}
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	expName.length = prin->len;
2207c478bd9Sstevel@tonic-gate 	expName.value = prin->name;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	major = (*g_import_name)(&minor, &expName,
22336e852a1SRaja Andra 	    (gss_OID) GSS_EXPORT_NAME, &name);
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	if (major == GSS_S_COMPLETE) {
2267c478bd9Sstevel@tonic-gate 		major = (*g_display_name)(&minor, name,
22736e852a1SRaja Andra 		    &display_name, &name_type);
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 		/* We're done with the gss_internal name */
2307c478bd9Sstevel@tonic-gate 		(void) (*g_release_name)(&minor, &name);
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 		if (major == GSS_S_COMPLETE) {
2337c478bd9Sstevel@tonic-gate 			/*
2347c478bd9Sstevel@tonic-gate 			 * Check if we've got a netname. If we do we copy it
2357c478bd9Sstevel@tonic-gate 			 * and make sure that its null terminated.
2367c478bd9Sstevel@tonic-gate 			 */
2377c478bd9Sstevel@tonic-gate 			if (OID_IS_EQUAL(DH_NETNAME, name_type)) {
23861961e0fSrobinson 				(void) strncpy(netname,
23936e852a1SRaja Andra 				    (char *)display_name.value,
24036e852a1SRaja Andra 				    MAXNETNAMELEN);
2417c478bd9Sstevel@tonic-gate 				netname[MAXNETNAMELEN] = '\0';
2427c478bd9Sstevel@tonic-gate 				stat = 0;
2437c478bd9Sstevel@tonic-gate 			}
2447c478bd9Sstevel@tonic-gate 			/*
2457c478bd9Sstevel@tonic-gate 			 * If there are other display formats that can
2467c478bd9Sstevel@tonic-gate 			 * be converted to netnames easily, insert here.
2477c478bd9Sstevel@tonic-gate 			 *
2487c478bd9Sstevel@tonic-gate 			 * else if (OID_IS_EQUAL(OTHER_NT_OID, name_type)) {
2497c478bd9Sstevel@tonic-gate 			 *	convert2netname(display_name.value, netname);
2507c478bd9Sstevel@tonic-gate 			 * } ...
2517c478bd9Sstevel@tonic-gate 			 */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 			/* Release temporty storage */
2547c478bd9Sstevel@tonic-gate 			(void) (*g_release_buffer)(&minor, &display_name);
2557c478bd9Sstevel@tonic-gate 			(void) (*g_release_oid)(&minor, &name_type);
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	if (stat == 0)
2607c478bd9Sstevel@tonic-gate 		return (stat);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	return (stat);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate /*
2667c478bd9Sstevel@tonic-gate  * Extract a public key given a key length and alg. type from a packed
2677c478bd9Sstevel@tonic-gate  * netobj containing extended Diffie-Hellman keys.
2687c478bd9Sstevel@tonic-gate  */
2697c478bd9Sstevel@tonic-gate char *
__nis_dhext_extract_pkey(netobj * no,keylen_t keylen,algtype_t algtype)2707c478bd9Sstevel@tonic-gate __nis_dhext_extract_pkey(netobj *no, keylen_t keylen, algtype_t algtype)
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	char		*hexkey;
27361961e0fSrobinson 	/* LINTED pointer cast */
2747c478bd9Sstevel@tonic-gate 	extdhkey_t	*keyent = (extdhkey_t *)no->n_bytes;
2757c478bd9Sstevel@tonic-gate 
27661961e0fSrobinson 	/* LINTED pointer cast */
2777c478bd9Sstevel@tonic-gate 	while (keyent < (extdhkey_t *)(no->n_bytes + no->n_len)) {
2787c478bd9Sstevel@tonic-gate 		char	*keyoffset;
2797c478bd9Sstevel@tonic-gate 		size_t	binlen = (ntohs(keyent->keylen) + 7) / 8;
2807c478bd9Sstevel@tonic-gate 		size_t	binpadlen = ((binlen + 3) / 4) * 4;
2817c478bd9Sstevel@tonic-gate 		size_t	hexkeylen = binlen * 2 + 1;
2827c478bd9Sstevel@tonic-gate 
2837c478bd9Sstevel@tonic-gate 		if (keylen == ntohs(keyent->keylen) &&
2847c478bd9Sstevel@tonic-gate 		    algtype == ntohs(keyent->algtype)) {
2857c478bd9Sstevel@tonic-gate 
28661961e0fSrobinson 			if (!(hexkey = malloc(hexkeylen)))
2877c478bd9Sstevel@tonic-gate 				return (NULL);
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate 			(void) bin2hex(binlen, keyent->key, hexkey);
2907c478bd9Sstevel@tonic-gate 			return (hexkey);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 		keyoffset = (char *)keyent + (sizeof (ushort_t) * 2) +
29336e852a1SRaja Andra 		    binpadlen;
29461961e0fSrobinson 		/* LINTED pointer cast */
2957c478bd9Sstevel@tonic-gate 		keyent = (extdhkey_t *)keyoffset;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	return (NULL);
2987c478bd9Sstevel@tonic-gate }
299