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 );
93 if ( entry == NULL ) {
94 ld->ld_errno = LDAP_PARAM_ERROR;
98 tmp = *entry->lm_ber; /* struct copy */
99 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
100 ld->ld_errno = LDAP_DECODING_ERROR;
108 * RFC 1823 ldap_dn2ufn
111 ldap_dn2ufn( LDAP_CONST char *dn )
116 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2ufn\n" ));
118 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
121 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
122 &out, LDAP_DN_FORMAT_UFN );
128 * RFC 1823 ldap_explode_dn
131 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
134 char **values = NULL;
136 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
139 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_explode_dn\n" ));
141 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
144 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
149 if( tmpDN == NULL ) {
150 values = LDAP_MALLOC( sizeof( char * ) );
151 if( values == NULL ) return NULL;
157 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ );
159 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
160 if ( values == NULL ) {
161 ldap_dnfree( tmpDN );
165 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
166 ldap_rdn2str( tmpDN[ 0 ][ iRDN ], &values[ iRDN ], flag );
168 ldap_dnfree( tmpDN );
169 values[ iRDN ] = NULL;
175 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
178 char **values = NULL;
183 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_explode_rdn\n" ));
185 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
189 * we only parse the first rdn
190 * FIXME: we prefer efficiency over checking if the _ENTIRE_
193 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
198 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) ;
199 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
200 if ( values == NULL ) {
201 ldap_rdnfree( tmpRDN );
205 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
206 ber_len_t l = 0, vl, al = 0;
208 LDAPAVA *ava = tmpRDN[ 0 ][ iAVA ];
210 if ( ava->la_flags == LDAP_AVA_BINARY ) {
211 vl = 1 + 2 * ava->la_value.bv_len;
214 if ( strval2strlen( &ava->la_value,
215 ava->la_flags, &vl ) ) {
221 al = ava->la_attr.bv_len;
222 l = vl + ava->la_attr.bv_len + 1;
224 str = LDAP_MALLOC( l + 1 );
225 AC_MEMCPY( str, ava->la_attr.bv_val,
226 ava->la_attr.bv_len );
231 str = LDAP_MALLOC( l + 1 );
234 if ( ava->la_flags == LDAP_AVA_BINARY ) {
236 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
241 if ( strval2str( &ava->la_value, &str[ al ],
242 ava->la_flags, &vl ) ) {
248 values[ iAVA ] = str;
250 values[ iAVA ] = NULL;
252 ldap_rdnfree( tmpRDN );
257 LBER_VFREE( values );
258 ldap_rdnfree( tmpRDN );
263 ldap_dn2dcedn( LDAP_CONST char *dn )
268 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2dcedn\n" ));
270 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
273 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
274 &out, LDAP_DN_FORMAT_DCE );
280 ldap_dcedn2dn( LDAP_CONST char *dce )
285 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dcedn2dn\n" ));
287 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
290 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
296 ldap_dn2ad_canonical( LDAP_CONST char *dn )
301 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn2ad_canonical\n" ));
303 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
306 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
307 &out, LDAP_DN_FORMAT_AD_CANONICAL );
313 * function that changes the string representation of dnin
314 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
317 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
318 * plus some rfc 1779)
319 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
320 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
321 * LDAP_DN_FORMAT_DCE (?)
323 * fout can be any of the above except
324 * LDAP_DN_FORMAT_LDAP
326 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
327 * LDAP_DN_FORMAT_AD_CANONICAL (?)
330 ldap_dn_normalize( LDAP_CONST char *dnin,
331 unsigned fin, char **dnout, unsigned fout )
334 LDAPDN *tmpDN = NULL;
337 LDAP_LOG (( "getdn", LDAP_LEVEL_ENTRY, "ldap_dn_normalize\n" ));
339 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
346 if ( dnin == NULL ) {
347 return( LDAP_SUCCESS );
350 rc = ldap_str2dn( dnin , &tmpDN, fin );
351 if ( rc != LDAP_SUCCESS ) {
355 rc = ldap_dn2str( tmpDN, dnout, fout );
357 ldap_dnfree( tmpDN );
365 /* #define B4ATTRTYPE 0x0001 */
366 #define B4OIDATTRTYPE 0x0002
367 #define B4STRINGATTRTYPE 0x0003
369 #define B4AVAEQUALS 0x0100
370 #define B4AVASEP 0x0200
371 #define B4RDNSEP 0x0300
372 #define GOTAVA 0x0400
374 #define B4ATTRVALUE 0x0010
375 #define B4STRINGVALUE 0x0020
376 #define B4IA5VALUEQUOTED 0x0030
377 #define B4IA5VALUE 0x0040
378 #define B4BINARYVALUE 0x0050
381 * Helpers (mostly from slap.h)
382 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
383 * Macros assume "C" Locale (ASCII)
385 #define LDAP_DN_ASCII_SPACE(c) \
386 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
387 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
388 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
389 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
391 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
392 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
393 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
394 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
395 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
396 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
399 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
400 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
401 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
402 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
403 #define LDAP_DN_ATTRDESC_CHAR(c) \
404 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
406 /* special symbols */
407 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
408 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
409 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
410 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
411 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
412 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
413 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
414 #define LDAP_DN_VALUE_END(c) \
415 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
416 #define LDAP_DN_NE(c) \
417 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
418 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
419 #define LDAP_DN_MAYESCAPE(c) \
420 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
421 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
422 #define LDAP_DN_NEEDESCAPE(c) \
423 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
424 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
425 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
426 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
427 #define LDAP_DN_WILLESCAPE_CHAR(c) \
428 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
429 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
430 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
431 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
434 #define LDAP_DN_VALUE_END_V2(c) \
435 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
437 #define LDAP_DN_V2_SPECIAL(c) \
438 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
439 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
440 || LDAP_DN_OCTOTHORPE(c) )
441 #define LDAP_DN_V2_PAIR(c) \
442 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
445 * DCE (mostly from Luke Howard and IBM implementation for AIX)
447 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
448 * Here escapes and valid chars for GDS are considered; as soon as more
449 * specific info is found, the macros will be updated.
451 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
452 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
454 * Metachars: '/', ',', '=', '\'.
456 * the '\' is used to escape other metachars.
462 * Attribute types must start with alphabetic chars and can contain
463 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
465 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
466 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
467 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
468 #define LDAP_DN_VALUE_END_DCE(c) \
469 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
470 #define LDAP_DN_NEEDESCAPE_DCE(c) \
471 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
474 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
475 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
476 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
477 #define LDAP_DN_VALUE_END_AD(c) \
478 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
479 #define LDAP_DN_NEEDESCAPE_AD(c) \
480 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
483 #define LDAP_DN_HEXPAIR(s) \
484 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
485 /* better look at the AttributeDescription? */
487 /* FIXME: no composite rdn or non-"dc" types, right?
488 * (what about "dc" in OID form?) */
489 /* FIXME: we do not allow binary values in domain, right? */
490 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
491 /* NOTE: don't use strcasecmp() as it is locale specific! */
492 #define LDAP_DC_ATTR "dc"
493 #define LDAP_DC_ATTRU "DC"
494 #define LDAP_DN_IS_RDN_DC( r ) \
495 ( (r) && (r)[0][0] && !(r)[0][1] \
496 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
497 && ((r)[0][0]->la_attr.bv_len == 2) \
498 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
499 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
500 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
501 || ((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
503 /* Composite rules */
504 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
505 ( LDAP_DN_LDAPV2(f) \
506 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
507 #define LDAP_DN_ALLOW_SPACES(f) \
508 ( LDAP_DN_LDAPV2(f) \
509 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
510 #define LDAP_DN_LDAP(f) \
511 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
512 #define LDAP_DN_LDAPV3(f) \
513 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
514 #define LDAP_DN_LDAPV2(f) \
515 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
516 #define LDAP_DN_DCE(f) \
517 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
518 #define LDAP_DN_UFN(f) \
519 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
520 #define LDAP_DN_ADC(f) \
521 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
522 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
525 * LDAPAVA helpers (will become part of the API for operations
526 * on structural representations of DNs).
529 ldapava_new( const struct berval *attr, const struct berval *val,
537 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
539 /* should we test it? */
544 ava->la_attr.bv_len = attr->bv_len;
545 ava->la_attr.bv_val = (char *)(ava+1);
546 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
547 ava->la_attr.bv_val[attr->bv_len] = '\0';
549 ava->la_value = *val;
550 ava->la_flags = flags;
552 ava->la_private = NULL;
558 ldap_avafree( LDAPAVA *ava )
563 /* ava's private must be freed by caller
564 * (at present let's skip this check because la_private
565 * basically holds static data) */
566 assert( ava->la_private == NULL );
570 /* la_attr is now contiguous with ava, not freed separately */
571 LDAP_FREE( ava->la_attr.bv_val );
573 LDAP_FREE( ava->la_value.bv_val );
579 ldap_rdnfree( LDAPRDN *rdn )
587 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
588 ldap_avafree( rdn[ 0 ][ iAVA ] );
595 ldap_dnfree( LDAPDN *dn )
603 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
604 ldap_rdnfree( dn[ 0 ][ iRDN ] );
611 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
612 * into a structural representation of the DN, by separating attribute
613 * types and values encoded in the more appropriate form, which is
614 * string or OID for attribute types and binary form of the BER encoded
615 * value or Unicode string. Formats different from LDAPv3 are parsed
616 * according to their own rules and turned into the more appropriate
617 * form according to LDAPv3.
619 * NOTE: I realize the code is getting spaghettish; it is rather
620 * experimental and will hopefully turn into something more simple
621 * and readable as soon as it works as expected.
625 * Default sizes of AVA and RDN static working arrays; if required
626 * the are dynamically resized. The values can be tuned in case
627 * of special requirements (e.g. very deep DN trees or high number
630 #define TMP_AVA_SLOTS 8
631 #define TMP_RDN_SLOTS 32
634 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
640 bv.bv_len = strlen( str );
641 bv.bv_val = (char *) str;
643 return ldap_bv2dn( &bv, dn, flags );
647 ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
650 int rc = LDAP_DECODING_ERROR;
653 LDAPDN *newDN = NULL;
654 LDAPRDN *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_;
655 int num_slots = TMP_RDN_SLOTS;
656 char *str = bv->bv_val;
657 char *end = str + bv->bv_len;
660 assert( bv->bv_val );
664 LDAP_LOG (( "getdn", LDAP_LEVEL_ARGS, "ldap_bv2dn(%s,%u)\n%s",
667 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
672 switch ( LDAP_DN_FORMAT( flags ) ) {
673 case LDAP_DN_FORMAT_LDAP:
674 case LDAP_DN_FORMAT_LDAPV3:
675 case LDAP_DN_FORMAT_LDAPV2:
676 case LDAP_DN_FORMAT_DCE:
679 /* unsupported in str2dn */
680 case LDAP_DN_FORMAT_UFN:
681 case LDAP_DN_FORMAT_AD_CANONICAL:
682 return LDAP_PARAM_ERROR;
684 case LDAP_DN_FORMAT_LBER:
686 return LDAP_PARAM_ERROR;
689 if ( bv->bv_len == 0 ) {
693 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
694 /* value must have embedded NULs */
695 return LDAP_DECODING_ERROR;
699 if ( LDAP_DN_DCE( flags ) ) {
702 * (from Luke Howard: thnx) A RDN separator is required
703 * at the beginning of an (absolute) DN.
705 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
711 * actually we do not want to accept by default the DCE form,
712 * we do not want to auto-detect it
715 } else if ( LDAP_DN_LDAP( flags ) ) {
717 * if dn starts with '/' let's make it a DCE dn
719 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
720 flags |= LDAP_DN_FORMAT_DCE;
726 for ( ; p < end; p++ ) {
729 tmpbv.bv_len = bv->bv_len - ( p - str );
730 tmpbv.bv_val = (char *)p;
732 err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags );
733 if ( err != LDAP_SUCCESS ) {
738 * We expect a rdn separator
740 if ( p < end && p[ 0 ] ) {
741 switch ( LDAP_DN_FORMAT( flags ) ) {
742 case LDAP_DN_FORMAT_LDAPV3:
743 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
744 rc = LDAP_DECODING_ERROR;
749 case LDAP_DN_FORMAT_LDAP:
750 case LDAP_DN_FORMAT_LDAPV2:
751 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
752 rc = LDAP_DECODING_ERROR;
757 case LDAP_DN_FORMAT_DCE:
758 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
759 rc = LDAP_DECODING_ERROR;
767 tmpDN[nrdns++] = newRDN;
771 * make the static RDN array dynamically rescalable
773 if ( nrdns == num_slots ) {
776 if ( tmpDN == tmpDN_ ) {
777 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
782 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
785 tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
796 if ( p >= end || p[ 0 ] == '\0' ) {
798 * the DN is over, phew
800 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
801 sizeof(LDAPRDN *) * (nrdns+1));
802 if ( newDN == NULL ) {
808 newDN[0] = (LDAPRDN **)(newDN+1);
810 if ( LDAP_DN_DCE( flags ) ) {
811 /* add in reversed order */
812 for ( i=0; i<nrdns; i++ )
813 newDN[0][i] = tmpDN[nrdns-1-i];
815 for ( i=0; i<nrdns; i++ )
816 newDN[0][i] = tmpDN[i];
818 newDN[0][nrdns] = NULL;
827 ldap_rdnfree( newRDN );
830 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
831 ldap_rdnfree( tmpDN[nrdns] );
836 if ( tmpDN != tmpDN_ ) {
841 LDAP_LOG (( "getdn", LDAP_LEVEL_RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
844 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
854 * Parses a relative DN according to flags up to a rdn separator
855 * or to the end of str.
856 * Returns the rdn and a pointer to the string continuation, which
857 * corresponds to the rdn separator or to '\0' in case the string is over.
860 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
861 char **n_in, unsigned flags )
866 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
868 bv.bv_len = strlen( str );
869 bv.bv_val = (char *) str;
871 return ldap_bv2rdn( &bv, rdn, n_in, flags );
875 ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
876 char **n_in, unsigned flags )
878 const char **n = (const char **) n_in;
882 int rc = LDAP_DECODING_ERROR;
883 int attrTypeEncoding = LDAP_AVA_STRING,
884 attrValueEncoding = LDAP_AVA_STRING;
886 struct berval attrType = { 0, NULL };
887 struct berval attrValue = { 0, NULL };
889 LDAPRDN *newRDN = NULL;
890 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
891 int num_slots = TMP_AVA_SLOTS;
897 assert( bv->bv_len );
898 assert( bv->bv_val );
899 assert( rdn || flags & LDAP_DN_SKIP );
903 stoplen = bv->bv_len;
910 switch ( LDAP_DN_FORMAT( flags ) ) {
911 case LDAP_DN_FORMAT_LDAP:
912 case LDAP_DN_FORMAT_LDAPV3:
913 case LDAP_DN_FORMAT_LDAPV2:
914 case LDAP_DN_FORMAT_DCE:
917 /* unsupported in str2dn */
918 case LDAP_DN_FORMAT_UFN:
919 case LDAP_DN_FORMAT_AD_CANONICAL:
920 return LDAP_PARAM_ERROR;
922 case LDAP_DN_FORMAT_LBER:
924 return LDAP_PARAM_ERROR;
927 if ( bv->bv_len == 0 ) {
932 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
933 /* value must have embedded NULs */
934 return LDAP_DECODING_ERROR;
938 for ( ; p[ 0 ] || state == GOTAVA; ) {
941 * The parser in principle advances one token a time,
942 * or toggles state if preferable.
947 * an AttributeType can be encoded as:
948 * - its string representation; in detail, implementations
949 * MUST recognize AttributeType string type names listed
950 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
951 * MAY recognize other names.
952 * - its numeric OID (a dotted decimal string); in detail
953 * RFC 2253 asserts that ``Implementations MUST allow
954 * an oid in the attribute type to be prefixed by one
955 * of the character strings "oid." or "OID."''. As soon
956 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
957 * I'm not sure whether this is required or not any
958 * longer; to be liberal, we still implement it.
961 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
962 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
969 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
970 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
975 /* whitespace is allowed (and trimmed) */
977 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
982 /* error: we expected an AVA */
988 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
989 state = B4OIDATTRTYPE;
993 /* else must be alpha */
994 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
998 /* LDAPv2 "oid." prefix */
999 if ( LDAP_DN_LDAPV2( flags ) ) {
1001 * to be overly pedantic, we only accept
1004 if ( flags & LDAP_DN_PEDANTIC ) {
1005 if ( !strncmp( p, "OID.", 4 )
1006 || !strncmp( p, "oid.", 4 ) ) {
1008 state = B4OIDATTRTYPE;
1012 if ( !strncasecmp( p, "oid.", 4 ) ) {
1014 state = B4OIDATTRTYPE;
1020 state = B4STRINGATTRTYPE;
1023 case B4OIDATTRTYPE: {
1024 int err = LDAP_SUCCESS;
1026 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1029 if ( err != LDAP_SUCCESS ) {
1032 attrType.bv_len = p - attrType.bv_val;
1034 attrTypeEncoding = LDAP_AVA_BINARY;
1036 state = B4AVAEQUALS;
1040 case B4STRINGATTRTYPE: {
1041 const char *startPos, *endPos = NULL;
1045 * the starting char has been found to be
1046 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1047 * FIXME: DCE attr types seem to have a more
1048 * restrictive syntax (no '-' ...)
1050 for ( startPos = p++; p[ 0 ]; p++ ) {
1051 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1055 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1058 * RFC 2253 does not explicitly
1059 * allow lang extensions to attribute
1062 if ( flags & LDAP_DN_PEDANTIC ) {
1067 * we trim ';' and following lang
1068 * and so from attribute types
1071 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1072 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1080 len = ( endPos ? endPos : p ) - startPos;
1085 attrTypeEncoding = LDAP_AVA_STRING;
1088 * here we need to decide whether to use it as is
1089 * or turn it in OID form; as a consequence, we
1090 * need to decide whether to binary encode the value
1093 state = B4AVAEQUALS;
1095 if ( flags & LDAP_DN_SKIP ) {
1099 attrType.bv_val = (char *)startPos;
1100 attrType.bv_len = len;
1106 /* spaces may not be allowed */
1107 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1108 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1113 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1118 /* need equal sign */
1119 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1124 /* spaces may not be allowed */
1125 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1126 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1131 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1137 * octothorpe means a BER encoded value will follow
1138 * FIXME: I don't think DCE will allow it
1140 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1142 attrValueEncoding = LDAP_AVA_BINARY;
1143 state = B4BINARYVALUE;
1147 /* STRING value expected */
1150 * if we're pedantic, an attribute type in OID form
1151 * SHOULD imply a BER encoded attribute value; we
1152 * should at least issue a warning
1154 if ( ( flags & LDAP_DN_PEDANTIC )
1155 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1156 /* OID attrType SHOULD use binary encoding */
1160 attrValueEncoding = LDAP_AVA_STRING;
1163 * LDAPv2 allows the attribute value to be quoted;
1164 * also, IA5 values are expected, in principle
1166 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1167 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1169 state = B4IA5VALUEQUOTED;
1173 if ( LDAP_DN_LDAPV2( flags ) ) {
1180 * here STRING means RFC 2253 string
1181 * FIXME: what about DCE strings?
1187 state = B4STRINGVALUE;
1192 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1200 switch ( LDAP_DN_FORMAT( flags ) ) {
1201 case LDAP_DN_FORMAT_LDAP:
1202 case LDAP_DN_FORMAT_LDAPV3:
1203 if ( str2strval( p, stoplen - ( p - str ),
1204 &attrValue, &p, flags,
1205 &attrValueEncoding ) ) {
1210 case LDAP_DN_FORMAT_DCE:
1211 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1224 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1231 case B4IA5VALUEQUOTED:
1233 /* lead quote already stripped */
1234 if ( quotedIA52strval( p, &attrValue,
1245 if ( !( flags & LDAP_DN_SKIP ) ) {
1249 * we accept empty values
1251 ava = ldapava_new( &attrType, &attrValue,
1252 attrValueEncoding );
1254 if ( ava == NULL ) {
1255 rc = LDAP_NO_MEMORY;
1258 tmpRDN[navas++] = ava;
1260 attrValue.bv_val = NULL;
1261 attrValue.bv_len = 0;
1264 * prepare room for new AVAs if needed
1266 if (navas == num_slots) {
1269 if ( tmpRDN == tmpRDN_ ) {
1270 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
1271 if ( tmp == NULL ) {
1272 rc = LDAP_NO_MEMORY;
1275 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1278 tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
1279 if ( tmp == NULL ) {
1280 rc = LDAP_NO_MEMORY;
1291 * if we got an AVA separator ('+', or ',' for DCE )
1292 * we expect a new AVA for this RDN; otherwise
1293 * we add the RDN to the DN
1295 switch ( LDAP_DN_FORMAT( flags ) ) {
1296 case LDAP_DN_FORMAT_LDAP:
1297 case LDAP_DN_FORMAT_LDAPV3:
1298 case LDAP_DN_FORMAT_LDAPV2:
1299 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1304 case LDAP_DN_FORMAT_DCE:
1305 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1313 * the RDN is over, phew
1316 if ( !( flags & LDAP_DN_SKIP ) ) {
1317 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1318 + sizeof(LDAPAVA *) * (navas+1) );
1319 if ( newRDN == NULL ) {
1320 rc = LDAP_NO_MEMORY;
1325 newRDN[0] = (LDAPAVA**)(newRDN+1);
1327 for (i=0; i<navas; i++)
1328 newRDN[0][i] = tmpRDN[i];
1329 newRDN[0][i] = NULL;
1337 /* they should have been used in an AVA */
1338 attrType.bv_val = NULL;
1339 attrValue.bv_val = NULL;
1354 /* They are set to NULL after they're used in an AVA */
1356 if ( attrValue.bv_val ) {
1357 free( attrValue.bv_val );
1360 for ( navas-- ; navas >= 0; navas-- ) {
1361 ldap_avafree( tmpRDN[navas] );
1366 if ( tmpRDN != tmpRDN_ ) {
1367 LDAP_FREE( tmpRDN );
1378 * reads in a UTF-8 string value, unescaping stuff:
1379 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1380 * '\' + HEXPAIR(p) -> unhex(p)
1383 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1385 const char *p, *end, *startPos, *endPos = NULL;
1386 ber_len_t len, escapes;
1393 end = str + stoplen;
1394 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1395 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1397 if ( p[ 0 ] == '\0' ) {
1400 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1405 if ( LDAP_DN_HEXPAIR( p ) ) {
1408 hexstr2bin( p, &c );
1411 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1414 * we assume the string is UTF-8
1416 *retFlags = LDAP_AVA_NONPRINTABLE;
1423 if ( LDAP_DN_PEDANTIC & flags ) {
1427 * we do not allow escaping
1428 * of chars that don't need
1429 * to and do not belong to
1434 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1435 if ( p[ 0 ] == '\0' ) {
1438 *retFlags = LDAP_AVA_NONPRINTABLE;
1440 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1441 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1444 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1446 * FIXME: maybe we can add
1447 * escapes if not pedantic?
1454 * we do allow unescaped spaces at the end
1455 * of the value only in non-pedantic mode
1457 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1458 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1459 if ( flags & LDAP_DN_PEDANTIC ) {
1463 /* strip trailing (unescaped) spaces */
1464 for ( endPos = p - 1;
1465 endPos > startPos + 1 &&
1466 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1467 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1474 if ( flags & LDAP_DN_SKIP ) {
1479 * FIXME: test memory?
1481 len = ( endPos ? endPos : p ) - startPos - escapes;
1484 if ( escapes == 0 ) {
1485 if ( *retFlags == LDAP_AVA_NONPRINTABLE ) {
1486 val->bv_val = LDAP_MALLOC( len + 1 );
1487 AC_MEMCPY( val->bv_val, startPos, len );
1488 val->bv_val[ len ] = '\0';
1490 val->bv_val = LDAP_STRNDUP( startPos, len );
1496 val->bv_val = LDAP_MALLOC( len + 1 );
1497 for ( s = 0, d = 0; d < len; ) {
1498 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1500 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1501 val->bv_val[ d++ ] =
1504 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1507 hexstr2bin( &startPos[ s ], &c );
1508 val->bv_val[ d++ ] = c;
1512 /* we should never get here */
1517 val->bv_val[ d++ ] = startPos[ s++ ];
1521 val->bv_val[ d ] = '\0';
1529 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1531 const char *p, *startPos, *endPos = NULL;
1532 ber_len_t len, escapes;
1540 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1541 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1543 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1550 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1555 * FIXME: can we accept anything else? I guess we need
1556 * to stop if a value is not legal
1561 * (unescaped) trailing spaces are trimmed must be silently ignored;
1564 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1565 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1566 if ( flags & LDAP_DN_PEDANTIC ) {
1570 /* strip trailing (unescaped) spaces */
1571 for ( endPos = p - 1;
1572 endPos > startPos + 1 &&
1573 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1574 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1581 if ( flags & LDAP_DN_SKIP ) {
1585 len = ( endPos ? endPos : p ) - startPos - escapes;
1587 if ( escapes == 0 ){
1588 val->bv_val = LDAP_STRNDUP( startPos, len );
1593 val->bv_val = LDAP_MALLOC( len + 1 );
1594 for ( s = 0, d = 0; d < len; ) {
1596 * This point is reached only if escapes
1597 * are properly used, so all we need to
1600 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1604 val->bv_val[ d++ ] = startPos[ s++ ];
1606 val->bv_val[ d ] = '\0';
1607 assert( strlen( val->bv_val ) == len );
1614 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1616 const char *p, *startPos, *endPos = NULL;
1617 ber_len_t len, escapes;
1629 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1630 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1632 if ( p[ 0 ] == '\0' ) {
1636 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1637 && ( LDAP_DN_PEDANTIC & flags ) ) {
1642 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1647 * FIXME: can we accept anything else? I guess we need
1648 * to stop if a value is not legal
1652 /* strip trailing (unescaped) spaces */
1654 endPos > startPos + 1 &&
1655 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1656 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1662 if ( flags & LDAP_DN_SKIP ) {
1666 len = ( endPos ? endPos : p ) - startPos - escapes;
1668 if ( escapes == 0 ) {
1669 val->bv_val = LDAP_STRNDUP( startPos, len );
1674 val->bv_val = LDAP_MALLOC( len + 1 );
1675 for ( s = 0, d = 0; d < len; ) {
1676 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1679 val->bv_val[ d++ ] = startPos[ s++ ];
1681 val->bv_val[ d ] = '\0';
1682 assert( strlen( val->bv_val ) == len );
1689 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1691 const char *p, *startPos, *endPos = NULL;
1693 unsigned escapes = 0;
1701 /* initial quote already eaten */
1702 for ( startPos = p = str; p[ 0 ]; p++ ) {
1704 * According to RFC 1779, the quoted value can
1705 * contain escaped as well as unescaped special values;
1706 * as a consequence we tolerate escaped values
1707 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1708 * (e.g. '","' -> '\,').
1710 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1711 if ( p[ 1 ] == '\0' ) {
1716 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1717 && ( LDAP_DN_PEDANTIC & flags ) ) {
1719 * do we allow to escape normal chars?
1720 * LDAPv2 does not allow any mechanism
1721 * for escaping chars with '\' and hex
1728 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1730 /* eat closing quotes */
1736 * FIXME: can we accept anything else? I guess we need
1737 * to stop if a value is not legal
1741 if ( endPos == NULL ) {
1745 /* Strip trailing (unescaped) spaces */
1746 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1751 if ( flags & LDAP_DN_SKIP ) {
1755 len = endPos - startPos - escapes;
1756 assert( endPos >= startPos + escapes );
1758 if ( escapes == 0 ) {
1759 val->bv_val = LDAP_STRNDUP( startPos, len );
1764 val->bv_val = LDAP_MALLOC( len + 1 );
1767 for ( s = d = 0; d < len; ) {
1768 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1771 val->bv_val[ d++ ] = str[ s++ ];
1773 val->bv_val[ d ] = '\0';
1774 assert( strlen( val->bv_val ) == len );
1781 hexstr2bin( const char *str, char *c )
1791 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1795 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1798 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1805 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1809 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1810 *c += c2 - 'A' + 10;
1812 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1813 *c += c2 - 'a' + 10;
1821 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1823 const char *p, *startPos, *endPos = NULL;
1833 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1834 switch ( LDAP_DN_FORMAT( flags ) ) {
1835 case LDAP_DN_FORMAT_LDAPV3:
1836 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1841 case LDAP_DN_FORMAT_LDAP:
1842 case LDAP_DN_FORMAT_LDAPV2:
1843 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1848 case LDAP_DN_FORMAT_DCE:
1849 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1855 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1856 if ( flags & LDAP_DN_PEDANTIC ) {
1861 for ( ; p[ 0 ]; p++ ) {
1862 switch ( LDAP_DN_FORMAT( flags ) ) {
1863 case LDAP_DN_FORMAT_LDAPV3:
1864 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1869 case LDAP_DN_FORMAT_LDAP:
1870 case LDAP_DN_FORMAT_LDAPV2:
1871 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1876 case LDAP_DN_FORMAT_DCE:
1877 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1886 if ( !LDAP_DN_HEXPAIR( p ) ) {
1894 if ( flags & LDAP_DN_SKIP ) {
1898 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1900 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1903 val->bv_val = LDAP_MALLOC( len + 1 );
1904 if ( val->bv_val == NULL ) {
1905 return( LDAP_NO_MEMORY );
1908 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1911 hexstr2bin( &startPos[ s ], &c );
1913 val->bv_val[ d ] = c;
1916 val->bv_val[ d ] = '\0';
1922 * convert a byte in a hexadecimal pair
1925 byte2hexpair( const char *val, char *pair )
1927 static const char hexdig[] = "0123456789ABCDEF";
1933 * we assume the string has enough room for the hex encoding
1937 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1938 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1944 * convert a binary value in hexadecimal pairs
1947 binval2hexstr( struct berval *val, char *str )
1954 if ( val->bv_len == 0 ) {
1959 * we assume the string has enough room for the hex encoding
1963 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
1964 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
1971 * Length of the string representation, accounting for escaped hex
1975 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
1977 ber_len_t l, cl = 1;
1979 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
1980 #ifdef PRETTY_ESCAPE
1981 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
1982 #endif /* PRETTY_ESCAPE */
1988 if ( val->bv_len == 0 ) {
1992 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
1997 if ( p[ 0 ] == '\0' ) {
2003 cl = LDAP_UTF8_CHARLEN2( p, cl );
2005 /* illegal utf-8 char! */
2008 } else if ( cl > 1 ) {
2011 for ( cnt = 1; cnt < cl; cnt++ ) {
2012 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2016 l += escaped_byte_len * cl;
2018 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2019 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2020 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2021 #ifdef PRETTY_ESCAPE
2023 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2025 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2029 * there might be some chars we want
2030 * to escape in form of a couple
2031 * of hexdigits for optimization purposes
2036 l += escaped_ascii_len;
2038 #else /* ! PRETTY_ESCAPE */
2040 #endif /* ! PRETTY_ESCAPE */
2053 * convert to string representation, escaping with hex the UTF-8 stuff;
2054 * assume the destination has enough room for escaping
2057 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2059 ber_len_t s, d, end;
2065 if ( val->bv_len == 0 ) {
2071 * we assume the string has enough room for the hex encoding
2074 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2080 if ( val->bv_val[ s ] == '\0' ) {
2090 * The length was checked in strval2strlen();
2091 * LDAP_UTF8_CHARLEN() should suffice
2093 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2097 * there might be some chars we want to escape in form
2098 * of a couple of hexdigits for optimization purposes
2100 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2101 #ifdef PRETTY_ESCAPE
2103 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2105 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2107 #else /* ! PRETTY_ESCAPE */
2108 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2109 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2110 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2112 #endif /* ! PRETTY_ESCAPE */
2116 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2121 } else if ( cl > 1 ) {
2123 str[ d++ ] = val->bv_val[ s++ ];
2127 #ifdef PRETTY_ESCAPE
2128 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2129 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2130 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2132 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2133 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2139 #endif /* PRETTY_ESCAPE */
2140 str[ d++ ] = val->bv_val[ s++ ];
2150 * Length of the IA5 string representation (no UTF-8 allowed)
2153 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2162 if ( val->bv_len == 0 ) {
2166 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2168 * Turn value into a binary encoded BER
2173 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2174 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2175 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2176 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2191 * convert to string representation (np UTF-8)
2192 * assume the destination has enough room for escaping
2195 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2197 ber_len_t s, d, end;
2203 if ( val->bv_len == 0 ) {
2208 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2210 * Turn value into a binary encoded BER
2217 * we assume the string has enough room for the hex encoding
2221 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2222 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2223 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2224 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2227 str[ d++ ] = val->bv_val[ s++ ];
2237 * Length of the (supposedly) DCE string representation,
2238 * accounting for escaped hex of UTF-8 chars
2241 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2250 if ( val->bv_len == 0 ) {
2254 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2256 * FIXME: Turn the value into a binary encoded BER?
2261 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2262 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2277 * convert to (supposedly) DCE string representation,
2278 * escaping with hex the UTF-8 stuff;
2279 * assume the destination has enough room for escaping
2282 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2290 if ( val->bv_len == 0 ) {
2295 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2297 * FIXME: Turn the value into a binary encoded BER?
2305 * we assume the string has enough room for the hex encoding
2309 for ( s = 0, d = 0; s < val->bv_len; ) {
2310 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2313 str[ d++ ] = val->bv_val[ s++ ];
2323 * Length of the (supposedly) AD canonical string representation,
2324 * accounting for escaped hex of UTF-8 chars
2327 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2336 if ( val->bv_len == 0 ) {
2340 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2342 * FIXME: Turn the value into a binary encoded BER?
2347 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2348 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2363 * convert to (supposedly) AD string representation,
2364 * escaping with hex the UTF-8 stuff;
2365 * assume the destination has enough room for escaping
2368 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2376 if ( val->bv_len == 0 ) {
2381 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2383 * FIXME: Turn the value into a binary encoded BER?
2391 * we assume the string has enough room for the hex encoding
2395 for ( s = 0, d = 0; s < val->bv_len; ) {
2396 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2399 str[ d++ ] = val->bv_val[ s++ ];
2409 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2410 * the first part of the AD representation of the DN is written in DNS
2411 * form, i.e. dot separated domain name components (as suggested
2412 * by Luke Howard, http://www.padl.com/~lukeh)
2415 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2418 int domain = 0, first = 1;
2419 ber_len_t l = 1; /* we move the null also */
2422 /* we are guaranteed there's enough memory in str */
2428 assert( *iRDN >= 0 );
2430 str = bv->bv_val + pos;
2432 for ( i = *iRDN; i >= 0; i-- ) {
2436 assert( dn[ 0 ][ i ] );
2439 assert( rdn[ 0 ][ 0 ] );
2440 ava = rdn[ 0 ][ 0 ];
2442 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2450 AC_MEMCPY( str, ava->la_value.bv_val,
2451 ava->la_value.bv_len + 1);
2452 l += ava->la_value.bv_len;
2455 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2456 AC_MEMCPY( str, ava->la_value.bv_val,
2457 ava->la_value.bv_len );
2458 str[ ava->la_value.bv_len ] = '.';
2459 l += ava->la_value.bv_len + 1;
2464 bv->bv_len = pos + l - 1;
2470 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2471 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2478 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2479 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2481 /* len(type) + '=' + '+' | ',' */
2482 l += ava->la_attr.bv_len + 2;
2484 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2485 /* octothorpe + twice the length */
2486 l += 1 + 2 * ava->la_value.bv_len;
2490 unsigned f = flags | ava->la_flags;
2492 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2505 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2506 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2511 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2512 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2514 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2515 ava->la_attr.bv_len );
2516 l += ava->la_attr.bv_len;
2520 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2522 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2525 l += 2 * ava->la_value.bv_len;
2529 unsigned f = flags | ava->la_flags;
2531 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2536 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2545 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2552 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2553 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2555 /* len(type) + '=' + ',' | '/' */
2556 l += ava->la_attr.bv_len + 2;
2558 switch ( ava->la_flags ) {
2559 case LDAP_AVA_BINARY:
2560 /* octothorpe + twice the length */
2561 l += 1 + 2 * ava->la_value.bv_len;
2564 case LDAP_AVA_STRING: {
2566 unsigned f = flags | ava->la_flags;
2568 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2586 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2591 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2592 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2597 str[ l++ ] = ( iAVA ? ',' : '/' );
2600 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2601 ava->la_attr.bv_len );
2602 l += ava->la_attr.bv_len;
2606 switch ( ava->la_flags ) {
2607 case LDAP_AVA_BINARY:
2609 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2612 l += 2 * ava->la_value.bv_len;
2615 case LDAP_AVA_STRING: {
2617 unsigned f = flags | ava->la_flags;
2619 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2637 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2647 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2648 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2651 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2653 /* FIXME: are binary values allowed in UFN? */
2654 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2655 /* octothorpe + twice the value */
2656 l += 1 + 2 * ava->la_value.bv_len;
2660 unsigned f = flags | ava->la_flags;
2662 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2675 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2680 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2681 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2683 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2685 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2688 l += 2 * ava->la_value.bv_len;
2692 unsigned f = flags | ava->la_flags;
2694 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2700 if ( rdn[ 0 ][ iAVA + 1 ]) {
2701 AC_MEMCPY( &str[ l ], " + ", 3 );
2705 AC_MEMCPY( &str[ l ], ", ", 2 );
2716 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2726 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2727 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2732 /* FIXME: are binary values allowed in UFN? */
2733 switch ( ava->la_flags ) {
2734 case LDAP_AVA_BINARY:
2735 /* octothorpe + twice the value */
2736 l += 1 + 2 * ava->la_value.bv_len;
2739 case LDAP_AVA_STRING: {
2741 unsigned f = flags | ava->la_flags;
2743 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2761 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2766 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2767 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2772 str[ l++ ] = ( iAVA ? ',' : '/' );
2775 switch ( ava->la_flags ) {
2776 case LDAP_AVA_BINARY:
2778 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2781 l += 2 * ava->la_value.bv_len;
2784 case LDAP_AVA_STRING: {
2786 unsigned f = flags | ava->la_flags;
2788 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2808 * Returns in str a string representation of rdn based on flags.
2809 * There is some duplication of code between this and ldap_dn2str;
2810 * this is wanted to reduce the allocation of temporary buffers.
2813 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2820 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2821 return LDAP_PARAM_ERROR;
2824 rc = ldap_rdn2bv( rdn, &bv, flags );
2830 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2840 if ( rdn == NULL ) {
2841 bv->bv_val = LDAP_STRDUP( "" );
2842 return( LDAP_SUCCESS );
2846 * This routine wastes "back" bytes at the end of the string
2849 switch ( LDAP_DN_FORMAT( flags ) ) {
2850 case LDAP_DN_FORMAT_LDAPV3:
2851 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2852 return LDAP_DECODING_ERROR;
2856 case LDAP_DN_FORMAT_LDAPV2:
2857 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2858 return LDAP_DECODING_ERROR;
2862 case LDAP_DN_FORMAT_UFN:
2863 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2864 return LDAP_DECODING_ERROR;
2868 case LDAP_DN_FORMAT_DCE:
2869 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2870 return LDAP_DECODING_ERROR;
2874 case LDAP_DN_FORMAT_AD_CANONICAL:
2875 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2876 return LDAP_DECODING_ERROR;
2881 return( LDAP_PARAM_ERROR );
2884 bv->bv_val = LDAP_MALLOC( l + 1 );
2886 switch ( LDAP_DN_FORMAT( flags ) ) {
2887 case LDAP_DN_FORMAT_LDAPV3:
2888 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2892 case LDAP_DN_FORMAT_LDAPV2:
2893 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2897 case LDAP_DN_FORMAT_UFN:
2898 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2902 case LDAP_DN_FORMAT_DCE:
2903 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2907 case LDAP_DN_FORMAT_AD_CANONICAL:
2908 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2913 /* need at least one of the previous */
2914 return LDAP_PARAM_ERROR;
2918 ldap_memfree( bv->bv_val );
2922 bv->bv_len = l - back;
2923 bv->bv_val[ bv->bv_len ] = '\0';
2925 return LDAP_SUCCESS;
2929 * Very bulk implementation; many optimizations can be performed
2930 * - a NULL dn results in an empty string ""
2933 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2934 * we must encode it in binary form ('#' + HEXPAIRs)
2935 * b) does DCE/AD support UTF-8?
2936 * no clue; don't think so.
2937 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2938 * use binary encoded BER
2940 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2947 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2948 return LDAP_PARAM_ERROR;
2951 rc = ldap_dn2bv( dn, &bv, flags );
2956 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
2959 int rc = LDAP_ENCODING_ERROR;
2962 /* stringifying helpers for LDAPv3/LDAPv2 */
2963 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2964 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2971 LDAP_LOG (( "getdn", LDAP_LEVEL_ARGS, "=> ldap_dn2bv(%u)\n%s%s",
2974 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
2978 * a null dn means an empty dn string
2979 * FIXME: better raise an error?
2982 bv->bv_val = LDAP_STRDUP( "" );
2983 return( LDAP_SUCCESS );
2986 switch ( LDAP_DN_FORMAT( flags ) ) {
2987 case LDAP_DN_FORMAT_LDAPV3:
2988 sv2l = strval2strlen;
2992 case LDAP_DN_FORMAT_LDAPV2:
2993 sv2l = strval2IA5strlen;
2994 sv2s = strval2IA5str;
2997 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
2999 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3001 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3002 goto return_results;
3008 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3009 rc = LDAP_NO_MEMORY;
3013 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3015 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3017 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
3019 LDAP_FREE( bv->bv_val );
3021 goto return_results;
3029 * trim the last ',' (the allocated memory
3030 * is one byte longer than required)
3032 bv->bv_len = len - 1;
3033 bv->bv_val[ bv->bv_len ] = '\0';
3038 case LDAP_DN_FORMAT_UFN: {
3040 * FIXME: quoting from RFC 1781:
3042 To take a distinguished name, and generate a name of this format with
3043 attribute types omitted, the following steps are followed.
3045 1. If the first attribute is of type CommonName, the type may be
3048 2. If the last attribute is of type Country, the type may be
3051 3. If the last attribute is of type Country, the last
3052 Organisation attribute may have the type omitted.
3054 4. All attributes of type OrganisationalUnit may have the type
3055 omitted, unless they are after an Organisation attribute or
3056 the first attribute is of type OrganisationalUnit.
3058 * this should be the pedantic implementation.
3060 * Here the standard implementation reflects
3061 * the one historically provided by OpenLDAP
3062 * (and UMIch, I presume), with the variant
3063 * of spaces and plusses (' + ') separating
3066 * A non-standard but nice implementation could
3067 * be to turn the final "dc" attributes into a
3068 * dot-separated domain.
3070 * Other improvements could involve the use of
3071 * friendly country names and so.
3074 int leftmost_dc = -1;
3076 #endif /* DC_IN_UFN */
3078 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3080 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3082 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3083 goto return_results;
3088 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3089 if ( leftmost_dc == -1 ) {
3095 #endif /* DC_IN_UFN */
3098 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3099 rc = LDAP_NO_MEMORY;
3104 if ( leftmost_dc == -1 ) {
3105 #endif /* DC_IN_UFN */
3106 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3108 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3110 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3112 LDAP_FREE( bv->bv_val );
3114 goto return_results;
3120 * trim the last ', ' (the allocated memory
3121 * is two bytes longer than required)
3123 bv->bv_len = len - 2;
3124 bv->bv_val[ bv->bv_len ] = '\0';
3127 last_iRDN = iRDN - 1;
3129 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3131 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3133 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3135 LDAP_FREE( bv->bv_val );
3137 goto return_results;
3142 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3143 LDAP_FREE( bv->bv_val );
3145 goto return_results;
3148 /* the string is correctly terminated by dn2domain */
3150 #endif /* DC_IN_UFN */
3156 case LDAP_DN_FORMAT_DCE:
3157 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3159 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3161 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3162 goto return_results;
3168 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3169 rc = LDAP_NO_MEMORY;
3173 for ( l = 0; iRDN--; ) {
3175 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3177 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3179 LDAP_FREE( bv->bv_val );
3181 goto return_results;
3189 bv->bv_val[ bv->bv_len ] = '\0';
3194 case LDAP_DN_FORMAT_AD_CANONICAL: {
3196 * Sort of UFN for DCE DNs: a slash ('/') separated
3197 * global->local DN with no types; strictly speaking,
3198 * the naming context should be a domain, which is
3199 * written in DNS-style, e.g. dot-deparated.
3203 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3207 * "microsoft.com/People/Bill,Gates"
3209 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3211 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3213 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3214 goto return_results;
3220 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3221 rc = LDAP_NO_MEMORY;
3226 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3227 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3229 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3231 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3232 flags, &rdnl, 0 ) ) {
3233 LDAP_FREE( bv->bv_val );
3235 goto return_results;
3244 * Strictly speaking, AD canonical requires
3245 * a DN to be in the form "..., dc=smtg",
3246 * i.e. terminated by a domain component
3248 if ( flags & LDAP_DN_PEDANTIC ) {
3249 LDAP_FREE( bv->bv_val );
3251 rc = LDAP_ENCODING_ERROR;
3255 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3257 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3259 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3260 flags, &rdnl, first ) ) {
3261 LDAP_FREE( bv->bv_val );
3263 goto return_results;
3273 bv->bv_val[ bv->bv_len ] = '\0';
3279 return LDAP_PARAM_ERROR;
3283 LDAP_LOG (( "getdn", LDAP_LEVEL_RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3284 bv->bv_val, flags, rc ));
3286 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3287 bv->bv_val, flags, rc );
3295 #include <openssl/x509.h>
3296 #include <openssl/err.h>
3298 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3299 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3300 * constructed DN will use numeric OIDs to identify attributeTypes,
3301 * and the func() will be invoked to rewrite the DN with the given
3304 * Otherwise the DN will use shortNames as defined in the OpenSSL
3307 * It's preferable to let slapd do the OID to attributeType mapping,
3308 * because the OpenSSL tables are known to have many typos in versions
3309 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3310 * so we're forced to use OpenSSL's mapping there.
3311 * -- Howard Chu 2002-04-18
3315 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3320 LDAPAVA *newAVA, *baseAVA;
3321 X509_NAME_ENTRY *ne;
3324 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3326 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3328 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3337 /* Get the number of AVAs. This is not necessarily the same as
3338 * the number of RDNs.
3340 navas = X509_NAME_entry_count( x509_name );
3342 /* Get the last element, to see how many RDNs there are */
3343 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3344 nrdns = ne->set + 1;
3346 /* Allocate the DN/RDN/AVA stuff as a single block */
3347 dnsize = sizeof(LDAPDN) + sizeof(LDAPRDN *) * (nrdns+1);
3348 dnsize += sizeof(LDAPRDN) * nrdns + sizeof(LDAPAVA *) * (navas+nrdns);
3349 dnsize += sizeof(LDAPAVA) * navas;
3350 if (dnsize > sizeof(ptrs)) {
3351 newDN = (LDAPDN *)LDAP_MALLOC( dnsize );
3352 if ( newDN == NULL )
3353 return LDAP_NO_MEMORY;
3355 newDN = (LDAPDN *)ptrs;
3358 newDN[0] = (LDAPRDN**)(newDN+1);
3359 newDN[0][nrdns] = NULL;
3360 newRDN = (LDAPRDN*)(newDN[0] + nrdns+1);
3361 newAVA = (LDAPAVA*)(newRDN + navas + nrdns*2);
3364 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3365 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3366 ne = X509_NAME_get_entry( x509_name, i );
3367 obj = X509_NAME_ENTRY_get_object( ne );
3368 str = X509_NAME_ENTRY_get_data( ne );
3370 /* If set changed, move to next RDN */
3371 if ( set != ne->set ) {
3372 /* If this is not the first time, end the
3373 * previous RDN and advance.
3376 newRDN[0][k] = NULL;
3377 newRDN = (LDAPRDN*)(newRDN[0]+k+1);
3379 newDN[0][j++] = newRDN;
3381 newRDN[0] = (LDAPAVA**)(newRDN+1);
3385 newAVA->la_private = NULL;
3386 newAVA->la_flags = LDAP_AVA_STRING;
3389 int n = OBJ_obj2nid( obj );
3393 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3394 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3396 get_oid: newAVA->la_attr.bv_val = oidptr;
3397 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3398 oidptr += newAVA->la_attr.bv_len + 1;
3399 oidrem -= newAVA->la_attr.bv_len + 1;
3401 /* Running out of OID buffer space? */
3403 if ( oidsize == 0 ) {
3404 oidsize = sizeof(oids) * 2;
3406 oidbuf = LDAP_MALLOC( oidsize );
3407 if ( oidbuf == NULL ) goto nomem;
3411 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3412 if ( oidbuf == NULL ) goto nomem;
3413 /* Buffer moved! Fix AVA pointers */
3414 if ( old != oidbuf ) {
3416 long dif = oidbuf - old;
3418 for (a=baseAVA; a<=newAVA; a++){
3419 if (a->la_attr.bv_val >= old &&
3420 a->la_attr.bv_val <= (old + oidsize))
3421 a->la_attr.bv_val += dif;
3424 oidptr = oidbuf + oidsize - oidrem;
3430 Val.bv_val = str->data;
3431 Val.bv_len = str->length;
3432 switch( str->type ) {
3433 case V_ASN1_UNIVERSALSTRING:
3434 /* This uses 32-bit ISO 10646-1 */
3435 csize = 4; goto to_utf8;
3436 case V_ASN1_BMPSTRING:
3437 /* This uses 16-bit ISO 10646-1 */
3438 csize = 2; goto to_utf8;
3439 case V_ASN1_T61STRING:
3440 /* This uses 8-bit, assume ISO 8859-1 */
3442 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3443 if (rc != LDAP_SUCCESS) goto nomem;
3444 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3446 case V_ASN1_UTF8STRING:
3447 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3448 /* This is already in UTF-8 encoding */
3449 case V_ASN1_IA5STRING:
3450 case V_ASN1_PRINTABLESTRING:
3451 /* These are always 7-bit strings */
3452 ber_dupbv( &newAVA->la_value, &Val );
3456 newRDN[0][k] = newAVA;
3460 newRDN[0][k] = NULL;
3463 rc = func( newDN, flags );
3464 if ( rc != LDAP_SUCCESS )
3468 rc = ldap_dn2bv( newDN, bv, LDAP_DN_FORMAT_LDAPV3 );
3471 for (;baseAVA < newAVA; baseAVA++) {
3472 LDAP_FREE( baseAVA->la_value.bv_val );
3476 LDAP_FREE( oidbuf );
3477 if ( newDN != (LDAPDN*) ptrs )
3481 #endif /* HAVE_TLS */