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" */
29 /* #define PRETTY_ESCAPE */
31 static int dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout );
33 /* from libraries/libldap/schema.c */
34 extern char * parse_numericoid(const char **sp, int *code, const int flags);
36 /* parsing/printing routines */
37 static int str2strval( const char *str, struct berval **val,
38 const char **next, unsigned flags, unsigned *retFlags );
39 static int DCE2strval( const char *str, struct berval **val,
40 const char **next, unsigned flags );
41 static int IA52strval( const char *str, struct berval **val,
42 const char **next, unsigned flags );
43 static int quotedIA52strval( const char *str, struct berval **val,
44 const char **next, unsigned flags );
45 static int hexstr2binval( const char *str, struct berval **val,
46 const char **next, unsigned flags );
47 static int hexstr2bin( const char *str, char *c );
48 static int byte2hexpair( const char *val, char *pair );
49 static int binval2hexstr( struct berval *val, char *str );
50 static int strval2strlen( struct berval *val, unsigned flags,
52 static int strval2str( struct berval *val, char *str, unsigned flags,
54 static int strval2IA5strlen( struct berval *val, unsigned flags,
56 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
58 static int strval2DCEstrlen( struct berval *val, unsigned flags,
60 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
62 static int strval2ADstrlen( struct berval *val, unsigned flags,
64 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
66 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
69 static LDAPAVA * ldapava_new(
70 const struct berval *attr, const struct berval *val, unsigned flags );
71 static LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
72 static LDAPRDN * ldapava_insert_into_rdn(
73 LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
74 static LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
75 static LDAPDN * ldapava_insert_into_dn(
76 LDAPDN *dn, LDAPRDN *rdn, unsigned where );
78 /* Higher level helpers */
79 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
80 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
81 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
82 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
83 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
84 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
85 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
86 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
87 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
88 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
91 * RFC 1823 ldap_get_dn
94 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
99 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
101 if ( entry == NULL ) {
102 ld->ld_errno = LDAP_PARAM_ERROR;
106 tmp = *entry->lm_ber; /* struct copy */
107 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
108 ld->ld_errno = LDAP_DECODING_ERROR;
116 * RFC 1823 ldap_dn2ufn
119 ldap_dn2ufn( LDAP_CONST char *dn )
123 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
125 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_UFN );
131 * RFC 1823 ldap_explode_dn
134 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
137 char **values = NULL;
139 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
141 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
143 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
148 if( tmpDN == NULL ) {
149 values = LDAP_MALLOC( sizeof( char * ) );
150 if( values == NULL ) return NULL;
156 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
157 char *str, **v = NULL;
159 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
161 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
163 LBER_VFREE( values );
164 ldap_dnfree( tmpDN );
168 values[ iRDN ] = str;
171 values[ iRDN ] = NULL;
176 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
179 char **values = NULL;
183 notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
185 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
188 * we only parse the first rdn
189 * FIXME: we prefer efficiency over checking if the _ENTIRE_
192 if ( ldap_str2rdn( rdn, &tmpRDN, &p, LDAP_DN_FORMAT_LDAP )
197 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
198 ber_len_t l = 0, vl, al = 0;
199 char *str, **v = NULL;
200 LDAPAVA *ava = tmpRDN[ iAVA ][ 0 ];
202 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
208 if ( ava->la_flags == LDAP_AVA_BINARY ) {
209 vl = 1 + 2 * ava->la_value->bv_len;
212 if ( strval2strlen( ava->la_value,
213 ava->la_flags, &vl ) ) {
219 al = ava->la_attr->bv_len;
220 l = vl + ava->la_attr->bv_len + 1;
222 str = LDAP_MALLOC( l + 1 );
223 AC_MEMCPY( str, ava->la_attr->bv_val,
224 ava->la_attr->bv_len );
229 str = LDAP_MALLOC( l + 1 );
232 if ( ava->la_flags == LDAP_AVA_BINARY ) {
234 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
239 if ( strval2str( ava->la_value, &str[ al ],
240 ava->la_flags, &vl ) ) {
246 values[ iAVA ] = str;
248 values[ iAVA ] = NULL;
250 ldap_rdnfree( tmpRDN );
255 LBER_VFREE( values );
256 ldap_rdnfree( tmpRDN );
261 ldap_dn2dcedn( LDAP_CONST char *dn )
265 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
267 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_DCE );
273 ldap_dcedn2dn( LDAP_CONST char *dce )
277 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
279 ( void )dn2dn( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
285 ldap_dn2ad_canonical( LDAP_CONST char *dn )
289 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
291 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP,
292 &out, LDAP_DN_FORMAT_AD_CANONICAL );
298 ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags )
302 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
304 return dn2dn( in, iflags, out, oflags);
308 * helper that changes the string representation of dnin
309 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
312 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
313 * plus some rfc 1779)
314 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
315 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
316 * LDAP_DN_FORMAT_DCE (?)
318 * fout can be any of the above except
319 * LDAP_DN_FORMAT_LDAP
321 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
322 * LDAP_DN_FORMAT_AD_CANONICAL (?)
325 dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
328 LDAPDN *tmpDN = NULL;
334 if ( dnin == NULL ) {
335 return( LDAP_SUCCESS );
338 rc = ldap_str2dn( dnin , &tmpDN, fin );
339 if ( rc != LDAP_SUCCESS ) {
343 rc = ldap_dn2str( tmpDN, dnout, fout );
345 ldap_dnfree( tmpDN );
353 /* #define B4ATTRTYPE 0x0001 */
354 #define B4OIDATTRTYPE 0x0002
355 #define B4STRINGATTRTYPE 0x0003
357 #define B4AVAEQUALS 0x0100
358 #define B4AVASEP 0x0200
359 #define B4RDNSEP 0x0300
360 #define GOTAVA 0x0400
362 #define B4ATTRVALUE 0x0010
363 #define B4STRINGVALUE 0x0020
364 #define B4IA5VALUEQUOTED 0x0030
365 #define B4IA5VALUE 0x0040
366 #define B4BINARYVALUE 0x0050
368 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
369 #define LDAP_DN_ASCII_SPACE(c) \
370 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
371 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
372 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
373 #define LDAP_DN_ASCII_ALPHA(c) \
374 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
375 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
376 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
377 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
378 #define LDAP_DN_ASCII_HEXDIGIT(c) \
379 ( LDAP_DN_ASCII_DIGIT(c) \
380 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
381 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
382 #define LDAP_DN_ASCII_ALNUM(c) \
383 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
384 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
387 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
388 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
389 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
390 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
391 #define LDAP_DN_ATTRDESC_CHAR(c) \
392 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
394 /* special symbols */
395 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
396 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
397 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
398 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
399 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
400 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
401 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
402 #define LDAP_DN_VALUE_END(c) \
403 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
404 #define LDAP_DN_NE(c) \
405 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
406 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
407 #define LDAP_DN_NEEDESCAPE(c) \
408 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
409 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
410 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
411 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
412 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
413 #define LDAP_DN_WILLESCAPE_CHAR(c) \
414 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
415 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
416 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
417 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
420 #define LDAP_DN_VALUE_END_V2(c) \
421 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
423 #define LDAP_DN_V2_SPECIAL(c) \
424 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
425 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
426 || LDAP_DN_OCTOTHORPE(c) )
427 #define LDAP_DN_V2_PAIR(c) \
428 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
431 * DCE (mostly from Luke Howard and IBM implementation for AIX)
433 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
434 * Here escapes and valid chars for GDS are considered; as soon as more
435 * specific info is found, the macros will be updated.
437 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
438 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
440 * Metachars: '/', ',', '=', '\'.
442 * the '\' is used to escape other metachars.
448 * Attribute types must start with alphabetic chars and can contain
449 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
451 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
452 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
453 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
454 #define LDAP_DN_VALUE_END_DCE(c) \
455 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
456 #define LDAP_DN_NEEDESCAPE_DCE(c) \
457 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
460 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
461 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
462 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
463 #define LDAP_DN_VALUE_END_AD(c) \
464 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
465 #define LDAP_DN_NEEDESCAPE_AD(c) \
466 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
469 #define LDAP_DN_HEXPAIR(s) \
470 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
471 #define LDAP_DC_ATTR "dc"
472 /* better look at the AttributeDescription? */
474 /* FIXME: no composite rdn or non-"dc" types, right?
475 * (what about "dc" in OID form?) */
476 /* FIXME: we do not allow binary values in domain, right? */
477 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
478 #define LDAP_DN_IS_RDN_DC( rdn ) \
479 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
480 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
481 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
483 /* Composite rules */
484 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
485 ( LDAP_DN_LDAPV2(f) \
486 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
487 #define LDAP_DN_ALLOW_SPACES(f) \
488 ( LDAP_DN_LDAPV2(f) \
489 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
490 #define LDAP_DN_LDAP(f) \
491 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
492 #define LDAP_DN_LDAPV3(f) \
493 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
494 #define LDAP_DN_LDAPV2(f) \
495 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
496 #define LDAP_DN_DCE(f) \
497 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
498 #define LDAP_DN_UFN(f) \
499 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
500 #define LDAP_DN_ADC(f) \
501 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
502 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
505 * LDAPAVA helpers (will become part of the API for operations
506 * on structural representations of DNs).
509 ldapava_new( const struct berval *attr, const struct berval *val,
517 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
519 /* should we test it? */
524 ava->la_attr = ( struct berval * )attr;
525 ava->la_value = ( struct berval * )val;
526 ava->la_flags = flags;
528 ava->la_private = NULL;
534 ldap_avafree( LDAPAVA *ava )
539 /* ava's private must be freed by caller
540 * (at present let's skip this check because la_private
541 * basically holds static data) */
542 assert( ava->la_private == NULL );
545 ber_bvfree( ava->la_attr );
546 ber_bvfree( ava->la_value );
552 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
560 for ( i = 0U; rdn[ i ]; i++ ) {
564 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
565 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
566 newRDN[ i ][ 0 ] = ava;
567 newRDN[ i + 1 ] = NULL;
573 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
581 for ( i = 0U; rdn[ i ]; i++ ) {
587 /* assume "at end", which corresponds to
588 * ldapava_append_to_rdn */
591 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
593 /* data after insert point */
594 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
595 ( i - where ) * sizeof( LDAPRDN * ) );
597 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
598 newRDN[ where ][ 0 ] = ava;
599 newRDN[ i + 1 ] = NULL;
605 ldap_rdnfree( LDAPRDN *rdn )
613 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
614 assert( rdn[ iAVA ][ 0 ] );
616 ldap_avafree( rdn[ iAVA ][ 0 ] );
623 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
631 for ( i = 0U; dn[ i ]; i++ ) {
635 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
636 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
637 newDN[ i ][ 0 ] = rdn;
638 newDN[ i + 1 ] = NULL;
644 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
652 for ( i = 0U; dn[ i ]; i++ ) {
658 /* assume "at end", which corresponds to
659 * ldapava_append_to_dn */
662 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
664 /* data after insert point */
665 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
666 ( i - where ) * sizeof( LDAPDN * ) );
668 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
669 newDN[ where ][ 0 ] = rdn;
670 newDN[ i + 1 ] = NULL;
676 ldap_dnfree( LDAPDN *dn )
684 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
685 assert( dn[ iRDN ][ 0 ] );
687 ldap_rdnfree( dn[ iRDN ][ 0 ] );
694 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
695 * into a structural representation of the DN, by separating attribute
696 * types and values encoded in the more appropriate form, which is
697 * string or OID for attribute types and binary form of the BER encoded
698 * value or Unicode string. Formats different from LDAPv3 are parsed
699 * according to their own rules and turned into the more appropriate
700 * form according to LDAPv3.
702 * NOTE: I realize the code is getting spaghettish; it is rather
703 * experimental and will hopefully turn into something more simple
704 * and readable as soon as it works as expected.
708 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
711 int rc = LDAP_INVALID_DN_SYNTAX;
713 LDAPDN *newDN = NULL;
714 LDAPRDN *newRDN = NULL;
719 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
723 switch ( LDAP_DN_FORMAT( flags ) ) {
724 case LDAP_DN_FORMAT_LDAP:
725 case LDAP_DN_FORMAT_LDAPV3:
726 case LDAP_DN_FORMAT_LDAPV2:
727 case LDAP_DN_FORMAT_DCE:
730 /* unsupported in str2dn */
731 case LDAP_DN_FORMAT_UFN:
732 case LDAP_DN_FORMAT_AD_CANONICAL:
733 return( LDAP_INVALID_DN_SYNTAX );
736 return( LDAP_OTHER );
739 if ( str[ 0 ] == '\0' ) {
740 return( LDAP_SUCCESS );
744 if ( LDAP_DN_DCE( flags ) ) {
747 * (from Luke Howard: thnx) A RDN separator is required
748 * at the beginning of an (absolute) DN.
750 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
755 } else if ( LDAP_DN_LDAP( flags ) ) {
757 * if dn starts with '/' let's make it a DCE dn
759 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
760 flags |= LDAP_DN_FORMAT_DCE;
765 for ( ; p[ 0 ]; p++ ) {
769 err = ldap_str2rdn( p, &newRDN, &p, flags );
770 if ( err != LDAP_SUCCESS ) {
775 * We expect a rdn separator
778 switch ( LDAP_DN_FORMAT( flags ) ) {
779 case LDAP_DN_FORMAT_LDAPV3:
780 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
786 case LDAP_DN_FORMAT_LDAP:
787 case LDAP_DN_FORMAT_LDAPV2:
788 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
794 case LDAP_DN_FORMAT_DCE:
795 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
804 if ( LDAP_DN_DCE( flags ) ) {
805 /* add in reversed order */
806 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
808 dn = ldapava_append_to_dn( newDN, newRDN );
819 if ( p[ 0 ] == '\0' ) {
822 * the DN is over, phew
831 ldap_rdnfree( newRDN );
835 ldap_dnfree( newDN );
841 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
850 * Parses a relative DN according to flags up to a rdn separator
851 * or to the end of str.
852 * Returns the rdn and a pointer to the string continuation, which
853 * corresponds to the rdn separator or to '\0' in case the string is over.
856 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
860 int rc = LDAP_INVALID_DN_SYNTAX;
861 int attrTypeEncoding = LDAP_AVA_STRING,
862 attrValueEncoding = LDAP_AVA_STRING;
864 struct berval *attrType = NULL;
865 struct berval *attrValue = NULL;
867 LDAPRDN *newRDN = NULL;
873 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
878 switch ( LDAP_DN_FORMAT( flags ) ) {
879 case LDAP_DN_FORMAT_LDAP:
880 case LDAP_DN_FORMAT_LDAPV3:
881 case LDAP_DN_FORMAT_LDAPV2:
882 case LDAP_DN_FORMAT_DCE:
885 /* unsupported in str2dn */
886 case LDAP_DN_FORMAT_UFN:
887 case LDAP_DN_FORMAT_AD_CANONICAL:
888 return( LDAP_INVALID_DN_SYNTAX );
891 return( LDAP_OTHER );
894 if ( str[ 0 ] == '\0' ) {
895 return( LDAP_SUCCESS );
899 for ( ; p[ 0 ] || state == GOTAVA; ) {
902 * The parser in principle advances one token a time,
903 * or toggles state if preferable.
908 * an AttributeType can be encoded as:
909 * - its string representation; in detail, implementations
910 * MUST recognize AttributeType string type names listed
911 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
912 * MAY recognize other names.
913 * - its numeric OID (a dotted decimal string); in detail
914 * RFC 2253 asserts that ``Implementations MUST allow
915 * an oid in the attribute type to be prefixed by one
916 * of the character strings "oid." or "OID."''. As soon
917 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
918 * I'm not sure whether this is required or not any
919 * longer; to be liberal, we still implement it.
922 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
923 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
930 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
931 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
936 /* whitespace is allowed (and trimmed) */
938 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
943 /* error: we expected an AVA */
949 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
950 state = B4OIDATTRTYPE;
954 /* else must be alpha */
955 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
959 /* LDAPv2 "oid." prefix */
960 if ( LDAP_DN_LDAPV2( flags ) ) {
962 * to be overly pedantic, we only accept
965 if ( flags & LDAP_DN_PEDANTIC ) {
966 if ( !strncmp( p, "OID.", 4 )
967 || !strncmp( p, "oid.", 4 ) ) {
969 state = B4OIDATTRTYPE;
973 if ( !strncasecmp( p, "oid.", 4 ) ) {
975 state = B4OIDATTRTYPE;
981 state = B4STRINGATTRTYPE;
984 case B4OIDATTRTYPE: {
985 int err = LDAP_SUCCESS;
988 type = parse_numericoid( &p, &err, 0 );
989 if ( type == NULL ) {
992 attrType = LDAP_MALLOC( sizeof( struct berval ) );
993 if ( attrType== NULL ) {
997 attrType->bv_val = type;
998 attrType->bv_len = strlen( type );
999 attrTypeEncoding = LDAP_AVA_BINARY;
1001 state = B4AVAEQUALS;
1005 case B4STRINGATTRTYPE: {
1006 const char *startPos, *endPos = NULL;
1010 * the starting char has been found to be
1011 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1012 * FIXME: DCE attr types seem to have a more
1013 * restrictive syntax (no '-' ...)
1015 for ( startPos = p++; p[ 0 ]; p++ ) {
1016 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1020 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1023 * RFC 2253 does not explicitly
1024 * allow lang extensions to attribute
1027 if ( flags & LDAP_DN_PEDANTIC ) {
1032 * we trim ';' and following lang
1033 * and so from attribute types
1036 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1037 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1045 len = ( endPos ? endPos : p ) - startPos;
1050 assert( attrType == NULL );
1051 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1052 if ( attrType == NULL ) {
1053 rc = LDAP_NO_MEMORY;
1056 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1057 if ( attrType->bv_val == NULL ) {
1058 rc = LDAP_NO_MEMORY;
1061 attrType->bv_len = len;
1062 attrTypeEncoding = LDAP_AVA_STRING;
1065 * here we need to decide whether to use it as is
1066 * or turn it in OID form; as a consequence, we
1067 * need to decide whether to binary encode the value
1070 state = B4AVAEQUALS;
1075 /* spaces may not be allowed */
1076 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1077 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1082 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1087 /* need equal sign */
1088 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1093 /* spaces may not be allowed */
1094 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1095 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1100 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1106 * octothorpe means a BER encoded value will follow
1107 * FIXME: I don't think DCE will allow it
1109 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1111 attrValueEncoding = LDAP_AVA_BINARY;
1112 state = B4BINARYVALUE;
1116 /* STRING value expected */
1119 * if we're pedantic, an attribute type in OID form
1120 * SHOULD imply a BER encoded attribute value; we
1121 * should at least issue a warning
1123 if ( ( flags & LDAP_DN_PEDANTIC )
1124 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1125 /* OID attrType SHOULD use binary encoding */
1129 attrValueEncoding = LDAP_AVA_STRING;
1132 * LDAPv2 allows the attribute value to be quoted;
1133 * also, IA5 values are expected, in principle
1135 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1136 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1138 state = B4IA5VALUEQUOTED;
1142 if ( LDAP_DN_LDAPV2( flags ) ) {
1149 * here STRING means RFC 2253 string
1150 * FIXME: what about DCE strings?
1152 state = B4STRINGVALUE;
1156 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1164 switch ( LDAP_DN_FORMAT( flags ) ) {
1165 case LDAP_DN_FORMAT_LDAP:
1166 case LDAP_DN_FORMAT_LDAPV3:
1167 if ( str2strval( p, &attrValue, &p, flags,
1168 &attrValueEncoding ) ) {
1173 case LDAP_DN_FORMAT_DCE:
1174 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1187 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1194 case B4IA5VALUEQUOTED:
1196 /* lead quote already stripped */
1197 if ( quotedIA52strval( p, &attrValue,
1211 * we accept empty values
1213 ava = ldapava_new( attrType, attrValue,
1214 attrValueEncoding );
1215 if ( ava == NULL ) {
1216 rc = LDAP_NO_MEMORY;
1220 rdn = ldapava_append_to_rdn( newRDN, ava );
1221 if ( rdn == NULL ) {
1222 rc = LDAP_NO_MEMORY;
1228 * if we got an AVA separator ('+', or ',' for DCE )
1229 * we expect a new AVA for this RDN; otherwise
1230 * we add the RDN to the DN
1232 switch ( LDAP_DN_FORMAT( flags ) ) {
1233 case LDAP_DN_FORMAT_LDAP:
1234 case LDAP_DN_FORMAT_LDAPV3:
1235 case LDAP_DN_FORMAT_LDAPV2:
1236 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1241 case LDAP_DN_FORMAT_DCE:
1242 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1250 * the RDN is over, phew
1257 /* they should have been used in an AVA */
1273 /* They are set to NULL after they're used in an AVA */
1275 ber_bvfree( attrType );
1279 ber_bvfree( attrValue );
1283 ldap_rdnfree( newRDN );
1289 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1297 * reads in a UTF-8 string value, unescaping stuff:
1298 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1299 * '\' + HEXPAIR(p) -> unhex(p)
1302 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1304 const char *p, *startPos, *endPos = NULL;
1305 ber_len_t len, escapes, unescapes;
1314 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1315 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1317 if ( p[ 0 ] == '\0' ) {
1320 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1321 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1322 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1327 if ( LDAP_DN_HEXPAIR( p ) ) {
1330 hexstr2bin( p, &c );
1333 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1336 * we assume the string is UTF-8
1338 *retFlags = LDAP_AVA_NONPRINTABLE;
1345 if ( LDAP_DN_PEDANTIC & flags ) {
1349 * FIXME: we allow escaping
1350 * of chars that don't need
1351 * to and do not belong to
1352 * HEXDIGITS (we also allow
1353 * single hexdigit; maybe we
1358 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1359 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1362 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1364 * FIXME: maybe we can add
1365 * escapes if not pedantic?
1372 * we do allow unescaped spaces at the end
1373 * of the value only in non-pedantic mode
1375 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1376 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1377 if ( flags & LDAP_DN_PEDANTIC ) {
1381 /* strip trailing (unescaped) spaces */
1382 for ( endPos = p - 1;
1383 endPos > startPos + 1 &&
1384 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1385 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1392 * FIXME: test memory?
1394 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1395 *val = LDAP_MALLOC( sizeof( struct berval ) );
1396 ( *val )->bv_len = len;
1398 if ( escapes == 0 && unescapes == 0 ) {
1399 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1404 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1405 for ( s = 0, d = 0; d < len; ) {
1406 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1408 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1409 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1410 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1411 ( *val )->bv_val[ d++ ] =
1414 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1417 hexstr2bin( &startPos[ s ], &c );
1418 ( *val )->bv_val[ d++ ] = c;
1423 * we allow escaping of chars
1424 * that do not need to
1426 ( *val )->bv_val[ d++ ] =
1431 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1435 ( *val )->bv_val[ d ] = '\0';
1436 assert( strlen( ( *val )->bv_val ) == len );
1446 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1448 const char *p, *startPos, *endPos = NULL;
1449 ber_len_t len, escapes;
1458 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1459 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1461 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1468 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1473 * FIXME: can we accept anything else? I guess we need
1474 * to stop if a value is not legal
1479 * (unescaped) trailing spaces are trimmed must be silently ignored;
1482 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1483 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1484 if ( flags & LDAP_DN_PEDANTIC ) {
1488 /* strip trailing (unescaped) spaces */
1489 for ( endPos = p - 1;
1490 endPos > startPos + 1 &&
1491 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1492 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1499 len = ( endPos ? endPos : p ) - startPos - escapes;
1500 *val = LDAP_MALLOC( sizeof( struct berval ) );
1501 ( *val )->bv_len = len;
1502 if ( escapes == 0 ){
1503 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1508 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1509 for ( s = 0, d = 0; d < len; ) {
1511 * This point is reached only if escapes
1512 * are properly used, so all we need to
1515 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1519 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1521 ( *val )->bv_val[ d ] = '\0';
1522 assert( strlen( ( *val )->bv_val ) == len );
1531 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1533 const char *p, *startPos, *endPos = NULL;
1534 ber_len_t len, escapes;
1547 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1548 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1550 if ( p[ 0 ] == '\0' ) {
1554 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1555 && ( LDAP_DN_PEDANTIC & flags ) ) {
1560 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1565 * FIXME: can we accept anything else? I guess we need
1566 * to stop if a value is not legal
1570 /* strip trailing (unescaped) spaces */
1572 endPos > startPos + 1 &&
1573 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1574 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1579 *val = LDAP_MALLOC( sizeof( struct berval ) );
1580 len = ( endPos ? endPos : p ) - startPos - escapes;
1581 ( *val )->bv_len = len;
1582 if ( escapes == 0 ) {
1583 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1588 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1589 for ( s = 0, d = 0; d < len; ) {
1590 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1593 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1595 ( *val )->bv_val[ d ] = '\0';
1596 assert( strlen( ( *val )->bv_val ) == len );
1604 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1606 const char *p, *startPos, *endPos = NULL;
1608 unsigned escapes = 0;
1617 /* initial quote already eaten */
1618 for ( startPos = p = str; p[ 0 ]; p++ ) {
1620 * According to RFC 1779, the quoted value can
1621 * contain escaped as well as unescaped special values;
1622 * as a consequence we tolerate escaped values
1623 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1624 * (e.g. '","' -> '\,').
1626 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1627 if ( p[ 1 ] == '\0' ) {
1632 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1633 && ( LDAP_DN_PEDANTIC & flags ) ) {
1635 * do we allow to escape normal chars?
1636 * LDAPv2 does not allow any mechanism
1637 * for escaping chars with '\' and hex
1644 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1646 /* eat closing quotes */
1652 * FIXME: can we accept anything else? I guess we need
1653 * to stop if a value is not legal
1657 if ( endPos == NULL ) {
1661 /* Strip trailing (unescaped) spaces */
1662 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1666 len = endPos - startPos - escapes;
1668 *val = LDAP_MALLOC( sizeof( struct berval ) );
1669 ( *val )->bv_len = len;
1670 if ( escapes == 0 ) {
1671 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1676 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1677 ( *val )->bv_len = len;
1679 for ( s = d = 0; d < len; ) {
1680 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1683 ( *val )->bv_val[ d++ ] = str[ s++ ];
1685 ( *val )->bv_val[ d ] = '\0';
1686 assert( strlen( ( *val )->bv_val ) == len );
1695 hexstr2bin( const char *str, char *c )
1705 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1711 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1718 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1724 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1725 *c += c2 - 'a' + 10;
1733 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1735 const char *p, *startPos, *endPos = NULL;
1746 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1747 switch ( LDAP_DN_FORMAT( flags ) ) {
1748 case LDAP_DN_FORMAT_LDAPV3:
1749 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1754 case LDAP_DN_FORMAT_LDAP:
1755 case LDAP_DN_FORMAT_LDAPV2:
1756 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1761 case LDAP_DN_FORMAT_DCE:
1762 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1768 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1769 if ( flags & LDAP_DN_PEDANTIC ) {
1774 for ( ; p[ 0 ]; p++ ) {
1775 switch ( LDAP_DN_FORMAT( flags ) ) {
1776 case LDAP_DN_FORMAT_LDAPV3:
1777 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1782 case LDAP_DN_FORMAT_LDAP:
1783 case LDAP_DN_FORMAT_LDAPV2:
1784 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1789 case LDAP_DN_FORMAT_DCE:
1790 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1799 if ( !LDAP_DN_HEXPAIR( p ) ) {
1806 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1808 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1810 *val = LDAP_MALLOC( sizeof( struct berval ) );
1811 if ( *val == NULL ) {
1812 return( LDAP_NO_MEMORY );
1815 ( *val )->bv_len = len;
1816 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1817 if ( ( *val )->bv_val == NULL ) {
1819 return( LDAP_NO_MEMORY );
1822 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1825 hexstr2bin( &startPos[ s ], &c );
1827 ( *val )->bv_val[ d ] = c;
1830 ( *val )->bv_val[ d ] = '\0';
1837 * convert a byte in a hexadecimal pair
1840 byte2hexpair( const char *val, char *pair )
1842 static const char hexdig[] = "0123456789ABCDEF";
1848 * we assume the string has enough room for the hex encoding
1852 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1853 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1859 * convert a binary value in hexadecimal pairs
1862 binval2hexstr( struct berval *val, char *str )
1869 if ( val->bv_len == 0 ) {
1874 * we assume the string has enough room for the hex encoding
1878 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1879 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1886 * Length of the string representation, accounting for escaped hex
1890 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1892 ber_len_t l, cl = 1;
1894 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1895 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1901 if ( val->bv_len == 0 ) {
1905 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
1906 cl = ldap_utf8_charlen( p );
1908 /* illegal utf-8 char! */
1911 } else if ( cl > 1 ) {
1914 for ( cnt = 1; cnt < cl; cnt++ ) {
1915 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
1919 l += escaped_byte_len * cl;
1921 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
1922 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1923 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
1924 #ifdef PRETTY_ESCAPE
1925 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
1928 * there might be some chars we want
1929 * to escape in form of a couple
1930 * of hexdigits for optimization purposes
1935 l += escaped_ascii_len;
1937 #else /* ! PRETTY_ESCAPE */
1939 #endif /* ! PRETTY_ESCAPE */
1952 * convert to string representation, escaping with hex the UTF-8 stuff;
1953 * assume the destination has enough room for escaping
1956 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
1958 ber_len_t s, d, end;
1964 if ( val->bv_len == 0 ) {
1970 * we assume the string has enough room for the hex encoding
1973 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
1974 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
1977 * there might be some chars we want to escape in form
1978 * of a couple of hexdigits for optimization purposes
1980 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
1981 #ifdef PRETTY_ESCAPE
1982 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
1983 #else /* ! PRETTY_ESCAPE */
1984 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
1985 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
1986 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
1988 #endif /* ! PRETTY_ESCAPE */
1992 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1997 } else if ( cl > 1 ) {
1999 str[ d++ ] = val->bv_val[ s++ ];
2003 #ifdef PRETTY_ESCAPE
2004 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2005 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2006 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2008 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2009 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2015 #endif /* PRETTY_ESCAPE */
2016 str[ d++ ] = val->bv_val[ s++ ];
2026 * Length of the IA5 string representation (no UTF-8 allowed)
2029 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2038 if ( val->bv_len == 0 ) {
2042 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2044 * Turn value into a binary encoded BER
2049 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2050 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2051 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2052 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2067 * convert to string representation (np UTF-8)
2068 * assume the destination has enough room for escaping
2071 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2073 ber_len_t s, d, end;
2079 if ( val->bv_len == 0 ) {
2084 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2086 * Turn value into a binary encoded BER
2093 * we assume the string has enough room for the hex encoding
2097 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2098 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2099 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2100 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2103 str[ d++ ] = val->bv_val[ s++ ];
2113 * Length of the (supposedly) DCE string representation,
2114 * accounting for escaped hex of UTF-8 chars
2117 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2126 if ( val->bv_len == 0 ) {
2130 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2132 * FIXME: Turn the value into a binary encoded BER?
2137 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2138 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2153 * convert to (supposedly) DCE string representation,
2154 * escaping with hex the UTF-8 stuff;
2155 * assume the destination has enough room for escaping
2158 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2166 if ( val->bv_len == 0 ) {
2171 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2173 * FIXME: Turn the value into a binary encoded BER?
2181 * we assume the string has enough room for the hex encoding
2185 for ( s = 0, d = 0; s < val->bv_len; ) {
2186 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2189 str[ d++ ] = val->bv_val[ s++ ];
2199 * Length of the (supposedly) AD canonical string representation,
2200 * accounting for escaped hex of UTF-8 chars
2203 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2212 if ( val->bv_len == 0 ) {
2216 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2218 * FIXME: Turn the value into a binary encoded BER?
2223 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2224 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2239 * convert to (supposedly) AD string representation,
2240 * escaping with hex the UTF-8 stuff;
2241 * assume the destination has enough room for escaping
2244 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2252 if ( val->bv_len == 0 ) {
2257 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2259 * FIXME: Turn the value into a binary encoded BER?
2267 * we assume the string has enough room for the hex encoding
2271 for ( s = 0, d = 0; s < val->bv_len; ) {
2272 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2275 str[ d++ ] = val->bv_val[ s++ ];
2285 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2286 * the forst part of the AD representation of the DN is written in DNS
2287 * form, i.e. dot separated domain name components (as suggested
2288 * by Luke Howard, http://www.padl.com/~lukeh)
2291 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2294 int domain = 0, first = 1;
2295 ber_len_t l = 1; /* we move the null also */
2297 /* we are guaranteed there's enough memory in str */
2303 assert( *iRDN > 0 );
2305 for ( i = *iRDN; i >= 0; i-- ) {
2309 assert( dn[ i ][ 0 ] );
2312 assert( rdn[ 0 ][ 0 ] );
2313 ava = rdn[ 0 ][ 0 ];
2315 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2323 AC_MEMCPY( str, ava->la_value->bv_val,
2324 ava->la_value->bv_len + 1);
2325 l += ava->la_value->bv_len;
2328 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2329 AC_MEMCPY( str, ava->la_value->bv_val,
2330 ava->la_value->bv_len );
2331 str[ ava->la_value->bv_len ] = '.';
2332 l += ava->la_value->bv_len + 1;
2342 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2343 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2350 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2351 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2353 /* len(type) + '=' + '+' | ',' */
2354 l += ava->la_attr->bv_len + 2;
2356 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2357 /* octothorpe + twice the length */
2358 l += 1 + 2 * ava->la_value->bv_len;
2362 unsigned f = flags | ava->la_flags;
2364 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2377 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2378 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2383 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2384 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2386 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2387 ava->la_attr->bv_len );
2388 l += ava->la_attr->bv_len;
2392 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2394 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2397 l += 2 * ava->la_value->bv_len;
2401 unsigned f = flags | ava->la_flags;
2403 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2408 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2417 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2424 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2425 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2427 /* len(type) + '=' + ',' | '/' */
2428 l += ava->la_attr->bv_len + 2;
2430 switch ( ava->la_flags ) {
2431 case LDAP_AVA_BINARY:
2432 /* octothorpe + twice the length */
2433 l += 1 + 2 * ava->la_value->bv_len;
2436 case LDAP_AVA_STRING: {
2438 unsigned f = flags | ava->la_flags;
2440 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2458 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2463 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2464 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2469 str[ l++ ] = ( iAVA ? ',' : '/' );
2472 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2473 ava->la_attr->bv_len );
2474 l += ava->la_attr->bv_len;
2478 switch ( ava->la_flags ) {
2479 case LDAP_AVA_BINARY:
2481 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2484 l += 2 * ava->la_value->bv_len;
2487 case LDAP_AVA_STRING: {
2489 unsigned f = flags | ava->la_flags;
2491 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2509 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2519 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2520 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2523 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2525 /* FIXME: are binary values allowed in UFN? */
2526 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2527 /* octothorpe + twice the value */
2528 l += 1 + 2 * ava->la_value->bv_len;
2532 unsigned f = flags | ava->la_flags;
2534 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2547 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2552 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2553 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2555 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2557 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2560 l += 2 * ava->la_value->bv_len;
2564 unsigned f = flags | ava->la_flags;
2566 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2572 if ( rdn[ iAVA + 1 ]) {
2573 AC_MEMCPY( &str[ l ], " + ", 3 );
2577 AC_MEMCPY( &str[ l ], ", ", 2 );
2588 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2598 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2599 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2604 /* FIXME: are binary values allowed in UFN? */
2605 switch ( ava->la_flags ) {
2606 case LDAP_AVA_BINARY:
2607 /* octothorpe + twice the value */
2608 l += 1 + 2 * ava->la_value->bv_len;
2611 case LDAP_AVA_STRING: {
2613 unsigned f = flags | ava->la_flags;
2615 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2633 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2638 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2639 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2644 str[ l++ ] = ( iAVA ? ',' : '/' );
2647 switch ( ava->la_flags ) {
2648 case LDAP_AVA_BINARY:
2650 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2653 l += 2 * ava->la_value->bv_len;
2656 case LDAP_AVA_STRING: {
2658 unsigned f = flags | ava->la_flags;
2660 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2680 * Returns in str a string representation of rdn based on flags.
2681 * There is some duplication of code between this and ldap_dn2str;
2682 * this is wanted to reduce the allocation of temporary buffers.
2685 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2692 if ( rdn == NULL ) {
2693 *str = LDAP_STRDUP( "" );
2694 return( LDAP_SUCCESS );
2698 * This routine wastes "back" bytes at the end of the string
2702 switch ( LDAP_DN_FORMAT( flags ) ) {
2703 case LDAP_DN_FORMAT_LDAPV3:
2704 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2705 return( LDAP_OTHER );
2709 case LDAP_DN_FORMAT_LDAPV2:
2710 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2711 return( LDAP_OTHER );
2715 case LDAP_DN_FORMAT_UFN:
2716 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2717 return( LDAP_OTHER );
2721 case LDAP_DN_FORMAT_DCE:
2722 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2723 return( LDAP_OTHER );
2727 case LDAP_DN_FORMAT_AD_CANONICAL:
2728 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2729 return( LDAP_OTHER );
2734 return( LDAP_INVALID_DN_SYNTAX );
2737 *str = LDAP_MALLOC( l + 1 );
2739 switch ( LDAP_DN_FORMAT( flags ) ) {
2740 case LDAP_DN_FORMAT_LDAPV3:
2741 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2745 case LDAP_DN_FORMAT_LDAPV2:
2746 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2750 case LDAP_DN_FORMAT_UFN:
2751 rc = rdn2UFNstr( rdn, *str, flags, &l );
2755 case LDAP_DN_FORMAT_DCE:
2756 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2760 case LDAP_DN_FORMAT_AD_CANONICAL:
2761 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
2766 /* need at least one of the previous */
2767 return( LDAP_OTHER );
2771 ldap_memfree( *str );
2772 return( LDAP_OTHER );
2775 ( *str )[ l - back ] = '\0';
2777 return( LDAP_SUCCESS );
2781 * Very bulk implementation; many optimizations can be performed
2782 * - a NULL dn results in an empty string ""
2785 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2786 * we must encode it in binary form ('#' + HEXPAIRs)
2787 * b) does DCE/AD support UTF-8?
2788 * no clue; don't think so.
2789 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2790 * use binary encoded BER
2792 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2795 int rc = LDAP_OTHER;
2798 /* stringifying helpers for LDAPv3/LDAPv2 */
2799 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2800 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2804 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2809 * a null dn means an empty dn string
2810 * FIXME: better raise an error?
2813 *str = LDAP_STRDUP( "" );
2814 return( LDAP_SUCCESS );
2817 switch ( LDAP_DN_FORMAT( flags ) ) {
2818 case LDAP_DN_FORMAT_LDAPV3:
2819 sv2l = strval2strlen;
2823 case LDAP_DN_FORMAT_LDAPV2:
2824 sv2l = strval2IA5strlen;
2825 sv2s = strval2IA5str;
2828 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2830 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2832 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
2833 goto return_results;
2839 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2840 rc = LDAP_NO_MEMORY;
2844 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2846 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2848 if ( rdn2str( rdn, &( *str )[ l ], flags,
2852 goto return_results;
2860 * trim the last ',' (the allocated memory
2861 * is one byte longer than required)
2863 ( *str )[ len - 1 ] = '\0';
2868 case LDAP_DN_FORMAT_UFN: {
2871 * FIXME: quoting from RFC 1781:
2873 To take a distinguished name, and generate a name of this format with
2874 attribute types omitted, the following steps are followed.
2876 1. If the first attribute is of type CommonName, the type may be
2879 2. If the last attribute is of type Country, the type may be
2882 3. If the last attribute is of type Country, the last
2883 Organisation attribute may have the type omitted.
2885 4. All attributes of type OrganisationalUnit may have the type
2886 omitted, unless they are after an Organisation attribute or
2887 the first attribute is of type OrganisationalUnit.
2889 * this should be the pedantic implementation.
2891 * Here the standard implementation reflects
2892 * the one historically provided by OpenLDAP
2893 * (and UMIch, I presume), with the variant
2894 * of spaces and plusses (' + ') separating
2897 * A non-standard but nice implementation could
2898 * be to turn the final "dc" attributes into a
2899 * dot-separated domain.
2901 * Other improvements could involve the use of
2902 * friendly country names and so.
2905 int leftmost_dc = -1;
2907 #endif /* DC_IN_UFN */
2909 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2911 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2913 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
2914 goto return_results;
2919 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
2920 if ( leftmost_dc == -1 ) {
2926 #endif /* DC_IN_UFN */
2929 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2930 rc = LDAP_NO_MEMORY;
2935 if ( leftmost_dc == -1 ) {
2936 #endif /* DC_IN_UFN */
2937 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2939 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2941 if ( rdn2UFNstr( rdn, &( *str )[ l ],
2945 goto return_results;
2951 * trim the last ', ' (the allocated memory
2952 * is two bytes longer than required)
2954 ( *str )[ len - 2 ] = '\0';
2957 last_iRDN = iRDN - 1;
2959 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
2961 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2963 if ( rdn2UFNstr( rdn, &( *str )[ l ],
2967 goto return_results;
2972 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
2975 goto return_results;
2978 /* the string is correctly terminated by dn2domain */
2980 #endif /* DC_IN_UFN */
2986 case LDAP_DN_FORMAT_DCE:
2988 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2990 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2992 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
2993 goto return_results;
2999 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3000 rc = LDAP_NO_MEMORY;
3004 for ( l = 0; iRDN--; ) {
3006 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3008 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
3012 goto return_results;
3019 ( *str )[ len ] = '\0';
3024 case LDAP_DN_FORMAT_AD_CANONICAL: {
3027 * Sort of UFN for DCE DNs: a slash ('/') separated
3028 * global->local DN with no types; strictly speaking,
3029 * the naming context should be a domain, which is
3030 * written in DNS-style, e.g. dot-deparated.
3034 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3038 * "microsoft.com/People/Bill,Gates"
3040 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3042 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3044 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3045 goto return_results;
3051 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3052 rc = LDAP_NO_MEMORY;
3057 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3058 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3060 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3062 if ( rdn2ADstr( rdn, &( *str )[ l ],
3063 flags, &rdnl, 0 ) ) {
3066 goto return_results;
3075 * Strictly speaking, AD canonical requires
3076 * a DN to be in the form "..., dc=smtg",
3077 * i.e. terminated by a domain component
3079 if ( flags & LDAP_DN_PEDANTIC ) {
3082 rc = LDAP_INVALID_DN_SYNTAX;
3086 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3088 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3090 if ( rdn2ADstr( rdn, &( *str )[ l ],
3091 flags, &rdnl, first ) ) {
3094 goto return_results;
3103 ( *str )[ len ] = '\0';
3110 return( LDAP_INVALID_DN_SYNTAX );
3114 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );