3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
27 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 static char *dn2dn( const char *dnin, unsigned fin, unsigned fout );
32 /* from libraries/libldap/schema.c */
33 extern char * parse_numericoid(const char **sp, int *code, const int flags);
35 /* parsing/printing routines */
36 static int str2strval( const char *str, struct berval **val,
37 const char **next, unsigned flags, unsigned *retFlags );
38 static int DCE2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags );
40 static int IA52strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int quotedIA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int hexstr2binval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
68 LDAPAVA * ldapava_new( const struct berval *attr, const struct berval *val,
70 void ldapava_free( LDAPAVA *ava );
71 LDAPRDN * ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava );
72 LDAPRDN * ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where );
73 /* void ldapava_free_rdn( LDAPRDN *rdn ); in ldap.h */
74 LDAPDN * ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn );
75 LDAPDN * ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where );
76 /* void ldapava_free_dn( LDAPDN *dn ); in ldap.h */
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 );
90 #ifndef USE_LDAP_DN_PARSING /* deprecated */
91 #define NAME_TYPE_LDAP_RDN 0
92 #define NAME_TYPE_LDAP_DN 1
93 #define NAME_TYPE_DCE_DN 2
95 static char **explode_name( const char *name, int notypes, int is_type );
96 #endif /* !USE_LDAP_DN_PARSING */
99 * RFC 1823 ldap_get_dn
102 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
107 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
109 if ( entry == NULL ) {
110 ld->ld_errno = LDAP_PARAM_ERROR;
114 tmp = *entry->lm_ber; /* struct copy */
115 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
116 ld->ld_errno = LDAP_DECODING_ERROR;
124 * RFC 1823 ldap_dn2ufn
127 ldap_dn2ufn( LDAP_CONST char *dn )
129 #ifndef USE_LDAP_DN_PARSING /* deprecated */
134 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
136 /* produces completely untyped UFNs */
142 vals = ldap_explode_dn( dn , 0 );
147 for ( i = 0; vals[i]; i++ ) {
150 rvals = ldap_explode_rdn( vals[i] , 1 );
151 if ( rvals == NULL ) {
156 LDAP_FREE( vals[i] );
157 vals[i] = ldap_charray2str( rvals, " + " );
161 ufn = ldap_charray2str( vals, ", " );
165 #else /* USE_LDAP_DN_PARSING */
166 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
168 return dn2dn( dn, LDAP_DN_FORMAT_LDAP, LDAP_DN_FORMAT_UFN );
169 #endif /* USE_LDAP_DN_PARSING */
173 * RFC 1823 ldap_explode_dn
176 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
178 #ifndef USE_LDAP_DN_PARSING /* deprecated */
179 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
181 return explode_name( dn, notypes, NAME_TYPE_LDAP_DN );
182 #else /* USE_LDAP_DN_PARSING */
184 char **values = NULL;
186 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
188 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
190 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
195 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
196 char *str, **v = NULL;
198 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
200 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
202 LBER_VFREE( values );
203 ldapava_free_dn( tmpDN );
207 values[ iRDN ] = str;
209 values[ iRDN ] = NULL;
212 #endif /* USE_LDAP_DN_PARSING */
216 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
218 #ifndef USE_LDAP_DN_PARSING /* deprecated */
219 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
221 return explode_name( rdn, notypes, NAME_TYPE_LDAP_RDN );
222 #else /* USE_LDAP_DN_PARSING */
224 char **values = NULL;
226 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
228 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
231 * we assume this dn is made of one rdn only
233 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAP )
238 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
239 ber_len_t l = 0, vl, al = 0;
240 char *str, **v = NULL;
241 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
243 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
249 if ( ava->la_flags == LDAP_AVA_BINARY ) {
250 vl = 1 + 2 * ava->la_value->bv_len;
253 if ( strval2strlen( ava->la_value,
254 ava->la_flags, &vl ) ) {
260 al = ava->la_attr->bv_len;
261 l = vl + ava->la_attr->bv_len + 1;
263 str = LDAP_MALLOC( l + 1 );
264 AC_MEMCPY( str, ava->la_attr->bv_val,
265 ava->la_attr->bv_len );
270 str = LDAP_MALLOC( l + 1 );
273 if ( ava->la_flags == LDAP_AVA_BINARY ) {
275 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
280 if ( strval2str( ava->la_value, &str[ al ],
281 ava->la_flags, &vl ) ) {
287 values[ iAVA ] = str;
289 values[ iAVA ] = NULL;
291 ldapava_free_dn( tmpDN );
296 LBER_VFREE( values );
297 ldapava_free_dn( tmpDN );
299 #endif /* USE_LDAP_DN_PARSING */
303 ldap_dn2dcedn( LDAP_CONST char *dn )
305 #ifndef USE_LDAP_DN_PARSING /* deprecated */
306 char *dce, *q, **rdns, **p;
309 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
311 rdns = explode_name( dn, 0, NAME_TYPE_LDAP_DN );
312 if ( rdns == NULL ) {
316 for ( p = rdns; *p != NULL; p++ ) {
317 len += strlen( *p ) + 1;
320 q = dce = LDAP_MALLOC( len + 1 );
325 p--; /* get back past NULL */
327 for ( ; p != rdns; p-- ) {
339 #else /* USE_LDAP_DN_PARSING */
340 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
342 return dn2dn( dn, LDAP_DN_FORMAT_LDAP, LDAP_DN_FORMAT_DCE );
343 #endif /* USE_LDAP_DN_PARSING */
347 ldap_dcedn2dn( LDAP_CONST char *dce )
349 #ifndef USE_LDAP_DN_PARSING
350 char *dn, *q, **rdns, **p;
353 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
355 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
356 if ( rdns == NULL ) {
362 for ( p = rdns; *p != NULL; p++ ) {
363 len += strlen( *p ) + 1;
366 q = dn = LDAP_MALLOC( len );
373 for ( ; p != rdns; p-- ) {
381 /* the name was fully qualified, thus the most-significant
382 * RDN was empty. trash the last comma */
386 /* the name was relative. copy the most significant RDN */
391 #else /* USE_LDAP_DN_PARSING */
392 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
394 return dn2dn( dce, LDAP_DN_FORMAT_DCE, LDAP_DN_FORMAT_LDAPV3 );
395 #endif /* USE_LDAP_DN_PARSING */
399 ldap_dn2ad_canonical( LDAP_CONST char *dn )
401 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
403 return dn2dn( dn, LDAP_DN_FORMAT_LDAP, LDAP_DN_FORMAT_AD_CANONICAL );
406 #ifndef USE_LDAP_DN_PARSING /* deprecated */
411 explode_name( const char *name, int notypes, int is_type )
413 const char *p, *q, *rdn;
415 int offset, state, have_equals, count = 0, endquote, len;
418 if(name == NULL) name = "";
420 /* skip leading whitespace */
421 while( ldap_utf8_isspace( name )) {
422 LDAP_UTF8_INCR( name );
437 if ( p[1] != '\0' ) {
438 offset = LDAP_UTF8_OFFSET(++p);
442 if ( state == INQUOTE )
448 if( state == OUTQUOTE ) have_equals++;
451 if (is_type == NAME_TYPE_LDAP_RDN)
455 if (is_type == NAME_TYPE_DCE_DN)
460 if (is_type == NAME_TYPE_LDAP_DN)
465 if ( state == OUTQUOTE ) {
469 if ( parts == NULL ) {
470 if (( parts = (char **)LDAP_MALLOC( 8
471 * sizeof( char *))) == NULL )
473 } else if ( count >= 8 ) {
474 if (( parts = (char **)LDAP_REALLOC( parts,
475 (count+1) * sizeof( char *)))
480 parts[ count ] = NULL;
484 for ( q = rdn; q < p && *q != '='; ++q ) {
496 if ( p[-1] == '"' ) {
504 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
507 AC_MEMCPY( parts[ count-1 ], rdn, len );
510 /* skip trailing spaces */
511 while( len > 0 && ldap_utf8_isspace(
512 &parts[count-1][len-1] ) )
518 parts[ count-1 ][ len ] = '\0';
522 * Don't forget to increment 'p' back to where
523 * it should be. If we don't, then we will
524 * never get past an "end quote."
529 rdn = *p ? &p[1] : p;
530 while ( ldap_utf8_isspace( rdn ) )
538 #endif /* !USE_LDAP_DN_PARSING */
541 * helper that changes the string representation of dnin
542 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
545 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
546 * plus some rfc 1779)
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 except
552 * LDAP_DN_FORMAT_LDAP
554 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
555 * LDAP_DN_FORMAT_AD_CANONICAL (?)
558 dn2dn( const char *dnin, unsigned fin, unsigned fout )
561 LDAPDN *tmpDN = NULL;
567 if ( ldap_str2dn( dnin , &tmpDN, fin ) != LDAP_SUCCESS ) {
571 /* don't care about the result ... */
572 ldap_dn2str( tmpDN, &dnout, fout );
574 ldapava_free_dn( tmpDN );
582 /* #define B4ATTRTYPE 0x0001 */
583 #define B4OIDATTRTYPE 0x0002
584 #define B4STRINGATTRTYPE 0x0003
586 #define B4AVAEQUALS 0x0100
587 #define B4AVASEP 0x0200
588 #define B4RDNSEP 0x0300
589 #define GOTAVA 0x0400
591 #define B4ATTRVALUE 0x0010
592 #define B4STRINGVALUE 0x0020
593 #define B4IA5VALUEQUOTED 0x0030
594 #define B4IA5VALUE 0x0040
595 #define B4BINARYVALUE 0x0050
597 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
598 #define LDAP_DN_ASCII_SPACE(c) \
599 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
600 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
601 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
602 #define LDAP_DN_ASCII_ALPHA(c) \
603 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
604 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
605 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
606 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
607 #define LDAP_DN_ASCII_HEXDIGIT(c) \
608 ( LDAP_DN_ASCII_DIGIT(c) \
609 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
610 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
611 #define LDAP_DN_ASCII_ALNUM(c) \
612 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
613 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
616 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
617 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
618 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
619 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
620 #define LDAP_DN_ATTRDESC_CHAR(c) \
621 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
623 /* special symbols */
624 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
625 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
626 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
627 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
628 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
629 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
630 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
631 #define LDAP_DN_VALUE_END(c) \
632 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
633 #define LDAP_DN_NE(c) \
634 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
635 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
636 #define LDAP_DN_NEEDESCAPE(c) \
637 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
638 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
639 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
640 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
641 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
642 #define LDAP_DN_WILLESCAPE_CHAR( c) \
643 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
644 #define LDAP_DN_WILLESCAPE(f, c) \
645 ( ( !( (f) & LDAP_DN_PRETTY ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
648 #define LDAP_DN_VALUE_END_V2(c) \
649 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
651 #define LDAP_DN_V2_SPECIAL(c) \
652 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
653 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
654 || LDAP_DN_OCTOTHORPE(c) )
655 #define LDAP_DN_V2_PAIR(c) \
656 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
659 * DCE (mostly from Luke Howard and IBM implementation for AIX)
661 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
662 * Here escapes and valid chars for GDS are considered; as soon as more
663 * specific info is found, the macros will be updated.
665 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
666 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
668 * Metachars: '/', ',', '=', '\'.
670 * the '\' is used to escape other metachars.
676 * Attribute types must start with alphabetic chars and can contain
677 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
679 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
680 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
681 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
682 #define LDAP_DN_VALUE_END_DCE(c) \
683 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
684 #define LDAP_DN_NEEDESCAPE_DCE(c) \
685 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
688 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
689 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
690 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
691 #define LDAP_DN_VALUE_END_AD(c) \
692 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
693 #define LDAP_DN_NEEDESCAPE_AD(c) \
694 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
697 #define LDAP_DN_HEXPAIR(s) \
698 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
699 #define LDAP_DC_ATTR "dc"
700 /* better look at the AttributeDescription? */
702 /* FIXME: no composite rdn or non-"dc" types, right?
703 * (what about "dc" in OID form?) */
704 /* FIXME: we do not allow binary values in domain, right? */
705 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
706 #define LDAP_DN_IS_RDN_DC( rdn ) \
707 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
708 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
709 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
711 /* Composite rules */
712 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
713 ( LDAP_DN_LDAPV2(f) \
714 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
715 #define LDAP_DN_ALLOW_SPACES(f) \
716 ( LDAP_DN_LDAPV2(f) \
717 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
718 #define LDAP_DN_LDAP(f) \
719 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
720 #define LDAP_DN_LDAPV3(f) \
721 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
722 #define LDAP_DN_LDAPV2(f) \
723 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
724 #define LDAP_DN_DCE(f) \
725 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
726 #define LDAP_DN_UFN(f) \
727 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
728 #define LDAP_DN_ADC(f) \
729 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
730 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
733 * LDAPAVA helpers (will become part of the API for operations
734 * on structural representations of DNs).
737 ldapava_new( const struct berval *attr, const struct berval *val,
745 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
747 /* should we test it? */
752 ava->la_attr = ( struct berval * )attr;
753 ava->la_value = ( struct berval * )val;
754 ava->la_flags = flags;
756 ava->la_private = NULL;
762 ldapava_free( LDAPAVA *ava )
766 ber_bvfree( ava->la_attr );
767 ber_bvfree( ava->la_value );
773 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
781 for ( i = 0U; rdn[ i ]; i++ ) {
785 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
786 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
787 newRDN[ i ][ 0 ] = ava;
788 newRDN[ i + 1 ] = NULL;
794 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
802 for ( i = 0U; rdn[ i ]; i++ ) {
808 /* assume "at end", which corresponds to
809 * ldapava_append_to_rdn */
812 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
814 /* data after insert point */
815 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
816 ( i - where ) * sizeof( LDAPRDN * ) );
818 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
819 newRDN[ where ][ 0 ] = ava;
820 newRDN[ i + 1 ] = NULL;
826 ldapava_free_rdn( LDAPRDN *rdn )
834 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
835 assert( rdn[ iAVA ][ 0 ] );
837 ldapava_free( rdn[ iAVA ][ 0 ] );
844 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
852 for ( i = 0U; dn[ i ]; i++ ) {
856 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
857 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
858 newDN[ i ][ 0 ] = rdn;
859 newDN[ i + 1 ] = NULL;
865 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
873 for ( i = 0U; dn[ i ]; i++ ) {
879 /* assume "at end", which corresponds to
880 * ldapava_append_to_dn */
883 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
885 /* data after insert point */
886 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
887 ( i - where ) * sizeof( LDAPDN * ) );
889 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
890 newDN[ where ][ 0 ] = rdn;
891 newDN[ i + 1 ] = NULL;
897 ldapava_free_dn( LDAPDN *dn )
905 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
906 assert( dn[ iRDN ][ 0 ] );
908 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
915 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
916 * into a structural representation of the DN, by separating attribute
917 * types and values encoded in the more appropriate form, which is
918 * string or OID for attribute types and binary form of the BER encoded
919 * value or Unicode string. Formats different from LDAPv3 are parsed
920 * according to their own rules and turned into the more appropriate
921 * form according to LDAPv3.
923 * NOTE: I realize the code is getting spaghettish; it is rather
924 * experimental and will hopefully turn into something more simple
925 * and readable as soon as it works as expected.
929 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
932 int rc = LDAP_INVALID_DN_SYNTAX;
934 LDAPDN *newDN = NULL;
935 LDAPRDN *newRDN = NULL;
940 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
944 switch ( LDAP_DN_FORMAT( flags ) ) {
945 case LDAP_DN_FORMAT_LDAP:
946 case LDAP_DN_FORMAT_LDAPV3:
947 case LDAP_DN_FORMAT_LDAPV2:
948 case LDAP_DN_FORMAT_DCE:
951 /* unsupported in str2dn */
952 case LDAP_DN_FORMAT_UFN:
953 case LDAP_DN_FORMAT_AD_CANONICAL:
954 return( LDAP_INVALID_DN_SYNTAX );
957 return( LDAP_OTHER );
960 if ( str[ 0 ] == '\0' ) {
961 return( LDAP_SUCCESS );
965 if ( LDAP_DN_DCE( flags ) ) {
968 * (from Luke Howard: thnx) A RDN separator is required
969 * at the beginning of an (absolute) DN.
971 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
976 } else if ( LDAP_DN_LDAP( flags ) ) {
978 * if dn starts with '/' let's make it a DCE dn
980 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
981 flags |= LDAP_DN_FORMAT_DCE;
986 for ( ; p[ 0 ]; p++ ) {
989 rc = ldap_str2rdn( p, &newRDN, &p, flags );
990 if ( rc != LDAP_SUCCESS ) {
995 * We expect a rdn separator
998 switch ( LDAP_DN_FORMAT( flags ) ) {
999 case LDAP_DN_FORMAT_LDAPV3:
1000 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
1006 case LDAP_DN_FORMAT_LDAP:
1007 case LDAP_DN_FORMAT_LDAPV2:
1008 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
1014 case LDAP_DN_FORMAT_DCE:
1015 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1024 if ( LDAP_DN_DCE( flags ) ) {
1025 /* add in reversed order */
1026 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1028 dn = ldapava_append_to_dn( newDN, newRDN );
1032 rc = LDAP_NO_MEMORY;
1039 if ( p[ 0 ] == '\0' ) {
1042 * the DN is over, phew
1051 ldapava_free_rdn( newRDN );
1055 ldapava_free_dn( newDN );
1061 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1070 * Parses a relative DN according to flags up to a rdn separator
1071 * or to the end of str.
1072 * Returns the rdn and a pointer to the string continuation, which
1073 * corresponds to the rdn separator or to '\0' in case the string is over.
1076 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1080 int rc = LDAP_INVALID_DN_SYNTAX;
1081 int attrTypeEncoding = LDAP_AVA_STRING,
1082 attrValueEncoding = LDAP_AVA_STRING;
1084 struct berval *attrType = NULL;
1085 struct berval *attrValue = NULL;
1087 LDAPRDN *newRDN = NULL;
1093 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1098 switch ( LDAP_DN_FORMAT( flags ) ) {
1099 case LDAP_DN_FORMAT_LDAP:
1100 case LDAP_DN_FORMAT_LDAPV3:
1101 case LDAP_DN_FORMAT_LDAPV2:
1102 case LDAP_DN_FORMAT_DCE:
1105 /* unsupported in str2dn */
1106 case LDAP_DN_FORMAT_UFN:
1107 case LDAP_DN_FORMAT_AD_CANONICAL:
1108 return( LDAP_INVALID_DN_SYNTAX );
1111 return( LDAP_OTHER );
1114 if ( str[ 0 ] == '\0' ) {
1115 return( LDAP_SUCCESS );
1119 for ( ; p[ 0 ] || state == GOTAVA; ) {
1122 * The parser in principle advances one token a time,
1123 * or toggles state if preferable.
1128 * an AttributeType can be encoded as:
1129 * - its string representation; in detail, implementations
1130 * MUST recognize AttributeType string type names listed
1131 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1132 * MAY recognize other names.
1133 * - its numeric OID (a dotted decimal string); in detail
1134 * RFC 2253 asserts that ``Implementations MUST allow
1135 * an oid in the attribute type to be prefixed by one
1136 * of the character strings "oid." or "OID."''. As soon
1137 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1138 * I'm not sure whether this is required or not any
1139 * longer; to be liberal, we still implement it.
1142 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1143 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1150 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1151 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1156 /* whitespace is allowed (and trimmed) */
1158 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1163 /* error: we expected an AVA */
1169 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1170 state = B4OIDATTRTYPE;
1174 /* else must be alpha */
1175 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1179 /* LDAPv2 "oid." prefix */
1180 if ( LDAP_DN_LDAPV2( flags ) ) {
1182 * to be overly pedantic, we only accept
1185 if ( flags & LDAP_DN_PEDANTIC ) {
1186 if ( !strncmp( p, "OID.", 4 )
1187 || !strncmp( p, "oid.", 4 ) ) {
1189 state = B4OIDATTRTYPE;
1193 if ( !strncasecmp( p, "oid.", 4 ) ) {
1195 state = B4OIDATTRTYPE;
1201 state = B4STRINGATTRTYPE;
1204 case B4OIDATTRTYPE: {
1205 int err = LDAP_SUCCESS;
1208 type = parse_numericoid( &p, &err, 0 );
1209 if ( type == NULL ) {
1212 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1213 if ( attrType== NULL ) {
1214 rc = LDAP_NO_MEMORY;
1217 attrType->bv_val = type;
1218 attrType->bv_len = strlen( type );
1219 attrTypeEncoding = LDAP_AVA_BINARY;
1221 state = B4AVAEQUALS;
1225 case B4STRINGATTRTYPE: {
1226 const char *startPos, *endPos = NULL;
1230 * the starting char has been found to be
1231 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1232 * FIXME: DCE attr types seem to have a more
1233 * restrictive syntax (no '-' ...)
1235 for ( startPos = p++; p[ 0 ]; p++ ) {
1236 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1240 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1243 * RFC 2253 does not explicitly
1244 * allow lang extensions to attribute
1247 if ( flags & LDAP_DN_PEDANTIC ) {
1252 * we trim ';' and following lang
1253 * and so from attribute types
1256 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1257 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1265 len = ( endPos ? endPos : p ) - startPos;
1270 assert( attrType == NULL );
1271 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1272 if ( attrType == NULL ) {
1273 rc = LDAP_NO_MEMORY;
1276 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1277 if ( attrType->bv_val == NULL ) {
1278 rc = LDAP_NO_MEMORY;
1281 attrType->bv_len = len;
1282 attrTypeEncoding = LDAP_AVA_STRING;
1285 * here we need to decide whether to use it as is
1286 * or turn it in OID form; as a consequence, we
1287 * need to decide whether to binary encode the value
1290 state = B4AVAEQUALS;
1295 /* spaces may not be allowed */
1296 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1297 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1302 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1307 /* need equal sign */
1308 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1313 /* spaces may not be allowed */
1314 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1315 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1320 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1326 * octothorpe means a BER encoded value will follow
1327 * FIXME: I don't think DCE will allow it
1329 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1331 attrValueEncoding = LDAP_AVA_BINARY;
1332 state = B4BINARYVALUE;
1336 /* STRING value expected */
1339 * if we're pedantic, an attribute type in OID form
1340 * SHOULD imply a BER encoded attribute value; we
1341 * should at least issue a warning
1343 if ( ( flags & LDAP_DN_PEDANTIC )
1344 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1345 /* OID attrType SHOULD use binary encoding */
1349 attrValueEncoding = LDAP_AVA_STRING;
1352 * LDAPv2 allows the attribute value to be quoted;
1353 * also, IA5 values are expected, in principle
1355 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1356 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1358 state = B4IA5VALUEQUOTED;
1362 if ( LDAP_DN_LDAPV2( flags ) ) {
1369 * here STRING means RFC 2253 string
1370 * FIXME: what about DCE strings?
1372 state = B4STRINGVALUE;
1376 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1384 switch ( LDAP_DN_FORMAT( flags ) ) {
1385 case LDAP_DN_FORMAT_LDAP:
1386 case LDAP_DN_FORMAT_LDAPV3:
1387 if ( str2strval( p, &attrValue, &p, flags,
1388 &attrValueEncoding ) ) {
1393 case LDAP_DN_FORMAT_DCE:
1394 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1407 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1414 case B4IA5VALUEQUOTED:
1416 /* lead quote already stripped */
1417 if ( quotedIA52strval( p, &attrValue,
1431 * we accept empty values
1433 ava = ldapava_new( attrType, attrValue,
1434 attrValueEncoding );
1435 if ( ava == NULL ) {
1436 rc = LDAP_NO_MEMORY;
1440 rdn = ldapava_append_to_rdn( newRDN, ava );
1441 if ( rdn == NULL ) {
1442 rc = LDAP_NO_MEMORY;
1448 * if we got an AVA separator ('+', or ',' for DCE )
1449 * we expect a new AVA for this RDN; otherwise
1450 * we add the RDN to the DN
1452 switch ( LDAP_DN_FORMAT( flags ) ) {
1453 case LDAP_DN_FORMAT_LDAP:
1454 case LDAP_DN_FORMAT_LDAPV3:
1455 case LDAP_DN_FORMAT_LDAPV2:
1456 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1461 case LDAP_DN_FORMAT_DCE:
1462 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1470 * the RDN is over, phew
1477 /* they should have been used in an AVA */
1493 /* They are set to NULL after they're used in an AVA */
1495 ber_bvfree( attrType );
1499 ber_bvfree( attrValue );
1503 ldapava_free_rdn( newRDN );
1509 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1517 * reads in a UTF-8 string value, unescaping stuff:
1518 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1519 * '\' + HEXPAIR(p) -> unhex(p)
1522 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1524 const char *p, *startPos, *endPos = NULL;
1525 ber_len_t len, escapes, unescapes;
1534 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1535 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1537 if ( p[ 0 ] == '\0' ) {
1540 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1541 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1542 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1547 if ( LDAP_DN_HEXPAIR( p ) ) {
1550 hexstr2bin( p, &c );
1553 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1556 * we assume the string is UTF-8
1558 *retFlags = LDAP_AVA_NONPRINTABLE;
1565 if ( LDAP_DN_PEDANTIC & flags ) {
1569 * FIXME: we allow escaping
1570 * of chars that don't need
1571 * to and do not belong to
1572 * HEXDIGITS (we also allow
1573 * single hexdigit; maybe we
1578 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1579 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1582 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1584 * FIXME: maybe we can add
1585 * escapes if not pedantic?
1592 * we do allow unescaped spaces at the end
1593 * of the value only in non-pedantic mode
1595 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1596 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1597 if ( flags & LDAP_DN_PEDANTIC ) {
1601 /* strip trailing (unescaped) spaces */
1602 for ( endPos = p - 1;
1603 endPos > startPos + 1 &&
1604 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1605 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1612 * FIXME: test memory?
1614 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1615 *val = LDAP_MALLOC( sizeof( struct berval ) );
1616 ( *val )->bv_len = len;
1618 if ( escapes == 0 && unescapes == 0 ) {
1619 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1624 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1625 for ( s = 0, d = 0; d < len; ) {
1626 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1628 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1629 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1630 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1631 ( *val )->bv_val[ d++ ] =
1634 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1637 hexstr2bin( &startPos[ s ], &c );
1638 ( *val )->bv_val[ d++ ] = c;
1643 * we allow escaping of chars
1644 * that do not need to
1646 ( *val )->bv_val[ d++ ] =
1651 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1655 ( *val )->bv_val[ d ] = '\0';
1656 assert( strlen( ( *val )->bv_val ) == len );
1666 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1668 const char *p, *startPos, *endPos = NULL;
1669 ber_len_t len, escapes;
1678 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1679 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1681 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1688 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1693 * FIXME: can we accept anything else? I guess we need
1694 * to stop if a value is not legal
1699 * (unescaped) trailing spaces are trimmed must be silently ignored;
1702 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1703 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1704 if ( flags & LDAP_DN_PEDANTIC ) {
1708 /* strip trailing (unescaped) spaces */
1709 for ( endPos = p - 1;
1710 endPos > startPos + 1 &&
1711 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1712 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1719 len = ( endPos ? endPos : p ) - startPos - escapes;
1720 *val = LDAP_MALLOC( sizeof( struct berval ) );
1721 ( *val )->bv_len = len;
1722 if ( escapes == 0 ){
1723 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1728 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1729 for ( s = 0, d = 0; d < len; ) {
1731 * This point is reached only if escapes
1732 * are properly used, so all we need to
1735 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1739 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1741 ( *val )->bv_val[ d ] = '\0';
1742 assert( strlen( ( *val )->bv_val ) == len );
1751 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1753 const char *p, *startPos, *endPos = NULL;
1754 ber_len_t len, escapes;
1767 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1768 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1770 if ( p[ 0 ] == '\0' ) {
1774 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1775 && ( LDAP_DN_PEDANTIC & flags ) ) {
1780 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1785 * FIXME: can we accept anything else? I guess we need
1786 * to stop if a value is not legal
1790 /* strip trailing (unescaped) spaces */
1792 endPos > startPos + 1 &&
1793 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1794 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1799 *val = LDAP_MALLOC( sizeof( struct berval ) );
1800 len = ( endPos ? endPos : p ) - startPos - escapes;
1801 ( *val )->bv_len = len;
1802 if ( escapes == 0 ) {
1803 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1808 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1809 for ( s = 0, d = 0; d < len; ) {
1810 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1813 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1815 ( *val )->bv_val[ d ] = '\0';
1816 assert( strlen( ( *val )->bv_val ) == len );
1824 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1826 const char *p, *startPos, *endPos = NULL;
1828 unsigned escapes = 0;
1837 /* initial quote already eaten */
1838 for ( startPos = p = str; p[ 0 ]; p++ ) {
1840 * According to RFC 1779, the quoted value can
1841 * contain escaped as well as unescaped special values;
1842 * as a consequence we tolerate escaped values
1843 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1844 * (e.g. '","' -> '\,').
1846 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1847 if ( p[ 1 ] == '\0' ) {
1852 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1853 && ( LDAP_DN_PEDANTIC & flags ) ) {
1855 * do we allow to escape normal chars?
1856 * LDAPv2 does not allow any mechanism
1857 * for escaping chars with '\' and hex
1864 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1866 /* eat closing quotes */
1872 * FIXME: can we accept anything else? I guess we need
1873 * to stop if a value is not legal
1877 if ( endPos == NULL ) {
1881 /* Strip trailing (unescaped) spaces */
1882 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1886 len = endPos - startPos - escapes;
1888 *val = LDAP_MALLOC( sizeof( struct berval ) );
1889 ( *val )->bv_len = len;
1890 if ( escapes == 0 ) {
1891 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1896 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1897 ( *val )->bv_len = len;
1899 for ( s = d = 0; d < len; ) {
1900 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1903 ( *val )->bv_val[ d++ ] = str[ s++ ];
1905 ( *val )->bv_val[ d ] = '\0';
1906 assert( strlen( ( *val )->bv_val ) == len );
1915 hexstr2bin( const char *str, char *c )
1925 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1931 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1938 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1944 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1945 *c += c2 - 'a' + 10;
1953 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1955 const char *p, *startPos, *endPos = NULL;
1966 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1967 switch ( LDAP_DN_FORMAT( flags ) ) {
1968 case LDAP_DN_FORMAT_LDAPV3:
1969 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1974 case LDAP_DN_FORMAT_LDAP:
1975 case LDAP_DN_FORMAT_LDAPV2:
1976 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1981 case LDAP_DN_FORMAT_DCE:
1982 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1988 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1989 if ( flags & LDAP_DN_PEDANTIC ) {
1994 for ( ; p[ 0 ]; p++ ) {
1995 switch ( LDAP_DN_FORMAT( flags ) ) {
1996 case LDAP_DN_FORMAT_LDAPV3:
1997 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2002 case LDAP_DN_FORMAT_LDAP:
2003 case LDAP_DN_FORMAT_LDAPV2:
2004 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2009 case LDAP_DN_FORMAT_DCE:
2010 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2019 if ( !LDAP_DN_HEXPAIR( p ) ) {
2026 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2028 assert( 2 * len == ( endPos ? endPos : p ) - startPos );
2030 *val = LDAP_MALLOC( sizeof( struct berval ) );
2031 if ( *val == NULL ) {
2032 return( LDAP_NO_MEMORY );
2035 ( *val )->bv_len = len;
2036 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
2037 if ( ( *val )->bv_val == NULL ) {
2039 return( LDAP_NO_MEMORY );
2042 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2045 hexstr2bin( &startPos[ s ], &c );
2047 ( *val )->bv_val[ d ] = c;
2050 ( *val )->bv_val[ d ] = '\0';
2057 * convert a byte in a hexadecimal pair
2060 byte2hexpair( const char *val, char *pair )
2062 static const char hexdig[] = "0123456789abcdef";
2068 * we assume the string has enough room for the hex encoding
2072 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2073 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2079 * convert a binary value in hexadecimal pairs
2082 binval2hexstr( struct berval *val, char *str )
2089 if ( val->bv_len == 0 ) {
2094 * we assume the string has enough room for the hex encoding
2098 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2099 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2106 * Length of the string representation, accounting for escaped hex
2110 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2112 ber_len_t l, cl = 1;
2119 if ( val->bv_len == 0 ) {
2123 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2124 cl = ldap_utf8_charlen( p );
2126 /* illegal utf-8 char! */
2129 } else if ( cl > 1 ) {
2132 for ( cnt = 1; cnt < cl; cnt++ ) {
2133 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2137 /* need to escape it */
2141 * there might be some chars we want to escape in form
2142 * of a couple of hexdigits for optimization purposes
2144 } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
2147 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2148 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2149 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2163 * convert to string representation, escaping with hex the UTF-8 stuff;
2164 * assume the destination has enough room for escaping
2167 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2169 ber_len_t s, d, end;
2175 if ( val->bv_len == 0 ) {
2181 * we assume the string has enough room for the hex encoding
2184 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2185 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2188 * there might be some chars we want to escape in form
2189 * of a couple of hexdigits for optimization purposes
2191 if ( cl > 1 || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
2194 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2200 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2201 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2202 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2205 str[ d++ ] = val->bv_val[ s++ ];
2215 * Length of the IA5 string representation (no UTF-8 allowed)
2218 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2227 if ( val->bv_len == 0 ) {
2231 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2233 * Turn value into a binary encoded BER
2238 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2239 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2240 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2241 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2256 * convert to string representation (np UTF-8)
2257 * assume the destination has enough room for escaping
2260 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2262 ber_len_t s, d, end;
2268 if ( val->bv_len == 0 ) {
2273 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2275 * Turn value into a binary encoded BER
2282 * we assume the string has enough room for the hex encoding
2286 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2287 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2288 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2289 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2292 str[ d++ ] = val->bv_val[ s++ ];
2302 * Length of the (supposedly) DCE string representation,
2303 * accounting for escaped hex of UTF-8 chars
2306 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2315 if ( val->bv_len == 0 ) {
2319 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2321 * FIXME: Turn the value into a binary encoded BER?
2326 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2327 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2342 * convert to (supposedly) DCE string representation,
2343 * escaping with hex the UTF-8 stuff;
2344 * assume the destination has enough room for escaping
2347 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2355 if ( val->bv_len == 0 ) {
2360 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2362 * FIXME: Turn the value into a binary encoded BER?
2370 * we assume the string has enough room for the hex encoding
2374 for ( s = 0, d = 0; s < val->bv_len; ) {
2375 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2378 str[ d++ ] = val->bv_val[ s++ ];
2388 * Length of the (supposedly) AD canonical string representation,
2389 * accounting for escaped hex of UTF-8 chars
2392 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2401 if ( val->bv_len == 0 ) {
2405 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2407 * FIXME: Turn the value into a binary encoded BER?
2412 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2413 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2428 * convert to (supposedly) AD string representation,
2429 * escaping with hex the UTF-8 stuff;
2430 * assume the destination has enough room for escaping
2433 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2441 if ( val->bv_len == 0 ) {
2446 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2448 * FIXME: Turn the value into a binary encoded BER?
2456 * we assume the string has enough room for the hex encoding
2460 for ( s = 0, d = 0; s < val->bv_len; ) {
2461 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2464 str[ d++ ] = val->bv_val[ s++ ];
2474 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2475 * the forst part of the AD representation of the DN is written in DNS
2476 * form, i.e. dot separated domain name components (as suggested
2477 * by Luke Howard, http://www.padl.com/~lukeh)
2480 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2483 int domain = 0, first = 1;
2484 ber_len_t l = 1; /* we move the null also */
2486 /* we are guaranteed there's enough memory in str */
2492 assert( *iRDN > 0 );
2494 for ( i = *iRDN; i >= 0; i-- ) {
2498 assert( dn[ i ][ 0 ] );
2501 assert( rdn[ 0 ][ 0 ] );
2502 ava = rdn[ 0 ][ 0 ];
2504 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2512 AC_MEMCPY( str, ava->la_value->bv_val,
2513 ava->la_value->bv_len + 1);
2514 l += ava->la_value->bv_len;
2517 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2518 AC_MEMCPY( str, ava->la_value->bv_val,
2519 ava->la_value->bv_len );
2520 str[ ava->la_value->bv_len ] = '.';
2521 l += ava->la_value->bv_len + 1;
2531 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2532 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2539 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2540 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2542 /* len(type) + '=' + '+' | ',' */
2543 l += ava->la_attr->bv_len + 2;
2545 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2546 /* octothorpe + twice the length */
2547 l += 1 + 2 * ava->la_value->bv_len;
2551 unsigned f = flags | ava->la_flags;
2553 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2566 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2567 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2572 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2573 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2575 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2576 ava->la_attr->bv_len );
2577 l += ava->la_attr->bv_len;
2581 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2583 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2586 l += 2 * ava->la_value->bv_len;
2590 unsigned f = flags | ava->la_flags;
2592 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2597 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2606 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2613 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2614 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2616 /* len(type) + '=' + ',' | '/' */
2617 l += ava->la_attr->bv_len + 2;
2619 switch ( ava->la_flags ) {
2620 case LDAP_AVA_BINARY:
2621 /* octothorpe + twice the length */
2622 l += 1 + 2 * ava->la_value->bv_len;
2625 case LDAP_AVA_STRING: {
2627 unsigned f = flags | ava->la_flags;
2629 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2647 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2652 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2653 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2658 str[ l++ ] = ( iAVA ? ',' : '/' );
2661 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2662 ava->la_attr->bv_len );
2663 l += ava->la_attr->bv_len;
2667 switch ( ava->la_flags ) {
2668 case LDAP_AVA_BINARY:
2670 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2673 l += 2 * ava->la_value->bv_len;
2676 case LDAP_AVA_STRING: {
2678 unsigned f = flags | ava->la_flags;
2680 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2698 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2708 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2709 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2712 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2714 /* FIXME: are binary values allowed in UFN? */
2715 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2716 /* octothorpe + twice the value */
2717 l += 1 + 2 * ava->la_value->bv_len;
2721 unsigned f = flags | ava->la_flags;
2723 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2736 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2741 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2742 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2744 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2746 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2749 l += 2 * ava->la_value->bv_len;
2753 unsigned f = flags | ava->la_flags;
2755 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2761 if ( rdn[ iAVA + 1 ]) {
2762 AC_MEMCPY( &str[ l ], " + ", 3 );
2766 AC_MEMCPY( &str[ l ], ", ", 2 );
2777 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2787 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2788 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2793 /* FIXME: are binary values allowed in UFN? */
2794 switch ( ava->la_flags ) {
2795 case LDAP_AVA_BINARY:
2796 /* octothorpe + twice the value */
2797 l += 1 + 2 * ava->la_value->bv_len;
2800 case LDAP_AVA_STRING: {
2802 unsigned f = flags | ava->la_flags;
2804 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2822 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2827 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2828 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2833 str[ l++ ] = ( iAVA ? ',' : '/' );
2836 switch ( ava->la_flags ) {
2837 case LDAP_AVA_BINARY:
2839 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2842 l += 2 * ava->la_value->bv_len;
2845 case LDAP_AVA_STRING: {
2847 unsigned f = flags | ava->la_flags;
2849 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2869 * Returns in str a string representation of rdn based on flags.
2870 * There is some duplication of code between this and ldap_dn2str;
2871 * this is wanted to reduce the allocation of temporary buffers.
2874 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2881 if ( rdn == NULL ) {
2882 *str = LDAP_STRDUP( "" );
2883 return( LDAP_SUCCESS );
2887 * This routine wastes "back" bytes at the end of the string
2891 switch ( LDAP_DN_FORMAT( flags ) ) {
2892 case LDAP_DN_FORMAT_LDAPV3:
2893 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2894 return( LDAP_OTHER );
2898 case LDAP_DN_FORMAT_LDAPV2:
2899 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2900 return( LDAP_OTHER );
2904 case LDAP_DN_FORMAT_UFN:
2905 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2906 return( LDAP_OTHER );
2910 case LDAP_DN_FORMAT_DCE:
2911 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2912 return( LDAP_OTHER );
2916 case LDAP_DN_FORMAT_AD_CANONICAL:
2917 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2918 return( LDAP_OTHER );
2923 return( LDAP_INVALID_DN_SYNTAX );
2926 *str = LDAP_MALLOC( l + 1 );
2928 switch ( LDAP_DN_FORMAT( flags ) ) {
2929 case LDAP_DN_FORMAT_LDAPV3:
2930 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2934 case LDAP_DN_FORMAT_LDAPV2:
2935 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2939 case LDAP_DN_FORMAT_UFN:
2940 rc = rdn2UFNstr( rdn, *str, flags, &l );
2944 case LDAP_DN_FORMAT_DCE:
2945 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2949 case LDAP_DN_FORMAT_AD_CANONICAL:
2950 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
2955 /* need at least one of the previous */
2956 return( LDAP_OTHER );
2960 ldap_memfree( *str );
2961 return( LDAP_OTHER );
2964 ( *str )[ l - back ] = '\0';
2966 return( LDAP_SUCCESS );
2970 * Very bulk implementation; many optimizations can be performed
2971 * - a NULL dn results in an empty string ""
2974 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2975 * we must encode it in binary form ('#' + HEXPAIRs)
2976 * b) does DCE/AD support UTF-8?
2977 * no clue; don't think so.
2978 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2979 * use binary encoded BER
2981 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2984 int rc = LDAP_OTHER;
2987 /* stringifying helpers for LDAPv3/LDAPv2 */
2988 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2989 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2993 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
2998 * a null dn means an empty dn string
2999 * FIXME: better raise an error?
3002 *str = LDAP_STRDUP( "" );
3003 return( LDAP_SUCCESS );
3006 switch ( LDAP_DN_FORMAT( flags ) ) {
3007 case LDAP_DN_FORMAT_LDAPV3:
3008 sv2l = strval2strlen;
3012 case LDAP_DN_FORMAT_LDAPV2:
3013 sv2l = strval2IA5strlen;
3014 sv2s = strval2IA5str;
3017 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3019 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3021 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3022 goto return_results;
3028 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3029 rc = LDAP_NO_MEMORY;
3033 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3035 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3037 if ( rdn2str( rdn, &( *str )[ l ], flags,
3041 goto return_results;
3049 * trim the last ',' (the allocated memory
3050 * is one byte longer than required)
3052 ( *str )[ len - 1 ] = '\0';
3057 case LDAP_DN_FORMAT_UFN: {
3060 * FIXME: quoting from RFC 1781:
3062 To take a distinguished name, and generate a name of this format with
3063 attribute types omitted, the following steps are followed.
3065 1. If the first attribute is of type CommonName, the type may be
3068 2. If the last attribute is of type Country, the type may be
3071 3. If the last attribute is of type Country, the last
3072 Organisation attribute may have the type omitted.
3074 4. All attributes of type OrganisationalUnit may have the type
3075 omitted, unless they are after an Organisation attribute or
3076 the first attribute is of type OrganisationalUnit.
3078 * this should be the pedantic implementation.
3080 * Here the standard implementation reflects
3081 * the one historically provided by OpenLDAP
3082 * (and UMIch, I presume), with the variant
3083 * of spaces and plusses (' + ') separating
3086 * A non-standard but nice implementation could
3087 * be to turn the final "dc" attributes into a
3088 * dot-separated domain.
3090 * Other improvements could involve the use of
3091 * friendly country names and so.
3094 int leftmost_dc = -1;
3096 #endif /* DC_IN_UFN */
3098 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3100 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3102 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3103 goto return_results;
3108 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3109 if ( leftmost_dc == -1 ) {
3115 #endif /* DC_IN_UFN */
3118 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3119 rc = LDAP_NO_MEMORY;
3124 if ( leftmost_dc == -1 ) {
3125 #endif /* DC_IN_UFN */
3126 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3128 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3130 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3134 goto return_results;
3140 * trim the last ', ' (the allocated memory
3141 * is two bytes longer than required)
3143 ( *str )[ len - 2 ] = '\0';
3146 last_iRDN = iRDN - 1;
3148 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3150 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3152 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3156 goto return_results;
3161 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3164 goto return_results;
3167 /* the string is correctly terminated by dn2domain */
3169 #endif /* DC_IN_UFN */
3175 case LDAP_DN_FORMAT_DCE:
3177 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3179 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3181 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3182 goto return_results;
3188 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3189 rc = LDAP_NO_MEMORY;
3193 for ( l = 0; iRDN--; ) {
3195 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3197 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
3201 goto return_results;
3208 ( *str )[ len ] = '\0';
3213 case LDAP_DN_FORMAT_AD_CANONICAL: {
3216 * Sort of UFN for DCE DNs: a slash ('/') separated
3217 * global->local DN with no types; strictly speaking,
3218 * the naming context should be a domain, which is
3219 * written in DNS-style, e.g. dot-deparated.
3223 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3227 * "microsoft.com/People/Bill,Gates"
3229 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3231 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3233 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3234 goto return_results;
3240 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3241 rc = LDAP_NO_MEMORY;
3246 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3247 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3249 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3251 if ( rdn2ADstr( rdn, &( *str )[ l ],
3252 flags, &rdnl, 0 ) ) {
3255 goto return_results;
3264 * Strictly speaking, AD canonical requires
3265 * a DN to be in the form "..., dc=smtg",
3266 * i.e. terminated by a domain component
3268 if ( flags & LDAP_DN_PEDANTIC ) {
3271 rc = LDAP_INVALID_DN_SYNTAX;
3275 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3277 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3279 if ( rdn2ADstr( rdn, &( *str )[ l ],
3280 flags, &rdnl, first ) ) {
3283 goto return_results;
3292 ( *str )[ len ] = '\0';
3299 return( LDAP_INVALID_DN_SYNTAX );
3303 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );