3 * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
4 * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
7 * Copyright (c) 1994 Regents of the University of Michigan.
17 #include <ac/stdlib.h>
20 #include <ac/socket.h>
21 #include <ac/string.h>
26 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
27 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 static int dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout );
32 /* from libraries/libldap/schema.c */
33 extern char * parse_numericoid(const char **sp, int *code, const int flags);
35 /* parsing/printing routines */
36 static int str2strval( const char *str, struct berval **val,
37 const char **next, unsigned flags, unsigned *retFlags );
38 static int DCE2strval( const char *str, struct berval **val,
39 const char **next, unsigned flags );
40 static int IA52strval( const char *str, struct berval **val,
41 const char **next, unsigned flags );
42 static int quotedIA52strval( const char *str, struct berval **val,
43 const char **next, unsigned flags );
44 static int hexstr2binval( const char *str, struct berval **val,
45 const char **next, unsigned flags );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN *dn, char *str, int *iRDN );
68 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 */
168 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
170 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_UFN );
173 #endif /* USE_LDAP_DN_PARSING */
177 * RFC 1823 ldap_explode_dn
180 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
182 #ifndef USE_LDAP_DN_PARSING /* deprecated */
183 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
185 return explode_name( dn, notypes, NAME_TYPE_LDAP_DN );
186 #else /* USE_LDAP_DN_PARSING */
188 char **values = NULL;
190 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
192 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
194 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
199 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
200 char *str, **v = NULL;
202 ldap_rdn2str( tmpDN[ iRDN ][ 0 ], &str, flag );
204 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iRDN ) );
206 LBER_VFREE( values );
207 ldapava_free_dn( tmpDN );
211 values[ iRDN ] = str;
213 values[ iRDN ] = NULL;
216 #endif /* USE_LDAP_DN_PARSING */
220 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
222 #ifndef USE_LDAP_DN_PARSING /* deprecated */
223 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
225 return explode_name( rdn, notypes, NAME_TYPE_LDAP_RDN );
226 #else /* USE_LDAP_DN_PARSING */
228 char **values = NULL;
230 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
232 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
235 * we assume this dn is made of one rdn only
237 if ( ldap_str2dn( rdn, &tmpDN, LDAP_DN_FORMAT_LDAP )
242 for ( iAVA = 0; tmpDN[ 0 ][ 0 ][ iAVA ]; iAVA++ ) {
243 ber_len_t l = 0, vl, al = 0;
244 char *str, **v = NULL;
245 LDAPAVA *ava = tmpDN[ 0 ][ 0 ][ iAVA ][ 0 ];
247 v = LDAP_REALLOC( values, sizeof( char * ) * ( 2 + iAVA ) );
253 if ( ava->la_flags == LDAP_AVA_BINARY ) {
254 vl = 1 + 2 * ava->la_value->bv_len;
257 if ( strval2strlen( ava->la_value,
258 ava->la_flags, &vl ) ) {
264 al = ava->la_attr->bv_len;
265 l = vl + ava->la_attr->bv_len + 1;
267 str = LDAP_MALLOC( l + 1 );
268 AC_MEMCPY( str, ava->la_attr->bv_val,
269 ava->la_attr->bv_len );
274 str = LDAP_MALLOC( l + 1 );
277 if ( ava->la_flags == LDAP_AVA_BINARY ) {
279 if ( binval2hexstr( ava->la_value, &str[ al ] ) ) {
284 if ( strval2str( ava->la_value, &str[ al ],
285 ava->la_flags, &vl ) ) {
291 values[ iAVA ] = str;
293 values[ iAVA ] = NULL;
295 ldapava_free_dn( tmpDN );
300 LBER_VFREE( values );
301 ldapava_free_dn( tmpDN );
303 #endif /* USE_LDAP_DN_PARSING */
307 ldap_dn2dcedn( LDAP_CONST char *dn )
309 #ifndef USE_LDAP_DN_PARSING /* deprecated */
310 char *dce, *q, **rdns, **p;
313 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
315 rdns = explode_name( dn, 0, NAME_TYPE_LDAP_DN );
316 if ( rdns == NULL ) {
320 for ( p = rdns; *p != NULL; p++ ) {
321 len += strlen( *p ) + 1;
324 q = dce = LDAP_MALLOC( len + 1 );
329 p--; /* get back past NULL */
331 for ( ; p != rdns; p-- ) {
343 #else /* USE_LDAP_DN_PARSING */
346 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
348 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP, &out, LDAP_DN_FORMAT_DCE );
351 #endif /* USE_LDAP_DN_PARSING */
355 ldap_dcedn2dn( LDAP_CONST char *dce )
357 #ifndef USE_LDAP_DN_PARSING
358 char *dn, *q, **rdns, **p;
361 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
363 rdns = explode_name( dce, 0, NAME_TYPE_DCE_DN );
364 if ( rdns == NULL ) {
370 for ( p = rdns; *p != NULL; p++ ) {
371 len += strlen( *p ) + 1;
374 q = dn = LDAP_MALLOC( len );
381 for ( ; p != rdns; p-- ) {
389 /* the name was fully qualified, thus the most-significant
390 * RDN was empty. trash the last comma */
394 /* the name was relative. copy the most significant RDN */
399 #else /* USE_LDAP_DN_PARSING */
402 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
404 ( void )dn2dn( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
407 #endif /* USE_LDAP_DN_PARSING */
411 ldap_dn2ad_canonical( LDAP_CONST char *dn )
415 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
417 ( void )dn2dn( dn, LDAP_DN_FORMAT_LDAP,
418 &out, LDAP_DN_FORMAT_AD_CANONICAL );
423 #ifndef USE_LDAP_DN_PARSING /* deprecated */
428 explode_name( const char *name, int notypes, int is_type )
430 const char *p, *q, *rdn;
432 int offset, state, have_equals, count = 0, endquote, len;
435 if(name == NULL) name = "";
437 /* skip leading whitespace */
438 while( ldap_utf8_isspace( name )) {
439 LDAP_UTF8_INCR( name );
454 if ( p[1] != '\0' ) {
455 offset = LDAP_UTF8_OFFSET(++p);
459 if ( state == INQUOTE )
465 if( state == OUTQUOTE ) have_equals++;
468 if (is_type == NAME_TYPE_LDAP_RDN)
472 if (is_type == NAME_TYPE_DCE_DN)
477 if (is_type == NAME_TYPE_LDAP_DN)
482 if ( state == OUTQUOTE ) {
486 if ( parts == NULL ) {
487 if (( parts = (char **)LDAP_MALLOC( 8
488 * sizeof( char *))) == NULL )
490 } else if ( count >= 8 ) {
491 if (( parts = (char **)LDAP_REALLOC( parts,
492 (count+1) * sizeof( char *)))
497 parts[ count ] = NULL;
501 for ( q = rdn; q < p && *q != '='; ++q ) {
513 if ( p[-1] == '"' ) {
521 if (( parts[ count-1 ] = (char *)LDAP_CALLOC( 1,
524 AC_MEMCPY( parts[ count-1 ], rdn, len );
527 /* skip trailing spaces */
528 while( len > 0 && ldap_utf8_isspace(
529 &parts[count-1][len-1] ) )
535 parts[ count-1 ][ len ] = '\0';
539 * Don't forget to increment 'p' back to where
540 * it should be. If we don't, then we will
541 * never get past an "end quote."
546 rdn = *p ? &p[1] : p;
547 while ( ldap_utf8_isspace( rdn ) )
555 #endif /* !USE_LDAP_DN_PARSING */
558 ldap_dn_normalize( const char *in, unsigned iflags, char **out, unsigned oflags )
562 #ifdef USE_LDAP_DN_PARSING
563 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
565 return dn2dn( in, iflags, out, oflags);
566 #else /* !USE_LDAP_DN_PARSING */
567 return( LDAP_OTHER );
568 #endif /* !USE_LDAP_DN_PARSING */
572 * helper that changes the string representation of dnin
573 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
576 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
577 * plus some rfc 1779)
578 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
579 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
580 * LDAP_DN_FORMAT_DCE (?)
582 * fout can be any of the above except
583 * LDAP_DN_FORMAT_LDAP
585 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
586 * LDAP_DN_FORMAT_AD_CANONICAL (?)
589 dn2dn( const char *dnin, unsigned fin, char **dnout, unsigned fout )
592 LDAPDN *tmpDN = NULL;
598 if ( dnin == NULL ) {
599 return( LDAP_SUCCESS );
602 rc = ldap_str2dn( dnin , &tmpDN, fin );
603 if ( rc != LDAP_SUCCESS ) {
607 rc = ldap_dn2str( tmpDN, dnout, fout );
609 ldapava_free_dn( tmpDN );
617 /* #define B4ATTRTYPE 0x0001 */
618 #define B4OIDATTRTYPE 0x0002
619 #define B4STRINGATTRTYPE 0x0003
621 #define B4AVAEQUALS 0x0100
622 #define B4AVASEP 0x0200
623 #define B4RDNSEP 0x0300
624 #define GOTAVA 0x0400
626 #define B4ATTRVALUE 0x0010
627 #define B4STRINGVALUE 0x0020
628 #define B4IA5VALUEQUOTED 0x0030
629 #define B4IA5VALUE 0x0040
630 #define B4BINARYVALUE 0x0050
632 /* Helpers (mostly from slapd.h; maybe it should be rewritten from this) */
633 #define LDAP_DN_ASCII_SPACE(c) \
634 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
635 #define LDAP_DN_ASCII_LOWER(c) ( (c) >= 'a' && (c) <= 'z' )
636 #define LDAP_DN_ASCII_UPPER(c) ( (c) >= 'A' && (c) <= 'Z' )
637 #define LDAP_DN_ASCII_ALPHA(c) \
638 ( LDAP_DN_ASCII_LOWER(c) || LDAP_DN_ASCII_UPPER(c) )
639 #define LDAP_DN_ASCII_DIGIT(c) ( (c) >= '0' && (c) <= '9' )
640 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) ( (c) >= 'a' && (c) <= 'f' )
641 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) ( (c) >= 'A' && (c) <= 'F' )
642 #define LDAP_DN_ASCII_HEXDIGIT(c) \
643 ( LDAP_DN_ASCII_DIGIT(c) \
644 || LDAP_DN_ASCII_LCASE_HEXALPHA(c) \
645 || LDAP_DN_ASCII_UCASE_HEXALPHA(c) )
646 #define LDAP_DN_ASCII_ALNUM(c) \
647 ( LDAP_DN_ASCII_ALPHA(c) || LDAP_DN_ASCII_DIGIT(c) )
648 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
651 #define LDAP_DN_OID_LEADCHAR(c) ( LDAP_DN_ASCII_DIGIT(c) )
652 #define LDAP_DN_DESC_LEADCHAR(c) ( LDAP_DN_ASCII_ALPHA(c) )
653 #define LDAP_DN_DESC_CHAR(c) ( LDAP_DN_ASCII_ALNUM(c) || (c) == '-' )
654 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
655 #define LDAP_DN_ATTRDESC_CHAR(c) \
656 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
658 /* special symbols */
659 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
660 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
661 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
662 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
663 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
664 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
665 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
666 #define LDAP_DN_VALUE_END(c) \
667 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
668 #define LDAP_DN_NE(c) \
669 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
670 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
671 #define LDAP_DN_NEEDESCAPE(c) \
672 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
673 #define LDAP_DN_NEEDESCAPE_LEAD(c) \
674 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) || LDAP_DN_NE(c) )
675 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
676 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
677 #define LDAP_DN_WILLESCAPE_CHAR( c) \
678 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
679 #define LDAP_DN_IS_PRETTY( f ) ( (f) & LDAP_DN_PRETTY )
680 #define LDAP_DN_WILLESCAPE(f, c) \
681 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
684 #define LDAP_DN_VALUE_END_V2(c) \
685 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
687 #define LDAP_DN_V2_SPECIAL(c) \
688 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
689 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
690 || LDAP_DN_OCTOTHORPE(c) )
691 #define LDAP_DN_V2_PAIR(c) \
692 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
695 * DCE (mostly from Luke Howard and IBM implementation for AIX)
697 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
698 * Here escapes and valid chars for GDS are considered; as soon as more
699 * specific info is found, the macros will be updated.
701 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
702 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
704 * Metachars: '/', ',', '=', '\'.
706 * the '\' is used to escape other metachars.
712 * Attribute types must start with alphabetic chars and can contain
713 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
715 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
716 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
717 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
718 #define LDAP_DN_VALUE_END_DCE(c) \
719 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
720 #define LDAP_DN_NEEDESCAPE_DCE(c) \
721 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
724 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
725 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
726 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
727 #define LDAP_DN_VALUE_END_AD(c) \
728 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
729 #define LDAP_DN_NEEDESCAPE_AD(c) \
730 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
733 #define LDAP_DN_HEXPAIR(s) \
734 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
735 #define LDAP_DC_ATTR "dc"
736 /* better look at the AttributeDescription? */
738 /* FIXME: no composite rdn or non-"dc" types, right?
739 * (what about "dc" in OID form?) */
740 /* FIXME: we do not allow binary values in domain, right? */
741 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
742 #define LDAP_DN_IS_RDN_DC( rdn ) \
743 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
744 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
745 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
747 /* Composite rules */
748 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
749 ( LDAP_DN_LDAPV2(f) \
750 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
751 #define LDAP_DN_ALLOW_SPACES(f) \
752 ( LDAP_DN_LDAPV2(f) \
753 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
754 #define LDAP_DN_LDAP(f) \
755 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
756 #define LDAP_DN_LDAPV3(f) \
757 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
758 #define LDAP_DN_LDAPV2(f) \
759 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
760 #define LDAP_DN_DCE(f) \
761 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
762 #define LDAP_DN_UFN(f) \
763 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
764 #define LDAP_DN_ADC(f) \
765 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
766 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
769 * LDAPAVA helpers (will become part of the API for operations
770 * on structural representations of DNs).
773 ldapava_new( const struct berval *attr, const struct berval *val,
781 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
783 /* should we test it? */
788 ava->la_attr = ( struct berval * )attr;
789 ava->la_value = ( struct berval * )val;
790 ava->la_flags = flags;
792 ava->la_private = NULL;
798 ldapava_free( LDAPAVA *ava )
802 ber_bvfree( ava->la_attr );
803 ber_bvfree( ava->la_value );
809 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
817 for ( i = 0U; rdn[ i ]; i++ ) {
821 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
822 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
823 newRDN[ i ][ 0 ] = ava;
824 newRDN[ i + 1 ] = NULL;
830 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
838 for ( i = 0U; rdn[ i ]; i++ ) {
844 /* assume "at end", which corresponds to
845 * ldapava_append_to_rdn */
848 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
850 /* data after insert point */
851 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
852 ( i - where ) * sizeof( LDAPRDN * ) );
854 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
855 newRDN[ where ][ 0 ] = ava;
856 newRDN[ i + 1 ] = NULL;
862 ldapava_free_rdn( LDAPRDN *rdn )
870 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
871 assert( rdn[ iAVA ][ 0 ] );
873 ldapava_free( rdn[ iAVA ][ 0 ] );
880 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
888 for ( i = 0U; dn[ i ]; i++ ) {
892 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
893 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
894 newDN[ i ][ 0 ] = rdn;
895 newDN[ i + 1 ] = NULL;
901 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
909 for ( i = 0U; dn[ i ]; i++ ) {
915 /* assume "at end", which corresponds to
916 * ldapava_append_to_dn */
919 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
921 /* data after insert point */
922 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
923 ( i - where ) * sizeof( LDAPDN * ) );
925 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
926 newDN[ where ][ 0 ] = rdn;
927 newDN[ i + 1 ] = NULL;
933 ldapava_free_dn( LDAPDN *dn )
941 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
942 assert( dn[ iRDN ][ 0 ] );
944 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
951 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
952 * into a structural representation of the DN, by separating attribute
953 * types and values encoded in the more appropriate form, which is
954 * string or OID for attribute types and binary form of the BER encoded
955 * value or Unicode string. Formats different from LDAPv3 are parsed
956 * according to their own rules and turned into the more appropriate
957 * form according to LDAPv3.
959 * NOTE: I realize the code is getting spaghettish; it is rather
960 * experimental and will hopefully turn into something more simple
961 * and readable as soon as it works as expected.
965 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
968 int rc = LDAP_INVALID_DN_SYNTAX;
970 LDAPDN *newDN = NULL;
971 LDAPRDN *newRDN = NULL;
976 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
980 switch ( LDAP_DN_FORMAT( flags ) ) {
981 case LDAP_DN_FORMAT_LDAP:
982 case LDAP_DN_FORMAT_LDAPV3:
983 case LDAP_DN_FORMAT_LDAPV2:
984 case LDAP_DN_FORMAT_DCE:
987 /* unsupported in str2dn */
988 case LDAP_DN_FORMAT_UFN:
989 case LDAP_DN_FORMAT_AD_CANONICAL:
990 return( LDAP_INVALID_DN_SYNTAX );
993 return( LDAP_OTHER );
996 if ( str[ 0 ] == '\0' ) {
997 return( LDAP_SUCCESS );
1001 if ( LDAP_DN_DCE( flags ) ) {
1004 * (from Luke Howard: thnx) A RDN separator is required
1005 * at the beginning of an (absolute) DN.
1007 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1012 } else if ( LDAP_DN_LDAP( flags ) ) {
1014 * if dn starts with '/' let's make it a DCE dn
1016 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1017 flags |= LDAP_DN_FORMAT_DCE;
1022 for ( ; p[ 0 ]; p++ ) {
1026 err = ldap_str2rdn( p, &newRDN, &p, flags );
1027 if ( err != LDAP_SUCCESS ) {
1032 * We expect a rdn separator
1035 switch ( LDAP_DN_FORMAT( flags ) ) {
1036 case LDAP_DN_FORMAT_LDAPV3:
1037 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
1043 case LDAP_DN_FORMAT_LDAP:
1044 case LDAP_DN_FORMAT_LDAPV2:
1045 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
1051 case LDAP_DN_FORMAT_DCE:
1052 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1061 if ( LDAP_DN_DCE( flags ) ) {
1062 /* add in reversed order */
1063 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1065 dn = ldapava_append_to_dn( newDN, newRDN );
1069 rc = LDAP_NO_MEMORY;
1076 if ( p[ 0 ] == '\0' ) {
1079 * the DN is over, phew
1088 ldapava_free_rdn( newRDN );
1092 ldapava_free_dn( newDN );
1098 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1107 * Parses a relative DN according to flags up to a rdn separator
1108 * or to the end of str.
1109 * Returns the rdn and a pointer to the string continuation, which
1110 * corresponds to the rdn separator or to '\0' in case the string is over.
1113 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1117 int rc = LDAP_INVALID_DN_SYNTAX;
1118 int attrTypeEncoding = LDAP_AVA_STRING,
1119 attrValueEncoding = LDAP_AVA_STRING;
1121 struct berval *attrType = NULL;
1122 struct berval *attrValue = NULL;
1124 LDAPRDN *newRDN = NULL;
1130 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1135 switch ( LDAP_DN_FORMAT( flags ) ) {
1136 case LDAP_DN_FORMAT_LDAP:
1137 case LDAP_DN_FORMAT_LDAPV3:
1138 case LDAP_DN_FORMAT_LDAPV2:
1139 case LDAP_DN_FORMAT_DCE:
1142 /* unsupported in str2dn */
1143 case LDAP_DN_FORMAT_UFN:
1144 case LDAP_DN_FORMAT_AD_CANONICAL:
1145 return( LDAP_INVALID_DN_SYNTAX );
1148 return( LDAP_OTHER );
1151 if ( str[ 0 ] == '\0' ) {
1152 return( LDAP_SUCCESS );
1156 for ( ; p[ 0 ] || state == GOTAVA; ) {
1159 * The parser in principle advances one token a time,
1160 * or toggles state if preferable.
1165 * an AttributeType can be encoded as:
1166 * - its string representation; in detail, implementations
1167 * MUST recognize AttributeType string type names listed
1168 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1169 * MAY recognize other names.
1170 * - its numeric OID (a dotted decimal string); in detail
1171 * RFC 2253 asserts that ``Implementations MUST allow
1172 * an oid in the attribute type to be prefixed by one
1173 * of the character strings "oid." or "OID."''. As soon
1174 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1175 * I'm not sure whether this is required or not any
1176 * longer; to be liberal, we still implement it.
1179 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1180 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1187 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1188 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1193 /* whitespace is allowed (and trimmed) */
1195 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1200 /* error: we expected an AVA */
1206 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1207 state = B4OIDATTRTYPE;
1211 /* else must be alpha */
1212 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1216 /* LDAPv2 "oid." prefix */
1217 if ( LDAP_DN_LDAPV2( flags ) ) {
1219 * to be overly pedantic, we only accept
1222 if ( flags & LDAP_DN_PEDANTIC ) {
1223 if ( !strncmp( p, "OID.", 4 )
1224 || !strncmp( p, "oid.", 4 ) ) {
1226 state = B4OIDATTRTYPE;
1230 if ( !strncasecmp( p, "oid.", 4 ) ) {
1232 state = B4OIDATTRTYPE;
1238 state = B4STRINGATTRTYPE;
1241 case B4OIDATTRTYPE: {
1242 int err = LDAP_SUCCESS;
1245 type = parse_numericoid( &p, &err, 0 );
1246 if ( type == NULL ) {
1249 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1250 if ( attrType== NULL ) {
1251 rc = LDAP_NO_MEMORY;
1254 attrType->bv_val = type;
1255 attrType->bv_len = strlen( type );
1256 attrTypeEncoding = LDAP_AVA_BINARY;
1258 state = B4AVAEQUALS;
1262 case B4STRINGATTRTYPE: {
1263 const char *startPos, *endPos = NULL;
1267 * the starting char has been found to be
1268 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1269 * FIXME: DCE attr types seem to have a more
1270 * restrictive syntax (no '-' ...)
1272 for ( startPos = p++; p[ 0 ]; p++ ) {
1273 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1277 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1280 * RFC 2253 does not explicitly
1281 * allow lang extensions to attribute
1284 if ( flags & LDAP_DN_PEDANTIC ) {
1289 * we trim ';' and following lang
1290 * and so from attribute types
1293 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1294 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1302 len = ( endPos ? endPos : p ) - startPos;
1307 assert( attrType == NULL );
1308 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1309 if ( attrType == NULL ) {
1310 rc = LDAP_NO_MEMORY;
1313 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1314 if ( attrType->bv_val == NULL ) {
1315 rc = LDAP_NO_MEMORY;
1318 attrType->bv_len = len;
1319 attrTypeEncoding = LDAP_AVA_STRING;
1322 * here we need to decide whether to use it as is
1323 * or turn it in OID form; as a consequence, we
1324 * need to decide whether to binary encode the value
1327 state = B4AVAEQUALS;
1332 /* spaces may not be allowed */
1333 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1334 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1339 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1344 /* need equal sign */
1345 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1350 /* spaces may not be allowed */
1351 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1352 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1357 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1363 * octothorpe means a BER encoded value will follow
1364 * FIXME: I don't think DCE will allow it
1366 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1368 attrValueEncoding = LDAP_AVA_BINARY;
1369 state = B4BINARYVALUE;
1373 /* STRING value expected */
1376 * if we're pedantic, an attribute type in OID form
1377 * SHOULD imply a BER encoded attribute value; we
1378 * should at least issue a warning
1380 if ( ( flags & LDAP_DN_PEDANTIC )
1381 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1382 /* OID attrType SHOULD use binary encoding */
1386 attrValueEncoding = LDAP_AVA_STRING;
1389 * LDAPv2 allows the attribute value to be quoted;
1390 * also, IA5 values are expected, in principle
1392 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1393 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1395 state = B4IA5VALUEQUOTED;
1399 if ( LDAP_DN_LDAPV2( flags ) ) {
1406 * here STRING means RFC 2253 string
1407 * FIXME: what about DCE strings?
1409 state = B4STRINGVALUE;
1413 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1421 switch ( LDAP_DN_FORMAT( flags ) ) {
1422 case LDAP_DN_FORMAT_LDAP:
1423 case LDAP_DN_FORMAT_LDAPV3:
1424 if ( str2strval( p, &attrValue, &p, flags,
1425 &attrValueEncoding ) ) {
1430 case LDAP_DN_FORMAT_DCE:
1431 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1444 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1451 case B4IA5VALUEQUOTED:
1453 /* lead quote already stripped */
1454 if ( quotedIA52strval( p, &attrValue,
1468 * we accept empty values
1470 ava = ldapava_new( attrType, attrValue,
1471 attrValueEncoding );
1472 if ( ava == NULL ) {
1473 rc = LDAP_NO_MEMORY;
1477 rdn = ldapava_append_to_rdn( newRDN, ava );
1478 if ( rdn == NULL ) {
1479 rc = LDAP_NO_MEMORY;
1485 * if we got an AVA separator ('+', or ',' for DCE )
1486 * we expect a new AVA for this RDN; otherwise
1487 * we add the RDN to the DN
1489 switch ( LDAP_DN_FORMAT( flags ) ) {
1490 case LDAP_DN_FORMAT_LDAP:
1491 case LDAP_DN_FORMAT_LDAPV3:
1492 case LDAP_DN_FORMAT_LDAPV2:
1493 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1498 case LDAP_DN_FORMAT_DCE:
1499 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1507 * the RDN is over, phew
1514 /* they should have been used in an AVA */
1530 /* They are set to NULL after they're used in an AVA */
1532 ber_bvfree( attrType );
1536 ber_bvfree( attrValue );
1540 ldapava_free_rdn( newRDN );
1546 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1554 * reads in a UTF-8 string value, unescaping stuff:
1555 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1556 * '\' + HEXPAIR(p) -> unhex(p)
1559 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1561 const char *p, *startPos, *endPos = NULL;
1562 ber_len_t len, escapes, unescapes;
1571 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1572 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1574 if ( p[ 0 ] == '\0' ) {
1577 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1578 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1579 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1584 if ( LDAP_DN_HEXPAIR( p ) ) {
1587 hexstr2bin( p, &c );
1590 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1593 * we assume the string is UTF-8
1595 *retFlags = LDAP_AVA_NONPRINTABLE;
1602 if ( LDAP_DN_PEDANTIC & flags ) {
1606 * FIXME: we allow escaping
1607 * of chars that don't need
1608 * to and do not belong to
1609 * HEXDIGITS (we also allow
1610 * single hexdigit; maybe we
1615 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1616 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1619 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1621 * FIXME: maybe we can add
1622 * escapes if not pedantic?
1629 * we do allow unescaped spaces at the end
1630 * of the value only in non-pedantic mode
1632 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1633 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1634 if ( flags & LDAP_DN_PEDANTIC ) {
1638 /* strip trailing (unescaped) spaces */
1639 for ( endPos = p - 1;
1640 endPos > startPos + 1 &&
1641 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1642 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1649 * FIXME: test memory?
1651 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1652 *val = LDAP_MALLOC( sizeof( struct berval ) );
1653 ( *val )->bv_len = len;
1655 if ( escapes == 0 && unescapes == 0 ) {
1656 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1661 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1662 for ( s = 0, d = 0; d < len; ) {
1663 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1665 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1666 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1667 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1668 ( *val )->bv_val[ d++ ] =
1671 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1674 hexstr2bin( &startPos[ s ], &c );
1675 ( *val )->bv_val[ d++ ] = c;
1680 * we allow escaping of chars
1681 * that do not need to
1683 ( *val )->bv_val[ d++ ] =
1688 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1692 ( *val )->bv_val[ d ] = '\0';
1693 assert( strlen( ( *val )->bv_val ) == len );
1703 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1705 const char *p, *startPos, *endPos = NULL;
1706 ber_len_t len, escapes;
1715 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1716 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1718 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1725 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1730 * FIXME: can we accept anything else? I guess we need
1731 * to stop if a value is not legal
1736 * (unescaped) trailing spaces are trimmed must be silently ignored;
1739 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1740 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1741 if ( flags & LDAP_DN_PEDANTIC ) {
1745 /* strip trailing (unescaped) spaces */
1746 for ( endPos = p - 1;
1747 endPos > startPos + 1 &&
1748 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1749 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1756 len = ( endPos ? endPos : p ) - startPos - escapes;
1757 *val = LDAP_MALLOC( sizeof( struct berval ) );
1758 ( *val )->bv_len = len;
1759 if ( escapes == 0 ){
1760 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1765 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1766 for ( s = 0, d = 0; d < len; ) {
1768 * This point is reached only if escapes
1769 * are properly used, so all we need to
1772 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1776 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1778 ( *val )->bv_val[ d ] = '\0';
1779 assert( strlen( ( *val )->bv_val ) == len );
1788 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1790 const char *p, *startPos, *endPos = NULL;
1791 ber_len_t len, escapes;
1804 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1805 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1807 if ( p[ 0 ] == '\0' ) {
1811 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1812 && ( LDAP_DN_PEDANTIC & flags ) ) {
1817 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1822 * FIXME: can we accept anything else? I guess we need
1823 * to stop if a value is not legal
1827 /* strip trailing (unescaped) spaces */
1829 endPos > startPos + 1 &&
1830 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1831 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1836 *val = LDAP_MALLOC( sizeof( struct berval ) );
1837 len = ( endPos ? endPos : p ) - startPos - escapes;
1838 ( *val )->bv_len = len;
1839 if ( escapes == 0 ) {
1840 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1845 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1846 for ( s = 0, d = 0; d < len; ) {
1847 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1850 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1852 ( *val )->bv_val[ d ] = '\0';
1853 assert( strlen( ( *val )->bv_val ) == len );
1861 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1863 const char *p, *startPos, *endPos = NULL;
1865 unsigned escapes = 0;
1874 /* initial quote already eaten */
1875 for ( startPos = p = str; p[ 0 ]; p++ ) {
1877 * According to RFC 1779, the quoted value can
1878 * contain escaped as well as unescaped special values;
1879 * as a consequence we tolerate escaped values
1880 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1881 * (e.g. '","' -> '\,').
1883 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1884 if ( p[ 1 ] == '\0' ) {
1889 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1890 && ( LDAP_DN_PEDANTIC & flags ) ) {
1892 * do we allow to escape normal chars?
1893 * LDAPv2 does not allow any mechanism
1894 * for escaping chars with '\' and hex
1901 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1903 /* eat closing quotes */
1909 * FIXME: can we accept anything else? I guess we need
1910 * to stop if a value is not legal
1914 if ( endPos == NULL ) {
1918 /* Strip trailing (unescaped) spaces */
1919 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1923 len = endPos - startPos - escapes;
1925 *val = LDAP_MALLOC( sizeof( struct berval ) );
1926 ( *val )->bv_len = len;
1927 if ( escapes == 0 ) {
1928 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1933 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1934 ( *val )->bv_len = len;
1936 for ( s = d = 0; d < len; ) {
1937 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1940 ( *val )->bv_val[ d++ ] = str[ s++ ];
1942 ( *val )->bv_val[ d ] = '\0';
1943 assert( strlen( ( *val )->bv_val ) == len );
1952 hexstr2bin( const char *str, char *c )
1962 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1968 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1975 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1981 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1982 *c += c2 - 'a' + 10;
1990 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1992 const char *p, *startPos, *endPos = NULL;
2003 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
2004 switch ( LDAP_DN_FORMAT( flags ) ) {
2005 case LDAP_DN_FORMAT_LDAPV3:
2006 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2011 case LDAP_DN_FORMAT_LDAP:
2012 case LDAP_DN_FORMAT_LDAPV2:
2013 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2018 case LDAP_DN_FORMAT_DCE:
2019 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2025 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
2026 if ( flags & LDAP_DN_PEDANTIC ) {
2031 for ( ; p[ 0 ]; p++ ) {
2032 switch ( LDAP_DN_FORMAT( flags ) ) {
2033 case LDAP_DN_FORMAT_LDAPV3:
2034 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2039 case LDAP_DN_FORMAT_LDAP:
2040 case LDAP_DN_FORMAT_LDAPV2:
2041 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2046 case LDAP_DN_FORMAT_DCE:
2047 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2056 if ( !LDAP_DN_HEXPAIR( p ) ) {
2063 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2065 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
2067 *val = LDAP_MALLOC( sizeof( struct berval ) );
2068 if ( *val == NULL ) {
2069 return( LDAP_NO_MEMORY );
2072 ( *val )->bv_len = len;
2073 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
2074 if ( ( *val )->bv_val == NULL ) {
2076 return( LDAP_NO_MEMORY );
2079 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2082 hexstr2bin( &startPos[ s ], &c );
2084 ( *val )->bv_val[ d ] = c;
2087 ( *val )->bv_val[ d ] = '\0';
2094 * convert a byte in a hexadecimal pair
2097 byte2hexpair( const char *val, char *pair )
2099 static const char hexdig[] = "0123456789abcdef";
2105 * we assume the string has enough room for the hex encoding
2109 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2110 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2116 * convert a binary value in hexadecimal pairs
2119 binval2hexstr( struct berval *val, char *str )
2126 if ( val->bv_len == 0 ) {
2131 * we assume the string has enough room for the hex encoding
2135 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2136 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2143 * Length of the string representation, accounting for escaped hex
2147 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2149 ber_len_t l, cl = 1;
2151 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2157 if ( val->bv_len == 0 ) {
2161 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2162 cl = ldap_utf8_charlen( p );
2164 /* illegal utf-8 char! */
2167 } else if ( cl > 1 ) {
2170 for ( cnt = 1; cnt < cl; cnt++ ) {
2171 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2175 l += escaped_byte_len * cl;
2178 * there might be some chars we want to escape in form
2179 * of a couple of hexdigits for optimization purposes
2181 } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
2184 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2185 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2186 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2200 * convert to string representation, escaping with hex the UTF-8 stuff;
2201 * assume the destination has enough room for escaping
2204 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2206 ber_len_t s, d, end;
2212 if ( val->bv_len == 0 ) {
2218 * we assume the string has enough room for the hex encoding
2221 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2222 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2225 * there might be some chars we want to escape in form
2226 * of a couple of hexdigits for optimization purposes
2228 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2229 || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
2232 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2237 } else if ( cl > 1 ) {
2239 str[ d++ ] = val->bv_val[ s++ ];
2243 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2244 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2245 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2248 str[ d++ ] = val->bv_val[ s++ ];
2258 * Length of the IA5 string representation (no UTF-8 allowed)
2261 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2270 if ( val->bv_len == 0 ) {
2274 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2276 * Turn value into a binary encoded BER
2281 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2282 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2283 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2284 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2299 * convert to string representation (np UTF-8)
2300 * assume the destination has enough room for escaping
2303 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2305 ber_len_t s, d, end;
2311 if ( val->bv_len == 0 ) {
2316 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2318 * Turn value into a binary encoded BER
2325 * we assume the string has enough room for the hex encoding
2329 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2330 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2331 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2332 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2335 str[ d++ ] = val->bv_val[ s++ ];
2345 * Length of the (supposedly) DCE string representation,
2346 * accounting for escaped hex of UTF-8 chars
2349 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2358 if ( val->bv_len == 0 ) {
2362 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2364 * FIXME: Turn the value into a binary encoded BER?
2369 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2370 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2385 * convert to (supposedly) DCE string representation,
2386 * escaping with hex the UTF-8 stuff;
2387 * assume the destination has enough room for escaping
2390 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2398 if ( val->bv_len == 0 ) {
2403 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2405 * FIXME: Turn the value into a binary encoded BER?
2413 * we assume the string has enough room for the hex encoding
2417 for ( s = 0, d = 0; s < val->bv_len; ) {
2418 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2421 str[ d++ ] = val->bv_val[ s++ ];
2431 * Length of the (supposedly) AD canonical string representation,
2432 * accounting for escaped hex of UTF-8 chars
2435 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2444 if ( val->bv_len == 0 ) {
2448 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2450 * FIXME: Turn the value into a binary encoded BER?
2455 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2456 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2471 * convert to (supposedly) AD string representation,
2472 * escaping with hex the UTF-8 stuff;
2473 * assume the destination has enough room for escaping
2476 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2484 if ( val->bv_len == 0 ) {
2489 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2491 * FIXME: Turn the value into a binary encoded BER?
2499 * we assume the string has enough room for the hex encoding
2503 for ( s = 0, d = 0; s < val->bv_len; ) {
2504 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2507 str[ d++ ] = val->bv_val[ s++ ];
2517 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2518 * the forst part of the AD representation of the DN is written in DNS
2519 * form, i.e. dot separated domain name components (as suggested
2520 * by Luke Howard, http://www.padl.com/~lukeh)
2523 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2526 int domain = 0, first = 1;
2527 ber_len_t l = 1; /* we move the null also */
2529 /* we are guaranteed there's enough memory in str */
2535 assert( *iRDN > 0 );
2537 for ( i = *iRDN; i >= 0; i-- ) {
2541 assert( dn[ i ][ 0 ] );
2544 assert( rdn[ 0 ][ 0 ] );
2545 ava = rdn[ 0 ][ 0 ];
2547 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2555 AC_MEMCPY( str, ava->la_value->bv_val,
2556 ava->la_value->bv_len + 1);
2557 l += ava->la_value->bv_len;
2560 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2561 AC_MEMCPY( str, ava->la_value->bv_val,
2562 ava->la_value->bv_len );
2563 str[ ava->la_value->bv_len ] = '.';
2564 l += ava->la_value->bv_len + 1;
2574 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2575 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2582 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2583 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2585 /* len(type) + '=' + '+' | ',' */
2586 l += ava->la_attr->bv_len + 2;
2588 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2589 /* octothorpe + twice the length */
2590 l += 1 + 2 * ava->la_value->bv_len;
2594 unsigned f = flags | ava->la_flags;
2596 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2609 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2610 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2615 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2616 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2618 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2619 ava->la_attr->bv_len );
2620 l += ava->la_attr->bv_len;
2624 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2626 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2629 l += 2 * ava->la_value->bv_len;
2633 unsigned f = flags | ava->la_flags;
2635 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2640 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2649 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2656 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2657 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2659 /* len(type) + '=' + ',' | '/' */
2660 l += ava->la_attr->bv_len + 2;
2662 switch ( ava->la_flags ) {
2663 case LDAP_AVA_BINARY:
2664 /* octothorpe + twice the length */
2665 l += 1 + 2 * ava->la_value->bv_len;
2668 case LDAP_AVA_STRING: {
2670 unsigned f = flags | ava->la_flags;
2672 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2690 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2695 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2696 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2701 str[ l++ ] = ( iAVA ? ',' : '/' );
2704 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2705 ava->la_attr->bv_len );
2706 l += ava->la_attr->bv_len;
2710 switch ( ava->la_flags ) {
2711 case LDAP_AVA_BINARY:
2713 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2716 l += 2 * ava->la_value->bv_len;
2719 case LDAP_AVA_STRING: {
2721 unsigned f = flags | ava->la_flags;
2723 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2741 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2751 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2752 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2755 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2757 /* FIXME: are binary values allowed in UFN? */
2758 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2759 /* octothorpe + twice the value */
2760 l += 1 + 2 * ava->la_value->bv_len;
2764 unsigned f = flags | ava->la_flags;
2766 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2779 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2784 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2785 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2787 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2789 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2792 l += 2 * ava->la_value->bv_len;
2796 unsigned f = flags | ava->la_flags;
2798 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2804 if ( rdn[ iAVA + 1 ]) {
2805 AC_MEMCPY( &str[ l ], " + ", 3 );
2809 AC_MEMCPY( &str[ l ], ", ", 2 );
2820 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2830 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2831 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2836 /* FIXME: are binary values allowed in UFN? */
2837 switch ( ava->la_flags ) {
2838 case LDAP_AVA_BINARY:
2839 /* octothorpe + twice the value */
2840 l += 1 + 2 * ava->la_value->bv_len;
2843 case LDAP_AVA_STRING: {
2845 unsigned f = flags | ava->la_flags;
2847 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2865 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2870 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2871 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2876 str[ l++ ] = ( iAVA ? ',' : '/' );
2879 switch ( ava->la_flags ) {
2880 case LDAP_AVA_BINARY:
2882 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2885 l += 2 * ava->la_value->bv_len;
2888 case LDAP_AVA_STRING: {
2890 unsigned f = flags | ava->la_flags;
2892 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2912 * Returns in str a string representation of rdn based on flags.
2913 * There is some duplication of code between this and ldap_dn2str;
2914 * this is wanted to reduce the allocation of temporary buffers.
2917 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2924 if ( rdn == NULL ) {
2925 *str = LDAP_STRDUP( "" );
2926 return( LDAP_SUCCESS );
2930 * This routine wastes "back" bytes at the end of the string
2934 switch ( LDAP_DN_FORMAT( flags ) ) {
2935 case LDAP_DN_FORMAT_LDAPV3:
2936 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2937 return( LDAP_OTHER );
2941 case LDAP_DN_FORMAT_LDAPV2:
2942 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2943 return( LDAP_OTHER );
2947 case LDAP_DN_FORMAT_UFN:
2948 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2949 return( LDAP_OTHER );
2953 case LDAP_DN_FORMAT_DCE:
2954 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2955 return( LDAP_OTHER );
2959 case LDAP_DN_FORMAT_AD_CANONICAL:
2960 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2961 return( LDAP_OTHER );
2966 return( LDAP_INVALID_DN_SYNTAX );
2969 *str = LDAP_MALLOC( l + 1 );
2971 switch ( LDAP_DN_FORMAT( flags ) ) {
2972 case LDAP_DN_FORMAT_LDAPV3:
2973 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2977 case LDAP_DN_FORMAT_LDAPV2:
2978 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2982 case LDAP_DN_FORMAT_UFN:
2983 rc = rdn2UFNstr( rdn, *str, flags, &l );
2987 case LDAP_DN_FORMAT_DCE:
2988 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2992 case LDAP_DN_FORMAT_AD_CANONICAL:
2993 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
2998 /* need at least one of the previous */
2999 return( LDAP_OTHER );
3003 ldap_memfree( *str );
3004 return( LDAP_OTHER );
3007 ( *str )[ l - back ] = '\0';
3009 return( LDAP_SUCCESS );
3013 * Very bulk implementation; many optimizations can be performed
3014 * - a NULL dn results in an empty string ""
3017 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
3018 * we must encode it in binary form ('#' + HEXPAIRs)
3019 * b) does DCE/AD support UTF-8?
3020 * no clue; don't think so.
3021 * c) what do we do when binary values must be converted in UTF/DCE/AD?
3022 * use binary encoded BER
3024 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
3027 int rc = LDAP_OTHER;
3030 /* stringifying helpers for LDAPv3/LDAPv2 */
3031 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3032 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3036 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
3041 * a null dn means an empty dn string
3042 * FIXME: better raise an error?
3045 *str = LDAP_STRDUP( "" );
3046 return( LDAP_SUCCESS );
3049 switch ( LDAP_DN_FORMAT( flags ) ) {
3050 case LDAP_DN_FORMAT_LDAPV3:
3051 sv2l = strval2strlen;
3055 case LDAP_DN_FORMAT_LDAPV2:
3056 sv2l = strval2IA5strlen;
3057 sv2s = strval2IA5str;
3060 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3062 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3064 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3065 goto return_results;
3071 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3072 rc = LDAP_NO_MEMORY;
3076 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3078 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3080 if ( rdn2str( rdn, &( *str )[ l ], flags,
3084 goto return_results;
3092 * trim the last ',' (the allocated memory
3093 * is one byte longer than required)
3095 ( *str )[ len - 1 ] = '\0';
3100 case LDAP_DN_FORMAT_UFN: {
3103 * FIXME: quoting from RFC 1781:
3105 To take a distinguished name, and generate a name of this format with
3106 attribute types omitted, the following steps are followed.
3108 1. If the first attribute is of type CommonName, the type may be
3111 2. If the last attribute is of type Country, the type may be
3114 3. If the last attribute is of type Country, the last
3115 Organisation attribute may have the type omitted.
3117 4. All attributes of type OrganisationalUnit may have the type
3118 omitted, unless they are after an Organisation attribute or
3119 the first attribute is of type OrganisationalUnit.
3121 * this should be the pedantic implementation.
3123 * Here the standard implementation reflects
3124 * the one historically provided by OpenLDAP
3125 * (and UMIch, I presume), with the variant
3126 * of spaces and plusses (' + ') separating
3129 * A non-standard but nice implementation could
3130 * be to turn the final "dc" attributes into a
3131 * dot-separated domain.
3133 * Other improvements could involve the use of
3134 * friendly country names and so.
3137 int leftmost_dc = -1;
3139 #endif /* DC_IN_UFN */
3141 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3143 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3145 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3146 goto return_results;
3151 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3152 if ( leftmost_dc == -1 ) {
3158 #endif /* DC_IN_UFN */
3161 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3162 rc = LDAP_NO_MEMORY;
3167 if ( leftmost_dc == -1 ) {
3168 #endif /* DC_IN_UFN */
3169 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3171 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3173 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3177 goto return_results;
3183 * trim the last ', ' (the allocated memory
3184 * is two bytes longer than required)
3186 ( *str )[ len - 2 ] = '\0';
3189 last_iRDN = iRDN - 1;
3191 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3193 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3195 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3199 goto return_results;
3204 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3207 goto return_results;
3210 /* the string is correctly terminated by dn2domain */
3212 #endif /* DC_IN_UFN */
3218 case LDAP_DN_FORMAT_DCE:
3220 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3222 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3224 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3225 goto return_results;
3231 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3232 rc = LDAP_NO_MEMORY;
3236 for ( l = 0; iRDN--; ) {
3238 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3240 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
3244 goto return_results;
3251 ( *str )[ len ] = '\0';
3256 case LDAP_DN_FORMAT_AD_CANONICAL: {
3259 * Sort of UFN for DCE DNs: a slash ('/') separated
3260 * global->local DN with no types; strictly speaking,
3261 * the naming context should be a domain, which is
3262 * written in DNS-style, e.g. dot-deparated.
3266 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3270 * "microsoft.com/People/Bill,Gates"
3272 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3274 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3276 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3277 goto return_results;
3283 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3284 rc = LDAP_NO_MEMORY;
3289 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3290 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3292 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3294 if ( rdn2ADstr( rdn, &( *str )[ l ],
3295 flags, &rdnl, 0 ) ) {
3298 goto return_results;
3307 * Strictly speaking, AD canonical requires
3308 * a DN to be in the form "..., dc=smtg",
3309 * i.e. terminated by a domain component
3311 if ( flags & LDAP_DN_PEDANTIC ) {
3314 rc = LDAP_INVALID_DN_SYNTAX;
3318 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3320 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3322 if ( rdn2ADstr( rdn, &( *str )[ l ],
3323 flags, &rdnl, first ) ) {
3326 goto return_results;
3335 ( *str )[ len ] = '\0';
3342 return( LDAP_INVALID_DN_SYNTAX );
3346 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );