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
34 * dn_validate - validate and compress dn. the dn is
35 * compressed in place are returned if valid.
39 dn_validate( char *dn_in )
47 for ( d = s = dn; *s; s++ ) {
51 if ( OID_LEADCHAR(*s) ) {
54 } else if ( ATTR_LEADCHAR(*s) ) {
57 } else if ( ! ASCII_SPACE( *s ) ) {
67 } else if ( *s == '=' ) {
70 } else if ( ASCII_SPACE( *s ) ) {
79 if ( ATTR_CHAR(*s) ) {
81 } else if ( *s == '=' ) {
84 } else if ( ASCII_SPACE( *s ) ) {
96 } else if ( ! ASCII_SPACE( *s ) ) {
97 /* not a valid dn - but what can we do here? */
105 state = INQUOTEDVALUE;
107 } else if ( ! ASCII_SPACE( *s ) ) {
114 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
115 while ( ASCII_SPACE( *(d - 1) ) )
123 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
124 !RDN_SEPARATOR( *s ) ) {
127 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
133 if ( !gotesc && *s == '"' ) {
136 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
139 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
145 if ( RDN_SEPARATOR( *s ) ) {
148 } else if ( !ASCII_SPACE( *s ) ) {
156 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
157 "dn_validate: unknown state %d for dn \"%s\".\n",
160 Debug( LDAP_DEBUG_ANY,
161 "dn_validate - unknown state %d\n", state, 0, 0 );
173 /* trim trailing spaces */
174 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
180 /* shouldn't be left in escape */
184 /* check end state */
186 case B4LEADTYPE: /* looking for first type */
187 case B4SEPARATOR: /* looking for separator */
188 case INVALUE: /* inside value */
198 * dn_normalize - put dn into a canonical form suitable for storing
199 * in a hash database. this involves normalizing the case as well as
200 * the format. the dn is normalized in place as well as returned if valid.
204 dn_normalize( char *dn )
209 ldap_pvt_str2upper( dn );
210 /* validate and compress dn */
211 out = dn_validate( dn );
213 /* enabling this might require reindexing */
214 struct berval *bvdn, *nbvdn;
217 bvdn = ber_bvstr( dn );
219 if ( dnNormalize( NULL, bvdn, &nbvdn ) == LDAP_SUCCESS ) {
220 if ( nbvdn->bv_len <= bvdn->bv_len ) {
222 strcpy( out, nbvdn->bv_val );
226 bvdn->bv_val = NULL; /* prevent bvfree from freeing dn */
234 * dn_parent - return a copy of the dn of dn's parent
250 while(*dn != '\0' && ASCII_SPACE(*dn)) {
258 if ( be != NULL && be_issuffix( be, dn ) ) {
263 * assume it is an X.500-style name, which looks like
264 * foo=bar,sha=baz,...
268 for ( s = dn; *s; s++ ) {
282 } else if ( DN_SEPARATOR( *s ) ) {
283 return ch_strdup( &s[1] );
288 return ch_strdup( "" );
298 if( dn_in == NULL ) {
302 while(*dn_in && ASCII_SPACE(*dn_in)) {
306 if( *dn_in == '\0' ) {
310 if ( be != NULL && be_issuffix( be, dn_in ) ) {
314 dn = ch_strdup( dn_in );
318 for ( s = dn; *s; s++ ) {
332 } else if ( DN_SEPARATOR( *s ) ) {
344 * return a charray of all subtrees to which the DN resides in
350 char *child, *parent;
351 char **subtree = NULL;
353 child = ch_strdup( dn );
356 charray_add( &subtree, child );
358 parent = dn_parent( be, child );
363 } while ( child != NULL );
370 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
371 * and suffix must be normalized.
380 int dnlen, suffixlen;
386 suffixlen = strlen( suffix );
387 dnlen = strlen( dn );
389 if ( suffixlen > dnlen ) {
393 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
397 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
400 * Copyright 1999, Juan C. Gomez, All rights reserved.
401 * This software is not subject to any license of Silicon Graphics
402 * Inc. or Purdue University.
404 * Redistribution and use in source and binary forms are permitted
405 * without restriction or fee of any kind as long as this notice
410 /* get_next_substring:
412 * Gets next substring in s, using d (or the end of the string '\0') as a
413 * string delimiter, and places it in a duplicated memory space. Leading
414 * spaces are ignored. String s **must** be null-terminated.
418 get_next_substring( const char * s, char d )
423 r = str = ch_malloc( strlen(s) + 1 );
425 /* Skip leading spaces */
427 while ( *s && ASCII_SPACE(*s) ) {
433 while ( *s && (*s != d) ) {
435 /* Don't stop when you see trailing spaces may be a multi-word
436 * string, i.e. name=John Doe!
452 * Given a string (i.e. an rdn) of the form:
453 * "attribute_type = attribute_value"
454 * this function returns the type of an attribute, that is the
455 * string "attribute_type" which is placed in newly allocated
456 * memory. The returned string will be null-terminated.
459 char * rdn_attr_type( const char * s )
461 return get_next_substring( s, '=' );
467 * Given a string (i.e. an rdn) of the form:
468 * "attribute_type = attribute_value"
469 * this function returns "attribute_type" which is placed in newly allocated
470 * memory. The returned string will be null-terminated and may contain
471 * spaces (i.e. "John Doe\0").
475 rdn_attr_value( const char * rdn )
480 if ( (str = strchr( rdn, '=' )) != NULL ) {
481 return get_next_substring(++str, '\0');
489 int rdn_validate( const char * rdn )
491 /* just a simple check for now */
492 return strchr( rdn, '=' ) != NULL;
498 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
501 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
505 build_new_dn( char ** new_dn,
508 const char * newrdn )
511 if ( p_dn == NULL ) {
512 *new_dn = ch_strdup( newrdn );
516 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
518 strcpy( *new_dn, newrdn );
519 strcat( *new_dn, "," );
520 strcat( *new_dn, p_dn );