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
33 * dn_pretty - "pretty" the DN
35 char *dn_pretty( const char *dn_in )
38 * dn_validate based implementation (for now)
39 * likely better just to zap this, dn_validate, dn_normalize
43 dn = ch_strdup( dn_in );
44 dn_out = dn_validate( dn );
45 if( dn_out == NULL ) free( dn );
51 * dn_validate - validate and compress dn. the dn is
52 * compressed in place are returned if valid.
56 dn_validate( char *dn_in )
58 struct berval val, *normalized;
61 if ( dn_in == NULL || dn_in[ 0 ] == '\0' ) {
66 val.bv_len = strlen( dn_in );
68 rc = dnPretty( NULL, &val, &normalized );
69 if ( rc != LDAP_SUCCESS ) {
73 if ( val.bv_len < normalized->bv_len ) {
74 ber_bvfree( normalized );
78 AC_MEMCPY( dn_in, normalized->bv_val, normalized->bv_len + 1 );
79 ber_bvfree( normalized );
85 * dn_normalize - put dn into a canonical form suitable for storing
86 * in a hash database. this involves normalizing the case as well as
87 * the format. the dn is normalized in place as well as returned if valid.
91 dn_normalize( char *dn )
93 struct berval val, *normalized;
96 if ( dn == NULL || dn[ 0 ] == '\0' ) {
101 val.bv_len = strlen( dn );
103 rc = dnNormalize( NULL, &val, &normalized );
104 if ( rc != LDAP_SUCCESS ) {
108 if ( val.bv_len < normalized->bv_len ) {
109 ber_bvfree( normalized );
113 AC_MEMCPY( dn, normalized->bv_val, normalized->bv_len + 1 );
114 ber_bvfree( normalized );
120 dn_match( const char *val, const char *asserted )
122 struct berval bval, basserted;
124 if ( val == NULL || asserted == NULL ) {
128 bval.bv_val = ( char * )val;
129 bval.bv_len = strlen( val );
131 basserted.bv_val = ( char * )asserted;
132 basserted.bv_len = strlen( asserted);
134 return dnMatch( NULL, 0, NULL, NULL, &bval, &basserted);
138 * dn_parent - return the dn's parent, in-place
153 while(*dn != '\0' && ASCII_SPACE(*dn)) {
161 if ( be != NULL && be_issuffix( be, dn ) ) {
166 * assume it is an X.500-style name, which looks like
167 * foo=bar,sha=baz,...
171 for ( s = dn; *s; s++ ) {
185 } else if ( DN_SEPARATOR( *s ) ) {
186 return (char *)s + 1;
201 if( dn_in == NULL ) {
205 while(*dn_in && ASCII_SPACE(*dn_in)) {
209 if( *dn_in == '\0' ) {
213 if ( be != NULL && be_issuffix( be, dn_in ) ) {
219 for ( s = (char *)dn_in; *s; s++ ) {
233 } else if ( DN_SEPARATOR( *s ) ) {
247 int i = dn_rdnlen( be, dn_in );
249 rdn = ch_malloc( i + 1 );
250 strncpy(rdn, dn_in, i);
256 * return a charray of all subtrees to which the DN resides in
262 char **subtree = NULL;
265 charray_add( &subtree, dn );
267 dn = dn_parent( be, dn );
269 } while ( dn != NULL );
277 const struct berval *dn,
278 const struct berval *suffix
281 int d = dn->bv_len - suffix->bv_len;
286 /* empty suffix matches any dn */
287 if ( suffix->bv_len == 0 ) {
291 /* suffix longer than dn */
296 /* no rdn separator or escaped rdn separator */
297 if ( d > 1 && ( !DN_SEPARATOR( dn->bv_val[ d - 1 ] )
298 || DN_ESCAPE( dn->bv_val[ d - 2 ] ) ) ) {
302 /* no possible match or malformed dn */
308 return( strcmp( dn->bv_val + d, suffix->bv_val ) == 0 );
312 * dn_issuffix - tells whether suffix is a suffix of dn. Both dn
313 * and suffix must be normalized.
322 struct berval bvdn, bvsuffix;
327 bvdn.bv_val = (char *) dn;
328 bvdn.bv_len = strlen( dn );
329 bvsuffix.bv_val = (char *) suffix;
330 bvsuffix.bv_len = strlen( suffix );
332 return dn_issuffixbv( &bvdn, &bvsuffix );
336 * get_next_substring(), rdn_attr_type(), rdn_attr_value(), and
339 * Copyright 1999, Juan C. Gomez, All rights reserved.
340 * This software is not subject to any license of Silicon Graphics
341 * Inc. or Purdue University.
343 * Redistribution and use in source and binary forms are permitted
344 * without restriction or fee of any kind as long as this notice
349 /* get_next_substring:
351 * Gets next substring in s, using d (or the end of the string '\0') as a
352 * string delimiter, and places it in a duplicated memory space. Leading
353 * spaces are ignored. String s **must** be null-terminated.
357 get_next_substring( const char * s, char d )
362 r = str = ch_malloc( strlen(s) + 1 );
364 /* Skip leading spaces */
366 while ( *s && ASCII_SPACE(*s) ) {
372 while ( *s && (*s != d) ) {
374 /* Don't stop when you see trailing spaces may be a multi-word
375 * string, i.e. name=John Doe!
390 * Given a string (i.e. an rdn) of the form:
391 * "attribute_type = attribute_value"
392 * this function returns the type of an attribute, that is the
393 * string "attribute_type" which is placed in newly allocated
394 * memory. The returned string will be null-terminated.
397 char * rdn_attr_type( const char * s )
399 return get_next_substring( s, '=' );
405 * Given a string (i.e. an rdn) of the form:
406 * "attribute_type = attribute_value"
407 * this function returns "attribute_type" which is placed in newly allocated
408 * memory. The returned string will be null-terminated and may contain
409 * spaces (i.e. "John Doe\0").
413 rdn_attr_value( const char * rdn )
418 if ( (str = strchr( rdn, '=' )) != NULL ) {
419 return get_next_substring(++str, '\0');
429 * Given a string (i.e. an rdn) of the form:
430 * "attribute_type=attribute_value[+attribute_type=attribute_value[...]]"
431 * this function stores the types of the attributes in ptypes, that is the
432 * array of strings "attribute_type" which is placed in newly allocated
433 * memory, and the values of the attributes in pvalues, that is the
434 * array of strings "attribute_value" which is placed in newly allocated
435 * memory. Returns 0 on success, -1 on failure.
437 * note: got part of the code from dn_validate
441 rdn_attrs( const char * rdn_in, char ***ptypes, char ***pvalues)
449 * explode the rdn in parts
451 parts = ldap_explode_rdn( rdn_in, 0 );
453 if ( parts == NULL ) {
457 for ( p = parts; p[0]; p++ ) {
460 /* split each rdn part in type value */
461 s = strchr( p[0], '=' );
463 charray_free( *ptypes );
464 charray_free( *pvalues );
465 charray_free( parts );
469 /* type should be fine */
470 charray_add_n( ptypes, p[0], ( s-p[0] ) );
472 /* value needs to be unescaped
473 * (maybe this should be moved to ldap_explode_rdn?) */
474 for ( e = d = s + 1; e[0]; e++ ) {
480 charray_add( pvalues, s + 1 );
484 charray_free( parts );
492 * 1 if rdn is a legal rdn;
493 * 0 otherwise (including a sequence of rdns)
495 * note: got it from dn_rdn; it should be rewritten
496 * according to dn_validate
499 rdn_validate( const char * rdn )
507 if ( strchr( rdn, '=' ) == NULL ) {
511 while ( *rdn && ASCII_SPACE( *rdn ) ) {
521 for ( ; *rdn; rdn++ ) {
522 if ( *rdn == '\\' ) {
535 } else if ( DN_SEPARATOR( *rdn ) ) {
547 * Used by ldbm/bdb2 back_modrdn to create the new dn of entries being
550 * new_dn = parent (p_dn) + separator(s) + rdn (newrdn) + null.
554 build_new_dn( char ** new_dn,
557 const char * newrdn )
560 if ( p_dn == NULL ) {
561 *new_dn = ch_strdup( newrdn );
565 *new_dn = (char *) ch_malloc( strlen( p_dn ) + strlen( newrdn ) + 3 );
567 strcpy( *new_dn, newrdn );
568 strcat( *new_dn, "," );
569 strcat( *new_dn, p_dn );