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
35 * Upper cases a UTF8 character. We cheat a bit, we only change to upper
36 * if the byte length is preserved. Should be replaced.
40 UTF8touppercheat( unsigned char *p )
45 len = LDAP_UTF8_CHARLEN( p );
50 u = ldap_utf8_to_ucs4( p );
51 if ( u == LDAP_UCS4_INVALID ) {
66 } else if ( u < 0x800 ) {
68 p[0] = 0xc0 | ( u >> 6 );
69 p[1] = 0x80 | ( u & 0x3f );
73 } else if ( u < 0x10000 ) {
75 p[0] = 0xe0 | ( u >> 12 );
76 p[1] = 0x80 | ( (u >> 6) & 0x3f );
77 p[2] = 0x80 | ( u & 0x3f );
81 } else if ( u < 0x200000 ) {
83 p[0] = 0xf0 | ( u >> 18 );
84 p[1] = 0x80 | ( (u >> 12) & 0x3f );
85 p[2] = 0x80 | ( (u >> 6) & 0x3f );
86 p[3] = 0x80 | ( u & 0x3f );
90 } else if ( u < 0x4000000 ) {
92 p[0] = 0xf8 | ( u >> 24 );
93 p[1] = 0x80 | ( (u >> 18) & 0x3f );
94 p[2] = 0x80 | ( (u >> 12) & 0x3f );
95 p[3] = 0x80 | ( (u >> 6) & 0x3f );
96 p[4] = 0x80 | ( u & 0x3f );
100 } else if ( len == 6 ) {
102 p[0] = 0xfc | ( u >> 30 );
103 p[1] = 0x80 | ( (u >> 24) & 0x3f );
104 p[2] = 0x80 | ( (u >> 18) & 0x3f );
105 p[3] = 0x80 | ( (u >> 12) & 0x3f );
106 p[4] = 0x80 | ( (u >> 6) & 0x3f );
107 p[5] = 0x80 | ( u & 0x3f );
117 * dn_validate - validate and compress dn. the dn is
118 * compressed in place are returned if valid.
122 dn_validate( char *dn_in )
130 for ( d = s = dn; *s; s++ ) {
134 if ( OID_LEADCHAR(*s) ) {
137 } else if ( ATTR_LEADCHAR(*s) ) {
140 } else if ( ! ASCII_SPACE( *s ) ) {
148 if ( OID_CHAR(*s) ) {
150 } else if ( *s == '=' ) {
153 } else if ( ASCII_SPACE( *s ) ) {
162 if ( ATTR_CHAR(*s) ) {
164 } else if ( *s == '=' ) {
167 } else if ( ASCII_SPACE( *s ) ) {
179 } else if ( ! ASCII_SPACE( *s ) ) {
180 /* not a valid dn - but what can we do here? */
188 state = INQUOTEDVALUE;
190 } else if ( ! ASCII_SPACE( *s ) ) {
197 if ( !gotesc && RDN_SEPARATOR( *s ) ) {
198 while ( ASCII_SPACE( *(d - 1) ) )
206 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) &&
207 !RDN_SEPARATOR( *s ) ) {
210 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
216 if ( !gotesc && *s == '"' ) {
219 } else if ( gotesc && !RDN_NEEDSESCAPE( *s ) ) {
222 } else if( !ASCII_SPACE( *s ) || !ASCII_SPACE( *(d - 1) ) ) {
228 if ( RDN_SEPARATOR( *s ) ) {
231 } else if ( !ASCII_SPACE( *s ) ) {
239 LDAP_LOG(( "operation", LDAP_LEVEL_ERR,
240 "dn_validate: unknown state %d for dn \"%s\".\n",
243 Debug( LDAP_DEBUG_ANY,
244 "dn_validate - unknown state %d\n", state, 0, 0 );
256 /* trim trailing spaces */
257 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
263 /* shouldn't be left in escape */
267 /* check end state */
269 case B4LEADTYPE: /* looking for first type */
270 case B4SEPARATOR: /* looking for separator */
271 case INVALUE: /* inside value */
281 * dn_normalize - put dn into a canonical form suitable for storing
282 * in a hash database. this involves normalizing the case as well as
283 * the format. the dn is normalized in place as well as returned if valid.
287 dn_normalize( char *dn )
291 ldap_pvt_str2upper( dn );
293 /* enabling this might require reindexing */
299 if ( LDAP_UTF8_ISASCII(p) ) {
300 *p = TOUPPER( (unsigned char) *p );
303 p += UTF8touppercheat( p );
307 /* validate and compress dn */
308 dn = dn_validate( dn );
314 * dn_parent - return a copy of the dn of dn's parent
330 while(*dn != '\0' && ASCII_SPACE(*dn)) {
338 if ( be != NULL && be_issuffix( be, dn ) ) {
343 * assume it is an X.500-style name, which looks like
344 * foo=bar,sha=baz,...
348 for ( s = dn; *s; s++ ) {
362 } else if ( DN_SEPARATOR( *s ) ) {
363 return ch_strdup( &s[1] );
368 return ch_strdup( "" );
378 if( dn_in == NULL ) {
382 while(*dn_in && ASCII_SPACE(*dn_in)) {
386 if( *dn_in == '\0' ) {
390 if ( be != NULL && be_issuffix( be, dn_in ) ) {
394 dn = ch_strdup( dn_in );
398 for ( s = dn; *s; s++ ) {
412 } else if ( DN_SEPARATOR( *s ) ) {
424 * return a charray of all subtrees to which the DN resides in
430 char *child, *parent;
431 char **subtree = NULL;
433 child = ch_strdup( dn );
436 charray_add( &subtree, child );
438 parent = dn_parent( be, child );
443 } while ( child != NULL );
450 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
451 * and suffix must be normalized.
460 int dnlen, suffixlen;
466 suffixlen = strlen( suffix );
467 dnlen = strlen( dn );
469 if ( suffixlen > dnlen ) {
473 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
477 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
480 * Copyright 1999, Juan C. Gomez, All rights reserved.
481 * This software is not subject to any license of Silicon Graphics
482 * Inc. or Purdue University.
484 * Redistribution and use in source and binary forms are permitted
485 * without restriction or fee of any kind as long as this notice
490 /* get_next_substring:
492 * Gets next substring in s, using d (or the end of the string '\0') as a
493 * string delimiter, and places it in a duplicated memory space. Leading
494 * spaces are ignored. String s **must** be null-terminated.
498 get_next_substring( const char * s, char d )
503 r = str = ch_malloc( strlen(s) + 1 );
505 /* Skip leading spaces */
507 while ( *s && ASCII_SPACE(*s) ) {
513 while ( *s && (*s != d) ) {
515 /* Don't stop when you see trailing spaces may be a multi-word
516 * string, i.e. name=John Doe!
532 * Given a string (i.e. an rdn) of the form:
533 * "attribute_type = attribute_value"
534 * this function returns the type of an attribute, that is the
535 * string "attribute_type" which is placed in newly allocated
536 * memory. The returned string will be null-terminated.
539 char * rdn_attr_type( const char * s )
541 return get_next_substring( s, '=' );
547 * Given a string (i.e. an rdn) of the form:
548 * "attribute_type = attribute_value"
549 * this function returns "attribute_type" which is placed in newly allocated
550 * memory. The returned string will be null-terminated and may contain
551 * spaces (i.e. "John Doe\0").
555 rdn_attr_value( const char * rdn )
560 if ( (str = strchr( rdn, '=' )) != NULL ) {
561 return get_next_substring(++str, '\0');
569 int rdn_validate( const char * rdn )
571 /* just a simple check for now */
572 return strchr( rdn, '=' ) != NULL;
578 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
581 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
585 build_new_dn( char ** new_dn,
588 const char * newrdn )
591 if ( p_dn == NULL ) {
592 *new_dn = ch_strdup( newrdn );
596 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
598 strcpy( *new_dn, newrdn );
599 strcat( *new_dn, "," );
600 strcat( *new_dn, p_dn );