1 /* dn.c - routines for dealing with distinguished names */
14 typedef enum DnState {
15 B4TYPE, /* before attribute type */
16 INTYPE, /* in attribute type */
17 B4EQUAL, /* before '=' */
18 B4VALUE, /* before attribute value */
19 INVALUE, /* in attribute value */
20 INQUOTEDVALUE, /* in "" in attribute value */
21 B4SEPARATOR, /* before separator ('+', ',' or ';') */
25 * dn_normalize_internal - put dn into a canonical form suitable for storing
26 * in a hash database. If correct_case == 1, this involves normalizing the case
27 * as well as the format. The dn is normalized in place as well as returned.
29 * The dn_normalize() and dn_normalize_case() macros use this function.
33 dn_normalize_internal( char *dn, int correct_case )
35 char *s, *d; /* source and destination pointers */
36 char *type; /* start of attr.type when state==INTYPE */
37 int gotesc; /* last char was '\\' */
38 int ic; /* ignore case */
41 /* Debug( LDAP_DEBUG_TRACE, "=> dn_normalize \"%s\"\n", dn, 0, 0 ); */
45 for ( d = s = dn; *s; s++ ) {
48 if ( ! SPACE( *s ) ) {
52 *d++ = TOUPPER( (unsigned char) *s );
58 } else if ( SPACE( *s ) ) {
61 *d++ = TOUPPER( (unsigned char) *s );
64 /* Check if case is ignored in this type */
67 if ( ! (attr_syntax( type ) | SYNTAX_CIS) )
77 } else if ( ! SPACE( *s ) ) {
78 /* not a valid dn - but what can we do here? */
79 *d++ = TOUPPER( (unsigned char) *s );
84 state = INQUOTEDVALUE;
86 } else if ( ! SPACE( *s ) ) {
88 *d++ = (ic ? TOUPPER((unsigned char) *s) : *s);
92 if ( !gotesc && SEPARATOR( *s ) ) {
93 while ( SPACE( *(d - 1) ) )
101 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
103 *--d = (ic ? TOUPPER((unsigned char) *s) : *s);
106 *d++ = (ic ? TOUPPER((unsigned char) *s) : *s);
110 if ( !gotesc && *s == '"' ) {
113 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
114 *--d = (ic ? TOUPPER((unsigned char) *s) : *s);
117 *d++ = (ic ? TOUPPER((unsigned char) *s) : *s);
121 if ( SEPARATOR( *s ) ) {
127 Debug( LDAP_DEBUG_ANY,
128 "dn_normalize - unknown state %d\n", state, 0, 0 );
139 /* Debug( LDAP_DEBUG_TRACE, "<= dn_normalize \"%s\"\n", dn, 0, 0 ); */
145 * dn_casecmp - compare two DNs after normalizing (private copies of) them
149 dn_casecmp( const char *dn1, const char *dn2 )
151 char *ndn1 = dn_normalize_case( ch_strdup( dn1 ) );
152 char *ndn2 = dn_normalize_case( ch_strdup( dn2 ) );
153 int i = strcmp( ndn1, ndn2 );
161 * dn_parent - return a copy of the dn of dn's parent
177 while(*dn && SPACE(*dn)) {
185 if ( be_issuffix( be, dn ) ) {
190 * no =, assume it is a dns name, like blah@some.domain.name
191 * if the blah@ part is there, return some.domain.name. if
192 * it's just some.domain.name, return domain.name.
194 if ( strchr( dn, '=' ) == NULL ) {
195 if ( (s = strchr( dn, '@' )) == NULL ) {
196 if ( (s = strchr( dn, '.' )) == NULL ) {
200 if ( *(s + 1) == '\0' ) {
203 return( ch_strdup( &s[1] ) );
208 * else assume it is an X.500-style name, which looks like
209 * foo=bar,sha=baz,...
213 for ( s = dn; *s; s++ ) {
227 } else if ( DNSEPARATOR( *s ) ) {
228 return( ch_strdup( &s[1] ) );
233 return( ch_strdup( "" ) );
247 while(*dn && SPACE(*dn)) {
255 if ( be_issuffix( be, dn ) ) {
259 dn = ch_strdup( dn );
262 * no =, assume it is a dns name, like blah@some.domain.name
263 * if the blah@ part is there, return some.domain.name. if
264 * it's just some.domain.name, return domain.name.
266 if ( strchr( dn, '=' ) == NULL ) {
267 if ( (s = strchr( dn, '@' )) == NULL ) {
268 if ( (s = strchr( dn, '.' )) == NULL ) {
277 * else assume it is an X.500-style name, which looks like
278 * foo=bar,sha=baz,...
283 for ( s = dn; *s; s++ ) {
297 } else if ( DNSEPARATOR( *s ) ) {
308 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
309 * and suffix must be normalized.
318 int dnlen, suffixlen;
324 suffixlen = strlen( suffix );
325 dnlen = strlen( dn );
327 if ( suffixlen > dnlen ) {
331 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
335 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
336 * returns (defined in slap.h): DN_DNS dns-style thing
337 * DN_X500 x500-style thing
343 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
347 dn_upcase( char *dn )
352 for ( s = dn; *s; s++ ) {
353 *s = TOUPPER( (unsigned char) *s );
361 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
364 * Copyright 1999, Juan C. Gomez, All rights reserved.
365 * This software is not subject to any license of Silicon Graphics
366 * Inc. or Purdue University.
368 * Redistribution and use in source and binary forms are permitted
369 * without restriction or fee of any kind as long as this notice
374 /* get_next_substring:
376 * Gets next substring in s, using d (or the end of the string '\0') as a
377 * string delimiter, and places it in a duplicated memory space. Leading
378 * spaces are ignored. String s **must** be null-terminated.
382 get_next_substring( char * s, char d )
387 r = str = ch_malloc( strlen(s) + 1 );
389 /* Skip leading spaces */
391 while ( *s && SPACE(*s) ) {
395 }/* while ( *s && 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!
407 }/* while ( *s && (*s != d) ) */
413 }/* char * get_word() */
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( char * s )
428 return get_next_substring( s, '=' );
430 }/* char * rdn_attr_type() */
435 * Given a string (i.e. an rdn) of the form:
436 * "attribute_type = attribute_value"
437 * this function returns "attribute_type" which is placed in newly allocated
438 * memory. The returned string will be null-terminated and may contain
439 * spaces (i.e. "John Doe\0").
443 rdn_attr_value( char * rdn )
448 if ( (str = strchr( rdn, '=' )) != NULL ) {
450 return get_next_substring(++str, '\0');
452 }/* if ( (str = strpbrk( rdn, "=" )) != NULL ) */
456 }/* char * rdn_attr_value() */
461 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
464 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
468 build_new_dn( char ** new_dn, char *e_dn, char * p_dn, char * newrdn )
471 if ( p_dn == NULL ) {
473 *new_dn = ch_strdup( newrdn );
478 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
480 if ( dn_type( e_dn ) == DN_X500 ) {
482 strcpy( *new_dn, newrdn );
483 strcat( *new_dn, "," );
484 strcat( *new_dn, p_dn );
491 strcpy( *new_dn, newrdn );
492 s = strchr( newrdn, '\0' );
495 if ( (*s != '.') && (*s != '@') ) {
497 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
501 strcat( *new_dn, sep );
503 }/* if ( (s = strpbrk( dn, ".@" )) != NULL ) */
505 }/* if ( *s != '.' && *s != '@' ) */
507 strcat( *new_dn, p_dn );
509 }/* if ( dn_type( e_dn ) == DN_X500 ) {}else */
511 }/* void build_new_dn() */