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
33 dnNormalize( Syntax *syntax, struct berval *val, struct berval **normalized );
36 * dn_validate - validate and compress dn. the dn is
37 * compressed in place are returned if valid.
41 dn_validate( char *dn_in )
49 for ( d = s = dn; *s; s++ ) {
53 if ( OID_LEADCHAR(*s) ) {
56 } else if ( ATTR_LEADCHAR(*s) ) {
59 } else if ( ! ASCII_SPACE( *s ) ) {
69 } else if ( *s == '=' ) {
72 } else if ( ASCII_SPACE( *s ) ) {
81 if ( ATTR_CHAR(*s) ) {
83 } else if ( *s == '=' ) {
86 } else if ( ASCII_SPACE( *s ) ) {
98 } else if ( ! ASCII_SPACE( *s ) ) {
99 /* not a valid dn - but what can we do here? */
107 state = INQUOTEDVALUE;
109 } else if ( ! ASCII_SPACE( *s ) ) {
116 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
117 while ( ASCII_SPACE( *(d - 1) ) )
125 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
126 !RDN_SEPARATOR( *s ) )
130 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
136 if ( !gotesc && *s == '"' ) {
139 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
142 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
148 if ( RDN_SEPARATOR( *s ) ) {
151 } else if ( !ASCII_SPACE( *s ) ) {
159 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
160 "dn_validate: unknown state %d for dn \"%s\".\n",
163 Debug( LDAP_DEBUG_ANY,
164 "dn_validate - unknown state %d\n", state, 0, 0 );
176 /* trim trailing spaces */
177 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
183 /* shouldn't be left in escape */
187 /* check end state */
189 case B4LEADTYPE: /* looking for first type */
190 case B4SEPARATOR: /* looking for separator */
191 case INVALUE: /* inside value */
201 * dn_normalize - put dn into a canonical form suitable for storing
202 * in a hash database. this involves normalizing the case as well as
203 * the format. the dn is normalized in place as well as returned if valid.
207 dn_normalize( char *dn )
210 struct berval *bvdn, *nbvdn;
213 bvdn = ber_bvstr( dn );
215 if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
216 if ( nbvdn->bv_len <= bvdn->bv_len ) {
218 strcpy( out, nbvdn->bv_val );
222 bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
229 * dn_parent - return a copy of the dn of dn's parent
245 while(*dn != '\0' && ASCII_SPACE(*dn)) {
253 if ( be != NULL && be_issuffix( be, dn ) ) {
258 * assume it is an X.500-style name, which looks like
259 * foo=bar,sha=baz,...
263 for ( s = dn; *s; s++ ) {
277 } else if ( DN_SEPARATOR( *s ) ) {
278 return ch_strdup( &s[1] );
283 return ch_strdup( "" );
293 if( dn_in == NULL ) {
297 while(*dn_in && ASCII_SPACE(*dn_in)) {
301 if( *dn_in == '\0' ) {
305 if ( be != NULL && be_issuffix( be, dn_in ) ) {
309 dn = ch_strdup( dn_in );
313 for ( s = dn; *s; s++ ) {
327 } else if ( DN_SEPARATOR( *s ) ) {
339 * return a charray of all subtrees to which the DN resides in
345 char *child, *parent;
346 char **subtree = NULL;
348 child = ch_strdup( dn );
351 charray_add( &subtree, child );
353 parent = dn_parent( be, child );
358 } while ( child != NULL );
365 * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
366 * and suffix must be normalized.
375 int dnlen, suffixlen;
381 suffixlen = strlen( suffix );
382 dnlen = strlen( dn );
384 if ( suffixlen > dnlen ) {
388 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
392 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
395 * Copyright 1999, Juan C. Gomez, All rights reserved.
396 * This software is not subject to any license of Silicon Graphics
397 * Inc. or Purdue University.
399 * Redistribution and use in source and binary forms are permitted
400 * without restriction or fee of any kind as long as this notice
405 /* get_next_substring:
407 * Gets next substring in s, using d (or the end of the string '\0') as a
408 * string delimiter, and places it in a duplicated memory space. Leading
409 * spaces are ignored. String s **must** be null-terminated.
413 get_next_substring( const char * s, char d )
418 r = str = ch_malloc( strlen(s) + 1 );
420 /* Skip leading spaces */
422 while ( *s && ASCII_SPACE(*s) ) {
428 while ( *s && (*s != d) ) {
430 /* Don't stop when you see trailing spaces may be a multi-word
431 * string, i.e. name=John Doe!
446 * Given a string (i.e. an rdn) of the form:
447 * "attribute_type = attribute_value"
448 * this function returns the type of an attribute, that is the
449 * string "attribute_type" which is placed in newly allocated
450 * memory. The returned string will be null-terminated.
453 char * rdn_attr_type( const char * s )
455 return get_next_substring( s, '=' );
461 * Given a string (i.e. an rdn) of the form:
462 * "attribute_type = attribute_value"
463 * this function returns "attribute_type" which is placed in newly allocated
464 * memory. The returned string will be null-terminated and may contain
465 * spaces (i.e. "John Doe\0").
469 rdn_attr_value( const char * rdn )
474 if ( (str = strchr( rdn, '=' )) != NULL ) {
475 return get_next_substring(++str, '\0');
485 * Given a string (i.e. an rdn) of the form:
486 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
487 * this function stores the types of the attributes in ptypes, that is the
488 * array of strings "attribute_type" which is placed in newly allocated
489 * memory, and the values of the attributes in pvalues, that is the
490 * array of strings "attribute_value" which is placed in newly allocated
491 * memory. Returns 0 on success, -1 on failure.
493 * note: got part of the code from dn_validate
497 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
505 * explode the rdn in parts
507 parts = ldap_explode_rdn( rdn_in, 0 );
509 if ( parts == NULL ) {
513 for ( p = parts; p[0]; p++ ) {
516 /* split each rdn part in type value */
517 s = strchr( p[0], '=' );
519 charray_free( *ptypes );
520 charray_free( *pvalues );
521 charray_free( parts );
525 /* type should be fine */
526 charray_add_n( ptypes, p[0], ( s-p[0] ) );
528 /* value needs to be unescaped
529 * (maybe this should be moved to ldap_explode_rdn?) */
530 for ( e = d = s + 1; e[0]; e++ ) {
536 charray_add( pvalues, s + 1 );
540 charray_free( parts );
548 * 1 if rdn is a legal rdn;
549 * 0 otherwise (including a sequence of rdns)
551 * note: got it from dn_rdn; it should be rewritten
552 * according to dn_validate
555 rdn_validate( const char * rdn )
563 if ( strchr( rdn, '=' ) == NULL ) {
567 while ( *rdn && ASCII_SPACE( *rdn ) ) {
577 for ( ; *rdn; rdn++ ) {
578 if ( *rdn == '\\' ) {
591 } else if ( DN_SEPARATOR( *rdn ) ) {
603 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
606 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
610 build_new_dn( char ** new_dn,
613 const char * newrdn )
616 if ( p_dn == NULL ) {
617 *new_dn = ch_strdup( newrdn );
621 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
623 strcpy( *new_dn, newrdn );
624 strcat( *new_dn, "," );
625 strcat( *new_dn, p_dn );