1 /* dn.c - routines for dealing with distinguished names */
19 #define INQUOTEDVALUE 5
23 * dn_normalize - put dn into a canonical format. the dn is
24 * normalized in place, as well as returned.
28 dn_normalize( char *dn )
33 /* Debug( LDAP_DEBUG_TRACE, "=> dn_normalize \"%s\"\n", dn, 0, 0 ); */
37 for ( d = s = dn; *s; s++ ) {
40 if ( ! SPACE( *s ) ) {
49 } else if ( SPACE( *s ) ) {
59 } else if ( ! SPACE( *s ) ) {
60 /* not a valid dn - but what can we do here? */
66 state = INQUOTEDVALUE;
68 } else if ( ! SPACE( *s ) ) {
74 if ( !gotesc && SEPARATOR( *s ) ) {
75 while ( SPACE( *(d - 1) ) )
83 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
92 if ( !gotesc && *s == '"' ) {
95 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
103 if ( SEPARATOR( *s ) ) {
109 Debug( LDAP_DEBUG_ANY,
110 "dn_normalize - unknown state %d\n", state, 0, 0 );
121 /* Debug( LDAP_DEBUG_TRACE, "<= dn_normalize \"%s\"\n", dn, 0, 0 ); */
126 * dn_normalize_case - put dn into a canonical form suitable for storing
127 * in a hash database. this involves normalizing the case as well as
128 * the format. the dn is normalized in place as well as returned.
132 dn_normalize_case( char *dn )
136 /* normalize format */
140 for ( s = dn; *s; s++ ) {
141 *s = TOUPPER( (unsigned char) *s );
148 * dn_parent - return a copy of the dn of dn's parent
164 while(*dn && SPACE(*dn)) {
172 if ( be != NULL && be_issuffix( be, dn ) ) {
177 * no =, assume it is a dns name, like blah@some.domain.name
178 * if the blah@ part is there, return some.domain.name. if
179 * it's just some.domain.name, return domain.name.
181 if ( strchr( dn, '=' ) == NULL ) {
182 if ( (s = strchr( dn, '@' )) == NULL ) {
183 if ( (s = strchr( dn, '.' )) == NULL ) {
187 if ( *(s + 1) == '\0' ) {
190 return( ch_strdup( &s[1] ) );
195 * else assume it is an X.500-style name, which looks like
196 * foo=bar,sha=baz,...
200 for ( s = dn; *s; s++ ) {
214 } else if ( DNSEPARATOR( *s ) ) {
215 return( ch_strdup( &s[1] ) );
220 return( ch_strdup( "" ) );
234 while(*dn && SPACE(*dn)) {
242 if ( be != NULL && be_issuffix( be, dn ) ) {
246 dn = ch_strdup( dn );
249 * no =, assume it is a dns name, like blah@some.domain.name
250 * if the blah@ part is there, return some.domain.name. if
251 * it's just some.domain.name, return domain.name.
253 if ( strchr( dn, '=' ) == NULL ) {
254 if ( (s = strchr( dn, '@' )) == NULL ) {
255 if ( (s = strchr( dn, '.' )) == NULL ) {
264 * else assume it is an X.500-style name, which looks like
265 * foo=bar,sha=baz,...
270 for ( s = dn; *s; s++ ) {
284 } else if ( DNSEPARATOR( *s ) ) {
295 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
296 * and suffix must be normalized.
305 int dnlen, suffixlen;
311 suffixlen = strlen( suffix );
312 dnlen = strlen( dn );
314 if ( suffixlen > dnlen ) {
318 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
322 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
323 * returns (defined in slap.h): DN_DNS dns-style thing
324 * DN_X500 x500-style thing
330 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
334 dn_upcase( char *dn )
339 for ( s = dn; *s; s++ ) {
340 *s = TOUPPER( (unsigned char) *s );
348 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
351 * Copyright 1999, Juan C. Gomez, All rights reserved.
352 * This software is not subject to any license of Silicon Graphics
353 * Inc. or Purdue University.
355 * Redistribution and use in source and binary forms are permitted
356 * without restriction or fee of any kind as long as this notice
361 /* get_next_substring:
363 * Gets next substring in s, using d (or the end of the string '\0') as a
364 * string delimiter, and places it in a duplicated memory space. Leading
365 * spaces are ignored. String s **must** be null-terminated.
369 get_next_substring( char * s, char d )
374 r = str = ch_malloc( strlen(s) + 1 );
376 /* Skip leading spaces */
378 while ( *s && SPACE(*s) ) {
382 }/* while ( *s && SPACE(*s) ) */
386 while ( *s && (*s != d) ) {
388 /* Don't stop when you see trailing spaces may be a multi-word
389 * string, i.e. name=John Doe!
394 }/* while ( *s && (*s != d) ) */
400 }/* char * get_word() */
405 * Given a string (i.e. an rdn) of the form:
406 * "attribute_type = attribute_value"
407 * this function returns the type of an attribute, that is the
408 * string "attribute_type" which is placed in newly allocated
409 * memory. The returned string will be null-terminated.
412 char * rdn_attr_type( char * s )
415 return get_next_substring( s, '=' );
417 }/* char * rdn_attr_type() */
422 * Given a string (i.e. an rdn) of the form:
423 * "attribute_type = attribute_value"
424 * this function returns "attribute_type" which is placed in newly allocated
425 * memory. The returned string will be null-terminated and may contain
426 * spaces (i.e. "John Doe\0").
430 rdn_attr_value( char * rdn )
435 if ( (str = strchr( rdn, '=' )) != NULL ) {
437 return get_next_substring(++str, '\0');
439 }/* if ( (str = strpbrk( rdn, "=" )) != NULL ) */
443 }/* char * rdn_attr_value() */
448 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
451 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
455 build_new_dn( char ** new_dn, char *e_dn, char * p_dn, char * newrdn )
458 if ( p_dn == NULL ) {
460 *new_dn = ch_strdup( newrdn );
465 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
467 if ( dn_type( e_dn ) == DN_X500 ) {
469 strcpy( *new_dn, newrdn );
470 strcat( *new_dn, "," );
471 strcat( *new_dn, p_dn );
478 strcpy( *new_dn, newrdn );
479 s = strchr( newrdn, '\0' );
482 if ( (*s != '.') && (*s != '@') ) {
484 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
488 strcat( *new_dn, sep );
490 }/* if ( (s = strpbrk( dn, ".@" )) != NULL ) */
492 }/* if ( *s != '.' && *s != '@' ) */
494 strcat( *new_dn, p_dn );
496 }/* if ( dn_type( e_dn ) == DN_X500 ) {}else */
498 }/* void build_new_dn() */