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_WILLESCAPE(f, c) \
680 ( ( !( (f) & LDAP_DN_PRETTY ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
683 #define LDAP_DN_VALUE_END_V2(c) \
684 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
686 #define LDAP_DN_V2_SPECIAL(c) \
687 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
688 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
689 || LDAP_DN_OCTOTHORPE(c) )
690 #define LDAP_DN_V2_PAIR(c) \
691 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
694 * DCE (mostly from Luke Howard and IBM implementation for AIX)
696 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
697 * Here escapes and valid chars for GDS are considered; as soon as more
698 * specific info is found, the macros will be updated.
700 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
701 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
703 * Metachars: '/', ',', '=', '\'.
705 * the '\' is used to escape other metachars.
711 * Attribute types must start with alphabetic chars and can contain
712 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
714 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
715 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
716 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
717 #define LDAP_DN_VALUE_END_DCE(c) \
718 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
719 #define LDAP_DN_NEEDESCAPE_DCE(c) \
720 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
723 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
724 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
725 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
726 #define LDAP_DN_VALUE_END_AD(c) \
727 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
728 #define LDAP_DN_NEEDESCAPE_AD(c) \
729 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
732 #define LDAP_DN_HEXPAIR(s) \
733 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
734 #define LDAP_DC_ATTR "dc"
735 /* better look at the AttributeDescription? */
737 /* FIXME: no composite rdn or non-"dc" types, right?
738 * (what about "dc" in OID form?) */
739 /* FIXME: we do not allow binary values in domain, right? */
740 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
741 #define LDAP_DN_IS_RDN_DC( rdn ) \
742 ( ( rdn ) && ( rdn )[ 0 ][ 0 ] && !( rdn )[ 1 ] \
743 && ( ( rdn )[ 0 ][ 0 ]->la_flags == LDAP_AVA_STRING ) \
744 && ! strcasecmp( ( rdn )[ 0 ][ 0 ]->la_attr->bv_val, LDAP_DC_ATTR ) )
746 /* Composite rules */
747 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
748 ( LDAP_DN_LDAPV2(f) \
749 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
750 #define LDAP_DN_ALLOW_SPACES(f) \
751 ( LDAP_DN_LDAPV2(f) \
752 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
753 #define LDAP_DN_LDAP(f) \
754 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
755 #define LDAP_DN_LDAPV3(f) \
756 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
757 #define LDAP_DN_LDAPV2(f) \
758 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
759 #define LDAP_DN_DCE(f) \
760 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
761 #define LDAP_DN_UFN(f) \
762 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
763 #define LDAP_DN_ADC(f) \
764 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
765 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
768 * LDAPAVA helpers (will become part of the API for operations
769 * on structural representations of DNs).
772 ldapava_new( const struct berval *attr, const struct berval *val,
780 ava = LDAP_MALLOC( sizeof( LDAPAVA ) );
782 /* should we test it? */
787 ava->la_attr = ( struct berval * )attr;
788 ava->la_value = ( struct berval * )val;
789 ava->la_flags = flags;
791 ava->la_private = NULL;
797 ldapava_free( LDAPAVA *ava )
801 ber_bvfree( ava->la_attr );
802 ber_bvfree( ava->la_value );
808 ldapava_append_to_rdn( LDAPRDN *rdn, LDAPAVA *ava )
816 for ( i = 0U; rdn[ i ]; i++ ) {
820 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
821 newRDN[ i ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
822 newRDN[ i ][ 0 ] = ava;
823 newRDN[ i + 1 ] = NULL;
829 ldapava_insert_into_rdn( LDAPRDN *rdn, LDAPAVA *ava, unsigned where )
837 for ( i = 0U; rdn[ i ]; i++ ) {
843 /* assume "at end", which corresponds to
844 * ldapava_append_to_rdn */
847 newRDN = LDAP_REALLOC( rdn, ( i + 2 ) * sizeof( LDAPAVA ** ) );
849 /* data after insert point */
850 AC_MEMCPY( &newRDN[ where + 1 ], &newRDN[ where ],
851 ( i - where ) * sizeof( LDAPRDN * ) );
853 newRDN[ where ] = LDAP_MALLOC( sizeof( LDAPAVA * ) );
854 newRDN[ where ][ 0 ] = ava;
855 newRDN[ i + 1 ] = NULL;
861 ldapava_free_rdn( LDAPRDN *rdn )
869 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
870 assert( rdn[ iAVA ][ 0 ] );
872 ldapava_free( rdn[ iAVA ][ 0 ] );
879 ldapava_append_to_dn( LDAPDN *dn, LDAPRDN *rdn )
887 for ( i = 0U; dn[ i ]; i++ ) {
891 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
892 newDN[ i ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
893 newDN[ i ][ 0 ] = rdn;
894 newDN[ i + 1 ] = NULL;
900 ldapava_insert_into_dn( LDAPDN *dn, LDAPRDN *rdn, unsigned where )
908 for ( i = 0U; dn[ i ]; i++ ) {
914 /* assume "at end", which corresponds to
915 * ldapava_append_to_dn */
918 newDN = LDAP_REALLOC( dn, ( i + 2 ) * sizeof( LDAPRDN ** ) );
920 /* data after insert point */
921 AC_MEMCPY( &newDN[ where + 1 ], &newDN[ where ],
922 ( i - where ) * sizeof( LDAPDN * ) );
924 newDN[ where ] = LDAP_MALLOC( sizeof( LDAPRDN * ) );
925 newDN[ where ][ 0 ] = rdn;
926 newDN[ i + 1 ] = NULL;
932 ldapava_free_dn( LDAPDN *dn )
940 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
941 assert( dn[ iRDN ][ 0 ] );
943 ldapava_free_rdn( dn[ iRDN ][ 0 ] );
950 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
951 * into a structural representation of the DN, by separating attribute
952 * types and values encoded in the more appropriate form, which is
953 * string or OID for attribute types and binary form of the BER encoded
954 * value or Unicode string. Formats different from LDAPv3 are parsed
955 * according to their own rules and turned into the more appropriate
956 * form according to LDAPv3.
958 * NOTE: I realize the code is getting spaghettish; it is rather
959 * experimental and will hopefully turn into something more simple
960 * and readable as soon as it works as expected.
964 ldap_str2dn( const char *str, LDAPDN **dn, unsigned flags )
967 int rc = LDAP_INVALID_DN_SYNTAX;
969 LDAPDN *newDN = NULL;
970 LDAPRDN *newRDN = NULL;
975 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2dn(%s,%u)\n%s", str, flags, "" );
979 switch ( LDAP_DN_FORMAT( flags ) ) {
980 case LDAP_DN_FORMAT_LDAP:
981 case LDAP_DN_FORMAT_LDAPV3:
982 case LDAP_DN_FORMAT_LDAPV2:
983 case LDAP_DN_FORMAT_DCE:
986 /* unsupported in str2dn */
987 case LDAP_DN_FORMAT_UFN:
988 case LDAP_DN_FORMAT_AD_CANONICAL:
989 return( LDAP_INVALID_DN_SYNTAX );
992 return( LDAP_OTHER );
995 if ( str[ 0 ] == '\0' ) {
996 return( LDAP_SUCCESS );
1000 if ( LDAP_DN_DCE( flags ) ) {
1003 * (from Luke Howard: thnx) A RDN separator is required
1004 * at the beginning of an (absolute) DN.
1006 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1011 } else if ( LDAP_DN_LDAP( flags ) ) {
1013 * if dn starts with '/' let's make it a DCE dn
1015 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1016 flags |= LDAP_DN_FORMAT_DCE;
1021 for ( ; p[ 0 ]; p++ ) {
1024 rc = ldap_str2rdn( p, &newRDN, &p, flags );
1025 if ( rc != LDAP_SUCCESS ) {
1030 * We expect a rdn separator
1033 switch ( LDAP_DN_FORMAT( flags ) ) {
1034 case LDAP_DN_FORMAT_LDAPV3:
1035 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
1041 case LDAP_DN_FORMAT_LDAP:
1042 case LDAP_DN_FORMAT_LDAPV2:
1043 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
1049 case LDAP_DN_FORMAT_DCE:
1050 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
1059 if ( LDAP_DN_DCE( flags ) ) {
1060 /* add in reversed order */
1061 dn = ldapava_insert_into_dn( newDN, newRDN, 0 );
1063 dn = ldapava_append_to_dn( newDN, newRDN );
1067 rc = LDAP_NO_MEMORY;
1074 if ( p[ 0 ] == '\0' ) {
1077 * the DN is over, phew
1086 ldapava_free_rdn( newRDN );
1090 ldapava_free_dn( newDN );
1096 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2dn(%s,%u)=%d\n", str, flags, rc );
1105 * Parses a relative DN according to flags up to a rdn separator
1106 * or to the end of str.
1107 * Returns the rdn and a pointer to the string continuation, which
1108 * corresponds to the rdn separator or to '\0' in case the string is over.
1111 ldap_str2rdn( const char *str, LDAPRDN **rdn, const char **n, unsigned flags )
1115 int rc = LDAP_INVALID_DN_SYNTAX;
1116 int attrTypeEncoding = LDAP_AVA_STRING,
1117 attrValueEncoding = LDAP_AVA_STRING;
1119 struct berval *attrType = NULL;
1120 struct berval *attrValue = NULL;
1122 LDAPRDN *newRDN = NULL;
1128 Debug( LDAP_DEBUG_TRACE, "=> ldap_str2rdn(%s,%u)\n%s", str, flags, "" );
1133 switch ( LDAP_DN_FORMAT( flags ) ) {
1134 case LDAP_DN_FORMAT_LDAP:
1135 case LDAP_DN_FORMAT_LDAPV3:
1136 case LDAP_DN_FORMAT_LDAPV2:
1137 case LDAP_DN_FORMAT_DCE:
1140 /* unsupported in str2dn */
1141 case LDAP_DN_FORMAT_UFN:
1142 case LDAP_DN_FORMAT_AD_CANONICAL:
1143 return( LDAP_INVALID_DN_SYNTAX );
1146 return( LDAP_OTHER );
1149 if ( str[ 0 ] == '\0' ) {
1150 return( LDAP_SUCCESS );
1154 for ( ; p[ 0 ] || state == GOTAVA; ) {
1157 * The parser in principle advances one token a time,
1158 * or toggles state if preferable.
1163 * an AttributeType can be encoded as:
1164 * - its string representation; in detail, implementations
1165 * MUST recognize AttributeType string type names listed
1166 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1167 * MAY recognize other names.
1168 * - its numeric OID (a dotted decimal string); in detail
1169 * RFC 2253 asserts that ``Implementations MUST allow
1170 * an oid in the attribute type to be prefixed by one
1171 * of the character strings "oid." or "OID."''. As soon
1172 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1173 * I'm not sure whether this is required or not any
1174 * longer; to be liberal, we still implement it.
1177 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1178 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1185 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1186 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1191 /* whitespace is allowed (and trimmed) */
1193 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1198 /* error: we expected an AVA */
1204 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1205 state = B4OIDATTRTYPE;
1209 /* else must be alpha */
1210 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1214 /* LDAPv2 "oid." prefix */
1215 if ( LDAP_DN_LDAPV2( flags ) ) {
1217 * to be overly pedantic, we only accept
1220 if ( flags & LDAP_DN_PEDANTIC ) {
1221 if ( !strncmp( p, "OID.", 4 )
1222 || !strncmp( p, "oid.", 4 ) ) {
1224 state = B4OIDATTRTYPE;
1228 if ( !strncasecmp( p, "oid.", 4 ) ) {
1230 state = B4OIDATTRTYPE;
1236 state = B4STRINGATTRTYPE;
1239 case B4OIDATTRTYPE: {
1240 int err = LDAP_SUCCESS;
1243 type = parse_numericoid( &p, &err, 0 );
1244 if ( type == NULL ) {
1247 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1248 if ( attrType== NULL ) {
1249 rc = LDAP_NO_MEMORY;
1252 attrType->bv_val = type;
1253 attrType->bv_len = strlen( type );
1254 attrTypeEncoding = LDAP_AVA_BINARY;
1256 state = B4AVAEQUALS;
1260 case B4STRINGATTRTYPE: {
1261 const char *startPos, *endPos = NULL;
1265 * the starting char has been found to be
1266 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1267 * FIXME: DCE attr types seem to have a more
1268 * restrictive syntax (no '-' ...)
1270 for ( startPos = p++; p[ 0 ]; p++ ) {
1271 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1275 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1278 * RFC 2253 does not explicitly
1279 * allow lang extensions to attribute
1282 if ( flags & LDAP_DN_PEDANTIC ) {
1287 * we trim ';' and following lang
1288 * and so from attribute types
1291 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1292 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1300 len = ( endPos ? endPos : p ) - startPos;
1305 assert( attrType == NULL );
1306 attrType = LDAP_MALLOC( sizeof( struct berval ) );
1307 if ( attrType == NULL ) {
1308 rc = LDAP_NO_MEMORY;
1311 attrType->bv_val = LDAP_STRNDUP( startPos, len );
1312 if ( attrType->bv_val == NULL ) {
1313 rc = LDAP_NO_MEMORY;
1316 attrType->bv_len = len;
1317 attrTypeEncoding = LDAP_AVA_STRING;
1320 * here we need to decide whether to use it as is
1321 * or turn it in OID form; as a consequence, we
1322 * need to decide whether to binary encode the value
1325 state = B4AVAEQUALS;
1330 /* spaces may not be allowed */
1331 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1332 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1337 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1342 /* need equal sign */
1343 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1348 /* spaces may not be allowed */
1349 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1350 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1355 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1361 * octothorpe means a BER encoded value will follow
1362 * FIXME: I don't think DCE will allow it
1364 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1366 attrValueEncoding = LDAP_AVA_BINARY;
1367 state = B4BINARYVALUE;
1371 /* STRING value expected */
1374 * if we're pedantic, an attribute type in OID form
1375 * SHOULD imply a BER encoded attribute value; we
1376 * should at least issue a warning
1378 if ( ( flags & LDAP_DN_PEDANTIC )
1379 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1380 /* OID attrType SHOULD use binary encoding */
1384 attrValueEncoding = LDAP_AVA_STRING;
1387 * LDAPv2 allows the attribute value to be quoted;
1388 * also, IA5 values are expected, in principle
1390 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1391 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1393 state = B4IA5VALUEQUOTED;
1397 if ( LDAP_DN_LDAPV2( flags ) ) {
1404 * here STRING means RFC 2253 string
1405 * FIXME: what about DCE strings?
1407 state = B4STRINGVALUE;
1411 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1419 switch ( LDAP_DN_FORMAT( flags ) ) {
1420 case LDAP_DN_FORMAT_LDAP:
1421 case LDAP_DN_FORMAT_LDAPV3:
1422 if ( str2strval( p, &attrValue, &p, flags,
1423 &attrValueEncoding ) ) {
1428 case LDAP_DN_FORMAT_DCE:
1429 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1442 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1449 case B4IA5VALUEQUOTED:
1451 /* lead quote already stripped */
1452 if ( quotedIA52strval( p, &attrValue,
1466 * we accept empty values
1468 ava = ldapava_new( attrType, attrValue,
1469 attrValueEncoding );
1470 if ( ava == NULL ) {
1471 rc = LDAP_NO_MEMORY;
1475 rdn = ldapava_append_to_rdn( newRDN, ava );
1476 if ( rdn == NULL ) {
1477 rc = LDAP_NO_MEMORY;
1483 * if we got an AVA separator ('+', or ',' for DCE )
1484 * we expect a new AVA for this RDN; otherwise
1485 * we add the RDN to the DN
1487 switch ( LDAP_DN_FORMAT( flags ) ) {
1488 case LDAP_DN_FORMAT_LDAP:
1489 case LDAP_DN_FORMAT_LDAPV3:
1490 case LDAP_DN_FORMAT_LDAPV2:
1491 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1496 case LDAP_DN_FORMAT_DCE:
1497 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1505 * the RDN is over, phew
1512 /* they should have been used in an AVA */
1528 /* They are set to NULL after they're used in an AVA */
1530 ber_bvfree( attrType );
1534 ber_bvfree( attrValue );
1538 ldapava_free_rdn( newRDN );
1544 Debug( LDAP_DEBUG_TRACE, "<= ldap_str2rdn(%*s)=%d\n",
1552 * reads in a UTF-8 string value, unescaping stuff:
1553 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1554 * '\' + HEXPAIR(p) -> unhex(p)
1557 str2strval( const char *str, struct berval **val, const char **next, unsigned flags, unsigned *retFlags )
1559 const char *p, *startPos, *endPos = NULL;
1560 ber_len_t len, escapes, unescapes;
1569 for ( startPos = p = str, escapes = 0, unescapes = 0; p[ 0 ]; p++ ) {
1570 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1572 if ( p[ 0 ] == '\0' ) {
1575 if ( ( p == startPos + 1 && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
1576 || ( LDAP_DN_VALUE_END( p[ 1 ] ) && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) )
1577 || LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1582 if ( LDAP_DN_HEXPAIR( p ) ) {
1585 hexstr2bin( p, &c );
1588 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1591 * we assume the string is UTF-8
1593 *retFlags = LDAP_AVA_NONPRINTABLE;
1600 if ( LDAP_DN_PEDANTIC & flags ) {
1604 * FIXME: we allow escaping
1605 * of chars that don't need
1606 * to and do not belong to
1607 * HEXDIGITS (we also allow
1608 * single hexdigit; maybe we
1613 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1614 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1617 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1619 * FIXME: maybe we can add
1620 * escapes if not pedantic?
1627 * we do allow unescaped spaces at the end
1628 * of the value only in non-pedantic mode
1630 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1631 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1632 if ( flags & LDAP_DN_PEDANTIC ) {
1636 /* strip trailing (unescaped) spaces */
1637 for ( endPos = p - 1;
1638 endPos > startPos + 1 &&
1639 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1640 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1647 * FIXME: test memory?
1649 len = ( endPos ? endPos : p ) - startPos - escapes - unescapes;
1650 *val = LDAP_MALLOC( sizeof( struct berval ) );
1651 ( *val )->bv_len = len;
1653 if ( escapes == 0 && unescapes == 0 ) {
1654 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1659 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1660 for ( s = 0, d = 0; d < len; ) {
1661 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1663 if ( ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( startPos[ s ] ) )
1664 || ( s == len - 1 && LDAP_DN_NEEDESCAPE_TRAIL( startPos[ s ] ) )
1665 || LDAP_DN_NEEDESCAPE( startPos[ s ] ) ) {
1666 ( *val )->bv_val[ d++ ] =
1669 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1672 hexstr2bin( &startPos[ s ], &c );
1673 ( *val )->bv_val[ d++ ] = c;
1678 * we allow escaping of chars
1679 * that do not need to
1681 ( *val )->bv_val[ d++ ] =
1686 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1690 ( *val )->bv_val[ d ] = '\0';
1691 assert( strlen( ( *val )->bv_val ) == len );
1701 DCE2strval( const char *str, struct berval **val, const char **next, unsigned flags )
1703 const char *p, *startPos, *endPos = NULL;
1704 ber_len_t len, escapes;
1713 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1714 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1716 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1723 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1728 * FIXME: can we accept anything else? I guess we need
1729 * to stop if a value is not legal
1734 * (unescaped) trailing spaces are trimmed must be silently ignored;
1737 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1738 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1739 if ( flags & LDAP_DN_PEDANTIC ) {
1743 /* strip trailing (unescaped) spaces */
1744 for ( endPos = p - 1;
1745 endPos > startPos + 1 &&
1746 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1747 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1754 len = ( endPos ? endPos : p ) - startPos - escapes;
1755 *val = LDAP_MALLOC( sizeof( struct berval ) );
1756 ( *val )->bv_len = len;
1757 if ( escapes == 0 ){
1758 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1763 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1764 for ( s = 0, d = 0; d < len; ) {
1766 * This point is reached only if escapes
1767 * are properly used, so all we need to
1770 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1774 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1776 ( *val )->bv_val[ d ] = '\0';
1777 assert( strlen( ( *val )->bv_val ) == len );
1786 IA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1788 const char *p, *startPos, *endPos = NULL;
1789 ber_len_t len, escapes;
1802 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1803 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1805 if ( p[ 0 ] == '\0' ) {
1809 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1810 && ( LDAP_DN_PEDANTIC & flags ) ) {
1815 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1820 * FIXME: can we accept anything else? I guess we need
1821 * to stop if a value is not legal
1825 /* strip trailing (unescaped) spaces */
1827 endPos > startPos + 1 &&
1828 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1829 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1834 *val = LDAP_MALLOC( sizeof( struct berval ) );
1835 len = ( endPos ? endPos : p ) - startPos - escapes;
1836 ( *val )->bv_len = len;
1837 if ( escapes == 0 ) {
1838 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1843 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1844 for ( s = 0, d = 0; d < len; ) {
1845 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1848 ( *val )->bv_val[ d++ ] = startPos[ s++ ];
1850 ( *val )->bv_val[ d ] = '\0';
1851 assert( strlen( ( *val )->bv_val ) == len );
1859 quotedIA52strval( const char *str, struct berval **val, const char **next, unsigned flags )
1861 const char *p, *startPos, *endPos = NULL;
1863 unsigned escapes = 0;
1872 /* initial quote already eaten */
1873 for ( startPos = p = str; p[ 0 ]; p++ ) {
1875 * According to RFC 1779, the quoted value can
1876 * contain escaped as well as unescaped special values;
1877 * as a consequence we tolerate escaped values
1878 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1879 * (e.g. '","' -> '\,').
1881 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1882 if ( p[ 1 ] == '\0' ) {
1887 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1888 && ( LDAP_DN_PEDANTIC & flags ) ) {
1890 * do we allow to escape normal chars?
1891 * LDAPv2 does not allow any mechanism
1892 * for escaping chars with '\' and hex
1899 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1901 /* eat closing quotes */
1907 * FIXME: can we accept anything else? I guess we need
1908 * to stop if a value is not legal
1912 if ( endPos == NULL ) {
1916 /* Strip trailing (unescaped) spaces */
1917 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1921 len = endPos - startPos - escapes;
1923 *val = LDAP_MALLOC( sizeof( struct berval ) );
1924 ( *val )->bv_len = len;
1925 if ( escapes == 0 ) {
1926 ( *val )->bv_val = LDAP_STRNDUP( startPos, len );
1931 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
1932 ( *val )->bv_len = len;
1934 for ( s = d = 0; d < len; ) {
1935 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1938 ( *val )->bv_val[ d++ ] = str[ s++ ];
1940 ( *val )->bv_val[ d ] = '\0';
1941 assert( strlen( ( *val )->bv_val ) == len );
1950 hexstr2bin( const char *str, char *c )
1960 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1966 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) ) {
1973 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1979 if ( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) ) {
1980 *c += c2 - 'a' + 10;
1988 hexstr2binval( const char *str, struct berval **val, const char **next, unsigned flags )
1990 const char *p, *startPos, *endPos = NULL;
2001 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
2002 switch ( LDAP_DN_FORMAT( flags ) ) {
2003 case LDAP_DN_FORMAT_LDAPV3:
2004 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2009 case LDAP_DN_FORMAT_LDAP:
2010 case LDAP_DN_FORMAT_LDAPV2:
2011 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2016 case LDAP_DN_FORMAT_DCE:
2017 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2023 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
2024 if ( flags & LDAP_DN_PEDANTIC ) {
2029 for ( ; p[ 0 ]; p++ ) {
2030 switch ( LDAP_DN_FORMAT( flags ) ) {
2031 case LDAP_DN_FORMAT_LDAPV3:
2032 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
2037 case LDAP_DN_FORMAT_LDAP:
2038 case LDAP_DN_FORMAT_LDAPV2:
2039 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
2044 case LDAP_DN_FORMAT_DCE:
2045 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
2054 if ( !LDAP_DN_HEXPAIR( p ) ) {
2061 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2063 assert( 2 * len == ( endPos ? endPos : p ) - startPos );
2065 *val = LDAP_MALLOC( sizeof( struct berval ) );
2066 if ( *val == NULL ) {
2067 return( LDAP_NO_MEMORY );
2070 ( *val )->bv_len = len;
2071 ( *val )->bv_val = LDAP_MALLOC( len + 1 );
2072 if ( ( *val )->bv_val == NULL ) {
2074 return( LDAP_NO_MEMORY );
2077 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2080 hexstr2bin( &startPos[ s ], &c );
2082 ( *val )->bv_val[ d ] = c;
2085 ( *val )->bv_val[ d ] = '\0';
2092 * convert a byte in a hexadecimal pair
2095 byte2hexpair( const char *val, char *pair )
2097 static const char hexdig[] = "0123456789abcdef";
2103 * we assume the string has enough room for the hex encoding
2107 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2108 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2114 * convert a binary value in hexadecimal pairs
2117 binval2hexstr( struct berval *val, char *str )
2124 if ( val->bv_len == 0 ) {
2129 * we assume the string has enough room for the hex encoding
2133 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2134 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2141 * Length of the string representation, accounting for escaped hex
2145 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2147 ber_len_t l, cl = 1;
2154 if ( val->bv_len == 0 ) {
2158 for ( l = 0, p = val->bv_val; p[ 0 ]; p += cl ) {
2159 cl = ldap_utf8_charlen( p );
2161 /* illegal utf-8 char! */
2164 } else if ( cl > 1 ) {
2167 for ( cnt = 1; cnt < cl; cnt++ ) {
2168 if ( ( p[ cnt ] & 0x80 ) == 0x00 ) {
2172 /* need to escape it */
2176 * there might be some chars we want to escape in form
2177 * of a couple of hexdigits for optimization purposes
2179 } else if ( LDAP_DN_WILLESCAPE( flags, p[ 0 ] ) ) {
2182 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2183 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2184 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2198 * convert to string representation, escaping with hex the UTF-8 stuff;
2199 * assume the destination has enough room for escaping
2202 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2204 ber_len_t s, d, end;
2210 if ( val->bv_len == 0 ) {
2216 * we assume the string has enough room for the hex encoding
2219 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2220 ber_len_t cl = ldap_utf8_charlen( &val->bv_val[ s ] );
2223 * there might be some chars we want to escape in form
2224 * of a couple of hexdigits for optimization purposes
2226 if ( cl > 1 || LDAP_DN_WILLESCAPE( flags, val->bv_val[ s ] ) ) {
2229 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2235 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2236 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2237 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2240 str[ d++ ] = val->bv_val[ s++ ];
2250 * Length of the IA5 string representation (no UTF-8 allowed)
2253 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2262 if ( val->bv_len == 0 ) {
2266 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2268 * Turn value into a binary encoded BER
2273 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2274 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2275 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2276 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2291 * convert to string representation (np UTF-8)
2292 * assume the destination has enough room for escaping
2295 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2297 ber_len_t s, d, end;
2303 if ( val->bv_len == 0 ) {
2308 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2310 * Turn value into a binary encoded BER
2317 * we assume the string has enough room for the hex encoding
2321 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2322 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2323 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2324 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2327 str[ d++ ] = val->bv_val[ s++ ];
2337 * Length of the (supposedly) DCE string representation,
2338 * accounting for escaped hex of UTF-8 chars
2341 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2350 if ( val->bv_len == 0 ) {
2354 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2356 * FIXME: Turn the value into a binary encoded BER?
2361 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2362 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2377 * convert to (supposedly) DCE string representation,
2378 * escaping with hex the UTF-8 stuff;
2379 * assume the destination has enough room for escaping
2382 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2390 if ( val->bv_len == 0 ) {
2395 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2397 * FIXME: Turn the value into a binary encoded BER?
2405 * we assume the string has enough room for the hex encoding
2409 for ( s = 0, d = 0; s < val->bv_len; ) {
2410 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2413 str[ d++ ] = val->bv_val[ s++ ];
2423 * Length of the (supposedly) AD canonical string representation,
2424 * accounting for escaped hex of UTF-8 chars
2427 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2436 if ( val->bv_len == 0 ) {
2440 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2442 * FIXME: Turn the value into a binary encoded BER?
2447 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2448 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2463 * convert to (supposedly) AD string representation,
2464 * escaping with hex the UTF-8 stuff;
2465 * assume the destination has enough room for escaping
2468 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2476 if ( val->bv_len == 0 ) {
2481 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2483 * FIXME: Turn the value into a binary encoded BER?
2491 * we assume the string has enough room for the hex encoding
2495 for ( s = 0, d = 0; s < val->bv_len; ) {
2496 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2499 str[ d++ ] = val->bv_val[ s++ ];
2509 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2510 * the forst part of the AD representation of the DN is written in DNS
2511 * form, i.e. dot separated domain name components (as suggested
2512 * by Luke Howard, http://www.padl.com/~lukeh)
2515 dn2domain( LDAPDN *dn, char *str, int *iRDN )
2518 int domain = 0, first = 1;
2519 ber_len_t l = 1; /* we move the null also */
2521 /* we are guaranteed there's enough memory in str */
2527 assert( *iRDN > 0 );
2529 for ( i = *iRDN; i >= 0; i-- ) {
2533 assert( dn[ i ][ 0 ] );
2536 assert( rdn[ 0 ][ 0 ] );
2537 ava = rdn[ 0 ][ 0 ];
2539 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2547 AC_MEMCPY( str, ava->la_value->bv_val,
2548 ava->la_value->bv_len + 1);
2549 l += ava->la_value->bv_len;
2552 AC_MEMCPY( str + ava->la_value->bv_len + 1, str, l);
2553 AC_MEMCPY( str, ava->la_value->bv_val,
2554 ava->la_value->bv_len );
2555 str[ ava->la_value->bv_len ] = '.';
2556 l += ava->la_value->bv_len + 1;
2566 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2567 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2574 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2575 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2577 /* len(type) + '=' + '+' | ',' */
2578 l += ava->la_attr->bv_len + 2;
2580 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2581 /* octothorpe + twice the length */
2582 l += 1 + 2 * ava->la_value->bv_len;
2586 unsigned f = flags | ava->la_flags;
2588 if ( ( *s2l )( ava->la_value, f, &vl ) ) {
2601 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2602 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2607 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2608 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2610 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2611 ava->la_attr->bv_len );
2612 l += ava->la_attr->bv_len;
2616 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2618 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2621 l += 2 * ava->la_value->bv_len;
2625 unsigned f = flags | ava->la_flags;
2627 if ( ( *s2s )( ava->la_value, &str[ l ], f, &vl ) ) {
2632 str[ l++ ] = ( rdn[ iAVA + 1 ] ? '+' : ',' );
2641 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2648 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2649 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2651 /* len(type) + '=' + ',' | '/' */
2652 l += ava->la_attr->bv_len + 2;
2654 switch ( ava->la_flags ) {
2655 case LDAP_AVA_BINARY:
2656 /* octothorpe + twice the length */
2657 l += 1 + 2 * ava->la_value->bv_len;
2660 case LDAP_AVA_STRING: {
2662 unsigned f = flags | ava->la_flags;
2664 if ( strval2DCEstrlen( ava->la_value, f, &vl ) ) {
2682 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2687 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2688 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2693 str[ l++ ] = ( iAVA ? ',' : '/' );
2696 AC_MEMCPY( &str[ l ], ava->la_attr->bv_val,
2697 ava->la_attr->bv_len );
2698 l += ava->la_attr->bv_len;
2702 switch ( ava->la_flags ) {
2703 case LDAP_AVA_BINARY:
2705 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2708 l += 2 * ava->la_value->bv_len;
2711 case LDAP_AVA_STRING: {
2713 unsigned f = flags | ava->la_flags;
2715 if ( strval2DCEstr( ava->la_value, &str[ l ], f, &vl ) ) {
2733 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2743 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2744 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2747 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2749 /* FIXME: are binary values allowed in UFN? */
2750 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2751 /* octothorpe + twice the value */
2752 l += 1 + 2 * ava->la_value->bv_len;
2756 unsigned f = flags | ava->la_flags;
2758 if ( strval2strlen( ava->la_value, f, &vl ) ) {
2771 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2776 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2777 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2779 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2781 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2784 l += 2 * ava->la_value->bv_len;
2788 unsigned f = flags | ava->la_flags;
2790 if ( strval2str( ava->la_value, &str[ l ], f, &vl ) ) {
2796 if ( rdn[ iAVA + 1 ]) {
2797 AC_MEMCPY( &str[ l ], " + ", 3 );
2801 AC_MEMCPY( &str[ l ], ", ", 2 );
2812 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2822 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2823 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2828 /* FIXME: are binary values allowed in UFN? */
2829 switch ( ava->la_flags ) {
2830 case LDAP_AVA_BINARY:
2831 /* octothorpe + twice the value */
2832 l += 1 + 2 * ava->la_value->bv_len;
2835 case LDAP_AVA_STRING: {
2837 unsigned f = flags | ava->la_flags;
2839 if ( strval2ADstrlen( ava->la_value, f, &vl ) ) {
2857 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2862 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2863 LDAPAVA *ava = rdn[ iAVA ][ 0 ];
2868 str[ l++ ] = ( iAVA ? ',' : '/' );
2871 switch ( ava->la_flags ) {
2872 case LDAP_AVA_BINARY:
2874 if ( binval2hexstr( ava->la_value, &str[ l ] ) ) {
2877 l += 2 * ava->la_value->bv_len;
2880 case LDAP_AVA_STRING: {
2882 unsigned f = flags | ava->la_flags;
2884 if ( strval2ADstr( ava->la_value, &str[ l ], f, &vl ) ) {
2904 * Returns in str a string representation of rdn based on flags.
2905 * There is some duplication of code between this and ldap_dn2str;
2906 * this is wanted to reduce the allocation of temporary buffers.
2909 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2916 if ( rdn == NULL ) {
2917 *str = LDAP_STRDUP( "" );
2918 return( LDAP_SUCCESS );
2922 * This routine wastes "back" bytes at the end of the string
2926 switch ( LDAP_DN_FORMAT( flags ) ) {
2927 case LDAP_DN_FORMAT_LDAPV3:
2928 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2929 return( LDAP_OTHER );
2933 case LDAP_DN_FORMAT_LDAPV2:
2934 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2935 return( LDAP_OTHER );
2939 case LDAP_DN_FORMAT_UFN:
2940 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2941 return( LDAP_OTHER );
2945 case LDAP_DN_FORMAT_DCE:
2946 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2947 return( LDAP_OTHER );
2951 case LDAP_DN_FORMAT_AD_CANONICAL:
2952 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2953 return( LDAP_OTHER );
2958 return( LDAP_INVALID_DN_SYNTAX );
2961 *str = LDAP_MALLOC( l + 1 );
2963 switch ( LDAP_DN_FORMAT( flags ) ) {
2964 case LDAP_DN_FORMAT_LDAPV3:
2965 rc = rdn2str( rdn, *str, flags, &l, strval2str );
2969 case LDAP_DN_FORMAT_LDAPV2:
2970 rc = rdn2str( rdn, *str, flags, &l, strval2IA5str );
2974 case LDAP_DN_FORMAT_UFN:
2975 rc = rdn2UFNstr( rdn, *str, flags, &l );
2979 case LDAP_DN_FORMAT_DCE:
2980 rc = rdn2DCEstr( rdn, *str, flags, &l, 1 );
2984 case LDAP_DN_FORMAT_AD_CANONICAL:
2985 rc = rdn2ADstr( rdn, *str, flags, &l, 1 );
2990 /* need at least one of the previous */
2991 return( LDAP_OTHER );
2995 ldap_memfree( *str );
2996 return( LDAP_OTHER );
2999 ( *str )[ l - back ] = '\0';
3001 return( LDAP_SUCCESS );
3005 * Very bulk implementation; many optimizations can be performed
3006 * - a NULL dn results in an empty string ""
3009 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
3010 * we must encode it in binary form ('#' + HEXPAIRs)
3011 * b) does DCE/AD support UTF-8?
3012 * no clue; don't think so.
3013 * c) what do we do when binary values must be converted in UTF/DCE/AD?
3014 * use binary encoded BER
3016 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
3019 int rc = LDAP_OTHER;
3022 /* stringifying helpers for LDAPv3/LDAPv2 */
3023 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3024 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3028 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2str(%u)\n%s%s", flags, "", "" );
3033 * a null dn means an empty dn string
3034 * FIXME: better raise an error?
3037 *str = LDAP_STRDUP( "" );
3038 return( LDAP_SUCCESS );
3041 switch ( LDAP_DN_FORMAT( flags ) ) {
3042 case LDAP_DN_FORMAT_LDAPV3:
3043 sv2l = strval2strlen;
3047 case LDAP_DN_FORMAT_LDAPV2:
3048 sv2l = strval2IA5strlen;
3049 sv2s = strval2IA5str;
3052 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3054 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3056 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3057 goto return_results;
3063 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3064 rc = LDAP_NO_MEMORY;
3068 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3070 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3072 if ( rdn2str( rdn, &( *str )[ l ], flags,
3076 goto return_results;
3084 * trim the last ',' (the allocated memory
3085 * is one byte longer than required)
3087 ( *str )[ len - 1 ] = '\0';
3092 case LDAP_DN_FORMAT_UFN: {
3095 * FIXME: quoting from RFC 1781:
3097 To take a distinguished name, and generate a name of this format with
3098 attribute types omitted, the following steps are followed.
3100 1. If the first attribute is of type CommonName, the type may be
3103 2. If the last attribute is of type Country, the type may be
3106 3. If the last attribute is of type Country, the last
3107 Organisation attribute may have the type omitted.
3109 4. All attributes of type OrganisationalUnit may have the type
3110 omitted, unless they are after an Organisation attribute or
3111 the first attribute is of type OrganisationalUnit.
3113 * this should be the pedantic implementation.
3115 * Here the standard implementation reflects
3116 * the one historically provided by OpenLDAP
3117 * (and UMIch, I presume), with the variant
3118 * of spaces and plusses (' + ') separating
3121 * A non-standard but nice implementation could
3122 * be to turn the final "dc" attributes into a
3123 * dot-separated domain.
3125 * Other improvements could involve the use of
3126 * friendly country names and so.
3129 int leftmost_dc = -1;
3131 #endif /* DC_IN_UFN */
3133 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3135 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3137 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3138 goto return_results;
3143 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3144 if ( leftmost_dc == -1 ) {
3150 #endif /* DC_IN_UFN */
3153 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3154 rc = LDAP_NO_MEMORY;
3159 if ( leftmost_dc == -1 ) {
3160 #endif /* DC_IN_UFN */
3161 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3163 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3165 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3169 goto return_results;
3175 * trim the last ', ' (the allocated memory
3176 * is two bytes longer than required)
3178 ( *str )[ len - 2 ] = '\0';
3181 last_iRDN = iRDN - 1;
3183 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3185 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3187 if ( rdn2UFNstr( rdn, &( *str )[ l ],
3191 goto return_results;
3196 if ( !dn2domain( dn, &( *str )[ l ], &last_iRDN ) ) {
3199 goto return_results;
3202 /* the string is correctly terminated by dn2domain */
3204 #endif /* DC_IN_UFN */
3210 case LDAP_DN_FORMAT_DCE:
3212 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3214 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3216 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3217 goto return_results;
3223 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3224 rc = LDAP_NO_MEMORY;
3228 for ( l = 0; iRDN--; ) {
3230 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3232 if ( rdn2DCEstr( rdn, &( *str )[ l ], flags,
3236 goto return_results;
3243 ( *str )[ len ] = '\0';
3248 case LDAP_DN_FORMAT_AD_CANONICAL: {
3251 * Sort of UFN for DCE DNs: a slash ('/') separated
3252 * global->local DN with no types; strictly speaking,
3253 * the naming context should be a domain, which is
3254 * written in DNS-style, e.g. dot-deparated.
3258 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3262 * "microsoft.com/People/Bill,Gates"
3264 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3266 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3268 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3269 goto return_results;
3275 if ( ( *str = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3276 rc = LDAP_NO_MEMORY;
3281 if ( iRDN && dn2domain( dn, *str, &iRDN ) ) {
3282 for ( l = strlen( *str ); iRDN >= 0 ; iRDN-- ) {
3284 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3286 if ( rdn2ADstr( rdn, &( *str )[ l ],
3287 flags, &rdnl, 0 ) ) {
3290 goto return_results;
3299 * Strictly speaking, AD canonical requires
3300 * a DN to be in the form "..., dc=smtg",
3301 * i.e. terminated by a domain component
3303 if ( flags & LDAP_DN_PEDANTIC ) {
3306 rc = LDAP_INVALID_DN_SYNTAX;
3310 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3312 LDAPRDN *rdn = dn[ iRDN ][ 0 ];
3314 if ( rdn2ADstr( rdn, &( *str )[ l ],
3315 flags, &rdnl, first ) ) {
3318 goto return_results;
3327 ( *str )[ len ] = '\0';
3334 return( LDAP_INVALID_DN_SYNTAX );
3338 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2str(%s,%u)=%d\n", *str, flags, rc );