1 /* dn.c - routines for dealing with distinguished names */
21 #define INQUOTEDVALUE 7
25 * dn_normalize - put dn into a canonical format. the dn is
26 * normalized in place, as well as returned if valid.
30 dn_normalize( char *dn )
37 for ( d = s = dn; *s; s++ ) {
41 if ( LEADOIDCHAR(*s) ) {
44 } else if ( LEADKEYCHAR(*s) ) {
47 } else if ( ! SPACE( *s ) ) {
57 } else if ( *s == '=' ) {
60 } else if ( SPACE( *s ) ) {
71 } else if ( *s == '=' ) {
74 } else if ( SPACE( *s ) ) {
86 } else if ( ! SPACE( *s ) ) {
87 /* not a valid dn - but what can we do here? */
95 state = INQUOTEDVALUE;
97 } else if ( ! SPACE( *s ) ) {
104 if ( !gotesc && SEPARATOR( *s ) ) {
105 while ( SPACE( *(d - 1) ) )
113 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
123 if ( !gotesc && *s == '"' ) {
126 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
134 if ( SEPARATOR( *s ) ) {
141 Debug( LDAP_DEBUG_ANY,
142 "dn_normalize - unknown state %d\n", state, 0, 0 );
154 /* shouldn't be left in escape */
158 /* check end state */
160 case B4LEADTYPE: /* looking for first type */
161 case B4SEPARATOR: /* looking for separator */
162 case INVALUE: /* inside value */
172 * dn_normalize_case - put dn into a canonical form suitable for storing
173 * in a hash database. this involves normalizing the case as well as
174 * the format. the dn is normalized in place as well as returned if valid.
178 dn_normalize_case( char *dn )
182 /* normalize format */
183 dn = dn_normalize( dn );
185 /* and upper case it */
190 * dn_parent - return a copy of the dn of dn's parent
206 while(*dn && SPACE(*dn)) {
214 if ( be != NULL && be_issuffix( be, dn ) ) {
219 * no =, assume it is a dns name, like blah@some.domain.name
220 * if the blah@ part is there, return some.domain.name. if
221 * it's just some.domain.name, return domain.name.
223 if ( strchr( dn, '=' ) == NULL ) {
224 if ( (s = strchr( dn, '@' )) == NULL ) {
225 if ( (s = strchr( dn, '.' )) == NULL ) {
229 if ( *(s + 1) == '\0' ) {
232 return( ch_strdup( &s[1] ) );
237 * else assume it is an X.500-style name, which looks like
238 * foo=bar,sha=baz,...
242 for ( s = dn; *s; s++ ) {
256 } else if ( DNSEPARATOR( *s ) ) {
257 return( ch_strdup( &s[1] ) );
262 return( ch_strdup( "" ) );
276 while(*dn && SPACE(*dn)) {
284 if ( be != NULL && be_issuffix( be, dn ) ) {
288 dn = ch_strdup( dn );
292 * no =, assume it is a dns name, like blah@some.domain.name
293 * if the blah@ part is there, return some.domain.name. if
294 * it's just some.domain.name, return domain.name.
296 if ( strchr( dn, '=' ) == NULL ) {
297 if ( (s = strchr( dn, '@' )) == NULL ) {
298 if ( (s = strchr( dn, '.' )) == NULL ) {
308 * else assume it is an X.500-style name, which looks like
309 * foo=bar,sha=baz,...
314 for ( s = dn; *s; s++ ) {
328 } else if ( DNSEPARATOR( *s ) ) {
339 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
340 * and suffix must be normalized.
349 int dnlen, suffixlen;
355 suffixlen = strlen( suffix );
356 dnlen = strlen( dn );
358 if ( suffixlen > dnlen ) {
362 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
367 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
368 * returns (defined in slap.h): DN_DNS dns-style thing
369 * DN_X500 x500-style thing
375 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
380 str2upper( char *str )
385 for ( s = str; *s; s++ ) {
386 *s = TOUPPER( (unsigned char) *s );
393 str2lower( char *str )
398 for ( s = str; *s; s++ ) {
399 *s = TOLOWER( (unsigned char) *s );
407 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
410 * Copyright 1999, Juan C. Gomez, All rights reserved.
411 * This software is not subject to any license of Silicon Graphics
412 * Inc. or Purdue University.
414 * Redistribution and use in source and binary forms are permitted
415 * without restriction or fee of any kind as long as this notice
420 /* get_next_substring:
422 * Gets next substring in s, using d (or the end of the string '\0') as a
423 * string delimiter, and places it in a duplicated memory space. Leading
424 * spaces are ignored. String s **must** be null-terminated.
428 get_next_substring( char * s, char d )
433 r = str = ch_malloc( strlen(s) + 1 );
435 /* Skip leading spaces */
437 while ( *s && SPACE(*s) ) {
445 while ( *s && (*s != d) ) {
447 /* Don't stop when you see trailing spaces may be a multi-word
448 * string, i.e. name=John Doe!
464 * Given a string (i.e. an rdn) of the form:
465 * "attribute_type = attribute_value"
466 * this function returns the type of an attribute, that is the
467 * string "attribute_type" which is placed in newly allocated
468 * memory. The returned string will be null-terminated.
471 char * rdn_attr_type( char * s )
474 return get_next_substring( s, '=' );
481 * Given a string (i.e. an rdn) of the form:
482 * "attribute_type = attribute_value"
483 * this function returns "attribute_type" which is placed in newly allocated
484 * memory. The returned string will be null-terminated and may contain
485 * spaces (i.e. "John Doe\0").
489 rdn_attr_value( char * rdn )
494 if ( (str = strchr( rdn, '=' )) != NULL ) {
496 return get_next_substring(++str, '\0');
505 int rdn_validate( const char * rdn )
507 /* just a simple check for now */
508 return strchr( rdn, '=' ) != NULL;
514 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
517 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
521 build_new_dn( char ** new_dn, char *e_dn, char * p_dn, char * newrdn )
524 if ( p_dn == NULL ) {
526 *new_dn = ch_strdup( newrdn );
531 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
534 if ( dn_type( e_dn ) == DN_X500 ) {
537 strcpy( *new_dn, newrdn );
538 strcat( *new_dn, "," );
539 strcat( *new_dn, p_dn );
547 strcpy( *new_dn, newrdn );
548 s = strchr( newrdn, '\0' );
551 if ( (*s != '.') && (*s != '@') ) {
553 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
557 strcat( *new_dn, sep );
563 strcat( *new_dn, p_dn );