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