3 * Copyright 1998-2002 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>
18 #include <ac/socket.h>
19 #include <ac/string.h>
23 #include "ldap_schema.h"
25 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
26 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
30 /* parsing/printing routines */
31 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
32 const char **next, unsigned flags, unsigned *retFlags );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2bin( const char *str, char *c );
42 static int byte2hexpair( const char *val, char *pair );
43 static int binval2hexstr( struct berval *val, char *str );
44 static int strval2strlen( struct berval *val, unsigned flags,
46 static int strval2str( struct berval *val, char *str, unsigned flags,
48 static int strval2IA5strlen( struct berval *val, unsigned flags,
50 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
52 static int strval2DCEstrlen( struct berval *val, unsigned flags,
54 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
56 static int strval2ADstrlen( struct berval *val, unsigned flags,
58 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
60 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
63 static LDAPAVA * ldapava_new(
64 const struct berval *attr, const struct berval *val, unsigned flags );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
88 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
94 assert( LDAP_VALID(ld) );
95 assert( entry != NULL );
97 tmp = *entry->lm_ber; /* struct copy */
98 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
99 ld->ld_errno = LDAP_DECODING_ERROR;
107 * RFC 1823 ldap_dn2ufn
110 ldap_dn2ufn( LDAP_CONST char *dn )
115 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
117 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
120 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
121 &out, LDAP_DN_FORMAT_UFN );
127 * RFC 1823 ldap_explode_dn
130 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
133 char **values = NULL;
135 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
138 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
140 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
143 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
148 if( tmpDN == NULL ) {
149 values = LDAP_MALLOC( sizeof( char * ) );
150 if( values == NULL ) return NULL;
156 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
158 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
159 if ( values == NULL ) {
160 ldap_dnfree( tmpDN );
164 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
165 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
167 ldap_dnfree( tmpDN );
168 values[ iRDN ] = NULL;
174 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
177 char **values = NULL;
182 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
184 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
188 * we only parse the first rdn
189 * FIXME: we prefer efficiency over checking if the _ENTIRE_
192 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
197 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
198 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
199 if ( values == NULL ) {
200 ldap_rdnfree( tmpRDN );
204 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
205 ber_len_t l = 0, vl, al = 0;
207 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
209 if ( ava->la_flags == LDAP_AVA_BINARY ) {
210 vl = 1 + 2 * ava->la_value.bv_len;
213 if ( strval2strlen( &ava->la_value,
214 ava->la_flags, &vl ) ) {
220 al = ava->la_attr.bv_len;
221 l = vl + ava->la_attr.bv_len + 1;
223 str = LDAP_MALLOC( l + 1 );
224 AC_MEMCPY( str, ava->la_attr.bv_val,
225 ava->la_attr.bv_len );
230 str = LDAP_MALLOC( l + 1 );
233 if ( ava->la_flags == LDAP_AVA_BINARY ) {
235 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
240 if ( strval2str( &ava->la_value, &str[ al ],
241 ava->la_flags, &vl ) ) {
247 values[ iAVA ] = str;
249 values[ iAVA ] = NULL;
251 ldap_rdnfree( tmpRDN );
256 LBER_VFREE( values );
257 ldap_rdnfree( tmpRDN );
262 ldap_dn2dcedn( LDAP_CONST char *dn )
267 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
269 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
272 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
273 &out, LDAP_DN_FORMAT_DCE );
279 ldap_dcedn2dn( LDAP_CONST char *dce )
284 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
286 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
289 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
295 ldap_dn2ad_canonical( LDAP_CONST char *dn )
300 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
302 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
305 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
306 &out, LDAP_DN_FORMAT_AD_CANONICAL );
312 * function that changes the string representation of dnin
313 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
316 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
317 * plus some rfc 1779)
318 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
319 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
320 * LDAP_DN_FORMAT_DCE (?)
322 * fout can be any of the above except
323 * LDAP_DN_FORMAT_LDAP
325 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
326 * LDAP_DN_FORMAT_AD_CANONICAL (?)
329 ldap_dn_normalize( LDAP_CONST char *dnin,
330 unsigned fin, char **dnout, unsigned fout )
333 LDAPDN *tmpDN = NULL;
336 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
338 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
345 if ( dnin == NULL ) {
346 return( LDAP_SUCCESS );
349 rc = ldap_str2dn( dnin , &tmpDN, fin );
350 if ( rc != LDAP_SUCCESS ) {
354 rc = ldap_dn2str( tmpDN, dnout, fout );
356 ldap_dnfree( tmpDN );
364 /* #define B4ATTRTYPE 0x0001 */
365 #define B4OIDATTRTYPE 0x0002
366 #define B4STRINGATTRTYPE 0x0003
368 #define B4AVAEQUALS 0x0100
369 #define B4AVASEP 0x0200
370 #define B4RDNSEP 0x0300
371 #define GOTAVA 0x0400
373 #define B4ATTRVALUE 0x0010
374 #define B4STRINGVALUE 0x0020
375 #define B4IA5VALUEQUOTED 0x0030
376 #define B4IA5VALUE 0x0040
377 #define B4BINARYVALUE 0x0050
380 * Helpers (mostly from slap.h)
381 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
382 * Macros assume "C" Locale (ASCII)
384 #define LDAP_DN_ASCII_SPACE(c) \
385 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
386 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
387 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
388 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
390 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
391 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
392 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
393 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
394 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
395 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
398 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
399 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
400 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
401 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
402 #define LDAP_DN_ATTRDESC_CHAR(c) \
403 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
405 /* special symbols */
406 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
407 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
408 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
409 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
410 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
411 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
412 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
413 #define LDAP_DN_VALUE_END(c) \
414 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
415 #define LDAP_DN_NE(c) \
416 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
417 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
418 #define LDAP_DN_MAYESCAPE(c) \
419 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
420 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
421 #define LDAP_DN_NEEDESCAPE(c) \
422 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
423 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
424 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
425 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
426 #define LDAP_DN_WILLESCAPE_CHAR(c) \
427 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
428 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
429 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
430 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
433 #define LDAP_DN_VALUE_END_V2(c) \
434 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
436 #define LDAP_DN_V2_SPECIAL(c) \
437 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
438 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
439 || LDAP_DN_OCTOTHORPE(c) )
440 #define LDAP_DN_V2_PAIR(c) \
441 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
444 * DCE (mostly from Luke Howard and IBM implementation for AIX)
446 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
447 * Here escapes and valid chars for GDS are considered; as soon as more
448 * specific info is found, the macros will be updated.
450 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
451 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
453 * Metachars: '/', ',', '=', '\'.
455 * the '\' is used to escape other metachars.
461 * Attribute types must start with alphabetic chars and can contain
462 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
464 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
465 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
466 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
467 #define LDAP_DN_VALUE_END_DCE(c) \
468 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
469 #define LDAP_DN_NEEDESCAPE_DCE(c) \
470 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
473 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
474 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
475 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
476 #define LDAP_DN_VALUE_END_AD(c) \
477 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
478 #define LDAP_DN_NEEDESCAPE_AD(c) \
479 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
482 #define LDAP_DN_HEXPAIR(s) \
483 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
484 /* better look at the AttributeDescription? */
486 /* FIXME: no composite rdn or non-"dc" types, right?
487 * (what about "dc" in OID form?) */
488 /* FIXME: we do not allow binary values in domain, right? */
489 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
490 /* NOTE: don't use strcasecmp() as it is locale specific! */
491 #define LDAP_DC_ATTR "dc"
492 #define LDAP_DC_ATTRU "DC"
493 #define LDAP_DN_IS_RDN_DC( r ) \
494 ( (r) && (r)[0][0] && !(r)[0][1] \
495 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
496 && ((r)[0][0]->la_attr.bv_len == 2) \
497 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
498 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
499 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
500 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
502 /* Composite rules */
503 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
504 ( LDAP_DN_LDAPV2(f) \
505 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
506 #define LDAP_DN_ALLOW_SPACES(f) \
507 ( LDAP_DN_LDAPV2(f) \
508 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
509 #define LDAP_DN_LDAP(f) \
510 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
511 #define LDAP_DN_LDAPV3(f) \
512 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
513 #define LDAP_DN_LDAPV2(f) \
514 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
515 #define LDAP_DN_DCE(f) \
516 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
517 #define LDAP_DN_UFN(f) \
518 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
519 #define LDAP_DN_ADC(f) \
520 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
521 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
524 * LDAPAVA helpers (will become part of the API for operations
525 * on structural representations of DNs).
528 ldapava_new( const struct berval *attr, const struct berval *val,
536 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
538 /* should we test it? */
543 ava->la_attr.bv_len = attr->bv_len;
544 ava->la_attr.bv_val = (char *)(ava+1);
545 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
546 ava->la_attr.bv_val[attr->bv_len] = '\0';
548 ava->la_value = *val;
549 ava->la_flags = flags;
551 ava->la_private = NULL;
557 ldap_avafree( LDAPAVA *ava )
562 /* ava's private must be freed by caller
563 * (at present let's skip this check because la_private
564 * basically holds static data) */
565 assert( ava->la_private == NULL );
569 /* la_attr is now contiguous with ava, not freed separately */
570 LDAP_FREE( ava->la_attr.bv_val );
572 LDAP_FREE( ava->la_value.bv_val );
578 ldap_rdnfree( LDAPRDN *rdn )
586 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
587 ldap_avafree( rdn[ 0 ][ iAVA ] );
594 ldap_dnfree( LDAPDN *dn )
602 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
603 ldap_rdnfree( dn[ 0 ][ iRDN ] );
610 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
611 * into a structural representation of the DN, by separating attribute
612 * types and values encoded in the more appropriate form, which is
613 * string or OID for attribute types and binary form of the BER encoded
614 * value or Unicode string. Formats different from LDAPv3 are parsed
615 * according to their own rules and turned into the more appropriate
616 * form according to LDAPv3.
618 * NOTE: I realize the code is getting spaghettish; it is rather
619 * experimental and will hopefully turn into something more simple
620 * and readable as soon as it works as expected.
624 * Default sizes of AVA and RDN static working arrays; if required
625 * the are dynamically resized. The values can be tuned in case
626 * of special requirements (e.g. very deep DN trees or high number
629 #define TMP_AVA_SLOTS 8
630 #define TMP_RDN_SLOTS 32
633 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
639 bv.bv_len = strlen( str );
640 bv.bv_val = (char *) str;
642 return ldap_bv2dn( &bv, dn, flags );
646 ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
649 int rc = LDAP_DECODING_ERROR;
652 LDAPDN *newDN = NULL;
653 LDAPRDN *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_;
654 int num_slots = TMP_RDN_SLOTS;
655 char *str = bv->bv_val;
656 char *end = str + bv->bv_len;
659 assert( bv->bv_val );
663 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
665 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
670 switch ( LDAP_DN_FORMAT( flags ) ) {
671 case LDAP_DN_FORMAT_LDAP:
672 case LDAP_DN_FORMAT_LDAPV3:
673 case LDAP_DN_FORMAT_LDAPV2:
674 case LDAP_DN_FORMAT_DCE:
677 /* unsupported in str2dn */
678 case LDAP_DN_FORMAT_UFN:
679 case LDAP_DN_FORMAT_AD_CANONICAL:
680 return LDAP_PARAM_ERROR;
682 case LDAP_DN_FORMAT_LBER:
684 return LDAP_PARAM_ERROR;
687 if ( bv->bv_len == 0 ) {
691 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
692 /* value must have embedded NULs */
693 return LDAP_DECODING_ERROR;
697 if ( LDAP_DN_DCE( flags ) ) {
700 * (from Luke Howard: thnx) A RDN separator is required
701 * at the beginning of an (absolute) DN.
703 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
709 * actually we do not want to accept by default the DCE form,
710 * we do not want to auto-detect it
713 } else if ( LDAP_DN_LDAP( flags ) ) {
715 * if dn starts with '/' let's make it a DCE dn
717 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
718 flags |= LDAP_DN_FORMAT_DCE;
724 for ( ; p < end; p++ ) {
727 tmpbv.bv_len = bv->bv_len - ( p - str );
728 tmpbv.bv_val = (char *)p;
730 err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags );
731 if ( err != LDAP_SUCCESS ) {
736 * We expect a rdn separator
738 if ( p < end && p[ 0 ] ) {
739 switch ( LDAP_DN_FORMAT( flags ) ) {
740 case LDAP_DN_FORMAT_LDAPV3:
741 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
742 rc = LDAP_DECODING_ERROR;
747 case LDAP_DN_FORMAT_LDAP:
748 case LDAP_DN_FORMAT_LDAPV2:
749 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
750 rc = LDAP_DECODING_ERROR;
755 case LDAP_DN_FORMAT_DCE:
756 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
757 rc = LDAP_DECODING_ERROR;
765 tmpDN[nrdns++] = newRDN;
769 * make the static RDN array dynamically rescalable
771 if ( nrdns == num_slots ) {
774 if ( tmpDN == tmpDN_ ) {
775 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
780 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
783 tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
794 if ( p >= end || p[ 0 ] == '\0' ) {
796 * the DN is over, phew
798 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
799 sizeof(LDAPRDN *) * (nrdns+1));
800 if ( newDN == NULL ) {
806 newDN[0] = (LDAPRDN **)(newDN+1);
808 if ( LDAP_DN_DCE( flags ) ) {
809 /* add in reversed order */
810 for ( i=0; i<nrdns; i++ )
811 newDN[0][i] = tmpDN[nrdns-1-i];
813 for ( i=0; i<nrdns; i++ )
814 newDN[0][i] = tmpDN[i];
816 newDN[0][nrdns] = NULL;
825 ldap_rdnfree( newRDN );
828 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
829 ldap_rdnfree( tmpDN[nrdns] );
834 if ( tmpDN != tmpDN_ ) {
839 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
842 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
852 * Parses a relative DN according to flags up to a rdn separator
853 * or to the end of str.
854 * Returns the rdn and a pointer to the string continuation, which
855 * corresponds to the rdn separator or to '\0' in case the string is over.
858 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
859 char **n_in, unsigned flags )
864 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
866 bv.bv_len = strlen( str );
867 bv.bv_val = (char *) str;
869 return ldap_bv2rdn( &bv, rdn, n_in, flags );
873 ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
874 char **n_in, unsigned flags )
876 const char **n = (const char **) n_in;
880 int rc = LDAP_DECODING_ERROR;
881 int attrTypeEncoding = LDAP_AVA_STRING,
882 attrValueEncoding = LDAP_AVA_STRING;
884 struct berval attrType = { 0, NULL };
885 struct berval attrValue = { 0, NULL };
887 LDAPRDN *newRDN = NULL;
888 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
889 int num_slots = TMP_AVA_SLOTS;
895 assert( bv->bv_len );
896 assert( bv->bv_val );
897 assert( rdn || flags & LDAP_DN_SKIP );
901 stoplen = bv->bv_len;
908 switch ( LDAP_DN_FORMAT( flags ) ) {
909 case LDAP_DN_FORMAT_LDAP:
910 case LDAP_DN_FORMAT_LDAPV3:
911 case LDAP_DN_FORMAT_LDAPV2:
912 case LDAP_DN_FORMAT_DCE:
915 /* unsupported in str2dn */
916 case LDAP_DN_FORMAT_UFN:
917 case LDAP_DN_FORMAT_AD_CANONICAL:
918 return LDAP_PARAM_ERROR;
920 case LDAP_DN_FORMAT_LBER:
922 return LDAP_PARAM_ERROR;
925 if ( bv->bv_len == 0 ) {
930 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
931 /* value must have embedded NULs */
932 return LDAP_DECODING_ERROR;
936 for ( ; p[ 0 ] || state == GOTAVA; ) {
939 * The parser in principle advances one token a time,
940 * or toggles state if preferable.
945 * an AttributeType can be encoded as:
946 * - its string representation; in detail, implementations
947 * MUST recognize AttributeType string type names listed
948 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
949 * MAY recognize other names.
950 * - its numeric OID (a dotted decimal string); in detail
951 * RFC 2253 asserts that ``Implementations MUST allow
952 * an oid in the attribute type to be prefixed by one
953 * of the character strings "oid." or "OID."''. As soon
954 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
955 * I'm not sure whether this is required or not any
956 * longer; to be liberal, we still implement it.
959 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
960 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
967 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
968 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
973 /* whitespace is allowed (and trimmed) */
975 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
980 /* error: we expected an AVA */
986 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
987 state = B4OIDATTRTYPE;
991 /* else must be alpha */
992 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
996 /* LDAPv2 "oid." prefix */
997 if ( LDAP_DN_LDAPV2( flags ) ) {
999 * to be overly pedantic, we only accept
1002 if ( flags & LDAP_DN_PEDANTIC ) {
1003 if ( !strncmp( p, "OID.", 4 )
1004 || !strncmp( p, "oid.", 4 ) ) {
1006 state = B4OIDATTRTYPE;
1010 if ( !strncasecmp( p, "oid.", 4 ) ) {
1012 state = B4OIDATTRTYPE;
1018 state = B4STRINGATTRTYPE;
1021 case B4OIDATTRTYPE: {
1022 int err = LDAP_SUCCESS;
1024 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1027 if ( err != LDAP_SUCCESS ) {
1030 attrType.bv_len = p - attrType.bv_val;
1032 attrTypeEncoding = LDAP_AVA_BINARY;
1034 state = B4AVAEQUALS;
1038 case B4STRINGATTRTYPE: {
1039 const char *startPos, *endPos = NULL;
1043 * the starting char has been found to be
1044 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1045 * FIXME: DCE attr types seem to have a more
1046 * restrictive syntax (no '-' ...)
1048 for ( startPos = p++; p[ 0 ]; p++ ) {
1049 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1053 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1056 * RFC 2253 does not explicitly
1057 * allow lang extensions to attribute
1060 if ( flags & LDAP_DN_PEDANTIC ) {
1065 * we trim ';' and following lang
1066 * and so from attribute types
1069 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1070 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1078 len = ( endPos ? endPos : p ) - startPos;
1083 attrTypeEncoding = LDAP_AVA_STRING;
1086 * here we need to decide whether to use it as is
1087 * or turn it in OID form; as a consequence, we
1088 * need to decide whether to binary encode the value
1091 state = B4AVAEQUALS;
1093 if ( flags & LDAP_DN_SKIP ) {
1097 attrType.bv_val = (char *)startPos;
1098 attrType.bv_len = len;
1104 /* spaces may not be allowed */
1105 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1106 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1111 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1116 /* need equal sign */
1117 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1122 /* spaces may not be allowed */
1123 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1124 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1129 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1135 * octothorpe means a BER encoded value will follow
1136 * FIXME: I don't think DCE will allow it
1138 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1140 attrValueEncoding = LDAP_AVA_BINARY;
1141 state = B4BINARYVALUE;
1145 /* STRING value expected */
1148 * if we're pedantic, an attribute type in OID form
1149 * SHOULD imply a BER encoded attribute value; we
1150 * should at least issue a warning
1152 if ( ( flags & LDAP_DN_PEDANTIC )
1153 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1154 /* OID attrType SHOULD use binary encoding */
1158 attrValueEncoding = LDAP_AVA_STRING;
1161 * LDAPv2 allows the attribute value to be quoted;
1162 * also, IA5 values are expected, in principle
1164 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1165 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1167 state = B4IA5VALUEQUOTED;
1171 if ( LDAP_DN_LDAPV2( flags ) ) {
1178 * here STRING means RFC 2253 string
1179 * FIXME: what about DCE strings?
1185 state = B4STRINGVALUE;
1190 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1198 switch ( LDAP_DN_FORMAT( flags ) ) {
1199 case LDAP_DN_FORMAT_LDAP:
1200 case LDAP_DN_FORMAT_LDAPV3:
1201 if ( str2strval( p, stoplen - ( p - str ),
1202 &attrValue, &p, flags,
1203 &attrValueEncoding ) ) {
1208 case LDAP_DN_FORMAT_DCE:
1209 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1222 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1229 case B4IA5VALUEQUOTED:
1231 /* lead quote already stripped */
1232 if ( quotedIA52strval( p, &attrValue,
1243 if ( !( flags & LDAP_DN_SKIP ) ) {
1247 * we accept empty values
1249 ava = ldapava_new( &attrType, &attrValue,
1250 attrValueEncoding );
1252 if ( ava == NULL ) {
1253 rc = LDAP_NO_MEMORY;
1256 tmpRDN[navas++] = ava;
1258 attrValue.bv_val = NULL;
1259 attrValue.bv_len = 0;
1262 * prepare room for new AVAs if needed
1264 if (navas == num_slots) {
1267 if ( tmpRDN == tmpRDN_ ) {
1268 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
1269 if ( tmp == NULL ) {
1270 rc = LDAP_NO_MEMORY;
1273 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1276 tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
1277 if ( tmp == NULL ) {
1278 rc = LDAP_NO_MEMORY;
1289 * if we got an AVA separator ('+', or ',' for DCE )
1290 * we expect a new AVA for this RDN; otherwise
1291 * we add the RDN to the DN
1293 switch ( LDAP_DN_FORMAT( flags ) ) {
1294 case LDAP_DN_FORMAT_LDAP:
1295 case LDAP_DN_FORMAT_LDAPV3:
1296 case LDAP_DN_FORMAT_LDAPV2:
1297 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1302 case LDAP_DN_FORMAT_DCE:
1303 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1311 * the RDN is over, phew
1314 if ( !( flags & LDAP_DN_SKIP ) ) {
1315 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1316 + sizeof(LDAPAVA *) * (navas+1) );
1317 if ( newRDN == NULL ) {
1318 rc = LDAP_NO_MEMORY;
1323 newRDN[0] = (LDAPAVA**)(newRDN+1);
1325 for (i=0; i<navas; i++)
1326 newRDN[0][i] = tmpRDN[i];
1327 newRDN[0][i] = NULL;
1335 /* they should have been used in an AVA */
1336 attrType.bv_val = NULL;
1337 attrValue.bv_val = NULL;
1352 /* They are set to NULL after they're used in an AVA */
1354 if ( attrValue.bv_val ) {
1355 free( attrValue.bv_val );
1358 for ( navas-- ; navas >= 0; navas-- ) {
1359 ldap_avafree( tmpRDN[navas] );
1364 if ( tmpRDN != tmpRDN_ ) {
1365 LDAP_FREE( tmpRDN );
1376 * reads in a UTF-8 string value, unescaping stuff:
1377 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1378 * '\' + HEXPAIR(p) -> unhex(p)
1381 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1383 const char *p, *end, *startPos, *endPos = NULL;
1384 ber_len_t len, escapes;
1391 end = str + stoplen;
1392 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1393 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1395 if ( p[ 0 ] == '\0' ) {
1398 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1403 if ( LDAP_DN_HEXPAIR( p ) ) {
1406 hexstr2bin( p, &c );
1409 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1412 * we assume the string is UTF-8
1414 *retFlags = LDAP_AVA_NONPRINTABLE;
1421 if ( LDAP_DN_PEDANTIC & flags ) {
1425 * we do not allow escaping
1426 * of chars that don't need
1427 * to and do not belong to
1432 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1433 if ( p[ 0 ] == '\0' ) {
1436 *retFlags = LDAP_AVA_NONPRINTABLE;
1438 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1439 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1442 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1444 * FIXME: maybe we can add
1445 * escapes if not pedantic?
1452 * we do allow unescaped spaces at the end
1453 * of the value only in non-pedantic mode
1455 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1456 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1457 if ( flags & LDAP_DN_PEDANTIC ) {
1461 /* strip trailing (unescaped) spaces */
1462 for ( endPos = p - 1;
1463 endPos > startPos + 1 &&
1464 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1465 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1472 if ( flags & LDAP_DN_SKIP ) {
1477 * FIXME: test memory?
1479 len = ( endPos ? endPos : p ) - startPos - escapes;
1482 if ( escapes == 0 ) {
1483 if ( *retFlags == LDAP_AVA_NONPRINTABLE ) {
1484 val->bv_val = LDAP_MALLOC( len + 1 );
1485 AC_MEMCPY( val->bv_val, startPos, len );
1486 val->bv_val[ len ] = '\0';
1488 val->bv_val = LDAP_STRNDUP( startPos, len );
1494 val->bv_val = LDAP_MALLOC( len + 1 );
1495 for ( s = 0, d = 0; d < len; ) {
1496 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1498 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1499 val->bv_val[ d++ ] =
1502 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1505 hexstr2bin( &startPos[ s ], &c );
1506 val->bv_val[ d++ ] = c;
1510 /* we should never get here */
1515 val->bv_val[ d++ ] = startPos[ s++ ];
1519 val->bv_val[ d ] = '\0';
1527 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1529 const char *p, *startPos, *endPos = NULL;
1530 ber_len_t len, escapes;
1538 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1539 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1541 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1548 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1553 * FIXME: can we accept anything else? I guess we need
1554 * to stop if a value is not legal
1559 * (unescaped) trailing spaces are trimmed must be silently ignored;
1562 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1563 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1564 if ( flags & LDAP_DN_PEDANTIC ) {
1568 /* strip trailing (unescaped) spaces */
1569 for ( endPos = p - 1;
1570 endPos > startPos + 1 &&
1571 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1572 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1579 if ( flags & LDAP_DN_SKIP ) {
1583 len = ( endPos ? endPos : p ) - startPos - escapes;
1585 if ( escapes == 0 ){
1586 val->bv_val = LDAP_STRNDUP( startPos, len );
1591 val->bv_val = LDAP_MALLOC( len + 1 );
1592 for ( s = 0, d = 0; d < len; ) {
1594 * This point is reached only if escapes
1595 * are properly used, so all we need to
1598 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1602 val->bv_val[ d++ ] = startPos[ s++ ];
1604 val->bv_val[ d ] = '\0';
1605 assert( strlen( val->bv_val ) == len );
1612 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1614 const char *p, *startPos, *endPos = NULL;
1615 ber_len_t len, escapes;
1627 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1628 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1630 if ( p[ 0 ] == '\0' ) {
1634 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1635 && ( LDAP_DN_PEDANTIC & flags ) ) {
1640 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1645 * FIXME: can we accept anything else? I guess we need
1646 * to stop if a value is not legal
1650 /* strip trailing (unescaped) spaces */
1652 endPos > startPos + 1 &&
1653 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1654 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1660 if ( flags & LDAP_DN_SKIP ) {
1664 len = ( endPos ? endPos : p ) - startPos - escapes;
1666 if ( escapes == 0 ) {
1667 val->bv_val = LDAP_STRNDUP( startPos, len );
1672 val->bv_val = LDAP_MALLOC( len + 1 );
1673 for ( s = 0, d = 0; d < len; ) {
1674 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1677 val->bv_val[ d++ ] = startPos[ s++ ];
1679 val->bv_val[ d ] = '\0';
1680 assert( strlen( val->bv_val ) == len );
1687 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1689 const char *p, *startPos, *endPos = NULL;
1691 unsigned escapes = 0;
1699 /* initial quote already eaten */
1700 for ( startPos = p = str; p[ 0 ]; p++ ) {
1702 * According to RFC 1779, the quoted value can
1703 * contain escaped as well as unescaped special values;
1704 * as a consequence we tolerate escaped values
1705 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1706 * (e.g. '","' -> '\,').
1708 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1709 if ( p[ 1 ] == '\0' ) {
1714 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1715 && ( LDAP_DN_PEDANTIC & flags ) ) {
1717 * do we allow to escape normal chars?
1718 * LDAPv2 does not allow any mechanism
1719 * for escaping chars with '\' and hex
1726 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1728 /* eat closing quotes */
1734 * FIXME: can we accept anything else? I guess we need
1735 * to stop if a value is not legal
1739 if ( endPos == NULL ) {
1743 /* Strip trailing (unescaped) spaces */
1744 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1749 if ( flags & LDAP_DN_SKIP ) {
1753 len = endPos - startPos - escapes;
1754 assert( endPos >= startPos + escapes );
1756 if ( escapes == 0 ) {
1757 val->bv_val = LDAP_STRNDUP( startPos, len );
1762 val->bv_val = LDAP_MALLOC( len + 1 );
1765 for ( s = d = 0; d < len; ) {
1766 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1769 val->bv_val[ d++ ] = str[ s++ ];
1771 val->bv_val[ d ] = '\0';
1772 assert( strlen( val->bv_val ) == len );
1779 hexstr2bin( const char *str, char *c )
1789 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1793 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1796 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1803 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1807 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1808 *c += c2 - 'A' + 10;
1810 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1811 *c += c2 - 'a' + 10;
1819 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1821 const char *p, *startPos, *endPos = NULL;
1831 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1832 switch ( LDAP_DN_FORMAT( flags ) ) {
1833 case LDAP_DN_FORMAT_LDAPV3:
1834 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1839 case LDAP_DN_FORMAT_LDAP:
1840 case LDAP_DN_FORMAT_LDAPV2:
1841 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1846 case LDAP_DN_FORMAT_DCE:
1847 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1853 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1854 if ( flags & LDAP_DN_PEDANTIC ) {
1859 for ( ; p[ 0 ]; p++ ) {
1860 switch ( LDAP_DN_FORMAT( flags ) ) {
1861 case LDAP_DN_FORMAT_LDAPV3:
1862 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1867 case LDAP_DN_FORMAT_LDAP:
1868 case LDAP_DN_FORMAT_LDAPV2:
1869 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1874 case LDAP_DN_FORMAT_DCE:
1875 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1884 if ( !LDAP_DN_HEXPAIR( p ) ) {
1892 if ( flags & LDAP_DN_SKIP ) {
1896 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1898 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1901 val->bv_val = LDAP_MALLOC( len + 1 );
1902 if ( val->bv_val == NULL ) {
1903 return( LDAP_NO_MEMORY );
1906 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1909 hexstr2bin( &startPos[ s ], &c );
1911 val->bv_val[ d ] = c;
1914 val->bv_val[ d ] = '\0';
1920 * convert a byte in a hexadecimal pair
1923 byte2hexpair( const char *val, char *pair )
1925 static const char hexdig[] = "0123456789ABCDEF";
1931 * we assume the string has enough room for the hex encoding
1935 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1936 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1942 * convert a binary value in hexadecimal pairs
1945 binval2hexstr( struct berval *val, char *str )
1952 if ( val->bv_len == 0 ) {
1957 * we assume the string has enough room for the hex encoding
1961 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1962 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1969 * Length of the string representation, accounting for escaped hex
1973 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1975 ber_len_t l, cl = 1;
1977 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1978 #ifdef PRETTY_ESCAPE
1979 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1980 #endif /* PRETTY_ESCAPE */
1986 if ( val->bv_len == 0 ) {
1990 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
1995 if ( p[ 0 ] == '\0' ) {
2001 cl = LDAP_UTF8_CHARLEN2( p, cl );
2003 /* illegal utf-8 char! */
2006 } else if ( cl > 1 ) {
2009 for ( cnt = 1; cnt < cl; cnt++ ) {
2010 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2014 l += escaped_byte_len * cl;
2016 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2017 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2018 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2019 #ifdef PRETTY_ESCAPE
2021 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2023 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2027 * there might be some chars we want
2028 * to escape in form of a couple
2029 * of hexdigits for optimization purposes
2034 l += escaped_ascii_len;
2036 #else /* ! PRETTY_ESCAPE */
2038 #endif /* ! PRETTY_ESCAPE */
2051 * convert to string representation, escaping with hex the UTF-8 stuff;
2052 * assume the destination has enough room for escaping
2055 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2057 ber_len_t s, d, end;
2063 if ( val->bv_len == 0 ) {
2069 * we assume the string has enough room for the hex encoding
2072 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2078 if ( val->bv_val[ s ] == '\0' ) {
2088 * The length was checked in strval2strlen();
2089 * LDAP_UTF8_CHARLEN() should suffice
2091 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2095 * there might be some chars we want to escape in form
2096 * of a couple of hexdigits for optimization purposes
2098 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2099 #ifdef PRETTY_ESCAPE
2101 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2103 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2105 #else /* ! PRETTY_ESCAPE */
2106 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2107 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2108 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2110 #endif /* ! PRETTY_ESCAPE */
2114 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2119 } else if ( cl > 1 ) {
2121 str[ d++ ] = val->bv_val[ s++ ];
2125 #ifdef PRETTY_ESCAPE
2126 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2127 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2128 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2130 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2131 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2137 #endif /* PRETTY_ESCAPE */
2138 str[ d++ ] = val->bv_val[ s++ ];
2148 * Length of the IA5 string representation (no UTF-8 allowed)
2151 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2160 if ( val->bv_len == 0 ) {
2164 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2166 * Turn value into a binary encoded BER
2171 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2172 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2173 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2174 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2189 * convert to string representation (np UTF-8)
2190 * assume the destination has enough room for escaping
2193 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2195 ber_len_t s, d, end;
2201 if ( val->bv_len == 0 ) {
2206 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2208 * Turn value into a binary encoded BER
2215 * 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 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2221 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2222 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2225 str[ d++ ] = val->bv_val[ s++ ];
2235 * Length of the (supposedly) DCE string representation,
2236 * accounting for escaped hex of UTF-8 chars
2239 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2248 if ( val->bv_len == 0 ) {
2252 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2254 * FIXME: Turn the value into a binary encoded BER?
2259 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2260 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2275 * convert to (supposedly) DCE string representation,
2276 * escaping with hex the UTF-8 stuff;
2277 * assume the destination has enough room for escaping
2280 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2288 if ( val->bv_len == 0 ) {
2293 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2295 * FIXME: Turn the value into a binary encoded BER?
2303 * we assume the string has enough room for the hex encoding
2307 for ( s = 0, d = 0; s < val->bv_len; ) {
2308 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2311 str[ d++ ] = val->bv_val[ s++ ];
2321 * Length of the (supposedly) AD canonical string representation,
2322 * accounting for escaped hex of UTF-8 chars
2325 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2334 if ( val->bv_len == 0 ) {
2338 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2340 * FIXME: Turn the value into a binary encoded BER?
2345 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2346 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2361 * convert to (supposedly) AD string representation,
2362 * escaping with hex the UTF-8 stuff;
2363 * assume the destination has enough room for escaping
2366 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2374 if ( val->bv_len == 0 ) {
2379 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2381 * FIXME: Turn the value into a binary encoded BER?
2389 * we assume the string has enough room for the hex encoding
2393 for ( s = 0, d = 0; s < val->bv_len; ) {
2394 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2397 str[ d++ ] = val->bv_val[ s++ ];
2407 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2408 * the first part of the AD representation of the DN is written in DNS
2409 * form, i.e. dot separated domain name components (as suggested
2410 * by Luke Howard, http://www.padl.com/~lukeh)
2413 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2416 int domain = 0, first = 1;
2417 ber_len_t l = 1; /* we move the null also */
2420 /* we are guaranteed there's enough memory in str */
2426 assert( *iRDN >= 0 );
2428 str = bv->bv_val + pos;
2430 for ( i = *iRDN; i >= 0; i-- ) {
2434 assert( dn[ 0 ][ i ] );
2437 assert( rdn[ 0 ][ 0 ] );
2438 ava = rdn[ 0 ][ 0 ];
2440 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2448 AC_MEMCPY( str, ava->la_value.bv_val,
2449 ava->la_value.bv_len + 1);
2450 l += ava->la_value.bv_len;
2453 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2454 AC_MEMCPY( str, ava->la_value.bv_val,
2455 ava->la_value.bv_len );
2456 str[ ava->la_value.bv_len ] = '.';
2457 l += ava->la_value.bv_len + 1;
2462 bv->bv_len = pos + l - 1;
2468 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2469 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2476 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2477 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2479 /* len(type) + '=' + '+' | ',' */
2480 l += ava->la_attr.bv_len + 2;
2482 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2483 /* octothorpe + twice the length */
2484 l += 1 + 2 * ava->la_value.bv_len;
2488 unsigned f = flags | ava->la_flags;
2490 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2503 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2504 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2509 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2510 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2512 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2513 ava->la_attr.bv_len );
2514 l += ava->la_attr.bv_len;
2518 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2520 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2523 l += 2 * ava->la_value.bv_len;
2527 unsigned f = flags | ava->la_flags;
2529 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2534 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2543 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2550 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2551 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2553 /* len(type) + '=' + ',' | '/' */
2554 l += ava->la_attr.bv_len + 2;
2556 switch ( ava->la_flags ) {
2557 case LDAP_AVA_BINARY:
2558 /* octothorpe + twice the length */
2559 l += 1 + 2 * ava->la_value.bv_len;
2562 case LDAP_AVA_STRING: {
2564 unsigned f = flags | ava->la_flags;
2566 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2584 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2589 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2590 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2595 str[ l++ ] = ( iAVA ? ',' : '/' );
2598 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2599 ava->la_attr.bv_len );
2600 l += ava->la_attr.bv_len;
2604 switch ( ava->la_flags ) {
2605 case LDAP_AVA_BINARY:
2607 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2610 l += 2 * ava->la_value.bv_len;
2613 case LDAP_AVA_STRING: {
2615 unsigned f = flags | ava->la_flags;
2617 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2635 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2645 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2646 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2649 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2651 /* FIXME: are binary values allowed in UFN? */
2652 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2653 /* octothorpe + twice the value */
2654 l += 1 + 2 * ava->la_value.bv_len;
2658 unsigned f = flags | ava->la_flags;
2660 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2673 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2678 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2679 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2681 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2683 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2686 l += 2 * ava->la_value.bv_len;
2690 unsigned f = flags | ava->la_flags;
2692 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2698 if ( rdn[ 0 ][ iAVA + 1 ]) {
2699 AC_MEMCPY( &str[ l ], " + ", 3 );
2703 AC_MEMCPY( &str[ l ], ", ", 2 );
2714 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2724 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2725 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2730 /* FIXME: are binary values allowed in UFN? */
2731 switch ( ava->la_flags ) {
2732 case LDAP_AVA_BINARY:
2733 /* octothorpe + twice the value */
2734 l += 1 + 2 * ava->la_value.bv_len;
2737 case LDAP_AVA_STRING: {
2739 unsigned f = flags | ava->la_flags;
2741 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2759 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2764 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2765 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2770 str[ l++ ] = ( iAVA ? ',' : '/' );
2773 switch ( ava->la_flags ) {
2774 case LDAP_AVA_BINARY:
2776 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2779 l += 2 * ava->la_value.bv_len;
2782 case LDAP_AVA_STRING: {
2784 unsigned f = flags | ava->la_flags;
2786 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2806 * Returns in str a string representation of rdn based on flags.
2807 * There is some duplication of code between this and ldap_dn2str;
2808 * this is wanted to reduce the allocation of temporary buffers.
2811 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2818 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2819 return LDAP_PARAM_ERROR;
2822 rc = ldap_rdn2bv( rdn, &bv, flags );
2828 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2838 if ( rdn == NULL ) {
2839 bv->bv_val = LDAP_STRDUP( "" );
2840 return( LDAP_SUCCESS );
2844 * This routine wastes "back" bytes at the end of the string
2847 switch ( LDAP_DN_FORMAT( flags ) ) {
2848 case LDAP_DN_FORMAT_LDAPV3:
2849 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2850 return LDAP_DECODING_ERROR;
2854 case LDAP_DN_FORMAT_LDAPV2:
2855 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2856 return LDAP_DECODING_ERROR;
2860 case LDAP_DN_FORMAT_UFN:
2861 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2862 return LDAP_DECODING_ERROR;
2866 case LDAP_DN_FORMAT_DCE:
2867 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2868 return LDAP_DECODING_ERROR;
2872 case LDAP_DN_FORMAT_AD_CANONICAL:
2873 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2874 return LDAP_DECODING_ERROR;
2879 return LDAP_PARAM_ERROR;
2882 bv->bv_val = LDAP_MALLOC( l + 1 );
2884 switch ( LDAP_DN_FORMAT( flags ) ) {
2885 case LDAP_DN_FORMAT_LDAPV3:
2886 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2890 case LDAP_DN_FORMAT_LDAPV2:
2891 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2895 case LDAP_DN_FORMAT_UFN:
2896 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2900 case LDAP_DN_FORMAT_DCE:
2901 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2905 case LDAP_DN_FORMAT_AD_CANONICAL:
2906 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2911 /* need at least one of the previous */
2912 return LDAP_PARAM_ERROR;
2916 ldap_memfree( bv->bv_val );
2920 bv->bv_len = l - back;
2921 bv->bv_val[ bv->bv_len ] = '\0';
2923 return LDAP_SUCCESS;
2927 * Very bulk implementation; many optimizations can be performed
2928 * - a NULL dn results in an empty string ""
2931 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2932 * we must encode it in binary form ('#' + HEXPAIRs)
2933 * b) does DCE/AD support UTF-8?
2934 * no clue; don't think so.
2935 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2936 * use binary encoded BER
2938 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2945 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2946 return LDAP_PARAM_ERROR;
2949 rc = ldap_dn2bv( dn, &bv, flags );
2954 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2957 int rc = LDAP_ENCODING_ERROR;
2960 /* stringifying helpers for LDAPv3/LDAPv2 */
2961 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2962 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2969 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s",
2972 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2976 * a null dn means an empty dn string
2977 * FIXME: better raise an error?
2980 bv->bv_val = LDAP_STRDUP( "" );
2981 return( LDAP_SUCCESS );
2984 switch ( LDAP_DN_FORMAT( flags ) ) {
2985 case LDAP_DN_FORMAT_LDAPV3:
2986 sv2l = strval2strlen;
2990 case LDAP_DN_FORMAT_LDAPV2:
2991 sv2l = strval2IA5strlen;
2992 sv2s = strval2IA5str;
2995 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2997 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
2999 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3000 goto return_results;
3006 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3007 rc = LDAP_NO_MEMORY;
3011 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3013 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3015 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
3017 LDAP_FREE( bv->bv_val );
3019 goto return_results;
3027 * trim the last ',' (the allocated memory
3028 * is one byte longer than required)
3030 bv->bv_len = len - 1;
3031 bv->bv_val[ bv->bv_len ] = '\0';
3036 case LDAP_DN_FORMAT_UFN: {
3038 * FIXME: quoting from RFC 1781:
3040 To take a distinguished name, and generate a name of this format with
3041 attribute types omitted, the following steps are followed.
3043 1. If the first attribute is of type CommonName, the type may be
3046 2. If the last attribute is of type Country, the type may be
3049 3. If the last attribute is of type Country, the last
3050 Organisation attribute may have the type omitted.
3052 4. All attributes of type OrganisationalUnit may have the type
3053 omitted, unless they are after an Organisation attribute or
3054 the first attribute is of type OrganisationalUnit.
3056 * this should be the pedantic implementation.
3058 * Here the standard implementation reflects
3059 * the one historically provided by OpenLDAP
3060 * (and UMIch, I presume), with the variant
3061 * of spaces and plusses (' + ') separating
3064 * A non-standard but nice implementation could
3065 * be to turn the final "dc" attributes into a
3066 * dot-separated domain.
3068 * Other improvements could involve the use of
3069 * friendly country names and so.
3072 int leftmost_dc = -1;
3074 #endif /* DC_IN_UFN */
3076 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3078 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3080 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3081 goto return_results;
3086 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3087 if ( leftmost_dc == -1 ) {
3093 #endif /* DC_IN_UFN */
3096 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3097 rc = LDAP_NO_MEMORY;
3102 if ( leftmost_dc == -1 ) {
3103 #endif /* DC_IN_UFN */
3104 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3106 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3108 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3110 LDAP_FREE( bv->bv_val );
3112 goto return_results;
3118 * trim the last ', ' (the allocated memory
3119 * is two bytes longer than required)
3121 bv->bv_len = len - 2;
3122 bv->bv_val[ bv->bv_len ] = '\0';
3125 last_iRDN = iRDN - 1;
3127 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3129 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3131 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3133 LDAP_FREE( bv->bv_val );
3135 goto return_results;
3140 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3141 LDAP_FREE( bv->bv_val );
3143 goto return_results;
3146 /* the string is correctly terminated by dn2domain */
3148 #endif /* DC_IN_UFN */
3154 case LDAP_DN_FORMAT_DCE:
3155 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3157 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3159 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3160 goto return_results;
3166 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3167 rc = LDAP_NO_MEMORY;
3171 for ( l = 0; iRDN--; ) {
3173 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3175 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3177 LDAP_FREE( bv->bv_val );
3179 goto return_results;
3187 bv->bv_val[ bv->bv_len ] = '\0';
3192 case LDAP_DN_FORMAT_AD_CANONICAL: {
3194 * Sort of UFN for DCE DNs: a slash ('/') separated
3195 * global->local DN with no types; strictly speaking,
3196 * the naming context should be a domain, which is
3197 * written in DNS-style, e.g. dot-deparated.
3201 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3205 * "microsoft.com/People/Bill,Gates"
3207 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3209 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3211 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3212 goto return_results;
3218 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3219 rc = LDAP_NO_MEMORY;
3224 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3225 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3227 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3229 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3230 flags, &rdnl, 0 ) ) {
3231 LDAP_FREE( bv->bv_val );
3233 goto return_results;
3242 * Strictly speaking, AD canonical requires
3243 * a DN to be in the form "..., dc=smtg",
3244 * i.e. terminated by a domain component
3246 if ( flags & LDAP_DN_PEDANTIC ) {
3247 LDAP_FREE( bv->bv_val );
3249 rc = LDAP_ENCODING_ERROR;
3253 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3255 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3257 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3258 flags, &rdnl, first ) ) {
3259 LDAP_FREE( bv->bv_val );
3261 goto return_results;
3271 bv->bv_val[ bv->bv_len ] = '\0';
3277 return LDAP_PARAM_ERROR;
3281 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3282 bv->bv_val, flags, rc );
3284 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3285 bv->bv_val, flags, rc );
3293 #include <openssl/x509.h>
3294 #include <openssl/err.h>
3296 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3297 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3298 * constructed DN will use numeric OIDs to identify attributeTypes,
3299 * and the func() will be invoked to rewrite the DN with the given
3302 * Otherwise the DN will use shortNames as defined in the OpenSSL
3305 * It's preferable to let slapd do the OID to attributeType mapping,
3306 * because the OpenSSL tables are known to have many typos in versions
3307 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3308 * so we're forced to use OpenSSL's mapping there.
3309 * -- Howard Chu 2002-04-18
3313 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3318 LDAPAVA *newAVA, *baseAVA;
3319 X509_NAME_ENTRY *ne;
3322 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3324 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3326 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3335 /* Get the number of AVAs. This is not necessarily the same as
3336 * the number of RDNs.
3338 navas = X509_NAME_entry_count( x509_name );
3340 /* Get the last element, to see how many RDNs there are */
3341 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3342 nrdns = ne->set + 1;
3344 /* Allocate the DN/RDN/AVA stuff as a single block */
3345 dnsize = sizeof(LDAPDN) + sizeof(LDAPRDN *) * (nrdns+1);
3346 dnsize += sizeof(LDAPRDN) * nrdns + sizeof(LDAPAVA *) * (navas+nrdns);
3347 dnsize += sizeof(LDAPAVA) * navas;
3348 if (dnsize > sizeof(ptrs)) {
3349 newDN = (LDAPDN *)LDAP_MALLOC( dnsize );
3350 if ( newDN == NULL )
3351 return LDAP_NO_MEMORY;
3353 newDN = (LDAPDN *)ptrs;
3356 newDN[0] = (LDAPRDN**)(newDN+1);
3357 newDN[0][nrdns] = NULL;
3358 newRDN = (LDAPRDN*)(newDN[0] + nrdns+1);
3359 newAVA = (LDAPAVA*)(newRDN + navas + nrdns*2);
3362 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3363 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3364 ne = X509_NAME_get_entry( x509_name, i );
3365 obj = X509_NAME_ENTRY_get_object( ne );
3366 str = X509_NAME_ENTRY_get_data( ne );
3368 /* If set changed, move to next RDN */
3369 if ( set != ne->set ) {
3370 /* If this is not the first time, end the
3371 * previous RDN and advance.
3374 newRDN[0][k] = NULL;
3375 newRDN = (LDAPRDN*)(newRDN[0]+k+1);
3377 newDN[0][j++] = newRDN;
3379 newRDN[0] = (LDAPAVA**)(newRDN+1);
3383 newAVA->la_private = NULL;
3384 newAVA->la_flags = LDAP_AVA_STRING;
3387 int n = OBJ_obj2nid( obj );
3391 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3392 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3394 get_oid: newAVA->la_attr.bv_val = oidptr;
3395 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3396 oidptr += newAVA->la_attr.bv_len + 1;
3397 oidrem -= newAVA->la_attr.bv_len + 1;
3399 /* Running out of OID buffer space? */
3401 if ( oidsize == 0 ) {
3402 oidsize = sizeof(oids) * 2;
3404 oidbuf = LDAP_MALLOC( oidsize );
3405 if ( oidbuf == NULL ) goto nomem;
3409 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3410 if ( oidbuf == NULL ) goto nomem;
3411 /* Buffer moved! Fix AVA pointers */
3412 if ( old != oidbuf ) {
3414 long dif = oidbuf - old;
3416 for (a=baseAVA; a<=newAVA; a++){
3417 if (a->la_attr.bv_val >= old &&
3418 a->la_attr.bv_val <= (old + oidsize))
3419 a->la_attr.bv_val += dif;
3422 oidptr = oidbuf + oidsize - oidrem;
3428 Val.bv_val = str->data;
3429 Val.bv_len = str->length;
3430 switch( str->type ) {
3431 case V_ASN1_UNIVERSALSTRING:
3432 /* This uses 32-bit ISO 10646-1 */
3433 csize = 4; goto to_utf8;
3434 case V_ASN1_BMPSTRING:
3435 /* This uses 16-bit ISO 10646-1 */
3436 csize = 2; goto to_utf8;
3437 case V_ASN1_T61STRING:
3438 /* This uses 8-bit, assume ISO 8859-1 */
3440 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3441 if (rc != LDAP_SUCCESS) goto nomem;
3442 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3444 case V_ASN1_UTF8STRING:
3445 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3446 /* This is already in UTF-8 encoding */
3447 case V_ASN1_IA5STRING:
3448 case V_ASN1_PRINTABLESTRING:
3449 /* These are always 7-bit strings */
3450 ber_dupbv( &newAVA->la_value, &Val );
3454 newRDN[0][k] = newAVA;
3458 newRDN[0][k] = NULL;
3461 rc = func( newDN, flags );
3462 if ( rc != LDAP_SUCCESS )
3466 rc = ldap_dn2bv( newDN, bv, LDAP_DN_FORMAT_LDAPV3 );
3469 for (;baseAVA < newAVA; baseAVA++) {
3470 LDAP_FREE( baseAVA->la_value.bv_val );
3474 LDAP_FREE( oidbuf );
3475 if ( newDN != (LDAPDN*) ptrs )
3479 #endif /* HAVE_TLS */