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, unsigned *retFlags );
33 static int DCE2strval( const char *str, struct berval *val,
34 const char **next, unsigned flags );
35 static int IA52strval( const char *str, struct berval *val,
36 const char **next, unsigned flags );
37 static int quotedIA52strval( const char *str, struct berval *val,
38 const char **next, unsigned flags );
39 static int hexstr2binval( const char *str, struct berval *val,
40 const char **next, unsigned flags );
41 static int hexstr2bin( const char *str, char *c );
42 static int byte2hexpair( const char *val, char *pair );
43 static int binval2hexstr( struct berval *val, char *str );
44 static int strval2strlen( struct berval *val, unsigned flags,
46 static int strval2str( struct berval *val, char *str, unsigned flags,
48 static int strval2IA5strlen( struct berval *val, unsigned flags,
50 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
52 static int strval2DCEstrlen( struct berval *val, unsigned flags,
54 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
56 static int strval2ADstrlen( struct berval *val, unsigned flags,
58 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
60 static int dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN );
63 static LDAPAVA * ldapava_new(
64 const struct berval *attr, const struct berval *val, unsigned flags );
66 /* Higher level helpers */
67 static int rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
68 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
69 static int rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
70 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
71 static int rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
72 static int rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len );
73 static int rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
74 static int rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flag, ber_len_t *len, int first );
75 static int rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len );
76 static int rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first );
79 * RFC 1823 ldap_get_dn
82 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
88 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
90 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
94 assert( LDAP_VALID(ld) );
95 assert( entry != NULL );
97 tmp = *entry->lm_ber; /* struct copy */
98 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
99 ld->ld_errno = LDAP_DECODING_ERROR;
107 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[ 0 ][ iRDN ]; iRDN++ );
209 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
210 if ( values == NULL ) {
211 ldap_dnfree( tmpDN );
215 for ( iRDN = 0; tmpDN[ 0 ][ iRDN ]; iRDN++ ) {
216 ldap_rdn2str( tmpDN[ 0 ][ 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[ 0 ][ iAVA ]; iAVA++ ) ;
249 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
250 if ( values == NULL ) {
251 ldap_rdnfree( tmpRDN );
255 for ( iAVA = 0; tmpRDN[ 0 ][ iAVA ]; iAVA++ ) {
256 ber_len_t l = 0, vl, al = 0;
258 LDAPAVA *ava = tmpRDN[ 0 ][ 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 )
384 LDAPDN *tmpDN = NULL;
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][0] && !(r)[0][1] \
546 && ((r)[0][0]->la_flags == LDAP_AVA_STRING) \
547 && ((r)[0][0]->la_attr.bv_len == 2) \
548 && (((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
549 || ((r)[0][0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
550 && (((r)[0][0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
551 || ((r)[0][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,
587 ava = LDAP_MALLOC( sizeof( LDAPAVA ) + attr->bv_len + 1 );
589 /* should we test it? */
594 ava->la_attr.bv_len = attr->bv_len;
595 ava->la_attr.bv_val = (char *)(ava+1);
596 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
597 ava->la_attr.bv_val[attr->bv_len] = '\0';
599 ava->la_value = *val;
600 ava->la_flags = flags;
602 ava->la_private = NULL;
608 ldap_avafree( LDAPAVA *ava )
613 /* ava's private must be freed by caller
614 * (at present let's skip this check because la_private
615 * basically holds static data) */
616 assert( ava->la_private == NULL );
620 /* la_attr is now contiguous with ava, not freed separately */
621 LDAP_FREE( ava->la_attr.bv_val );
623 LDAP_FREE( ava->la_value.bv_val );
629 ldap_rdnfree( LDAPRDN *rdn )
637 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
638 ldap_avafree( rdn[ 0 ][ iAVA ] );
645 ldap_dnfree( LDAPDN *dn )
653 for ( iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
654 ldap_rdnfree( dn[ 0 ][ iRDN ] );
661 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
662 * into a structural representation of the DN, by separating attribute
663 * types and values encoded in the more appropriate form, which is
664 * string or OID for attribute types and binary form of the BER encoded
665 * value or Unicode string. Formats different from LDAPv3 are parsed
666 * according to their own rules and turned into the more appropriate
667 * form according to LDAPv3.
669 * NOTE: I realize the code is getting spaghettish; it is rather
670 * experimental and will hopefully turn into something more simple
671 * and readable as soon as it works as expected.
675 * Default sizes of AVA and RDN static working arrays; if required
676 * the are dynamically resized. The values can be tuned in case
677 * of special requirements (e.g. very deep DN trees or high number
680 #define TMP_AVA_SLOTS 8
681 #define TMP_RDN_SLOTS 32
684 ldap_str2dn( LDAP_CONST char *str, LDAPDN **dn, unsigned flags )
690 bv.bv_len = strlen( str );
691 bv.bv_val = (char *) str;
693 return ldap_bv2dn( &bv, dn, flags );
697 ldap_bv2dn( struct berval *bv, LDAPDN **dn, unsigned flags )
700 int rc = LDAP_DECODING_ERROR;
703 LDAPDN *newDN = NULL;
704 LDAPRDN *newRDN = NULL, *tmpDN_[TMP_RDN_SLOTS], **tmpDN = tmpDN_;
705 int num_slots = TMP_RDN_SLOTS;
706 char *str = bv->bv_val;
707 char *end = str + bv->bv_len;
710 assert( bv->bv_val );
714 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
716 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
721 switch ( LDAP_DN_FORMAT( flags ) ) {
722 case LDAP_DN_FORMAT_LDAP:
723 case LDAP_DN_FORMAT_LDAPV3:
724 case LDAP_DN_FORMAT_LDAPV2:
725 case LDAP_DN_FORMAT_DCE:
728 /* unsupported in str2dn */
729 case LDAP_DN_FORMAT_UFN:
730 case LDAP_DN_FORMAT_AD_CANONICAL:
731 return LDAP_PARAM_ERROR;
733 case LDAP_DN_FORMAT_LBER:
735 return LDAP_PARAM_ERROR;
738 if ( bv->bv_len == 0 ) {
742 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
743 /* value must have embedded NULs */
744 return LDAP_DECODING_ERROR;
748 if ( LDAP_DN_DCE( flags ) ) {
751 * (from Luke Howard: thnx) A RDN separator is required
752 * at the beginning of an (absolute) DN.
754 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
760 * actually we do not want to accept by default the DCE form,
761 * we do not want to auto-detect it
764 } else if ( LDAP_DN_LDAP( flags ) ) {
766 * if dn starts with '/' let's make it a DCE dn
768 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
769 flags |= LDAP_DN_FORMAT_DCE;
775 for ( ; p < end; p++ ) {
778 tmpbv.bv_len = bv->bv_len - ( p - str );
779 tmpbv.bv_val = (char *)p;
781 err = ldap_bv2rdn( &tmpbv, &newRDN, (char **) &p, flags );
782 if ( err != LDAP_SUCCESS ) {
787 * We expect a rdn separator
789 if ( p < end && p[ 0 ] ) {
790 switch ( LDAP_DN_FORMAT( flags ) ) {
791 case LDAP_DN_FORMAT_LDAPV3:
792 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
793 rc = LDAP_DECODING_ERROR;
798 case LDAP_DN_FORMAT_LDAP:
799 case LDAP_DN_FORMAT_LDAPV2:
800 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
801 rc = LDAP_DECODING_ERROR;
806 case LDAP_DN_FORMAT_DCE:
807 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
808 rc = LDAP_DECODING_ERROR;
816 tmpDN[nrdns++] = newRDN;
820 * make the static RDN array dynamically rescalable
822 if ( nrdns == num_slots ) {
825 if ( tmpDN == tmpDN_ ) {
826 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPRDN * ) );
831 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
834 tmp = LDAP_REALLOC( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ) );
845 if ( p >= end || p[ 0 ] == '\0' ) {
847 * the DN is over, phew
849 newDN = (LDAPDN *)LDAP_MALLOC( sizeof(LDAPDN) +
850 sizeof(LDAPRDN *) * (nrdns+1));
851 if ( newDN == NULL ) {
857 newDN[0] = (LDAPRDN **)(newDN+1);
859 if ( LDAP_DN_DCE( flags ) ) {
860 /* add in reversed order */
861 for ( i=0; i<nrdns; i++ )
862 newDN[0][i] = tmpDN[nrdns-1-i];
864 for ( i=0; i<nrdns; i++ )
865 newDN[0][i] = tmpDN[i];
867 newDN[0][nrdns] = NULL;
876 ldap_rdnfree( newRDN );
879 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
880 ldap_rdnfree( tmpDN[nrdns] );
885 if ( tmpDN != tmpDN_ ) {
890 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
893 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
903 * Parses a relative DN according to flags up to a rdn separator
904 * or to the end of str.
905 * Returns the rdn and a pointer to the string continuation, which
906 * corresponds to the rdn separator or to '\0' in case the string is over.
909 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN **rdn,
910 char **n_in, unsigned flags )
915 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
917 bv.bv_len = strlen( str );
918 bv.bv_val = (char *) str;
920 return ldap_bv2rdn( &bv, rdn, n_in, flags );
924 ldap_bv2rdn( struct berval *bv, LDAPRDN **rdn,
925 char **n_in, unsigned flags )
927 const char **n = (const char **) n_in;
931 int rc = LDAP_DECODING_ERROR;
932 int attrTypeEncoding = LDAP_AVA_STRING,
933 attrValueEncoding = LDAP_AVA_STRING;
935 struct berval attrType = { 0, NULL };
936 struct berval attrValue = { 0, NULL };
938 LDAPRDN *newRDN = NULL;
939 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
940 int num_slots = TMP_AVA_SLOTS;
946 assert( bv->bv_len );
947 assert( bv->bv_val );
948 assert( rdn || flags & LDAP_DN_SKIP );
952 stoplen = bv->bv_len;
959 switch ( LDAP_DN_FORMAT( flags ) ) {
960 case LDAP_DN_FORMAT_LDAP:
961 case LDAP_DN_FORMAT_LDAPV3:
962 case LDAP_DN_FORMAT_LDAPV2:
963 case LDAP_DN_FORMAT_DCE:
966 /* unsupported in str2dn */
967 case LDAP_DN_FORMAT_UFN:
968 case LDAP_DN_FORMAT_AD_CANONICAL:
969 return LDAP_PARAM_ERROR;
971 case LDAP_DN_FORMAT_LBER:
973 return LDAP_PARAM_ERROR;
976 if ( bv->bv_len == 0 ) {
981 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
982 /* value must have embedded NULs */
983 return LDAP_DECODING_ERROR;
987 for ( ; p[ 0 ] || state == GOTAVA; ) {
990 * The parser in principle advances one token a time,
991 * or toggles state if preferable.
996 * an AttributeType can be encoded as:
997 * - its string representation; in detail, implementations
998 * MUST recognize AttributeType string type names listed
999 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1000 * MAY recognize other names.
1001 * - its numeric OID (a dotted decimal string); in detail
1002 * RFC 2253 asserts that ``Implementations MUST allow
1003 * an oid in the attribute type to be prefixed by one
1004 * of the character strings "oid." or "OID."''. As soon
1005 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1006 * I'm not sure whether this is required or not any
1007 * longer; to be liberal, we still implement it.
1010 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1011 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1018 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1019 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1024 /* whitespace is allowed (and trimmed) */
1026 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1031 /* error: we expected an AVA */
1037 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1038 state = B4OIDATTRTYPE;
1042 /* else must be alpha */
1043 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1047 /* LDAPv2 "oid." prefix */
1048 if ( LDAP_DN_LDAPV2( flags ) ) {
1050 * to be overly pedantic, we only accept
1053 if ( flags & LDAP_DN_PEDANTIC ) {
1054 if ( !strncmp( p, "OID.", 4 )
1055 || !strncmp( p, "oid.", 4 ) ) {
1057 state = B4OIDATTRTYPE;
1061 if ( !strncasecmp( p, "oid.", 4 ) ) {
1063 state = B4OIDATTRTYPE;
1069 state = B4STRINGATTRTYPE;
1072 case B4OIDATTRTYPE: {
1073 int err = LDAP_SUCCESS;
1075 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1078 if ( err != LDAP_SUCCESS ) {
1081 attrType.bv_len = p - attrType.bv_val;
1083 attrTypeEncoding = LDAP_AVA_BINARY;
1085 state = B4AVAEQUALS;
1089 case B4STRINGATTRTYPE: {
1090 const char *startPos, *endPos = NULL;
1094 * the starting char has been found to be
1095 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1096 * FIXME: DCE attr types seem to have a more
1097 * restrictive syntax (no '-' ...)
1099 for ( startPos = p++; p[ 0 ]; p++ ) {
1100 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1104 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1107 * RFC 2253 does not explicitly
1108 * allow lang extensions to attribute
1111 if ( flags & LDAP_DN_PEDANTIC ) {
1116 * we trim ';' and following lang
1117 * and so from attribute types
1120 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1121 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1129 len = ( endPos ? endPos : p ) - startPos;
1134 attrTypeEncoding = LDAP_AVA_STRING;
1137 * here we need to decide whether to use it as is
1138 * or turn it in OID form; as a consequence, we
1139 * need to decide whether to binary encode the value
1142 state = B4AVAEQUALS;
1144 if ( flags & LDAP_DN_SKIP ) {
1148 attrType.bv_val = (char *)startPos;
1149 attrType.bv_len = len;
1155 /* spaces may not be allowed */
1156 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1157 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1162 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1167 /* need equal sign */
1168 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1173 /* spaces may not be allowed */
1174 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1175 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1180 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1186 * octothorpe means a BER encoded value will follow
1187 * FIXME: I don't think DCE will allow it
1189 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1191 attrValueEncoding = LDAP_AVA_BINARY;
1192 state = B4BINARYVALUE;
1196 /* STRING value expected */
1199 * if we're pedantic, an attribute type in OID form
1200 * SHOULD imply a BER encoded attribute value; we
1201 * should at least issue a warning
1203 if ( ( flags & LDAP_DN_PEDANTIC )
1204 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1205 /* OID attrType SHOULD use binary encoding */
1209 attrValueEncoding = LDAP_AVA_STRING;
1212 * LDAPv2 allows the attribute value to be quoted;
1213 * also, IA5 values are expected, in principle
1215 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1216 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1218 state = B4IA5VALUEQUOTED;
1222 if ( LDAP_DN_LDAPV2( flags ) ) {
1229 * here STRING means RFC 2253 string
1230 * FIXME: what about DCE strings?
1236 state = B4STRINGVALUE;
1241 if ( hexstr2binval( p, &attrValue, &p, flags ) ) {
1249 switch ( LDAP_DN_FORMAT( flags ) ) {
1250 case LDAP_DN_FORMAT_LDAP:
1251 case LDAP_DN_FORMAT_LDAPV3:
1252 if ( str2strval( p, stoplen - ( p - str ),
1253 &attrValue, &p, flags,
1254 &attrValueEncoding ) ) {
1259 case LDAP_DN_FORMAT_DCE:
1260 if ( DCE2strval( p, &attrValue, &p, flags ) ) {
1273 if ( IA52strval( p, &attrValue, &p, flags ) ) {
1280 case B4IA5VALUEQUOTED:
1282 /* lead quote already stripped */
1283 if ( quotedIA52strval( p, &attrValue,
1294 if ( !( flags & LDAP_DN_SKIP ) ) {
1298 * we accept empty values
1300 ava = ldapava_new( &attrType, &attrValue,
1301 attrValueEncoding );
1303 if ( ava == NULL ) {
1304 rc = LDAP_NO_MEMORY;
1307 tmpRDN[navas++] = ava;
1309 attrValue.bv_val = NULL;
1310 attrValue.bv_len = 0;
1313 * prepare room for new AVAs if needed
1315 if (navas == num_slots) {
1318 if ( tmpRDN == tmpRDN_ ) {
1319 tmp = LDAP_MALLOC( num_slots * 2 * sizeof( LDAPAVA * ) );
1320 if ( tmp == NULL ) {
1321 rc = LDAP_NO_MEMORY;
1324 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1327 tmp = LDAP_REALLOC( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ) );
1328 if ( tmp == NULL ) {
1329 rc = LDAP_NO_MEMORY;
1340 * if we got an AVA separator ('+', or ',' for DCE )
1341 * we expect a new AVA for this RDN; otherwise
1342 * we add the RDN to the DN
1344 switch ( LDAP_DN_FORMAT( flags ) ) {
1345 case LDAP_DN_FORMAT_LDAP:
1346 case LDAP_DN_FORMAT_LDAPV3:
1347 case LDAP_DN_FORMAT_LDAPV2:
1348 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1353 case LDAP_DN_FORMAT_DCE:
1354 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1362 * the RDN is over, phew
1365 if ( !( flags & LDAP_DN_SKIP ) ) {
1366 newRDN = (LDAPRDN *)LDAP_MALLOC( sizeof(LDAPRDN)
1367 + sizeof(LDAPAVA *) * (navas+1) );
1368 if ( newRDN == NULL ) {
1369 rc = LDAP_NO_MEMORY;
1374 newRDN[0] = (LDAPAVA**)(newRDN+1);
1376 for (i=0; i<navas; i++)
1377 newRDN[0][i] = tmpRDN[i];
1378 newRDN[0][i] = NULL;
1386 /* they should have been used in an AVA */
1387 attrType.bv_val = NULL;
1388 attrValue.bv_val = NULL;
1403 /* They are set to NULL after they're used in an AVA */
1405 if ( attrValue.bv_val ) {
1406 free( attrValue.bv_val );
1409 for ( navas-- ; navas >= 0; navas-- ) {
1410 ldap_avafree( tmpRDN[navas] );
1415 if ( tmpRDN != tmpRDN_ ) {
1416 LDAP_FREE( tmpRDN );
1427 * reads in a UTF-8 string value, unescaping stuff:
1428 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1429 * '\' + HEXPAIR(p) -> unhex(p)
1432 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, unsigned *retFlags )
1434 const char *p, *end, *startPos, *endPos = NULL;
1435 ber_len_t len, escapes;
1442 end = str + stoplen;
1443 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1444 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1446 if ( p[ 0 ] == '\0' ) {
1449 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1454 if ( LDAP_DN_HEXPAIR( p ) ) {
1457 hexstr2bin( p, &c );
1460 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1463 * we assume the string is UTF-8
1465 *retFlags = LDAP_AVA_NONPRINTABLE;
1472 if ( LDAP_DN_PEDANTIC & flags ) {
1476 * we do not allow escaping
1477 * of chars that don't need
1478 * to and do not belong to
1483 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1484 if ( p[ 0 ] == '\0' ) {
1487 *retFlags = LDAP_AVA_NONPRINTABLE;
1489 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1490 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1493 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1495 * FIXME: maybe we can add
1496 * escapes if not pedantic?
1503 * we do allow unescaped spaces at the end
1504 * of the value only in non-pedantic mode
1506 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1507 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1508 if ( flags & LDAP_DN_PEDANTIC ) {
1512 /* strip trailing (unescaped) spaces */
1513 for ( endPos = p - 1;
1514 endPos > startPos + 1 &&
1515 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1516 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1523 if ( flags & LDAP_DN_SKIP ) {
1528 * FIXME: test memory?
1530 len = ( endPos ? endPos : p ) - startPos - escapes;
1533 if ( escapes == 0 ) {
1534 if ( *retFlags == LDAP_AVA_NONPRINTABLE ) {
1535 val->bv_val = LDAP_MALLOC( len + 1 );
1536 AC_MEMCPY( val->bv_val, startPos, len );
1537 val->bv_val[ len ] = '\0';
1539 val->bv_val = LDAP_STRNDUP( startPos, len );
1545 val->bv_val = LDAP_MALLOC( len + 1 );
1546 for ( s = 0, d = 0; d < len; ) {
1547 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1549 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1550 val->bv_val[ d++ ] =
1553 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1556 hexstr2bin( &startPos[ s ], &c );
1557 val->bv_val[ d++ ] = c;
1561 /* we should never get here */
1566 val->bv_val[ d++ ] = startPos[ s++ ];
1570 val->bv_val[ d ] = '\0';
1578 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags )
1580 const char *p, *startPos, *endPos = NULL;
1581 ber_len_t len, escapes;
1589 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1590 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1592 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1599 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1604 * FIXME: can we accept anything else? I guess we need
1605 * to stop if a value is not legal
1610 * (unescaped) trailing spaces are trimmed must be silently ignored;
1613 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1614 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1615 if ( flags & LDAP_DN_PEDANTIC ) {
1619 /* strip trailing (unescaped) spaces */
1620 for ( endPos = p - 1;
1621 endPos > startPos + 1 &&
1622 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1623 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1630 if ( flags & LDAP_DN_SKIP ) {
1634 len = ( endPos ? endPos : p ) - startPos - escapes;
1636 if ( escapes == 0 ){
1637 val->bv_val = LDAP_STRNDUP( startPos, len );
1642 val->bv_val = LDAP_MALLOC( len + 1 );
1643 for ( s = 0, d = 0; d < len; ) {
1645 * This point is reached only if escapes
1646 * are properly used, so all we need to
1649 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1653 val->bv_val[ d++ ] = startPos[ s++ ];
1655 val->bv_val[ d ] = '\0';
1656 assert( strlen( val->bv_val ) == len );
1663 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1665 const char *p, *startPos, *endPos = NULL;
1666 ber_len_t len, escapes;
1678 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1679 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1681 if ( p[ 0 ] == '\0' ) {
1685 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1686 && ( LDAP_DN_PEDANTIC & flags ) ) {
1691 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1696 * FIXME: can we accept anything else? I guess we need
1697 * to stop if a value is not legal
1701 /* strip trailing (unescaped) spaces */
1703 endPos > startPos + 1 &&
1704 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1705 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1711 if ( flags & LDAP_DN_SKIP ) {
1715 len = ( endPos ? endPos : p ) - startPos - escapes;
1717 if ( escapes == 0 ) {
1718 val->bv_val = LDAP_STRNDUP( startPos, len );
1723 val->bv_val = LDAP_MALLOC( len + 1 );
1724 for ( s = 0, d = 0; d < len; ) {
1725 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1728 val->bv_val[ d++ ] = startPos[ s++ ];
1730 val->bv_val[ d ] = '\0';
1731 assert( strlen( val->bv_val ) == len );
1738 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags )
1740 const char *p, *startPos, *endPos = NULL;
1742 unsigned escapes = 0;
1750 /* initial quote already eaten */
1751 for ( startPos = p = str; p[ 0 ]; p++ ) {
1753 * According to RFC 1779, the quoted value can
1754 * contain escaped as well as unescaped special values;
1755 * as a consequence we tolerate escaped values
1756 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1757 * (e.g. '","' -> '\,').
1759 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1760 if ( p[ 1 ] == '\0' ) {
1765 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1766 && ( LDAP_DN_PEDANTIC & flags ) ) {
1768 * do we allow to escape normal chars?
1769 * LDAPv2 does not allow any mechanism
1770 * for escaping chars with '\' and hex
1777 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1779 /* eat closing quotes */
1785 * FIXME: can we accept anything else? I guess we need
1786 * to stop if a value is not legal
1790 if ( endPos == NULL ) {
1794 /* Strip trailing (unescaped) spaces */
1795 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1800 if ( flags & LDAP_DN_SKIP ) {
1804 len = endPos - startPos - escapes;
1805 assert( endPos >= startPos + escapes );
1807 if ( escapes == 0 ) {
1808 val->bv_val = LDAP_STRNDUP( startPos, len );
1813 val->bv_val = LDAP_MALLOC( len + 1 );
1816 for ( s = d = 0; d < len; ) {
1817 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1820 val->bv_val[ d++ ] = str[ s++ ];
1822 val->bv_val[ d ] = '\0';
1823 assert( strlen( val->bv_val ) == len );
1830 hexstr2bin( const char *str, char *c )
1840 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1844 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1847 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1854 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1858 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1859 *c += c2 - 'A' + 10;
1861 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1862 *c += c2 - 'a' + 10;
1870 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags )
1872 const char *p, *startPos, *endPos = NULL;
1882 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1883 switch ( LDAP_DN_FORMAT( flags ) ) {
1884 case LDAP_DN_FORMAT_LDAPV3:
1885 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1890 case LDAP_DN_FORMAT_LDAP:
1891 case LDAP_DN_FORMAT_LDAPV2:
1892 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1897 case LDAP_DN_FORMAT_DCE:
1898 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1904 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1905 if ( flags & LDAP_DN_PEDANTIC ) {
1910 for ( ; p[ 0 ]; p++ ) {
1911 switch ( LDAP_DN_FORMAT( flags ) ) {
1912 case LDAP_DN_FORMAT_LDAPV3:
1913 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1918 case LDAP_DN_FORMAT_LDAP:
1919 case LDAP_DN_FORMAT_LDAPV2:
1920 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1925 case LDAP_DN_FORMAT_DCE:
1926 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1935 if ( !LDAP_DN_HEXPAIR( p ) ) {
1943 if ( flags & LDAP_DN_SKIP ) {
1947 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1949 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1952 val->bv_val = LDAP_MALLOC( len + 1 );
1953 if ( val->bv_val == NULL ) {
1954 return( LDAP_NO_MEMORY );
1957 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1960 hexstr2bin( &startPos[ s ], &c );
1962 val->bv_val[ d ] = c;
1965 val->bv_val[ d ] = '\0';
1971 * convert a byte in a hexadecimal pair
1974 byte2hexpair( const char *val, char *pair )
1976 static const char hexdig[] = "0123456789ABCDEF";
1982 * we assume the string has enough room for the hex encoding
1986 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1987 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1993 * convert a binary value in hexadecimal pairs
1996 binval2hexstr( struct berval *val, char *str )
2003 if ( val->bv_len == 0 ) {
2008 * we assume the string has enough room for the hex encoding
2012 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2013 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2020 * Length of the string representation, accounting for escaped hex
2024 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2026 ber_len_t l, cl = 1;
2028 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2029 #ifdef PRETTY_ESCAPE
2030 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2031 #endif /* PRETTY_ESCAPE */
2037 if ( val->bv_len == 0 ) {
2041 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2046 if ( p[ 0 ] == '\0' ) {
2052 cl = LDAP_UTF8_CHARLEN2( p, cl );
2054 /* illegal utf-8 char! */
2057 } else if ( cl > 1 ) {
2060 for ( cnt = 1; cnt < cl; cnt++ ) {
2061 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2065 l += escaped_byte_len * cl;
2067 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2068 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2069 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2070 #ifdef PRETTY_ESCAPE
2072 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2074 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2078 * there might be some chars we want
2079 * to escape in form of a couple
2080 * of hexdigits for optimization purposes
2085 l += escaped_ascii_len;
2087 #else /* ! PRETTY_ESCAPE */
2089 #endif /* ! PRETTY_ESCAPE */
2102 * convert to string representation, escaping with hex the UTF-8 stuff;
2103 * assume the destination has enough room for escaping
2106 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2108 ber_len_t s, d, end;
2114 if ( val->bv_len == 0 ) {
2120 * we assume the string has enough room for the hex encoding
2123 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2129 if ( val->bv_val[ s ] == '\0' ) {
2139 * The length was checked in strval2strlen();
2140 * LDAP_UTF8_CHARLEN() should suffice
2142 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2146 * there might be some chars we want to escape in form
2147 * of a couple of hexdigits for optimization purposes
2149 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2150 #ifdef PRETTY_ESCAPE
2152 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2154 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2156 #else /* ! PRETTY_ESCAPE */
2157 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2158 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2159 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2161 #endif /* ! PRETTY_ESCAPE */
2165 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2170 } else if ( cl > 1 ) {
2172 str[ d++ ] = val->bv_val[ s++ ];
2176 #ifdef PRETTY_ESCAPE
2177 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2178 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2179 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2181 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2182 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2188 #endif /* PRETTY_ESCAPE */
2189 str[ d++ ] = val->bv_val[ s++ ];
2199 * Length of the IA5 string representation (no UTF-8 allowed)
2202 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2211 if ( val->bv_len == 0 ) {
2215 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2217 * Turn value into a binary encoded BER
2222 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2223 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2224 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2225 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2240 * convert to string representation (np UTF-8)
2241 * assume the destination has enough room for escaping
2244 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2246 ber_len_t s, d, end;
2252 if ( val->bv_len == 0 ) {
2257 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2259 * Turn value into a binary encoded BER
2266 * we assume the string has enough room for the hex encoding
2270 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2271 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2272 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2273 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2276 str[ d++ ] = val->bv_val[ s++ ];
2286 * Length of the (supposedly) DCE string representation,
2287 * accounting for escaped hex of UTF-8 chars
2290 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2299 if ( val->bv_len == 0 ) {
2303 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2305 * FIXME: Turn the value into a binary encoded BER?
2310 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2311 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2326 * convert to (supposedly) DCE string representation,
2327 * escaping with hex the UTF-8 stuff;
2328 * assume the destination has enough room for escaping
2331 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2339 if ( val->bv_len == 0 ) {
2344 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2346 * FIXME: Turn the value into a binary encoded BER?
2354 * we assume the string has enough room for the hex encoding
2358 for ( s = 0, d = 0; s < val->bv_len; ) {
2359 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2362 str[ d++ ] = val->bv_val[ s++ ];
2372 * Length of the (supposedly) AD canonical string representation,
2373 * accounting for escaped hex of UTF-8 chars
2376 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2385 if ( val->bv_len == 0 ) {
2389 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2391 * FIXME: Turn the value into a binary encoded BER?
2396 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2397 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2412 * convert to (supposedly) AD string representation,
2413 * escaping with hex the UTF-8 stuff;
2414 * assume the destination has enough room for escaping
2417 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2425 if ( val->bv_len == 0 ) {
2430 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2432 * FIXME: Turn the value into a binary encoded BER?
2440 * we assume the string has enough room for the hex encoding
2444 for ( s = 0, d = 0; s < val->bv_len; ) {
2445 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2448 str[ d++ ] = val->bv_val[ s++ ];
2458 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2459 * the first part of the AD representation of the DN is written in DNS
2460 * form, i.e. dot separated domain name components (as suggested
2461 * by Luke Howard, http://www.padl.com/~lukeh)
2464 dn2domain( LDAPDN *dn, struct berval *bv, int pos, int *iRDN )
2467 int domain = 0, first = 1;
2468 ber_len_t l = 1; /* we move the null also */
2471 /* we are guaranteed there's enough memory in str */
2477 assert( *iRDN >= 0 );
2479 str = bv->bv_val + pos;
2481 for ( i = *iRDN; i >= 0; i-- ) {
2485 assert( dn[ 0 ][ i ] );
2488 assert( rdn[ 0 ][ 0 ] );
2489 ava = rdn[ 0 ][ 0 ];
2491 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2499 AC_MEMCPY( str, ava->la_value.bv_val,
2500 ava->la_value.bv_len + 1);
2501 l += ava->la_value.bv_len;
2504 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2505 AC_MEMCPY( str, ava->la_value.bv_val,
2506 ava->la_value.bv_len );
2507 str[ ava->la_value.bv_len ] = '.';
2508 l += ava->la_value.bv_len + 1;
2513 bv->bv_len = pos + l - 1;
2519 rdn2strlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len,
2520 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2527 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2528 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2530 /* len(type) + '=' + '+' | ',' */
2531 l += ava->la_attr.bv_len + 2;
2533 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2534 /* octothorpe + twice the length */
2535 l += 1 + 2 * ava->la_value.bv_len;
2539 unsigned f = flags | ava->la_flags;
2541 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2554 rdn2str( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len,
2555 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2560 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2561 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2563 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2564 ava->la_attr.bv_len );
2565 l += ava->la_attr.bv_len;
2569 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2571 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2574 l += 2 * ava->la_value.bv_len;
2578 unsigned f = flags | ava->la_flags;
2580 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2585 str[ l++ ] = ( rdn[ 0 ][ iAVA + 1 ] ? '+' : ',' );
2594 rdn2DCEstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2601 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2602 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2604 /* len(type) + '=' + ',' | '/' */
2605 l += ava->la_attr.bv_len + 2;
2607 switch ( ava->la_flags ) {
2608 case LDAP_AVA_BINARY:
2609 /* octothorpe + twice the length */
2610 l += 1 + 2 * ava->la_value.bv_len;
2613 case LDAP_AVA_STRING: {
2615 unsigned f = flags | ava->la_flags;
2617 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2635 rdn2DCEstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2640 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2641 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2646 str[ l++ ] = ( iAVA ? ',' : '/' );
2649 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2650 ava->la_attr.bv_len );
2651 l += ava->la_attr.bv_len;
2655 switch ( ava->la_flags ) {
2656 case LDAP_AVA_BINARY:
2658 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2661 l += 2 * ava->la_value.bv_len;
2664 case LDAP_AVA_STRING: {
2666 unsigned f = flags | ava->la_flags;
2668 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2686 rdn2UFNstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2696 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2697 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2700 l += ( rdn[ 0 ][ iAVA + 1 ] ? 3 : 2 );
2702 /* FIXME: are binary values allowed in UFN? */
2703 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2704 /* octothorpe + twice the value */
2705 l += 1 + 2 * ava->la_value.bv_len;
2709 unsigned f = flags | ava->la_flags;
2711 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2724 rdn2UFNstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len )
2729 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2730 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2732 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2734 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2737 l += 2 * ava->la_value.bv_len;
2741 unsigned f = flags | ava->la_flags;
2743 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2749 if ( rdn[ 0 ][ iAVA + 1 ]) {
2750 AC_MEMCPY( &str[ l ], " + ", 3 );
2754 AC_MEMCPY( &str[ l ], ", ", 2 );
2765 rdn2ADstrlen( LDAPRDN *rdn, unsigned flags, ber_len_t *len )
2775 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2776 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2781 /* FIXME: are binary values allowed in UFN? */
2782 switch ( ava->la_flags ) {
2783 case LDAP_AVA_BINARY:
2784 /* octothorpe + twice the value */
2785 l += 1 + 2 * ava->la_value.bv_len;
2788 case LDAP_AVA_STRING: {
2790 unsigned f = flags | ava->la_flags;
2792 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2810 rdn2ADstr( LDAPRDN *rdn, char *str, unsigned flags, ber_len_t *len, int first )
2815 for ( iAVA = 0; rdn[ 0 ][ iAVA ]; iAVA++ ) {
2816 LDAPAVA *ava = rdn[ 0 ][ iAVA ];
2821 str[ l++ ] = ( iAVA ? ',' : '/' );
2824 switch ( ava->la_flags ) {
2825 case LDAP_AVA_BINARY:
2827 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2830 l += 2 * ava->la_value.bv_len;
2833 case LDAP_AVA_STRING: {
2835 unsigned f = flags | ava->la_flags;
2837 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2857 * Returns in str a string representation of rdn based on flags.
2858 * There is some duplication of code between this and ldap_dn2str;
2859 * this is wanted to reduce the allocation of temporary buffers.
2862 ldap_rdn2str( LDAPRDN *rdn, char **str, unsigned flags )
2869 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2870 return LDAP_PARAM_ERROR;
2873 rc = ldap_rdn2bv( rdn, &bv, flags );
2879 ldap_rdn2bv( LDAPRDN *rdn, struct berval *bv, unsigned flags )
2889 if ( rdn == NULL ) {
2890 bv->bv_val = LDAP_STRDUP( "" );
2891 return( LDAP_SUCCESS );
2895 * This routine wastes "back" bytes at the end of the string
2898 switch ( LDAP_DN_FORMAT( flags ) ) {
2899 case LDAP_DN_FORMAT_LDAPV3:
2900 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2901 return LDAP_DECODING_ERROR;
2905 case LDAP_DN_FORMAT_LDAPV2:
2906 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2907 return LDAP_DECODING_ERROR;
2911 case LDAP_DN_FORMAT_UFN:
2912 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2913 return LDAP_DECODING_ERROR;
2917 case LDAP_DN_FORMAT_DCE:
2918 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2919 return LDAP_DECODING_ERROR;
2923 case LDAP_DN_FORMAT_AD_CANONICAL:
2924 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2925 return LDAP_DECODING_ERROR;
2930 return LDAP_PARAM_ERROR;
2933 bv->bv_val = LDAP_MALLOC( l + 1 );
2935 switch ( LDAP_DN_FORMAT( flags ) ) {
2936 case LDAP_DN_FORMAT_LDAPV3:
2937 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2941 case LDAP_DN_FORMAT_LDAPV2:
2942 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2946 case LDAP_DN_FORMAT_UFN:
2947 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2951 case LDAP_DN_FORMAT_DCE:
2952 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2956 case LDAP_DN_FORMAT_AD_CANONICAL:
2957 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2962 /* need at least one of the previous */
2963 return LDAP_PARAM_ERROR;
2967 ldap_memfree( bv->bv_val );
2971 bv->bv_len = l - back;
2972 bv->bv_val[ bv->bv_len ] = '\0';
2974 return LDAP_SUCCESS;
2978 * Very bulk implementation; many optimizations can be performed
2979 * - a NULL dn results in an empty string ""
2982 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2983 * we must encode it in binary form ('#' + HEXPAIRs)
2984 * b) does DCE/AD support UTF-8?
2985 * no clue; don't think so.
2986 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2987 * use binary encoded BER
2989 int ldap_dn2str( LDAPDN *dn, char **str, unsigned flags )
2996 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2997 return LDAP_PARAM_ERROR;
3000 rc = ldap_dn2bv( dn, &bv, flags );
3005 int ldap_dn2bv( LDAPDN *dn, struct berval *bv, unsigned flags )
3008 int rc = LDAP_ENCODING_ERROR;
3011 /* stringifying helpers for LDAPv3/LDAPv2 */
3012 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3013 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3020 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s",
3023 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
3027 * a null dn means an empty dn string
3028 * FIXME: better raise an error?
3031 bv->bv_val = LDAP_STRDUP( "" );
3032 return( LDAP_SUCCESS );
3035 switch ( LDAP_DN_FORMAT( flags ) ) {
3036 case LDAP_DN_FORMAT_LDAPV3:
3037 sv2l = strval2strlen;
3041 case LDAP_DN_FORMAT_LDAPV2:
3042 sv2l = strval2IA5strlen;
3043 sv2s = strval2IA5str;
3046 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3048 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3050 if ( rdn2strlen( rdn, flags, &rdnl, sv2l ) ) {
3051 goto return_results;
3057 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3058 rc = LDAP_NO_MEMORY;
3062 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3064 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3066 if ( rdn2str( rdn, &bv->bv_val[ l ], flags,
3068 LDAP_FREE( bv->bv_val );
3070 goto return_results;
3078 * trim the last ',' (the allocated memory
3079 * is one byte longer than required)
3081 bv->bv_len = len - 1;
3082 bv->bv_val[ bv->bv_len ] = '\0';
3087 case LDAP_DN_FORMAT_UFN: {
3089 * FIXME: quoting from RFC 1781:
3091 To take a distinguished name, and generate a name of this format with
3092 attribute types omitted, the following steps are followed.
3094 1. If the first attribute is of type CommonName, the type may be
3097 2. If the last attribute is of type Country, the type may be
3100 3. If the last attribute is of type Country, the last
3101 Organisation attribute may have the type omitted.
3103 4. All attributes of type OrganisationalUnit may have the type
3104 omitted, unless they are after an Organisation attribute or
3105 the first attribute is of type OrganisationalUnit.
3107 * this should be the pedantic implementation.
3109 * Here the standard implementation reflects
3110 * the one historically provided by OpenLDAP
3111 * (and UMIch, I presume), with the variant
3112 * of spaces and plusses (' + ') separating
3115 * A non-standard but nice implementation could
3116 * be to turn the final "dc" attributes into a
3117 * dot-separated domain.
3119 * Other improvements could involve the use of
3120 * friendly country names and so.
3123 int leftmost_dc = -1;
3125 #endif /* DC_IN_UFN */
3127 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3129 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3131 if ( rdn2UFNstrlen( rdn, flags, &rdnl ) ) {
3132 goto return_results;
3137 if ( LDAP_DN_IS_RDN_DC( rdn ) ) {
3138 if ( leftmost_dc == -1 ) {
3144 #endif /* DC_IN_UFN */
3147 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3148 rc = LDAP_NO_MEMORY;
3153 if ( leftmost_dc == -1 ) {
3154 #endif /* DC_IN_UFN */
3155 for ( l = 0, iRDN = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3157 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3159 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3161 LDAP_FREE( bv->bv_val );
3163 goto return_results;
3169 * trim the last ', ' (the allocated memory
3170 * is two bytes longer than required)
3172 bv->bv_len = len - 2;
3173 bv->bv_val[ bv->bv_len ] = '\0';
3176 last_iRDN = iRDN - 1;
3178 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3180 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3182 if ( rdn2UFNstr( rdn, &bv->bv_val[ l ],
3184 LDAP_FREE( bv->bv_val );
3186 goto return_results;
3191 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3192 LDAP_FREE( bv->bv_val );
3194 goto return_results;
3197 /* the string is correctly terminated by dn2domain */
3199 #endif /* DC_IN_UFN */
3205 case LDAP_DN_FORMAT_DCE:
3206 for ( iRDN = 0, len = 0; dn[ 0 ][ iRDN ]; iRDN++ ) {
3208 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3210 if ( rdn2DCEstrlen( rdn, flags, &rdnl ) ) {
3211 goto return_results;
3217 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3218 rc = LDAP_NO_MEMORY;
3222 for ( l = 0; iRDN--; ) {
3224 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3226 if ( rdn2DCEstr( rdn, &bv->bv_val[ l ], flags,
3228 LDAP_FREE( bv->bv_val );
3230 goto return_results;
3238 bv->bv_val[ bv->bv_len ] = '\0';
3243 case LDAP_DN_FORMAT_AD_CANONICAL: {
3245 * Sort of UFN for DCE DNs: a slash ('/') separated
3246 * global->local DN with no types; strictly speaking,
3247 * the naming context should be a domain, which is
3248 * written in DNS-style, e.g. dot-deparated.
3252 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3256 * "microsoft.com/People/Bill,Gates"
3258 for ( iRDN = 0, len = -1; dn[ 0 ][ iRDN ]; iRDN++ ) {
3260 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3262 if ( rdn2ADstrlen( rdn, flags, &rdnl ) ) {
3263 goto return_results;
3269 if ( ( bv->bv_val = LDAP_MALLOC( len + 1 ) ) == NULL ) {
3270 rc = LDAP_NO_MEMORY;
3275 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) ) {
3276 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3278 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3280 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3281 flags, &rdnl, 0 ) ) {
3282 LDAP_FREE( bv->bv_val );
3284 goto return_results;
3293 * Strictly speaking, AD canonical requires
3294 * a DN to be in the form "..., dc=smtg",
3295 * i.e. terminated by a domain component
3297 if ( flags & LDAP_DN_PEDANTIC ) {
3298 LDAP_FREE( bv->bv_val );
3300 rc = LDAP_ENCODING_ERROR;
3304 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3306 LDAPRDN *rdn = dn[ 0 ][ iRDN ];
3308 if ( rdn2ADstr( rdn, &bv->bv_val[ l ],
3309 flags, &rdnl, first ) ) {
3310 LDAP_FREE( bv->bv_val );
3312 goto return_results;
3322 bv->bv_val[ bv->bv_len ] = '\0';
3328 return LDAP_PARAM_ERROR;
3332 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3333 bv->bv_val, flags, rc );
3335 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3336 bv->bv_val, flags, rc );
3344 #include <openssl/x509.h>
3345 #include <openssl/err.h>
3347 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3348 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3349 * constructed DN will use numeric OIDs to identify attributeTypes,
3350 * and the func() will be invoked to rewrite the DN with the given
3353 * Otherwise the DN will use shortNames as defined in the OpenSSL
3356 * It's preferable to let slapd do the OID to attributeType mapping,
3357 * because the OpenSSL tables are known to have many typos in versions
3358 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3359 * so we're forced to use OpenSSL's mapping there.
3360 * -- Howard Chu 2002-04-18
3364 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3369 LDAPAVA *newAVA, *baseAVA;
3370 X509_NAME_ENTRY *ne;
3373 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3375 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3377 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3386 /* Get the number of AVAs. This is not necessarily the same as
3387 * the number of RDNs.
3389 navas = X509_NAME_entry_count( x509_name );
3391 /* Get the last element, to see how many RDNs there are */
3392 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3393 nrdns = ne->set + 1;
3395 /* Allocate the DN/RDN/AVA stuff as a single block */
3396 dnsize = sizeof(LDAPDN) + sizeof(LDAPRDN *) * (nrdns+1);
3397 dnsize += sizeof(LDAPRDN) * nrdns + sizeof(LDAPAVA *) * (navas+nrdns);
3398 dnsize += sizeof(LDAPAVA) * navas;
3399 if (dnsize > sizeof(ptrs)) {
3400 newDN = (LDAPDN *)LDAP_MALLOC( dnsize );
3401 if ( newDN == NULL )
3402 return LDAP_NO_MEMORY;
3404 newDN = (LDAPDN *)ptrs;
3407 newDN[0] = (LDAPRDN**)(newDN+1);
3408 newDN[0][nrdns] = NULL;
3409 newRDN = (LDAPRDN*)(newDN[0] + nrdns+1);
3410 newAVA = (LDAPAVA*)(newRDN + navas + nrdns*2);
3413 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3414 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3415 ne = X509_NAME_get_entry( x509_name, i );
3416 obj = X509_NAME_ENTRY_get_object( ne );
3417 str = X509_NAME_ENTRY_get_data( ne );
3419 /* If set changed, move to next RDN */
3420 if ( set != ne->set ) {
3421 /* If this is not the first time, end the
3422 * previous RDN and advance.
3425 newRDN[0][k] = NULL;
3426 newRDN = (LDAPRDN*)(newRDN[0]+k+1);
3428 newDN[0][j++] = newRDN;
3430 newRDN[0] = (LDAPAVA**)(newRDN+1);
3434 newAVA->la_private = NULL;
3435 newAVA->la_flags = LDAP_AVA_STRING;
3438 int n = OBJ_obj2nid( obj );
3442 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3443 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3445 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3446 __etoa( newAVA->la_attr.bv_val );
3449 get_oid: newAVA->la_attr.bv_val = oidptr;
3450 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3452 __etoa( newAVA->la_attr.bv_val );
3454 oidptr += newAVA->la_attr.bv_len + 1;
3455 oidrem -= newAVA->la_attr.bv_len + 1;
3457 /* Running out of OID buffer space? */
3459 if ( oidsize == 0 ) {
3460 oidsize = sizeof(oids) * 2;
3462 oidbuf = LDAP_MALLOC( oidsize );
3463 if ( oidbuf == NULL ) goto nomem;
3467 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3468 if ( oidbuf == NULL ) goto nomem;
3469 /* Buffer moved! Fix AVA pointers */
3470 if ( old != oidbuf ) {
3472 long dif = oidbuf - old;
3474 for (a=baseAVA; a<=newAVA; a++){
3475 if (a->la_attr.bv_val >= old &&
3476 a->la_attr.bv_val <= (old + oidsize))
3477 a->la_attr.bv_val += dif;
3480 oidptr = oidbuf + oidsize - oidrem;
3486 Val.bv_val = str->data;
3487 Val.bv_len = str->length;
3488 switch( str->type ) {
3489 case V_ASN1_UNIVERSALSTRING:
3490 /* This uses 32-bit ISO 10646-1 */
3491 csize = 4; goto to_utf8;
3492 case V_ASN1_BMPSTRING:
3493 /* This uses 16-bit ISO 10646-1 */
3494 csize = 2; goto to_utf8;
3495 case V_ASN1_T61STRING:
3496 /* This uses 8-bit, assume ISO 8859-1 */
3498 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3499 if (rc != LDAP_SUCCESS) goto nomem;
3500 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3502 case V_ASN1_UTF8STRING:
3503 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3504 /* This is already in UTF-8 encoding */
3505 case V_ASN1_IA5STRING:
3506 case V_ASN1_PRINTABLESTRING:
3507 /* These are always 7-bit strings */
3508 ber_dupbv( &newAVA->la_value, &Val );
3512 newRDN[0][k] = newAVA;
3516 newRDN[0][k] = NULL;
3519 rc = func( newDN, flags );
3520 if ( rc != LDAP_SUCCESS )
3524 rc = ldap_dn2bv( newDN, bv, LDAP_DN_FORMAT_LDAPV3 );
3527 for (;baseAVA < newAVA; baseAVA++) {
3528 LDAP_FREE( baseAVA->la_value.bv_val );
3530 if ( !func ) LDAP_FREE( baseAVA->la_attr.bv_val );
3535 LDAP_FREE( oidbuf );
3536 if ( newDN != (LDAPDN*) ptrs )
3540 #endif /* HAVE_TLS */