1 /* dn.c - routines for dealing with distinguished names */
4 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
13 #include <ac/socket.h>
14 #include <ac/string.h>
28 #define INQUOTEDVALUE 7
34 * Upper cases a UTF8 character. We cheat a bit, we only change to upper
35 * if the byte length is preserved. Should be replaced.
39 UTF8touppercheat( unsigned char *p )
44 len = LDAP_UTF8_CHARLEN( p );
49 u = ldap_utf8_to_ucs4( p );
50 if ( u == LDAP_UCS4_INVALID ) {
65 } else if ( u < 0x800 ) {
67 p[0] = 0xc0 | ( u >> 6 );
68 p[1] = 0x80 | ( u & 0x3f );
72 } else if ( u < 0x10000 ) {
74 p[0] = 0xe0 | ( u >> 12 );
75 p[1] = 0x80 | ( (u >> 6) & 0x3f );
76 p[2] = 0x80 | ( u & 0x3f );
80 } else if ( u < 0x200000 ) {
82 p[0] = 0xf0 | ( u >> 18 );
83 p[1] = 0x80 | ( (u >> 12) & 0x3f );
84 p[2] = 0x80 | ( (u >> 6) & 0x3f );
85 p[3] = 0x80 | ( u & 0x3f );
89 } else if ( u < 0x4000000 ) {
91 p[0] = 0xf8 | ( u >> 24 );
92 p[1] = 0x80 | ( (u >> 18) & 0x3f );
93 p[2] = 0x80 | ( (u >> 12) & 0x3f );
94 p[3] = 0x80 | ( (u >> 6) & 0x3f );
95 p[4] = 0x80 | ( u & 0x3f );
99 } else if ( len == 6 ) {
101 p[0] = 0xfc | ( u >> 30 );
102 p[1] = 0x80 | ( (u >> 24) & 0x3f );
103 p[2] = 0x80 | ( (u >> 18) & 0x3f );
104 p[3] = 0x80 | ( (u >> 12) & 0x3f );
105 p[4] = 0x80 | ( (u >> 6) & 0x3f );
106 p[5] = 0x80 | ( u & 0x3f );
116 * dn_validate - validate and compress dn. the dn is
117 * compressed in place are returned if valid.
121 dn_validate( char *dn_in )
129 for ( d = s = dn; *s; s++ ) {
133 if ( OID_LEADCHAR(*s) ) {
136 } else if ( ATTR_LEADCHAR(*s) ) {
139 } else if ( ! ASCII_SPACE( *s ) ) {
147 if ( OID_CHAR(*s) ) {
149 } else if ( *s == '=' ) {
152 } else if ( ASCII_SPACE( *s ) ) {
161 if ( ATTR_CHAR(*s) ) {
163 } else if ( *s == '=' ) {
166 } else if ( ASCII_SPACE( *s ) ) {
178 } else if ( ! ASCII_SPACE( *s ) ) {
179 /* not a valid dn - but what can we do here? */
187 state = INQUOTEDVALUE;
189 } else if ( ! ASCII_SPACE( *s ) ) {
196 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
197 while ( ASCII_SPACE( *(d - 1) ) )
205 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
206 !RDN_SEPARATOR( *s ) ) {
209 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
215 if ( !gotesc && *s == '"' ) {
218 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
221 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
227 if ( RDN_SEPARATOR( *s ) ) {
230 } else if ( !ASCII_SPACE( *s ) ) {
237 Debug( LDAP_DEBUG_ANY,
238 "dn_validate - unknown state %d\n", state, 0, 0 );
249 /* trim trailing spaces */
250 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
256 /* shouldn't be left in escape */
260 /* check end state */
262 case B4LEADTYPE: /* looking for first type */
263 case B4SEPARATOR: /* looking for separator */
264 case INVALUE: /* inside value */
274 * dn_normalize - put dn into a canonical form suitable for storing
275 * in a hash database. this involves normalizing the case as well as
276 * the format. the dn is normalized in place as well as returned if valid.
280 dn_normalize( char *dn )
284 ldap_pvt_str2upper( dn );
286 /* enabling this might require reindexing */
292 if ( LDAP_UTF8_ISASCII(p) ) {
293 *p = TOUPPER( (unsigned char) *p );
296 p += UTF8touppercheat( p );
300 /* validate and compress dn */
301 dn = dn_validate( dn );
307 * dn_parent - return a copy of the dn of dn's parent
323 while(*dn != '\0' && ASCII_SPACE(*dn)) {
331 if ( be != NULL && be_issuffix( be, dn ) ) {
336 * assume it is an X.500-style name, which looks like
337 * foo=bar,sha=baz,...
341 for ( s = dn; *s; s++ ) {
355 } else if ( DN_SEPARATOR( *s ) ) {
356 return ch_strdup( &s[1] );
361 return ch_strdup( "" );
371 if( dn_in == NULL ) {
375 while(*dn_in && ASCII_SPACE(*dn_in)) {
379 if( *dn_in == '\0' ) {
383 if ( be != NULL && be_issuffix( be, dn_in ) ) {
387 dn = ch_strdup( dn_in );
391 for ( s = dn; *s; s++ ) {
405 } else if ( DN_SEPARATOR( *s ) ) {
417 * return a charray of all subtrees to which the DN resides in
423 char *child, *parent;
424 char **subtree = NULL;
426 child = ch_strdup( dn );
429 charray_add( &subtree, child );
431 parent = dn_parent( be, child );
436 } while ( child != NULL );
443 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
444 * and suffix must be normalized.
453 int dnlen, suffixlen;
459 suffixlen = strlen( suffix );
460 dnlen = strlen( dn );
462 if ( suffixlen > dnlen ) {
466 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
470 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
473 * Copyright 1999, Juan C. Gomez, All rights reserved.
474 * This software is not subject to any license of Silicon Graphics
475 * Inc. or Purdue University.
477 * Redistribution and use in source and binary forms are permitted
478 * without restriction or fee of any kind as long as this notice
483 /* get_next_substring:
485 * Gets next substring in s, using d (or the end of the string '\0') as a
486 * string delimiter, and places it in a duplicated memory space. Leading
487 * spaces are ignored. String s **must** be null-terminated.
491 get_next_substring( const char * s, char d )
496 r = str = ch_malloc( strlen(s) + 1 );
498 /* Skip leading spaces */
500 while ( *s && ASCII_SPACE(*s) ) {
506 while ( *s && (*s != d) ) {
508 /* Don't stop when you see trailing spaces may be a multi-word
509 * string, i.e. name=John Doe!
525 * Given a string (i.e. an rdn) of the form:
526 * "attribute_type = attribute_value"
527 * this function returns the type of an attribute, that is the
528 * string "attribute_type" which is placed in newly allocated
529 * memory. The returned string will be null-terminated.
532 char * rdn_attr_type( const char * s )
534 return get_next_substring( s, '=' );
540 * Given a string (i.e. an rdn) of the form:
541 * "attribute_type = attribute_value"
542 * this function returns "attribute_type" which is placed in newly allocated
543 * memory. The returned string will be null-terminated and may contain
544 * spaces (i.e. "John Doe\0").
548 rdn_attr_value( const char * rdn )
553 if ( (str = strchr( rdn, '=' )) != NULL ) {
554 return get_next_substring(++str, '\0');
562 int rdn_validate( const char * rdn )
564 /* just a simple check for now */
565 return strchr( rdn, '=' ) != NULL;
571 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
574 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
578 build_new_dn( char ** new_dn,
581 const char * newrdn )
584 if ( p_dn == NULL ) {
585 *new_dn = ch_strdup( newrdn );
589 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
591 strcpy( *new_dn, newrdn );
592 strcat( *new_dn, "," );
593 strcat( *new_dn, p_dn );