1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-1999 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 ( LEADOIDCHAR(*s) ) {
51 } else if ( LEADKEYCHAR(*s) ) {
54 } else if ( ! SPACE( *s ) ) {
64 } else if ( *s == '=' ) {
67 } else if ( SPACE( *s ) ) {
78 } else if ( *s == '=' ) {
81 } else if ( SPACE( *s ) ) {
93 } else if ( ! SPACE( *s ) ) {
94 /* not a valid dn - but what can we do here? */
102 state = INQUOTEDVALUE;
104 } else if ( ! SPACE( *s ) ) {
111 if ( !gotesc && SEPARATOR( *s ) ) {
112 while ( SPACE( *(d - 1) ) )
120 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
130 if ( !gotesc && *s == '"' ) {
133 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
141 if ( SEPARATOR( *s ) ) {
148 Debug( LDAP_DEBUG_ANY,
149 "dn_validate - unknown state %d\n", state, 0, 0 );
161 /* shouldn't be left in escape */
165 /* check end state */
167 case B4LEADTYPE: /* looking for first type */
168 case B4SEPARATOR: /* looking for separator */
169 case INVALUE: /* inside value */
179 * dn_normalize - put dn into a canonical form suitable for storing
180 * in a hash database. this involves normalizing the case as well as
181 * the format. the dn is normalized in place as well as returned if valid.
185 dn_normalize( char *dn )
188 ldap_pvt_str2upper( dn );
190 /* validate and compress dn */
191 dn = dn_validate( dn );
197 * dn_parent - return a copy of the dn of dn's parent
213 while(*dn && SPACE(*dn)) {
221 if ( be != NULL && be_issuffix( be, dn ) ) {
226 * else assume it is an X.500-style name, which looks like
227 * foo=bar,sha=baz,...
231 for ( s = dn; *s; s++ ) {
245 } else if ( DNSEPARATOR( *s ) ) {
246 return( ch_strdup( &s[1] ) );
251 return( ch_strdup( "" ) );
265 while(*dn && SPACE(*dn)) {
273 if ( be != NULL && be_issuffix( be, dn ) ) {
277 dn = ch_strdup( dn );
281 for ( s = dn; *s; s++ ) {
295 } else if ( DNSEPARATOR( *s ) ) {
307 * return a charray of all subtrees to which the DN resides in
313 char *child, *parent;
314 char **subtree = NULL;
316 child = ch_strdup( dn );
319 charray_add( &subtree, child );
321 parent = dn_parent( be, child );
326 } while ( child != NULL );
333 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
334 * and suffix must be normalized.
343 int dnlen, suffixlen;
349 suffixlen = strlen( suffix );
350 dnlen = strlen( dn );
352 if ( suffixlen > dnlen ) {
356 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
360 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
363 * Copyright 1999, Juan C. Gomez, All rights reserved.
364 * This software is not subject to any license of Silicon Graphics
365 * Inc. or Purdue University.
367 * Redistribution and use in source and binary forms are permitted
368 * without restriction or fee of any kind as long as this notice
373 /* get_next_substring:
375 * Gets next substring in s, using d (or the end of the string '\0') as a
376 * string delimiter, and places it in a duplicated memory space. Leading
377 * spaces are ignored. String s **must** be null-terminated.
381 get_next_substring( char * s, char d )
386 r = str = ch_malloc( strlen(s) + 1 );
388 /* Skip leading spaces */
390 while ( *s && SPACE(*s) ) {
398 while ( *s && (*s != d) ) {
400 /* Don't stop when you see trailing spaces may be a multi-word
401 * string, i.e. name=John Doe!
417 * Given a string (i.e. an rdn) of the form:
418 * "attribute_type = attribute_value"
419 * this function returns the type of an attribute, that is the
420 * string "attribute_type" which is placed in newly allocated
421 * memory. The returned string will be null-terminated.
424 char * rdn_attr_type( char * s )
427 return get_next_substring( s, '=' );
434 * Given a string (i.e. an rdn) of the form:
435 * "attribute_type = attribute_value"
436 * this function returns "attribute_type" which is placed in newly allocated
437 * memory. The returned string will be null-terminated and may contain
438 * spaces (i.e. "John Doe\0").
442 rdn_attr_value( char * rdn )
447 if ( (str = strchr( rdn, '=' )) != NULL ) {
449 return get_next_substring(++str, '\0');
458 int rdn_validate( const char * rdn )
460 /* just a simple check for now */
461 return strchr( rdn, '=' ) != NULL;
467 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
470 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
474 build_new_dn( char ** new_dn,
477 const char * newrdn )
480 if ( p_dn == NULL ) {
482 *new_dn = ch_strdup( newrdn );
487 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
489 strcpy( *new_dn, newrdn );
490 strcat( *new_dn, "," );
491 strcat( *new_dn, p_dn );