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 /* forces the use of new dn parsing routines */
27 /* #define USE_LDAP_DN_PARSING */
28 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
29 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
32 static char *dn2dn( const char *dnin, unsigned fin, unsigned fout );
34 /* from libraries/libldap/schema.c */
35 extern char * parse_numericoid(const char **sp, int *code, const int flags);
37 /* parsing/printing routines */
38 static int str2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags, unsigned *retFlags );
40 static int DCE2strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int IA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int quotedIA52strval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2binval( const char *str, struct berval **val,
47 const char **next, unsigned flags );
48 static int hexstr2bin( const char *str, char *c );
49 static int byte2hexpair( const char *val, char *pair );
50 static int binval2hexstr( struct berval *val, char *str );
51 static int strval2strlen( struct berval *val, unsigned flags,
53 static int strval2str( struct berval *val, char *str, unsigned flags,
55 static int strval2IA5strlen( struct berval *val, unsigned flags,
57 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
59 static int strval2DCEstrlen( struct berval *val, unsigned flags,
61 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
63 static int strval2ADstrlen( struct berval *val, unsigned flags,
65 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
67 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
70 LDAPAVA * ldapava_new( const struct berval *attr, const struct berval *val,
72 void ldapava_free( LDAPAVA *ava );
73 LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
74 LDAPRDN * ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
75 /* void ldapava_free_rdn( LDAPRDN *rdn ); in ldap.h */
76 LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
77 LDAPDN * ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where );
78 /* void ldapava_free_dn( LDAPDN *dn ); in ldap.h */
80 /* Higher level helpers */
81 static int rdn2strlen( LDAPRDN *rdn, ber_len_t *len,
82 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
83 static int rdn2str( LDAPRDN *rdn, char *str, ber_len_t *len,
84 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
85 static int rdn2UFNstrlen( LDAPRDN *rdn, ber_len_t *len );
86 static int rdn2UFNstr( LDAPRDN *rdn, char *str, ber_len_t *len );
87 static int rdn2DCEstrlen( LDAPRDN *rdn, ber_len_t *len );
88 static int rdn2DCEstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first );
89 static int rdn2ADstrlen( LDAPRDN *rdn, ber_len_t *len );
90 static int rdn2ADstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first );
92 #ifndef USE_LDAP_DN_PARSING /* deprecated */
93 #define NAME_TYPE_LDAP_RDN 0
94 #define NAME_TYPE_LDAP_DN 1
95 #define NAME_TYPE_DCE_DN 2
97 static char **explode_name( const char *name, int notypes, int is_type );
98 #endif /* !USE_LDAP_DN_PARSING */
101 * RFC 1823 ldap_get_dn
104 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
109 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
111 if ( entry == NULL ) {
112 ld->ld_errno = LDAP_PARAM_ERROR;
116 tmp = *entry->lm_ber; /* struct copy */
117 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
118 ld->ld_errno = LDAP_DECODING_ERROR;
126 * RFC 1823 ldap_dn2ufn
129 ldap_dn2ufn( LDAP_CONST char *dn )
131 #ifndef USE_LDAP_DN_PARSING /* deprecated */
136 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
138 /* produces completely untyped UFNs */
144 vals = ldap_explode_dn( dn , 0 );
149 for ( i = 0; vals[i]; i++ ) {
152 rvals = ldap_explode_rdn( vals[i] , 1 );
153 if ( rvals == NULL ) {
158 LDAP_FREE( vals[i] );
159 vals[i] = ldap_charray2str( rvals, " + " );
163 ufn = ldap_charray2str( vals, ", " );
167 #else /* USE_LDAP_DN_PARSING */
168 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
170 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_UFN );
171 #endif /* USE_LDAP_DN_PARSING */
175 * RFC 1823 ldap_explode_dn
178 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
180 #ifndef USE_LDAP_DN_PARSING /* deprecated */
181 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
183 return explode_name( dn, notypes, NAME_TYPE_LDAP_DN );
184 #else /* USE_LDAP_DN_PARSING */
186 char **values = NULL;
188 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
190 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
192 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAPV3 )
197 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
198 char *str, **v = NULL;
200 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
202 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
204 LBER_VFREE( values );
205 ldapava_free_dn( tmpDN );
209 values[ iRDN ] = str;
211 values[ iRDN ] = NULL;
214 #endif /* USE_LDAP_DN_PARSING */
218 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
220 #ifndef USE_LDAP_DN_PARSING /* deprecated */
221 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
223 return explode_name( rdn, notypes, NAME_TYPE_LDAP_RDN );
224 #else /* USE_LDAP_DN_PARSING */
226 char **values = NULL;
228 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
230 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
233 * we assume this dn is made of one rdn only
235 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAPV3 )
240 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
241 ber_len_t l = 0, vl, al = 0;
242 char *str, **v = NULL;
243 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
245 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
251 if ( ava->la_flags == LDAP_AVA_BINARY ) {
252 vl = 1 + 2 * ava->la_value->bv_len;
255 if ( strval2strlen( ava->la_value,
256 ava->la_flags, &vl ) ) {
262 al = ava->la_attr->bv_len;
263 l = vl + ava->la_attr->bv_len + 1;
265 str = LDAP_MALLOC( l + 1 );
266 AC_MEMCPY( str, ava->la_attr->bv_val,
267 ava->la_attr->bv_len );
272 str = LDAP_MALLOC( l + 1 );
275 if ( ava->la_flags == LDAP_AVA_BINARY ) {
277 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
282 if ( strval2str( ava->la_value, &str[ al ],
283 ava->la_flags, &vl ) ) {
289 values[ iAVA ] = str;
291 values[ iAVA ] = NULL;
293 ldapava_free_dn( tmpDN );
298 LBER_VFREE( values );
299 ldapava_free_dn( tmpDN );
301 #endif /* USE_LDAP_DN_PARSING */
305 ldap_dn2dcedn( LDAP_CONST char *dn )
307 #ifndef USE_LDAP_DN_PARSING /* deprecated */
308 char *dce, *q, **rdns, **p;
311 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
313 rdns = explode_name( dn, 0, NAME_TYPE_LDAP_DN );
314 if ( rdns == NULL ) {
318 for ( p = rdns; *p != NULL; p++ ) {
319 len += strlen( *p ) + 1;
322 q = dce = LDAP_MALLOC( len + 1 );
327 p--; /* get back past NULL */
329 for ( ; p != rdns; p-- ) {
341 #else /* USE_LDAP_DN_PARSING */
342 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
344 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_DCE );
345 #endif /* USE_LDAP_DN_PARSING */
349 ldap_dcedn2dn( LDAP_CONST char *dce )
351 #ifndef USE_LDAP_DN_PARSING
352 char *dn, *q, **rdns, **p;
355 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
357 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
358 if ( rdns == NULL ) {
364 for ( p = rdns; *p != NULL; p++ ) {
365 len += strlen( *p ) + 1;
368 q = dn = LDAP_MALLOC( len );
375 for ( ; p != rdns; p-- ) {
383 /* the name was fully qualified, thus the most-significant
384 * RDN was empty. trash the last comma */
388 /* the name was relative. copy the most significant RDN */
393 #else /* USE_LDAP_DN_PARSING */
394 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
396 return dn2dn( dce, LDAP_DN_FORMAT_DCE, LDAP_DN_FORMAT_LDAPV3 );
397 #endif /* USE_LDAP_DN_PARSING */
401 ldap_dn2ad_canonical( LDAP_CONST char *dn )
403 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
405 return dn2dn( dn, LDAP_DN_FORMAT_LDAPV3, LDAP_DN_FORMAT_AD_CANONICAL );
408 #ifndef USE_LDAP_DN_PARSING /* deprecated */
413 explode_name( const char *name, int notypes, int is_type )
415 const char *p, *q, *rdn;
417 int offset, state, have_equals, count = 0, endquote, len;
420 if(name == NULL) name = "";
422 /* skip leading whitespace */
423 while( ldap_utf8_isspace( name )) {
424 LDAP_UTF8_INCR( name );
439 if ( p[1] != '\0' ) {
440 offset = LDAP_UTF8_OFFSET(++p);
444 if ( state == INQUOTE )
450 if( state == OUTQUOTE ) have_equals++;
453 if (is_type == NAME_TYPE_LDAP_RDN)
457 if (is_type == NAME_TYPE_DCE_DN)
462 if (is_type == NAME_TYPE_LDAP_DN)
467 if ( state == OUTQUOTE ) {
471 if ( parts == NULL ) {
472 if (( parts = (char **)LDAP_MALLOC( 8
473 * sizeof( char *))) == NULL )
475 } else if ( count >= 8 ) {
476 if (( parts = (char **)LDAP_REALLOC( parts,
477 (count+1) * sizeof( char *)))
482 parts[ count ] = NULL;
486 for ( q = rdn; q < p && *q != '='; ++q ) {
498 if ( p[-1] == '"' ) {
506 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
509 AC_MEMCPY( parts[ count-1 ], rdn, len );
512 /* skip trailing spaces */
513 while( len > 0 && ldap_utf8_isspace(
514 &parts[count-1][len-1] ) )
520 parts[ count-1 ][ len ] = '\0';
524 * Don't forget to increment 'p' back to where
525 * it should be. If we don't, then we will
526 * never get past an "end quote."
531 rdn = *p ? &p[1] : p;
532 while ( ldap_utf8_isspace( rdn ) )
540 #endif /* !USE_LDAP_DN_PARSING */
543 * helper that changes the string representation of dnin
544 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
547 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
548 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
549 * LDAP_DN_FORMAT_DCE (?)
551 * fout can be any of the above plus:
552 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
553 * LDAP_DN_FORMAT_AD_CANONICAL (?)
556 dn2dn( const char *dnin, unsigned fin, unsigned fout )
559 LDAPDN *tmpDN = NULL;
565 if ( ldap_str2dn( dnin , &tmpDN, fin ) != LDAP_SUCCESS ) {
569 /* don't care about the result ... */
570 ldap_dn2str( tmpDN, &dnout, fout );
572 ldapava_free_dn( tmpDN );
580 #define B4ATTRTYPE 0x0001
581 #define B4OIDATTRTYPE 0x0002
582 #define B4STRINGATTRTYPE 0x0003
584 #define B4AVAEQUALS 0x0100
585 #define B4AVASEP 0x0200
586 #define B4RDNSEP 0x0300
587 #define GOTAVA 0x0400
589 #define B4ATTRVALUE 0x0010
590 #define B4STRINGVALUE 0x0020
591 #define B4IA5VALUEQUOTED 0x0030
592 #define B4IA5VALUE 0x0040
593 #define B4BINARYVALUE 0x0050
595 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
596 #define LDAP_DN_ASCII_SPACE(c) \
597 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
598 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
599 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
600 #define LDAP_DN_ASCII_ALPHA(c) \
601 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
602 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
603 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
604 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
605 #define LDAP_DN_ASCII_HEXDIGIT(c) \
606 ( LDAP_DN_ASCII_DIGIT(c) \
607 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
608 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
609 #define LDAP_DN_ASCII_ALNUM(c) \
610 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
611 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
614 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
615 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
616 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
617 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
618 #define LDAP_DN_ATTRDESC_CHAR(c) \
619 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
621 /* special symbols */
622 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
623 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
624 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
625 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
626 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
627 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
628 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
629 #define LDAP_DN_VALUE_END(c) \
630 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
631 #define LDAP_DN_NE(c) \
632 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
633 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
634 #define LDAP_DN_NEEDESCAPE(c) \
635 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
636 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
637 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
638 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
639 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
642 #define LDAP_DN_VALUE_END_V2(c) \
643 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
645 #define LDAP_DN_V2_SPECIAL(c) \
646 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
647 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
648 || LDAP_DN_OCTOTHORPE(c) )
649 #define LDAP_DN_V2_PAIR(c) \
650 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
653 * DCE (mostly from Luke Howard and IBM implementation for AIX)
655 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
656 * Here escapes and valid chars for GDS are considered; as soon as more
657 * specific info is found, the macros will be updated.
659 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
660 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
662 * Metachars: '/', ',', '=', '\'.
664 * the '\' is used to escape other metachars.
670 * Attribute types must start with alphabetic chars and can contain
671 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
673 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
674 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
675 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
676 #define LDAP_DN_VALUE_END_DCE(c) \
677 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
678 #define LDAP_DN_NEEDESCAPE_DCE(c) \
679 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
682 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
683 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
684 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
685 #define LDAP_DN_VALUE_END_AD(c) \
686 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
687 #define LDAP_DN_NEEDESCAPE_AD(c) \
688 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
691 #define LDAP_DN_HEXPAIR(s) \
692 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
693 #define LDAP_DC_ATTR "dc"
694 /* better look at the AttributeDescription? */
696 /* FIXME: no composite rdn or non-"dc" types, right?
697 * (what about "dc" in OID form?) */
698 /* FIXME: we do not allow binary values in domain, right? */
699 #define LDAP_DN_IS_RDN_DC( rdn ) \
700 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
701 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
702 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
704 /* Composite rules */
705 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
706 ( ( (f) & LDAP_DN_FORMAT_LDAPV2 ) \
707 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
708 #define LDAP_DN_ALLOW_SPACES(f) \
709 ( ( (f) & LDAP_DN_FORMAT_LDAPV2 ) \
710 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
711 #define LDAP_DN_LDAPV3(f) \
712 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
713 #define LDAP_DN_LDAPV2(f) \
714 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
715 #define LDAP_DN_DCE(f) \
716 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
717 #define LDAP_DN_UFN(f) \
718 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
719 #define LDAP_DN_ADC(f) \
720 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
721 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
724 * LDAPAVA helpers (will become part of the API for operations
725 * on structural representations of DNs).
728 ldapava_new( const struct berval *attr, const struct berval *val,
736 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
738 /* should we test it? */
743 ava->la_attr = ( struct berval * )attr;
744 ava->la_value = ( struct berval * )val;
745 ava->la_flags = flags;
751 ldapava_free( LDAPAVA *ava )
755 ber_bvfree( ava->la_attr );
756 ber_bvfree( ava->la_value );
762 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
770 for ( i = 0U; rdn[ i ]; i++ ) {
774 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
775 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
776 newRDN[ i ][ 0 ] = ava;
777 newRDN[ i + 1 ] = NULL;
783 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
791 for ( i = 0U; rdn[ i ]; i++ ) {
797 /* assume "at end", which corresponds to
798 * ldapava_append_to_rdn */
801 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
803 /* data after insert point */
804 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
805 ( i - where ) * sizeof( LDAPRDN * ) );
807 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
808 newRDN[ where ][ 0 ] = ava;
809 newRDN[ i + 1 ] = NULL;
815 ldapava_free_rdn( LDAPRDN *rdn )
823 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
824 assert( rdn[ iAVA ][ 0 ] );
826 ldapava_free( rdn[ iAVA ][ 0 ] );
833 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
841 for ( i = 0U; dn[ i ]; i++ ) {
845 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
846 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
847 newDN[ i ][ 0 ] = rdn;
848 newDN[ i + 1 ] = NULL;
854 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
862 for ( i = 0U; dn[ i ]; i++ ) {
868 /* assume "at end", which corresponds to
869 * ldapava_append_to_dn */
872 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
874 /* data after insert point */
875 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
876 ( i - where ) * sizeof( LDAPDN * ) );
878 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
879 newDN[ where ][ 0 ] = rdn;
880 newDN[ i + 1 ] = NULL;
886 ldapava_free_dn( LDAPDN *dn )
894 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
895 assert( dn[ iRDN ][ 0 ] );
897 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
904 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
905 * into a structured representation of the DN, by separating attribute
906 * types and values encoded in the more appropriate form, which is
907 * string or OID for attribute types and binary form of the BER encoded
908 * value or Unicode string. Formats different from LDAPv3 are parsed
909 * according to their own rules and turned into the more appropriate
910 * form according to LDAPv3.
912 * NOTE: I realize the code is getting spaghettish; it is rather
913 * experimental and will hopefully turn into something more simple
914 * and readable as soon as it works as expected.
918 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
921 int rc = LDAP_INVALID_DN_SYNTAX;
923 LDAPDN *newDN = NULL;
924 LDAPRDN *newRDN = NULL;
929 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
933 switch ( LDAP_DN_FORMAT( flags ) ) {
934 case LDAP_DN_FORMAT_LDAPV3:
935 case LDAP_DN_FORMAT_LDAPV2:
936 case LDAP_DN_FORMAT_DCE:
939 /* unsupported in str2dn */
940 case LDAP_DN_FORMAT_UFN:
941 case LDAP_DN_FORMAT_AD_CANONICAL:
942 return( LDAP_INVALID_DN_SYNTAX );
945 return( LDAP_OTHER );
948 if ( str[ 0 ] == '\0' ) {
949 return( LDAP_SUCCESS );
953 if ( LDAP_DN_DCE( flags ) ) {
956 * (from Luke Howard: thnx) A RDN separator is required
957 * at the beginning of an (absolute) DN.
959 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
965 for ( ; p[ 0 ]; p++ ) {
968 rc = ldap_str2rdn( p, &newRDN, &p, flags );
969 if ( rc != LDAP_SUCCESS ) {
974 * We expect a rdn separator
977 switch ( LDAP_DN_FORMAT( flags ) ) {
978 case LDAP_DN_FORMAT_LDAPV3:
979 case LDAP_DN_FORMAT_LDAPV2:
980 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
986 case LDAP_DN_FORMAT_DCE:
987 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
996 if ( LDAP_DN_DCE( flags ) ) {
997 /* add in reversed order */
998 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1000 dn = ldapava_append_to_dn( newDN, newRDN );
1004 rc = LDAP_NO_MEMORY;
1011 if ( p[ 0 ] == '\0' ) {
1014 * the DN is over, phew
1023 ldapava_free_rdn( newRDN );
1027 ldapava_free_dn( newDN );
1033 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1042 * Parses a relative DN according to flags up to a rdn separator
1043 * or to the end of str.
1044 * Returns the rdn and a pointer to the string continuation, which
1045 * corresponds to the rdn separator or to '\0' in case the string is over.
1048 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1052 int rc = LDAP_INVALID_DN_SYNTAX;
1053 int attrTypeEncoding = LDAP_AVA_STRING,
1054 attrValueEncoding = LDAP_AVA_STRING;
1056 struct berval *attrType = NULL;
1057 struct berval *attrValue = NULL;
1059 LDAPRDN *newRDN = NULL;
1065 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1070 switch ( LDAP_DN_FORMAT( flags ) ) {
1071 case LDAP_DN_FORMAT_LDAPV3:
1072 case LDAP_DN_FORMAT_LDAPV2:
1073 case LDAP_DN_FORMAT_DCE:
1076 /* unsupported in str2dn */
1077 case LDAP_DN_FORMAT_UFN:
1078 case LDAP_DN_FORMAT_AD_CANONICAL:
1079 return( LDAP_INVALID_DN_SYNTAX );
1082 return( LDAP_OTHER );
1085 if ( str[ 0 ] == '\0' ) {
1086 return( LDAP_SUCCESS );
1090 for ( ; p[ 0 ] || state == GOTAVA; ) {
1093 * The parser in principle advances one token a time,
1094 * or toggles state if preferable.
1099 * an AttributeType can be encoded as:
1100 * - its string representation; in detail, implementations
1101 * MUST recognize AttributeType string type names listed
1102 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1103 * MAY recognize other names.
1104 * - its numeric OID (a dotted decimal string); in detail
1105 * RFC 2253 asserts that ``Implementations MUST allow
1106 * an oid in the attribute type to be prefixed by one
1107 * of the character strings "oid." or "OID."''. As soon
1108 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1109 * I'm not sure whether this is required or not any
1110 * longer; to be liberal, we still implement it.
1113 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1114 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1121 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1122 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1127 /* whitespace is allowed (and trimmed) */
1129 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1134 /* error: we expected an AVA */
1144 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1145 state = B4OIDATTRTYPE;
1149 /* else must be alpha */
1150 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1154 /* LDAPv2 "oid." prefix */
1155 if ( LDAP_DN_LDAPV2( flags ) ) {
1157 * to be overly pedantic, we only accept
1160 if ( flags & LDAP_DN_PEDANTIC ) {
1161 if ( !strncmp( p, "oid.", 4 )
1162 || !strncmp( p, "OID.", 4 ) ) {
1164 state = B4OIDATTRTYPE;
1168 if ( !strncasecmp( p, "oid.", 4 ) ) {
1170 state = B4OIDATTRTYPE;
1176 state = B4STRINGATTRTYPE;
1179 case B4OIDATTRTYPE: {
1180 int err = LDAP_SUCCESS;
1183 type = parse_numericoid( &p, &err, 0 );
1184 if ( type == NULL ) {
1187 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1188 if ( attrType== NULL ) {
1189 rc = LDAP_NO_MEMORY;
1192 attrType->bv_val = type;
1193 attrType->bv_len = strlen( type );
1194 attrTypeEncoding = LDAP_AVA_BINARY;
1196 state = B4AVAEQUALS;
1200 case B4STRINGATTRTYPE: {
1201 const char *startPos, *endPos = NULL;
1205 * the starting char has been found to be
1206 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1207 * FIXME: DCE attr types seem to have a more
1208 * restrictive syntax
1210 for ( startPos = p++; p[ 0 ]; p++ ) {
1211 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1215 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1218 * RFC 2253 does not explicitly
1219 * allow lang extensions to attribute
1222 if ( flags & LDAP_DN_PEDANTIC ) {
1227 * we trim ';' and following lang
1228 * and so from attribute types
1231 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1232 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1240 len = ( endPos ? endPos : p ) - startPos;
1245 assert( attrType == NULL );
1246 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1247 if ( attrType == NULL ) {
1248 rc = LDAP_NO_MEMORY;
1251 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1252 if ( attrType->bv_val == NULL ) {
1253 rc = LDAP_NO_MEMORY;
1256 attrType->bv_len = len;
1257 attrTypeEncoding = LDAP_AVA_STRING;
1260 * here we need to decide whether to use it as is
1261 * or turn it in OID form; as a consequence, we
1262 * need to decide whether to binary encode the value
1265 state = B4AVAEQUALS;
1270 /* spaces may not be allowed */
1271 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1272 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1277 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1282 /* need equal sign */
1283 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1288 /* spaces may not be allowed */
1289 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1290 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1295 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1301 * octothorpe means a BER encoded value will follow
1302 * FIXME: I don't think DCE will allow it
1304 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1306 attrValueEncoding = LDAP_AVA_BINARY;
1307 state = B4BINARYVALUE;
1311 /* STRING value expected */
1314 * if we're pedantic, an attribute type in OID form
1315 * SHOULD imply a BER encoded attribute value; we
1316 * should at least issue a warning
1318 if ( ( flags & LDAP_DN_PEDANTIC )
1319 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1320 /* OID attrType SHOULD use binary encoding */
1324 attrValueEncoding = LDAP_AVA_STRING;
1327 * LDAPv2 allows the attribute value to be quoted;
1328 * also, IA5 values are expected, in principle
1330 if ( LDAP_DN_LDAPV2( flags ) ) {
1331 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1333 state = B4IA5VALUEQUOTED;
1342 * here STRING means RFC 2253 string
1343 * FIXME: what about DCE strings?
1345 state = B4STRINGVALUE;
1349 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1357 switch ( LDAP_DN_FORMAT( flags ) ) {
1358 case LDAP_DN_FORMAT_LDAPV3:
1359 if ( str2strval( p, &attrValue,
1361 &attrValueEncoding ) ) {
1366 case LDAP_DN_FORMAT_DCE:
1367 /* FIXME: does DCE use UTF-8? */
1368 if ( DCE2strval( p, &attrValue,
1382 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1389 case B4IA5VALUEQUOTED:
1391 /* lead quote already stripped */
1392 if ( quotedIA52strval( p, &attrValue,
1406 * we accept empty values
1408 ava = ldapava_new( attrType, attrValue,
1409 attrValueEncoding );
1410 if ( ava == NULL ) {
1411 rc = LDAP_NO_MEMORY;
1415 rdn = ldapava_append_to_rdn( newRDN, ava );
1416 if ( rdn == NULL ) {
1417 rc = LDAP_NO_MEMORY;
1423 * if we got an AVA separator ('+', | ',' * for DCE )
1424 * we expect a new AVA for this RDN; otherwise
1425 * we add the RDN to the DN
1427 switch ( LDAP_DN_FORMAT( flags ) ) {
1428 case LDAP_DN_FORMAT_LDAPV3:
1429 case LDAP_DN_FORMAT_LDAPV2:
1430 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1435 case LDAP_DN_FORMAT_DCE:
1436 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1444 * the RDN is over, phew
1451 /* they should have been used in an AVA */
1467 /* They are set to NULL after they're used in an AVA */
1469 ber_bvfree( attrType );
1473 ber_bvfree( attrValue );
1477 ldapava_free_rdn( newRDN );
1482 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1490 * reads in a UTF-8 string value, unescaping stuff:
1491 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1492 * '\' + HEXPAIR(p) -> unhex(p)
1495 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1497 const char *p, *startPos, *endPos = NULL;
1498 ber_len_t len, escapes, unescapes;
1507 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1508 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1510 if ( p[ 0 ] == '\0' ) {
1513 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1514 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1515 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1520 if ( LDAP_DN_HEXPAIR( p ) ) {
1523 hexstr2bin( p, &c );
1526 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1529 * we assume the string is UTF-8
1531 *retFlags = LDAP_AVA_NONPRINTABLE;
1538 if ( LDAP_DN_PEDANTIC & flags ) {
1542 * FIXME: we allow escaping
1543 * of chars that don't need
1544 * to and do not belong to
1545 * HEXDIGITS (we also allow
1546 * single hexdigit; maybe we
1551 } else if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1554 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1556 * FIXME: maybe we can add
1557 * escapes if not pedantic?
1564 * we do allow unescaped spaces at the end
1565 * of the value only in non-pedantic mode
1567 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1568 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1569 if ( flags & LDAP_DN_PEDANTIC ) {
1573 /* strip trailing (unescaped) spaces */
1574 for ( endPos = p - 1;
1575 endPos > startPos + 1 &&
1576 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1577 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1584 * FIXME: test memory?
1586 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1587 *val = LDAP_MALLOC( sizeof( struct berval ) );
1588 ( *val )->bv_len = len;
1590 if ( escapes == 0 && unescapes == 0 ) {
1591 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1596 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1597 for ( s = 0, d = 0; d < len; ) {
1598 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1600 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1601 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1602 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1603 ( *val )->bv_val[ d++ ] =
1606 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1609 hexstr2bin( &startPos[ s ], &c );
1610 ( *val )->bv_val[ d++ ] = c;
1615 * we allow escaping of chars
1616 * that do not need to
1618 ( *val )->bv_val[ d++ ] =
1623 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1627 ( *val )->bv_val[ d ] = '\0';
1628 assert( strlen( ( *val )->bv_val ) == len );
1638 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1640 const char *p, *startPos, *endPos = NULL;
1641 ber_len_t len, escapes;
1650 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1651 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1653 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1660 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1665 * FIXME: can we accept anything else? I guess we need
1666 * to stop if a value is not legal
1671 * (unescaped) trailing spaces are trimmed must be silently ignored;
1674 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1675 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1676 if ( flags & LDAP_DN_PEDANTIC ) {
1680 /* strip trailing (unescaped) spaces */
1681 for ( endPos = p - 1;
1682 endPos > startPos + 1 &&
1683 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1684 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1691 len = ( endPos ? endPos : p ) - startPos - escapes;
1692 *val = LDAP_MALLOC( sizeof( struct berval ) );
1693 ( *val )->bv_len = len;
1694 if ( escapes == 0 ){
1695 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1700 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1701 for ( s = 0, d = 0; d < len; ) {
1703 * This point is reached only if escapes
1704 * are properly used, so all we need to
1707 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1711 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1713 ( *val )->bv_val[ d ] = '\0';
1714 assert( strlen( ( *val )->bv_val ) == len );
1723 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1725 const char *p, *startPos, *endPos = NULL;
1726 ber_len_t len, escapes;
1739 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1740 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1742 if ( p[ 0 ] == '\0' ) {
1746 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1747 && ( LDAP_DN_PEDANTIC & flags ) ) {
1752 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1757 * FIXME: can we accept anything else? I guess we need
1758 * to stop if a value is not legal
1762 /* strip trailing (unescaped) spaces */
1764 endPos > startPos + 1 &&
1765 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1766 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1771 *val = LDAP_MALLOC( sizeof( struct berval ) );
1772 len = ( endPos ? endPos : p ) - startPos - escapes;
1773 ( *val )->bv_len = len;
1774 if ( escapes == 0 ) {
1775 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1780 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1781 for ( s = 0, d = 0; d < len; ) {
1782 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1785 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1787 ( *val )->bv_val[ d ] = '\0';
1788 assert( strlen( ( *val )->bv_val ) == len );
1796 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1798 const char *p, *startPos, *endPos = NULL;
1800 unsigned escapes = 0;
1809 /* initial quote already eaten */
1810 for ( startPos = p = str; p[ 0 ]; p++ ) {
1812 * According to RFC 1779, the quoted value can
1813 * contain escaped as well as unescaped special values;
1814 * as a consequence we tolerate escaped values
1815 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1816 * (e.g. '","' -> '\,').
1818 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1819 if ( p[ 1 ] == '\0' ) {
1824 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1825 && ( LDAP_DN_PEDANTIC & flags ) ) {
1827 * do we allow to escape normal chars?
1828 * LDAPv2 does not allow any mechanism
1829 * for escaping chars with '\' and hex
1836 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1838 /* eat closing quotes */
1844 * FIXME: can we accept anything else? I guess we need
1845 * to stop if a value is not legal
1849 if ( endPos == NULL ) {
1853 /* Strip trailing (unescaped) spaces */
1854 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1858 len = endPos - startPos - escapes;
1860 *val = LDAP_MALLOC( sizeof( struct berval ) );
1861 ( *val )->bv_len = len;
1862 if ( escapes == 0 ) {
1863 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1868 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1869 ( *val )->bv_len = len;
1871 for ( s = d = 0; d < len; ) {
1872 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1875 ( *val )->bv_val[ d++ ] = str[ s++ ];
1877 ( *val )->bv_val[ d ] = '\0';
1878 assert( strlen( ( *val )->bv_val ) == len );
1887 hexstr2bin( const char *str, char *c )
1897 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1903 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1910 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1916 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1917 *c += c2 - 'a' + 10;
1925 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1927 const char *p, *startPos, *endPos = NULL;
1938 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1939 switch ( LDAP_DN_FORMAT( flags ) ) {
1940 case LDAP_DN_FORMAT_LDAPV3:
1941 case LDAP_DN_FORMAT_LDAPV2:
1942 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1947 case LDAP_DN_FORMAT_DCE:
1948 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1954 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1955 if ( flags & LDAP_DN_PEDANTIC ) {
1960 for ( ; p[ 0 ]; p++ ) {
1961 switch ( LDAP_DN_FORMAT( flags ) ) {
1962 case LDAP_DN_FORMAT_LDAPV3:
1963 case LDAP_DN_FORMAT_LDAPV2:
1964 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1969 case LDAP_DN_FORMAT_DCE:
1970 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1979 if ( !LDAP_DN_HEXPAIR( p ) ) {
1986 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1988 assert( 2 * len == ( endPos ? endPos : p ) - startPos );
1990 *val = LDAP_MALLOC( sizeof( struct berval ) );
1991 if ( *val == NULL ) {
1992 return( LDAP_NO_MEMORY );
1995 ( *val )->bv_len = len;
1996 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1997 if ( ( *val )->bv_val == NULL ) {
1999 return( LDAP_NO_MEMORY );
2002 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2005 hexstr2bin( &startPos[ s ], &c );
2007 ( *val )->bv_val[ d ] = c;
2010 ( *val )->bv_val[ d ] = '\0';
2017 * convert a byte in a hexadecimal pair
2020 byte2hexpair( const char *val, char *pair )
2022 static const char hexdig[] = "0123456789abcdef";
2028 * we assume the string has enough room for the hex encoding
2032 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2033 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2039 * convert a binary value in hexadecimal pairs
2042 binval2hexstr( struct berval *val, char *str )
2049 if ( val->bv_len == 0 ) {
2054 * we assume the string has enough room for the hex encoding
2058 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2059 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2066 * Length of the string representation, accounting for escaped hex
2070 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2072 ber_len_t l, cl = 1;
2079 if ( val->bv_len == 0 ) {
2083 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2084 cl = ldap_utf8_charlen( p );
2086 /* illegal utf-8 char! */
2089 } else if ( cl > 1 ) {
2092 for ( cnt = 1; cnt < cl; cnt++ ) {
2093 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2097 /* need to escape it */
2100 } else if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2101 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2102 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2116 * convert to string representation, escaping with hex the UTF-8 stuff;
2117 * assume the destination has enough room for escaping
2120 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2122 ber_len_t s, d, end;
2128 if ( val->bv_len == 0 ) {
2134 * we assume the string has enough room for the hex encoding
2137 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2138 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2143 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2149 if ( ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2150 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2151 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2154 str[ d++ ] = val->bv_val[ s++ ];
2164 * Length of the IA5 string representation (no UTF-8 allowed)
2167 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2176 if ( val->bv_len == 0 ) {
2180 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2182 * Turn value into a binary encoded BER
2187 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2188 if ( ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2189 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
2190 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
2205 * convert to string representation (np UTF-8)
2206 * assume the destination has enough room for escaping
2209 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2211 ber_len_t s, d, end;
2217 if ( val->bv_len == 0 ) {
2222 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2224 * Turn value into a binary encoded BER
2231 * we assume the string has enough room for the hex encoding
2235 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2236 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2237 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2238 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] ) ) {
2241 str[ d++ ] = val->bv_val[ s++ ];
2251 * Length of the (supposedly) DCE string representation,
2252 * accounting for escaped hex of UTF-8 chars
2255 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2264 if ( val->bv_len == 0 ) {
2268 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2270 * FIXME: Turn the value into a binary encoded BER?
2275 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2276 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2291 * convert to (supposedly) DCE string representation,
2292 * escaping with hex the UTF-8 stuff;
2293 * assume the destination has enough room for escaping
2296 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2304 if ( val->bv_len == 0 ) {
2309 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2311 * FIXME: Turn the value into a binary encoded BER?
2319 * we assume the string has enough room for the hex encoding
2323 for ( s = 0, d = 0; s < val->bv_len; ) {
2324 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2327 str[ d++ ] = val->bv_val[ s++ ];
2337 * Length of the (supposedly) AD canonical string representation,
2338 * accounting for escaped hex of UTF-8 chars
2341 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2350 if ( val->bv_len == 0 ) {
2354 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2356 * FIXME: Turn the value into a binary encoded BER?
2361 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2362 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2377 * convert to (supposedly) AD string representation,
2378 * escaping with hex the UTF-8 stuff;
2379 * assume the destination has enough room for escaping
2382 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2390 if ( val->bv_len == 0 ) {
2395 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2397 * FIXME: Turn the value into a binary encoded BER?
2405 * we assume the string has enough room for the hex encoding
2409 for ( s = 0, d = 0; s < val->bv_len; ) {
2410 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2413 str[ d++ ] = val->bv_val[ s++ ];
2423 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2424 * the forst part of the AD representation of the DN is written in DNS
2425 * form, i.e. dot separated domain name components (as suggested
2426 * by Luke Howard, http://www.padl.com/~lukeh)
2429 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2432 int domain = 0, first = 1;
2433 ber_len_t l = 1; /* we move the null also */
2435 /* we are guaranteed there's enough memory in str */
2441 assert( *iRDN > 0 );
2443 for ( i = *iRDN; i >= 0; i-- ) {
2447 assert( dn[ i ][ 0 ] );
2450 assert( rdn[ 0 ][ 0 ] );
2451 ava = rdn[ 0 ][ 0 ];
2453 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2461 AC_MEMCPY( str, ava->la_value->bv_val,
2462 ava->la_value->bv_len + 1);
2463 l += ava->la_value->bv_len;
2466 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2467 AC_MEMCPY( str, ava->la_value->bv_val,
2468 ava->la_value->bv_len );
2469 str[ ava->la_value->bv_len ] = '.';
2470 l += ava->la_value->bv_len + 1;
2480 rdn2strlen( LDAPRDN *rdn, ber_len_t *len,
2481 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2488 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2489 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2491 /* len(type) + '=' + '+' | ',' */
2492 l += ava->la_attr->bv_len + 2;
2494 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2495 /* octothorpe + twice the length */
2496 l += 1 + 2 * ava->la_value->bv_len;
2501 if ( ( *s2l )( ava->la_value, ava->la_flags, &vl ) ) {
2514 rdn2str( LDAPRDN *rdn, char *str, ber_len_t *len,
2515 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2520 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2521 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2523 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2524 ava->la_attr->bv_len );
2525 l += ava->la_attr->bv_len;
2529 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2531 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2534 l += 2 * ava->la_value->bv_len;
2539 if ( ( *s2s )( ava->la_value, &str[ l ],
2540 ava->la_flags, &vl ) ) {
2545 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2554 rdn2DCEstrlen( LDAPRDN *rdn, ber_len_t *len )
2561 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2562 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2564 /* len(type) + '=' + ',' | '/' */
2565 l += ava->la_attr->bv_len + 2;
2567 switch ( ava->la_flags ) {
2568 case LDAP_AVA_BINARY:
2569 /* octothorpe + twice the length */
2570 l += 1 + 2 * ava->la_value->bv_len;
2573 case LDAP_AVA_STRING: {
2576 if ( strval2DCEstrlen( ava->la_value,
2577 ava->la_flags, &vl ) ) {
2595 rdn2DCEstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2600 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2601 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2606 str[ l++ ] = ( iAVA ? ',' : '/' );
2609 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2610 ava->la_attr->bv_len );
2611 l += ava->la_attr->bv_len;
2615 switch ( ava->la_flags ) {
2616 case LDAP_AVA_BINARY:
2618 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2621 l += 2 * ava->la_value->bv_len;
2624 case LDAP_AVA_STRING: {
2627 if ( strval2DCEstr( ava->la_value, &str[ l ],
2628 ava->la_flags, &vl ) ) {
2646 rdn2UFNstrlen( LDAPRDN *rdn, ber_len_t *len )
2656 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2657 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2660 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2662 /* FIXME: are binary values allowed in UFN? */
2663 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2664 /* octothorpe + twice the value */
2665 l += 1 + 2 * ava->la_value->bv_len;
2670 if ( strval2strlen( ava->la_value, ava->la_flags,
2684 rdn2UFNstr( LDAPRDN *rdn, char *str, ber_len_t *len )
2689 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2690 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2692 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2694 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2697 l += 2 * ava->la_value->bv_len;
2702 if ( strval2str( ava->la_value, &str[ l ],
2703 ava->la_flags, &vl ) ) {
2709 if ( rdn[ iAVA + 1 ]) {
2710 AC_MEMCPY( &str[ l ], " + ", 3 );
2714 AC_MEMCPY( &str[ l ], ", ", 2 );
2725 rdn2ADstrlen( LDAPRDN *rdn, ber_len_t *len )
2735 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2736 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2741 /* FIXME: are binary values allowed in UFN? */
2742 switch ( ava->la_flags ) {
2743 case LDAP_AVA_BINARY:
2744 /* octothorpe + twice the value */
2745 l += 1 + 2 * ava->la_value->bv_len;
2748 case LDAP_AVA_STRING: {
2751 if ( strval2ADstrlen( ava->la_value,
2752 ava->la_flags, &vl ) ) {
2770 rdn2ADstr( LDAPRDN *rdn, char *str, ber_len_t *len, int first )
2775 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2776 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2781 str[ l++ ] = ( iAVA ? ',' : '/' );
2784 switch ( ava->la_flags ) {
2785 case LDAP_AVA_BINARY:
2787 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2790 l += 2 * ava->la_value->bv_len;
2793 case LDAP_AVA_STRING: {
2796 if ( strval2ADstr( ava->la_value, &str[ l ],
2797 ava->la_flags, &vl ) ) {
2817 * Returns in str a string representation of rdn based on flags.
2818 * There is some duplication of code between this and ldap_dn2str;
2819 * this is wanted to reduce the allocation of temporary buffers.
2822 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2829 if ( rdn == NULL ) {
2830 *str = LDAP_STRDUP( "" );
2831 return( LDAP_SUCCESS );
2835 * This routine wastes "back" bytes at the end of the string
2839 switch ( LDAP_DN_FORMAT( flags ) ) {
2840 case LDAP_DN_FORMAT_LDAPV3:
2841 if ( rdn2strlen( rdn, &l, strval2strlen ) ) {
2842 return( LDAP_OTHER );
2846 case LDAP_DN_FORMAT_LDAPV2:
2847 if ( rdn2strlen( rdn, &l, strval2IA5strlen ) ) {
2848 return( LDAP_OTHER );
2852 case LDAP_DN_FORMAT_UFN:
2853 if ( rdn2UFNstrlen( rdn, &l ) ) {
2854 return( LDAP_OTHER );
2858 case LDAP_DN_FORMAT_DCE:
2859 if ( rdn2DCEstrlen( rdn, &l ) ) {
2860 return( LDAP_OTHER );
2864 case LDAP_DN_FORMAT_AD_CANONICAL:
2865 if ( rdn2ADstrlen( rdn, &l ) ) {
2866 return( LDAP_OTHER );
2871 return( LDAP_OTHER );
2874 *str = LDAP_MALLOC( l + 1 );
2876 switch ( LDAP_DN_FORMAT( flags ) ) {
2877 case LDAP_DN_FORMAT_LDAPV3:
2878 rc = rdn2str( rdn, *str, &l, strval2str );
2882 case LDAP_DN_FORMAT_LDAPV2:
2883 rc = rdn2str( rdn, *str, &l, strval2IA5str );
2887 case LDAP_DN_FORMAT_UFN:
2888 rc = rdn2UFNstr( rdn, *str, &l );
2892 case LDAP_DN_FORMAT_DCE:
2893 rc = rdn2DCEstr( rdn, *str, &l, 1 );
2897 case LDAP_DN_FORMAT_AD_CANONICAL:
2898 rc = rdn2ADstr( rdn, *str, &l, 1 );
2903 /* need at least one of the previous */
2904 return( LDAP_OTHER );
2908 ldap_memfree( *str );
2909 return( LDAP_OTHER );
2912 ( *str )[ l - back ] = '\0';
2914 return( LDAP_SUCCESS );
2918 * Very bulk implementation; many optimizations can be performed
2919 * - a NULL dn results in an empty string ""
2922 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2923 * we must encode it in binary form ('#' + HEXPAIRs)
2924 * b) does DCE/AD support UTF-8?
2925 * no clue; don't think so.
2926 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2927 * use binary encoded BER
2929 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2932 int rc = LDAP_OTHER;
2935 /* stringifying helpers for LDAPv3/LDAPv2 */
2936 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2937 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2941 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2946 * a null dn means an empty dn string
2947 * FIXME: better raise an error?
2950 *str = LDAP_STRDUP( "" );
2951 return( LDAP_SUCCESS );
2954 switch ( LDAP_DN_FORMAT( flags ) ) {
2955 case LDAP_DN_FORMAT_LDAPV3:
2956 sv2l = strval2strlen;
2960 case LDAP_DN_FORMAT_LDAPV2:
2961 sv2l = strval2IA5strlen;
2962 sv2s = strval2IA5str;
2965 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
2967 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2969 if ( rdn2strlen( rdn, &rdnl, sv2l ) ) {
2970 goto return_results;
2976 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
2977 rc = LDAP_NO_MEMORY;
2981 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
2983 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
2985 if ( rdn2str( rdn, &( *str )[ l ], &rdnl, sv2s ) ) {
2988 goto return_results;
2996 * trim the last ',' (the allocated memory
2997 * is one byte longer than required)
2999 ( *str )[ len - 1 ] = '\0';
3004 case LDAP_DN_FORMAT_UFN: {
3007 * FIXME: quoting from RFC 1781:
3009 To take a distinguished name, and generate a name of this format with
3010 attribute types omitted, the following steps are followed.
3012 1. If the first attribute is of type CommonName, the type may be
3015 2. If the last attribute is of type Country, the type may be
3018 3. If the last attribute is of type Country, the last
3019 Organisation attribute may have the type omitted.
3021 4. All attributes of type OrganisationalUnit may have the type
3022 omitted, unless they are after an Organisation attribute or
3023 the first attribute is of type OrganisationalUnit.
3025 * this should be the pedantic implementation.
3027 * Here the standard implementation reflects
3028 * the one historically provided by OpenLDAP
3029 * (and UMIch, I presume), with the variant
3030 * of spaces and plusses (' + ') separating
3033 * A non-standard but nice implementation could
3034 * be to turn the final "dc" attributes into a
3035 * dot-separated domain.
3037 * Other improvements could involve the use of
3038 * friendly country names and so.
3041 int leftmost_dc = -1;
3043 #endif /* DC_IN_UFN */
3045 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3047 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3049 if ( rdn2UFNstrlen( rdn, &rdnl ) ) {
3050 goto return_results;
3055 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3056 if ( leftmost_dc == -1 ) {
3062 #endif /* DC_IN_UFN */
3065 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3066 rc = LDAP_NO_MEMORY;
3071 if ( leftmost_dc == -1 ) {
3072 #endif /* DC_IN_UFN */
3073 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3075 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3077 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3080 goto return_results;
3086 * trim the last ', ' (the allocated memory
3087 * is two bytes longer than required)
3089 ( *str )[ len - 2 ] = '\0';
3092 last_iRDN = iRDN - 1;
3094 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3096 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3098 if ( rdn2UFNstr( rdn, &( *str )[ l ], &vl ) ) {
3101 goto return_results;
3106 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3109 goto return_results;
3112 /* the string is correctly terminated by dn2domain */
3114 #endif /* DC_IN_UFN */
3120 case LDAP_DN_FORMAT_DCE:
3122 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3124 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3126 if ( rdn2DCEstrlen( rdn, &rdnl ) ) {
3127 goto return_results;
3133 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3134 rc = LDAP_NO_MEMORY;
3138 for ( l = 0; iRDN--; ) {
3140 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3142 if ( rdn2DCEstr( rdn, &( *str )[ l ], &rdnl, 0 ) ) {
3145 goto return_results;
3152 ( *str )[ len ] = '\0';
3157 case LDAP_DN_FORMAT_AD_CANONICAL: {
3160 * Sort of UFN for DCE DNs: a slash ('/') separated
3161 * global->local DN with no types; strictly speaking,
3162 * the naming context should be a domain, which is
3163 * written in DNS-style, e.g. dot-deparated.
3167 * "cn=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3171 * "microsoft.com/People/Bill,Gates"
3173 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3175 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3177 if ( rdn2ADstrlen( rdn, &rdnl ) ) {
3178 goto return_results;
3184 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3185 rc = LDAP_NO_MEMORY;
3190 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3191 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3193 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3195 if ( rdn2ADstr( rdn, &( *str )[ l ],
3199 goto return_results;
3208 * Strictly speaking, AD canonical requires
3209 * a DN to be in the form "..., dc=smtg",
3210 * i.e. terminated by a domain component
3212 if ( flags & LDAP_DN_PEDANTIC ) {
3215 rc = LDAP_INVALID_DN_SYNTAX;
3219 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3221 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3223 if ( rdn2ADstr( rdn, &( *str )[ l ],
3227 goto return_results;
3236 ( *str )[ len ] = '\0';
3247 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );