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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_get_dn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2ufn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_explode_dn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_explode_rdn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2dcedn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dcedn2dn\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2ad_canonical\n" ));
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 (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn_normalize\n" ));
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 (( "getdn", LDAP_LEVEL_ARGS, "ldap_bv2dn(%s,%u)\n%s",
666 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
671 switch ( LDAP_DN_FORMAT( flags ) ) {
672 case LDAP_DN_FORMAT_LDAP:
673 case LDAP_DN_FORMAT_LDAPV3:
674 case LDAP_DN_FORMAT_LDAPV2:
675 case LDAP_DN_FORMAT_DCE:
678 /* unsupported in str2dn */
679 case LDAP_DN_FORMAT_UFN:
680 case LDAP_DN_FORMAT_AD_CANONICAL:
681 return LDAP_PARAM_ERROR;
683 case LDAP_DN_FORMAT_LBER:
685 return LDAP_PARAM_ERROR;
688 if ( bv->bv_len == 0 ) {
692 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
693 /* value must have embedded NULs */
694 return LDAP_DECODING_ERROR;
698 if ( LDAP_DN_DCE( flags ) ) {
701 * (from Luke Howard: thnx) A RDN separator is required
702 * at the beginning of an (absolute) DN.
704 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
710 * actually we do not want to accept by default the DCE form,
711 * we do not want to auto-detect it
714 } else if ( LDAP_DN_LDAP( flags ) ) {
716 * if dn starts with '/' let's make it a DCE dn
718 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
719 flags |= LDAP_DN_FORMAT_DCE;
725 for ( ; p < end; p++ ) {
728 tmpbv.bv_len = bv->bv_len - ( p - str );
729 tmpbv.bv_val = (char *)p;
731 err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags );
732 if ( err != LDAP_SUCCESS ) {
737 * We expect a rdn separator
739 if ( p < end && p[ 0 ] ) {
740 switch ( LDAP_DN_FORMAT( flags ) ) {
741 case LDAP_DN_FORMAT_LDAPV3:
742 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
743 rc = LDAP_DECODING_ERROR;
748 case LDAP_DN_FORMAT_LDAP:
749 case LDAP_DN_FORMAT_LDAPV2:
750 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
751 rc = LDAP_DECODING_ERROR;
756 case LDAP_DN_FORMAT_DCE:
757 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
758 rc = LDAP_DECODING_ERROR;
766 tmpDN[nrdns++] = newRDN;
770 * make the static RDN array dynamically rescalable
772 if ( nrdns == num_slots ) {
775 if ( tmpDN == tmpDN_ ) {
776 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
781 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
784 tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
795 if ( p >= end || p[ 0 ] == '\0' ) {
797 * the DN is over, phew
799 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
800 sizeof(LDAPRDN *) * (nrdns+1));
801 if ( newDN == NULL ) {
807 newDN[0] = (LDAPRDN **)(newDN+1);
809 if ( LDAP_DN_DCE( flags ) ) {
810 /* add in reversed order */
811 for ( i=0; i<nrdns; i++ )
812 newDN[0][i] = tmpDN[nrdns-1-i];
814 for ( i=0; i<nrdns; i++ )
815 newDN[0][i] = tmpDN[i];
817 newDN[0][nrdns] = NULL;
826 ldap_rdnfree( newRDN );
829 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
830 ldap_rdnfree( tmpDN[nrdns] );
835 if ( tmpDN != tmpDN_ ) {
840 LDAP_LOG (( "getdn", LDAP_LEVEL_RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
843 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
853 * Parses a relative DN according to flags up to a rdn separator
854 * or to the end of str.
855 * Returns the rdn and a pointer to the string continuation, which
856 * corresponds to the rdn separator or to '\0' in case the string is over.
859 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
860 char **n_in, unsigned flags )
865 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
867 bv.bv_len = strlen( str );
868 bv.bv_val = (char *) str;
870 return ldap_bv2rdn( &bv, rdn, n_in, flags );
874 ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
875 char **n_in, unsigned flags )
877 const char **n = (const char **) n_in;
881 int rc = LDAP_DECODING_ERROR;
882 int attrTypeEncoding = LDAP_AVA_STRING,
883 attrValueEncoding = LDAP_AVA_STRING;
885 struct berval attrType = { 0, NULL };
886 struct berval attrValue = { 0, NULL };
888 LDAPRDN *newRDN = NULL;
889 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
890 int num_slots = TMP_AVA_SLOTS;
896 assert( bv->bv_len );
897 assert( bv->bv_val );
898 assert( rdn || flags & LDAP_DN_SKIP );
902 stoplen = bv->bv_len;
909 switch ( LDAP_DN_FORMAT( flags ) ) {
910 case LDAP_DN_FORMAT_LDAP:
911 case LDAP_DN_FORMAT_LDAPV3:
912 case LDAP_DN_FORMAT_LDAPV2:
913 case LDAP_DN_FORMAT_DCE:
916 /* unsupported in str2dn */
917 case LDAP_DN_FORMAT_UFN:
918 case LDAP_DN_FORMAT_AD_CANONICAL:
919 return LDAP_PARAM_ERROR;
921 case LDAP_DN_FORMAT_LBER:
923 return LDAP_PARAM_ERROR;
926 if ( bv->bv_len == 0 ) {
931 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
932 /* value must have embedded NULs */
933 return LDAP_DECODING_ERROR;
937 for ( ; p[ 0 ] || state == GOTAVA; ) {
940 * The parser in principle advances one token a time,
941 * or toggles state if preferable.
946 * an AttributeType can be encoded as:
947 * - its string representation; in detail, implementations
948 * MUST recognize AttributeType string type names listed
949 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
950 * MAY recognize other names.
951 * - its numeric OID (a dotted decimal string); in detail
952 * RFC 2253 asserts that ``Implementations MUST allow
953 * an oid in the attribute type to be prefixed by one
954 * of the character strings "oid." or "OID."''. As soon
955 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
956 * I'm not sure whether this is required or not any
957 * longer; to be liberal, we still implement it.
960 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
961 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
968 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
969 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
974 /* whitespace is allowed (and trimmed) */
976 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
981 /* error: we expected an AVA */
987 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
988 state = B4OIDATTRTYPE;
992 /* else must be alpha */
993 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
997 /* LDAPv2 "oid." prefix */
998 if ( LDAP_DN_LDAPV2( flags ) ) {
1000 * to be overly pedantic, we only accept
1003 if ( flags & LDAP_DN_PEDANTIC ) {
1004 if ( !strncmp( p, "OID.", 4 )
1005 || !strncmp( p, "oid.", 4 ) ) {
1007 state = B4OIDATTRTYPE;
1011 if ( !strncasecmp( p, "oid.", 4 ) ) {
1013 state = B4OIDATTRTYPE;
1019 state = B4STRINGATTRTYPE;
1022 case B4OIDATTRTYPE: {
1023 int err = LDAP_SUCCESS;
1025 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1028 if ( err != LDAP_SUCCESS ) {
1031 attrType.bv_len = p - attrType.bv_val;
1033 attrTypeEncoding = LDAP_AVA_BINARY;
1035 state = B4AVAEQUALS;
1039 case B4STRINGATTRTYPE: {
1040 const char *startPos, *endPos = NULL;
1044 * the starting char has been found to be
1045 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1046 * FIXME: DCE attr types seem to have a more
1047 * restrictive syntax (no '-' ...)
1049 for ( startPos = p++; p[ 0 ]; p++ ) {
1050 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1054 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1057 * RFC 2253 does not explicitly
1058 * allow lang extensions to attribute
1061 if ( flags & LDAP_DN_PEDANTIC ) {
1066 * we trim ';' and following lang
1067 * and so from attribute types
1070 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1071 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1079 len = ( endPos ? endPos : p ) - startPos;
1084 attrTypeEncoding = LDAP_AVA_STRING;
1087 * here we need to decide whether to use it as is
1088 * or turn it in OID form; as a consequence, we
1089 * need to decide whether to binary encode the value
1092 state = B4AVAEQUALS;
1094 if ( flags & LDAP_DN_SKIP ) {
1098 attrType.bv_val = (char *)startPos;
1099 attrType.bv_len = len;
1105 /* spaces may not be allowed */
1106 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1107 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1112 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1117 /* need equal sign */
1118 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1123 /* spaces may not be allowed */
1124 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1125 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1130 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1136 * octothorpe means a BER encoded value will follow
1137 * FIXME: I don't think DCE will allow it
1139 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1141 attrValueEncoding = LDAP_AVA_BINARY;
1142 state = B4BINARYVALUE;
1146 /* STRING value expected */
1149 * if we're pedantic, an attribute type in OID form
1150 * SHOULD imply a BER encoded attribute value; we
1151 * should at least issue a warning
1153 if ( ( flags & LDAP_DN_PEDANTIC )
1154 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1155 /* OID attrType SHOULD use binary encoding */
1159 attrValueEncoding = LDAP_AVA_STRING;
1162 * LDAPv2 allows the attribute value to be quoted;
1163 * also, IA5 values are expected, in principle
1165 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1166 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1168 state = B4IA5VALUEQUOTED;
1172 if ( LDAP_DN_LDAPV2( flags ) ) {
1179 * here STRING means RFC 2253 string
1180 * FIXME: what about DCE strings?
1186 state = B4STRINGVALUE;
1191 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1199 switch ( LDAP_DN_FORMAT( flags ) ) {
1200 case LDAP_DN_FORMAT_LDAP:
1201 case LDAP_DN_FORMAT_LDAPV3:
1202 if ( str2strval( p, stoplen - ( p - str ),
1203 &attrValue, &p, flags,
1204 &attrValueEncoding ) ) {
1209 case LDAP_DN_FORMAT_DCE:
1210 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1223 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1230 case B4IA5VALUEQUOTED:
1232 /* lead quote already stripped */
1233 if ( quotedIA52strval( p, &attrValue,
1244 if ( !( flags & LDAP_DN_SKIP ) ) {
1248 * we accept empty values
1250 ava = ldapava_new( &attrType, &attrValue,
1251 attrValueEncoding );
1253 if ( ava == NULL ) {
1254 rc = LDAP_NO_MEMORY;
1257 tmpRDN[navas++] = ava;
1259 attrValue.bv_val = NULL;
1260 attrValue.bv_len = 0;
1263 * prepare room for new AVAs if needed
1265 if (navas == num_slots) {
1268 if ( tmpRDN == tmpRDN_ ) {
1269 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
1270 if ( tmp == NULL ) {
1271 rc = LDAP_NO_MEMORY;
1274 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1277 tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
1278 if ( tmp == NULL ) {
1279 rc = LDAP_NO_MEMORY;
1290 * if we got an AVA separator ('+', or ',' for DCE )
1291 * we expect a new AVA for this RDN; otherwise
1292 * we add the RDN to the DN
1294 switch ( LDAP_DN_FORMAT( flags ) ) {
1295 case LDAP_DN_FORMAT_LDAP:
1296 case LDAP_DN_FORMAT_LDAPV3:
1297 case LDAP_DN_FORMAT_LDAPV2:
1298 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1303 case LDAP_DN_FORMAT_DCE:
1304 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1312 * the RDN is over, phew
1315 if ( !( flags & LDAP_DN_SKIP ) ) {
1316 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1317 + sizeof(LDAPAVA *) * (navas+1) );
1318 if ( newRDN == NULL ) {
1319 rc = LDAP_NO_MEMORY;
1324 newRDN[0] = (LDAPAVA**)(newRDN+1);
1326 for (i=0; i<navas; i++)
1327 newRDN[0][i] = tmpRDN[i];
1328 newRDN[0][i] = NULL;
1336 /* they should have been used in an AVA */
1337 attrType.bv_val = NULL;
1338 attrValue.bv_val = NULL;
1353 /* They are set to NULL after they're used in an AVA */
1355 if ( attrValue.bv_val ) {
1356 free( attrValue.bv_val );
1359 for ( navas-- ; navas >= 0; navas-- ) {
1360 ldap_avafree( tmpRDN[navas] );
1365 if ( tmpRDN != tmpRDN_ ) {
1366 LDAP_FREE( tmpRDN );
1377 * reads in a UTF-8 string value, unescaping stuff:
1378 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1379 * '\' + HEXPAIR(p) -> unhex(p)
1382 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1384 const char *p, *end, *startPos, *endPos = NULL;
1385 ber_len_t len, escapes;
1392 end = str + stoplen;
1393 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1394 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1396 if ( p[ 0 ] == '\0' ) {
1399 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1404 if ( LDAP_DN_HEXPAIR( p ) ) {
1407 hexstr2bin( p, &c );
1410 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1413 * we assume the string is UTF-8
1415 *retFlags = LDAP_AVA_NONPRINTABLE;
1422 if ( LDAP_DN_PEDANTIC & flags ) {
1426 * we do not allow escaping
1427 * of chars that don't need
1428 * to and do not belong to
1433 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1434 if ( p[ 0 ] == '\0' ) {
1437 *retFlags = LDAP_AVA_NONPRINTABLE;
1439 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1440 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1443 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1445 * FIXME: maybe we can add
1446 * escapes if not pedantic?
1453 * we do allow unescaped spaces at the end
1454 * of the value only in non-pedantic mode
1456 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1457 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1458 if ( flags & LDAP_DN_PEDANTIC ) {
1462 /* strip trailing (unescaped) spaces */
1463 for ( endPos = p - 1;
1464 endPos > startPos + 1 &&
1465 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1466 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1473 if ( flags & LDAP_DN_SKIP ) {
1478 * FIXME: test memory?
1480 len = ( endPos ? endPos : p ) - startPos - escapes;
1483 if ( escapes == 0 ) {
1484 if ( *retFlags == LDAP_AVA_NONPRINTABLE ) {
1485 val->bv_val = LDAP_MALLOC( len + 1 );
1486 AC_MEMCPY( val->bv_val, startPos, len );
1487 val->bv_val[ len ] = '\0';
1489 val->bv_val = LDAP_STRNDUP( startPos, len );
1495 val->bv_val = LDAP_MALLOC( len + 1 );
1496 for ( s = 0, d = 0; d < len; ) {
1497 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1499 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1500 val->bv_val[ d++ ] =
1503 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1506 hexstr2bin( &startPos[ s ], &c );
1507 val->bv_val[ d++ ] = c;
1511 /* we should never get here */
1516 val->bv_val[ d++ ] = startPos[ s++ ];
1520 val->bv_val[ d ] = '\0';
1528 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1530 const char *p, *startPos, *endPos = NULL;
1531 ber_len_t len, escapes;
1539 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1540 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1542 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1549 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1554 * FIXME: can we accept anything else? I guess we need
1555 * to stop if a value is not legal
1560 * (unescaped) trailing spaces are trimmed must be silently ignored;
1563 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1564 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1565 if ( flags & LDAP_DN_PEDANTIC ) {
1569 /* strip trailing (unescaped) spaces */
1570 for ( endPos = p - 1;
1571 endPos > startPos + 1 &&
1572 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1573 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1580 if ( flags & LDAP_DN_SKIP ) {
1584 len = ( endPos ? endPos : p ) - startPos - escapes;
1586 if ( escapes == 0 ){
1587 val->bv_val = LDAP_STRNDUP( startPos, len );
1592 val->bv_val = LDAP_MALLOC( len + 1 );
1593 for ( s = 0, d = 0; d < len; ) {
1595 * This point is reached only if escapes
1596 * are properly used, so all we need to
1599 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1603 val->bv_val[ d++ ] = startPos[ s++ ];
1605 val->bv_val[ d ] = '\0';
1606 assert( strlen( val->bv_val ) == len );
1613 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1615 const char *p, *startPos, *endPos = NULL;
1616 ber_len_t len, escapes;
1628 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1629 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1631 if ( p[ 0 ] == '\0' ) {
1635 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1636 && ( LDAP_DN_PEDANTIC & flags ) ) {
1641 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1646 * FIXME: can we accept anything else? I guess we need
1647 * to stop if a value is not legal
1651 /* strip trailing (unescaped) spaces */
1653 endPos > startPos + 1 &&
1654 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1655 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1661 if ( flags & LDAP_DN_SKIP ) {
1665 len = ( endPos ? endPos : p ) - startPos - escapes;
1667 if ( escapes == 0 ) {
1668 val->bv_val = LDAP_STRNDUP( startPos, len );
1673 val->bv_val = LDAP_MALLOC( len + 1 );
1674 for ( s = 0, d = 0; d < len; ) {
1675 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1678 val->bv_val[ d++ ] = startPos[ s++ ];
1680 val->bv_val[ d ] = '\0';
1681 assert( strlen( val->bv_val ) == len );
1688 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1690 const char *p, *startPos, *endPos = NULL;
1692 unsigned escapes = 0;
1700 /* initial quote already eaten */
1701 for ( startPos = p = str; p[ 0 ]; p++ ) {
1703 * According to RFC 1779, the quoted value can
1704 * contain escaped as well as unescaped special values;
1705 * as a consequence we tolerate escaped values
1706 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1707 * (e.g. '","' -> '\,').
1709 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1710 if ( p[ 1 ] == '\0' ) {
1715 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1716 && ( LDAP_DN_PEDANTIC & flags ) ) {
1718 * do we allow to escape normal chars?
1719 * LDAPv2 does not allow any mechanism
1720 * for escaping chars with '\' and hex
1727 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1729 /* eat closing quotes */
1735 * FIXME: can we accept anything else? I guess we need
1736 * to stop if a value is not legal
1740 if ( endPos == NULL ) {
1744 /* Strip trailing (unescaped) spaces */
1745 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1750 if ( flags & LDAP_DN_SKIP ) {
1754 len = endPos - startPos - escapes;
1755 assert( endPos >= startPos + escapes );
1757 if ( escapes == 0 ) {
1758 val->bv_val = LDAP_STRNDUP( startPos, len );
1763 val->bv_val = LDAP_MALLOC( len + 1 );
1766 for ( s = d = 0; d < len; ) {
1767 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1770 val->bv_val[ d++ ] = str[ s++ ];
1772 val->bv_val[ d ] = '\0';
1773 assert( strlen( val->bv_val ) == len );
1780 hexstr2bin( const char *str, char *c )
1790 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1794 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1797 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1804 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1808 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1809 *c += c2 - 'A' + 10;
1811 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1812 *c += c2 - 'a' + 10;
1820 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1822 const char *p, *startPos, *endPos = NULL;
1832 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1833 switch ( LDAP_DN_FORMAT( flags ) ) {
1834 case LDAP_DN_FORMAT_LDAPV3:
1835 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1840 case LDAP_DN_FORMAT_LDAP:
1841 case LDAP_DN_FORMAT_LDAPV2:
1842 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1847 case LDAP_DN_FORMAT_DCE:
1848 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1854 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1855 if ( flags & LDAP_DN_PEDANTIC ) {
1860 for ( ; p[ 0 ]; p++ ) {
1861 switch ( LDAP_DN_FORMAT( flags ) ) {
1862 case LDAP_DN_FORMAT_LDAPV3:
1863 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1868 case LDAP_DN_FORMAT_LDAP:
1869 case LDAP_DN_FORMAT_LDAPV2:
1870 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1875 case LDAP_DN_FORMAT_DCE:
1876 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1885 if ( !LDAP_DN_HEXPAIR( p ) ) {
1893 if ( flags & LDAP_DN_SKIP ) {
1897 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1899 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1902 val->bv_val = LDAP_MALLOC( len + 1 );
1903 if ( val->bv_val == NULL ) {
1904 return( LDAP_NO_MEMORY );
1907 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1910 hexstr2bin( &startPos[ s ], &c );
1912 val->bv_val[ d ] = c;
1915 val->bv_val[ d ] = '\0';
1921 * convert a byte in a hexadecimal pair
1924 byte2hexpair( const char *val, char *pair )
1926 static const char hexdig[] = "0123456789ABCDEF";
1932 * we assume the string has enough room for the hex encoding
1936 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1937 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1943 * convert a binary value in hexadecimal pairs
1946 binval2hexstr( struct berval *val, char *str )
1953 if ( val->bv_len == 0 ) {
1958 * we assume the string has enough room for the hex encoding
1962 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1963 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1970 * Length of the string representation, accounting for escaped hex
1974 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1976 ber_len_t l, cl = 1;
1978 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1979 #ifdef PRETTY_ESCAPE
1980 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1981 #endif /* PRETTY_ESCAPE */
1987 if ( val->bv_len == 0 ) {
1991 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
1996 if ( p[ 0 ] == '\0' ) {
2002 cl = LDAP_UTF8_CHARLEN2( p, cl );
2004 /* illegal utf-8 char! */
2007 } else if ( cl > 1 ) {
2010 for ( cnt = 1; cnt < cl; cnt++ ) {
2011 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2015 l += escaped_byte_len * cl;
2017 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2018 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2019 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2020 #ifdef PRETTY_ESCAPE
2022 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2024 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2028 * there might be some chars we want
2029 * to escape in form of a couple
2030 * of hexdigits for optimization purposes
2035 l += escaped_ascii_len;
2037 #else /* ! PRETTY_ESCAPE */
2039 #endif /* ! PRETTY_ESCAPE */
2052 * convert to string representation, escaping with hex the UTF-8 stuff;
2053 * assume the destination has enough room for escaping
2056 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2058 ber_len_t s, d, end;
2064 if ( val->bv_len == 0 ) {
2070 * we assume the string has enough room for the hex encoding
2073 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2079 if ( val->bv_val[ s ] == '\0' ) {
2089 * The length was checked in strval2strlen();
2090 * LDAP_UTF8_CHARLEN() should suffice
2092 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2096 * there might be some chars we want to escape in form
2097 * of a couple of hexdigits for optimization purposes
2099 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2100 #ifdef PRETTY_ESCAPE
2102 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2104 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2106 #else /* ! PRETTY_ESCAPE */
2107 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2108 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2109 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2111 #endif /* ! PRETTY_ESCAPE */
2115 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2120 } else if ( cl > 1 ) {
2122 str[ d++ ] = val->bv_val[ s++ ];
2126 #ifdef PRETTY_ESCAPE
2127 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2128 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2129 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2131 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2132 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2138 #endif /* PRETTY_ESCAPE */
2139 str[ d++ ] = val->bv_val[ s++ ];
2149 * Length of the IA5 string representation (no UTF-8 allowed)
2152 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2161 if ( val->bv_len == 0 ) {
2165 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2167 * Turn value into a binary encoded BER
2172 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2173 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2174 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2175 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2190 * convert to string representation (np UTF-8)
2191 * assume the destination has enough room for escaping
2194 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2196 ber_len_t s, d, end;
2202 if ( val->bv_len == 0 ) {
2207 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2209 * Turn value into a binary encoded BER
2216 * we assume the string has enough room for the hex encoding
2220 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2221 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2222 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2223 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2226 str[ d++ ] = val->bv_val[ s++ ];
2236 * Length of the (supposedly) DCE string representation,
2237 * accounting for escaped hex of UTF-8 chars
2240 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2249 if ( val->bv_len == 0 ) {
2253 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2255 * FIXME: Turn the value into a binary encoded BER?
2260 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2261 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2276 * convert to (supposedly) DCE string representation,
2277 * escaping with hex the UTF-8 stuff;
2278 * assume the destination has enough room for escaping
2281 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2289 if ( val->bv_len == 0 ) {
2294 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2296 * FIXME: Turn the value into a binary encoded BER?
2304 * we assume the string has enough room for the hex encoding
2308 for ( s = 0, d = 0; s < val->bv_len; ) {
2309 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2312 str[ d++ ] = val->bv_val[ s++ ];
2322 * Length of the (supposedly) AD canonical string representation,
2323 * accounting for escaped hex of UTF-8 chars
2326 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2335 if ( val->bv_len == 0 ) {
2339 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2341 * FIXME: Turn the value into a binary encoded BER?
2346 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2347 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2362 * convert to (supposedly) AD string representation,
2363 * escaping with hex the UTF-8 stuff;
2364 * assume the destination has enough room for escaping
2367 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2375 if ( val->bv_len == 0 ) {
2380 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2382 * FIXME: Turn the value into a binary encoded BER?
2390 * we assume the string has enough room for the hex encoding
2394 for ( s = 0, d = 0; s < val->bv_len; ) {
2395 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2398 str[ d++ ] = val->bv_val[ s++ ];
2408 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2409 * the first part of the AD representation of the DN is written in DNS
2410 * form, i.e. dot separated domain name components (as suggested
2411 * by Luke Howard, http://www.padl.com/~lukeh)
2414 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2417 int domain = 0, first = 1;
2418 ber_len_t l = 1; /* we move the null also */
2421 /* we are guaranteed there's enough memory in str */
2427 assert( *iRDN >= 0 );
2429 str = bv->bv_val + pos;
2431 for ( i = *iRDN; i >= 0; i-- ) {
2435 assert( dn[ 0 ][ i ] );
2438 assert( rdn[ 0 ][ 0 ] );
2439 ava = rdn[ 0 ][ 0 ];
2441 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2449 AC_MEMCPY( str, ava->la_value.bv_val,
2450 ava->la_value.bv_len + 1);
2451 l += ava->la_value.bv_len;
2454 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2455 AC_MEMCPY( str, ava->la_value.bv_val,
2456 ava->la_value.bv_len );
2457 str[ ava->la_value.bv_len ] = '.';
2458 l += ava->la_value.bv_len + 1;
2463 bv->bv_len = pos + l - 1;
2469 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2470 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2477 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2478 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2480 /* len(type) + '=' + '+' | ',' */
2481 l += ava->la_attr.bv_len + 2;
2483 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2484 /* octothorpe + twice the length */
2485 l += 1 + 2 * ava->la_value.bv_len;
2489 unsigned f = flags | ava->la_flags;
2491 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2504 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2505 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2510 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2511 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2513 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2514 ava->la_attr.bv_len );
2515 l += ava->la_attr.bv_len;
2519 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2521 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2524 l += 2 * ava->la_value.bv_len;
2528 unsigned f = flags | ava->la_flags;
2530 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2535 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2544 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2551 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2552 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2554 /* len(type) + '=' + ',' | '/' */
2555 l += ava->la_attr.bv_len + 2;
2557 switch ( ava->la_flags ) {
2558 case LDAP_AVA_BINARY:
2559 /* octothorpe + twice the length */
2560 l += 1 + 2 * ava->la_value.bv_len;
2563 case LDAP_AVA_STRING: {
2565 unsigned f = flags | ava->la_flags;
2567 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2585 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2590 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2591 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2596 str[ l++ ] = ( iAVA ? ',' : '/' );
2599 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2600 ava->la_attr.bv_len );
2601 l += ava->la_attr.bv_len;
2605 switch ( ava->la_flags ) {
2606 case LDAP_AVA_BINARY:
2608 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2611 l += 2 * ava->la_value.bv_len;
2614 case LDAP_AVA_STRING: {
2616 unsigned f = flags | ava->la_flags;
2618 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2636 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2646 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2647 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2650 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2652 /* FIXME: are binary values allowed in UFN? */
2653 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2654 /* octothorpe + twice the value */
2655 l += 1 + 2 * ava->la_value.bv_len;
2659 unsigned f = flags | ava->la_flags;
2661 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2674 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2679 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2680 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2682 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2684 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2687 l += 2 * ava->la_value.bv_len;
2691 unsigned f = flags | ava->la_flags;
2693 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2699 if ( rdn[ 0 ][ iAVA + 1 ]) {
2700 AC_MEMCPY( &str[ l ], " + ", 3 );
2704 AC_MEMCPY( &str[ l ], ", ", 2 );
2715 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2725 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2726 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2731 /* FIXME: are binary values allowed in UFN? */
2732 switch ( ava->la_flags ) {
2733 case LDAP_AVA_BINARY:
2734 /* octothorpe + twice the value */
2735 l += 1 + 2 * ava->la_value.bv_len;
2738 case LDAP_AVA_STRING: {
2740 unsigned f = flags | ava->la_flags;
2742 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2760 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2765 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2766 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2771 str[ l++ ] = ( iAVA ? ',' : '/' );
2774 switch ( ava->la_flags ) {
2775 case LDAP_AVA_BINARY:
2777 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2780 l += 2 * ava->la_value.bv_len;
2783 case LDAP_AVA_STRING: {
2785 unsigned f = flags | ava->la_flags;
2787 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2807 * Returns in str a string representation of rdn based on flags.
2808 * There is some duplication of code between this and ldap_dn2str;
2809 * this is wanted to reduce the allocation of temporary buffers.
2812 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2819 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2820 return LDAP_PARAM_ERROR;
2823 rc = ldap_rdn2bv( rdn, &bv, flags );
2829 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2839 if ( rdn == NULL ) {
2840 bv->bv_val = LDAP_STRDUP( "" );
2841 return( LDAP_SUCCESS );
2845 * This routine wastes "back" bytes at the end of the string
2848 switch ( LDAP_DN_FORMAT( flags ) ) {
2849 case LDAP_DN_FORMAT_LDAPV3:
2850 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2851 return LDAP_DECODING_ERROR;
2855 case LDAP_DN_FORMAT_LDAPV2:
2856 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2857 return LDAP_DECODING_ERROR;
2861 case LDAP_DN_FORMAT_UFN:
2862 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2863 return LDAP_DECODING_ERROR;
2867 case LDAP_DN_FORMAT_DCE:
2868 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2869 return LDAP_DECODING_ERROR;
2873 case LDAP_DN_FORMAT_AD_CANONICAL:
2874 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2875 return LDAP_DECODING_ERROR;
2880 return LDAP_PARAM_ERROR;
2883 bv->bv_val = LDAP_MALLOC( l + 1 );
2885 switch ( LDAP_DN_FORMAT( flags ) ) {
2886 case LDAP_DN_FORMAT_LDAPV3:
2887 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2891 case LDAP_DN_FORMAT_LDAPV2:
2892 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2896 case LDAP_DN_FORMAT_UFN:
2897 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2901 case LDAP_DN_FORMAT_DCE:
2902 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2906 case LDAP_DN_FORMAT_AD_CANONICAL:
2907 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2912 /* need at least one of the previous */
2913 return LDAP_PARAM_ERROR;
2917 ldap_memfree( bv->bv_val );
2921 bv->bv_len = l - back;
2922 bv->bv_val[ bv->bv_len ] = '\0';
2924 return LDAP_SUCCESS;
2928 * Very bulk implementation; many optimizations can be performed
2929 * - a NULL dn results in an empty string ""
2932 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2933 * we must encode it in binary form ('#' + HEXPAIRs)
2934 * b) does DCE/AD support UTF-8?
2935 * no clue; don't think so.
2936 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2937 * use binary encoded BER
2939 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2946 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2947 return LDAP_PARAM_ERROR;
2950 rc = ldap_dn2bv( dn, &bv, flags );
2955 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2958 int rc = LDAP_ENCODING_ERROR;
2961 /* stringifying helpers for LDAPv3/LDAPv2 */
2962 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2963 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2970 LDAP_LOG (( "getdn", LDAP_LEVEL_ARGS, "=> ldap_dn2bv(%u)\n%s%s",
2973 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2977 * a null dn means an empty dn string
2978 * FIXME: better raise an error?
2981 bv->bv_val = LDAP_STRDUP( "" );
2982 return( LDAP_SUCCESS );
2985 switch ( LDAP_DN_FORMAT( flags ) ) {
2986 case LDAP_DN_FORMAT_LDAPV3:
2987 sv2l = strval2strlen;
2991 case LDAP_DN_FORMAT_LDAPV2:
2992 sv2l = strval2IA5strlen;
2993 sv2s = strval2IA5str;
2996 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2998 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3000 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3001 goto return_results;
3007 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3008 rc = LDAP_NO_MEMORY;
3012 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3014 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3016 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
3018 LDAP_FREE( bv->bv_val );
3020 goto return_results;
3028 * trim the last ',' (the allocated memory
3029 * is one byte longer than required)
3031 bv->bv_len = len - 1;
3032 bv->bv_val[ bv->bv_len ] = '\0';
3037 case LDAP_DN_FORMAT_UFN: {
3039 * FIXME: quoting from RFC 1781:
3041 To take a distinguished name, and generate a name of this format with
3042 attribute types omitted, the following steps are followed.
3044 1. If the first attribute is of type CommonName, the type may be
3047 2. If the last attribute is of type Country, the type may be
3050 3. If the last attribute is of type Country, the last
3051 Organisation attribute may have the type omitted.
3053 4. All attributes of type OrganisationalUnit may have the type
3054 omitted, unless they are after an Organisation attribute or
3055 the first attribute is of type OrganisationalUnit.
3057 * this should be the pedantic implementation.
3059 * Here the standard implementation reflects
3060 * the one historically provided by OpenLDAP
3061 * (and UMIch, I presume), with the variant
3062 * of spaces and plusses (' + ') separating
3065 * A non-standard but nice implementation could
3066 * be to turn the final "dc" attributes into a
3067 * dot-separated domain.
3069 * Other improvements could involve the use of
3070 * friendly country names and so.
3073 int leftmost_dc = -1;
3075 #endif /* DC_IN_UFN */
3077 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3079 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3081 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3082 goto return_results;
3087 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3088 if ( leftmost_dc == -1 ) {
3094 #endif /* DC_IN_UFN */
3097 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3098 rc = LDAP_NO_MEMORY;
3103 if ( leftmost_dc == -1 ) {
3104 #endif /* DC_IN_UFN */
3105 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3107 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3109 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3111 LDAP_FREE( bv->bv_val );
3113 goto return_results;
3119 * trim the last ', ' (the allocated memory
3120 * is two bytes longer than required)
3122 bv->bv_len = len - 2;
3123 bv->bv_val[ bv->bv_len ] = '\0';
3126 last_iRDN = iRDN - 1;
3128 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3130 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3132 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3134 LDAP_FREE( bv->bv_val );
3136 goto return_results;
3141 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3142 LDAP_FREE( bv->bv_val );
3144 goto return_results;
3147 /* the string is correctly terminated by dn2domain */
3149 #endif /* DC_IN_UFN */
3155 case LDAP_DN_FORMAT_DCE:
3156 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3158 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3160 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3161 goto return_results;
3167 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3168 rc = LDAP_NO_MEMORY;
3172 for ( l = 0; iRDN--; ) {
3174 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3176 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3178 LDAP_FREE( bv->bv_val );
3180 goto return_results;
3188 bv->bv_val[ bv->bv_len ] = '\0';
3193 case LDAP_DN_FORMAT_AD_CANONICAL: {
3195 * Sort of UFN for DCE DNs: a slash ('/') separated
3196 * global->local DN with no types; strictly speaking,
3197 * the naming context should be a domain, which is
3198 * written in DNS-style, e.g. dot-deparated.
3202 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3206 * "microsoft.com/People/Bill,Gates"
3208 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3210 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3212 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3213 goto return_results;
3219 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3220 rc = LDAP_NO_MEMORY;
3225 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3226 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3228 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3230 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3231 flags, &rdnl, 0 ) ) {
3232 LDAP_FREE( bv->bv_val );
3234 goto return_results;
3243 * Strictly speaking, AD canonical requires
3244 * a DN to be in the form "..., dc=smtg",
3245 * i.e. terminated by a domain component
3247 if ( flags & LDAP_DN_PEDANTIC ) {
3248 LDAP_FREE( bv->bv_val );
3250 rc = LDAP_ENCODING_ERROR;
3254 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3256 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3258 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3259 flags, &rdnl, first ) ) {
3260 LDAP_FREE( bv->bv_val );
3262 goto return_results;
3272 bv->bv_val[ bv->bv_len ] = '\0';
3278 return LDAP_PARAM_ERROR;
3282 LDAP_LOG (( "getdn", LDAP_LEVEL_RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3283 bv->bv_val, flags, rc ));
3285 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3286 bv->bv_val, flags, rc );
3294 #include <openssl/x509.h>
3295 #include <openssl/err.h>
3297 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3298 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3299 * constructed DN will use numeric OIDs to identify attributeTypes,
3300 * and the func() will be invoked to rewrite the DN with the given
3303 * Otherwise the DN will use shortNames as defined in the OpenSSL
3306 * It's preferable to let slapd do the OID to attributeType mapping,
3307 * because the OpenSSL tables are known to have many typos in versions
3308 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3309 * so we're forced to use OpenSSL's mapping there.
3310 * -- Howard Chu 2002-04-18
3314 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3319 LDAPAVA *newAVA, *baseAVA;
3320 X509_NAME_ENTRY *ne;
3323 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3325 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3327 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3336 /* Get the number of AVAs. This is not necessarily the same as
3337 * the number of RDNs.
3339 navas = X509_NAME_entry_count( x509_name );
3341 /* Get the last element, to see how many RDNs there are */
3342 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3343 nrdns = ne->set + 1;
3345 /* Allocate the DN/RDN/AVA stuff as a single block */
3346 dnsize = sizeof(LDAPDN) + sizeof(LDAPRDN *) * (nrdns+1);
3347 dnsize += sizeof(LDAPRDN) * nrdns + sizeof(LDAPAVA *) * (navas+nrdns);
3348 dnsize += sizeof(LDAPAVA) * navas;
3349 if (dnsize > sizeof(ptrs)) {
3350 newDN = (LDAPDN *)LDAP_MALLOC( dnsize );
3351 if ( newDN == NULL )
3352 return LDAP_NO_MEMORY;
3354 newDN = (LDAPDN *)ptrs;
3357 newDN[0] = (LDAPRDN**)(newDN+1);
3358 newDN[0][nrdns] = NULL;
3359 newRDN = (LDAPRDN*)(newDN[0] + nrdns+1);
3360 newAVA = (LDAPAVA*)(newRDN + navas + nrdns*2);
3363 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3364 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3365 ne = X509_NAME_get_entry( x509_name, i );
3366 obj = X509_NAME_ENTRY_get_object( ne );
3367 str = X509_NAME_ENTRY_get_data( ne );
3369 /* If set changed, move to next RDN */
3370 if ( set != ne->set ) {
3371 /* If this is not the first time, end the
3372 * previous RDN and advance.
3375 newRDN[0][k] = NULL;
3376 newRDN = (LDAPRDN*)(newRDN[0]+k+1);
3378 newDN[0][j++] = newRDN;
3380 newRDN[0] = (LDAPAVA**)(newRDN+1);
3384 newAVA->la_private = NULL;
3385 newAVA->la_flags = LDAP_AVA_STRING;
3388 int n = OBJ_obj2nid( obj );
3392 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3393 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3395 get_oid: newAVA->la_attr.bv_val = oidptr;
3396 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3397 oidptr += newAVA->la_attr.bv_len + 1;
3398 oidrem -= newAVA->la_attr.bv_len + 1;
3400 /* Running out of OID buffer space? */
3402 if ( oidsize == 0 ) {
3403 oidsize = sizeof(oids) * 2;
3405 oidbuf = LDAP_MALLOC( oidsize );
3406 if ( oidbuf == NULL ) goto nomem;
3410 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3411 if ( oidbuf == NULL ) goto nomem;
3412 /* Buffer moved! Fix AVA pointers */
3413 if ( old != oidbuf ) {
3415 long dif = oidbuf - old;
3417 for (a=baseAVA; a<=newAVA; a++){
3418 if (a->la_attr.bv_val >= old &&
3419 a->la_attr.bv_val <= (old + oidsize))
3420 a->la_attr.bv_val += dif;
3423 oidptr = oidbuf + oidsize - oidrem;
3429 Val.bv_val = str->data;
3430 Val.bv_len = str->length;
3431 switch( str->type ) {
3432 case V_ASN1_UNIVERSALSTRING:
3433 /* This uses 32-bit ISO 10646-1 */
3434 csize = 4; goto to_utf8;
3435 case V_ASN1_BMPSTRING:
3436 /* This uses 16-bit ISO 10646-1 */
3437 csize = 2; goto to_utf8;
3438 case V_ASN1_T61STRING:
3439 /* This uses 8-bit, assume ISO 8859-1 */
3441 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3442 if (rc != LDAP_SUCCESS) goto nomem;
3443 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3445 case V_ASN1_UTF8STRING:
3446 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3447 /* This is already in UTF-8 encoding */
3448 case V_ASN1_IA5STRING:
3449 case V_ASN1_PRINTABLESTRING:
3450 /* These are always 7-bit strings */
3451 ber_dupbv( &newAVA->la_value, &Val );
3455 newRDN[0][k] = newAVA;
3459 newRDN[0][k] = NULL;
3462 rc = func( newDN, flags );
3463 if ( rc != LDAP_SUCCESS )
3467 rc = ldap_dn2bv( newDN, bv, LDAP_DN_FORMAT_LDAPV3 );
3470 for (;baseAVA < newAVA; baseAVA++) {
3471 LDAP_FREE( baseAVA->la_value.bv_val );
3475 LDAP_FREE( oidbuf );
3476 if ( newDN != (LDAPDN*) ptrs )
3480 #endif /* HAVE_TLS */