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 )
134 /* normalize format */
137 /* and upper case it */
138 return( str2upper( dn ) );
142 * dn_parent - return a copy of the dn of dn's parent
158 while(*dn && SPACE(*dn)) {
166 if ( be != NULL && be_issuffix( be, dn ) ) {
171 * no =, assume it is a dns name, like blah@some.domain.name
172 * if the blah@ part is there, return some.domain.name. if
173 * it's just some.domain.name, return domain.name.
175 if ( strchr( dn, '=' ) == NULL ) {
176 if ( (s = strchr( dn, '@' )) == NULL ) {
177 if ( (s = strchr( dn, '.' )) == NULL ) {
181 if ( *(s + 1) == '\0' ) {
184 return( ch_strdup( &s[1] ) );
189 * else assume it is an X.500-style name, which looks like
190 * foo=bar,sha=baz,...
194 for ( s = dn; *s; s++ ) {
208 } else if ( DNSEPARATOR( *s ) ) {
209 return( ch_strdup( &s[1] ) );
214 return( ch_strdup( "" ) );
228 while(*dn && SPACE(*dn)) {
236 if ( be != NULL && be_issuffix( be, dn ) ) {
240 dn = ch_strdup( dn );
243 * no =, assume it is a dns name, like blah@some.domain.name
244 * if the blah@ part is there, return some.domain.name. if
245 * it's just some.domain.name, return domain.name.
247 if ( strchr( dn, '=' ) == NULL ) {
248 if ( (s = strchr( dn, '@' )) == NULL ) {
249 if ( (s = strchr( dn, '.' )) == NULL ) {
258 * else assume it is an X.500-style name, which looks like
259 * foo=bar,sha=baz,...
264 for ( s = dn; *s; s++ ) {
278 } else if ( DNSEPARATOR( *s ) ) {
289 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
290 * and suffix must be normalized.
299 int dnlen, suffixlen;
305 suffixlen = strlen( suffix );
306 dnlen = strlen( dn );
308 if ( suffixlen > dnlen ) {
312 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
316 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
317 * returns (defined in slap.h): DN_DNS dns-style thing
318 * DN_X500 x500-style thing
324 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
328 str2upper( char *str )
333 for ( s = str; *s; s++ ) {
334 *s = TOUPPER( (unsigned char) *s );
341 str2lower( char *str )
346 for ( s = str; *s; s++ ) {
347 *s = TOLOWER( (unsigned char) *s );
355 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
358 * Copyright 1999, Juan C. Gomez, All rights reserved.
359 * This software is not subject to any license of Silicon Graphics
360 * Inc. or Purdue University.
362 * Redistribution and use in source and binary forms are permitted
363 * without restriction or fee of any kind as long as this notice
368 /* get_next_substring:
370 * Gets next substring in s, using d (or the end of the string '\0') as a
371 * string delimiter, and places it in a duplicated memory space. Leading
372 * spaces are ignored. String s **must** be null-terminated.
376 get_next_substring( char * s, char d )
381 r = str = ch_malloc( strlen(s) + 1 );
383 /* Skip leading spaces */
385 while ( *s && SPACE(*s) ) {
389 }/* while ( *s && SPACE(*s) ) */
393 while ( *s && (*s != d) ) {
395 /* Don't stop when you see trailing spaces may be a multi-word
396 * string, i.e. name=John Doe!
401 }/* while ( *s && (*s != d) ) */
407 }/* char * get_word() */
412 * Given a string (i.e. an rdn) of the form:
413 * "attribute_type = attribute_value"
414 * this function returns the type of an attribute, that is the
415 * string "attribute_type" which is placed in newly allocated
416 * memory. The returned string will be null-terminated.
419 char * rdn_attr_type( char * s )
422 return get_next_substring( s, '=' );
424 }/* char * rdn_attr_type() */
429 * Given a string (i.e. an rdn) of the form:
430 * "attribute_type = attribute_value"
431 * this function returns "attribute_type" which is placed in newly allocated
432 * memory. The returned string will be null-terminated and may contain
433 * spaces (i.e. "John Doe\0").
437 rdn_attr_value( char * rdn )
442 if ( (str = strchr( rdn, '=' )) != NULL ) {
444 return get_next_substring(++str, '\0');
446 }/* if ( (str = strpbrk( rdn, "=" )) != NULL ) */
450 }/* char * rdn_attr_value() */
455 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
458 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
462 build_new_dn( char ** new_dn, char *e_dn, char * p_dn, char * newrdn )
465 if ( p_dn == NULL ) {
467 *new_dn = ch_strdup( newrdn );
472 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
474 if ( dn_type( e_dn ) == DN_X500 ) {
476 strcpy( *new_dn, newrdn );
477 strcat( *new_dn, "," );
478 strcat( *new_dn, p_dn );
485 strcpy( *new_dn, newrdn );
486 s = strchr( newrdn, '\0' );
489 if ( (*s != '.') && (*s != '@') ) {
491 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
495 strcat( *new_dn, sep );
497 }/* if ( (s = strpbrk( dn, ".@" )) != NULL ) */
499 }/* if ( *s != '.' && *s != '@' ) */
501 strcat( *new_dn, p_dn );
503 }/* if ( dn_type( e_dn ) == DN_X500 ) {}else */
505 }/* void build_new_dn() */