1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2000 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_in )
39 #ifdef USE_LDAP_DN_PARSING
40 struct berval val, *normalized;
43 if ( dn_in == NULL || dn_in[ 0 ] == '\0' ) {
48 val.bv_len = strlen( dn_in );
50 rc = dnPretty( NULL, &val, &normalized );
51 if ( rc != LDAP_SUCCESS ) {
55 if ( val.bv_len < normalized->bv_len ) {
56 ber_bvfree( normalized );
60 AC_MEMCPY( dn_in, normalized->bv_val, normalized->bv_len + 1 );
61 ber_bvfree( normalized );
65 #else /* !USE_LDAP_DN_PARSING */
72 for ( d = s = dn; *s; s++ ) {
76 if ( OID_LEADCHAR(*s) ) {
79 } else if ( ATTR_LEADCHAR(*s) ) {
82 } else if ( ! ASCII_SPACE( *s ) ) {
92 } else if ( *s == '=' ) {
95 } else if ( ASCII_SPACE( *s ) ) {
104 if ( ATTR_CHAR(*s) ) {
106 } else if ( *s == '=' ) {
109 } else if ( ASCII_SPACE( *s ) ) {
121 } else if ( ! ASCII_SPACE( *s ) ) {
122 /* not a valid dn - but what can we do here? */
130 state = INQUOTEDVALUE;
132 } else if ( ! ASCII_SPACE( *s ) ) {
139 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
140 while ( ASCII_SPACE( *(d - 1) ) )
148 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
149 !RDN_SEPARATOR( *s ) )
153 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
159 if ( !gotesc && *s == '"' ) {
162 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
165 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
171 if ( RDN_SEPARATOR( *s ) ) {
174 } else if ( !ASCII_SPACE( *s ) ) {
182 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
183 "dn_validate: unknown state %d for dn \"%s\".\n",
186 Debug( LDAP_DEBUG_ANY,
187 "dn_validate - unknown state %d\n", state, 0, 0 );
199 /* trim trailing spaces */
200 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
206 /* shouldn't be left in escape */
210 /* check end state */
212 case B4LEADTYPE: /* looking for first type */
213 case B4SEPARATOR: /* looking for separator */
214 case INVALUE: /* inside value */
221 #endif /* !USE_LDAP_DN_PARSING */
225 * dn_normalize - put dn into a canonical form suitable for storing
226 * in a hash database. this involves normalizing the case as well as
227 * the format. the dn is normalized in place as well as returned if valid.
231 dn_normalize( char *dn )
233 #ifdef USE_LDAP_DN_PARSING
234 struct berval val, *normalized;
237 if ( dn == NULL || dn[ 0 ] == '\0' ) {
242 val.bv_len = strlen( dn );
244 rc = dnNormalize( NULL, &val, &normalized );
245 if ( rc != LDAP_SUCCESS ) {
249 if ( val.bv_len < normalized->bv_len ) {
250 ber_bvfree( normalized );
254 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
255 ber_bvfree( normalized );
259 #else /* !USE_LDAP_DN_PARSING */
261 struct berval *bvdn, *nbvdn;
264 bvdn = ber_bvstr( dn );
266 if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
267 if ( nbvdn->bv_len <= bvdn->bv_len ) {
269 strcpy( out, nbvdn->bv_val );
273 bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
277 #endif /* !USE_LDAP_DN_PARSING */
281 dn_match( const char *val, const char *asserted )
283 struct berval bval, basserted;
285 if ( val == NULL || asserted == NULL ) {
289 bval.bv_val = ( char * )val;
290 bval.bv_len = strlen( val );
292 basserted.bv_val = ( char * )asserted;
293 basserted.bv_len = strlen( asserted);
295 return dnMatch( NULL, 0, NULL, NULL, &bval, &basserted);
299 * dn_parent - return a copy of the dn of dn's parent
315 while(*dn != '\0' && ASCII_SPACE(*dn)) {
323 if ( be != NULL && be_issuffix( be, dn ) ) {
328 * assume it is an X.500-style name, which looks like
329 * foo=bar,sha=baz,...
333 for ( s = dn; *s; s++ ) {
347 } else if ( DN_SEPARATOR( *s ) ) {
348 return ch_strdup( &s[1] );
353 return ch_strdup( "" );
363 if( dn_in == NULL ) {
367 while(*dn_in && ASCII_SPACE(*dn_in)) {
371 if( *dn_in == '\0' ) {
375 if ( be != NULL && be_issuffix( be, dn_in ) ) {
379 dn = ch_strdup( dn_in );
383 for ( s = dn; *s; s++ ) {
397 } else if ( DN_SEPARATOR( *s ) ) {
409 * return a charray of all subtrees to which the DN resides in
415 char *child, *parent;
416 char **subtree = NULL;
418 child = ch_strdup( dn );
421 charray_add( &subtree, child );
423 parent = dn_parent( be, child );
428 } while ( child != NULL );
435 * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
436 * and suffix must be normalized.
445 int dnlen, suffixlen;
451 suffixlen = strlen( suffix );
452 dnlen = strlen( dn );
454 if ( suffixlen > dnlen ) {
458 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
462 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
465 * Copyright 1999, Juan C. Gomez, All rights reserved.
466 * This software is not subject to any license of Silicon Graphics
467 * Inc. or Purdue University.
469 * Redistribution and use in source and binary forms are permitted
470 * without restriction or fee of any kind as long as this notice
475 /* get_next_substring:
477 * Gets next substring in s, using d (or the end of the string '\0') as a
478 * string delimiter, and places it in a duplicated memory space. Leading
479 * spaces are ignored. String s **must** be null-terminated.
483 get_next_substring( const char * s, char d )
488 r = str = ch_malloc( strlen(s) + 1 );
490 /* Skip leading spaces */
492 while ( *s && ASCII_SPACE(*s) ) {
498 while ( *s && (*s != d) ) {
500 /* Don't stop when you see trailing spaces may be a multi-word
501 * string, i.e. name=John Doe!
516 * Given a string (i.e. an rdn) of the form:
517 * "attribute_type = attribute_value"
518 * this function returns the type of an attribute, that is the
519 * string "attribute_type" which is placed in newly allocated
520 * memory. The returned string will be null-terminated.
523 char * rdn_attr_type( const char * s )
525 return get_next_substring( s, '=' );
531 * Given a string (i.e. an rdn) of the form:
532 * "attribute_type = attribute_value"
533 * this function returns "attribute_type" which is placed in newly allocated
534 * memory. The returned string will be null-terminated and may contain
535 * spaces (i.e. "John Doe\0").
539 rdn_attr_value( const char * rdn )
544 if ( (str = strchr( rdn, '=' )) != NULL ) {
545 return get_next_substring(++str, '\0');
555 * Given a string (i.e. an rdn) of the form:
556 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
557 * this function stores the types of the attributes in ptypes, that is the
558 * array of strings "attribute_type" which is placed in newly allocated
559 * memory, and the values of the attributes in pvalues, that is the
560 * array of strings "attribute_value" which is placed in newly allocated
561 * memory. Returns 0 on success, -1 on failure.
563 * note: got part of the code from dn_validate
567 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
575 * explode the rdn in parts
577 parts = ldap_explode_rdn( rdn_in, 0 );
579 if ( parts == NULL ) {
583 for ( p = parts; p[0]; p++ ) {
586 /* split each rdn part in type value */
587 s = strchr( p[0], '=' );
589 charray_free( *ptypes );
590 charray_free( *pvalues );
591 charray_free( parts );
595 /* type should be fine */
596 charray_add_n( ptypes, p[0], ( s-p[0] ) );
598 /* value needs to be unescaped
599 * (maybe this should be moved to ldap_explode_rdn?) */
600 for ( e = d = s + 1; e[0]; e++ ) {
606 charray_add( pvalues, s + 1 );
610 charray_free( parts );
618 * 1 if rdn is a legal rdn;
619 * 0 otherwise (including a sequence of rdns)
621 * note: got it from dn_rdn; it should be rewritten
622 * according to dn_validate
625 rdn_validate( const char * rdn )
633 if ( strchr( rdn, '=' ) == NULL ) {
637 while ( *rdn && ASCII_SPACE( *rdn ) ) {
647 for ( ; *rdn; rdn++ ) {
648 if ( *rdn == '\\' ) {
661 } else if ( DN_SEPARATOR( *rdn ) ) {
673 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
676 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
680 build_new_dn( char ** new_dn,
683 const char * newrdn )
686 if ( p_dn == NULL ) {
687 *new_dn = ch_strdup( newrdn );
691 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
693 strcpy( *new_dn, newrdn );
694 strcat( *new_dn, "," );
695 strcat( *new_dn, p_dn );