1 /* dn.c - routines for dealing with distinguished names */
3 * Copyright 1998-1999 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
12 #include <ac/socket.h>
13 #include <ac/string.h>
25 #define INQUOTEDVALUE 7
29 * dn_normalize - put dn into a canonical format. the dn is
30 * normalized in place, as well as returned if valid.
34 dn_normalize( char *dn )
41 for ( d = s = dn; *s; s++ ) {
45 if ( LEADOIDCHAR(*s) ) {
48 } else if ( LEADKEYCHAR(*s) ) {
51 } else if ( ! SPACE( *s ) ) {
61 } else if ( *s == '=' ) {
64 } else if ( SPACE( *s ) ) {
75 } else if ( *s == '=' ) {
78 } else if ( SPACE( *s ) ) {
90 } else if ( ! SPACE( *s ) ) {
91 /* not a valid dn - but what can we do here? */
99 state = INQUOTEDVALUE;
101 } else if ( ! SPACE( *s ) ) {
108 if ( !gotesc && SEPARATOR( *s ) ) {
109 while ( SPACE( *(d - 1) ) )
117 } else if ( gotesc && !NEEDSESCAPE( *s ) &&
127 if ( !gotesc && *s == '"' ) {
130 } else if ( gotesc && !NEEDSESCAPE( *s ) ) {
138 if ( SEPARATOR( *s ) ) {
145 Debug( LDAP_DEBUG_ANY,
146 "dn_normalize - unknown state %d\n", state, 0, 0 );
158 /* shouldn't be left in escape */
162 /* check end state */
164 case B4LEADTYPE: /* looking for first type */
165 case B4SEPARATOR: /* looking for separator */
166 case INVALUE: /* inside value */
176 * dn_normalize_case - put dn into a canonical form suitable for storing
177 * in a hash database. this involves normalizing the case as well as
178 * the format. the dn is normalized in place as well as returned if valid.
182 dn_normalize_case( char *dn )
186 /* normalize format */
187 dn = dn_normalize( dn );
189 /* and upper case it */
194 * dn_parent - return a copy of the dn of dn's parent
210 while(*dn && SPACE(*dn)) {
218 if ( be != NULL && be_issuffix( be, dn ) ) {
223 * no =, assume it is a dns name, like blah@some.domain.name
224 * if the blah@ part is there, return some.domain.name. if
225 * it's just some.domain.name, return domain.name.
227 if ( strchr( dn, '=' ) == NULL ) {
228 if ( (s = strchr( dn, '@' )) == NULL ) {
229 if ( (s = strchr( dn, '.' )) == NULL ) {
233 if ( *(s + 1) == '\0' ) {
236 return( ch_strdup( &s[1] ) );
241 * else assume it is an X.500-style name, which looks like
242 * foo=bar,sha=baz,...
246 for ( s = dn; *s; s++ ) {
260 } else if ( DNSEPARATOR( *s ) ) {
261 return( ch_strdup( &s[1] ) );
266 return( ch_strdup( "" ) );
280 while(*dn && SPACE(*dn)) {
288 if ( be != NULL && be_issuffix( be, dn ) ) {
292 dn = ch_strdup( dn );
296 * no =, assume it is a dns name, like blah@some.domain.name
297 * if the blah@ part is there, return some.domain.name. if
298 * it's just some.domain.name, return domain.name.
300 if ( strchr( dn, '=' ) == NULL ) {
301 if ( (s = strchr( dn, '@' )) == NULL ) {
302 if ( (s = strchr( dn, '.' )) == NULL ) {
312 * else assume it is an X.500-style name, which looks like
313 * foo=bar,sha=baz,...
318 for ( s = dn; *s; s++ ) {
332 } else if ( DNSEPARATOR( *s ) ) {
344 * return a charray of all subtrees to which the DN resides in
350 /* not yet implemented */
356 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
357 * and suffix must be normalized.
366 int dnlen, suffixlen;
372 suffixlen = strlen( suffix );
373 dnlen = strlen( dn );
375 if ( suffixlen > dnlen ) {
379 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
384 * dn_type - tells whether the given dn is an X.500 thing or DNS thing
385 * returns (defined in slap.h): DN_DNS dns-style thing
386 * DN_X500 x500-style thing
392 return( strchr( dn, '=' ) == NULL ? DN_DNS : DN_X500 );
397 str2upper( char *str )
402 for ( s = str; *s; s++ ) {
403 *s = TOUPPER( (unsigned char) *s );
410 str2lower( char *str )
415 for ( s = str; *s; s++ ) {
416 *s = TOLOWER( (unsigned char) *s );
424 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
427 * Copyright 1999, Juan C. Gomez, All rights reserved.
428 * This software is not subject to any license of Silicon Graphics
429 * Inc. or Purdue University.
431 * Redistribution and use in source and binary forms are permitted
432 * without restriction or fee of any kind as long as this notice
437 /* get_next_substring:
439 * Gets next substring in s, using d (or the end of the string '\0') as a
440 * string delimiter, and places it in a duplicated memory space. Leading
441 * spaces are ignored. String s **must** be null-terminated.
445 get_next_substring( char * s, char d )
450 r = str = ch_malloc( strlen(s) + 1 );
452 /* Skip leading spaces */
454 while ( *s && SPACE(*s) ) {
462 while ( *s && (*s != d) ) {
464 /* Don't stop when you see trailing spaces may be a multi-word
465 * string, i.e. name=John Doe!
481 * Given a string (i.e. an rdn) of the form:
482 * "attribute_type = attribute_value"
483 * this function returns the type of an attribute, that is the
484 * string "attribute_type" which is placed in newly allocated
485 * memory. The returned string will be null-terminated.
488 char * rdn_attr_type( char * s )
491 return get_next_substring( s, '=' );
498 * Given a string (i.e. an rdn) of the form:
499 * "attribute_type = attribute_value"
500 * this function returns "attribute_type" which is placed in newly allocated
501 * memory. The returned string will be null-terminated and may contain
502 * spaces (i.e. "John Doe\0").
506 rdn_attr_value( char * rdn )
511 if ( (str = strchr( rdn, '=' )) != NULL ) {
513 return get_next_substring(++str, '\0');
522 int rdn_validate( const char * rdn )
524 /* just a simple check for now */
525 return strchr( rdn, '=' ) != NULL;
531 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
534 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
538 build_new_dn( char ** new_dn, char *e_dn, char * p_dn, char * newrdn )
541 if ( p_dn == NULL ) {
543 *new_dn = ch_strdup( newrdn );
548 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
551 if ( dn_type( e_dn ) == DN_X500 ) {
554 strcpy( *new_dn, newrdn );
555 strcat( *new_dn, "," );
556 strcat( *new_dn, p_dn );
564 strcpy( *new_dn, newrdn );
565 s = strchr( newrdn, '\0' );
568 if ( (*s != '.') && (*s != '@') ) {
570 if ( (s = strpbrk( e_dn, ".@" )) != NULL ) {
574 strcat( *new_dn, sep );
580 strcat( *new_dn, p_dn );