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 fprintf(stderr, ">>> dn_validate: %s\n", dn_in);
45 if ( dn_in == NULL || dn_in[ 0 ] == '\0' ) {
50 val.bv_len = strlen( dn_in );
52 rc = dnPretty( NULL, &val, &normalized );
53 if ( rc != LDAP_SUCCESS ) {
57 if ( val.bv_len < normalized->bv_len ) {
58 ber_bvfree( normalized );
62 AC_MEMCPY( dn_in, normalized->bv_val, normalized->bv_len + 1 );
63 ber_bvfree( normalized );
65 fprintf(stderr, "<<< dn_validate: %s\n", dn_in);
69 #else /* !USE_LDAP_DN_PARSING */
76 for ( d = s = dn; *s; s++ ) {
80 if ( OID_LEADCHAR(*s) ) {
83 } else if ( ATTR_LEADCHAR(*s) ) {
86 } else if ( ! ASCII_SPACE( *s ) ) {
96 } else if ( *s == '=' ) {
99 } else if ( ASCII_SPACE( *s ) ) {
108 if ( ATTR_CHAR(*s) ) {
110 } else if ( *s == '=' ) {
113 } else if ( ASCII_SPACE( *s ) ) {
125 } else if ( ! ASCII_SPACE( *s ) ) {
126 /* not a valid dn - but what can we do here? */
134 state = INQUOTEDVALUE;
136 } else if ( ! ASCII_SPACE( *s ) ) {
143 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
144 while ( ASCII_SPACE( *(d - 1) ) )
152 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
153 !RDN_SEPARATOR( *s ) )
157 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
163 if ( !gotesc && *s == '"' ) {
166 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
169 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
175 if ( RDN_SEPARATOR( *s ) ) {
178 } else if ( !ASCII_SPACE( *s ) ) {
186 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
187 "dn_validate: unknown state %d for dn \"%s\".\n",
190 Debug( LDAP_DEBUG_ANY,
191 "dn_validate - unknown state %d\n", state, 0, 0 );
203 /* trim trailing spaces */
204 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
210 /* shouldn't be left in escape */
214 /* check end state */
216 case B4LEADTYPE: /* looking for first type */
217 case B4SEPARATOR: /* looking for separator */
218 case INVALUE: /* inside value */
225 #endif /* !USE_LDAP_DN_PARSING */
229 * dn_normalize - put dn into a canonical form suitable for storing
230 * in a hash database. this involves normalizing the case as well as
231 * the format. the dn is normalized in place as well as returned if valid.
235 dn_normalize( char *dn )
237 #ifdef USE_LDAP_DN_PARSING
238 struct berval val, *normalized;
241 fprintf(stderr, ">>> dn_normalize: %s\n", dn);
243 if ( dn == NULL || dn[ 0 ] == '\0' ) {
248 val.bv_len = strlen( dn );
250 rc = dnNormalize( NULL, &val, &normalized );
251 if ( rc != LDAP_SUCCESS ) {
255 if ( val.bv_len < normalized->bv_len ) {
256 ber_bvfree( normalized );
260 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
261 ber_bvfree( normalized );
263 ( void )ldap_pvt_str2upper( dn );
265 fprintf(stderr, "<<< dn_normalize: %s\n", dn);
269 #else /* !USE_LDAP_DN_PARSING */
271 struct berval *bvdn, *nbvdn;
274 bvdn = ber_bvstr( dn );
276 if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
277 if ( nbvdn->bv_len <= bvdn->bv_len ) {
279 strcpy( out, nbvdn->bv_val );
283 bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
287 #endif /* !USE_LDAP_DN_PARSING */
291 dn_match( const char *val, const char *asserted )
293 struct berval bval, basserted;
295 if ( val == NULL || asserted == NULL ) {
299 bval.bv_val = ( char * )val;
300 bval.bv_len = strlen( val );
302 basserted.bv_val = ( char * )asserted;
303 basserted.bv_len = strlen( asserted);
305 return dnMatch( NULL, 0, NULL, NULL, &bval, &basserted);
309 * dn_parent - return a copy of the dn of dn's parent
325 while(*dn != '\0' && ASCII_SPACE(*dn)) {
333 if ( be != NULL && be_issuffix( be, dn ) ) {
338 * assume it is an X.500-style name, which looks like
339 * foo=bar,sha=baz,...
343 for ( s = dn; *s; s++ ) {
357 } else if ( DN_SEPARATOR( *s ) ) {
358 return ch_strdup( &s[1] );
363 return ch_strdup( "" );
373 if( dn_in == NULL ) {
377 while(*dn_in && ASCII_SPACE(*dn_in)) {
381 if( *dn_in == '\0' ) {
385 if ( be != NULL && be_issuffix( be, dn_in ) ) {
389 dn = ch_strdup( dn_in );
393 for ( s = dn; *s; s++ ) {
407 } else if ( DN_SEPARATOR( *s ) ) {
419 * return a charray of all subtrees to which the DN resides in
425 char *child, *parent;
426 char **subtree = NULL;
428 child = ch_strdup( dn );
431 charray_add( &subtree, child );
433 parent = dn_parent( be, child );
438 } while ( child != NULL );
445 * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
446 * and suffix must be normalized.
455 int dnlen, suffixlen;
461 suffixlen = strlen( suffix );
462 dnlen = strlen( dn );
464 if ( suffixlen > dnlen ) {
468 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
472 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
475 * Copyright 1999, Juan C. Gomez, All rights reserved.
476 * This software is not subject to any license of Silicon Graphics
477 * Inc. or Purdue University.
479 * Redistribution and use in source and binary forms are permitted
480 * without restriction or fee of any kind as long as this notice
485 /* get_next_substring:
487 * Gets next substring in s, using d (or the end of the string '\0') as a
488 * string delimiter, and places it in a duplicated memory space. Leading
489 * spaces are ignored. String s **must** be null-terminated.
493 get_next_substring( const char * s, char d )
498 r = str = ch_malloc( strlen(s) + 1 );
500 /* Skip leading spaces */
502 while ( *s && ASCII_SPACE(*s) ) {
508 while ( *s && (*s != d) ) {
510 /* Don't stop when you see trailing spaces may be a multi-word
511 * string, i.e. name=John Doe!
526 * Given a string (i.e. an rdn) of the form:
527 * "attribute_type = attribute_value"
528 * this function returns the type of an attribute, that is the
529 * string "attribute_type" which is placed in newly allocated
530 * memory. The returned string will be null-terminated.
533 char * rdn_attr_type( const char * s )
535 return get_next_substring( s, '=' );
541 * Given a string (i.e. an rdn) of the form:
542 * "attribute_type = attribute_value"
543 * this function returns "attribute_type" which is placed in newly allocated
544 * memory. The returned string will be null-terminated and may contain
545 * spaces (i.e. "John Doe\0").
549 rdn_attr_value( const char * rdn )
554 if ( (str = strchr( rdn, '=' )) != NULL ) {
555 return get_next_substring(++str, '\0');
565 * Given a string (i.e. an rdn) of the form:
566 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
567 * this function stores the types of the attributes in ptypes, that is the
568 * array of strings "attribute_type" which is placed in newly allocated
569 * memory, and the values of the attributes in pvalues, that is the
570 * array of strings "attribute_value" which is placed in newly allocated
571 * memory. Returns 0 on success, -1 on failure.
573 * note: got part of the code from dn_validate
577 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
585 * explode the rdn in parts
587 parts = ldap_explode_rdn( rdn_in, 0 );
589 if ( parts == NULL ) {
593 for ( p = parts; p[0]; p++ ) {
596 /* split each rdn part in type value */
597 s = strchr( p[0], '=' );
599 charray_free( *ptypes );
600 charray_free( *pvalues );
601 charray_free( parts );
605 /* type should be fine */
606 charray_add_n( ptypes, p[0], ( s-p[0] ) );
608 /* value needs to be unescaped
609 * (maybe this should be moved to ldap_explode_rdn?) */
610 for ( e = d = s + 1; e[0]; e++ ) {
616 charray_add( pvalues, s + 1 );
620 charray_free( parts );
628 * 1 if rdn is a legal rdn;
629 * 0 otherwise (including a sequence of rdns)
631 * note: got it from dn_rdn; it should be rewritten
632 * according to dn_validate
635 rdn_validate( const char * rdn )
643 if ( strchr( rdn, '=' ) == NULL ) {
647 while ( *rdn && ASCII_SPACE( *rdn ) ) {
657 for ( ; *rdn; rdn++ ) {
658 if ( *rdn == '\\' ) {
671 } else if ( DN_SEPARATOR( *rdn ) ) {
683 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
686 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
690 build_new_dn( char ** new_dn,
693 const char * newrdn )
696 if ( p_dn == NULL ) {
697 *new_dn = ch_strdup( newrdn );
701 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
703 strcpy( *new_dn, newrdn );
704 strcat( *new_dn, "," );
705 strcat( *new_dn, p_dn );