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 )
44 for ( d = s = dn; *s; s++ ) {
48 if ( OID_LEADCHAR(*s) ) {
51 } else if ( ATTR_LEADCHAR(*s) ) {
54 } else if ( ! ASCII_SPACE( *s ) ) {
64 } else if ( *s == '=' ) {
67 } else if ( ASCII_SPACE( *s ) ) {
76 if ( ATTR_CHAR(*s) ) {
78 } else if ( *s == '=' ) {
81 } else if ( ASCII_SPACE( *s ) ) {
93 } else if ( ! ASCII_SPACE( *s ) ) {
94 /* not a valid dn - but what can we do here? */
102 state = INQUOTEDVALUE;
104 } else if ( ! ASCII_SPACE( *s ) ) {
111 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
112 while ( ASCII_SPACE( *(d - 1) ) )
120 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
121 !RDN_SEPARATOR( *s ) ) {
130 if ( !gotesc && *s == '"' ) {
133 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
142 if ( RDN_SEPARATOR( *s ) ) {
150 Debug( LDAP_DEBUG_ANY,
151 "dn_validate - unknown state %d\n", state, 0, 0 );
164 /* shouldn't be left in escape */
168 /* check end state */
170 case B4LEADTYPE: /* looking for first type */
171 case B4SEPARATOR: /* looking for separator */
172 case INVALUE: /* inside value */
182 * dn_normalize - put dn into a canonical form suitable for storing
183 * in a hash database. this involves normalizing the case as well as
184 * the format. the dn is normalized in place as well as returned if valid.
188 dn_normalize( char *dn )
191 ldap_pvt_str2upper( dn );
193 /* validate and compress dn */
194 dn = dn_validate( dn );
200 * dn_parent - return a copy of the dn of dn's parent
216 while(*dn != '\0' && ASCII_SPACE(*dn)) {
224 if ( be != NULL && be_issuffix( be, dn ) ) {
229 * assume it is an X.500-style name, which looks like
230 * foo=bar,sha=baz,...
234 for ( s = dn; *s; s++ ) {
248 } else if ( DN_SEPARATOR( *s ) ) {
249 return( ch_strdup( &s[1] ) );
254 return( ch_strdup( "" ) );
264 if( dn_in == NULL ) {
268 while(*dn_in && ASCII_SPACE(*dn_in)) {
272 if( *dn_in == '\0' ) {
276 if ( be != NULL && be_issuffix( be, dn_in ) ) {
280 dn = ch_strdup( dn_in );
284 for ( s = dn; *s; s++ ) {
298 } else if ( DN_SEPARATOR( *s ) ) {
310 * return a charray of all subtrees to which the DN resides in
316 char *child, *parent;
317 char **subtree = NULL;
319 child = ch_strdup( dn );
322 charray_add( &subtree, child );
324 parent = dn_parent( be, child );
329 } while ( child != NULL );
336 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
337 * and suffix must be normalized.
346 int dnlen, suffixlen;
352 suffixlen = strlen( suffix );
353 dnlen = strlen( dn );
355 if ( suffixlen > dnlen ) {
359 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
363 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
366 * Copyright 1999, Juan C. Gomez, All rights reserved.
367 * This software is not subject to any license of Silicon Graphics
368 * Inc. or Purdue University.
370 * Redistribution and use in source and binary forms are permitted
371 * without restriction or fee of any kind as long as this notice
376 /* get_next_substring:
378 * Gets next substring in s, using d (or the end of the string '\0') as a
379 * string delimiter, and places it in a duplicated memory space. Leading
380 * spaces are ignored. String s **must** be null-terminated.
384 get_next_substring( const char * s, char d )
389 r = str = ch_malloc( strlen(s) + 1 );
391 /* Skip leading spaces */
393 while ( *s && ASCII_SPACE(*s) ) {
399 while ( *s && (*s != d) ) {
401 /* Don't stop when you see trailing spaces may be a multi-word
402 * string, i.e. name=John Doe!
418 * Given a string (i.e. an rdn) of the form:
419 * "attribute_type = attribute_value"
420 * this function returns the type of an attribute, that is the
421 * string "attribute_type" which is placed in newly allocated
422 * memory. The returned string will be null-terminated.
425 char * rdn_attr_type( const char * s )
427 return get_next_substring( s, '=' );
433 * Given a string (i.e. an rdn) of the form:
434 * "attribute_type = attribute_value"
435 * this function returns "attribute_type" which is placed in newly allocated
436 * memory. The returned string will be null-terminated and may contain
437 * spaces (i.e. "John Doe\0").
441 rdn_attr_value( const char * rdn )
446 if ( (str = strchr( rdn, '=' )) != NULL ) {
447 return get_next_substring(++str, '\0');
455 int rdn_validate( const char * rdn )
457 /* just a simple check for now */
458 return strchr( rdn, '=' ) != NULL;
464 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
467 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
471 build_new_dn( char ** new_dn,
474 const char * newrdn )
477 if ( p_dn == NULL ) {
478 *new_dn = ch_strdup( newrdn );
482 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
484 strcpy( *new_dn, newrdn );
485 strcat( *new_dn, "," );
486 strcat( *new_dn, p_dn );