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%s", str, flags, "" );
741 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
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,%u)=%d\n",
928 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
938 * Parses a relative DN according to flags up to a rdn separator
939 * or to the end of str.
940 * Returns the rdn and a pointer to the string continuation, which
941 * corresponds to the rdn separator or to '\0' in case the string is over.
944 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
945 char **n_in, unsigned flags )
950 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
952 bv.bv_len = strlen( str );
953 bv.bv_val = (char *) str;
955 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
959 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
960 char **n_in, unsigned flags )
962 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
966 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
967 char **n_in, unsigned flags, void *ctx )
969 const char **n = (const char **) n_in;
973 int rc = LDAP_DECODING_ERROR;
974 int attrTypeEncoding = LDAP_AVA_STRING,
975 attrValueEncoding = LDAP_AVA_STRING;
977 struct berval attrType = BER_BVNULL;
978 struct berval attrValue = BER_BVNULL;
980 LDAPRDN newRDN = NULL;
981 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
982 int num_slots = TMP_AVA_SLOTS;
988 assert( bv->bv_len );
989 assert( bv->bv_val );
990 assert( rdn || flags & LDAP_DN_SKIP );
994 stoplen = bv->bv_len;
1001 switch ( LDAP_DN_FORMAT( flags ) ) {
1002 case LDAP_DN_FORMAT_LDAP:
1003 case LDAP_DN_FORMAT_LDAPV3:
1004 case LDAP_DN_FORMAT_LDAPV2:
1005 case LDAP_DN_FORMAT_DCE:
1008 /* unsupported in str2dn */
1009 case LDAP_DN_FORMAT_UFN:
1010 case LDAP_DN_FORMAT_AD_CANONICAL:
1011 return LDAP_PARAM_ERROR;
1013 case LDAP_DN_FORMAT_LBER:
1015 return LDAP_PARAM_ERROR;
1018 if ( bv->bv_len == 0 ) {
1019 return LDAP_SUCCESS;
1023 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1024 /* value must have embedded NULs */
1025 return LDAP_DECODING_ERROR;
1029 for ( ; p[ 0 ] || state == GOTAVA; ) {
1032 * The parser in principle advances one token a time,
1033 * or toggles state if preferable.
1038 * an AttributeType can be encoded as:
1039 * - its string representation; in detail, implementations
1040 * MUST recognize AttributeType string type names listed
1041 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1042 * MAY recognize other names.
1043 * - its numeric OID (a dotted decimal string); in detail
1044 * RFC 2253 asserts that ``Implementations MUST allow
1045 * an oid in the attribute type to be prefixed by one
1046 * of the character strings "oid." or "OID."''. As soon
1047 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1048 * I'm not sure whether this is required or not any
1049 * longer; to be liberal, we still implement it.
1052 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1053 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1060 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1061 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1066 /* whitespace is allowed (and trimmed) */
1068 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1073 /* error: we expected an AVA */
1079 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1080 state = B4OIDATTRTYPE;
1084 /* else must be alpha */
1085 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1089 /* LDAPv2 "oid." prefix */
1090 if ( LDAP_DN_LDAPV2( flags ) ) {
1092 * to be overly pedantic, we only accept
1095 if ( flags & LDAP_DN_PEDANTIC ) {
1096 if ( !strncmp( p, "OID.", 4 )
1097 || !strncmp( p, "oid.", 4 ) ) {
1099 state = B4OIDATTRTYPE;
1103 if ( !strncasecmp( p, "oid.", 4 ) ) {
1105 state = B4OIDATTRTYPE;
1111 state = B4STRINGATTRTYPE;
1114 case B4OIDATTRTYPE: {
1115 int err = LDAP_SUCCESS;
1117 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1120 if ( err != LDAP_SUCCESS ) {
1123 attrType.bv_len = p - attrType.bv_val;
1125 attrTypeEncoding = LDAP_AVA_BINARY;
1127 state = B4AVAEQUALS;
1131 case B4STRINGATTRTYPE: {
1132 const char *startPos, *endPos = NULL;
1136 * the starting char has been found to be
1137 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1138 * FIXME: DCE attr types seem to have a more
1139 * restrictive syntax (no '-' ...)
1141 for ( startPos = p++; p[ 0 ]; p++ ) {
1142 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1146 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1149 * RFC 2253 does not explicitly
1150 * allow lang extensions to attribute
1153 if ( flags & LDAP_DN_PEDANTIC ) {
1158 * we trim ';' and following lang
1159 * and so from attribute types
1162 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1163 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1171 len = ( endPos ? endPos : p ) - startPos;
1176 attrTypeEncoding = LDAP_AVA_STRING;
1179 * here we need to decide whether to use it as is
1180 * or turn it in OID form; as a consequence, we
1181 * need to decide whether to binary encode the value
1184 state = B4AVAEQUALS;
1186 if ( flags & LDAP_DN_SKIP ) {
1190 attrType.bv_val = (char *)startPos;
1191 attrType.bv_len = len;
1197 /* spaces may not be allowed */
1198 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1199 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1204 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1209 /* need equal sign */
1210 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1215 /* spaces may not be allowed */
1216 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1217 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1222 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1228 * octothorpe means a BER encoded value will follow
1229 * FIXME: I don't think DCE will allow it
1231 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1233 attrValueEncoding = LDAP_AVA_BINARY;
1234 state = B4BINARYVALUE;
1238 /* STRING value expected */
1241 * if we're pedantic, an attribute type in OID form
1242 * SHOULD imply a BER encoded attribute value; we
1243 * should at least issue a warning
1245 if ( ( flags & LDAP_DN_PEDANTIC )
1246 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1247 /* OID attrType SHOULD use binary encoding */
1251 attrValueEncoding = LDAP_AVA_STRING;
1254 * LDAPv2 allows the attribute value to be quoted;
1255 * also, IA5 values are expected, in principle
1257 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1258 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1260 state = B4IA5VALUEQUOTED;
1264 if ( LDAP_DN_LDAPV2( flags ) ) {
1271 * here STRING means RFC 2253 string
1272 * FIXME: what about DCE strings?
1278 state = B4STRINGVALUE;
1283 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1291 switch ( LDAP_DN_FORMAT( flags ) ) {
1292 case LDAP_DN_FORMAT_LDAP:
1293 case LDAP_DN_FORMAT_LDAPV3:
1294 if ( str2strval( p, stoplen - ( p - str ),
1295 &attrValue, &p, flags,
1296 &attrValueEncoding, ctx ) ) {
1301 case LDAP_DN_FORMAT_DCE:
1302 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1315 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1322 case B4IA5VALUEQUOTED:
1324 /* lead quote already stripped */
1325 if ( quotedIA52strval( p, &attrValue,
1326 &p, flags, ctx ) ) {
1336 if ( !( flags & LDAP_DN_SKIP ) ) {
1340 * we accept empty values
1342 ava = ldapava_new( &attrType, &attrValue,
1343 attrValueEncoding, ctx );
1344 if ( ava == NULL ) {
1345 rc = LDAP_NO_MEMORY;
1348 tmpRDN[navas++] = ava;
1350 attrValue.bv_val = NULL;
1351 attrValue.bv_len = 0;
1354 * prepare room for new AVAs if needed
1356 if (navas == num_slots) {
1359 if ( tmpRDN == tmpRDN_ ) {
1360 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1361 if ( tmp == NULL ) {
1362 rc = LDAP_NO_MEMORY;
1365 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1368 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1369 if ( tmp == NULL ) {
1370 rc = LDAP_NO_MEMORY;
1381 * if we got an AVA separator ('+', or ',' for DCE )
1382 * we expect a new AVA for this RDN; otherwise
1383 * we add the RDN to the DN
1385 switch ( LDAP_DN_FORMAT( flags ) ) {
1386 case LDAP_DN_FORMAT_LDAP:
1387 case LDAP_DN_FORMAT_LDAPV3:
1388 case LDAP_DN_FORMAT_LDAPV2:
1389 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1394 case LDAP_DN_FORMAT_DCE:
1395 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1403 * the RDN is over, phew
1406 if ( !( flags & LDAP_DN_SKIP ) ) {
1407 newRDN = (LDAPRDN)LDAP_MALLOCX(
1408 sizeof(LDAPAVA) * (navas+1), ctx );
1409 if ( newRDN == NULL ) {
1410 rc = LDAP_NO_MEMORY;
1413 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1414 newRDN[navas] = NULL;
1422 /* they should have been used in an AVA */
1423 attrType.bv_val = NULL;
1424 attrValue.bv_val = NULL;
1439 /* They are set to NULL after they're used in an AVA */
1441 if ( attrValue.bv_val ) {
1442 LDAP_FREEX( attrValue.bv_val, ctx );
1445 for ( navas-- ; navas >= 0; navas-- ) {
1446 ldapava_free( tmpRDN[navas], ctx );
1451 if ( tmpRDN != tmpRDN_ ) {
1452 LDAP_FREEX( tmpRDN, ctx );
1463 * reads in a UTF-8 string value, unescaping stuff:
1464 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1465 * '\' + HEXPAIR(p) -> unhex(p)
1468 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1470 const char *p, *end, *startPos, *endPos = NULL;
1471 ber_len_t len, escapes;
1478 end = str + stoplen;
1479 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1480 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1482 if ( p[ 0 ] == '\0' ) {
1485 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1490 if ( LDAP_DN_HEXPAIR( p ) ) {
1493 hexstr2bin( p, &c );
1496 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1499 * we assume the string is UTF-8
1501 *retFlags = LDAP_AVA_NONPRINTABLE;
1508 if ( LDAP_DN_PEDANTIC & flags ) {
1512 * we do not allow escaping
1513 * of chars that don't need
1514 * to and do not belong to
1519 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1520 if ( p[ 0 ] == '\0' ) {
1523 *retFlags = LDAP_AVA_NONPRINTABLE;
1525 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1526 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1529 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1531 * FIXME: maybe we can add
1532 * escapes if not pedantic?
1539 * we do allow unescaped spaces at the end
1540 * of the value only in non-pedantic mode
1542 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1543 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1544 if ( flags & LDAP_DN_PEDANTIC ) {
1548 /* strip trailing (unescaped) spaces */
1549 for ( endPos = p - 1;
1550 endPos > startPos + 1 &&
1551 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1552 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1559 if ( flags & LDAP_DN_SKIP ) {
1564 * FIXME: test memory?
1566 len = ( endPos ? endPos : p ) - startPos - escapes;
1569 if ( escapes == 0 ) {
1570 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1571 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1572 AC_MEMCPY( val->bv_val, startPos, len );
1573 val->bv_val[ len ] = '\0';
1575 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1581 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1582 for ( s = 0, d = 0; d < len; ) {
1583 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1585 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1586 val->bv_val[ d++ ] =
1589 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1592 hexstr2bin( &startPos[ s ], &c );
1593 val->bv_val[ d++ ] = c;
1597 /* we should never get here */
1602 val->bv_val[ d++ ] = startPos[ s++ ];
1606 val->bv_val[ d ] = '\0';
1614 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1616 const char *p, *startPos, *endPos = NULL;
1617 ber_len_t len, escapes;
1625 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1626 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1628 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1635 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1640 * FIXME: can we accept anything else? I guess we need
1641 * to stop if a value is not legal
1646 * (unescaped) trailing spaces are trimmed must be silently ignored;
1649 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1650 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1651 if ( flags & LDAP_DN_PEDANTIC ) {
1655 /* strip trailing (unescaped) spaces */
1656 for ( endPos = p - 1;
1657 endPos > startPos + 1 &&
1658 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1659 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1666 if ( flags & LDAP_DN_SKIP ) {
1670 len = ( endPos ? endPos : p ) - startPos - escapes;
1672 if ( escapes == 0 ){
1673 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1678 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1679 for ( s = 0, d = 0; d < len; ) {
1681 * This point is reached only if escapes
1682 * are properly used, so all we need to
1685 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1689 val->bv_val[ d++ ] = startPos[ s++ ];
1691 val->bv_val[ d ] = '\0';
1692 assert( strlen( val->bv_val ) == len );
1699 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1701 const char *p, *startPos, *endPos = NULL;
1702 ber_len_t len, escapes;
1714 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1715 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1717 if ( p[ 0 ] == '\0' ) {
1721 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1722 && ( LDAP_DN_PEDANTIC & flags ) ) {
1727 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1732 * FIXME: can we accept anything else? I guess we need
1733 * to stop if a value is not legal
1737 /* strip trailing (unescaped) spaces */
1739 endPos > startPos + 1 &&
1740 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1741 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1747 if ( flags & LDAP_DN_SKIP ) {
1751 len = ( endPos ? endPos : p ) - startPos - escapes;
1753 if ( escapes == 0 ) {
1754 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1759 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1760 for ( s = 0, d = 0; d < len; ) {
1761 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1764 val->bv_val[ d++ ] = startPos[ s++ ];
1766 val->bv_val[ d ] = '\0';
1767 assert( strlen( val->bv_val ) == len );
1774 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1776 const char *p, *startPos, *endPos = NULL;
1778 unsigned escapes = 0;
1786 /* initial quote already eaten */
1787 for ( startPos = p = str; p[ 0 ]; p++ ) {
1789 * According to RFC 1779, the quoted value can
1790 * contain escaped as well as unescaped special values;
1791 * as a consequence we tolerate escaped values
1792 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1793 * (e.g. '","' -> '\,').
1795 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1796 if ( p[ 1 ] == '\0' ) {
1801 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1802 && ( LDAP_DN_PEDANTIC & flags ) ) {
1804 * do we allow to escape normal chars?
1805 * LDAPv2 does not allow any mechanism
1806 * for escaping chars with '\' and hex
1813 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1815 /* eat closing quotes */
1821 * FIXME: can we accept anything else? I guess we need
1822 * to stop if a value is not legal
1826 if ( endPos == NULL ) {
1830 /* Strip trailing (unescaped) spaces */
1831 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1836 if ( flags & LDAP_DN_SKIP ) {
1840 len = endPos - startPos - escapes;
1841 assert( endPos >= startPos + escapes );
1843 if ( escapes == 0 ) {
1844 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1849 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1852 for ( s = d = 0; d < len; ) {
1853 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1856 val->bv_val[ d++ ] = str[ s++ ];
1858 val->bv_val[ d ] = '\0';
1859 assert( strlen( val->bv_val ) == len );
1866 hexstr2bin( const char *str, char *c )
1876 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1880 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1883 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1890 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1894 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1895 *c += c2 - 'A' + 10;
1897 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1898 *c += c2 - 'a' + 10;
1906 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1908 const char *p, *startPos, *endPos = NULL;
1918 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1919 switch ( LDAP_DN_FORMAT( flags ) ) {
1920 case LDAP_DN_FORMAT_LDAPV3:
1921 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1926 case LDAP_DN_FORMAT_LDAP:
1927 case LDAP_DN_FORMAT_LDAPV2:
1928 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1933 case LDAP_DN_FORMAT_DCE:
1934 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1940 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1941 if ( flags & LDAP_DN_PEDANTIC ) {
1946 for ( ; p[ 0 ]; p++ ) {
1947 switch ( LDAP_DN_FORMAT( flags ) ) {
1948 case LDAP_DN_FORMAT_LDAPV3:
1949 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1954 case LDAP_DN_FORMAT_LDAP:
1955 case LDAP_DN_FORMAT_LDAPV2:
1956 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1961 case LDAP_DN_FORMAT_DCE:
1962 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1971 if ( !LDAP_DN_HEXPAIR( p ) ) {
1979 if ( flags & LDAP_DN_SKIP ) {
1983 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1985 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1988 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1989 if ( val->bv_val == NULL ) {
1990 return( LDAP_NO_MEMORY );
1993 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1996 hexstr2bin( &startPos[ s ], &c );
1998 val->bv_val[ d ] = c;
2001 val->bv_val[ d ] = '\0';
2007 * convert a byte in a hexadecimal pair
2010 byte2hexpair( const char *val, char *pair )
2012 static const char hexdig[] = "0123456789ABCDEF";
2018 * we assume the string has enough room for the hex encoding
2022 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2023 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2029 * convert a binary value in hexadecimal pairs
2032 binval2hexstr( struct berval *val, char *str )
2039 if ( val->bv_len == 0 ) {
2044 * we assume the string has enough room for the hex encoding
2048 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2049 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2056 * Length of the string representation, accounting for escaped hex
2060 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2062 ber_len_t l, cl = 1;
2064 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2065 #ifdef PRETTY_ESCAPE
2066 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2067 #endif /* PRETTY_ESCAPE */
2073 if ( val->bv_len == 0 ) {
2077 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2082 if ( p[ 0 ] == '\0' ) {
2088 cl = LDAP_UTF8_CHARLEN2( p, cl );
2090 /* illegal utf-8 char! */
2093 } else if ( cl > 1 ) {
2096 for ( cnt = 1; cnt < cl; cnt++ ) {
2097 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2101 l += escaped_byte_len * cl;
2103 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2104 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2105 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2106 #ifdef PRETTY_ESCAPE
2108 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2110 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2114 * there might be some chars we want
2115 * to escape in form of a couple
2116 * of hexdigits for optimization purposes
2121 l += escaped_ascii_len;
2123 #else /* ! PRETTY_ESCAPE */
2125 #endif /* ! PRETTY_ESCAPE */
2138 * convert to string representation, escaping with hex the UTF-8 stuff;
2139 * assume the destination has enough room for escaping
2142 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2144 ber_len_t s, d, end;
2150 if ( val->bv_len == 0 ) {
2156 * we assume the string has enough room for the hex encoding
2159 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2165 if ( val->bv_val[ s ] == '\0' ) {
2175 * The length was checked in strval2strlen();
2176 * LDAP_UTF8_CHARLEN() should suffice
2178 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2182 * there might be some chars we want to escape in form
2183 * of a couple of hexdigits for optimization purposes
2185 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2186 #ifdef PRETTY_ESCAPE
2188 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2190 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2192 #else /* ! PRETTY_ESCAPE */
2193 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2194 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2195 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2197 #endif /* ! PRETTY_ESCAPE */
2201 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2206 } else if ( cl > 1 ) {
2208 str[ d++ ] = val->bv_val[ s++ ];
2212 #ifdef PRETTY_ESCAPE
2213 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2214 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2215 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2217 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2218 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2224 #endif /* PRETTY_ESCAPE */
2225 str[ d++ ] = val->bv_val[ s++ ];
2235 * Length of the IA5 string representation (no UTF-8 allowed)
2238 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2247 if ( val->bv_len == 0 ) {
2251 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2253 * Turn value into a binary encoded BER
2258 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2259 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2260 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2261 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2276 * convert to string representation (np UTF-8)
2277 * assume the destination has enough room for escaping
2280 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2282 ber_len_t s, d, end;
2288 if ( val->bv_len == 0 ) {
2293 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2295 * Turn value into a binary encoded BER
2302 * we assume the string has enough room for the hex encoding
2306 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2307 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2308 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2309 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2312 str[ d++ ] = val->bv_val[ s++ ];
2322 * Length of the (supposedly) DCE string representation,
2323 * accounting for escaped hex of UTF-8 chars
2326 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2335 if ( val->bv_len == 0 ) {
2339 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2341 * FIXME: Turn the value into a binary encoded BER?
2346 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2347 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2362 * convert to (supposedly) DCE string representation,
2363 * escaping with hex the UTF-8 stuff;
2364 * assume the destination has enough room for escaping
2367 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2375 if ( val->bv_len == 0 ) {
2380 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2382 * FIXME: Turn the value into a binary encoded BER?
2390 * we assume the string has enough room for the hex encoding
2394 for ( s = 0, d = 0; s < val->bv_len; ) {
2395 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2398 str[ d++ ] = val->bv_val[ s++ ];
2408 * Length of the (supposedly) AD canonical string representation,
2409 * accounting for escaped hex of UTF-8 chars
2412 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2421 if ( val->bv_len == 0 ) {
2425 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2427 * FIXME: Turn the value into a binary encoded BER?
2432 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2433 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2448 * convert to (supposedly) AD string representation,
2449 * escaping with hex the UTF-8 stuff;
2450 * assume the destination has enough room for escaping
2453 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2461 if ( val->bv_len == 0 ) {
2466 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2468 * FIXME: Turn the value into a binary encoded BER?
2476 * we assume the string has enough room for the hex encoding
2480 for ( s = 0, d = 0; s < val->bv_len; ) {
2481 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2484 str[ d++ ] = val->bv_val[ s++ ];
2494 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2495 * the first part of the AD representation of the DN is written in DNS
2496 * form, i.e. dot separated domain name components (as suggested
2497 * by Luke Howard, http://www.padl.com/~lukeh)
2500 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2503 int domain = 0, first = 1;
2504 ber_len_t l = 1; /* we move the null also */
2507 /* we are guaranteed there's enough memory in str */
2513 assert( *iRDN >= 0 );
2515 str = bv->bv_val + pos;
2517 for ( i = *iRDN; i >= 0; i-- ) {
2527 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2535 AC_MEMCPY( str, ava->la_value.bv_val,
2536 ava->la_value.bv_len + 1);
2537 l += ava->la_value.bv_len;
2540 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2541 AC_MEMCPY( str, ava->la_value.bv_val,
2542 ava->la_value.bv_len );
2543 str[ ava->la_value.bv_len ] = '.';
2544 l += ava->la_value.bv_len + 1;
2549 bv->bv_len = pos + l - 1;
2555 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2556 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2563 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2564 LDAPAVA *ava = rdn[ iAVA ];
2566 /* len(type) + '=' + '+' | ',' */
2567 l += ava->la_attr.bv_len + 2;
2569 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2570 /* octothorpe + twice the length */
2571 l += 1 + 2 * ava->la_value.bv_len;
2575 unsigned f = flags | ava->la_flags;
2577 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2590 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2591 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2596 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2597 LDAPAVA *ava = rdn[ iAVA ];
2599 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2600 ava->la_attr.bv_len );
2601 l += ava->la_attr.bv_len;
2605 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2607 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2610 l += 2 * ava->la_value.bv_len;
2614 unsigned f = flags | ava->la_flags;
2616 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2621 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2630 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2637 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2638 LDAPAVA *ava = rdn[ iAVA ];
2640 /* len(type) + '=' + ',' | '/' */
2641 l += ava->la_attr.bv_len + 2;
2643 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2644 /* octothorpe + twice the length */
2645 l += 1 + 2 * ava->la_value.bv_len;
2648 unsigned f = flags | ava->la_flags;
2650 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2663 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2668 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2669 LDAPAVA *ava = rdn[ iAVA ];
2674 str[ l++ ] = ( iAVA ? ',' : '/' );
2677 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2678 ava->la_attr.bv_len );
2679 l += ava->la_attr.bv_len;
2683 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2685 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2688 l += 2 * ava->la_value.bv_len;
2691 unsigned f = flags | ava->la_flags;
2693 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2706 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2716 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2717 LDAPAVA *ava = rdn[ iAVA ];
2720 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2722 /* FIXME: are binary values allowed in UFN? */
2723 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2724 /* octothorpe + twice the value */
2725 l += 1 + 2 * ava->la_value.bv_len;
2729 unsigned f = flags | ava->la_flags;
2731 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2744 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2749 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2750 LDAPAVA *ava = rdn[ iAVA ];
2752 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2754 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2757 l += 2 * ava->la_value.bv_len;
2761 unsigned f = flags | ava->la_flags;
2763 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2769 if ( rdn[ iAVA + 1 ] ) {
2770 AC_MEMCPY( &str[ l ], " + ", 3 );
2774 AC_MEMCPY( &str[ l ], ", ", 2 );
2785 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2795 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2796 LDAPAVA *ava = rdn[ iAVA ];
2801 /* FIXME: are binary values allowed in UFN? */
2802 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2803 /* octothorpe + twice the value */
2804 l += 1 + 2 * ava->la_value.bv_len;
2807 unsigned f = flags | ava->la_flags;
2809 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2822 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2827 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2828 LDAPAVA *ava = rdn[ iAVA ];
2833 str[ l++ ] = ( iAVA ? ',' : '/' );
2836 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2838 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2841 l += 2 * ava->la_value.bv_len;
2844 unsigned f = flags | ava->la_flags;
2846 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2861 * Returns in str a string representation of rdn based on flags.
2862 * There is some duplication of code between this and ldap_dn2str;
2863 * this is wanted to reduce the allocation of temporary buffers.
2866 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2873 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2874 return LDAP_PARAM_ERROR;
2877 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2883 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2885 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2889 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2899 if ( rdn == NULL ) {
2900 bv->bv_val = LDAP_STRDUPX( "", ctx );
2901 return( LDAP_SUCCESS );
2905 * This routine wastes "back" bytes at the end of the string
2908 switch ( LDAP_DN_FORMAT( flags ) ) {
2909 case LDAP_DN_FORMAT_LDAPV3:
2910 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2911 return LDAP_DECODING_ERROR;
2915 case LDAP_DN_FORMAT_LDAPV2:
2916 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2917 return LDAP_DECODING_ERROR;
2921 case LDAP_DN_FORMAT_UFN:
2922 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2923 return LDAP_DECODING_ERROR;
2927 case LDAP_DN_FORMAT_DCE:
2928 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2929 return LDAP_DECODING_ERROR;
2933 case LDAP_DN_FORMAT_AD_CANONICAL:
2934 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2935 return LDAP_DECODING_ERROR;
2940 return LDAP_PARAM_ERROR;
2943 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2945 switch ( LDAP_DN_FORMAT( flags ) ) {
2946 case LDAP_DN_FORMAT_LDAPV3:
2947 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2951 case LDAP_DN_FORMAT_LDAPV2:
2952 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2956 case LDAP_DN_FORMAT_UFN:
2957 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2961 case LDAP_DN_FORMAT_DCE:
2962 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2966 case LDAP_DN_FORMAT_AD_CANONICAL:
2967 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2972 /* need at least one of the previous */
2973 return LDAP_PARAM_ERROR;
2977 LDAP_FREEX( bv->bv_val, ctx );
2981 bv->bv_len = l - back;
2982 bv->bv_val[ bv->bv_len ] = '\0';
2984 return LDAP_SUCCESS;
2988 * Very bulk implementation; many optimizations can be performed
2989 * - a NULL dn results in an empty string ""
2992 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2993 * we must encode it in binary form ('#' + HEXPAIRs)
2994 * b) does DCE/AD support UTF-8?
2995 * no clue; don't think so.
2996 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2997 * use binary encoded BER
2999 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
3006 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
3007 return LDAP_PARAM_ERROR;
3010 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
3015 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
3017 return ldap_dn2bv_x( dn, bv, flags, NULL );
3020 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
3023 int rc = LDAP_ENCODING_ERROR;
3026 /* stringifying helpers for LDAPv3/LDAPv2 */
3027 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3028 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3035 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s",
3038 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
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,%u)=%d\n",
3350 bv->bv_val, flags, rc );
3352 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3353 bv->bv_val, flags, 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 */