17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
37c478bd9Sstevel@tonic-gate  * All rights reserved.
47c478bd9Sstevel@tonic-gate  */
57c478bd9Sstevel@tonic-gate 
67c478bd9Sstevel@tonic-gate /*
77c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
87c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
97c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
107c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
117c478bd9Sstevel@tonic-gate  *
127c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
137c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
147c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
157c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
167c478bd9Sstevel@tonic-gate  *
177c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
187c478bd9Sstevel@tonic-gate  * March 31, 1998.
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
217c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
227c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
237c478bd9Sstevel@tonic-gate  * Rights Reserved.
247c478bd9Sstevel@tonic-gate  *
257c478bd9Sstevel@tonic-gate  * Contributor(s):
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  *  Copyright (c) 1994 Regents of the University of Michigan.
297c478bd9Sstevel@tonic-gate  *  All rights reserved.
307c478bd9Sstevel@tonic-gate  */
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *  getdn.c
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include "ldap-int.h"
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate char *
387c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_get_dn(LDAP * ld,LDAPMessage * entry)397c478bd9Sstevel@tonic-gate ldap_get_dn( LDAP *ld, LDAPMessage *entry )
407c478bd9Sstevel@tonic-gate {
417c478bd9Sstevel@tonic-gate 	char			*dn;
427c478bd9Sstevel@tonic-gate 	struct berelement	tmp;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
477c478bd9Sstevel@tonic-gate 		return( NULL );		/* punt */
487c478bd9Sstevel@tonic-gate 	}
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
517c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
527c478bd9Sstevel@tonic-gate 		return( NULL );
537c478bd9Sstevel@tonic-gate 	}
547c478bd9Sstevel@tonic-gate 
557c478bd9Sstevel@tonic-gate 	tmp = *entry->lm_ber;	/* struct copy */
567c478bd9Sstevel@tonic-gate 	if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
577c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
587c478bd9Sstevel@tonic-gate 		return( NULL );
597c478bd9Sstevel@tonic-gate 	}
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate 	return( dn );
627c478bd9Sstevel@tonic-gate }
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate char *
657c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_dn2ufn(const char * dn)667c478bd9Sstevel@tonic-gate ldap_dn2ufn( const char *dn )
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	char	*p, *ufn, *r;
697c478bd9Sstevel@tonic-gate 	size_t	plen;
707c478bd9Sstevel@tonic-gate 	int	state;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
757c478bd9Sstevel@tonic-gate 		dn = "";
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
797c478bd9Sstevel@tonic-gate 		return( nsldapi_strdup( (char *)dn ));
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	ufn = nsldapi_strdup( ++p );
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define INQUOTE		1
847c478bd9Sstevel@tonic-gate #define OUTQUOTE	2
857c478bd9Sstevel@tonic-gate 	state = OUTQUOTE;
867c478bd9Sstevel@tonic-gate 	for ( p = ufn, r = ufn; *p; p += plen ) {
877c478bd9Sstevel@tonic-gate 	    plen = 1;
887c478bd9Sstevel@tonic-gate 		switch ( *p ) {
897c478bd9Sstevel@tonic-gate 		case '\\':
907c478bd9Sstevel@tonic-gate 			if ( *++p == '\0' )
917c478bd9Sstevel@tonic-gate 				plen=0;
927c478bd9Sstevel@tonic-gate 			else {
937c478bd9Sstevel@tonic-gate 				*r++ = '\\';
947c478bd9Sstevel@tonic-gate 				r += (plen = LDAP_UTF8COPY(r,p));
957c478bd9Sstevel@tonic-gate 			}
967c478bd9Sstevel@tonic-gate 			break;
977c478bd9Sstevel@tonic-gate 		case '"':
987c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
997c478bd9Sstevel@tonic-gate 				state = OUTQUOTE;
1007c478bd9Sstevel@tonic-gate 			else
1017c478bd9Sstevel@tonic-gate 				state = INQUOTE;
1027c478bd9Sstevel@tonic-gate 			*r++ = *p;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 		case ';':
1057c478bd9Sstevel@tonic-gate 		case ',':
1067c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE )
1077c478bd9Sstevel@tonic-gate 				*r++ = ',';
1087c478bd9Sstevel@tonic-gate 			else
1097c478bd9Sstevel@tonic-gate 				*r++ = *p;
1107c478bd9Sstevel@tonic-gate 			break;
1117c478bd9Sstevel@tonic-gate 		case '=':
1127c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
1137c478bd9Sstevel@tonic-gate 				*r++ = *p;
1147c478bd9Sstevel@tonic-gate 			else {
1157c478bd9Sstevel@tonic-gate 				char	*rsave = r;
1167c478bd9Sstevel@tonic-gate 				LDAP_UTF8DEC(r);
1177c478bd9Sstevel@tonic-gate 				*rsave = '\0';
1187c478bd9Sstevel@tonic-gate 				while ( !ldap_utf8isspace( r ) && *r != ';'
1197c478bd9Sstevel@tonic-gate 				    && *r != ',' && r > ufn )
1207c478bd9Sstevel@tonic-gate 					LDAP_UTF8DEC(r);
1217c478bd9Sstevel@tonic-gate 				LDAP_UTF8INC(r);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 				if ( strcasecmp( r, "c" )
1247c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "o" )
1257c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "ou" )
1267c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "st" )
1277c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "l" )
1287c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "dc" )
1297c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "uid" )
1307c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "cn" ) ) {
1317c478bd9Sstevel@tonic-gate 					r = rsave;
1327c478bd9Sstevel@tonic-gate 					*r++ = '=';
1337c478bd9Sstevel@tonic-gate 				}
1347c478bd9Sstevel@tonic-gate 			}
1357c478bd9Sstevel@tonic-gate 			break;
1367c478bd9Sstevel@tonic-gate 		default:
1377c478bd9Sstevel@tonic-gate 			r += (plen = LDAP_UTF8COPY(r,p));
1387c478bd9Sstevel@tonic-gate 			break;
1397c478bd9Sstevel@tonic-gate 		}
1407c478bd9Sstevel@tonic-gate 	}
1417c478bd9Sstevel@tonic-gate 	*r = '\0';
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	return( ufn );
1447c478bd9Sstevel@tonic-gate }
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate char **
1477c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_dns(const char * dn)1487c478bd9Sstevel@tonic-gate ldap_explode_dns( const char *dn )
1497c478bd9Sstevel@tonic-gate {
1507c478bd9Sstevel@tonic-gate 	int	ncomps, maxcomps;
1517c478bd9Sstevel@tonic-gate 	char	*s, *cpydn;
1527c478bd9Sstevel@tonic-gate 	char	**rdns;
1537c478bd9Sstevel@tonic-gate #ifdef HAVE_STRTOK_R	/* defined in portable.h */
1547c478bd9Sstevel@tonic-gate 	char	*lasts;
1557c478bd9Sstevel@tonic-gate #endif
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
1587c478bd9Sstevel@tonic-gate 		dn = "";
1597c478bd9Sstevel@tonic-gate 	}
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	if ( (rdns = (char **)NSLDAPI_MALLOC( 8 * sizeof(char *) )) == NULL ) {
1627c478bd9Sstevel@tonic-gate 		return( NULL );
1637c478bd9Sstevel@tonic-gate 	}
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate 	maxcomps = 8;
1667c478bd9Sstevel@tonic-gate 	ncomps = 0;
1677c478bd9Sstevel@tonic-gate 	cpydn = nsldapi_strdup( (char *)dn );
1687c478bd9Sstevel@tonic-gate 	for ( s = STRTOK( cpydn, "@.", &lasts ); s != NULL;
1697c478bd9Sstevel@tonic-gate 	    s = STRTOK( NULL, "@.", &lasts ) ) {
1707c478bd9Sstevel@tonic-gate 		if ( ncomps == maxcomps ) {
1717c478bd9Sstevel@tonic-gate 			maxcomps *= 2;
1727c478bd9Sstevel@tonic-gate 			if ( (rdns = (char **)NSLDAPI_REALLOC( rdns, maxcomps *
1737c478bd9Sstevel@tonic-gate 			    sizeof(char *) )) == NULL ) {
1747c478bd9Sstevel@tonic-gate 				NSLDAPI_FREE( cpydn );
1757c478bd9Sstevel@tonic-gate 				return( NULL );
1767c478bd9Sstevel@tonic-gate 			}
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		rdns[ncomps++] = nsldapi_strdup( s );
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 	rdns[ncomps] = NULL;
1817c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE( cpydn );
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	return( rdns );
1847c478bd9Sstevel@tonic-gate }
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate #define LDAP_DN		1
1877c478bd9Sstevel@tonic-gate #define LDAP_RDN	2
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate static char **
ldap_explode(const char * dn,const int notypes,const int nametype)1907c478bd9Sstevel@tonic-gate ldap_explode( const char *dn, const int notypes, const int nametype )
1917c478bd9Sstevel@tonic-gate {
1927c478bd9Sstevel@tonic-gate 	char	*p, *q, *rdnstart, **rdns = NULL;
1937c478bd9Sstevel@tonic-gate 	size_t	plen = 0;
1947c478bd9Sstevel@tonic-gate 	int	state, count = 0, endquote, len, goteq;
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_explode\n", 0, 0, 0 );
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
1997c478bd9Sstevel@tonic-gate 		dn = "";
2007c478bd9Sstevel@tonic-gate 	}
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate #if 0
2037c478bd9Sstevel@tonic-gate 	if ( ldap_is_dns_dn( dn ) ) {
2047c478bd9Sstevel@tonic-gate 		return( ldap_explode_dns( dn ) );
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	while ( ldap_utf8isspace( (char *)dn )) { /* ignore leading spaces */
2097c478bd9Sstevel@tonic-gate 		++dn;
2107c478bd9Sstevel@tonic-gate 	}
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	p = rdnstart = (char *) dn;
2137c478bd9Sstevel@tonic-gate 	state = OUTQUOTE;
2147c478bd9Sstevel@tonic-gate 	goteq = 0;
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate 	do {
2177c478bd9Sstevel@tonic-gate 		p += plen;
2187c478bd9Sstevel@tonic-gate 		plen = 1;
2197c478bd9Sstevel@tonic-gate 		switch ( *p ) {
2207c478bd9Sstevel@tonic-gate 		case '\\':
2217c478bd9Sstevel@tonic-gate 			if ( *++p == '\0' )
2227c478bd9Sstevel@tonic-gate 				p--;
2237c478bd9Sstevel@tonic-gate 			else
2247c478bd9Sstevel@tonic-gate 				plen = LDAP_UTF8LEN(p);
2257c478bd9Sstevel@tonic-gate 			break;
2267c478bd9Sstevel@tonic-gate 		case '"':
2277c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
2287c478bd9Sstevel@tonic-gate 				state = OUTQUOTE;
2297c478bd9Sstevel@tonic-gate 			else
2307c478bd9Sstevel@tonic-gate 				state = INQUOTE;
2317c478bd9Sstevel@tonic-gate 			break;
2327c478bd9Sstevel@tonic-gate 		case '+': if ( nametype != LDAP_RDN ) break;
233d7fdecd2SToomas Soome 			/* FALLTHROUGH */
2347c478bd9Sstevel@tonic-gate 		case ';':
2357c478bd9Sstevel@tonic-gate 		case ',':
2367c478bd9Sstevel@tonic-gate 		case '\0':
2377c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE ) {
2387c478bd9Sstevel@tonic-gate 				/*
2397c478bd9Sstevel@tonic-gate 				 * semicolon and comma are not valid RDN
2407c478bd9Sstevel@tonic-gate 				 * separators.
2417c478bd9Sstevel@tonic-gate 				 */
242*55fea89dSDan Cross 				if ( nametype == LDAP_RDN &&
2437c478bd9Sstevel@tonic-gate 					( *p == ';' || *p == ',' || !goteq)) {
2447c478bd9Sstevel@tonic-gate 					ldap_charray_free( rdns );
2457c478bd9Sstevel@tonic-gate 					return NULL;
2467c478bd9Sstevel@tonic-gate 				}
2477c478bd9Sstevel@tonic-gate 				if ( (*p == ',' || *p == ';') && !goteq ) {
2487c478bd9Sstevel@tonic-gate                                    /* If we get here, we have a case similar
2497c478bd9Sstevel@tonic-gate 				    * to <attr>=<value>,<string>,<attr>=<value>
2507c478bd9Sstevel@tonic-gate 				    * This is not a valid dn */
2517c478bd9Sstevel@tonic-gate 				    ldap_charray_free( rdns );
2527c478bd9Sstevel@tonic-gate 				    return NULL;
2537c478bd9Sstevel@tonic-gate 				}
2547c478bd9Sstevel@tonic-gate 				goteq = 0;
2557c478bd9Sstevel@tonic-gate 				++count;
2567c478bd9Sstevel@tonic-gate 				if ( rdns == NULL ) {
2577c478bd9Sstevel@tonic-gate 					if (( rdns = (char **)NSLDAPI_MALLOC( 8
2587c478bd9Sstevel@tonic-gate 						 * sizeof( char *))) == NULL )
2597c478bd9Sstevel@tonic-gate 						return( NULL );
2607c478bd9Sstevel@tonic-gate 				} else if ( count >= 8 ) {
2617c478bd9Sstevel@tonic-gate 					if (( rdns = (char **)NSLDAPI_REALLOC(
2627c478bd9Sstevel@tonic-gate 					    rdns, (count+1) *
2637c478bd9Sstevel@tonic-gate 					    sizeof( char *))) == NULL )
2647c478bd9Sstevel@tonic-gate 						return( NULL );
2657c478bd9Sstevel@tonic-gate 				}
2667c478bd9Sstevel@tonic-gate 				rdns[ count ] = NULL;
2677c478bd9Sstevel@tonic-gate 				endquote = 0;
2687c478bd9Sstevel@tonic-gate 				if ( notypes ) {
2697c478bd9Sstevel@tonic-gate 					for ( q = rdnstart;
2707c478bd9Sstevel@tonic-gate 					    q < p && *q != '='; ++q ) {
2717c478bd9Sstevel@tonic-gate 						;
2727c478bd9Sstevel@tonic-gate 					}
2737c478bd9Sstevel@tonic-gate 					if ( q < p ) { /* *q == '=' */
2747c478bd9Sstevel@tonic-gate 						rdnstart = ++q;
2757c478bd9Sstevel@tonic-gate 					}
2767c478bd9Sstevel@tonic-gate 					if ( *rdnstart == '"' ) {
2777c478bd9Sstevel@tonic-gate 						++rdnstart;
2787c478bd9Sstevel@tonic-gate 					}
279*55fea89dSDan Cross 
2807c478bd9Sstevel@tonic-gate 					if ( *(p-1) == '"' ) {
2817c478bd9Sstevel@tonic-gate 						endquote = 1;
2827c478bd9Sstevel@tonic-gate 						--p;
2837c478bd9Sstevel@tonic-gate 					}
2847c478bd9Sstevel@tonic-gate 				}
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 				len = p - rdnstart;
2877c478bd9Sstevel@tonic-gate 				if (( rdns[ count-1 ] = (char *)NSLDAPI_CALLOC(
2887c478bd9Sstevel@tonic-gate 				    1, len + 1 )) != NULL ) {
2897c478bd9Sstevel@tonic-gate 				    	SAFEMEMCPY( rdns[ count-1 ], rdnstart,
2907c478bd9Sstevel@tonic-gate 					    len );
2917c478bd9Sstevel@tonic-gate 					if ( !endquote ) {
2927c478bd9Sstevel@tonic-gate 						/* trim trailing spaces */
2937c478bd9Sstevel@tonic-gate 						while ( len > 0 &&
2947c478bd9Sstevel@tonic-gate 						    ldap_utf8isspace(
2957c478bd9Sstevel@tonic-gate 						    &rdns[count-1][len-1] )) {
2967c478bd9Sstevel@tonic-gate 							--len;
2977c478bd9Sstevel@tonic-gate 						}
2987c478bd9Sstevel@tonic-gate 					}
2997c478bd9Sstevel@tonic-gate 					rdns[ count-1 ][ len ] = '\0';
3007c478bd9Sstevel@tonic-gate 				}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 				/*
3037c478bd9Sstevel@tonic-gate 				 *  Don't forget to increment 'p' back to where
3047c478bd9Sstevel@tonic-gate 				 *  it should be.  If we don't, then we will
3057c478bd9Sstevel@tonic-gate 				 *  never get past an "end quote."
3067c478bd9Sstevel@tonic-gate 				 */
3077c478bd9Sstevel@tonic-gate 				if ( endquote == 1 )
3087c478bd9Sstevel@tonic-gate 					p++;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 				rdnstart = *p ? p + 1 : p;
3117c478bd9Sstevel@tonic-gate 				while ( ldap_utf8isspace( rdnstart ))
3127c478bd9Sstevel@tonic-gate 					++rdnstart;
3137c478bd9Sstevel@tonic-gate 			}
3147c478bd9Sstevel@tonic-gate 			break;
3157c478bd9Sstevel@tonic-gate 		case '=':
3167c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE ) {
3177c478bd9Sstevel@tonic-gate 				goteq = 1;
3187c478bd9Sstevel@tonic-gate 			}
319d7fdecd2SToomas Soome 			/* FALLTHROUGH */
3207c478bd9Sstevel@tonic-gate 		default:
3217c478bd9Sstevel@tonic-gate 			plen = LDAP_UTF8LEN(p);
3227c478bd9Sstevel@tonic-gate 			break;
3237c478bd9Sstevel@tonic-gate 		}
3247c478bd9Sstevel@tonic-gate 	} while ( *p );
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	return( rdns );
3277c478bd9Sstevel@tonic-gate }
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate char **
3307c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_dn(const char * dn,const int notypes)3317c478bd9Sstevel@tonic-gate ldap_explode_dn( const char *dn, const int notypes )
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	return( ldap_explode( dn, notypes, LDAP_DN ) );
3347c478bd9Sstevel@tonic-gate }
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate char **
3377c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_rdn(const char * rdn,const int notypes)3387c478bd9Sstevel@tonic-gate ldap_explode_rdn( const char *rdn, const int notypes )
3397c478bd9Sstevel@tonic-gate {
3407c478bd9Sstevel@tonic-gate 	return( ldap_explode( rdn, notypes, LDAP_RDN ) );
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate int
3447c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_is_dns_dn(const char * dn)3457c478bd9Sstevel@tonic-gate ldap_is_dns_dn( const char *dn )
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	return( dn != NULL && dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
3487c478bd9Sstevel@tonic-gate 	    strchr( dn, ',' ) == NULL );
3497c478bd9Sstevel@tonic-gate }
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate /*
3547c478bd9Sstevel@tonic-gate  * Convert a DNS domain name into an X.500 distinguished name.
3557c478bd9Sstevel@tonic-gate  * For example, "sales.wiz.com" -> "dc=sales,dc=wiz,dc=com"
3567c478bd9Sstevel@tonic-gate  *
3577c478bd9Sstevel@tonic-gate  * If an error is encountered zero is returned, otherwise a string
3587c478bd9Sstevel@tonic-gate  * distinguished name and the number of nameparts is returned.
3597c478bd9Sstevel@tonic-gate  * The caller should free the returned string if it is non-zero.
3607c478bd9Sstevel@tonic-gate  */
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate char *
ldap_dns_to_dn(char * dns_name,int * nameparts)3637c478bd9Sstevel@tonic-gate ldap_dns_to_dn(
3647c478bd9Sstevel@tonic-gate         char    *dns_name,
3657c478bd9Sstevel@tonic-gate         int     *nameparts
3667c478bd9Sstevel@tonic-gate )
3677c478bd9Sstevel@tonic-gate {
3687c478bd9Sstevel@tonic-gate         size_t  dns_len;
3697c478bd9Sstevel@tonic-gate         char    *dn = 0;
3707c478bd9Sstevel@tonic-gate         char    *cp;
3717c478bd9Sstevel@tonic-gate 
3727c478bd9Sstevel@tonic-gate         /* check for NULL string, empty name and name ending in '.' */
3737c478bd9Sstevel@tonic-gate         if (dns_name && (dns_len = strlen(dns_name)) &&
3747c478bd9Sstevel@tonic-gate             (dns_name[dns_len - 1] != '.')) {
3757c478bd9Sstevel@tonic-gate                 if (dn = (char *)malloc(dns_len * 3 + 1)) {
3767c478bd9Sstevel@tonic-gate                         *nameparts = 0;
3777c478bd9Sstevel@tonic-gate                         cp = dn;
3787c478bd9Sstevel@tonic-gate                         while (*dns_name) {
3797c478bd9Sstevel@tonic-gate                                 *cp++ = 'd';
3807c478bd9Sstevel@tonic-gate                                 *cp++ = 'c';
3817c478bd9Sstevel@tonic-gate                                 *cp++ = '=';
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate                                 while (*dns_name && (*dns_name != '.')) {
3847c478bd9Sstevel@tonic-gate                                         *cp++ = *dns_name++;
3857c478bd9Sstevel@tonic-gate                                 }
3867c478bd9Sstevel@tonic-gate                                 if (*dns_name == '.') {
3877c478bd9Sstevel@tonic-gate                                         dns_name++;
3887c478bd9Sstevel@tonic-gate                                         *cp++ = ',';
3897c478bd9Sstevel@tonic-gate                                 }
3907c478bd9Sstevel@tonic-gate                                 (*nameparts)++;
3917c478bd9Sstevel@tonic-gate                         }
3927c478bd9Sstevel@tonic-gate                         *cp = '\0';
3937c478bd9Sstevel@tonic-gate                 }
3947c478bd9Sstevel@tonic-gate         }
3957c478bd9Sstevel@tonic-gate         return (dn);
3967c478bd9Sstevel@tonic-gate }
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate #endif
3997c478bd9Sstevel@tonic-gate 
400