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
32 * Upper cases a UTF8 character. We cheat a bit, we only change to upper
33 * if the byte length is preserved. Should be replaced.
37 UTF8touppercheat( unsigned char *p )
42 len = LDAP_UTF8_CHARLEN( p );
47 u = ldap_utf8_to_ucs4( p );
48 if ( u == LDAP_UCS4_INVALID ) {
63 } else if ( u < 0x800 ) {
65 p[0] = 0xc0 | ( u >> 6 );
66 p[1] = 0x80 | ( u & 0x3f );
70 } else if ( u < 0x10000 ) {
72 p[0] = 0xe0 | ( u >> 12 );
73 p[1] = 0x80 | ( (u >> 6) & 0x3f );
74 p[2] = 0x80 | ( u & 0x3f );
78 } else if ( u < 0x200000 ) {
80 p[0] = 0xf0 | ( u >> 18 );
81 p[1] = 0x80 | ( (u >> 12) & 0x3f );
82 p[2] = 0x80 | ( (u >> 6) & 0x3f );
83 p[3] = 0x80 | ( u & 0x3f );
87 } else if ( u < 0x4000000 ) {
89 p[0] = 0xf8 | ( u >> 24 );
90 p[1] = 0x80 | ( (u >> 18) & 0x3f );
91 p[2] = 0x80 | ( (u >> 12) & 0x3f );
92 p[3] = 0x80 | ( (u >> 6) & 0x3f );
93 p[4] = 0x80 | ( u & 0x3f );
97 } else if ( len == 6 ) {
99 p[0] = 0xfc | ( u >> 30 );
100 p[1] = 0x80 | ( (u >> 24) & 0x3f );
101 p[2] = 0x80 | ( (u >> 18) & 0x3f );
102 p[3] = 0x80 | ( (u >> 12) & 0x3f );
103 p[4] = 0x80 | ( (u >> 6) & 0x3f );
104 p[5] = 0x80 | ( u & 0x3f );
113 * dn_validate - validate and compress dn. the dn is
114 * compressed in place are returned if valid.
118 dn_validate( char *dn_in )
126 for ( d = s = dn; *s; s++ ) {
130 if ( OID_LEADCHAR(*s) ) {
133 } else if ( ATTR_LEADCHAR(*s) ) {
136 } else if ( ! ASCII_SPACE( *s ) ) {
144 if ( OID_CHAR(*s) ) {
146 } else if ( *s == '=' ) {
149 } else if ( ASCII_SPACE( *s ) ) {
158 if ( ATTR_CHAR(*s) ) {
160 } else if ( *s == '=' ) {
163 } else if ( ASCII_SPACE( *s ) ) {
175 } else if ( ! ASCII_SPACE( *s ) ) {
176 /* not a valid dn - but what can we do here? */
184 state = INQUOTEDVALUE;
186 } else if ( ! ASCII_SPACE( *s ) ) {
193 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
194 while ( ASCII_SPACE( *(d - 1) ) )
202 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
203 !RDN_SEPARATOR( *s ) ) {
206 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
212 if ( !gotesc && *s == '"' ) {
215 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
218 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
224 if ( RDN_SEPARATOR( *s ) ) {
227 } else if ( !ASCII_SPACE( *s ) ) {
234 Debug( LDAP_DEBUG_ANY,
235 "dn_validate - unknown state %d\n", state, 0, 0 );
246 /* trim trailing spaces */
247 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
253 /* shouldn't be left in escape */
257 /* check end state */
259 case B4LEADTYPE: /* looking for first type */
260 case B4SEPARATOR: /* looking for separator */
261 case INVALUE: /* inside value */
271 * dn_normalize - put dn into a canonical form suitable for storing
272 * in a hash database. this involves normalizing the case as well as
273 * the format. the dn is normalized in place as well as returned if valid.
277 dn_normalize( char *dn )
281 ldap_pvt_str2upper( dn );
283 /* enabling this might require reindexing */
289 if ( LDAP_UTF8_ISASCII(p) ) {
290 *p = TOUPPER( (unsigned char) *p );
293 p += UTF8touppercheat( p );
297 /* validate and compress dn */
298 dn = dn_validate( dn );
304 * dn_parent - return a copy of the dn of dn's parent
320 while(*dn != '\0' && ASCII_SPACE(*dn)) {
328 if ( be != NULL && be_issuffix( be, dn ) ) {
333 * assume it is an X.500-style name, which looks like
334 * foo=bar,sha=baz,...
338 for ( s = dn; *s; s++ ) {
352 } else if ( DN_SEPARATOR( *s ) ) {
353 return ch_strdup( &s[1] );
358 return ch_strdup( "" );
368 if( dn_in == NULL ) {
372 while(*dn_in && ASCII_SPACE(*dn_in)) {
376 if( *dn_in == '\0' ) {
380 if ( be != NULL && be_issuffix( be, dn_in ) ) {
384 dn = ch_strdup( dn_in );
388 for ( s = dn; *s; s++ ) {
402 } else if ( DN_SEPARATOR( *s ) ) {
414 * return a charray of all subtrees to which the DN resides in
420 char *child, *parent;
421 char **subtree = NULL;
423 child = ch_strdup( dn );
426 charray_add( &subtree, child );
428 parent = dn_parent( be, child );
433 } while ( child != NULL );
440 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
441 * and suffix must be normalized.
450 int dnlen, suffixlen;
456 suffixlen = strlen( suffix );
457 dnlen = strlen( dn );
459 if ( suffixlen > dnlen ) {
463 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
467 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
470 * Copyright 1999, Juan C. Gomez, All rights reserved.
471 * This software is not subject to any license of Silicon Graphics
472 * Inc. or Purdue University.
474 * Redistribution and use in source and binary forms are permitted
475 * without restriction or fee of any kind as long as this notice
480 /* get_next_substring:
482 * Gets next substring in s, using d (or the end of the string '\0') as a
483 * string delimiter, and places it in a duplicated memory space. Leading
484 * spaces are ignored. String s **must** be null-terminated.
488 get_next_substring( const char * s, char d )
493 r = str = ch_malloc( strlen(s) + 1 );
495 /* Skip leading spaces */
497 while ( *s && ASCII_SPACE(*s) ) {
503 while ( *s && (*s != d) ) {
505 /* Don't stop when you see trailing spaces may be a multi-word
506 * string, i.e. name=John Doe!
522 * Given a string (i.e. an rdn) of the form:
523 * "attribute_type = attribute_value"
524 * this function returns the type of an attribute, that is the
525 * string "attribute_type" which is placed in newly allocated
526 * memory. The returned string will be null-terminated.
529 char * rdn_attr_type( const char * s )
531 return get_next_substring( s, '=' );
537 * Given a string (i.e. an rdn) of the form:
538 * "attribute_type = attribute_value"
539 * this function returns "attribute_type" which is placed in newly allocated
540 * memory. The returned string will be null-terminated and may contain
541 * spaces (i.e. "John Doe\0").
545 rdn_attr_value( const char * rdn )
550 if ( (str = strchr( rdn, '=' )) != NULL ) {
551 return get_next_substring(++str, '\0');
559 int rdn_validate( const char * rdn )
561 /* just a simple check for now */
562 return strchr( rdn, '=' ) != NULL;
568 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
571 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
575 build_new_dn( char ** new_dn,
578 const char * newrdn )
581 if ( p_dn == NULL ) {
582 *new_dn = ch_strdup( newrdn );
586 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
588 strcpy( *new_dn, newrdn );
589 strcat( *new_dn, "," );
590 strcat( *new_dn, p_dn );