1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
28 #define INQUOTEDVALUE 7
32 * dn_validate - validate and compress dn. the dn is
33 * compressed in place are returned if valid.
37 dn_validate( char *dn )
44 for ( d = s = dn; *s; s++ ) {
48 if ( LEADOIDCHAR(*s) ) {
51 } else if ( LEADKEYCHAR(*s) ) {
54 } else if ( ! SPACE( *s ) ) {
64 } else if ( *s == '=' ) {
67 } else if ( SPACE( *s ) ) {
78 } else if ( *s == '=' ) {
81 } else if ( SPACE( *s ) ) {
93 } else if ( ! SPACE( *s ) ) {
94 /* not a valid dn - but what can we do here? */
102 state = INQUOTEDVALUE;
104 } else if ( ! SPACE( *s ) ) {
111 if ( !gotesc && SEPARATOR( *s ) ) {
112 while ( SPACE( *(d - 1) ) )
120 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
130 if ( !gotesc && *s == '"' ) {
133 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
141 if ( SEPARATOR( *s ) ) {
148 Debug( LDAP_DEBUG_ANY,
149 "dn_validate - unknown state %d\n", state, 0, 0 );
161 /* shouldn't be left in escape */
165 /* check end state */
167 case B4LEADTYPE: /* looking for first type */
168 case B4SEPARATOR: /* looking for separator */
169 case INVALUE: /* inside value */
179 * dn_normalize - put dn into a canonical form suitable for storing
180 * in a hash database. this involves normalizing the case as well as
181 * the format. the dn is normalized in place as well as returned if valid.
185 dn_normalize( char *dn )
187 ldap_pvt_str2upper( dn );
189 /* validate and compress dn */
190 dn = dn_validate( dn );
192 /* and upper case it */
197 * dn_parent - return a copy of the dn of dn's parent
213 while(*dn && SPACE(*dn)) {
221 if ( be != NULL && be_issuffix( be, dn ) ) {
227 * no =, assume it is a dns name, like blah@some.domain.name
228 * if the blah@ part is there, return some.domain.name. if
229 * it's just some.domain.name, return domain.name.
231 if ( strchr( dn, '=' ) == NULL ) {
232 if ( (s = strchr( dn, '@' )) == NULL ) {
233 if ( (s = strchr( dn, '.' )) == NULL ) {
237 if ( *(s + 1) == '\0' ) {
240 return( ch_strdup( &s[1] ) );
246 * else assume it is an X.500-style name, which looks like
247 * foo=bar,sha=baz,...
251 for ( s = dn; *s; s++ ) {
265 } else if ( DNSEPARATOR( *s ) ) {
266 return( ch_strdup( &s[1] ) );
271 return( ch_strdup( "" ) );
285 while(*dn && SPACE(*dn)) {
293 if ( be != NULL && be_issuffix( be, dn ) ) {
297 dn = ch_strdup( dn );
301 * no =, assume it is a dns name, like blah@some.domain.name
302 * if the blah@ part is there, return some.domain.name. if
303 * it's just some.domain.name, return domain.name.
305 if ( strchr( dn, '=' ) == NULL ) {
306 if ( (s = strchr( dn, '@' )) == NULL ) {
307 if ( (s = strchr( dn, '.' )) == NULL ) {
317 * else assume it is an X.500-style name, which looks like
318 * foo=bar,sha=baz,...
323 for ( s = dn; *s; s++ ) {
337 } else if ( DNSEPARATOR( *s ) ) {
349 * return a charray of all subtrees to which the DN resides in
355 char *child, *parent;
356 char **subtree = NULL;
358 child = ch_strdup( dn );
361 charray_add( &subtree, child );
363 parent = dn_parent( be, child );
368 } while ( child != NULL );
375 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
376 * and suffix must be normalized.
385 int dnlen, suffixlen;
391 suffixlen = strlen( suffix );
392 dnlen = strlen( dn );
394 if ( suffixlen > dnlen ) {
398 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
403 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
404 * returns (defined in slap.h): DN_DNS dns-style thing
405 * DN_X500 x500-style thing
411 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
416 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
419 * Copyright 1999, Juan C. Gomez, All rights reserved.
420 * This software is not subject to any license of Silicon Graphics
421 * Inc. or Purdue University.
423 * Redistribution and use in source and binary forms are permitted
424 * without restriction or fee of any kind as long as this notice
429 /* get_next_substring:
431 * Gets next substring in s, using d (or the end of the string '\0') as a
432 * string delimiter, and places it in a duplicated memory space. Leading
433 * spaces are ignored. String s **must** be null-terminated.
437 get_next_substring( char * s, char d )
442 r = str = ch_malloc( strlen(s) + 1 );
444 /* Skip leading spaces */
446 while ( *s && SPACE(*s) ) {
454 while ( *s && (*s != d) ) {
456 /* Don't stop when you see trailing spaces may be a multi-word
457 * string, i.e. name=John Doe!
473 * Given a string (i.e. an rdn) of the form:
474 * "attribute_type = attribute_value"
475 * this function returns the type of an attribute, that is the
476 * string "attribute_type" which is placed in newly allocated
477 * memory. The returned string will be null-terminated.
480 char * rdn_attr_type( char * s )
483 return get_next_substring( s, '=' );
490 * Given a string (i.e. an rdn) of the form:
491 * "attribute_type = attribute_value"
492 * this function returns "attribute_type" which is placed in newly allocated
493 * memory. The returned string will be null-terminated and may contain
494 * spaces (i.e. "John Doe\0").
498 rdn_attr_value( char * rdn )
503 if ( (str = strchr( rdn, '=' )) != NULL ) {
505 return get_next_substring(++str, '\0');
514 int rdn_validate( const char * rdn )
516 /* just a simple check for now */
517 return strchr( rdn, '=' ) != NULL;
523 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
526 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
530 build_new_dn( char ** new_dn,
533 const char * newrdn )
536 if ( p_dn == NULL ) {
538 *new_dn = ch_strdup( newrdn );
543 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
546 if ( dn_type( e_dn ) == DN_X500 ) {
549 strcpy( *new_dn, newrdn );
550 strcat( *new_dn, "," );
551 strcat( *new_dn, p_dn );
559 strcpy( *new_dn, newrdn );
560 s = strchr( newrdn, '\0' );
563 if ( (*s != '.') && (*s != '@') ) {
565 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
569 strcat( *new_dn, sep );
575 strcat( *new_dn, p_dn );