2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1994 Regents of the University of Michigan.
16 * All rights reserved.
23 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 #include "ldap_schema.h"
31 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
32 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
38 /* parsing/printing routines */
39 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
40 const char **next, unsigned flags, int *retFlags, void *ctx );
41 static int DCE2strval( const char *str, struct berval *val,
42 const char **next, unsigned flags, void *ctx );
43 static int IA52strval( const char *str, struct berval *val,
44 const char **next, unsigned flags, void *ctx );
45 static int quotedIA52strval( const char *str, struct berval *val,
46 const char **next, unsigned flags, void *ctx );
47 static int hexstr2binval( const char *str, struct berval *val,
48 const char **next, unsigned flags, void *ctx );
49 static int hexstr2bin( const char *str, char *c );
50 static int byte2hexpair( const char *val, char *pair );
51 static int binval2hexstr( struct berval *val, char *str );
52 static int strval2strlen( struct berval *val, unsigned flags,
54 static int strval2str( struct berval *val, char *str, unsigned flags,
56 static int strval2IA5strlen( struct berval *val, unsigned flags,
58 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
60 static int strval2DCEstrlen( struct berval *val, unsigned flags,
62 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
64 static int strval2ADstrlen( struct berval *val, unsigned flags,
66 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
68 static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
71 static LDAPAVA * ldapava_new(
72 const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
74 /* Higher level helpers */
75 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
76 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
77 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
78 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
79 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
80 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
81 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
82 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
83 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
84 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
87 * RFC 1823 ldap_get_dn
90 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
96 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
98 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
101 assert( ld != NULL );
102 assert( LDAP_VALID(ld) );
103 assert( entry != NULL );
105 tmp = *entry->lm_ber; /* struct copy */
106 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
107 ld->ld_errno = LDAP_DECODING_ERROR;
115 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
118 BerElement tmp, *ber;
120 int rc = LDAP_SUCCESS;
123 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
125 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
128 assert( ld != NULL );
129 assert( LDAP_VALID(ld) );
130 assert( entry != NULL );
131 assert( dn != NULL );
138 ber = ldap_alloc_ber_with_options( ld );
140 return LDAP_NO_MEMORY;
147 *ber = *entry->lm_ber; /* struct copy */
148 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
149 rc = ld->ld_errno = LDAP_DECODING_ERROR;
151 if ( rc == LDAP_SUCCESS ) {
152 /* set the length to avoid overrun */
153 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
154 if( rc != LBER_OPT_SUCCESS ) {
155 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
158 if ( rc != LDAP_SUCCESS && berout ) {
166 * RFC 1823 ldap_dn2ufn
169 ldap_dn2ufn( LDAP_CONST char *dn )
174 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
176 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
179 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
180 &out, LDAP_DN_FORMAT_UFN );
186 * RFC 1823 ldap_explode_dn
189 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
192 char **values = NULL;
194 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
197 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
199 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
202 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
207 if( tmpDN == NULL ) {
208 values = LDAP_MALLOC( sizeof( char * ) );
209 if( values == NULL ) return NULL;
215 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
217 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
218 if ( values == NULL ) {
219 ldap_dnfree( tmpDN );
223 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
224 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
226 ldap_dnfree( tmpDN );
227 values[ iRDN ] = NULL;
233 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
236 char **values = NULL;
241 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
247 * we only parse the first rdn
248 * FIXME: we prefer efficiency over checking if the _ENTIRE_
251 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
256 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
257 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
258 if ( values == NULL ) {
259 ldap_rdnfree( tmpRDN );
263 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
264 ber_len_t l = 0, vl, al = 0;
266 LDAPAVA *ava = tmpRDN[ iAVA ];
268 if ( ava->la_flags & LDAP_AVA_BINARY ) {
269 vl = 1 + 2 * ava->la_value.bv_len;
272 if ( strval2strlen( &ava->la_value,
273 ava->la_flags, &vl ) ) {
279 al = ava->la_attr.bv_len;
280 l = vl + ava->la_attr.bv_len + 1;
282 str = LDAP_MALLOC( l + 1 );
283 AC_MEMCPY( str, ava->la_attr.bv_val,
284 ava->la_attr.bv_len );
289 str = LDAP_MALLOC( l + 1 );
292 if ( ava->la_flags & LDAP_AVA_BINARY ) {
294 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
299 if ( strval2str( &ava->la_value, &str[ al ],
300 ava->la_flags, &vl ) ) {
306 values[ iAVA ] = str;
308 values[ iAVA ] = NULL;
310 ldap_rdnfree( tmpRDN );
315 LBER_VFREE( values );
316 ldap_rdnfree( tmpRDN );
321 ldap_dn2dcedn( LDAP_CONST char *dn )
326 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
328 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
331 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
332 &out, LDAP_DN_FORMAT_DCE );
338 ldap_dcedn2dn( LDAP_CONST char *dce )
343 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
345 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
348 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
354 ldap_dn2ad_canonical( LDAP_CONST char *dn )
359 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
364 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
365 &out, LDAP_DN_FORMAT_AD_CANONICAL );
371 * function that changes the string representation of dnin
372 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
375 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
376 * plus some rfc 1779)
377 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
378 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
379 * LDAP_DN_FORMAT_DCE (?)
381 * fout can be any of the above except
382 * LDAP_DN_FORMAT_LDAP
384 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
385 * LDAP_DN_FORMAT_AD_CANONICAL (?)
388 ldap_dn_normalize( LDAP_CONST char *dnin,
389 unsigned fin, char **dnout, unsigned fout )
395 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
404 if ( dnin == NULL ) {
405 return( LDAP_SUCCESS );
408 rc = ldap_str2dn( dnin , &tmpDN, fin );
409 if ( rc != LDAP_SUCCESS ) {
413 rc = ldap_dn2str( tmpDN, dnout, fout );
415 ldap_dnfree( tmpDN );
423 /* #define B4ATTRTYPE 0x0001 */
424 #define B4OIDATTRTYPE 0x0002
425 #define B4STRINGATTRTYPE 0x0003
427 #define B4AVAEQUALS 0x0100
428 #define B4AVASEP 0x0200
429 #define B4RDNSEP 0x0300
430 #define GOTAVA 0x0400
432 #define B4ATTRVALUE 0x0010
433 #define B4STRINGVALUE 0x0020
434 #define B4IA5VALUEQUOTED 0x0030
435 #define B4IA5VALUE 0x0040
436 #define B4BINARYVALUE 0x0050
439 * Helpers (mostly from slap.h)
440 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
441 * Macros assume "C" Locale (ASCII)
443 #define LDAP_DN_ASCII_SPACE(c) \
444 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
445 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
446 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
447 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
449 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
450 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
451 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
452 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
453 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
454 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
457 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
458 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
459 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
460 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
461 #define LDAP_DN_ATTRDESC_CHAR(c) \
462 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
464 /* special symbols */
465 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
466 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
467 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
468 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
469 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
470 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
471 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
472 #define LDAP_DN_VALUE_END(c) \
473 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
474 #define LDAP_DN_NE(c) \
475 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
476 || LDAP_DN_AVA_EQUALS(c) || LDAP_DN_QUOTES(c) \
477 || (c) == '<' || (c) == '>' )
478 #define LDAP_DN_MAYESCAPE(c) \
479 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
480 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
481 #define LDAP_DN_NEEDESCAPE(c) \
482 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
483 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
484 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
485 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
486 #define LDAP_DN_WILLESCAPE_CHAR(c) \
487 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
488 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
489 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
490 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
493 #define LDAP_DN_VALUE_END_V2(c) \
494 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
496 #define LDAP_DN_V2_SPECIAL(c) \
497 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
498 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
499 || LDAP_DN_OCTOTHORPE(c) )
500 #define LDAP_DN_V2_PAIR(c) \
501 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
504 * DCE (mostly from Luke Howard and IBM implementation for AIX)
506 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
507 * Here escapes and valid chars for GDS are considered; as soon as more
508 * specific info is found, the macros will be updated.
510 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
511 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
513 * Metachars: '/', ',', '=', '\'.
515 * the '\' is used to escape other metachars.
521 * Attribute types must start with alphabetic chars and can contain
522 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
524 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
525 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
526 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
527 #define LDAP_DN_VALUE_END_DCE(c) \
528 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
529 #define LDAP_DN_NEEDESCAPE_DCE(c) \
530 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
533 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
534 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
535 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
536 #define LDAP_DN_VALUE_END_AD(c) \
537 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
538 #define LDAP_DN_NEEDESCAPE_AD(c) \
539 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
542 #define LDAP_DN_HEXPAIR(s) \
543 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
544 /* better look at the AttributeDescription? */
546 /* FIXME: no composite rdn or non-"dc" types, right?
547 * (what about "dc" in OID form?) */
548 /* FIXME: we do not allow binary values in domain, right? */
549 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
550 /* NOTE: don't use strcasecmp() as it is locale specific! */
551 #define LDAP_DC_ATTR "dc"
552 #define LDAP_DC_ATTRU "DC"
553 #define LDAP_DN_IS_RDN_DC( r ) \
554 ( (r) && (r)[0] && !(r)[1] \
555 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
556 && ((r)[0]->la_attr.bv_len == 2) \
557 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
558 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
559 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
560 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
562 /* Composite rules */
563 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
564 ( LDAP_DN_LDAPV2(f) \
565 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
566 #define LDAP_DN_ALLOW_SPACES(f) \
567 ( LDAP_DN_LDAPV2(f) \
568 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
569 #define LDAP_DN_LDAP(f) \
570 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
571 #define LDAP_DN_LDAPV3(f) \
572 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
573 #define LDAP_DN_LDAPV2(f) \
574 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
575 #define LDAP_DN_DCE(f) \
576 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
577 #define LDAP_DN_UFN(f) \
578 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
579 #define LDAP_DN_ADC(f) \
580 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
581 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
584 * LDAPAVA helpers (will become part of the API for operations
585 * on structural representations of DNs).
588 ldapava_new( const struct berval *attr, const struct berval *val,
589 unsigned flags, void *ctx )
596 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
599 ava->la_attr.bv_len = attr->bv_len;
600 ava->la_attr.bv_val = (char *)(ava+1);
601 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
602 ava->la_attr.bv_val[attr->bv_len] = '\0';
604 ava->la_value = *val;
605 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
607 ava->la_private = NULL;
614 ldapava_free( LDAPAVA *ava, void *ctx )
619 /* ava's private must be freed by caller
620 * (at present let's skip this check because la_private
621 * basically holds static data) */
622 assert( ava->la_private == NULL );
625 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
626 LDAP_FREEX( ava->la_value.bv_val, ctx );
628 LDAP_FREEX( ava, ctx );
632 ldap_rdnfree( LDAPRDN rdn )
634 ldap_rdnfree_x( rdn, NULL );
638 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
646 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
647 ldapava_free( rdn[ iAVA ], ctx );
650 LDAP_FREEX( rdn, ctx );
654 ldap_dnfree( LDAPDN dn )
656 ldap_dnfree_x( dn, NULL );
660 ldap_dnfree_x( LDAPDN dn, void *ctx )
668 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
669 ldap_rdnfree_x( dn[ iRDN ], ctx );
672 LDAP_FREEX( dn, ctx );
676 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
677 * into a structural representation of the DN, by separating attribute
678 * types and values encoded in the more appropriate form, which is
679 * string or OID for attribute types and binary form of the BER encoded
680 * value or Unicode string. Formats different from LDAPv3 are parsed
681 * according to their own rules and turned into the more appropriate
682 * form according to LDAPv3.
684 * NOTE: I realize the code is getting spaghettish; it is rather
685 * experimental and will hopefully turn into something more simple
686 * and readable as soon as it works as expected.
690 * Default sizes of AVA and RDN static working arrays; if required
691 * the are dynamically resized. The values can be tuned in case
692 * of special requirements (e.g. very deep DN trees or high number
695 #define TMP_AVA_SLOTS 8
696 #define TMP_RDN_SLOTS 32
699 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
705 bv.bv_len = strlen( str );
706 bv.bv_val = (char *) str;
708 return ldap_bv2dn_x( &bv, dn, flags, NULL );
712 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
714 return ldap_bv2dn_x( bv, dn, flags, NULL );
718 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
721 int rc = LDAP_DECODING_ERROR;
725 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
726 int num_slots = TMP_RDN_SLOTS;
728 struct berval bvtmp, *bv = &bvtmp;
731 assert( bvin->bv_val );
736 end = str + bv->bv_len;
739 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n", str, flags, 0 );
741 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
746 switch ( LDAP_DN_FORMAT( flags ) ) {
747 case LDAP_DN_FORMAT_LDAP:
748 case LDAP_DN_FORMAT_LDAPV3:
749 case LDAP_DN_FORMAT_DCE:
752 /* allow DN enclosed in brackets */
753 case LDAP_DN_FORMAT_LDAPV2:
754 if ( str[0] == '<' ) {
755 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
756 rc = LDAP_DECODING_ERROR;
766 /* unsupported in str2dn */
767 case LDAP_DN_FORMAT_UFN:
768 case LDAP_DN_FORMAT_AD_CANONICAL:
769 return LDAP_PARAM_ERROR;
771 case LDAP_DN_FORMAT_LBER:
773 return LDAP_PARAM_ERROR;
776 if ( bv->bv_len == 0 ) {
780 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
781 /* value must have embedded NULs */
782 return LDAP_DECODING_ERROR;
786 if ( LDAP_DN_DCE( flags ) ) {
789 * (from Luke Howard: thnx) A RDN separator is required
790 * at the beginning of an (absolute) DN.
792 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
798 * actually we do not want to accept by default the DCE form,
799 * we do not want to auto-detect it
802 } else if ( LDAP_DN_LDAP( flags ) ) {
804 * if dn starts with '/' let's make it a DCE dn
806 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
807 flags |= LDAP_DN_FORMAT_DCE;
813 for ( ; p < end; p++ ) {
816 tmpbv.bv_len = bv->bv_len - ( p - str );
817 tmpbv.bv_val = (char *)p;
819 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
820 if ( err != LDAP_SUCCESS ) {
825 * We expect a rdn separator
827 if ( p < end && p[ 0 ] ) {
828 switch ( LDAP_DN_FORMAT( flags ) ) {
829 case LDAP_DN_FORMAT_LDAPV3:
830 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
831 rc = LDAP_DECODING_ERROR;
836 case LDAP_DN_FORMAT_LDAP:
837 case LDAP_DN_FORMAT_LDAPV2:
838 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
839 rc = LDAP_DECODING_ERROR;
844 case LDAP_DN_FORMAT_DCE:
845 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
846 rc = LDAP_DECODING_ERROR;
854 tmpDN[nrdns++] = newRDN;
858 * make the static RDN array dynamically rescalable
860 if ( nrdns == num_slots ) {
863 if ( tmpDN == tmpDN_ ) {
864 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
869 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
872 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
883 if ( p >= end || p[ 0 ] == '\0' ) {
885 * the DN is over, phew
887 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
888 if ( newDN == NULL ) {
894 if ( LDAP_DN_DCE( flags ) ) {
895 /* add in reversed order */
896 for ( i=0; i<nrdns; i++ )
897 newDN[i] = tmpDN[nrdns-1-i];
899 for ( i=0; i<nrdns; i++ )
911 ldap_rdnfree_x( newRDN, ctx );
914 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
915 ldap_rdnfree_x( tmpDN[nrdns], ctx );
920 if ( tmpDN != tmpDN_ ) {
921 LDAP_FREEX( tmpDN, ctx );
925 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s)=%d %s\n",
926 str, rc, ldap_err2string( rc ) );
928 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
929 ldap_err2string( rc ) );
939 * Parses a relative DN according to flags up to a rdn separator
940 * or to the end of str.
941 * Returns the rdn and a pointer to the string continuation, which
942 * corresponds to the rdn separator or to '\0' in case the string is over.
945 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
946 char **n_in, unsigned flags )
951 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
953 bv.bv_len = strlen( str );
954 bv.bv_val = (char *) str;
956 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
960 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
961 char **n_in, unsigned flags )
963 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
967 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
968 char **n_in, unsigned flags, void *ctx )
970 const char **n = (const char **) n_in;
974 int rc = LDAP_DECODING_ERROR;
975 int attrTypeEncoding = LDAP_AVA_STRING,
976 attrValueEncoding = LDAP_AVA_STRING;
978 struct berval attrType = BER_BVNULL;
979 struct berval attrValue = BER_BVNULL;
981 LDAPRDN newRDN = NULL;
982 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
983 int num_slots = TMP_AVA_SLOTS;
989 assert( bv->bv_len );
990 assert( bv->bv_val );
991 assert( rdn || flags & LDAP_DN_SKIP );
995 stoplen = bv->bv_len;
1002 switch ( LDAP_DN_FORMAT( flags ) ) {
1003 case LDAP_DN_FORMAT_LDAP:
1004 case LDAP_DN_FORMAT_LDAPV3:
1005 case LDAP_DN_FORMAT_LDAPV2:
1006 case LDAP_DN_FORMAT_DCE:
1009 /* unsupported in str2dn */
1010 case LDAP_DN_FORMAT_UFN:
1011 case LDAP_DN_FORMAT_AD_CANONICAL:
1012 return LDAP_PARAM_ERROR;
1014 case LDAP_DN_FORMAT_LBER:
1016 return LDAP_PARAM_ERROR;
1019 if ( bv->bv_len == 0 ) {
1020 return LDAP_SUCCESS;
1024 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1025 /* value must have embedded NULs */
1026 return LDAP_DECODING_ERROR;
1030 for ( ; p[ 0 ] || state == GOTAVA; ) {
1033 * The parser in principle advances one token a time,
1034 * or toggles state if preferable.
1039 * an AttributeType can be encoded as:
1040 * - its string representation; in detail, implementations
1041 * MUST recognize AttributeType string type names listed
1042 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1043 * MAY recognize other names.
1044 * - its numeric OID (a dotted decimal string); in detail
1045 * RFC 2253 asserts that ``Implementations MUST allow
1046 * an oid in the attribute type to be prefixed by one
1047 * of the character strings "oid." or "OID."''. As soon
1048 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1049 * I'm not sure whether this is required or not any
1050 * longer; to be liberal, we still implement it.
1053 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1054 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1061 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1062 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1067 /* whitespace is allowed (and trimmed) */
1069 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1074 /* error: we expected an AVA */
1080 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1081 state = B4OIDATTRTYPE;
1085 /* else must be alpha */
1086 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1090 /* LDAPv2 "oid." prefix */
1091 if ( LDAP_DN_LDAPV2( flags ) ) {
1093 * to be overly pedantic, we only accept
1096 if ( flags & LDAP_DN_PEDANTIC ) {
1097 if ( !strncmp( p, "OID.", 4 )
1098 || !strncmp( p, "oid.", 4 ) ) {
1100 state = B4OIDATTRTYPE;
1104 if ( !strncasecmp( p, "oid.", 4 ) ) {
1106 state = B4OIDATTRTYPE;
1112 state = B4STRINGATTRTYPE;
1115 case B4OIDATTRTYPE: {
1116 int err = LDAP_SUCCESS;
1118 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1121 if ( err != LDAP_SUCCESS ) {
1124 attrType.bv_len = p - attrType.bv_val;
1126 attrTypeEncoding = LDAP_AVA_BINARY;
1128 state = B4AVAEQUALS;
1132 case B4STRINGATTRTYPE: {
1133 const char *startPos, *endPos = NULL;
1137 * the starting char has been found to be
1138 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1139 * FIXME: DCE attr types seem to have a more
1140 * restrictive syntax (no '-' ...)
1142 for ( startPos = p++; p[ 0 ]; p++ ) {
1143 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1147 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1150 * RFC 2253 does not explicitly
1151 * allow lang extensions to attribute
1154 if ( flags & LDAP_DN_PEDANTIC ) {
1159 * we trim ';' and following lang
1160 * and so from attribute types
1163 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1164 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1172 len = ( endPos ? endPos : p ) - startPos;
1177 attrTypeEncoding = LDAP_AVA_STRING;
1180 * here we need to decide whether to use it as is
1181 * or turn it in OID form; as a consequence, we
1182 * need to decide whether to binary encode the value
1185 state = B4AVAEQUALS;
1187 if ( flags & LDAP_DN_SKIP ) {
1191 attrType.bv_val = (char *)startPos;
1192 attrType.bv_len = len;
1198 /* spaces may not be allowed */
1199 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1200 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1205 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1210 /* need equal sign */
1211 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1216 /* spaces may not be allowed */
1217 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1218 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1223 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1229 * octothorpe means a BER encoded value will follow
1230 * FIXME: I don't think DCE will allow it
1232 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1234 attrValueEncoding = LDAP_AVA_BINARY;
1235 state = B4BINARYVALUE;
1239 /* STRING value expected */
1242 * if we're pedantic, an attribute type in OID form
1243 * SHOULD imply a BER encoded attribute value; we
1244 * should at least issue a warning
1246 if ( ( flags & LDAP_DN_PEDANTIC )
1247 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1248 /* OID attrType SHOULD use binary encoding */
1252 attrValueEncoding = LDAP_AVA_STRING;
1255 * LDAPv2 allows the attribute value to be quoted;
1256 * also, IA5 values are expected, in principle
1258 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1259 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1261 state = B4IA5VALUEQUOTED;
1265 if ( LDAP_DN_LDAPV2( flags ) ) {
1272 * here STRING means RFC 2253 string
1273 * FIXME: what about DCE strings?
1279 state = B4STRINGVALUE;
1284 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1292 switch ( LDAP_DN_FORMAT( flags ) ) {
1293 case LDAP_DN_FORMAT_LDAP:
1294 case LDAP_DN_FORMAT_LDAPV3:
1295 if ( str2strval( p, stoplen - ( p - str ),
1296 &attrValue, &p, flags,
1297 &attrValueEncoding, ctx ) ) {
1302 case LDAP_DN_FORMAT_DCE:
1303 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1316 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1323 case B4IA5VALUEQUOTED:
1325 /* lead quote already stripped */
1326 if ( quotedIA52strval( p, &attrValue,
1327 &p, flags, ctx ) ) {
1337 if ( !( flags & LDAP_DN_SKIP ) ) {
1341 * we accept empty values
1343 ava = ldapava_new( &attrType, &attrValue,
1344 attrValueEncoding, ctx );
1345 if ( ava == NULL ) {
1346 rc = LDAP_NO_MEMORY;
1349 tmpRDN[navas++] = ava;
1351 attrValue.bv_val = NULL;
1352 attrValue.bv_len = 0;
1355 * prepare room for new AVAs if needed
1357 if (navas == num_slots) {
1360 if ( tmpRDN == tmpRDN_ ) {
1361 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1362 if ( tmp == NULL ) {
1363 rc = LDAP_NO_MEMORY;
1366 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1369 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1370 if ( tmp == NULL ) {
1371 rc = LDAP_NO_MEMORY;
1382 * if we got an AVA separator ('+', or ',' for DCE )
1383 * we expect a new AVA for this RDN; otherwise
1384 * we add the RDN to the DN
1386 switch ( LDAP_DN_FORMAT( flags ) ) {
1387 case LDAP_DN_FORMAT_LDAP:
1388 case LDAP_DN_FORMAT_LDAPV3:
1389 case LDAP_DN_FORMAT_LDAPV2:
1390 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1395 case LDAP_DN_FORMAT_DCE:
1396 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1404 * the RDN is over, phew
1407 if ( !( flags & LDAP_DN_SKIP ) ) {
1408 newRDN = (LDAPRDN)LDAP_MALLOCX(
1409 sizeof(LDAPAVA) * (navas+1), ctx );
1410 if ( newRDN == NULL ) {
1411 rc = LDAP_NO_MEMORY;
1414 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1415 newRDN[navas] = NULL;
1423 /* they should have been used in an AVA */
1424 attrType.bv_val = NULL;
1425 attrValue.bv_val = NULL;
1440 /* They are set to NULL after they're used in an AVA */
1442 if ( attrValue.bv_val ) {
1443 LDAP_FREEX( attrValue.bv_val, ctx );
1446 for ( navas-- ; navas >= 0; navas-- ) {
1447 ldapava_free( tmpRDN[navas], ctx );
1452 if ( tmpRDN != tmpRDN_ ) {
1453 LDAP_FREEX( tmpRDN, ctx );
1464 * reads in a UTF-8 string value, unescaping stuff:
1465 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1466 * '\' + HEXPAIR(p) -> unhex(p)
1469 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1471 const char *p, *end, *startPos, *endPos = NULL;
1472 ber_len_t len, escapes;
1479 end = str + stoplen;
1480 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1481 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1483 if ( p[ 0 ] == '\0' ) {
1486 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1491 if ( LDAP_DN_HEXPAIR( p ) ) {
1494 hexstr2bin( p, &c );
1497 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1500 * we assume the string is UTF-8
1502 *retFlags = LDAP_AVA_NONPRINTABLE;
1509 if ( LDAP_DN_PEDANTIC & flags ) {
1513 * we do not allow escaping
1514 * of chars that don't need
1515 * to and do not belong to
1520 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1521 if ( p[ 0 ] == '\0' ) {
1524 *retFlags = LDAP_AVA_NONPRINTABLE;
1526 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1527 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1530 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1532 * FIXME: maybe we can add
1533 * escapes if not pedantic?
1540 * we do allow unescaped spaces at the end
1541 * of the value only in non-pedantic mode
1543 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1544 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1545 if ( flags & LDAP_DN_PEDANTIC ) {
1549 /* strip trailing (unescaped) spaces */
1550 for ( endPos = p - 1;
1551 endPos > startPos + 1 &&
1552 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1553 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1560 if ( flags & LDAP_DN_SKIP ) {
1565 * FIXME: test memory?
1567 len = ( endPos ? endPos : p ) - startPos - escapes;
1570 if ( escapes == 0 ) {
1571 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1572 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1573 AC_MEMCPY( val->bv_val, startPos, len );
1574 val->bv_val[ len ] = '\0';
1576 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1582 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1583 for ( s = 0, d = 0; d < len; ) {
1584 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1586 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1587 val->bv_val[ d++ ] =
1590 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1593 hexstr2bin( &startPos[ s ], &c );
1594 val->bv_val[ d++ ] = c;
1598 /* we should never get here */
1603 val->bv_val[ d++ ] = startPos[ s++ ];
1607 val->bv_val[ d ] = '\0';
1615 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1617 const char *p, *startPos, *endPos = NULL;
1618 ber_len_t len, escapes;
1626 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1627 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1629 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1636 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1641 * FIXME: can we accept anything else? I guess we need
1642 * to stop if a value is not legal
1647 * (unescaped) trailing spaces are trimmed must be silently ignored;
1650 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1651 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1652 if ( flags & LDAP_DN_PEDANTIC ) {
1656 /* strip trailing (unescaped) spaces */
1657 for ( endPos = p - 1;
1658 endPos > startPos + 1 &&
1659 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1660 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1667 if ( flags & LDAP_DN_SKIP ) {
1671 len = ( endPos ? endPos : p ) - startPos - escapes;
1673 if ( escapes == 0 ){
1674 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1679 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1680 for ( s = 0, d = 0; d < len; ) {
1682 * This point is reached only if escapes
1683 * are properly used, so all we need to
1686 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1690 val->bv_val[ d++ ] = startPos[ s++ ];
1692 val->bv_val[ d ] = '\0';
1693 assert( strlen( val->bv_val ) == len );
1700 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1702 const char *p, *startPos, *endPos = NULL;
1703 ber_len_t len, escapes;
1715 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1716 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1718 if ( p[ 0 ] == '\0' ) {
1722 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1723 && ( LDAP_DN_PEDANTIC & flags ) ) {
1728 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1733 * FIXME: can we accept anything else? I guess we need
1734 * to stop if a value is not legal
1738 /* strip trailing (unescaped) spaces */
1740 endPos > startPos + 1 &&
1741 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1742 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1748 if ( flags & LDAP_DN_SKIP ) {
1752 len = ( endPos ? endPos : p ) - startPos - escapes;
1754 if ( escapes == 0 ) {
1755 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1760 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1761 for ( s = 0, d = 0; d < len; ) {
1762 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1765 val->bv_val[ d++ ] = startPos[ s++ ];
1767 val->bv_val[ d ] = '\0';
1768 assert( strlen( val->bv_val ) == len );
1775 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1777 const char *p, *startPos, *endPos = NULL;
1779 unsigned escapes = 0;
1787 /* initial quote already eaten */
1788 for ( startPos = p = str; p[ 0 ]; p++ ) {
1790 * According to RFC 1779, the quoted value can
1791 * contain escaped as well as unescaped special values;
1792 * as a consequence we tolerate escaped values
1793 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1794 * (e.g. '","' -> '\,').
1796 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1797 if ( p[ 1 ] == '\0' ) {
1802 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1803 && ( LDAP_DN_PEDANTIC & flags ) ) {
1805 * do we allow to escape normal chars?
1806 * LDAPv2 does not allow any mechanism
1807 * for escaping chars with '\' and hex
1814 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1816 /* eat closing quotes */
1822 * FIXME: can we accept anything else? I guess we need
1823 * to stop if a value is not legal
1827 if ( endPos == NULL ) {
1831 /* Strip trailing (unescaped) spaces */
1832 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1837 if ( flags & LDAP_DN_SKIP ) {
1841 len = endPos - startPos - escapes;
1842 assert( endPos >= startPos + escapes );
1844 if ( escapes == 0 ) {
1845 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1850 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1853 for ( s = d = 0; d < len; ) {
1854 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1857 val->bv_val[ d++ ] = str[ s++ ];
1859 val->bv_val[ d ] = '\0';
1860 assert( strlen( val->bv_val ) == len );
1867 hexstr2bin( const char *str, char *c )
1877 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1881 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1884 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1891 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1895 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1896 *c += c2 - 'A' + 10;
1898 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1899 *c += c2 - 'a' + 10;
1907 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1909 const char *p, *startPos, *endPos = NULL;
1919 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1920 switch ( LDAP_DN_FORMAT( flags ) ) {
1921 case LDAP_DN_FORMAT_LDAPV3:
1922 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1927 case LDAP_DN_FORMAT_LDAP:
1928 case LDAP_DN_FORMAT_LDAPV2:
1929 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1934 case LDAP_DN_FORMAT_DCE:
1935 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1941 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1942 if ( flags & LDAP_DN_PEDANTIC ) {
1947 for ( ; p[ 0 ]; p++ ) {
1948 switch ( LDAP_DN_FORMAT( flags ) ) {
1949 case LDAP_DN_FORMAT_LDAPV3:
1950 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1955 case LDAP_DN_FORMAT_LDAP:
1956 case LDAP_DN_FORMAT_LDAPV2:
1957 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1962 case LDAP_DN_FORMAT_DCE:
1963 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1972 if ( !LDAP_DN_HEXPAIR( p ) ) {
1980 if ( flags & LDAP_DN_SKIP ) {
1984 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1986 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1989 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1990 if ( val->bv_val == NULL ) {
1991 return( LDAP_NO_MEMORY );
1994 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1997 hexstr2bin( &startPos[ s ], &c );
1999 val->bv_val[ d ] = c;
2002 val->bv_val[ d ] = '\0';
2008 * convert a byte in a hexadecimal pair
2011 byte2hexpair( const char *val, char *pair )
2013 static const char hexdig[] = "0123456789ABCDEF";
2019 * we assume the string has enough room for the hex encoding
2023 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2024 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2030 * convert a binary value in hexadecimal pairs
2033 binval2hexstr( struct berval *val, char *str )
2040 if ( val->bv_len == 0 ) {
2045 * we assume the string has enough room for the hex encoding
2049 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2050 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2057 * Length of the string representation, accounting for escaped hex
2061 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2063 ber_len_t l, cl = 1;
2065 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2066 #ifdef PRETTY_ESCAPE
2067 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2068 #endif /* PRETTY_ESCAPE */
2074 if ( val->bv_len == 0 ) {
2078 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2083 if ( p[ 0 ] == '\0' ) {
2089 cl = LDAP_UTF8_CHARLEN2( p, cl );
2091 /* illegal utf-8 char! */
2094 } else if ( cl > 1 ) {
2097 for ( cnt = 1; cnt < cl; cnt++ ) {
2098 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2102 l += escaped_byte_len * cl;
2104 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2105 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2106 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2107 #ifdef PRETTY_ESCAPE
2109 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2111 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2115 * there might be some chars we want
2116 * to escape in form of a couple
2117 * of hexdigits for optimization purposes
2122 l += escaped_ascii_len;
2124 #else /* ! PRETTY_ESCAPE */
2126 #endif /* ! PRETTY_ESCAPE */
2139 * convert to string representation, escaping with hex the UTF-8 stuff;
2140 * assume the destination has enough room for escaping
2143 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2145 ber_len_t s, d, end;
2151 if ( val->bv_len == 0 ) {
2157 * we assume the string has enough room for the hex encoding
2160 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2166 if ( val->bv_val[ s ] == '\0' ) {
2176 * The length was checked in strval2strlen();
2177 * LDAP_UTF8_CHARLEN() should suffice
2179 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2183 * there might be some chars we want to escape in form
2184 * of a couple of hexdigits for optimization purposes
2186 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2187 #ifdef PRETTY_ESCAPE
2189 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2191 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2193 #else /* ! PRETTY_ESCAPE */
2194 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2195 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2196 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2198 #endif /* ! PRETTY_ESCAPE */
2202 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2207 } else if ( cl > 1 ) {
2209 str[ d++ ] = val->bv_val[ s++ ];
2213 #ifdef PRETTY_ESCAPE
2214 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2215 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2216 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2218 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2219 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2225 #endif /* PRETTY_ESCAPE */
2226 str[ d++ ] = val->bv_val[ s++ ];
2236 * Length of the IA5 string representation (no UTF-8 allowed)
2239 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2248 if ( val->bv_len == 0 ) {
2252 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2254 * Turn value into a binary encoded BER
2259 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2260 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2261 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2262 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2277 * convert to string representation (np UTF-8)
2278 * assume the destination has enough room for escaping
2281 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2283 ber_len_t s, d, end;
2289 if ( val->bv_len == 0 ) {
2294 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2296 * Turn value into a binary encoded BER
2303 * we assume the string has enough room for the hex encoding
2307 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2308 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2309 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2310 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2313 str[ d++ ] = val->bv_val[ s++ ];
2323 * Length of the (supposedly) DCE string representation,
2324 * accounting for escaped hex of UTF-8 chars
2327 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2336 if ( val->bv_len == 0 ) {
2340 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2342 * FIXME: Turn the value into a binary encoded BER?
2347 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2348 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2363 * convert to (supposedly) DCE string representation,
2364 * escaping with hex the UTF-8 stuff;
2365 * assume the destination has enough room for escaping
2368 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2376 if ( val->bv_len == 0 ) {
2381 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2383 * FIXME: Turn the value into a binary encoded BER?
2391 * we assume the string has enough room for the hex encoding
2395 for ( s = 0, d = 0; s < val->bv_len; ) {
2396 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2399 str[ d++ ] = val->bv_val[ s++ ];
2409 * Length of the (supposedly) AD canonical string representation,
2410 * accounting for escaped hex of UTF-8 chars
2413 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2422 if ( val->bv_len == 0 ) {
2426 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2428 * FIXME: Turn the value into a binary encoded BER?
2433 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2434 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2449 * convert to (supposedly) AD string representation,
2450 * escaping with hex the UTF-8 stuff;
2451 * assume the destination has enough room for escaping
2454 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2462 if ( val->bv_len == 0 ) {
2467 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2469 * FIXME: Turn the value into a binary encoded BER?
2477 * we assume the string has enough room for the hex encoding
2481 for ( s = 0, d = 0; s < val->bv_len; ) {
2482 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2485 str[ d++ ] = val->bv_val[ s++ ];
2495 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2496 * the first part of the AD representation of the DN is written in DNS
2497 * form, i.e. dot separated domain name components (as suggested
2498 * by Luke Howard, http://www.padl.com/~lukeh)
2501 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2504 int domain = 0, first = 1;
2505 ber_len_t l = 1; /* we move the null also */
2508 /* we are guaranteed there's enough memory in str */
2514 assert( *iRDN >= 0 );
2516 str = bv->bv_val + pos;
2518 for ( i = *iRDN; i >= 0; i-- ) {
2528 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2536 AC_MEMCPY( str, ava->la_value.bv_val,
2537 ava->la_value.bv_len + 1);
2538 l += ava->la_value.bv_len;
2541 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2542 AC_MEMCPY( str, ava->la_value.bv_val,
2543 ava->la_value.bv_len );
2544 str[ ava->la_value.bv_len ] = '.';
2545 l += ava->la_value.bv_len + 1;
2550 bv->bv_len = pos + l - 1;
2556 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2557 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2564 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2565 LDAPAVA *ava = rdn[ iAVA ];
2567 /* len(type) + '=' + '+' | ',' */
2568 l += ava->la_attr.bv_len + 2;
2570 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2571 /* octothorpe + twice the length */
2572 l += 1 + 2 * ava->la_value.bv_len;
2576 unsigned f = flags | ava->la_flags;
2578 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2591 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2592 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2597 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2598 LDAPAVA *ava = rdn[ iAVA ];
2600 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2601 ava->la_attr.bv_len );
2602 l += ava->la_attr.bv_len;
2606 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2608 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2611 l += 2 * ava->la_value.bv_len;
2615 unsigned f = flags | ava->la_flags;
2617 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2622 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2631 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2638 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2639 LDAPAVA *ava = rdn[ iAVA ];
2641 /* len(type) + '=' + ',' | '/' */
2642 l += ava->la_attr.bv_len + 2;
2644 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2645 /* octothorpe + twice the length */
2646 l += 1 + 2 * ava->la_value.bv_len;
2649 unsigned f = flags | ava->la_flags;
2651 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2664 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2669 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2670 LDAPAVA *ava = rdn[ iAVA ];
2675 str[ l++ ] = ( iAVA ? ',' : '/' );
2678 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2679 ava->la_attr.bv_len );
2680 l += ava->la_attr.bv_len;
2684 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2686 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2689 l += 2 * ava->la_value.bv_len;
2692 unsigned f = flags | ava->la_flags;
2694 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2707 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2717 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2718 LDAPAVA *ava = rdn[ iAVA ];
2721 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2723 /* FIXME: are binary values allowed in UFN? */
2724 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2725 /* octothorpe + twice the value */
2726 l += 1 + 2 * ava->la_value.bv_len;
2730 unsigned f = flags | ava->la_flags;
2732 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2745 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2750 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2751 LDAPAVA *ava = rdn[ iAVA ];
2753 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2755 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2758 l += 2 * ava->la_value.bv_len;
2762 unsigned f = flags | ava->la_flags;
2764 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2770 if ( rdn[ iAVA + 1 ] ) {
2771 AC_MEMCPY( &str[ l ], " + ", 3 );
2775 AC_MEMCPY( &str[ l ], ", ", 2 );
2786 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2796 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2797 LDAPAVA *ava = rdn[ iAVA ];
2802 /* FIXME: are binary values allowed in UFN? */
2803 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2804 /* octothorpe + twice the value */
2805 l += 1 + 2 * ava->la_value.bv_len;
2808 unsigned f = flags | ava->la_flags;
2810 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2823 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2828 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2829 LDAPAVA *ava = rdn[ iAVA ];
2834 str[ l++ ] = ( iAVA ? ',' : '/' );
2837 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2839 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2842 l += 2 * ava->la_value.bv_len;
2845 unsigned f = flags | ava->la_flags;
2847 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2862 * Returns in str a string representation of rdn based on flags.
2863 * There is some duplication of code between this and ldap_dn2str;
2864 * this is wanted to reduce the allocation of temporary buffers.
2867 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2874 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2875 return LDAP_PARAM_ERROR;
2878 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2884 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2886 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2890 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2900 if ( rdn == NULL ) {
2901 bv->bv_val = LDAP_STRDUPX( "", ctx );
2902 return( LDAP_SUCCESS );
2906 * This routine wastes "back" bytes at the end of the string
2909 switch ( LDAP_DN_FORMAT( flags ) ) {
2910 case LDAP_DN_FORMAT_LDAPV3:
2911 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2912 return LDAP_DECODING_ERROR;
2916 case LDAP_DN_FORMAT_LDAPV2:
2917 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2918 return LDAP_DECODING_ERROR;
2922 case LDAP_DN_FORMAT_UFN:
2923 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2924 return LDAP_DECODING_ERROR;
2928 case LDAP_DN_FORMAT_DCE:
2929 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2930 return LDAP_DECODING_ERROR;
2934 case LDAP_DN_FORMAT_AD_CANONICAL:
2935 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2936 return LDAP_DECODING_ERROR;
2941 return LDAP_PARAM_ERROR;
2944 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2946 switch ( LDAP_DN_FORMAT( flags ) ) {
2947 case LDAP_DN_FORMAT_LDAPV3:
2948 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2952 case LDAP_DN_FORMAT_LDAPV2:
2953 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2957 case LDAP_DN_FORMAT_UFN:
2958 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2962 case LDAP_DN_FORMAT_DCE:
2963 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2967 case LDAP_DN_FORMAT_AD_CANONICAL:
2968 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2973 /* need at least one of the previous */
2974 return LDAP_PARAM_ERROR;
2978 LDAP_FREEX( bv->bv_val, ctx );
2982 bv->bv_len = l - back;
2983 bv->bv_val[ bv->bv_len ] = '\0';
2985 return LDAP_SUCCESS;
2989 * Very bulk implementation; many optimizations can be performed
2990 * - a NULL dn results in an empty string ""
2993 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2994 * we must encode it in binary form ('#' + HEXPAIRs)
2995 * b) does DCE/AD support UTF-8?
2996 * no clue; don't think so.
2997 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2998 * use binary encoded BER
3000 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
3007 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
3008 return LDAP_PARAM_ERROR;
3011 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
3016 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
3018 return ldap_dn2bv_x( dn, bv, flags, NULL );
3021 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
3024 int rc = LDAP_ENCODING_ERROR;
3027 /* stringifying helpers for LDAPv3/LDAPv2 */
3028 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3029 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3036 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3038 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3042 * a null dn means an empty dn string
3043 * FIXME: better raise an error?
3046 bv->bv_val = LDAP_STRDUPX( "", ctx );
3047 return( LDAP_SUCCESS );
3050 switch ( LDAP_DN_FORMAT( flags ) ) {
3051 case LDAP_DN_FORMAT_LDAPV3:
3052 sv2l = strval2strlen;
3056 case LDAP_DN_FORMAT_LDAPV2:
3057 sv2l = strval2IA5strlen;
3058 sv2s = strval2IA5str;
3061 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3063 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3064 goto return_results;
3070 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3071 rc = LDAP_NO_MEMORY;
3075 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3078 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3080 LDAP_FREEX( bv->bv_val, ctx );
3082 goto return_results;
3090 * trim the last ',' (the allocated memory
3091 * is one byte longer than required)
3093 bv->bv_len = len - 1;
3094 bv->bv_val[ bv->bv_len ] = '\0';
3099 case LDAP_DN_FORMAT_UFN: {
3101 * FIXME: quoting from RFC 1781:
3103 To take a distinguished name, and generate a name of this format with
3104 attribute types omitted, the following steps are followed.
3106 1. If the first attribute is of type CommonName, the type may be
3109 2. If the last attribute is of type Country, the type may be
3112 3. If the last attribute is of type Country, the last
3113 Organisation attribute may have the type omitted.
3115 4. All attributes of type OrganisationalUnit may have the type
3116 omitted, unless they are after an Organisation attribute or
3117 the first attribute is of type OrganisationalUnit.
3119 * this should be the pedantic implementation.
3121 * Here the standard implementation reflects
3122 * the one historically provided by OpenLDAP
3123 * (and UMIch, I presume), with the variant
3124 * of spaces and plusses (' + ') separating
3127 * A non-standard but nice implementation could
3128 * be to turn the final "dc" attributes into a
3129 * dot-separated domain.
3131 * Other improvements could involve the use of
3132 * friendly country names and so.
3135 int leftmost_dc = -1;
3137 #endif /* DC_IN_UFN */
3139 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3142 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3143 goto return_results;
3148 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3149 if ( leftmost_dc == -1 ) {
3155 #endif /* DC_IN_UFN */
3158 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3159 rc = LDAP_NO_MEMORY;
3164 if ( leftmost_dc == -1 ) {
3165 #endif /* DC_IN_UFN */
3166 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3169 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3171 LDAP_FREEX( bv->bv_val, ctx );
3173 goto return_results;
3179 * trim the last ', ' (the allocated memory
3180 * is two bytes longer than required)
3182 bv->bv_len = len - 2;
3183 bv->bv_val[ bv->bv_len ] = '\0';
3186 last_iRDN = iRDN - 1;
3188 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3191 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3193 LDAP_FREEX( bv->bv_val, ctx );
3195 goto return_results;
3200 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3201 LDAP_FREEX( bv->bv_val, ctx );
3203 goto return_results;
3206 /* the string is correctly terminated by dn2domain */
3208 #endif /* DC_IN_UFN */
3214 case LDAP_DN_FORMAT_DCE:
3215 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3217 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3218 goto return_results;
3224 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3225 rc = LDAP_NO_MEMORY;
3229 for ( l = 0; iRDN--; ) {
3232 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3234 LDAP_FREEX( bv->bv_val, ctx );
3236 goto return_results;
3244 bv->bv_val[ bv->bv_len ] = '\0';
3249 case LDAP_DN_FORMAT_AD_CANONICAL: {
3250 int trailing_slash = 1;
3253 * Sort of UFN for DCE DNs: a slash ('/') separated
3254 * global->local DN with no types; strictly speaking,
3255 * the naming context should be a domain, which is
3256 * written in DNS-style, e.g. dot-deparated.
3260 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3264 * "microsoft.com/People/Bill,Gates"
3266 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3269 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3270 goto return_results;
3276 /* reserve room for trailing '/' in case the DN
3277 * is exactly a domain */
3278 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3280 rc = LDAP_NO_MEMORY;
3285 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3286 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3291 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3292 flags, &rdnl, 0 ) ) {
3293 LDAP_FREEX( bv->bv_val, ctx );
3295 goto return_results;
3304 * Strictly speaking, AD canonical requires
3305 * a DN to be in the form "..., dc=smtg",
3306 * i.e. terminated by a domain component
3308 if ( flags & LDAP_DN_PEDANTIC ) {
3309 LDAP_FREEX( bv->bv_val, ctx );
3311 rc = LDAP_ENCODING_ERROR;
3315 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3318 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3319 flags, &rdnl, first ) ) {
3320 LDAP_FREEX( bv->bv_val, ctx );
3322 goto return_results;
3331 if ( trailing_slash ) {
3332 /* the DN is exactly a domain -- need a trailing
3333 * slash; room was reserved in advance */
3334 bv->bv_val[ len ] = '/';
3339 bv->bv_val[ bv->bv_len ] = '\0';
3345 return LDAP_PARAM_ERROR;
3349 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s)=%d %s\n",
3350 bv->bv_val, rc, ldap_err2string( rc ) );
3352 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s)=%d %s\n",
3353 bv->bv_val, rc, ldap_err2string( rc ) );
3361 #include <openssl/x509.h>
3362 #include <openssl/err.h>
3364 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3365 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3366 * constructed DN will use numeric OIDs to identify attributeTypes,
3367 * and the func() will be invoked to rewrite the DN with the given
3370 * Otherwise the DN will use shortNames as defined in the OpenSSL
3373 * It's preferable to let slapd do the OID to attributeType mapping,
3374 * because the OpenSSL tables are known to have many typos in versions
3375 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3376 * so we're forced to use OpenSSL's mapping there.
3377 * -- Howard Chu 2002-04-18
3381 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3386 LDAPAVA *newAVA, *baseAVA;
3387 X509_NAME_ENTRY *ne;
3390 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3392 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3394 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3403 /* Get the number of AVAs. This is not necessarily the same as
3404 * the number of RDNs.
3406 navas = X509_NAME_entry_count( x509_name );
3408 /* Get the last element, to see how many RDNs there are */
3409 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3410 nrdns = ne->set + 1;
3412 /* Allocate the DN/RDN/AVA stuff as a single block */
3413 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3414 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3415 dnsize += sizeof(LDAPAVA) * navas;
3416 if (dnsize > sizeof(ptrs)) {
3417 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3418 if ( newDN == NULL )
3419 return LDAP_NO_MEMORY;
3421 newDN = (LDAPDN)ptrs;
3424 newDN[nrdns] = NULL;
3425 newRDN = (LDAPRDN)(newDN + nrdns+1);
3426 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3429 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3430 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3431 ne = X509_NAME_get_entry( x509_name, i );
3432 obj = X509_NAME_ENTRY_get_object( ne );
3433 str = X509_NAME_ENTRY_get_data( ne );
3435 /* If set changed, move to next RDN */
3436 if ( set != ne->set ) {
3437 /* If this is not the first time, end the
3438 * previous RDN and advance.
3444 newDN[j++] = newRDN;
3449 newAVA->la_private = NULL;
3450 newAVA->la_flags = LDAP_AVA_STRING;
3453 int n = OBJ_obj2nid( obj );
3457 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3458 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3460 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3461 __etoa( newAVA->la_attr.bv_val );
3462 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3465 get_oid: newAVA->la_attr.bv_val = oidptr;
3466 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3468 __etoa( newAVA->la_attr.bv_val );
3470 oidptr += newAVA->la_attr.bv_len + 1;
3471 oidrem -= newAVA->la_attr.bv_len + 1;
3473 /* Running out of OID buffer space? */
3475 if ( oidsize == 0 ) {
3476 oidsize = sizeof(oids) * 2;
3478 oidbuf = LDAP_MALLOC( oidsize );
3479 if ( oidbuf == NULL ) goto nomem;
3483 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3484 if ( oidbuf == NULL ) goto nomem;
3485 /* Buffer moved! Fix AVA pointers */
3486 if ( old != oidbuf ) {
3488 long dif = oidbuf - old;
3490 for (a=baseAVA; a<=newAVA; a++){
3491 if (a->la_attr.bv_val >= old &&
3492 a->la_attr.bv_val <= (old + oidsize))
3493 a->la_attr.bv_val += dif;
3496 oidptr = oidbuf + oidsize - oidrem;
3502 Val.bv_val = (char *) str->data;
3503 Val.bv_len = str->length;
3504 switch( str->type ) {
3505 case V_ASN1_UNIVERSALSTRING:
3506 /* This uses 32-bit ISO 10646-1 */
3507 csize = 4; goto to_utf8;
3508 case V_ASN1_BMPSTRING:
3509 /* This uses 16-bit ISO 10646-1 */
3510 csize = 2; goto to_utf8;
3511 case V_ASN1_T61STRING:
3512 /* This uses 8-bit, assume ISO 8859-1 */
3514 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3515 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3516 if (rc != LDAP_SUCCESS) goto nomem;
3517 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3519 case V_ASN1_UTF8STRING:
3520 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3521 /* This is already in UTF-8 encoding */
3522 case V_ASN1_IA5STRING:
3523 case V_ASN1_PRINTABLESTRING:
3524 /* These are always 7-bit strings */
3525 newAVA->la_value = Val;
3536 rc = func( newDN, flags, NULL );
3537 if ( rc != LDAP_SUCCESS )
3541 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3544 for (;baseAVA < newAVA; baseAVA++) {
3545 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3546 LDAP_FREE( baseAVA->la_attr.bv_val );
3547 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3548 LDAP_FREE( baseAVA->la_value.bv_val );
3552 LDAP_FREE( oidbuf );
3553 if ( newDN != (LDAPDN) ptrs )
3557 #endif /* HAVE_TLS */