1 /* dn.c - routines for dealing with distinguished names */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
27 #define INQUOTEDVALUE 7
31 * dn_normalize - put dn into a canonical format. the dn is
32 * normalized in place, as well as returned if valid.
36 dn_normalize( char *dn )
43 for ( d = s = dn; *s; s++ ) {
47 if ( LEADOIDCHAR(*s) ) {
50 } else if ( LEADKEYCHAR(*s) ) {
53 } else if ( ! SPACE( *s ) ) {
63 } else if ( *s == '=' ) {
66 } else if ( SPACE( *s ) ) {
77 } else if ( *s == '=' ) {
80 } else if ( SPACE( *s ) ) {
92 } else if ( ! SPACE( *s ) ) {
93 /* not a valid dn - but what can we do here? */
101 state = INQUOTEDVALUE;
103 } else if ( ! SPACE( *s ) ) {
110 if ( !gotesc && SEPARATOR( *s ) ) {
111 while ( SPACE( *(d - 1) ) )
119 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
129 if ( !gotesc && *s == '"' ) {
132 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
140 if ( SEPARATOR( *s ) ) {
147 Debug( LDAP_DEBUG_ANY,
148 "dn_normalize - unknown state %d\n", state, 0, 0 );
160 /* shouldn't be left in escape */
164 /* check end state */
166 case B4LEADTYPE: /* looking for first type */
167 case B4SEPARATOR: /* looking for separator */
168 case INVALUE: /* inside value */
178 * dn_normalize_case - put dn into a canonical form suitable for storing
179 * in a hash database. this involves normalizing the case as well as
180 * the format. the dn is normalized in place as well as returned if valid.
184 dn_normalize_case( char *dn )
186 ldap_pvt_str2upper( dn );
188 /* normalize format */
189 dn = dn_normalize( dn );
191 /* and upper case it */
196 * dn_parent - return a copy of the dn of dn's parent
212 while(*dn && SPACE(*dn)) {
220 if ( be != NULL && be_issuffix( be, dn ) ) {
226 * no =, assume it is a dns name, like blah@some.domain.name
227 * if the blah@ part is there, return some.domain.name. if
228 * it's just some.domain.name, return domain.name.
230 if ( strchr( dn, '=' ) == NULL ) {
231 if ( (s = strchr( dn, '@' )) == NULL ) {
232 if ( (s = strchr( dn, '.' )) == NULL ) {
236 if ( *(s + 1) == '\0' ) {
239 return( ch_strdup( &s[1] ) );
245 * else assume it is an X.500-style name, which looks like
246 * foo=bar,sha=baz,...
250 for ( s = dn; *s; s++ ) {
264 } else if ( DNSEPARATOR( *s ) ) {
265 return( ch_strdup( &s[1] ) );
270 return( ch_strdup( "" ) );
284 while(*dn && SPACE(*dn)) {
292 if ( be != NULL && be_issuffix( be, dn ) ) {
296 dn = ch_strdup( dn );
300 * no =, assume it is a dns name, like blah@some.domain.name
301 * if the blah@ part is there, return some.domain.name. if
302 * it's just some.domain.name, return domain.name.
304 if ( strchr( dn, '=' ) == NULL ) {
305 if ( (s = strchr( dn, '@' )) == NULL ) {
306 if ( (s = strchr( dn, '.' )) == NULL ) {
316 * else assume it is an X.500-style name, which looks like
317 * foo=bar,sha=baz,...
322 for ( s = dn; *s; s++ ) {
336 } else if ( DNSEPARATOR( *s ) ) {
348 * return a charray of all subtrees to which the DN resides in
354 char *child, *parent;
355 char **subtree = NULL;
357 child = ch_strdup( dn );
360 charray_add( &subtree, child );
362 parent = dn_parent( be, child );
367 } while ( child != NULL );
374 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
375 * and suffix must be normalized.
384 int dnlen, suffixlen;
390 suffixlen = strlen( suffix );
391 dnlen = strlen( dn );
393 if ( suffixlen > dnlen ) {
397 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
402 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
403 * returns (defined in slap.h): DN_DNS dns-style thing
404 * DN_X500 x500-style thing
410 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
415 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
418 * Copyright 1999, Juan C. Gomez, All rights reserved.
419 * This software is not subject to any license of Silicon Graphics
420 * Inc. or Purdue University.
422 * Redistribution and use in source and binary forms are permitted
423 * without restriction or fee of any kind as long as this notice
428 /* get_next_substring:
430 * Gets next substring in s, using d (or the end of the string '\0') as a
431 * string delimiter, and places it in a duplicated memory space. Leading
432 * spaces are ignored. String s **must** be null-terminated.
436 get_next_substring( char * s, char d )
441 r = str = ch_malloc( strlen(s) + 1 );
443 /* Skip leading spaces */
445 while ( *s && SPACE(*s) ) {
453 while ( *s && (*s != d) ) {
455 /* Don't stop when you see trailing spaces may be a multi-word
456 * string, i.e. name=John Doe!
472 * Given a string (i.e. an rdn) of the form:
473 * "attribute_type = attribute_value"
474 * this function returns the type of an attribute, that is the
475 * string "attribute_type" which is placed in newly allocated
476 * memory. The returned string will be null-terminated.
479 char * rdn_attr_type( char * s )
482 return get_next_substring( s, '=' );
489 * Given a string (i.e. an rdn) of the form:
490 * "attribute_type = attribute_value"
491 * this function returns "attribute_type" which is placed in newly allocated
492 * memory. The returned string will be null-terminated and may contain
493 * spaces (i.e. "John Doe\0").
497 rdn_attr_value( char * rdn )
502 if ( (str = strchr( rdn, '=' )) != NULL ) {
504 return get_next_substring(++str, '\0');
513 int rdn_validate( const char * rdn )
515 /* just a simple check for now */
516 return strchr( rdn, '=' ) != NULL;
522 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
525 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
529 build_new_dn( char ** new_dn,
532 const char * newrdn )
535 if ( p_dn == NULL ) {
537 *new_dn = ch_strdup( newrdn );
542 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
545 if ( dn_type( e_dn ) == DN_X500 ) {
548 strcpy( *new_dn, newrdn );
549 strcat( *new_dn, "," );
550 strcat( *new_dn, p_dn );
558 strcpy( *new_dn, newrdn );
559 s = strchr( newrdn, '\0' );
562 if ( (*s != '.') && (*s != '@') ) {
564 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
568 strcat( *new_dn, sep );
574 strcat( *new_dn, p_dn );