xref: /titanic_53/usr/src/lib/libldap5/sources/ldap/common/getdn.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 by Sun Microsystems, Inc.
3*7c478bd9Sstevel@tonic-gate  * All rights reserved.
4*7c478bd9Sstevel@tonic-gate  */
5*7c478bd9Sstevel@tonic-gate 
6*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
7*7c478bd9Sstevel@tonic-gate 
8*7c478bd9Sstevel@tonic-gate /*
9*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the Netscape Public
10*7c478bd9Sstevel@tonic-gate  * License Version 1.1 (the "License"); you may not use this file
11*7c478bd9Sstevel@tonic-gate  * except in compliance with the License. You may obtain a copy of
12*7c478bd9Sstevel@tonic-gate  * the License at http://www.mozilla.org/NPL/
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * Software distributed under the License is distributed on an "AS
15*7c478bd9Sstevel@tonic-gate  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
16*7c478bd9Sstevel@tonic-gate  * implied. See the License for the specific language governing
17*7c478bd9Sstevel@tonic-gate  * rights and limitations under the License.
18*7c478bd9Sstevel@tonic-gate  *
19*7c478bd9Sstevel@tonic-gate  * The Original Code is Mozilla Communicator client code, released
20*7c478bd9Sstevel@tonic-gate  * March 31, 1998.
21*7c478bd9Sstevel@tonic-gate  *
22*7c478bd9Sstevel@tonic-gate  * The Initial Developer of the Original Code is Netscape
23*7c478bd9Sstevel@tonic-gate  * Communications Corporation. Portions created by Netscape are
24*7c478bd9Sstevel@tonic-gate  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
25*7c478bd9Sstevel@tonic-gate  * Rights Reserved.
26*7c478bd9Sstevel@tonic-gate  *
27*7c478bd9Sstevel@tonic-gate  * Contributor(s):
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate /*
30*7c478bd9Sstevel@tonic-gate  *  Copyright (c) 1994 Regents of the University of Michigan.
31*7c478bd9Sstevel@tonic-gate  *  All rights reserved.
32*7c478bd9Sstevel@tonic-gate  */
33*7c478bd9Sstevel@tonic-gate /*
34*7c478bd9Sstevel@tonic-gate  *  getdn.c
35*7c478bd9Sstevel@tonic-gate  */
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate #if 0
38*7c478bd9Sstevel@tonic-gate #ifndef lint
39*7c478bd9Sstevel@tonic-gate static char copyright[] = "@(#) Copyright (c) 1990 Regents of the University of Michigan.\nAll rights reserved.\n";
40*7c478bd9Sstevel@tonic-gate #endif
41*7c478bd9Sstevel@tonic-gate #endif
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate char *
46*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_get_dn(LDAP * ld,LDAPMessage * entry)47*7c478bd9Sstevel@tonic-gate ldap_get_dn( LDAP *ld, LDAPMessage *entry )
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	char			*dn;
50*7c478bd9Sstevel@tonic-gate 	struct berelement	tmp;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
53*7c478bd9Sstevel@tonic-gate 
54*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
55*7c478bd9Sstevel@tonic-gate 		return( NULL );		/* punt */
56*7c478bd9Sstevel@tonic-gate 	}
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	if ( !NSLDAPI_VALID_LDAPMESSAGE_ENTRY_POINTER( entry )) {
59*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_PARAM_ERROR, NULL, NULL );
60*7c478bd9Sstevel@tonic-gate 		return( NULL );
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	tmp = *entry->lm_ber;	/* struct copy */
64*7c478bd9Sstevel@tonic-gate 	if ( ber_scanf( &tmp, "{a", &dn ) == LBER_ERROR ) {
65*7c478bd9Sstevel@tonic-gate 		LDAP_SET_LDERRNO( ld, LDAP_DECODING_ERROR, NULL, NULL );
66*7c478bd9Sstevel@tonic-gate 		return( NULL );
67*7c478bd9Sstevel@tonic-gate 	}
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	return( dn );
70*7c478bd9Sstevel@tonic-gate }
71*7c478bd9Sstevel@tonic-gate 
72*7c478bd9Sstevel@tonic-gate char *
73*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_dn2ufn(const char * dn)74*7c478bd9Sstevel@tonic-gate ldap_dn2ufn( const char *dn )
75*7c478bd9Sstevel@tonic-gate {
76*7c478bd9Sstevel@tonic-gate 	char	*p, *ufn, *r;
77*7c478bd9Sstevel@tonic-gate 	size_t	plen;
78*7c478bd9Sstevel@tonic-gate 	int	state;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
83*7c478bd9Sstevel@tonic-gate 		dn = "";
84*7c478bd9Sstevel@tonic-gate 	}
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	if ( ldap_is_dns_dn( dn ) || ( p = strchr( dn, '=' )) == NULL )
87*7c478bd9Sstevel@tonic-gate 		return( nsldapi_strdup( (char *)dn ));
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	ufn = nsldapi_strdup( ++p );
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate #define INQUOTE		1
92*7c478bd9Sstevel@tonic-gate #define OUTQUOTE	2
93*7c478bd9Sstevel@tonic-gate 	state = OUTQUOTE;
94*7c478bd9Sstevel@tonic-gate 	for ( p = ufn, r = ufn; *p; p += plen ) {
95*7c478bd9Sstevel@tonic-gate 	    plen = 1;
96*7c478bd9Sstevel@tonic-gate 		switch ( *p ) {
97*7c478bd9Sstevel@tonic-gate 		case '\\':
98*7c478bd9Sstevel@tonic-gate 			if ( *++p == '\0' )
99*7c478bd9Sstevel@tonic-gate 				plen=0;
100*7c478bd9Sstevel@tonic-gate 			else {
101*7c478bd9Sstevel@tonic-gate 				*r++ = '\\';
102*7c478bd9Sstevel@tonic-gate 				r += (plen = LDAP_UTF8COPY(r,p));
103*7c478bd9Sstevel@tonic-gate 			}
104*7c478bd9Sstevel@tonic-gate 			break;
105*7c478bd9Sstevel@tonic-gate 		case '"':
106*7c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
107*7c478bd9Sstevel@tonic-gate 				state = OUTQUOTE;
108*7c478bd9Sstevel@tonic-gate 			else
109*7c478bd9Sstevel@tonic-gate 				state = INQUOTE;
110*7c478bd9Sstevel@tonic-gate 			*r++ = *p;
111*7c478bd9Sstevel@tonic-gate 			break;
112*7c478bd9Sstevel@tonic-gate 		case ';':
113*7c478bd9Sstevel@tonic-gate 		case ',':
114*7c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE )
115*7c478bd9Sstevel@tonic-gate 				*r++ = ',';
116*7c478bd9Sstevel@tonic-gate 			else
117*7c478bd9Sstevel@tonic-gate 				*r++ = *p;
118*7c478bd9Sstevel@tonic-gate 			break;
119*7c478bd9Sstevel@tonic-gate 		case '=':
120*7c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
121*7c478bd9Sstevel@tonic-gate 				*r++ = *p;
122*7c478bd9Sstevel@tonic-gate 			else {
123*7c478bd9Sstevel@tonic-gate 				char	*rsave = r;
124*7c478bd9Sstevel@tonic-gate 				LDAP_UTF8DEC(r);
125*7c478bd9Sstevel@tonic-gate 				*rsave = '\0';
126*7c478bd9Sstevel@tonic-gate 				while ( !ldap_utf8isspace( r ) && *r != ';'
127*7c478bd9Sstevel@tonic-gate 				    && *r != ',' && r > ufn )
128*7c478bd9Sstevel@tonic-gate 					LDAP_UTF8DEC(r);
129*7c478bd9Sstevel@tonic-gate 				LDAP_UTF8INC(r);
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 				if ( strcasecmp( r, "c" )
132*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "o" )
133*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "ou" )
134*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "st" )
135*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "l" )
136*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "dc" )
137*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "uid" )
138*7c478bd9Sstevel@tonic-gate 				    && strcasecmp( r, "cn" ) ) {
139*7c478bd9Sstevel@tonic-gate 					r = rsave;
140*7c478bd9Sstevel@tonic-gate 					*r++ = '=';
141*7c478bd9Sstevel@tonic-gate 				}
142*7c478bd9Sstevel@tonic-gate 			}
143*7c478bd9Sstevel@tonic-gate 			break;
144*7c478bd9Sstevel@tonic-gate 		default:
145*7c478bd9Sstevel@tonic-gate 			r += (plen = LDAP_UTF8COPY(r,p));
146*7c478bd9Sstevel@tonic-gate 			break;
147*7c478bd9Sstevel@tonic-gate 		}
148*7c478bd9Sstevel@tonic-gate 	}
149*7c478bd9Sstevel@tonic-gate 	*r = '\0';
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	return( ufn );
152*7c478bd9Sstevel@tonic-gate }
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate char **
155*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_dns(const char * dn)156*7c478bd9Sstevel@tonic-gate ldap_explode_dns( const char *dn )
157*7c478bd9Sstevel@tonic-gate {
158*7c478bd9Sstevel@tonic-gate 	int	ncomps, maxcomps;
159*7c478bd9Sstevel@tonic-gate 	char	*s, *cpydn;
160*7c478bd9Sstevel@tonic-gate 	char	**rdns;
161*7c478bd9Sstevel@tonic-gate #ifdef HAVE_STRTOK_R	/* defined in portable.h */
162*7c478bd9Sstevel@tonic-gate 	char	*lasts;
163*7c478bd9Sstevel@tonic-gate #endif
164*7c478bd9Sstevel@tonic-gate 
165*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
166*7c478bd9Sstevel@tonic-gate 		dn = "";
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if ( (rdns = (char **)NSLDAPI_MALLOC( 8 * sizeof(char *) )) == NULL ) {
170*7c478bd9Sstevel@tonic-gate 		return( NULL );
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	maxcomps = 8;
174*7c478bd9Sstevel@tonic-gate 	ncomps = 0;
175*7c478bd9Sstevel@tonic-gate 	cpydn = nsldapi_strdup( (char *)dn );
176*7c478bd9Sstevel@tonic-gate 	for ( s = STRTOK( cpydn, "@.", &lasts ); s != NULL;
177*7c478bd9Sstevel@tonic-gate 	    s = STRTOK( NULL, "@.", &lasts ) ) {
178*7c478bd9Sstevel@tonic-gate 		if ( ncomps == maxcomps ) {
179*7c478bd9Sstevel@tonic-gate 			maxcomps *= 2;
180*7c478bd9Sstevel@tonic-gate 			if ( (rdns = (char **)NSLDAPI_REALLOC( rdns, maxcomps *
181*7c478bd9Sstevel@tonic-gate 			    sizeof(char *) )) == NULL ) {
182*7c478bd9Sstevel@tonic-gate 				NSLDAPI_FREE( cpydn );
183*7c478bd9Sstevel@tonic-gate 				return( NULL );
184*7c478bd9Sstevel@tonic-gate 			}
185*7c478bd9Sstevel@tonic-gate 		}
186*7c478bd9Sstevel@tonic-gate 		rdns[ncomps++] = nsldapi_strdup( s );
187*7c478bd9Sstevel@tonic-gate 	}
188*7c478bd9Sstevel@tonic-gate 	rdns[ncomps] = NULL;
189*7c478bd9Sstevel@tonic-gate 	NSLDAPI_FREE( cpydn );
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	return( rdns );
192*7c478bd9Sstevel@tonic-gate }
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate #define LDAP_DN		1
195*7c478bd9Sstevel@tonic-gate #define LDAP_RDN	2
196*7c478bd9Sstevel@tonic-gate 
197*7c478bd9Sstevel@tonic-gate static char **
ldap_explode(const char * dn,const int notypes,const int nametype)198*7c478bd9Sstevel@tonic-gate ldap_explode( const char *dn, const int notypes, const int nametype )
199*7c478bd9Sstevel@tonic-gate {
200*7c478bd9Sstevel@tonic-gate 	char	*p, *q, *rdnstart, **rdns = NULL;
201*7c478bd9Sstevel@tonic-gate 	size_t	plen = 0;
202*7c478bd9Sstevel@tonic-gate 	int	state, count = 0, endquote, len, goteq;
203*7c478bd9Sstevel@tonic-gate 
204*7c478bd9Sstevel@tonic-gate 	LDAPDebug( LDAP_DEBUG_TRACE, "ldap_explode\n", 0, 0, 0 );
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate 	if ( dn == NULL ) {
207*7c478bd9Sstevel@tonic-gate 		dn = "";
208*7c478bd9Sstevel@tonic-gate 	}
209*7c478bd9Sstevel@tonic-gate 
210*7c478bd9Sstevel@tonic-gate #if 0
211*7c478bd9Sstevel@tonic-gate 	if ( ldap_is_dns_dn( dn ) ) {
212*7c478bd9Sstevel@tonic-gate 		return( ldap_explode_dns( dn ) );
213*7c478bd9Sstevel@tonic-gate 	}
214*7c478bd9Sstevel@tonic-gate #endif
215*7c478bd9Sstevel@tonic-gate 
216*7c478bd9Sstevel@tonic-gate 	while ( ldap_utf8isspace( (char *)dn )) { /* ignore leading spaces */
217*7c478bd9Sstevel@tonic-gate 		++dn;
218*7c478bd9Sstevel@tonic-gate 	}
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	p = rdnstart = (char *) dn;
221*7c478bd9Sstevel@tonic-gate 	state = OUTQUOTE;
222*7c478bd9Sstevel@tonic-gate 	goteq = 0;
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	do {
225*7c478bd9Sstevel@tonic-gate 		p += plen;
226*7c478bd9Sstevel@tonic-gate 		plen = 1;
227*7c478bd9Sstevel@tonic-gate 		switch ( *p ) {
228*7c478bd9Sstevel@tonic-gate 		case '\\':
229*7c478bd9Sstevel@tonic-gate 			if ( *++p == '\0' )
230*7c478bd9Sstevel@tonic-gate 				p--;
231*7c478bd9Sstevel@tonic-gate 			else
232*7c478bd9Sstevel@tonic-gate 				plen = LDAP_UTF8LEN(p);
233*7c478bd9Sstevel@tonic-gate 			break;
234*7c478bd9Sstevel@tonic-gate 		case '"':
235*7c478bd9Sstevel@tonic-gate 			if ( state == INQUOTE )
236*7c478bd9Sstevel@tonic-gate 				state = OUTQUOTE;
237*7c478bd9Sstevel@tonic-gate 			else
238*7c478bd9Sstevel@tonic-gate 				state = INQUOTE;
239*7c478bd9Sstevel@tonic-gate 			break;
240*7c478bd9Sstevel@tonic-gate 		case '+': if ( nametype != LDAP_RDN ) break;
241*7c478bd9Sstevel@tonic-gate 		case ';':
242*7c478bd9Sstevel@tonic-gate 		case ',':
243*7c478bd9Sstevel@tonic-gate 		case '\0':
244*7c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE ) {
245*7c478bd9Sstevel@tonic-gate 				/*
246*7c478bd9Sstevel@tonic-gate 				 * semicolon and comma are not valid RDN
247*7c478bd9Sstevel@tonic-gate 				 * separators.
248*7c478bd9Sstevel@tonic-gate 				 */
249*7c478bd9Sstevel@tonic-gate 				if ( nametype == LDAP_RDN &&
250*7c478bd9Sstevel@tonic-gate 					( *p == ';' || *p == ',' || !goteq)) {
251*7c478bd9Sstevel@tonic-gate 					ldap_charray_free( rdns );
252*7c478bd9Sstevel@tonic-gate 					return NULL;
253*7c478bd9Sstevel@tonic-gate 				}
254*7c478bd9Sstevel@tonic-gate 				if ( (*p == ',' || *p == ';') && !goteq ) {
255*7c478bd9Sstevel@tonic-gate                                    /* If we get here, we have a case similar
256*7c478bd9Sstevel@tonic-gate 				    * to <attr>=<value>,<string>,<attr>=<value>
257*7c478bd9Sstevel@tonic-gate 				    * This is not a valid dn */
258*7c478bd9Sstevel@tonic-gate 				    ldap_charray_free( rdns );
259*7c478bd9Sstevel@tonic-gate 				    return NULL;
260*7c478bd9Sstevel@tonic-gate 				}
261*7c478bd9Sstevel@tonic-gate 				goteq = 0;
262*7c478bd9Sstevel@tonic-gate 				++count;
263*7c478bd9Sstevel@tonic-gate 				if ( rdns == NULL ) {
264*7c478bd9Sstevel@tonic-gate 					if (( rdns = (char **)NSLDAPI_MALLOC( 8
265*7c478bd9Sstevel@tonic-gate 						 * sizeof( char *))) == NULL )
266*7c478bd9Sstevel@tonic-gate 						return( NULL );
267*7c478bd9Sstevel@tonic-gate 				} else if ( count >= 8 ) {
268*7c478bd9Sstevel@tonic-gate 					if (( rdns = (char **)NSLDAPI_REALLOC(
269*7c478bd9Sstevel@tonic-gate 					    rdns, (count+1) *
270*7c478bd9Sstevel@tonic-gate 					    sizeof( char *))) == NULL )
271*7c478bd9Sstevel@tonic-gate 						return( NULL );
272*7c478bd9Sstevel@tonic-gate 				}
273*7c478bd9Sstevel@tonic-gate 				rdns[ count ] = NULL;
274*7c478bd9Sstevel@tonic-gate 				endquote = 0;
275*7c478bd9Sstevel@tonic-gate 				if ( notypes ) {
276*7c478bd9Sstevel@tonic-gate 					for ( q = rdnstart;
277*7c478bd9Sstevel@tonic-gate 					    q < p && *q != '='; ++q ) {
278*7c478bd9Sstevel@tonic-gate 						;
279*7c478bd9Sstevel@tonic-gate 					}
280*7c478bd9Sstevel@tonic-gate 					if ( q < p ) { /* *q == '=' */
281*7c478bd9Sstevel@tonic-gate 						rdnstart = ++q;
282*7c478bd9Sstevel@tonic-gate 					}
283*7c478bd9Sstevel@tonic-gate 					if ( *rdnstart == '"' ) {
284*7c478bd9Sstevel@tonic-gate 						++rdnstart;
285*7c478bd9Sstevel@tonic-gate 					}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate 					if ( *(p-1) == '"' ) {
288*7c478bd9Sstevel@tonic-gate 						endquote = 1;
289*7c478bd9Sstevel@tonic-gate 						--p;
290*7c478bd9Sstevel@tonic-gate 					}
291*7c478bd9Sstevel@tonic-gate 				}
292*7c478bd9Sstevel@tonic-gate 
293*7c478bd9Sstevel@tonic-gate 				len = p - rdnstart;
294*7c478bd9Sstevel@tonic-gate 				if (( rdns[ count-1 ] = (char *)NSLDAPI_CALLOC(
295*7c478bd9Sstevel@tonic-gate 				    1, len + 1 )) != NULL ) {
296*7c478bd9Sstevel@tonic-gate 				    	SAFEMEMCPY( rdns[ count-1 ], rdnstart,
297*7c478bd9Sstevel@tonic-gate 					    len );
298*7c478bd9Sstevel@tonic-gate 					if ( !endquote ) {
299*7c478bd9Sstevel@tonic-gate 						/* trim trailing spaces */
300*7c478bd9Sstevel@tonic-gate 						while ( len > 0 &&
301*7c478bd9Sstevel@tonic-gate 						    ldap_utf8isspace(
302*7c478bd9Sstevel@tonic-gate 						    &rdns[count-1][len-1] )) {
303*7c478bd9Sstevel@tonic-gate 							--len;
304*7c478bd9Sstevel@tonic-gate 						}
305*7c478bd9Sstevel@tonic-gate 					}
306*7c478bd9Sstevel@tonic-gate 					rdns[ count-1 ][ len ] = '\0';
307*7c478bd9Sstevel@tonic-gate 				}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 				/*
310*7c478bd9Sstevel@tonic-gate 				 *  Don't forget to increment 'p' back to where
311*7c478bd9Sstevel@tonic-gate 				 *  it should be.  If we don't, then we will
312*7c478bd9Sstevel@tonic-gate 				 *  never get past an "end quote."
313*7c478bd9Sstevel@tonic-gate 				 */
314*7c478bd9Sstevel@tonic-gate 				if ( endquote == 1 )
315*7c478bd9Sstevel@tonic-gate 					p++;
316*7c478bd9Sstevel@tonic-gate 
317*7c478bd9Sstevel@tonic-gate 				rdnstart = *p ? p + 1 : p;
318*7c478bd9Sstevel@tonic-gate 				while ( ldap_utf8isspace( rdnstart ))
319*7c478bd9Sstevel@tonic-gate 					++rdnstart;
320*7c478bd9Sstevel@tonic-gate 			}
321*7c478bd9Sstevel@tonic-gate 			break;
322*7c478bd9Sstevel@tonic-gate 		case '=':
323*7c478bd9Sstevel@tonic-gate 			if ( state == OUTQUOTE ) {
324*7c478bd9Sstevel@tonic-gate 				goteq = 1;
325*7c478bd9Sstevel@tonic-gate 			}
326*7c478bd9Sstevel@tonic-gate 			/* FALL */
327*7c478bd9Sstevel@tonic-gate 		default:
328*7c478bd9Sstevel@tonic-gate 			plen = LDAP_UTF8LEN(p);
329*7c478bd9Sstevel@tonic-gate 			break;
330*7c478bd9Sstevel@tonic-gate 		}
331*7c478bd9Sstevel@tonic-gate 	} while ( *p );
332*7c478bd9Sstevel@tonic-gate 
333*7c478bd9Sstevel@tonic-gate 	return( rdns );
334*7c478bd9Sstevel@tonic-gate }
335*7c478bd9Sstevel@tonic-gate 
336*7c478bd9Sstevel@tonic-gate char **
337*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_dn(const char * dn,const int notypes)338*7c478bd9Sstevel@tonic-gate ldap_explode_dn( const char *dn, const int notypes )
339*7c478bd9Sstevel@tonic-gate {
340*7c478bd9Sstevel@tonic-gate 	return( ldap_explode( dn, notypes, LDAP_DN ) );
341*7c478bd9Sstevel@tonic-gate }
342*7c478bd9Sstevel@tonic-gate 
343*7c478bd9Sstevel@tonic-gate char **
344*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_explode_rdn(const char * rdn,const int notypes)345*7c478bd9Sstevel@tonic-gate ldap_explode_rdn( const char *rdn, const int notypes )
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	return( ldap_explode( rdn, notypes, LDAP_RDN ) );
348*7c478bd9Sstevel@tonic-gate }
349*7c478bd9Sstevel@tonic-gate 
350*7c478bd9Sstevel@tonic-gate int
351*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_is_dns_dn(const char * dn)352*7c478bd9Sstevel@tonic-gate ldap_is_dns_dn( const char *dn )
353*7c478bd9Sstevel@tonic-gate {
354*7c478bd9Sstevel@tonic-gate 	return( dn != NULL && dn[ 0 ] != '\0' && strchr( dn, '=' ) == NULL &&
355*7c478bd9Sstevel@tonic-gate 	    strchr( dn, ',' ) == NULL );
356*7c478bd9Sstevel@tonic-gate }
357*7c478bd9Sstevel@tonic-gate 
358*7c478bd9Sstevel@tonic-gate #ifdef _SOLARIS_SDK
359*7c478bd9Sstevel@tonic-gate 
360*7c478bd9Sstevel@tonic-gate /*
361*7c478bd9Sstevel@tonic-gate  * Convert a DNS domain name into an X.500 distinguished name.
362*7c478bd9Sstevel@tonic-gate  * For example, "sales.wiz.com" -> "dc=sales,dc=wiz,dc=com"
363*7c478bd9Sstevel@tonic-gate  *
364*7c478bd9Sstevel@tonic-gate  * If an error is encountered zero is returned, otherwise a string
365*7c478bd9Sstevel@tonic-gate  * distinguished name and the number of nameparts is returned.
366*7c478bd9Sstevel@tonic-gate  * The caller should free the returned string if it is non-zero.
367*7c478bd9Sstevel@tonic-gate  */
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate char *
ldap_dns_to_dn(char * dns_name,int * nameparts)370*7c478bd9Sstevel@tonic-gate ldap_dns_to_dn(
371*7c478bd9Sstevel@tonic-gate         char    *dns_name,
372*7c478bd9Sstevel@tonic-gate         int     *nameparts
373*7c478bd9Sstevel@tonic-gate )
374*7c478bd9Sstevel@tonic-gate {
375*7c478bd9Sstevel@tonic-gate         size_t  dns_len;
376*7c478bd9Sstevel@tonic-gate         char    *dn = 0;
377*7c478bd9Sstevel@tonic-gate         char    *cp;
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate         /* check for NULL string, empty name and name ending in '.' */
380*7c478bd9Sstevel@tonic-gate         if (dns_name && (dns_len = strlen(dns_name)) &&
381*7c478bd9Sstevel@tonic-gate             (dns_name[dns_len - 1] != '.')) {
382*7c478bd9Sstevel@tonic-gate                 if (dn = (char *)malloc(dns_len * 3 + 1)) {
383*7c478bd9Sstevel@tonic-gate                         *nameparts = 0;
384*7c478bd9Sstevel@tonic-gate                         cp = dn;
385*7c478bd9Sstevel@tonic-gate                         while (*dns_name) {
386*7c478bd9Sstevel@tonic-gate                                 *cp++ = 'd';
387*7c478bd9Sstevel@tonic-gate                                 *cp++ = 'c';
388*7c478bd9Sstevel@tonic-gate                                 *cp++ = '=';
389*7c478bd9Sstevel@tonic-gate 
390*7c478bd9Sstevel@tonic-gate                                 while (*dns_name && (*dns_name != '.')) {
391*7c478bd9Sstevel@tonic-gate                                         *cp++ = *dns_name++;
392*7c478bd9Sstevel@tonic-gate                                 }
393*7c478bd9Sstevel@tonic-gate                                 if (*dns_name == '.') {
394*7c478bd9Sstevel@tonic-gate                                         dns_name++;
395*7c478bd9Sstevel@tonic-gate                                         *cp++ = ',';
396*7c478bd9Sstevel@tonic-gate                                 }
397*7c478bd9Sstevel@tonic-gate                                 (*nameparts)++;
398*7c478bd9Sstevel@tonic-gate                         }
399*7c478bd9Sstevel@tonic-gate                         *cp = '\0';
400*7c478bd9Sstevel@tonic-gate                 }
401*7c478bd9Sstevel@tonic-gate         }
402*7c478bd9Sstevel@tonic-gate         return (dn);
403*7c478bd9Sstevel@tonic-gate }
404*7c478bd9Sstevel@tonic-gate 
405*7c478bd9Sstevel@tonic-gate #endif
406*7c478bd9Sstevel@tonic-gate 
407