3 * Copyright 1998-2003 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, int *retFlags, void *ctx );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags, void *ctx );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags, void *ctx );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags, void *ctx );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags, void *ctx );
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, void *ctx );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
88 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
94 assert( LDAP_VALID(ld) );
95 assert( entry != NULL );
97 tmp = *entry->lm_ber; /* struct copy */
98 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
99 ld->ld_errno = LDAP_DECODING_ERROR;
107 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
110 BerElement tmp, *ber;
112 int rc = LDAP_SUCCESS;
115 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
117 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
120 assert( ld != NULL );
121 assert( LDAP_VALID(ld) );
122 assert( entry != NULL );
123 assert( dn != NULL );
130 ber = ldap_alloc_ber_with_options( ld );
132 return LDAP_NO_MEMORY;
139 *ber = *entry->lm_ber; /* struct copy */
140 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
141 rc = ld->ld_errno = LDAP_DECODING_ERROR;
143 if ( rc == LDAP_SUCCESS ) {
144 /* set the length to avoid overrun */
145 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
146 if( rc != LBER_OPT_SUCCESS ) {
147 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
150 if ( rc != LDAP_SUCCESS && berout ) {
158 * RFC 1823 ldap_dn2ufn
161 ldap_dn2ufn( LDAP_CONST char *dn )
166 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
168 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
171 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
172 &out, LDAP_DN_FORMAT_UFN );
178 * RFC 1823 ldap_explode_dn
181 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
184 char **values = NULL;
186 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
189 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
191 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
194 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
199 if( tmpDN == NULL ) {
200 values = LDAP_MALLOC( sizeof( char * ) );
201 if( values == NULL ) return NULL;
207 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
209 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
210 if ( values == NULL ) {
211 ldap_dnfree( tmpDN );
215 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
216 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
218 ldap_dnfree( tmpDN );
219 values[ iRDN ] = NULL;
225 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
228 char **values = NULL;
233 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
235 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
239 * we only parse the first rdn
240 * FIXME: we prefer efficiency over checking if the _ENTIRE_
243 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
248 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
249 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
250 if ( values == NULL ) {
251 ldap_rdnfree( tmpRDN );
255 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
256 ber_len_t l = 0, vl, al = 0;
258 LDAPAVA *ava = tmpRDN[ iAVA ];
260 if ( ava->la_flags & LDAP_AVA_BINARY ) {
261 vl = 1 + 2 * ava->la_value.bv_len;
264 if ( strval2strlen( &ava->la_value,
265 ava->la_flags, &vl ) ) {
271 al = ava->la_attr.bv_len;
272 l = vl + ava->la_attr.bv_len + 1;
274 str = LDAP_MALLOC( l + 1 );
275 AC_MEMCPY( str, ava->la_attr.bv_val,
276 ava->la_attr.bv_len );
281 str = LDAP_MALLOC( l + 1 );
284 if ( ava->la_flags & LDAP_AVA_BINARY ) {
286 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
291 if ( strval2str( &ava->la_value, &str[ al ],
292 ava->la_flags, &vl ) ) {
298 values[ iAVA ] = str;
300 values[ iAVA ] = NULL;
302 ldap_rdnfree( tmpRDN );
307 LBER_VFREE( values );
308 ldap_rdnfree( tmpRDN );
313 ldap_dn2dcedn( LDAP_CONST char *dn )
318 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
320 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
323 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
324 &out, LDAP_DN_FORMAT_DCE );
330 ldap_dcedn2dn( LDAP_CONST char *dce )
335 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
337 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
340 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
346 ldap_dn2ad_canonical( LDAP_CONST char *dn )
351 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
353 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
356 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
357 &out, LDAP_DN_FORMAT_AD_CANONICAL );
363 * function that changes the string representation of dnin
364 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
367 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
368 * plus some rfc 1779)
369 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
370 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
371 * LDAP_DN_FORMAT_DCE (?)
373 * fout can be any of the above except
374 * LDAP_DN_FORMAT_LDAP
376 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
377 * LDAP_DN_FORMAT_AD_CANONICAL (?)
380 ldap_dn_normalize( LDAP_CONST char *dnin,
381 unsigned fin, char **dnout, unsigned fout )
387 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
389 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
396 if ( dnin == NULL ) {
397 return( LDAP_SUCCESS );
400 rc = ldap_str2dn( dnin , &tmpDN, fin );
401 if ( rc != LDAP_SUCCESS ) {
405 rc = ldap_dn2str( tmpDN, dnout, fout );
407 ldap_dnfree( tmpDN );
415 /* #define B4ATTRTYPE 0x0001 */
416 #define B4OIDATTRTYPE 0x0002
417 #define B4STRINGATTRTYPE 0x0003
419 #define B4AVAEQUALS 0x0100
420 #define B4AVASEP 0x0200
421 #define B4RDNSEP 0x0300
422 #define GOTAVA 0x0400
424 #define B4ATTRVALUE 0x0010
425 #define B4STRINGVALUE 0x0020
426 #define B4IA5VALUEQUOTED 0x0030
427 #define B4IA5VALUE 0x0040
428 #define B4BINARYVALUE 0x0050
431 * Helpers (mostly from slap.h)
432 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
433 * Macros assume "C" Locale (ASCII)
435 #define LDAP_DN_ASCII_SPACE(c) \
436 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
437 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
438 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
439 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
441 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
442 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
443 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
444 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
445 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
446 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
449 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
450 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
451 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
452 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
453 #define LDAP_DN_ATTRDESC_CHAR(c) \
454 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
456 /* special symbols */
457 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
458 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
459 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
460 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
461 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
462 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
463 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
464 #define LDAP_DN_VALUE_END(c) \
465 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
466 #define LDAP_DN_NE(c) \
467 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
468 || LDAP_DN_QUOTES(c) || (c) == '<' || (c) == '>' )
469 #define LDAP_DN_MAYESCAPE(c) \
470 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
471 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
472 #define LDAP_DN_NEEDESCAPE(c) \
473 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
474 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
475 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
476 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
477 #define LDAP_DN_WILLESCAPE_CHAR(c) \
478 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
479 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
480 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
481 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
484 #define LDAP_DN_VALUE_END_V2(c) \
485 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
487 #define LDAP_DN_V2_SPECIAL(c) \
488 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
489 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
490 || LDAP_DN_OCTOTHORPE(c) )
491 #define LDAP_DN_V2_PAIR(c) \
492 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
495 * DCE (mostly from Luke Howard and IBM implementation for AIX)
497 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
498 * Here escapes and valid chars for GDS are considered; as soon as more
499 * specific info is found, the macros will be updated.
501 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
502 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
504 * Metachars: '/', ',', '=', '\'.
506 * the '\' is used to escape other metachars.
512 * Attribute types must start with alphabetic chars and can contain
513 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
515 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
516 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
517 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
518 #define LDAP_DN_VALUE_END_DCE(c) \
519 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
520 #define LDAP_DN_NEEDESCAPE_DCE(c) \
521 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
524 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
525 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
526 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
527 #define LDAP_DN_VALUE_END_AD(c) \
528 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
529 #define LDAP_DN_NEEDESCAPE_AD(c) \
530 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
533 #define LDAP_DN_HEXPAIR(s) \
534 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
535 /* better look at the AttributeDescription? */
537 /* FIXME: no composite rdn or non-"dc" types, right?
538 * (what about "dc" in OID form?) */
539 /* FIXME: we do not allow binary values in domain, right? */
540 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
541 /* NOTE: don't use strcasecmp() as it is locale specific! */
542 #define LDAP_DC_ATTR "dc"
543 #define LDAP_DC_ATTRU "DC"
544 #define LDAP_DN_IS_RDN_DC( r ) \
545 ( (r) && (r)[0] && !(r)[1] \
546 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
547 && ((r)[0]->la_attr.bv_len == 2) \
548 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
549 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
550 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
551 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
553 /* Composite rules */
554 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
555 ( LDAP_DN_LDAPV2(f) \
556 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
557 #define LDAP_DN_ALLOW_SPACES(f) \
558 ( LDAP_DN_LDAPV2(f) \
559 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
560 #define LDAP_DN_LDAP(f) \
561 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
562 #define LDAP_DN_LDAPV3(f) \
563 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
564 #define LDAP_DN_LDAPV2(f) \
565 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
566 #define LDAP_DN_DCE(f) \
567 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
568 #define LDAP_DN_UFN(f) \
569 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
570 #define LDAP_DN_ADC(f) \
571 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
572 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
575 * LDAPAVA helpers (will become part of the API for operations
576 * on structural representations of DNs).
579 ldapava_new( const struct berval *attr, const struct berval *val,
580 unsigned flags, void *ctx )
587 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
590 ava->la_attr.bv_len = attr->bv_len;
591 ava->la_attr.bv_val = (char *)(ava+1);
592 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
593 ava->la_attr.bv_val[attr->bv_len] = '\0';
595 ava->la_value = *val;
596 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
598 ava->la_private = NULL;
605 ldapava_free( LDAPAVA *ava, void *ctx )
610 /* ava's private must be freed by caller
611 * (at present let's skip this check because la_private
612 * basically holds static data) */
613 assert( ava->la_private == NULL );
616 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
617 LDAP_FREEX( ava->la_value.bv_val, ctx );
619 LDAP_FREEX( ava, ctx );
623 ldap_rdnfree( LDAPRDN rdn )
625 ldap_rdnfree_x( rdn, NULL );
629 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
637 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
638 ldapava_free( rdn[ iAVA ], ctx );
641 LDAP_FREEX( rdn, ctx );
645 ldap_dnfree( LDAPDN dn )
647 ldap_dnfree_x( dn, NULL );
651 ldap_dnfree_x( LDAPDN dn, void *ctx )
659 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
660 ldap_rdnfree_x( dn[ iRDN ], ctx );
663 LDAP_FREEX( dn, ctx );
667 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
668 * into a structural representation of the DN, by separating attribute
669 * types and values encoded in the more appropriate form, which is
670 * string or OID for attribute types and binary form of the BER encoded
671 * value or Unicode string. Formats different from LDAPv3 are parsed
672 * according to their own rules and turned into the more appropriate
673 * form according to LDAPv3.
675 * NOTE: I realize the code is getting spaghettish; it is rather
676 * experimental and will hopefully turn into something more simple
677 * and readable as soon as it works as expected.
681 * Default sizes of AVA and RDN static working arrays; if required
682 * the are dynamically resized. The values can be tuned in case
683 * of special requirements (e.g. very deep DN trees or high number
686 #define TMP_AVA_SLOTS 8
687 #define TMP_RDN_SLOTS 32
690 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
696 bv.bv_len = strlen( str );
697 bv.bv_val = (char *) str;
699 return ldap_bv2dn_x( &bv, dn, flags, NULL );
703 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
705 return ldap_bv2dn_x( bv, dn, flags, NULL );
709 ldap_bv2dn_x( struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )
712 int rc = LDAP_DECODING_ERROR;
716 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
717 int num_slots = TMP_RDN_SLOTS;
718 char *str = bv->bv_val;
719 char *end = str + bv->bv_len;
722 assert( bv->bv_val );
726 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
728 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
733 switch ( LDAP_DN_FORMAT( flags ) ) {
734 case LDAP_DN_FORMAT_LDAP:
735 case LDAP_DN_FORMAT_LDAPV3:
736 case LDAP_DN_FORMAT_LDAPV2:
737 case LDAP_DN_FORMAT_DCE:
740 /* unsupported in str2dn */
741 case LDAP_DN_FORMAT_UFN:
742 case LDAP_DN_FORMAT_AD_CANONICAL:
743 return LDAP_PARAM_ERROR;
745 case LDAP_DN_FORMAT_LBER:
747 return LDAP_PARAM_ERROR;
750 if ( bv->bv_len == 0 ) {
754 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
755 /* value must have embedded NULs */
756 return LDAP_DECODING_ERROR;
760 if ( LDAP_DN_DCE( flags ) ) {
763 * (from Luke Howard: thnx) A RDN separator is required
764 * at the beginning of an (absolute) DN.
766 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
772 * actually we do not want to accept by default the DCE form,
773 * we do not want to auto-detect it
776 } else if ( LDAP_DN_LDAP( flags ) ) {
778 * if dn starts with '/' let's make it a DCE dn
780 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
781 flags |= LDAP_DN_FORMAT_DCE;
787 for ( ; p < end; p++ ) {
790 tmpbv.bv_len = bv->bv_len - ( p - str );
791 tmpbv.bv_val = (char *)p;
793 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
794 if ( err != LDAP_SUCCESS ) {
799 * We expect a rdn separator
801 if ( p < end && p[ 0 ] ) {
802 switch ( LDAP_DN_FORMAT( flags ) ) {
803 case LDAP_DN_FORMAT_LDAPV3:
804 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
805 rc = LDAP_DECODING_ERROR;
810 case LDAP_DN_FORMAT_LDAP:
811 case LDAP_DN_FORMAT_LDAPV2:
812 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
813 rc = LDAP_DECODING_ERROR;
818 case LDAP_DN_FORMAT_DCE:
819 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
820 rc = LDAP_DECODING_ERROR;
828 tmpDN[nrdns++] = newRDN;
832 * make the static RDN array dynamically rescalable
834 if ( nrdns == num_slots ) {
837 if ( tmpDN == tmpDN_ ) {
838 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
843 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
846 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
857 if ( p >= end || p[ 0 ] == '\0' ) {
859 * the DN is over, phew
861 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
862 if ( newDN == NULL ) {
868 if ( LDAP_DN_DCE( flags ) ) {
869 /* add in reversed order */
870 for ( i=0; i<nrdns; i++ )
871 newDN[i] = tmpDN[nrdns-1-i];
873 for ( i=0; i<nrdns; i++ )
885 ldap_rdnfree_x( newRDN, ctx );
888 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
889 ldap_rdnfree_x( tmpDN[nrdns], ctx );
894 if ( tmpDN != tmpDN_ ) {
895 LDAP_FREEX( tmpDN, ctx );
899 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
902 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
912 * Parses a relative DN according to flags up to a rdn separator
913 * or to the end of str.
914 * Returns the rdn and a pointer to the string continuation, which
915 * corresponds to the rdn separator or to '\0' in case the string is over.
918 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
919 char **n_in, unsigned flags )
924 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
926 bv.bv_len = strlen( str );
927 bv.bv_val = (char *) str;
929 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
933 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
934 char **n_in, unsigned flags )
936 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
940 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
941 char **n_in, unsigned flags, void *ctx )
943 const char **n = (const char **) n_in;
947 int rc = LDAP_DECODING_ERROR;
948 int attrTypeEncoding = LDAP_AVA_STRING,
949 attrValueEncoding = LDAP_AVA_STRING;
951 struct berval attrType = { 0, NULL };
952 struct berval attrValue = { 0, NULL };
954 LDAPRDN newRDN = NULL;
955 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
956 int num_slots = TMP_AVA_SLOTS;
962 assert( bv->bv_len );
963 assert( bv->bv_val );
964 assert( rdn || flags & LDAP_DN_SKIP );
968 stoplen = bv->bv_len;
975 switch ( LDAP_DN_FORMAT( flags ) ) {
976 case LDAP_DN_FORMAT_LDAP:
977 case LDAP_DN_FORMAT_LDAPV3:
978 case LDAP_DN_FORMAT_LDAPV2:
979 case LDAP_DN_FORMAT_DCE:
982 /* unsupported in str2dn */
983 case LDAP_DN_FORMAT_UFN:
984 case LDAP_DN_FORMAT_AD_CANONICAL:
985 return LDAP_PARAM_ERROR;
987 case LDAP_DN_FORMAT_LBER:
989 return LDAP_PARAM_ERROR;
992 if ( bv->bv_len == 0 ) {
997 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
998 /* value must have embedded NULs */
999 return LDAP_DECODING_ERROR;
1003 for ( ; p[ 0 ] || state == GOTAVA; ) {
1006 * The parser in principle advances one token a time,
1007 * or toggles state if preferable.
1012 * an AttributeType can be encoded as:
1013 * - its string representation; in detail, implementations
1014 * MUST recognize AttributeType string type names listed
1015 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1016 * MAY recognize other names.
1017 * - its numeric OID (a dotted decimal string); in detail
1018 * RFC 2253 asserts that ``Implementations MUST allow
1019 * an oid in the attribute type to be prefixed by one
1020 * of the character strings "oid." or "OID."''. As soon
1021 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1022 * I'm not sure whether this is required or not any
1023 * longer; to be liberal, we still implement it.
1026 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1027 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1034 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1035 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1040 /* whitespace is allowed (and trimmed) */
1042 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1047 /* error: we expected an AVA */
1053 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1054 state = B4OIDATTRTYPE;
1058 /* else must be alpha */
1059 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1063 /* LDAPv2 "oid." prefix */
1064 if ( LDAP_DN_LDAPV2( flags ) ) {
1066 * to be overly pedantic, we only accept
1069 if ( flags & LDAP_DN_PEDANTIC ) {
1070 if ( !strncmp( p, "OID.", 4 )
1071 || !strncmp( p, "oid.", 4 ) ) {
1073 state = B4OIDATTRTYPE;
1077 if ( !strncasecmp( p, "oid.", 4 ) ) {
1079 state = B4OIDATTRTYPE;
1085 state = B4STRINGATTRTYPE;
1088 case B4OIDATTRTYPE: {
1089 int err = LDAP_SUCCESS;
1091 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1094 if ( err != LDAP_SUCCESS ) {
1097 attrType.bv_len = p - attrType.bv_val;
1099 attrTypeEncoding = LDAP_AVA_BINARY;
1101 state = B4AVAEQUALS;
1105 case B4STRINGATTRTYPE: {
1106 const char *startPos, *endPos = NULL;
1110 * the starting char has been found to be
1111 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1112 * FIXME: DCE attr types seem to have a more
1113 * restrictive syntax (no '-' ...)
1115 for ( startPos = p++; p[ 0 ]; p++ ) {
1116 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1120 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1123 * RFC 2253 does not explicitly
1124 * allow lang extensions to attribute
1127 if ( flags & LDAP_DN_PEDANTIC ) {
1132 * we trim ';' and following lang
1133 * and so from attribute types
1136 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1137 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1145 len = ( endPos ? endPos : p ) - startPos;
1150 attrTypeEncoding = LDAP_AVA_STRING;
1153 * here we need to decide whether to use it as is
1154 * or turn it in OID form; as a consequence, we
1155 * need to decide whether to binary encode the value
1158 state = B4AVAEQUALS;
1160 if ( flags & LDAP_DN_SKIP ) {
1164 attrType.bv_val = (char *)startPos;
1165 attrType.bv_len = len;
1171 /* spaces may not be allowed */
1172 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1173 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1178 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1183 /* need equal sign */
1184 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1189 /* spaces may not be allowed */
1190 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1191 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1196 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1202 * octothorpe means a BER encoded value will follow
1203 * FIXME: I don't think DCE will allow it
1205 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1207 attrValueEncoding = LDAP_AVA_BINARY;
1208 state = B4BINARYVALUE;
1212 /* STRING value expected */
1215 * if we're pedantic, an attribute type in OID form
1216 * SHOULD imply a BER encoded attribute value; we
1217 * should at least issue a warning
1219 if ( ( flags & LDAP_DN_PEDANTIC )
1220 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1221 /* OID attrType SHOULD use binary encoding */
1225 attrValueEncoding = LDAP_AVA_STRING;
1228 * LDAPv2 allows the attribute value to be quoted;
1229 * also, IA5 values are expected, in principle
1231 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1232 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1234 state = B4IA5VALUEQUOTED;
1238 if ( LDAP_DN_LDAPV2( flags ) ) {
1245 * here STRING means RFC 2253 string
1246 * FIXME: what about DCE strings?
1252 state = B4STRINGVALUE;
1257 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1265 switch ( LDAP_DN_FORMAT( flags ) ) {
1266 case LDAP_DN_FORMAT_LDAP:
1267 case LDAP_DN_FORMAT_LDAPV3:
1268 if ( str2strval( p, stoplen - ( p - str ),
1269 &attrValue, &p, flags,
1270 &attrValueEncoding, ctx ) ) {
1275 case LDAP_DN_FORMAT_DCE:
1276 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1289 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1296 case B4IA5VALUEQUOTED:
1298 /* lead quote already stripped */
1299 if ( quotedIA52strval( p, &attrValue,
1300 &p, flags, ctx ) ) {
1310 if ( !( flags & LDAP_DN_SKIP ) ) {
1314 * we accept empty values
1316 ava = ldapava_new( &attrType, &attrValue,
1317 attrValueEncoding, ctx );
1318 if ( ava == NULL ) {
1319 rc = LDAP_NO_MEMORY;
1322 tmpRDN[navas++] = ava;
1324 attrValue.bv_val = NULL;
1325 attrValue.bv_len = 0;
1328 * prepare room for new AVAs if needed
1330 if (navas == num_slots) {
1333 if ( tmpRDN == tmpRDN_ ) {
1334 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1335 if ( tmp == NULL ) {
1336 rc = LDAP_NO_MEMORY;
1339 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1342 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1343 if ( tmp == NULL ) {
1344 rc = LDAP_NO_MEMORY;
1355 * if we got an AVA separator ('+', or ',' for DCE )
1356 * we expect a new AVA for this RDN; otherwise
1357 * we add the RDN to the DN
1359 switch ( LDAP_DN_FORMAT( flags ) ) {
1360 case LDAP_DN_FORMAT_LDAP:
1361 case LDAP_DN_FORMAT_LDAPV3:
1362 case LDAP_DN_FORMAT_LDAPV2:
1363 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1368 case LDAP_DN_FORMAT_DCE:
1369 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1377 * the RDN is over, phew
1380 if ( !( flags & LDAP_DN_SKIP ) ) {
1381 newRDN = (LDAPRDN)LDAP_MALLOCX(
1382 sizeof(LDAPAVA) * (navas+1), ctx );
1383 if ( newRDN == NULL ) {
1384 rc = LDAP_NO_MEMORY;
1387 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1388 newRDN[navas] = NULL;
1396 /* they should have been used in an AVA */
1397 attrType.bv_val = NULL;
1398 attrValue.bv_val = NULL;
1413 /* They are set to NULL after they're used in an AVA */
1415 if ( attrValue.bv_val ) {
1416 LDAP_FREEX( attrValue.bv_val, ctx );
1419 for ( navas-- ; navas >= 0; navas-- ) {
1420 ldapava_free( tmpRDN[navas], ctx );
1425 if ( tmpRDN != tmpRDN_ ) {
1426 LDAP_FREEX( tmpRDN, ctx );
1437 * reads in a UTF-8 string value, unescaping stuff:
1438 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1439 * '\' + HEXPAIR(p) -> unhex(p)
1442 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1444 const char *p, *end, *startPos, *endPos = NULL;
1445 ber_len_t len, escapes;
1452 end = str + stoplen;
1453 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1454 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1456 if ( p[ 0 ] == '\0' ) {
1459 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1464 if ( LDAP_DN_HEXPAIR( p ) ) {
1467 hexstr2bin( p, &c );
1470 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1473 * we assume the string is UTF-8
1475 *retFlags = LDAP_AVA_NONPRINTABLE;
1482 if ( LDAP_DN_PEDANTIC & flags ) {
1486 * we do not allow escaping
1487 * of chars that don't need
1488 * to and do not belong to
1493 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1494 if ( p[ 0 ] == '\0' ) {
1497 *retFlags = LDAP_AVA_NONPRINTABLE;
1499 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1500 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1503 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1505 * FIXME: maybe we can add
1506 * escapes if not pedantic?
1513 * we do allow unescaped spaces at the end
1514 * of the value only in non-pedantic mode
1516 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1517 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1518 if ( flags & LDAP_DN_PEDANTIC ) {
1522 /* strip trailing (unescaped) spaces */
1523 for ( endPos = p - 1;
1524 endPos > startPos + 1 &&
1525 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1526 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1533 if ( flags & LDAP_DN_SKIP ) {
1538 * FIXME: test memory?
1540 len = ( endPos ? endPos : p ) - startPos - escapes;
1543 if ( escapes == 0 ) {
1544 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1545 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1546 AC_MEMCPY( val->bv_val, startPos, len );
1547 val->bv_val[ len ] = '\0';
1549 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1555 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1556 for ( s = 0, d = 0; d < len; ) {
1557 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1559 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1560 val->bv_val[ d++ ] =
1563 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1566 hexstr2bin( &startPos[ s ], &c );
1567 val->bv_val[ d++ ] = c;
1571 /* we should never get here */
1576 val->bv_val[ d++ ] = startPos[ s++ ];
1580 val->bv_val[ d ] = '\0';
1588 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1590 const char *p, *startPos, *endPos = NULL;
1591 ber_len_t len, escapes;
1599 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1600 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1602 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1609 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1614 * FIXME: can we accept anything else? I guess we need
1615 * to stop if a value is not legal
1620 * (unescaped) trailing spaces are trimmed must be silently ignored;
1623 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1624 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1625 if ( flags & LDAP_DN_PEDANTIC ) {
1629 /* strip trailing (unescaped) spaces */
1630 for ( endPos = p - 1;
1631 endPos > startPos + 1 &&
1632 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1633 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1640 if ( flags & LDAP_DN_SKIP ) {
1644 len = ( endPos ? endPos : p ) - startPos - escapes;
1646 if ( escapes == 0 ){
1647 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1652 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1653 for ( s = 0, d = 0; d < len; ) {
1655 * This point is reached only if escapes
1656 * are properly used, so all we need to
1659 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1663 val->bv_val[ d++ ] = startPos[ s++ ];
1665 val->bv_val[ d ] = '\0';
1666 assert( strlen( val->bv_val ) == len );
1673 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1675 const char *p, *startPos, *endPos = NULL;
1676 ber_len_t len, escapes;
1688 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1689 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1691 if ( p[ 0 ] == '\0' ) {
1695 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1696 && ( LDAP_DN_PEDANTIC & flags ) ) {
1701 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1706 * FIXME: can we accept anything else? I guess we need
1707 * to stop if a value is not legal
1711 /* strip trailing (unescaped) spaces */
1713 endPos > startPos + 1 &&
1714 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1715 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1721 if ( flags & LDAP_DN_SKIP ) {
1725 len = ( endPos ? endPos : p ) - startPos - escapes;
1727 if ( escapes == 0 ) {
1728 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1733 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1734 for ( s = 0, d = 0; d < len; ) {
1735 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1738 val->bv_val[ d++ ] = startPos[ s++ ];
1740 val->bv_val[ d ] = '\0';
1741 assert( strlen( val->bv_val ) == len );
1748 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1750 const char *p, *startPos, *endPos = NULL;
1752 unsigned escapes = 0;
1760 /* initial quote already eaten */
1761 for ( startPos = p = str; p[ 0 ]; p++ ) {
1763 * According to RFC 1779, the quoted value can
1764 * contain escaped as well as unescaped special values;
1765 * as a consequence we tolerate escaped values
1766 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1767 * (e.g. '","' -> '\,').
1769 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1770 if ( p[ 1 ] == '\0' ) {
1775 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1776 && ( LDAP_DN_PEDANTIC & flags ) ) {
1778 * do we allow to escape normal chars?
1779 * LDAPv2 does not allow any mechanism
1780 * for escaping chars with '\' and hex
1787 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1789 /* eat closing quotes */
1795 * FIXME: can we accept anything else? I guess we need
1796 * to stop if a value is not legal
1800 if ( endPos == NULL ) {
1804 /* Strip trailing (unescaped) spaces */
1805 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1810 if ( flags & LDAP_DN_SKIP ) {
1814 len = endPos - startPos - escapes;
1815 assert( endPos >= startPos + escapes );
1817 if ( escapes == 0 ) {
1818 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1823 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1826 for ( s = d = 0; d < len; ) {
1827 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1830 val->bv_val[ d++ ] = str[ s++ ];
1832 val->bv_val[ d ] = '\0';
1833 assert( strlen( val->bv_val ) == len );
1840 hexstr2bin( const char *str, char *c )
1850 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1854 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1857 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1864 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1868 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1869 *c += c2 - 'A' + 10;
1871 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1872 *c += c2 - 'a' + 10;
1880 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1882 const char *p, *startPos, *endPos = NULL;
1892 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1893 switch ( LDAP_DN_FORMAT( flags ) ) {
1894 case LDAP_DN_FORMAT_LDAPV3:
1895 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1900 case LDAP_DN_FORMAT_LDAP:
1901 case LDAP_DN_FORMAT_LDAPV2:
1902 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1907 case LDAP_DN_FORMAT_DCE:
1908 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1914 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1915 if ( flags & LDAP_DN_PEDANTIC ) {
1920 for ( ; p[ 0 ]; p++ ) {
1921 switch ( LDAP_DN_FORMAT( flags ) ) {
1922 case LDAP_DN_FORMAT_LDAPV3:
1923 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1928 case LDAP_DN_FORMAT_LDAP:
1929 case LDAP_DN_FORMAT_LDAPV2:
1930 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1935 case LDAP_DN_FORMAT_DCE:
1936 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1945 if ( !LDAP_DN_HEXPAIR( p ) ) {
1953 if ( flags & LDAP_DN_SKIP ) {
1957 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1959 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1962 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1963 if ( val->bv_val == NULL ) {
1964 return( LDAP_NO_MEMORY );
1967 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1970 hexstr2bin( &startPos[ s ], &c );
1972 val->bv_val[ d ] = c;
1975 val->bv_val[ d ] = '\0';
1981 * convert a byte in a hexadecimal pair
1984 byte2hexpair( const char *val, char *pair )
1986 static const char hexdig[] = "0123456789ABCDEF";
1992 * we assume the string has enough room for the hex encoding
1996 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1997 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2003 * convert a binary value in hexadecimal pairs
2006 binval2hexstr( struct berval *val, char *str )
2013 if ( val->bv_len == 0 ) {
2018 * we assume the string has enough room for the hex encoding
2022 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2023 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2030 * Length of the string representation, accounting for escaped hex
2034 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2036 ber_len_t l, cl = 1;
2038 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2039 #ifdef PRETTY_ESCAPE
2040 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2041 #endif /* PRETTY_ESCAPE */
2047 if ( val->bv_len == 0 ) {
2051 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2056 if ( p[ 0 ] == '\0' ) {
2062 cl = LDAP_UTF8_CHARLEN2( p, cl );
2064 /* illegal utf-8 char! */
2067 } else if ( cl > 1 ) {
2070 for ( cnt = 1; cnt < cl; cnt++ ) {
2071 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2075 l += escaped_byte_len * cl;
2077 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2078 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2079 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2080 #ifdef PRETTY_ESCAPE
2082 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2084 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2088 * there might be some chars we want
2089 * to escape in form of a couple
2090 * of hexdigits for optimization purposes
2095 l += escaped_ascii_len;
2097 #else /* ! PRETTY_ESCAPE */
2099 #endif /* ! PRETTY_ESCAPE */
2112 * convert to string representation, escaping with hex the UTF-8 stuff;
2113 * assume the destination has enough room for escaping
2116 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2118 ber_len_t s, d, end;
2124 if ( val->bv_len == 0 ) {
2130 * we assume the string has enough room for the hex encoding
2133 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2139 if ( val->bv_val[ s ] == '\0' ) {
2149 * The length was checked in strval2strlen();
2150 * LDAP_UTF8_CHARLEN() should suffice
2152 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2156 * there might be some chars we want to escape in form
2157 * of a couple of hexdigits for optimization purposes
2159 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2160 #ifdef PRETTY_ESCAPE
2162 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2164 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2166 #else /* ! PRETTY_ESCAPE */
2167 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2168 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2169 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2171 #endif /* ! PRETTY_ESCAPE */
2175 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2180 } else if ( cl > 1 ) {
2182 str[ d++ ] = val->bv_val[ s++ ];
2186 #ifdef PRETTY_ESCAPE
2187 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2188 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2189 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2191 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2192 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2198 #endif /* PRETTY_ESCAPE */
2199 str[ d++ ] = val->bv_val[ s++ ];
2209 * Length of the IA5 string representation (no UTF-8 allowed)
2212 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2221 if ( val->bv_len == 0 ) {
2225 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2227 * Turn value into a binary encoded BER
2232 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2233 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2234 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2235 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2250 * convert to string representation (np UTF-8)
2251 * assume the destination has enough room for escaping
2254 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2256 ber_len_t s, d, end;
2262 if ( val->bv_len == 0 ) {
2267 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2269 * Turn value into a binary encoded BER
2276 * we assume the string has enough room for the hex encoding
2280 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2281 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2282 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2283 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2286 str[ d++ ] = val->bv_val[ s++ ];
2296 * Length of the (supposedly) DCE string representation,
2297 * accounting for escaped hex of UTF-8 chars
2300 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2309 if ( val->bv_len == 0 ) {
2313 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2315 * FIXME: Turn the value into a binary encoded BER?
2320 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2321 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2336 * convert to (supposedly) DCE string representation,
2337 * escaping with hex the UTF-8 stuff;
2338 * assume the destination has enough room for escaping
2341 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2349 if ( val->bv_len == 0 ) {
2354 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2356 * FIXME: Turn the value into a binary encoded BER?
2364 * we assume the string has enough room for the hex encoding
2368 for ( s = 0, d = 0; s < val->bv_len; ) {
2369 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2372 str[ d++ ] = val->bv_val[ s++ ];
2382 * Length of the (supposedly) AD canonical string representation,
2383 * accounting for escaped hex of UTF-8 chars
2386 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2395 if ( val->bv_len == 0 ) {
2399 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2401 * FIXME: Turn the value into a binary encoded BER?
2406 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2407 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2422 * convert to (supposedly) AD string representation,
2423 * escaping with hex the UTF-8 stuff;
2424 * assume the destination has enough room for escaping
2427 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2435 if ( val->bv_len == 0 ) {
2440 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2442 * FIXME: Turn the value into a binary encoded BER?
2450 * we assume the string has enough room for the hex encoding
2454 for ( s = 0, d = 0; s < val->bv_len; ) {
2455 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2458 str[ d++ ] = val->bv_val[ s++ ];
2468 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2469 * the first part of the AD representation of the DN is written in DNS
2470 * form, i.e. dot separated domain name components (as suggested
2471 * by Luke Howard, http://www.padl.com/~lukeh)
2474 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2477 int domain = 0, first = 1;
2478 ber_len_t l = 1; /* we move the null also */
2481 /* we are guaranteed there's enough memory in str */
2487 assert( *iRDN >= 0 );
2489 str = bv->bv_val + pos;
2491 for ( i = *iRDN; i >= 0; i-- ) {
2501 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2509 AC_MEMCPY( str, ava->la_value.bv_val,
2510 ava->la_value.bv_len + 1);
2511 l += ava->la_value.bv_len;
2514 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2515 AC_MEMCPY( str, ava->la_value.bv_val,
2516 ava->la_value.bv_len );
2517 str[ ava->la_value.bv_len ] = '.';
2518 l += ava->la_value.bv_len + 1;
2523 bv->bv_len = pos + l - 1;
2529 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2530 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2537 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2538 LDAPAVA *ava = rdn[ iAVA ];
2540 /* len(type) + '=' + '+' | ',' */
2541 l += ava->la_attr.bv_len + 2;
2543 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2544 /* octothorpe + twice the length */
2545 l += 1 + 2 * ava->la_value.bv_len;
2549 unsigned f = flags | ava->la_flags;
2551 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2564 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2565 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2570 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2571 LDAPAVA *ava = rdn[ iAVA ];
2573 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2574 ava->la_attr.bv_len );
2575 l += ava->la_attr.bv_len;
2579 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2581 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2584 l += 2 * ava->la_value.bv_len;
2588 unsigned f = flags | ava->la_flags;
2590 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2595 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2604 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2611 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2612 LDAPAVA *ava = rdn[ iAVA ];
2614 /* len(type) + '=' + ',' | '/' */
2615 l += ava->la_attr.bv_len + 2;
2617 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2618 /* octothorpe + twice the length */
2619 l += 1 + 2 * ava->la_value.bv_len;
2622 unsigned f = flags | ava->la_flags;
2624 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2637 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2642 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2643 LDAPAVA *ava = rdn[ iAVA ];
2648 str[ l++ ] = ( iAVA ? ',' : '/' );
2651 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2652 ava->la_attr.bv_len );
2653 l += ava->la_attr.bv_len;
2657 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2659 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2662 l += 2 * ava->la_value.bv_len;
2665 unsigned f = flags | ava->la_flags;
2667 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2680 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2690 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2691 LDAPAVA *ava = rdn[ iAVA ];
2694 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2696 /* FIXME: are binary values allowed in UFN? */
2697 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2698 /* octothorpe + twice the value */
2699 l += 1 + 2 * ava->la_value.bv_len;
2703 unsigned f = flags | ava->la_flags;
2705 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2718 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2723 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2724 LDAPAVA *ava = rdn[ iAVA ];
2726 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2728 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2731 l += 2 * ava->la_value.bv_len;
2735 unsigned f = flags | ava->la_flags;
2737 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2743 if ( rdn[ iAVA + 1 ] ) {
2744 AC_MEMCPY( &str[ l ], " + ", 3 );
2748 AC_MEMCPY( &str[ l ], ", ", 2 );
2759 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2769 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2770 LDAPAVA *ava = rdn[ iAVA ];
2775 /* FIXME: are binary values allowed in UFN? */
2776 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2777 /* octothorpe + twice the value */
2778 l += 1 + 2 * ava->la_value.bv_len;
2781 unsigned f = flags | ava->la_flags;
2783 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2796 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2801 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2802 LDAPAVA *ava = rdn[ iAVA ];
2807 str[ l++ ] = ( iAVA ? ',' : '/' );
2810 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2812 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2815 l += 2 * ava->la_value.bv_len;
2818 unsigned f = flags | ava->la_flags;
2820 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2835 * Returns in str a string representation of rdn based on flags.
2836 * There is some duplication of code between this and ldap_dn2str;
2837 * this is wanted to reduce the allocation of temporary buffers.
2840 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2847 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2848 return LDAP_PARAM_ERROR;
2851 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2857 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2859 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2863 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2873 if ( rdn == NULL ) {
2874 bv->bv_val = LDAP_STRDUPX( "", ctx );
2875 return( LDAP_SUCCESS );
2879 * This routine wastes "back" bytes at the end of the string
2882 switch ( LDAP_DN_FORMAT( flags ) ) {
2883 case LDAP_DN_FORMAT_LDAPV3:
2884 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2885 return LDAP_DECODING_ERROR;
2889 case LDAP_DN_FORMAT_LDAPV2:
2890 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2891 return LDAP_DECODING_ERROR;
2895 case LDAP_DN_FORMAT_UFN:
2896 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2897 return LDAP_DECODING_ERROR;
2901 case LDAP_DN_FORMAT_DCE:
2902 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2903 return LDAP_DECODING_ERROR;
2907 case LDAP_DN_FORMAT_AD_CANONICAL:
2908 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2909 return LDAP_DECODING_ERROR;
2914 return LDAP_PARAM_ERROR;
2917 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2919 switch ( LDAP_DN_FORMAT( flags ) ) {
2920 case LDAP_DN_FORMAT_LDAPV3:
2921 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2925 case LDAP_DN_FORMAT_LDAPV2:
2926 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2930 case LDAP_DN_FORMAT_UFN:
2931 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2935 case LDAP_DN_FORMAT_DCE:
2936 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2940 case LDAP_DN_FORMAT_AD_CANONICAL:
2941 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2946 /* need at least one of the previous */
2947 return LDAP_PARAM_ERROR;
2951 LDAP_FREEX( bv->bv_val, ctx );
2955 bv->bv_len = l - back;
2956 bv->bv_val[ bv->bv_len ] = '\0';
2958 return LDAP_SUCCESS;
2962 * Very bulk implementation; many optimizations can be performed
2963 * - a NULL dn results in an empty string ""
2966 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2967 * we must encode it in binary form ('#' + HEXPAIRs)
2968 * b) does DCE/AD support UTF-8?
2969 * no clue; don't think so.
2970 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2971 * use binary encoded BER
2973 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
2980 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2981 return LDAP_PARAM_ERROR;
2984 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
2989 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
2991 return ldap_dn2bv_x( dn, bv, flags, NULL );
2994 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
2997 int rc = LDAP_ENCODING_ERROR;
3000 /* stringifying helpers for LDAPv3/LDAPv2 */
3001 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3002 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3009 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s",
3012 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
3016 * a null dn means an empty dn string
3017 * FIXME: better raise an error?
3020 bv->bv_val = LDAP_STRDUPX( "", ctx );
3021 return( LDAP_SUCCESS );
3024 switch ( LDAP_DN_FORMAT( flags ) ) {
3025 case LDAP_DN_FORMAT_LDAPV3:
3026 sv2l = strval2strlen;
3030 case LDAP_DN_FORMAT_LDAPV2:
3031 sv2l = strval2IA5strlen;
3032 sv2s = strval2IA5str;
3035 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3037 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3038 goto return_results;
3044 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3045 rc = LDAP_NO_MEMORY;
3049 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3052 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3054 LDAP_FREEX( bv->bv_val, ctx );
3056 goto return_results;
3064 * trim the last ',' (the allocated memory
3065 * is one byte longer than required)
3067 bv->bv_len = len - 1;
3068 bv->bv_val[ bv->bv_len ] = '\0';
3073 case LDAP_DN_FORMAT_UFN: {
3075 * FIXME: quoting from RFC 1781:
3077 To take a distinguished name, and generate a name of this format with
3078 attribute types omitted, the following steps are followed.
3080 1. If the first attribute is of type CommonName, the type may be
3083 2. If the last attribute is of type Country, the type may be
3086 3. If the last attribute is of type Country, the last
3087 Organisation attribute may have the type omitted.
3089 4. All attributes of type OrganisationalUnit may have the type
3090 omitted, unless they are after an Organisation attribute or
3091 the first attribute is of type OrganisationalUnit.
3093 * this should be the pedantic implementation.
3095 * Here the standard implementation reflects
3096 * the one historically provided by OpenLDAP
3097 * (and UMIch, I presume), with the variant
3098 * of spaces and plusses (' + ') separating
3101 * A non-standard but nice implementation could
3102 * be to turn the final "dc" attributes into a
3103 * dot-separated domain.
3105 * Other improvements could involve the use of
3106 * friendly country names and so.
3109 int leftmost_dc = -1;
3111 #endif /* DC_IN_UFN */
3113 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3116 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3117 goto return_results;
3122 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3123 if ( leftmost_dc == -1 ) {
3129 #endif /* DC_IN_UFN */
3132 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3133 rc = LDAP_NO_MEMORY;
3138 if ( leftmost_dc == -1 ) {
3139 #endif /* DC_IN_UFN */
3140 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3143 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3145 LDAP_FREEX( bv->bv_val, ctx );
3147 goto return_results;
3153 * trim the last ', ' (the allocated memory
3154 * is two bytes longer than required)
3156 bv->bv_len = len - 2;
3157 bv->bv_val[ bv->bv_len ] = '\0';
3160 last_iRDN = iRDN - 1;
3162 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3165 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3167 LDAP_FREEX( bv->bv_val, ctx );
3169 goto return_results;
3174 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3175 LDAP_FREEX( bv->bv_val, ctx );
3177 goto return_results;
3180 /* the string is correctly terminated by dn2domain */
3182 #endif /* DC_IN_UFN */
3188 case LDAP_DN_FORMAT_DCE:
3189 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3191 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3192 goto return_results;
3198 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3199 rc = LDAP_NO_MEMORY;
3203 for ( l = 0; iRDN--; ) {
3206 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3208 LDAP_FREEX( bv->bv_val, ctx );
3210 goto return_results;
3218 bv->bv_val[ bv->bv_len ] = '\0';
3223 case LDAP_DN_FORMAT_AD_CANONICAL: {
3225 * Sort of UFN for DCE DNs: a slash ('/') separated
3226 * global->local DN with no types; strictly speaking,
3227 * the naming context should be a domain, which is
3228 * written in DNS-style, e.g. dot-deparated.
3232 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3236 * "microsoft.com/People/Bill,Gates"
3238 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3241 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3242 goto return_results;
3248 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3249 rc = LDAP_NO_MEMORY;
3254 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3255 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3258 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3259 flags, &rdnl, 0 ) ) {
3260 LDAP_FREEX( bv->bv_val, ctx );
3262 goto return_results;
3271 * Strictly speaking, AD canonical requires
3272 * a DN to be in the form "..., dc=smtg",
3273 * i.e. terminated by a domain component
3275 if ( flags & LDAP_DN_PEDANTIC ) {
3276 LDAP_FREEX( bv->bv_val, ctx );
3278 rc = LDAP_ENCODING_ERROR;
3282 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3285 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3286 flags, &rdnl, first ) ) {
3287 LDAP_FREEX( bv->bv_val, ctx );
3289 goto return_results;
3299 bv->bv_val[ bv->bv_len ] = '\0';
3305 return LDAP_PARAM_ERROR;
3309 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3310 bv->bv_val, flags, rc );
3312 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3313 bv->bv_val, flags, rc );
3321 #include <openssl/x509.h>
3322 #include <openssl/err.h>
3324 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3325 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3326 * constructed DN will use numeric OIDs to identify attributeTypes,
3327 * and the func() will be invoked to rewrite the DN with the given
3330 * Otherwise the DN will use shortNames as defined in the OpenSSL
3333 * It's preferable to let slapd do the OID to attributeType mapping,
3334 * because the OpenSSL tables are known to have many typos in versions
3335 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3336 * so we're forced to use OpenSSL's mapping there.
3337 * -- Howard Chu 2002-04-18
3341 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3346 LDAPAVA *newAVA, *baseAVA;
3347 X509_NAME_ENTRY *ne;
3350 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3352 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3354 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3363 /* Get the number of AVAs. This is not necessarily the same as
3364 * the number of RDNs.
3366 navas = X509_NAME_entry_count( x509_name );
3368 /* Get the last element, to see how many RDNs there are */
3369 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3370 nrdns = ne->set + 1;
3372 /* Allocate the DN/RDN/AVA stuff as a single block */
3373 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3374 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3375 dnsize += sizeof(LDAPAVA) * navas;
3376 if (dnsize > sizeof(ptrs)) {
3377 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3378 if ( newDN == NULL )
3379 return LDAP_NO_MEMORY;
3381 newDN = (LDAPDN)ptrs;
3384 newDN[nrdns] = NULL;
3385 newRDN = (LDAPRDN)(newDN + nrdns+1);
3386 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3389 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3390 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3391 ne = X509_NAME_get_entry( x509_name, i );
3392 obj = X509_NAME_ENTRY_get_object( ne );
3393 str = X509_NAME_ENTRY_get_data( ne );
3395 /* If set changed, move to next RDN */
3396 if ( set != ne->set ) {
3397 /* If this is not the first time, end the
3398 * previous RDN and advance.
3404 newDN[j++] = newRDN;
3409 newAVA->la_private = NULL;
3410 newAVA->la_flags = LDAP_AVA_STRING;
3413 int n = OBJ_obj2nid( obj );
3417 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3418 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3420 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3421 __etoa( newAVA->la_attr.bv_val );
3422 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3425 get_oid: newAVA->la_attr.bv_val = oidptr;
3426 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3428 __etoa( newAVA->la_attr.bv_val );
3430 oidptr += newAVA->la_attr.bv_len + 1;
3431 oidrem -= newAVA->la_attr.bv_len + 1;
3433 /* Running out of OID buffer space? */
3435 if ( oidsize == 0 ) {
3436 oidsize = sizeof(oids) * 2;
3438 oidbuf = LDAP_MALLOC( oidsize );
3439 if ( oidbuf == NULL ) goto nomem;
3443 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3444 if ( oidbuf == NULL ) goto nomem;
3445 /* Buffer moved! Fix AVA pointers */
3446 if ( old != oidbuf ) {
3448 long dif = oidbuf - old;
3450 for (a=baseAVA; a<=newAVA; a++){
3451 if (a->la_attr.bv_val >= old &&
3452 a->la_attr.bv_val <= (old + oidsize))
3453 a->la_attr.bv_val += dif;
3456 oidptr = oidbuf + oidsize - oidrem;
3462 Val.bv_val = (char *) str->data;
3463 Val.bv_len = str->length;
3464 switch( str->type ) {
3465 case V_ASN1_UNIVERSALSTRING:
3466 /* This uses 32-bit ISO 10646-1 */
3467 csize = 4; goto to_utf8;
3468 case V_ASN1_BMPSTRING:
3469 /* This uses 16-bit ISO 10646-1 */
3470 csize = 2; goto to_utf8;
3471 case V_ASN1_T61STRING:
3472 /* This uses 8-bit, assume ISO 8859-1 */
3474 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3475 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3476 if (rc != LDAP_SUCCESS) goto nomem;
3477 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3479 case V_ASN1_UTF8STRING:
3480 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3481 /* This is already in UTF-8 encoding */
3482 case V_ASN1_IA5STRING:
3483 case V_ASN1_PRINTABLESTRING:
3484 /* These are always 7-bit strings */
3485 newAVA->la_value = Val;
3496 rc = func( newDN, flags, NULL );
3497 if ( rc != LDAP_SUCCESS )
3501 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3504 for (;baseAVA < newAVA; baseAVA++) {
3505 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3506 LDAP_FREE( baseAVA->la_attr.bv_val );
3507 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3508 LDAP_FREE( baseAVA->la_value.bv_val );
3512 LDAP_FREE( oidbuf );
3513 if ( newDN != (LDAPDN) ptrs )
3517 #endif /* HAVE_TLS */