3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
27 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 static int dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout );
32 /* from libraries/libldap/schema.c */
33 extern char * parse_numericoid(const char **sp, int *code, const int flags);
35 /* parsing/printing routines */
36 static int str2strval( const char *str, struct berval **val,
37 const char **next, unsigned flags, unsigned *retFlags );
38 static int DCE2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags );
40 static int IA52strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int quotedIA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int hexstr2binval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
68 static LDAPAVA * ldapava_new(
69 const struct berval *attr, const struct berval *val, unsigned flags );
70 static LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
71 static LDAPRDN * ldapava_insert_into_rdn(
72 LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
73 static LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
74 static LDAPDN * ldapava_insert_into_dn(
75 LDAPDN *dn, LDAPRDN *rdn, unsigned where );
77 /* Higher level helpers */
78 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
79 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
80 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
81 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
82 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
83 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
84 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
85 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
86 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
87 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
90 * RFC 1823 ldap_get_dn
93 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
98 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
100 if ( entry == NULL ) {
101 ld->ld_errno = LDAP_PARAM_ERROR;
105 tmp = *entry->lm_ber; /* struct copy */
106 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
107 ld->ld_errno = LDAP_DECODING_ERROR;
115 * RFC 1823 ldap_dn2ufn
118 ldap_dn2ufn( LDAP_CONST char *dn )
122 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
124 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_UFN );
130 * RFC 1823 ldap_explode_dn
133 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
136 char **values = NULL;
138 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
140 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
142 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
147 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
148 char *str, **v = NULL;
150 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
152 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
154 LBER_VFREE( values );
155 ldap_dnfree( tmpDN );
159 values[ iRDN ] = str;
161 values[ iRDN ] = NULL;
167 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
170 char **values = NULL;
172 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
174 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
177 * we assume this dn is made of one rdn only
179 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAP )
184 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
185 ber_len_t l = 0, vl, al = 0;
186 char *str, **v = NULL;
187 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
189 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
195 if ( ava->la_flags == LDAP_AVA_BINARY ) {
196 vl = 1 + 2 * ava->la_value->bv_len;
199 if ( strval2strlen( ava->la_value,
200 ava->la_flags, &vl ) ) {
206 al = ava->la_attr->bv_len;
207 l = vl + ava->la_attr->bv_len + 1;
209 str = LDAP_MALLOC( l + 1 );
210 AC_MEMCPY( str, ava->la_attr->bv_val,
211 ava->la_attr->bv_len );
216 str = LDAP_MALLOC( l + 1 );
219 if ( ava->la_flags == LDAP_AVA_BINARY ) {
221 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
226 if ( strval2str( ava->la_value, &str[ al ],
227 ava->la_flags, &vl ) ) {
233 values[ iAVA ] = str;
235 values[ iAVA ] = NULL;
237 ldap_dnfree( tmpDN );
242 LBER_VFREE( values );
243 ldap_dnfree( tmpDN );
248 ldap_dn2dcedn( LDAP_CONST char *dn )
252 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
254 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_DCE );
260 ldap_dcedn2dn( LDAP_CONST char *dce )
264 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
266 ( void )dn2dn( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
272 ldap_dn2ad_canonical( LDAP_CONST char *dn )
276 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
278 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP,
279 &out, LDAP_DN_FORMAT_AD_CANONICAL );
285 ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags )
289 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
291 return dn2dn( in, iflags, out, oflags);
295 * helper that changes the string representation of dnin
296 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
299 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
300 * plus some rfc 1779)
301 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
302 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
303 * LDAP_DN_FORMAT_DCE (?)
305 * fout can be any of the above except
306 * LDAP_DN_FORMAT_LDAP
308 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
309 * LDAP_DN_FORMAT_AD_CANONICAL (?)
312 dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
315 LDAPDN *tmpDN = NULL;
321 if ( dnin == NULL ) {
322 return( LDAP_SUCCESS );
325 rc = ldap_str2dn( dnin , &tmpDN, fin );
326 if ( rc != LDAP_SUCCESS ) {
330 rc = ldap_dn2str( tmpDN, dnout, fout );
332 ldap_dnfree( tmpDN );
340 /* #define B4ATTRTYPE 0x0001 */
341 #define B4OIDATTRTYPE 0x0002
342 #define B4STRINGATTRTYPE 0x0003
344 #define B4AVAEQUALS 0x0100
345 #define B4AVASEP 0x0200
346 #define B4RDNSEP 0x0300
347 #define GOTAVA 0x0400
349 #define B4ATTRVALUE 0x0010
350 #define B4STRINGVALUE 0x0020
351 #define B4IA5VALUEQUOTED 0x0030
352 #define B4IA5VALUE 0x0040
353 #define B4BINARYVALUE 0x0050
355 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
356 #define LDAP_DN_ASCII_SPACE(c) \
357 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
358 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
359 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
360 #define LDAP_DN_ASCII_ALPHA(c) \
361 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
362 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
363 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
364 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
365 #define LDAP_DN_ASCII_HEXDIGIT(c) \
366 ( LDAP_DN_ASCII_DIGIT(c) \
367 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
368 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
369 #define LDAP_DN_ASCII_ALNUM(c) \
370 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
371 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
374 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
375 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
376 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
377 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
378 #define LDAP_DN_ATTRDESC_CHAR(c) \
379 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
381 /* special symbols */
382 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
383 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
384 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
385 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
386 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
387 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
388 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
389 #define LDAP_DN_VALUE_END(c) \
390 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
391 #define LDAP_DN_NE(c) \
392 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
393 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
394 #define LDAP_DN_NEEDESCAPE(c) \
395 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
396 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
397 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
398 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
399 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
400 #define LDAP_DN_WILLESCAPE_CHAR( c) \
401 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
402 #define LDAP_DN_IS_PRETTY( f ) ( (f) & LDAP_DN_PRETTY )
403 #define LDAP_DN_WILLESCAPE(f, c) \
404 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
407 #define LDAP_DN_VALUE_END_V2(c) \
408 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
410 #define LDAP_DN_V2_SPECIAL(c) \
411 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
412 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
413 || LDAP_DN_OCTOTHORPE(c) )
414 #define LDAP_DN_V2_PAIR(c) \
415 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
418 * DCE (mostly from Luke Howard and IBM implementation for AIX)
420 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
421 * Here escapes and valid chars for GDS are considered; as soon as more
422 * specific info is found, the macros will be updated.
424 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
425 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
427 * Metachars: '/', ',', '=', '\'.
429 * the '\' is used to escape other metachars.
435 * Attribute types must start with alphabetic chars and can contain
436 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
438 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
439 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
440 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
441 #define LDAP_DN_VALUE_END_DCE(c) \
442 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
443 #define LDAP_DN_NEEDESCAPE_DCE(c) \
444 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
447 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
448 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
449 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
450 #define LDAP_DN_VALUE_END_AD(c) \
451 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
452 #define LDAP_DN_NEEDESCAPE_AD(c) \
453 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
456 #define LDAP_DN_HEXPAIR(s) \
457 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
458 #define LDAP_DC_ATTR "dc"
459 /* better look at the AttributeDescription? */
461 /* FIXME: no composite rdn or non-"dc" types, right?
462 * (what about "dc" in OID form?) */
463 /* FIXME: we do not allow binary values in domain, right? */
464 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
465 #define LDAP_DN_IS_RDN_DC( rdn ) \
466 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
467 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
468 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
470 /* Composite rules */
471 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
472 ( LDAP_DN_LDAPV2(f) \
473 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
474 #define LDAP_DN_ALLOW_SPACES(f) \
475 ( LDAP_DN_LDAPV2(f) \
476 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
477 #define LDAP_DN_LDAP(f) \
478 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
479 #define LDAP_DN_LDAPV3(f) \
480 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
481 #define LDAP_DN_LDAPV2(f) \
482 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
483 #define LDAP_DN_DCE(f) \
484 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
485 #define LDAP_DN_UFN(f) \
486 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
487 #define LDAP_DN_ADC(f) \
488 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
489 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
492 * LDAPAVA helpers (will become part of the API for operations
493 * on structural representations of DNs).
496 ldapava_new( const struct berval *attr, const struct berval *val,
504 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
506 /* should we test it? */
511 ava->la_attr = ( struct berval * )attr;
512 ava->la_value = ( struct berval * )val;
513 ava->la_flags = flags;
515 ava->la_private = NULL;
521 ldap_avafree( LDAPAVA *ava )
526 /* ava's private must be freed by caller */
527 assert( ava->la_private != NULL );
530 ber_bvfree( ava->la_attr );
531 ber_bvfree( ava->la_value );
537 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
545 for ( i = 0U; rdn[ i ]; i++ ) {
549 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
550 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
551 newRDN[ i ][ 0 ] = ava;
552 newRDN[ i + 1 ] = NULL;
558 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
566 for ( i = 0U; rdn[ i ]; i++ ) {
572 /* assume "at end", which corresponds to
573 * ldapava_append_to_rdn */
576 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
578 /* data after insert point */
579 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
580 ( i - where ) * sizeof( LDAPRDN * ) );
582 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
583 newRDN[ where ][ 0 ] = ava;
584 newRDN[ i + 1 ] = NULL;
590 ldap_rdnfree( LDAPRDN *rdn )
598 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
599 assert( rdn[ iAVA ][ 0 ] );
601 ldap_avafree( rdn[ iAVA ][ 0 ] );
608 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
616 for ( i = 0U; dn[ i ]; i++ ) {
620 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
621 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
622 newDN[ i ][ 0 ] = rdn;
623 newDN[ i + 1 ] = NULL;
629 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
637 for ( i = 0U; dn[ i ]; i++ ) {
643 /* assume "at end", which corresponds to
644 * ldapava_append_to_dn */
647 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
649 /* data after insert point */
650 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
651 ( i - where ) * sizeof( LDAPDN * ) );
653 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
654 newDN[ where ][ 0 ] = rdn;
655 newDN[ i + 1 ] = NULL;
661 ldap_dnfree( LDAPDN *dn )
669 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
670 assert( dn[ iRDN ][ 0 ] );
672 ldap_rdnfree( dn[ iRDN ][ 0 ] );
679 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
680 * into a structural representation of the DN, by separating attribute
681 * types and values encoded in the more appropriate form, which is
682 * string or OID for attribute types and binary form of the BER encoded
683 * value or Unicode string. Formats different from LDAPv3 are parsed
684 * according to their own rules and turned into the more appropriate
685 * form according to LDAPv3.
687 * NOTE: I realize the code is getting spaghettish; it is rather
688 * experimental and will hopefully turn into something more simple
689 * and readable as soon as it works as expected.
693 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
696 int rc = LDAP_INVALID_DN_SYNTAX;
698 LDAPDN *newDN = NULL;
699 LDAPRDN *newRDN = NULL;
704 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
708 switch ( LDAP_DN_FORMAT( flags ) ) {
709 case LDAP_DN_FORMAT_LDAP:
710 case LDAP_DN_FORMAT_LDAPV3:
711 case LDAP_DN_FORMAT_LDAPV2:
712 case LDAP_DN_FORMAT_DCE:
715 /* unsupported in str2dn */
716 case LDAP_DN_FORMAT_UFN:
717 case LDAP_DN_FORMAT_AD_CANONICAL:
718 return( LDAP_INVALID_DN_SYNTAX );
721 return( LDAP_OTHER );
724 if ( str[ 0 ] == '\0' ) {
725 return( LDAP_SUCCESS );
729 if ( LDAP_DN_DCE( flags ) ) {
732 * (from Luke Howard: thnx) A RDN separator is required
733 * at the beginning of an (absolute) DN.
735 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
740 } else if ( LDAP_DN_LDAP( flags ) ) {
742 * if dn starts with '/' let's make it a DCE dn
744 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
745 flags |= LDAP_DN_FORMAT_DCE;
750 for ( ; p[ 0 ]; p++ ) {
754 err = ldap_str2rdn( p, &newRDN, &p, flags );
755 if ( err != LDAP_SUCCESS ) {
760 * We expect a rdn separator
763 switch ( LDAP_DN_FORMAT( flags ) ) {
764 case LDAP_DN_FORMAT_LDAPV3:
765 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
771 case LDAP_DN_FORMAT_LDAP:
772 case LDAP_DN_FORMAT_LDAPV2:
773 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
779 case LDAP_DN_FORMAT_DCE:
780 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
789 if ( LDAP_DN_DCE( flags ) ) {
790 /* add in reversed order */
791 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
793 dn = ldapava_append_to_dn( newDN, newRDN );
804 if ( p[ 0 ] == '\0' ) {
807 * the DN is over, phew
816 ldap_rdnfree( newRDN );
820 ldap_dnfree( newDN );
826 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
835 * Parses a relative DN according to flags up to a rdn separator
836 * or to the end of str.
837 * Returns the rdn and a pointer to the string continuation, which
838 * corresponds to the rdn separator or to '\0' in case the string is over.
841 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
845 int rc = LDAP_INVALID_DN_SYNTAX;
846 int attrTypeEncoding = LDAP_AVA_STRING,
847 attrValueEncoding = LDAP_AVA_STRING;
849 struct berval *attrType = NULL;
850 struct berval *attrValue = NULL;
852 LDAPRDN *newRDN = NULL;
858 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
863 switch ( LDAP_DN_FORMAT( flags ) ) {
864 case LDAP_DN_FORMAT_LDAP:
865 case LDAP_DN_FORMAT_LDAPV3:
866 case LDAP_DN_FORMAT_LDAPV2:
867 case LDAP_DN_FORMAT_DCE:
870 /* unsupported in str2dn */
871 case LDAP_DN_FORMAT_UFN:
872 case LDAP_DN_FORMAT_AD_CANONICAL:
873 return( LDAP_INVALID_DN_SYNTAX );
876 return( LDAP_OTHER );
879 if ( str[ 0 ] == '\0' ) {
880 return( LDAP_SUCCESS );
884 for ( ; p[ 0 ] || state == GOTAVA; ) {
887 * The parser in principle advances one token a time,
888 * or toggles state if preferable.
893 * an AttributeType can be encoded as:
894 * - its string representation; in detail, implementations
895 * MUST recognize AttributeType string type names listed
896 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
897 * MAY recognize other names.
898 * - its numeric OID (a dotted decimal string); in detail
899 * RFC 2253 asserts that ``Implementations MUST allow
900 * an oid in the attribute type to be prefixed by one
901 * of the character strings "oid." or "OID."''. As soon
902 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
903 * I'm not sure whether this is required or not any
904 * longer; to be liberal, we still implement it.
907 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
908 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
915 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
916 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
921 /* whitespace is allowed (and trimmed) */
923 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
928 /* error: we expected an AVA */
934 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
935 state = B4OIDATTRTYPE;
939 /* else must be alpha */
940 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
944 /* LDAPv2 "oid." prefix */
945 if ( LDAP_DN_LDAPV2( flags ) ) {
947 * to be overly pedantic, we only accept
950 if ( flags & LDAP_DN_PEDANTIC ) {
951 if ( !strncmp( p, "OID.", 4 )
952 || !strncmp( p, "oid.", 4 ) ) {
954 state = B4OIDATTRTYPE;
958 if ( !strncasecmp( p, "oid.", 4 ) ) {
960 state = B4OIDATTRTYPE;
966 state = B4STRINGATTRTYPE;
969 case B4OIDATTRTYPE: {
970 int err = LDAP_SUCCESS;
973 type = parse_numericoid( &p, &err, 0 );
974 if ( type == NULL ) {
977 attrType = LDAP_MALLOC( sizeof( struct berval ) );
978 if ( attrType== NULL ) {
982 attrType->bv_val = type;
983 attrType->bv_len = strlen( type );
984 attrTypeEncoding = LDAP_AVA_BINARY;
990 case B4STRINGATTRTYPE: {
991 const char *startPos, *endPos = NULL;
995 * the starting char has been found to be
996 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
997 * FIXME: DCE attr types seem to have a more
998 * restrictive syntax (no '-' ...)
1000 for ( startPos = p++; p[ 0 ]; p++ ) {
1001 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1005 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1008 * RFC 2253 does not explicitly
1009 * allow lang extensions to attribute
1012 if ( flags & LDAP_DN_PEDANTIC ) {
1017 * we trim ';' and following lang
1018 * and so from attribute types
1021 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1022 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1030 len = ( endPos ? endPos : p ) - startPos;
1035 assert( attrType == NULL );
1036 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1037 if ( attrType == NULL ) {
1038 rc = LDAP_NO_MEMORY;
1041 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1042 if ( attrType->bv_val == NULL ) {
1043 rc = LDAP_NO_MEMORY;
1046 attrType->bv_len = len;
1047 attrTypeEncoding = LDAP_AVA_STRING;
1050 * here we need to decide whether to use it as is
1051 * or turn it in OID form; as a consequence, we
1052 * need to decide whether to binary encode the value
1055 state = B4AVAEQUALS;
1060 /* spaces may not be allowed */
1061 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1062 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1067 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1072 /* need equal sign */
1073 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1078 /* spaces may not be allowed */
1079 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1080 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1085 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1091 * octothorpe means a BER encoded value will follow
1092 * FIXME: I don't think DCE will allow it
1094 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1096 attrValueEncoding = LDAP_AVA_BINARY;
1097 state = B4BINARYVALUE;
1101 /* STRING value expected */
1104 * if we're pedantic, an attribute type in OID form
1105 * SHOULD imply a BER encoded attribute value; we
1106 * should at least issue a warning
1108 if ( ( flags & LDAP_DN_PEDANTIC )
1109 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1110 /* OID attrType SHOULD use binary encoding */
1114 attrValueEncoding = LDAP_AVA_STRING;
1117 * LDAPv2 allows the attribute value to be quoted;
1118 * also, IA5 values are expected, in principle
1120 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1121 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1123 state = B4IA5VALUEQUOTED;
1127 if ( LDAP_DN_LDAPV2( flags ) ) {
1134 * here STRING means RFC 2253 string
1135 * FIXME: what about DCE strings?
1137 state = B4STRINGVALUE;
1141 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1149 switch ( LDAP_DN_FORMAT( flags ) ) {
1150 case LDAP_DN_FORMAT_LDAP:
1151 case LDAP_DN_FORMAT_LDAPV3:
1152 if ( str2strval( p, &attrValue, &p, flags,
1153 &attrValueEncoding ) ) {
1158 case LDAP_DN_FORMAT_DCE:
1159 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1172 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1179 case B4IA5VALUEQUOTED:
1181 /* lead quote already stripped */
1182 if ( quotedIA52strval( p, &attrValue,
1196 * we accept empty values
1198 ava = ldapava_new( attrType, attrValue,
1199 attrValueEncoding );
1200 if ( ava == NULL ) {
1201 rc = LDAP_NO_MEMORY;
1205 rdn = ldapava_append_to_rdn( newRDN, ava );
1206 if ( rdn == NULL ) {
1207 rc = LDAP_NO_MEMORY;
1213 * if we got an AVA separator ('+', or ',' for DCE )
1214 * we expect a new AVA for this RDN; otherwise
1215 * we add the RDN to the DN
1217 switch ( LDAP_DN_FORMAT( flags ) ) {
1218 case LDAP_DN_FORMAT_LDAP:
1219 case LDAP_DN_FORMAT_LDAPV3:
1220 case LDAP_DN_FORMAT_LDAPV2:
1221 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1226 case LDAP_DN_FORMAT_DCE:
1227 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1235 * the RDN is over, phew
1242 /* they should have been used in an AVA */
1258 /* They are set to NULL after they're used in an AVA */
1260 ber_bvfree( attrType );
1264 ber_bvfree( attrValue );
1268 ldap_rdnfree( newRDN );
1274 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1282 * reads in a UTF-8 string value, unescaping stuff:
1283 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1284 * '\' + HEXPAIR(p) -> unhex(p)
1287 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1289 const char *p, *startPos, *endPos = NULL;
1290 ber_len_t len, escapes, unescapes;
1299 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1300 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1302 if ( p[ 0 ] == '\0' ) {
1305 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1306 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1307 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1312 if ( LDAP_DN_HEXPAIR( p ) ) {
1315 hexstr2bin( p, &c );
1318 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1321 * we assume the string is UTF-8
1323 *retFlags = LDAP_AVA_NONPRINTABLE;
1330 if ( LDAP_DN_PEDANTIC & flags ) {
1334 * FIXME: we allow escaping
1335 * of chars that don't need
1336 * to and do not belong to
1337 * HEXDIGITS (we also allow
1338 * single hexdigit; maybe we
1343 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1344 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1347 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1349 * FIXME: maybe we can add
1350 * escapes if not pedantic?
1357 * we do allow unescaped spaces at the end
1358 * of the value only in non-pedantic mode
1360 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1361 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1362 if ( flags & LDAP_DN_PEDANTIC ) {
1366 /* strip trailing (unescaped) spaces */
1367 for ( endPos = p - 1;
1368 endPos > startPos + 1 &&
1369 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1370 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1377 * FIXME: test memory?
1379 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1380 *val = LDAP_MALLOC( sizeof( struct berval ) );
1381 ( *val )->bv_len = len;
1383 if ( escapes == 0 && unescapes == 0 ) {
1384 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1389 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1390 for ( s = 0, d = 0; d < len; ) {
1391 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1393 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1394 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1395 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1396 ( *val )->bv_val[ d++ ] =
1399 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1402 hexstr2bin( &startPos[ s ], &c );
1403 ( *val )->bv_val[ d++ ] = c;
1408 * we allow escaping of chars
1409 * that do not need to
1411 ( *val )->bv_val[ d++ ] =
1416 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1420 ( *val )->bv_val[ d ] = '\0';
1421 assert( strlen( ( *val )->bv_val ) == len );
1431 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1433 const char *p, *startPos, *endPos = NULL;
1434 ber_len_t len, escapes;
1443 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1444 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1446 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1453 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1458 * FIXME: can we accept anything else? I guess we need
1459 * to stop if a value is not legal
1464 * (unescaped) trailing spaces are trimmed must be silently ignored;
1467 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1468 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1469 if ( flags & LDAP_DN_PEDANTIC ) {
1473 /* strip trailing (unescaped) spaces */
1474 for ( endPos = p - 1;
1475 endPos > startPos + 1 &&
1476 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1477 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1484 len = ( endPos ? endPos : p ) - startPos - escapes;
1485 *val = LDAP_MALLOC( sizeof( struct berval ) );
1486 ( *val )->bv_len = len;
1487 if ( escapes == 0 ){
1488 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1493 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1494 for ( s = 0, d = 0; d < len; ) {
1496 * This point is reached only if escapes
1497 * are properly used, so all we need to
1500 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1504 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1506 ( *val )->bv_val[ d ] = '\0';
1507 assert( strlen( ( *val )->bv_val ) == len );
1516 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1518 const char *p, *startPos, *endPos = NULL;
1519 ber_len_t len, escapes;
1532 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1533 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1535 if ( p[ 0 ] == '\0' ) {
1539 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1540 && ( LDAP_DN_PEDANTIC & flags ) ) {
1545 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1550 * FIXME: can we accept anything else? I guess we need
1551 * to stop if a value is not legal
1555 /* strip trailing (unescaped) spaces */
1557 endPos > startPos + 1 &&
1558 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1559 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1564 *val = LDAP_MALLOC( sizeof( struct berval ) );
1565 len = ( endPos ? endPos : p ) - startPos - escapes;
1566 ( *val )->bv_len = len;
1567 if ( escapes == 0 ) {
1568 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1573 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1574 for ( s = 0, d = 0; d < len; ) {
1575 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1578 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1580 ( *val )->bv_val[ d ] = '\0';
1581 assert( strlen( ( *val )->bv_val ) == len );
1589 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1591 const char *p, *startPos, *endPos = NULL;
1593 unsigned escapes = 0;
1602 /* initial quote already eaten */
1603 for ( startPos = p = str; p[ 0 ]; p++ ) {
1605 * According to RFC 1779, the quoted value can
1606 * contain escaped as well as unescaped special values;
1607 * as a consequence we tolerate escaped values
1608 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1609 * (e.g. '","' -> '\,').
1611 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1612 if ( p[ 1 ] == '\0' ) {
1617 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1618 && ( LDAP_DN_PEDANTIC & flags ) ) {
1620 * do we allow to escape normal chars?
1621 * LDAPv2 does not allow any mechanism
1622 * for escaping chars with '\' and hex
1629 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1631 /* eat closing quotes */
1637 * FIXME: can we accept anything else? I guess we need
1638 * to stop if a value is not legal
1642 if ( endPos == NULL ) {
1646 /* Strip trailing (unescaped) spaces */
1647 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1651 len = endPos - startPos - escapes;
1653 *val = LDAP_MALLOC( sizeof( struct berval ) );
1654 ( *val )->bv_len = len;
1655 if ( escapes == 0 ) {
1656 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1661 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1662 ( *val )->bv_len = len;
1664 for ( s = d = 0; d < len; ) {
1665 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1668 ( *val )->bv_val[ d++ ] = str[ s++ ];
1670 ( *val )->bv_val[ d ] = '\0';
1671 assert( strlen( ( *val )->bv_val ) == len );
1680 hexstr2bin( const char *str, char *c )
1690 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1696 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1703 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1709 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1710 *c += c2 - 'a' + 10;
1718 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1720 const char *p, *startPos, *endPos = NULL;
1731 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1732 switch ( LDAP_DN_FORMAT( flags ) ) {
1733 case LDAP_DN_FORMAT_LDAPV3:
1734 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1739 case LDAP_DN_FORMAT_LDAP:
1740 case LDAP_DN_FORMAT_LDAPV2:
1741 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1746 case LDAP_DN_FORMAT_DCE:
1747 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1753 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1754 if ( flags & LDAP_DN_PEDANTIC ) {
1759 for ( ; p[ 0 ]; p++ ) {
1760 switch ( LDAP_DN_FORMAT( flags ) ) {
1761 case LDAP_DN_FORMAT_LDAPV3:
1762 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1767 case LDAP_DN_FORMAT_LDAP:
1768 case LDAP_DN_FORMAT_LDAPV2:
1769 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1774 case LDAP_DN_FORMAT_DCE:
1775 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1784 if ( !LDAP_DN_HEXPAIR( p ) ) {
1791 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1793 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1795 *val = LDAP_MALLOC( sizeof( struct berval ) );
1796 if ( *val == NULL ) {
1797 return( LDAP_NO_MEMORY );
1800 ( *val )->bv_len = len;
1801 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1802 if ( ( *val )->bv_val == NULL ) {
1804 return( LDAP_NO_MEMORY );
1807 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1810 hexstr2bin( &startPos[ s ], &c );
1812 ( *val )->bv_val[ d ] = c;
1815 ( *val )->bv_val[ d ] = '\0';
1822 * convert a byte in a hexadecimal pair
1825 byte2hexpair( const char *val, char *pair )
1827 static const char hexdig[] = "0123456789abcdef";
1833 * we assume the string has enough room for the hex encoding
1837 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1838 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1844 * convert a binary value in hexadecimal pairs
1847 binval2hexstr( struct berval *val, char *str )
1854 if ( val->bv_len == 0 ) {
1859 * we assume the string has enough room for the hex encoding
1863 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1864 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1871 * Length of the string representation, accounting for escaped hex
1875 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1877 ber_len_t l, cl = 1;
1879 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1885 if ( val->bv_len == 0 ) {
1889 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1890 cl = ldap_utf8_charlen( p );
1892 /* illegal utf-8 char! */
1895 } else if ( cl > 1 ) {
1898 for ( cnt = 1; cnt < cl; cnt++ ) {
1899 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1903 l += escaped_byte_len * cl;
1906 * there might be some chars we want to escape in form
1907 * of a couple of hexdigits for optimization purposes
1909 } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
1912 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1913 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1914 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1928 * convert to string representation, escaping with hex the UTF-8 stuff;
1929 * assume the destination has enough room for escaping
1932 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1934 ber_len_t s, d, end;
1940 if ( val->bv_len == 0 ) {
1946 * we assume the string has enough room for the hex encoding
1949 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1950 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
1953 * there might be some chars we want to escape in form
1954 * of a couple of hexdigits for optimization purposes
1956 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1957 || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
1960 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1965 } else if ( cl > 1 ) {
1967 str[ d++ ] = val->bv_val[ s++ ];
1971 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1972 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1973 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
1976 str[ d++ ] = val->bv_val[ s++ ];
1986 * Length of the IA5 string representation (no UTF-8 allowed)
1989 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
1998 if ( val->bv_len == 0 ) {
2002 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2004 * Turn value into a binary encoded BER
2009 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2010 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2011 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2012 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2027 * convert to string representation (np UTF-8)
2028 * assume the destination has enough room for escaping
2031 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2033 ber_len_t s, d, end;
2039 if ( val->bv_len == 0 ) {
2044 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2046 * Turn value into a binary encoded BER
2053 * we assume the string has enough room for the hex encoding
2057 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2058 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2059 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2060 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2063 str[ d++ ] = val->bv_val[ s++ ];
2073 * Length of the (supposedly) DCE string representation,
2074 * accounting for escaped hex of UTF-8 chars
2077 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2086 if ( val->bv_len == 0 ) {
2090 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2092 * FIXME: Turn the value into a binary encoded BER?
2097 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2098 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2113 * convert to (supposedly) DCE string representation,
2114 * escaping with hex the UTF-8 stuff;
2115 * assume the destination has enough room for escaping
2118 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2126 if ( val->bv_len == 0 ) {
2131 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2133 * FIXME: Turn the value into a binary encoded BER?
2141 * we assume the string has enough room for the hex encoding
2145 for ( s = 0, d = 0; s < val->bv_len; ) {
2146 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2149 str[ d++ ] = val->bv_val[ s++ ];
2159 * Length of the (supposedly) AD canonical string representation,
2160 * accounting for escaped hex of UTF-8 chars
2163 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2172 if ( val->bv_len == 0 ) {
2176 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2178 * FIXME: Turn the value into a binary encoded BER?
2183 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2184 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2199 * convert to (supposedly) AD string representation,
2200 * escaping with hex the UTF-8 stuff;
2201 * assume the destination has enough room for escaping
2204 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2212 if ( val->bv_len == 0 ) {
2217 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2219 * FIXME: Turn the value into a binary encoded BER?
2227 * we assume the string has enough room for the hex encoding
2231 for ( s = 0, d = 0; s < val->bv_len; ) {
2232 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2235 str[ d++ ] = val->bv_val[ s++ ];
2245 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2246 * the forst part of the AD representation of the DN is written in DNS
2247 * form, i.e. dot separated domain name components (as suggested
2248 * by Luke Howard, http://www.padl.com/~lukeh)
2251 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2254 int domain = 0, first = 1;
2255 ber_len_t l = 1; /* we move the null also */
2257 /* we are guaranteed there's enough memory in str */
2263 assert( *iRDN > 0 );
2265 for ( i = *iRDN; i >= 0; i-- ) {
2269 assert( dn[ i ][ 0 ] );
2272 assert( rdn[ 0 ][ 0 ] );
2273 ava = rdn[ 0 ][ 0 ];
2275 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2283 AC_MEMCPY( str, ava->la_value->bv_val,
2284 ava->la_value->bv_len + 1);
2285 l += ava->la_value->bv_len;
2288 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2289 AC_MEMCPY( str, ava->la_value->bv_val,
2290 ava->la_value->bv_len );
2291 str[ ava->la_value->bv_len ] = '.';
2292 l += ava->la_value->bv_len + 1;
2302 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2303 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2310 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2311 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2313 /* len(type) + '=' + '+' | ',' */
2314 l += ava->la_attr->bv_len + 2;
2316 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2317 /* octothorpe + twice the length */
2318 l += 1 + 2 * ava->la_value->bv_len;
2322 unsigned f = flags | ava->la_flags;
2324 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2337 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2338 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2343 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2344 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2346 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2347 ava->la_attr->bv_len );
2348 l += ava->la_attr->bv_len;
2352 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2354 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2357 l += 2 * ava->la_value->bv_len;
2361 unsigned f = flags | ava->la_flags;
2363 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2368 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2377 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2384 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2385 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2387 /* len(type) + '=' + ',' | '/' */
2388 l += ava->la_attr->bv_len + 2;
2390 switch ( ava->la_flags ) {
2391 case LDAP_AVA_BINARY:
2392 /* octothorpe + twice the length */
2393 l += 1 + 2 * ava->la_value->bv_len;
2396 case LDAP_AVA_STRING: {
2398 unsigned f = flags | ava->la_flags;
2400 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2418 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2423 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2424 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2429 str[ l++ ] = ( iAVA ? ',' : '/' );
2432 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2433 ava->la_attr->bv_len );
2434 l += ava->la_attr->bv_len;
2438 switch ( ava->la_flags ) {
2439 case LDAP_AVA_BINARY:
2441 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2444 l += 2 * ava->la_value->bv_len;
2447 case LDAP_AVA_STRING: {
2449 unsigned f = flags | ava->la_flags;
2451 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2469 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2479 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2480 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2483 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2485 /* FIXME: are binary values allowed in UFN? */
2486 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2487 /* octothorpe + twice the value */
2488 l += 1 + 2 * ava->la_value->bv_len;
2492 unsigned f = flags | ava->la_flags;
2494 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2507 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2512 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2513 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2515 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2517 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2520 l += 2 * ava->la_value->bv_len;
2524 unsigned f = flags | ava->la_flags;
2526 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2532 if ( rdn[ iAVA + 1 ]) {
2533 AC_MEMCPY( &str[ l ], " + ", 3 );
2537 AC_MEMCPY( &str[ l ], ", ", 2 );
2548 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2558 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2559 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2564 /* FIXME: are binary values allowed in UFN? */
2565 switch ( ava->la_flags ) {
2566 case LDAP_AVA_BINARY:
2567 /* octothorpe + twice the value */
2568 l += 1 + 2 * ava->la_value->bv_len;
2571 case LDAP_AVA_STRING: {
2573 unsigned f = flags | ava->la_flags;
2575 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2593 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2598 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2599 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2604 str[ l++ ] = ( iAVA ? ',' : '/' );
2607 switch ( ava->la_flags ) {
2608 case LDAP_AVA_BINARY:
2610 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2613 l += 2 * ava->la_value->bv_len;
2616 case LDAP_AVA_STRING: {
2618 unsigned f = flags | ava->la_flags;
2620 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2640 * Returns in str a string representation of rdn based on flags.
2641 * There is some duplication of code between this and ldap_dn2str;
2642 * this is wanted to reduce the allocation of temporary buffers.
2645 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2652 if ( rdn == NULL ) {
2653 *str = LDAP_STRDUP( "" );
2654 return( LDAP_SUCCESS );
2658 * This routine wastes "back" bytes at the end of the string
2662 switch ( LDAP_DN_FORMAT( flags ) ) {
2663 case LDAP_DN_FORMAT_LDAPV3:
2664 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2665 return( LDAP_OTHER );
2669 case LDAP_DN_FORMAT_LDAPV2:
2670 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2671 return( LDAP_OTHER );
2675 case LDAP_DN_FORMAT_UFN:
2676 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2677 return( LDAP_OTHER );
2681 case LDAP_DN_FORMAT_DCE:
2682 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2683 return( LDAP_OTHER );
2687 case LDAP_DN_FORMAT_AD_CANONICAL:
2688 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2689 return( LDAP_OTHER );
2694 return( LDAP_INVALID_DN_SYNTAX );
2697 *str = LDAP_MALLOC( l + 1 );
2699 switch ( LDAP_DN_FORMAT( flags ) ) {
2700 case LDAP_DN_FORMAT_LDAPV3:
2701 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2705 case LDAP_DN_FORMAT_LDAPV2:
2706 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2710 case LDAP_DN_FORMAT_UFN:
2711 rc = rdn2UFNstr( rdn, *str, flags, &l );
2715 case LDAP_DN_FORMAT_DCE:
2716 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2720 case LDAP_DN_FORMAT_AD_CANONICAL:
2721 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
2726 /* need at least one of the previous */
2727 return( LDAP_OTHER );
2731 ldap_memfree( *str );
2732 return( LDAP_OTHER );
2735 ( *str )[ l - back ] = '\0';
2737 return( LDAP_SUCCESS );
2741 * Very bulk implementation; many optimizations can be performed
2742 * - a NULL dn results in an empty string ""
2745 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2746 * we must encode it in binary form ('#' + HEXPAIRs)
2747 * b) does DCE/AD support UTF-8?
2748 * no clue; don't think so.
2749 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2750 * use binary encoded BER
2752 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2755 int rc = LDAP_OTHER;
2758 /* stringifying helpers for LDAPv3/LDAPv2 */
2759 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2760 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2764 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2769 * a null dn means an empty dn string
2770 * FIXME: better raise an error?
2773 *str = LDAP_STRDUP( "" );
2774 return( LDAP_SUCCESS );
2777 switch ( LDAP_DN_FORMAT( flags ) ) {
2778 case LDAP_DN_FORMAT_LDAPV3:
2779 sv2l = strval2strlen;
2783 case LDAP_DN_FORMAT_LDAPV2:
2784 sv2l = strval2IA5strlen;
2785 sv2s = strval2IA5str;
2788 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2790 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2792 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2793 goto return_results;
2799 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2800 rc = LDAP_NO_MEMORY;
2804 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2806 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2808 if ( rdn2str( rdn, &( *str )[ l ], flags,
2812 goto return_results;
2820 * trim the last ',' (the allocated memory
2821 * is one byte longer than required)
2823 ( *str )[ len - 1 ] = '\0';
2828 case LDAP_DN_FORMAT_UFN: {
2831 * FIXME: quoting from RFC 1781:
2833 To take a distinguished name, and generate a name of this format with
2834 attribute types omitted, the following steps are followed.
2836 1. If the first attribute is of type CommonName, the type may be
2839 2. If the last attribute is of type Country, the type may be
2842 3. If the last attribute is of type Country, the last
2843 Organisation attribute may have the type omitted.
2845 4. All attributes of type OrganisationalUnit may have the type
2846 omitted, unless they are after an Organisation attribute or
2847 the first attribute is of type OrganisationalUnit.
2849 * this should be the pedantic implementation.
2851 * Here the standard implementation reflects
2852 * the one historically provided by OpenLDAP
2853 * (and UMIch, I presume), with the variant
2854 * of spaces and plusses (' + ') separating
2857 * A non-standard but nice implementation could
2858 * be to turn the final "dc" attributes into a
2859 * dot-separated domain.
2861 * Other improvements could involve the use of
2862 * friendly country names and so.
2865 int leftmost_dc = -1;
2867 #endif /* DC_IN_UFN */
2869 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2871 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2873 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2874 goto return_results;
2879 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2880 if ( leftmost_dc == -1 ) {
2886 #endif /* DC_IN_UFN */
2889 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2890 rc = LDAP_NO_MEMORY;
2895 if ( leftmost_dc == -1 ) {
2896 #endif /* DC_IN_UFN */
2897 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2899 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2901 if ( rdn2UFNstr( rdn, &( *str )[ l ],
2905 goto return_results;
2911 * trim the last ', ' (the allocated memory
2912 * is two bytes longer than required)
2914 ( *str )[ len - 2 ] = '\0';
2917 last_iRDN = iRDN - 1;
2919 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2921 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2923 if ( rdn2UFNstr( rdn, &( *str )[ l ],
2927 goto return_results;
2932 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
2935 goto return_results;
2938 /* the string is correctly terminated by dn2domain */
2940 #endif /* DC_IN_UFN */
2946 case LDAP_DN_FORMAT_DCE:
2948 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2950 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2952 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2953 goto return_results;
2959 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2960 rc = LDAP_NO_MEMORY;
2964 for ( l = 0; iRDN--; ) {
2966 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2968 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
2972 goto return_results;
2979 ( *str )[ len ] = '\0';
2984 case LDAP_DN_FORMAT_AD_CANONICAL: {
2987 * Sort of UFN for DCE DNs: a slash ('/') separated
2988 * global->local DN with no types; strictly speaking,
2989 * the naming context should be a domain, which is
2990 * written in DNS-style, e.g. dot-deparated.
2994 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
2998 * "microsoft.com/People/Bill,Gates"
3000 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3002 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3004 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3005 goto return_results;
3011 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3012 rc = LDAP_NO_MEMORY;
3017 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3018 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3020 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3022 if ( rdn2ADstr( rdn, &( *str )[ l ],
3023 flags, &rdnl, 0 ) ) {
3026 goto return_results;
3035 * Strictly speaking, AD canonical requires
3036 * a DN to be in the form "..., dc=smtg",
3037 * i.e. terminated by a domain component
3039 if ( flags & LDAP_DN_PEDANTIC ) {
3042 rc = LDAP_INVALID_DN_SYNTAX;
3046 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3048 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3050 if ( rdn2ADstr( rdn, &( *str )[ l ],
3051 flags, &rdnl, first ) ) {
3054 goto return_results;
3063 ( *str )[ len ] = '\0';
3070 return( LDAP_INVALID_DN_SYNTAX );
3074 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );