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 ) ) {
238 Debug( LDAP_DEBUG_ANY,
239 "dn_validate - unknown state %d\n", state, 0, 0 );
250 /* trim trailing spaces */
251 while( d > dn_in && ASCII_SPACE( *(d-1) ) ) {
257 /* shouldn't be left in escape */
261 /* check end state */
263 case B4LEADTYPE: /* looking for first type */
264 case B4SEPARATOR: /* looking for separator */
265 case INVALUE: /* inside value */
275 * dn_normalize - put dn into a canonical form suitable for storing
276 * in a hash database. this involves normalizing the case as well as
277 * the format. the dn is normalized in place as well as returned if valid.
281 dn_normalize( char *dn )
285 ldap_pvt_str2upper( dn );
287 /* enabling this might require reindexing */
293 if ( LDAP_UTF8_ISASCII(p) ) {
294 *p = TOUPPER( (unsigned char) *p );
297 p += UTF8touppercheat( p );
301 /* validate and compress dn */
302 dn = dn_validate( dn );
308 * dn_parent - return a copy of the dn of dn's parent
324 while(*dn != '\0' && ASCII_SPACE(*dn)) {
332 if ( be != NULL && be_issuffix( be, dn ) ) {
337 * assume it is an X.500-style name, which looks like
338 * foo=bar,sha=baz,...
342 for ( s = dn; *s; s++ ) {
356 } else if ( DN_SEPARATOR( *s ) ) {
357 return ch_strdup( &s[1] );
362 return ch_strdup( "" );
372 if( dn_in == NULL ) {
376 while(*dn_in && ASCII_SPACE(*dn_in)) {
380 if( *dn_in == '\0' ) {
384 if ( be != NULL && be_issuffix( be, dn_in ) ) {
388 dn = ch_strdup( dn_in );
392 for ( s = dn; *s; s++ ) {
406 } else if ( DN_SEPARATOR( *s ) ) {
418 * return a charray of all subtrees to which the DN resides in
424 char *child, *parent;
425 char **subtree = NULL;
427 child = ch_strdup( dn );
430 charray_add( &subtree, child );
432 parent = dn_parent( be, child );
437 } while ( child != NULL );
444 * dn_issuffix - tells whether suffix is a suffix of dn. both dn
445 * and suffix must be normalized.
454 int dnlen, suffixlen;
460 suffixlen = strlen( suffix );
461 dnlen = strlen( dn );
463 if ( suffixlen > dnlen ) {
467 return( strcmp( dn + dnlen - suffixlen, suffix ) == 0 );
471 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
474 * Copyright 1999, Juan C. Gomez, All rights reserved.
475 * This software is not subject to any license of Silicon Graphics
476 * Inc. or Purdue University.
478 * Redistribution and use in source and binary forms are permitted
479 * without restriction or fee of any kind as long as this notice
484 /* get_next_substring:
486 * Gets next substring in s, using d (or the end of the string '\0') as a
487 * string delimiter, and places it in a duplicated memory space. Leading
488 * spaces are ignored. String s **must** be null-terminated.
492 get_next_substring( const char * s, char d )
497 r = str = ch_malloc( strlen(s) + 1 );
499 /* Skip leading spaces */
501 while ( *s && ASCII_SPACE(*s) ) {
507 while ( *s && (*s != d) ) {
509 /* Don't stop when you see trailing spaces may be a multi-word
510 * string, i.e. name=John Doe!
526 * Given a string (i.e. an rdn) of the form:
527 * "attribute_type = attribute_value"
528 * this function returns the type of an attribute, that is the
529 * string "attribute_type" which is placed in newly allocated
530 * memory. The returned string will be null-terminated.
533 char * rdn_attr_type( const char * s )
535 return get_next_substring( s, '=' );
541 * Given a string (i.e. an rdn) of the form:
542 * "attribute_type = attribute_value"
543 * this function returns "attribute_type" which is placed in newly allocated
544 * memory. The returned string will be null-terminated and may contain
545 * spaces (i.e. "John Doe\0").
549 rdn_attr_value( const char * rdn )
554 if ( (str = strchr( rdn, '=' )) != NULL ) {
555 return get_next_substring(++str, '\0');
563 int rdn_validate( const char * rdn )
565 /* just a simple check for now */
566 return strchr( rdn, '=' ) != NULL;
572 * Used by ldbm/bdb2_back_modrdn to create the new dn of entries being
575 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
579 build_new_dn( char ** new_dn,
582 const char * newrdn )
585 if ( p_dn == NULL ) {
586 *new_dn = ch_strdup( newrdn );
590 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
592 strcpy( *new_dn, newrdn );
593 strcat( *new_dn, "," );
594 strcat( *new_dn, p_dn );