2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2005 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" */
35 /* parsing/printing routines */
36 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
37 const char **next, unsigned flags, int *retFlags, void *ctx );
38 static int DCE2strval( const char *str, struct berval *val,
39 const char **next, unsigned flags, void *ctx );
40 static int IA52strval( const char *str, struct berval *val,
41 const char **next, unsigned flags, void *ctx );
42 static int quotedIA52strval( const char *str, struct berval *val,
43 const char **next, unsigned flags, void *ctx );
44 static int hexstr2binval( const char *str, struct berval *val,
45 const char **next, unsigned flags, void *ctx );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
68 static LDAPAVA * ldapava_new(
69 const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
71 /* Higher level helpers */
72 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
73 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
74 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
75 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
76 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
77 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
78 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
79 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
80 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
81 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
84 * RFC 1823 ldap_get_dn
87 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
92 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
95 assert( LDAP_VALID(ld) );
96 assert( entry != NULL );
98 tmp = *entry->lm_ber; /* struct copy */
99 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
100 ld->ld_errno = LDAP_DECODING_ERROR;
108 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
111 BerElement tmp, *ber;
113 int rc = LDAP_SUCCESS;
115 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
117 assert( ld != NULL );
118 assert( LDAP_VALID(ld) );
119 assert( entry != NULL );
120 assert( dn != NULL );
127 ber = ldap_alloc_ber_with_options( ld );
129 return LDAP_NO_MEMORY;
136 *ber = *entry->lm_ber; /* struct copy */
137 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
138 rc = ld->ld_errno = LDAP_DECODING_ERROR;
140 if ( rc == LDAP_SUCCESS ) {
141 /* set the length to avoid overrun */
142 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
143 if( rc != LBER_OPT_SUCCESS ) {
144 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
147 if ( rc != LDAP_SUCCESS && berout ) {
155 * RFC 1823 ldap_dn2ufn
158 ldap_dn2ufn( LDAP_CONST char *dn )
162 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
164 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
165 &out, LDAP_DN_FORMAT_UFN );
171 * RFC 1823 ldap_explode_dn
174 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
177 char **values = NULL;
179 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
181 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
183 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
188 if( tmpDN == NULL ) {
189 values = LDAP_MALLOC( sizeof( char * ) );
190 if( values == NULL ) return NULL;
196 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
198 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
199 if ( values == NULL ) {
200 ldap_dnfree( tmpDN );
204 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
205 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
207 ldap_dnfree( tmpDN );
208 values[ iRDN ] = NULL;
214 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
217 char **values = NULL;
221 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
224 * we only parse the first rdn
225 * FIXME: we prefer efficiency over checking if the _ENTIRE_
228 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
233 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
234 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
235 if ( values == NULL ) {
236 ldap_rdnfree( tmpRDN );
240 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
241 ber_len_t l = 0, vl, al = 0;
243 LDAPAVA *ava = tmpRDN[ iAVA ];
245 if ( ava->la_flags & LDAP_AVA_BINARY ) {
246 vl = 1 + 2 * ava->la_value.bv_len;
249 if ( strval2strlen( &ava->la_value,
250 ava->la_flags, &vl ) ) {
256 al = ava->la_attr.bv_len;
257 l = vl + ava->la_attr.bv_len + 1;
259 str = LDAP_MALLOC( l + 1 );
260 AC_MEMCPY( str, ava->la_attr.bv_val,
261 ava->la_attr.bv_len );
266 str = LDAP_MALLOC( l + 1 );
269 if ( ava->la_flags & LDAP_AVA_BINARY ) {
271 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
276 if ( strval2str( &ava->la_value, &str[ al ],
277 ava->la_flags, &vl ) ) {
283 values[ iAVA ] = str;
285 values[ iAVA ] = NULL;
287 ldap_rdnfree( tmpRDN );
292 LBER_VFREE( values );
293 ldap_rdnfree( tmpRDN );
298 ldap_dn2dcedn( LDAP_CONST char *dn )
302 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
304 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
305 &out, LDAP_DN_FORMAT_DCE );
311 ldap_dcedn2dn( LDAP_CONST char *dce )
315 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
317 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
323 ldap_dn2ad_canonical( LDAP_CONST char *dn )
327 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
329 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
330 &out, LDAP_DN_FORMAT_AD_CANONICAL );
336 * function that changes the string representation of dnin
337 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
340 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
341 * plus some rfc 1779)
342 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
343 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
344 * LDAP_DN_FORMAT_DCE (?)
346 * fout can be any of the above except
347 * LDAP_DN_FORMAT_LDAP
349 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
350 * LDAP_DN_FORMAT_AD_CANONICAL (?)
353 ldap_dn_normalize( LDAP_CONST char *dnin,
354 unsigned fin, char **dnout, unsigned fout )
359 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
365 if ( dnin == NULL ) {
366 return( LDAP_SUCCESS );
369 rc = ldap_str2dn( dnin , &tmpDN, fin );
370 if ( rc != LDAP_SUCCESS ) {
374 rc = ldap_dn2str( tmpDN, dnout, fout );
376 ldap_dnfree( tmpDN );
384 /* #define B4ATTRTYPE 0x0001 */
385 #define B4OIDATTRTYPE 0x0002
386 #define B4STRINGATTRTYPE 0x0003
388 #define B4AVAEQUALS 0x0100
389 #define B4AVASEP 0x0200
390 #define B4RDNSEP 0x0300
391 #define GOTAVA 0x0400
393 #define B4ATTRVALUE 0x0010
394 #define B4STRINGVALUE 0x0020
395 #define B4IA5VALUEQUOTED 0x0030
396 #define B4IA5VALUE 0x0040
397 #define B4BINARYVALUE 0x0050
400 * Helpers (mostly from slap.h)
401 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
402 * Macros assume "C" Locale (ASCII)
404 #define LDAP_DN_ASCII_SPACE(c) \
405 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
406 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
407 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
408 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
410 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
411 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
412 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
413 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
414 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
415 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
418 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
419 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
420 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
421 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
422 #define LDAP_DN_ATTRDESC_CHAR(c) \
423 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
425 /* special symbols */
426 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
427 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
428 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
429 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
430 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
431 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
432 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
433 #define LDAP_DN_VALUE_END(c) \
434 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
436 /* NOTE: according to draft-ietf-ldapbis-dn, '=' can be escaped
437 * and treated as special, i.e. escaped both as "\<hexpair>" and
438 * as "\=", but it is treated as a regular char, i.e. it can also
441 * As such, we currently choose to allow reading unescaped '=',
442 * but we always produce escaped '\3D'; this may change in the
443 * future, if compatibility issues do not arise */
445 #define LDAP_DN_NE(c) \
446 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
447 || LDAP_DN_QUOTES(c) \
448 || (c) == '<' || (c) == '>' )
449 #define LDAP_DN_MAYESCAPE(c) \
450 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
451 || LDAP_DN_AVA_EQUALS(c) \
452 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
453 #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
454 #else /* ! LDAP_DEVEL */
455 #define LDAP_DN_NE(c) \
456 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
457 || LDAP_DN_AVA_EQUALS(c) || LDAP_DN_QUOTES(c) \
458 || (c) == '<' || (c) == '>' )
459 #define LDAP_DN_MAYESCAPE(c) \
460 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
461 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
462 #define LDAP_DN_SHOULDESCAPE(c) ( 0 )
463 #endif /* ! LDAP_DEVEL */
465 #define LDAP_DN_NEEDESCAPE(c) \
466 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
467 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
468 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
469 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
470 #define LDAP_DN_WILLESCAPE_CHAR(c) \
471 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
472 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
473 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
474 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
477 #define LDAP_DN_VALUE_END_V2(c) \
478 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
480 #define LDAP_DN_V2_SPECIAL(c) \
481 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
482 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
483 || LDAP_DN_OCTOTHORPE(c) )
484 #define LDAP_DN_V2_PAIR(c) \
485 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
488 * DCE (mostly from Luke Howard and IBM implementation for AIX)
490 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
491 * Here escapes and valid chars for GDS are considered; as soon as more
492 * specific info is found, the macros will be updated.
494 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
495 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
497 * Metachars: '/', ',', '=', '\'.
499 * the '\' is used to escape other metachars.
505 * Attribute types must start with alphabetic chars and can contain
506 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
508 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
509 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
510 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
511 #define LDAP_DN_VALUE_END_DCE(c) \
512 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
513 #define LDAP_DN_NEEDESCAPE_DCE(c) \
514 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
517 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
518 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
519 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
520 #define LDAP_DN_VALUE_END_AD(c) \
521 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
522 #define LDAP_DN_NEEDESCAPE_AD(c) \
523 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
526 #define LDAP_DN_HEXPAIR(s) \
527 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
528 /* better look at the AttributeDescription? */
530 /* FIXME: no composite rdn or non-"dc" types, right?
531 * (what about "dc" in OID form?) */
532 /* FIXME: we do not allow binary values in domain, right? */
533 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
534 /* NOTE: don't use strcasecmp() as it is locale specific! */
535 #define LDAP_DC_ATTR "dc"
536 #define LDAP_DC_ATTRU "DC"
537 #define LDAP_DN_IS_RDN_DC( r ) \
538 ( (r) && (r)[0] && !(r)[1] \
539 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
540 && ((r)[0]->la_attr.bv_len == 2) \
541 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
542 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
543 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
544 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
546 /* Composite rules */
547 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
548 ( LDAP_DN_LDAPV2(f) \
549 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
550 #define LDAP_DN_ALLOW_SPACES(f) \
551 ( LDAP_DN_LDAPV2(f) \
552 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
553 #define LDAP_DN_LDAP(f) \
554 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
555 #define LDAP_DN_LDAPV3(f) \
556 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
557 #define LDAP_DN_LDAPV2(f) \
558 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
559 #define LDAP_DN_DCE(f) \
560 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
561 #define LDAP_DN_UFN(f) \
562 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
563 #define LDAP_DN_ADC(f) \
564 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
565 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
568 * LDAPAVA helpers (will become part of the API for operations
569 * on structural representations of DNs).
572 ldapava_new( const struct berval *attr, const struct berval *val,
573 unsigned flags, void *ctx )
580 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
583 ava->la_attr.bv_len = attr->bv_len;
584 ava->la_attr.bv_val = (char *)(ava+1);
585 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
586 ava->la_attr.bv_val[attr->bv_len] = '\0';
588 ava->la_value = *val;
589 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
591 ava->la_private = NULL;
598 ldapava_free( LDAPAVA *ava, void *ctx )
603 /* ava's private must be freed by caller
604 * (at present let's skip this check because la_private
605 * basically holds static data) */
606 assert( ava->la_private == NULL );
609 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
610 LDAP_FREEX( ava->la_value.bv_val, ctx );
612 LDAP_FREEX( ava, ctx );
616 ldap_rdnfree( LDAPRDN rdn )
618 ldap_rdnfree_x( rdn, NULL );
622 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
630 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
631 ldapava_free( rdn[ iAVA ], ctx );
634 LDAP_FREEX( rdn, ctx );
638 ldap_dnfree( LDAPDN dn )
640 ldap_dnfree_x( dn, NULL );
644 ldap_dnfree_x( LDAPDN dn, void *ctx )
652 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
653 ldap_rdnfree_x( dn[ iRDN ], ctx );
656 LDAP_FREEX( dn, ctx );
660 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
661 * into a structural representation of the DN, by separating attribute
662 * types and values encoded in the more appropriate form, which is
663 * string or OID for attribute types and binary form of the BER encoded
664 * value or Unicode string. Formats different from LDAPv3 are parsed
665 * according to their own rules and turned into the more appropriate
666 * form according to LDAPv3.
668 * NOTE: I realize the code is getting spaghettish; it is rather
669 * experimental and will hopefully turn into something more simple
670 * and readable as soon as it works as expected.
674 * Default sizes of AVA and RDN static working arrays; if required
675 * the are dynamically resized. The values can be tuned in case
676 * of special requirements (e.g. very deep DN trees or high number
679 #define TMP_AVA_SLOTS 8
680 #define TMP_RDN_SLOTS 32
683 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
689 bv.bv_len = strlen( str );
690 bv.bv_val = (char *) str;
692 return ldap_bv2dn_x( &bv, dn, flags, NULL );
696 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
698 return ldap_bv2dn_x( bv, dn, flags, NULL );
702 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
705 int rc = LDAP_DECODING_ERROR;
709 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
710 int num_slots = TMP_RDN_SLOTS;
712 struct berval bvtmp, *bv = &bvtmp;
715 assert( bvin->bv_val );
720 end = str + bv->bv_len;
722 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
726 switch ( LDAP_DN_FORMAT( flags ) ) {
727 case LDAP_DN_FORMAT_LDAP:
728 case LDAP_DN_FORMAT_LDAPV3:
729 case LDAP_DN_FORMAT_DCE:
732 /* allow DN enclosed in brackets */
733 case LDAP_DN_FORMAT_LDAPV2:
734 if ( str[0] == '<' ) {
735 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
736 rc = LDAP_DECODING_ERROR;
746 /* unsupported in str2dn */
747 case LDAP_DN_FORMAT_UFN:
748 case LDAP_DN_FORMAT_AD_CANONICAL:
749 return LDAP_PARAM_ERROR;
751 case LDAP_DN_FORMAT_LBER:
753 return LDAP_PARAM_ERROR;
756 if ( bv->bv_len == 0 ) {
760 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
761 /* value must have embedded NULs */
762 return LDAP_DECODING_ERROR;
766 if ( LDAP_DN_DCE( flags ) ) {
769 * (from Luke Howard: thnx) A RDN separator is required
770 * at the beginning of an (absolute) DN.
772 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
778 * actually we do not want to accept by default the DCE form,
779 * we do not want to auto-detect it
782 } else if ( LDAP_DN_LDAP( flags ) ) {
784 * if dn starts with '/' let's make it a DCE dn
786 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
787 flags |= LDAP_DN_FORMAT_DCE;
793 for ( ; p < end; p++ ) {
796 tmpbv.bv_len = bv->bv_len - ( p - str );
797 tmpbv.bv_val = (char *)p;
799 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
800 if ( err != LDAP_SUCCESS ) {
805 * We expect a rdn separator
807 if ( p < end && p[ 0 ] ) {
808 switch ( LDAP_DN_FORMAT( flags ) ) {
809 case LDAP_DN_FORMAT_LDAPV3:
810 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
811 rc = LDAP_DECODING_ERROR;
816 case LDAP_DN_FORMAT_LDAP:
817 case LDAP_DN_FORMAT_LDAPV2:
818 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
819 rc = LDAP_DECODING_ERROR;
824 case LDAP_DN_FORMAT_DCE:
825 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
826 rc = LDAP_DECODING_ERROR;
834 tmpDN[nrdns++] = newRDN;
838 * make the static RDN array dynamically rescalable
840 if ( nrdns == num_slots ) {
843 if ( tmpDN == tmpDN_ ) {
844 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
849 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
852 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
863 if ( p >= end || p[ 0 ] == '\0' ) {
865 * the DN is over, phew
867 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
868 if ( newDN == NULL ) {
874 if ( LDAP_DN_DCE( flags ) ) {
875 /* add in reversed order */
876 for ( i=0; i<nrdns; i++ )
877 newDN[i] = tmpDN[nrdns-1-i];
879 for ( i=0; i<nrdns; i++ )
891 ldap_rdnfree_x( newRDN, ctx );
894 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
895 ldap_rdnfree_x( tmpDN[nrdns], ctx );
900 if ( tmpDN != tmpDN_ ) {
901 LDAP_FREEX( tmpDN, ctx );
904 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
905 ldap_err2string( rc ) );
914 * Parses a relative DN according to flags up to a rdn separator
915 * or to the end of str.
916 * Returns the rdn and a pointer to the string continuation, which
917 * corresponds to the rdn separator or to '\0' in case the string is over.
920 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
921 char **n_in, unsigned flags )
926 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
928 bv.bv_len = strlen( str );
929 bv.bv_val = (char *) str;
931 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
935 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
936 char **n_in, unsigned flags )
938 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
942 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
943 char **n_in, unsigned flags, void *ctx )
945 const char **n = (const char **) n_in;
949 int rc = LDAP_DECODING_ERROR;
950 int attrTypeEncoding = LDAP_AVA_STRING,
951 attrValueEncoding = LDAP_AVA_STRING;
953 struct berval attrType = BER_BVNULL;
954 struct berval attrValue = BER_BVNULL;
956 LDAPRDN newRDN = NULL;
957 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
958 int num_slots = TMP_AVA_SLOTS;
964 assert( bv->bv_len );
965 assert( bv->bv_val );
966 assert( rdn || flags & LDAP_DN_SKIP );
970 stoplen = bv->bv_len;
977 switch ( LDAP_DN_FORMAT( flags ) ) {
978 case LDAP_DN_FORMAT_LDAP:
979 case LDAP_DN_FORMAT_LDAPV3:
980 case LDAP_DN_FORMAT_LDAPV2:
981 case LDAP_DN_FORMAT_DCE:
984 /* unsupported in str2dn */
985 case LDAP_DN_FORMAT_UFN:
986 case LDAP_DN_FORMAT_AD_CANONICAL:
987 return LDAP_PARAM_ERROR;
989 case LDAP_DN_FORMAT_LBER:
991 return LDAP_PARAM_ERROR;
994 if ( bv->bv_len == 0 ) {
999 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1000 /* value must have embedded NULs */
1001 return LDAP_DECODING_ERROR;
1005 for ( ; p[ 0 ] || state == GOTAVA; ) {
1008 * The parser in principle advances one token a time,
1009 * or toggles state if preferable.
1014 * an AttributeType can be encoded as:
1015 * - its string representation; in detail, implementations
1016 * MUST recognize AttributeType string type names listed
1017 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1018 * MAY recognize other names.
1019 * - its numeric OID (a dotted decimal string); in detail
1020 * RFC 2253 asserts that ``Implementations MUST allow
1021 * an oid in the attribute type to be prefixed by one
1022 * of the character strings "oid." or "OID."''. As soon
1023 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1024 * I'm not sure whether this is required or not any
1025 * longer; to be liberal, we still implement it.
1028 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1029 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1036 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1037 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1042 /* whitespace is allowed (and trimmed) */
1044 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1049 /* error: we expected an AVA */
1055 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1056 state = B4OIDATTRTYPE;
1060 /* else must be alpha */
1061 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1065 /* LDAPv2 "oid." prefix */
1066 if ( LDAP_DN_LDAPV2( flags ) ) {
1068 * to be overly pedantic, we only accept
1071 if ( flags & LDAP_DN_PEDANTIC ) {
1072 if ( !strncmp( p, "OID.", 4 )
1073 || !strncmp( p, "oid.", 4 ) ) {
1075 state = B4OIDATTRTYPE;
1079 if ( !strncasecmp( p, "oid.", 4 ) ) {
1081 state = B4OIDATTRTYPE;
1087 state = B4STRINGATTRTYPE;
1090 case B4OIDATTRTYPE: {
1091 int err = LDAP_SUCCESS;
1093 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1096 if ( err != LDAP_SUCCESS ) {
1099 attrType.bv_len = p - attrType.bv_val;
1101 attrTypeEncoding = LDAP_AVA_BINARY;
1103 state = B4AVAEQUALS;
1107 case B4STRINGATTRTYPE: {
1108 const char *startPos, *endPos = NULL;
1112 * the starting char has been found to be
1113 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1114 * FIXME: DCE attr types seem to have a more
1115 * restrictive syntax (no '-' ...)
1117 for ( startPos = p++; p[ 0 ]; p++ ) {
1118 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1122 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1125 * RFC 2253 does not explicitly
1126 * allow lang extensions to attribute
1129 if ( flags & LDAP_DN_PEDANTIC ) {
1134 * we trim ';' and following lang
1135 * and so from attribute types
1138 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1139 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1147 len = ( endPos ? endPos : p ) - startPos;
1152 attrTypeEncoding = LDAP_AVA_STRING;
1155 * here we need to decide whether to use it as is
1156 * or turn it in OID form; as a consequence, we
1157 * need to decide whether to binary encode the value
1160 state = B4AVAEQUALS;
1162 if ( flags & LDAP_DN_SKIP ) {
1166 attrType.bv_val = (char *)startPos;
1167 attrType.bv_len = len;
1173 /* spaces may not be allowed */
1174 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1175 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1180 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1185 /* need equal sign */
1186 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1191 /* spaces may not be allowed */
1192 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1193 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1198 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1204 * octothorpe means a BER encoded value will follow
1205 * FIXME: I don't think DCE will allow it
1207 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1209 attrValueEncoding = LDAP_AVA_BINARY;
1210 state = B4BINARYVALUE;
1214 /* STRING value expected */
1217 * if we're pedantic, an attribute type in OID form
1218 * SHOULD imply a BER encoded attribute value; we
1219 * should at least issue a warning
1221 if ( ( flags & LDAP_DN_PEDANTIC )
1222 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1223 /* OID attrType SHOULD use binary encoding */
1227 attrValueEncoding = LDAP_AVA_STRING;
1230 * LDAPv2 allows the attribute value to be quoted;
1231 * also, IA5 values are expected, in principle
1233 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1234 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1236 state = B4IA5VALUEQUOTED;
1240 if ( LDAP_DN_LDAPV2( flags ) ) {
1247 * here STRING means RFC 2253 string
1248 * FIXME: what about DCE strings?
1254 state = B4STRINGVALUE;
1259 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1267 switch ( LDAP_DN_FORMAT( flags ) ) {
1268 case LDAP_DN_FORMAT_LDAP:
1269 case LDAP_DN_FORMAT_LDAPV3:
1270 if ( str2strval( p, stoplen - ( p - str ),
1271 &attrValue, &p, flags,
1272 &attrValueEncoding, ctx ) ) {
1277 case LDAP_DN_FORMAT_DCE:
1278 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1291 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1298 case B4IA5VALUEQUOTED:
1300 /* lead quote already stripped */
1301 if ( quotedIA52strval( p, &attrValue,
1302 &p, flags, ctx ) ) {
1312 if ( !( flags & LDAP_DN_SKIP ) ) {
1316 * we accept empty values
1318 ava = ldapava_new( &attrType, &attrValue,
1319 attrValueEncoding, ctx );
1320 if ( ava == NULL ) {
1321 rc = LDAP_NO_MEMORY;
1324 tmpRDN[navas++] = ava;
1326 attrValue.bv_val = NULL;
1327 attrValue.bv_len = 0;
1330 * prepare room for new AVAs if needed
1332 if (navas == num_slots) {
1335 if ( tmpRDN == tmpRDN_ ) {
1336 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1337 if ( tmp == NULL ) {
1338 rc = LDAP_NO_MEMORY;
1341 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1344 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1345 if ( tmp == NULL ) {
1346 rc = LDAP_NO_MEMORY;
1357 * if we got an AVA separator ('+', or ',' for DCE )
1358 * we expect a new AVA for this RDN; otherwise
1359 * we add the RDN to the DN
1361 switch ( LDAP_DN_FORMAT( flags ) ) {
1362 case LDAP_DN_FORMAT_LDAP:
1363 case LDAP_DN_FORMAT_LDAPV3:
1364 case LDAP_DN_FORMAT_LDAPV2:
1365 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1370 case LDAP_DN_FORMAT_DCE:
1371 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1379 * the RDN is over, phew
1382 if ( !( flags & LDAP_DN_SKIP ) ) {
1383 newRDN = (LDAPRDN)LDAP_MALLOCX(
1384 sizeof(LDAPAVA) * (navas+1), ctx );
1385 if ( newRDN == NULL ) {
1386 rc = LDAP_NO_MEMORY;
1389 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1390 newRDN[navas] = NULL;
1398 /* they should have been used in an AVA */
1399 attrType.bv_val = NULL;
1400 attrValue.bv_val = NULL;
1415 /* They are set to NULL after they're used in an AVA */
1417 if ( attrValue.bv_val ) {
1418 LDAP_FREEX( attrValue.bv_val, ctx );
1421 for ( navas-- ; navas >= 0; navas-- ) {
1422 ldapava_free( tmpRDN[navas], ctx );
1427 if ( tmpRDN != tmpRDN_ ) {
1428 LDAP_FREEX( tmpRDN, ctx );
1439 * reads in a UTF-8 string value, unescaping stuff:
1440 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1441 * '\' + HEXPAIR(p) -> unhex(p)
1444 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1446 const char *p, *end, *startPos, *endPos = NULL;
1447 ber_len_t len, escapes;
1454 end = str + stoplen;
1455 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1456 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1458 if ( p[ 0 ] == '\0' ) {
1461 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1466 if ( LDAP_DN_HEXPAIR( p ) ) {
1469 hexstr2bin( p, &c );
1472 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1475 * we assume the string is UTF-8
1477 *retFlags = LDAP_AVA_NONPRINTABLE;
1484 if ( LDAP_DN_PEDANTIC & flags ) {
1488 * we do not allow escaping
1489 * of chars that don't need
1490 * to and do not belong to
1495 } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1496 if ( p[ 0 ] == '\0' ) {
1499 *retFlags = LDAP_AVA_NONPRINTABLE;
1501 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1502 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1505 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1507 * FIXME: maybe we can add
1508 * escapes if not pedantic?
1515 * we do allow unescaped spaces at the end
1516 * of the value only in non-pedantic mode
1518 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1519 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1520 if ( flags & LDAP_DN_PEDANTIC ) {
1524 /* strip trailing (unescaped) spaces */
1525 for ( endPos = p - 1;
1526 endPos > startPos + 1 &&
1527 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1528 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1535 if ( flags & LDAP_DN_SKIP ) {
1540 * FIXME: test memory?
1542 len = ( endPos ? endPos : p ) - startPos - escapes;
1545 if ( escapes == 0 ) {
1546 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1547 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1548 AC_MEMCPY( val->bv_val, startPos, len );
1549 val->bv_val[ len ] = '\0';
1551 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1557 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1558 for ( s = 0, d = 0; d < len; ) {
1559 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1561 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1562 val->bv_val[ d++ ] =
1565 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1568 hexstr2bin( &startPos[ s ], &c );
1569 val->bv_val[ d++ ] = c;
1573 /* we should never get here */
1578 val->bv_val[ d++ ] = startPos[ s++ ];
1582 val->bv_val[ d ] = '\0';
1590 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1592 const char *p, *startPos, *endPos = NULL;
1593 ber_len_t len, escapes;
1601 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1602 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1604 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1611 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1616 * FIXME: can we accept anything else? I guess we need
1617 * to stop if a value is not legal
1622 * (unescaped) trailing spaces are trimmed must be silently ignored;
1625 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1626 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1627 if ( flags & LDAP_DN_PEDANTIC ) {
1631 /* strip trailing (unescaped) spaces */
1632 for ( endPos = p - 1;
1633 endPos > startPos + 1 &&
1634 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1635 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1642 if ( flags & LDAP_DN_SKIP ) {
1646 len = ( endPos ? endPos : p ) - startPos - escapes;
1648 if ( escapes == 0 ){
1649 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1654 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1655 for ( s = 0, d = 0; d < len; ) {
1657 * This point is reached only if escapes
1658 * are properly used, so all we need to
1661 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1665 val->bv_val[ d++ ] = startPos[ s++ ];
1667 val->bv_val[ d ] = '\0';
1668 assert( strlen( val->bv_val ) == len );
1675 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1677 const char *p, *startPos, *endPos = NULL;
1678 ber_len_t len, escapes;
1690 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1691 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1693 if ( p[ 0 ] == '\0' ) {
1697 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1698 && ( LDAP_DN_PEDANTIC & flags ) ) {
1703 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1708 * FIXME: can we accept anything else? I guess we need
1709 * to stop if a value is not legal
1713 /* strip trailing (unescaped) spaces */
1715 endPos > startPos + 1 &&
1716 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1717 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1723 if ( flags & LDAP_DN_SKIP ) {
1727 len = ( endPos ? endPos : p ) - startPos - escapes;
1729 if ( escapes == 0 ) {
1730 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1735 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1736 for ( s = 0, d = 0; d < len; ) {
1737 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1740 val->bv_val[ d++ ] = startPos[ s++ ];
1742 val->bv_val[ d ] = '\0';
1743 assert( strlen( val->bv_val ) == len );
1750 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1752 const char *p, *startPos, *endPos = NULL;
1754 unsigned escapes = 0;
1762 /* initial quote already eaten */
1763 for ( startPos = p = str; p[ 0 ]; p++ ) {
1765 * According to RFC 1779, the quoted value can
1766 * contain escaped as well as unescaped special values;
1767 * as a consequence we tolerate escaped values
1768 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1769 * (e.g. '","' -> '\,').
1771 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1772 if ( p[ 1 ] == '\0' ) {
1777 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1778 && ( LDAP_DN_PEDANTIC & flags ) ) {
1780 * do we allow to escape normal chars?
1781 * LDAPv2 does not allow any mechanism
1782 * for escaping chars with '\' and hex
1789 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1791 /* eat closing quotes */
1797 * FIXME: can we accept anything else? I guess we need
1798 * to stop if a value is not legal
1802 if ( endPos == NULL ) {
1806 /* Strip trailing (unescaped) spaces */
1807 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1812 if ( flags & LDAP_DN_SKIP ) {
1816 len = endPos - startPos - escapes;
1817 assert( endPos >= startPos + escapes );
1819 if ( escapes == 0 ) {
1820 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1825 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1828 for ( s = d = 0; d < len; ) {
1829 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1832 val->bv_val[ d++ ] = str[ s++ ];
1834 val->bv_val[ d ] = '\0';
1835 assert( strlen( val->bv_val ) == len );
1842 hexstr2bin( const char *str, char *c )
1852 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1856 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1859 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1866 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1870 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1871 *c += c2 - 'A' + 10;
1873 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1874 *c += c2 - 'a' + 10;
1882 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1884 const char *p, *startPos, *endPos = NULL;
1894 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1895 switch ( LDAP_DN_FORMAT( flags ) ) {
1896 case LDAP_DN_FORMAT_LDAPV3:
1897 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1902 case LDAP_DN_FORMAT_LDAP:
1903 case LDAP_DN_FORMAT_LDAPV2:
1904 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1909 case LDAP_DN_FORMAT_DCE:
1910 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1916 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1917 if ( flags & LDAP_DN_PEDANTIC ) {
1922 for ( ; p[ 0 ]; p++ ) {
1923 switch ( LDAP_DN_FORMAT( flags ) ) {
1924 case LDAP_DN_FORMAT_LDAPV3:
1925 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1930 case LDAP_DN_FORMAT_LDAP:
1931 case LDAP_DN_FORMAT_LDAPV2:
1932 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1937 case LDAP_DN_FORMAT_DCE:
1938 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1947 if ( !LDAP_DN_HEXPAIR( p ) ) {
1955 if ( flags & LDAP_DN_SKIP ) {
1959 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1961 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1964 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1965 if ( val->bv_val == NULL ) {
1966 return( LDAP_NO_MEMORY );
1969 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1972 hexstr2bin( &startPos[ s ], &c );
1974 val->bv_val[ d ] = c;
1977 val->bv_val[ d ] = '\0';
1983 * convert a byte in a hexadecimal pair
1986 byte2hexpair( const char *val, char *pair )
1988 static const char hexdig[] = "0123456789ABCDEF";
1994 * we assume the string has enough room for the hex encoding
1998 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1999 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2005 * convert a binary value in hexadecimal pairs
2008 binval2hexstr( struct berval *val, char *str )
2015 if ( val->bv_len == 0 ) {
2020 * we assume the string has enough room for the hex encoding
2024 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2025 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2032 * Length of the string representation, accounting for escaped hex
2036 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2038 ber_len_t l, cl = 1;
2040 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2041 #ifdef PRETTY_ESCAPE
2042 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2043 #endif /* PRETTY_ESCAPE */
2049 if ( val->bv_len == 0 ) {
2053 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2058 if ( p[ 0 ] == '\0' ) {
2064 cl = LDAP_UTF8_CHARLEN2( p, cl );
2066 /* illegal utf-8 char! */
2069 } else if ( cl > 1 ) {
2072 for ( cnt = 1; cnt < cl; cnt++ ) {
2073 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2077 l += escaped_byte_len * cl;
2079 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2080 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2081 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2082 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2083 #ifdef PRETTY_ESCAPE
2085 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2087 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2091 * there might be some chars we want
2092 * to escape in form of a couple
2093 * of hexdigits for optimization purposes
2098 l += escaped_ascii_len;
2100 #else /* ! PRETTY_ESCAPE */
2102 #endif /* ! PRETTY_ESCAPE */
2115 * convert to string representation, escaping with hex the UTF-8 stuff;
2116 * assume the destination has enough room for escaping
2119 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2121 ber_len_t s, d, end;
2127 if ( val->bv_len == 0 ) {
2133 * we assume the string has enough room for the hex encoding
2136 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2142 if ( val->bv_val[ s ] == '\0' ) {
2152 * The length was checked in strval2strlen();
2153 * LDAP_UTF8_CHARLEN() should suffice
2155 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2159 * there might be some chars we want to escape in form
2160 * of a couple of hexdigits for optimization purposes
2162 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2163 #ifdef PRETTY_ESCAPE
2165 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2167 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2169 #else /* ! PRETTY_ESCAPE */
2170 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2171 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2172 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2173 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2175 #endif /* ! PRETTY_ESCAPE */
2179 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2184 } else if ( cl > 1 ) {
2186 str[ d++ ] = val->bv_val[ s++ ];
2190 #ifdef PRETTY_ESCAPE
2191 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2192 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2193 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2194 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2196 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2197 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2203 #endif /* PRETTY_ESCAPE */
2204 str[ d++ ] = val->bv_val[ s++ ];
2214 * Length of the IA5 string representation (no UTF-8 allowed)
2217 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2226 if ( val->bv_len == 0 ) {
2230 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2232 * Turn value into a binary encoded BER
2237 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2238 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2239 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2240 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2241 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2256 * convert to string representation (np UTF-8)
2257 * assume the destination has enough room for escaping
2260 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2262 ber_len_t s, d, end;
2268 if ( val->bv_len == 0 ) {
2273 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2275 * Turn value into a binary encoded BER
2282 * we assume the string has enough room for the hex encoding
2286 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2287 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2288 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2289 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2290 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2293 str[ d++ ] = val->bv_val[ s++ ];
2303 * Length of the (supposedly) DCE string representation,
2304 * accounting for escaped hex of UTF-8 chars
2307 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2316 if ( val->bv_len == 0 ) {
2320 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2322 * FIXME: Turn the value into a binary encoded BER?
2327 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2328 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2343 * convert to (supposedly) DCE string representation,
2344 * escaping with hex the UTF-8 stuff;
2345 * assume the destination has enough room for escaping
2348 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2356 if ( val->bv_len == 0 ) {
2361 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2363 * FIXME: Turn the value into a binary encoded BER?
2371 * we assume the string has enough room for the hex encoding
2375 for ( s = 0, d = 0; s < val->bv_len; ) {
2376 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2379 str[ d++ ] = val->bv_val[ s++ ];
2389 * Length of the (supposedly) AD canonical string representation,
2390 * accounting for escaped hex of UTF-8 chars
2393 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2402 if ( val->bv_len == 0 ) {
2406 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2408 * FIXME: Turn the value into a binary encoded BER?
2413 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2414 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2429 * convert to (supposedly) AD string representation,
2430 * escaping with hex the UTF-8 stuff;
2431 * assume the destination has enough room for escaping
2434 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2442 if ( val->bv_len == 0 ) {
2447 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2449 * FIXME: Turn the value into a binary encoded BER?
2457 * we assume the string has enough room for the hex encoding
2461 for ( s = 0, d = 0; s < val->bv_len; ) {
2462 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2465 str[ d++ ] = val->bv_val[ s++ ];
2475 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2476 * the first part of the AD representation of the DN is written in DNS
2477 * form, i.e. dot separated domain name components (as suggested
2478 * by Luke Howard, http://www.padl.com/~lukeh)
2481 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2484 int domain = 0, first = 1;
2485 ber_len_t l = 1; /* we move the null also */
2488 /* we are guaranteed there's enough memory in str */
2494 assert( *iRDN >= 0 );
2496 str = bv->bv_val + pos;
2498 for ( i = *iRDN; i >= 0; i-- ) {
2508 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2516 AC_MEMCPY( str, ava->la_value.bv_val,
2517 ava->la_value.bv_len + 1);
2518 l += ava->la_value.bv_len;
2521 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2522 AC_MEMCPY( str, ava->la_value.bv_val,
2523 ava->la_value.bv_len );
2524 str[ ava->la_value.bv_len ] = '.';
2525 l += ava->la_value.bv_len + 1;
2530 bv->bv_len = pos + l - 1;
2536 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2537 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2544 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2545 LDAPAVA *ava = rdn[ iAVA ];
2547 /* len(type) + '=' + '+' | ',' */
2548 l += ava->la_attr.bv_len + 2;
2550 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2551 /* octothorpe + twice the length */
2552 l += 1 + 2 * ava->la_value.bv_len;
2556 unsigned f = flags | ava->la_flags;
2558 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2571 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2572 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2577 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2578 LDAPAVA *ava = rdn[ iAVA ];
2580 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2581 ava->la_attr.bv_len );
2582 l += ava->la_attr.bv_len;
2586 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2588 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2591 l += 2 * ava->la_value.bv_len;
2595 unsigned f = flags | ava->la_flags;
2597 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2602 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2611 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2618 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2619 LDAPAVA *ava = rdn[ iAVA ];
2621 /* len(type) + '=' + ',' | '/' */
2622 l += ava->la_attr.bv_len + 2;
2624 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2625 /* octothorpe + twice the length */
2626 l += 1 + 2 * ava->la_value.bv_len;
2629 unsigned f = flags | ava->la_flags;
2631 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2644 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2649 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2650 LDAPAVA *ava = rdn[ iAVA ];
2655 str[ l++ ] = ( iAVA ? ',' : '/' );
2658 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2659 ava->la_attr.bv_len );
2660 l += ava->la_attr.bv_len;
2664 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2666 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2669 l += 2 * ava->la_value.bv_len;
2672 unsigned f = flags | ava->la_flags;
2674 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2687 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2697 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2698 LDAPAVA *ava = rdn[ iAVA ];
2701 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2703 /* FIXME: are binary values allowed in UFN? */
2704 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2705 /* octothorpe + twice the value */
2706 l += 1 + 2 * ava->la_value.bv_len;
2710 unsigned f = flags | ava->la_flags;
2712 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2725 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2730 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2731 LDAPAVA *ava = rdn[ iAVA ];
2733 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2735 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2738 l += 2 * ava->la_value.bv_len;
2742 unsigned f = flags | ava->la_flags;
2744 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2750 if ( rdn[ iAVA + 1 ] ) {
2751 AC_MEMCPY( &str[ l ], " + ", 3 );
2755 AC_MEMCPY( &str[ l ], ", ", 2 );
2766 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2776 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2777 LDAPAVA *ava = rdn[ iAVA ];
2782 /* FIXME: are binary values allowed in UFN? */
2783 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2784 /* octothorpe + twice the value */
2785 l += 1 + 2 * ava->la_value.bv_len;
2788 unsigned f = flags | ava->la_flags;
2790 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2803 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2808 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2809 LDAPAVA *ava = rdn[ iAVA ];
2814 str[ l++ ] = ( iAVA ? ',' : '/' );
2817 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2819 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2822 l += 2 * ava->la_value.bv_len;
2825 unsigned f = flags | ava->la_flags;
2827 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2842 * Returns in str a string representation of rdn based on flags.
2843 * There is some duplication of code between this and ldap_dn2str;
2844 * this is wanted to reduce the allocation of temporary buffers.
2847 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2854 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2855 return LDAP_PARAM_ERROR;
2858 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2864 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2866 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2870 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2880 if ( rdn == NULL ) {
2881 bv->bv_val = LDAP_STRDUPX( "", ctx );
2882 return( LDAP_SUCCESS );
2886 * This routine wastes "back" bytes at the end of the string
2889 switch ( LDAP_DN_FORMAT( flags ) ) {
2890 case LDAP_DN_FORMAT_LDAPV3:
2891 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2892 return LDAP_DECODING_ERROR;
2896 case LDAP_DN_FORMAT_LDAPV2:
2897 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2898 return LDAP_DECODING_ERROR;
2902 case LDAP_DN_FORMAT_UFN:
2903 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2904 return LDAP_DECODING_ERROR;
2908 case LDAP_DN_FORMAT_DCE:
2909 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2910 return LDAP_DECODING_ERROR;
2914 case LDAP_DN_FORMAT_AD_CANONICAL:
2915 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2916 return LDAP_DECODING_ERROR;
2921 return LDAP_PARAM_ERROR;
2924 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2926 switch ( LDAP_DN_FORMAT( flags ) ) {
2927 case LDAP_DN_FORMAT_LDAPV3:
2928 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2932 case LDAP_DN_FORMAT_LDAPV2:
2933 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2937 case LDAP_DN_FORMAT_UFN:
2938 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2942 case LDAP_DN_FORMAT_DCE:
2943 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2947 case LDAP_DN_FORMAT_AD_CANONICAL:
2948 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2953 /* need at least one of the previous */
2954 return LDAP_PARAM_ERROR;
2958 LDAP_FREEX( bv->bv_val, ctx );
2962 bv->bv_len = l - back;
2963 bv->bv_val[ bv->bv_len ] = '\0';
2965 return LDAP_SUCCESS;
2969 * Very bulk implementation; many optimizations can be performed
2970 * - a NULL dn results in an empty string ""
2973 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2974 * we must encode it in binary form ('#' + HEXPAIRs)
2975 * b) does DCE/AD support UTF-8?
2976 * no clue; don't think so.
2977 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2978 * use binary encoded BER
2980 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
2987 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2988 return LDAP_PARAM_ERROR;
2991 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
2996 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
2998 return ldap_dn2bv_x( dn, bv, flags, NULL );
3001 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
3004 int rc = LDAP_ENCODING_ERROR;
3007 /* stringifying helpers for LDAPv3/LDAPv2 */
3008 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3009 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3015 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3018 * a null dn means an empty dn string
3019 * FIXME: better raise an error?
3022 bv->bv_val = LDAP_STRDUPX( "", ctx );
3023 return( LDAP_SUCCESS );
3026 switch ( LDAP_DN_FORMAT( flags ) ) {
3027 case LDAP_DN_FORMAT_LDAPV3:
3028 sv2l = strval2strlen;
3032 case LDAP_DN_FORMAT_LDAPV2:
3033 sv2l = strval2IA5strlen;
3034 sv2s = strval2IA5str;
3037 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3039 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3040 goto return_results;
3046 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3047 rc = LDAP_NO_MEMORY;
3051 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3054 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3056 LDAP_FREEX( bv->bv_val, ctx );
3058 goto return_results;
3066 * trim the last ',' (the allocated memory
3067 * is one byte longer than required)
3069 bv->bv_len = len - 1;
3070 bv->bv_val[ bv->bv_len ] = '\0';
3075 case LDAP_DN_FORMAT_UFN: {
3077 * FIXME: quoting from RFC 1781:
3079 To take a distinguished name, and generate a name of this format with
3080 attribute types omitted, the following steps are followed.
3082 1. If the first attribute is of type CommonName, the type may be
3085 2. If the last attribute is of type Country, the type may be
3088 3. If the last attribute is of type Country, the last
3089 Organisation attribute may have the type omitted.
3091 4. All attributes of type OrganisationalUnit may have the type
3092 omitted, unless they are after an Organisation attribute or
3093 the first attribute is of type OrganisationalUnit.
3095 * this should be the pedantic implementation.
3097 * Here the standard implementation reflects
3098 * the one historically provided by OpenLDAP
3099 * (and UMIch, I presume), with the variant
3100 * of spaces and plusses (' + ') separating
3103 * A non-standard but nice implementation could
3104 * be to turn the final "dc" attributes into a
3105 * dot-separated domain.
3107 * Other improvements could involve the use of
3108 * friendly country names and so.
3111 int leftmost_dc = -1;
3113 #endif /* DC_IN_UFN */
3115 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3118 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3119 goto return_results;
3124 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3125 if ( leftmost_dc == -1 ) {
3131 #endif /* DC_IN_UFN */
3134 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3135 rc = LDAP_NO_MEMORY;
3140 if ( leftmost_dc == -1 ) {
3141 #endif /* DC_IN_UFN */
3142 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3145 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3147 LDAP_FREEX( bv->bv_val, ctx );
3149 goto return_results;
3155 * trim the last ', ' (the allocated memory
3156 * is two bytes longer than required)
3158 bv->bv_len = len - 2;
3159 bv->bv_val[ bv->bv_len ] = '\0';
3162 last_iRDN = iRDN - 1;
3164 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3167 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3169 LDAP_FREEX( bv->bv_val, ctx );
3171 goto return_results;
3176 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3177 LDAP_FREEX( bv->bv_val, ctx );
3179 goto return_results;
3182 /* the string is correctly terminated by dn2domain */
3184 #endif /* DC_IN_UFN */
3190 case LDAP_DN_FORMAT_DCE:
3191 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3193 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3194 goto return_results;
3200 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3201 rc = LDAP_NO_MEMORY;
3205 for ( l = 0; iRDN--; ) {
3208 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3210 LDAP_FREEX( bv->bv_val, ctx );
3212 goto return_results;
3220 bv->bv_val[ bv->bv_len ] = '\0';
3225 case LDAP_DN_FORMAT_AD_CANONICAL: {
3226 int trailing_slash = 1;
3229 * Sort of UFN for DCE DNs: a slash ('/') separated
3230 * global->local DN with no types; strictly speaking,
3231 * the naming context should be a domain, which is
3232 * written in DNS-style, e.g. dot-deparated.
3236 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3240 * "microsoft.com/People/Bill,Gates"
3242 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3245 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3246 goto return_results;
3252 /* reserve room for trailing '/' in case the DN
3253 * is exactly a domain */
3254 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3256 rc = LDAP_NO_MEMORY;
3261 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3262 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3267 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3268 flags, &rdnl, 0 ) ) {
3269 LDAP_FREEX( bv->bv_val, ctx );
3271 goto return_results;
3280 * Strictly speaking, AD canonical requires
3281 * a DN to be in the form "..., dc=smtg",
3282 * i.e. terminated by a domain component
3284 if ( flags & LDAP_DN_PEDANTIC ) {
3285 LDAP_FREEX( bv->bv_val, ctx );
3287 rc = LDAP_ENCODING_ERROR;
3291 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3294 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3295 flags, &rdnl, first ) ) {
3296 LDAP_FREEX( bv->bv_val, ctx );
3298 goto return_results;
3307 if ( trailing_slash ) {
3308 /* the DN is exactly a domain -- need a trailing
3309 * slash; room was reserved in advance */
3310 bv->bv_val[ len ] = '/';
3315 bv->bv_val[ bv->bv_len ] = '\0';
3321 return LDAP_PARAM_ERROR;
3324 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s)=%d %s\n",
3325 bv->bv_val, rc, ldap_err2string( rc ) );
3332 #include <openssl/x509.h>
3333 #include <openssl/err.h>
3335 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3336 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3337 * constructed DN will use numeric OIDs to identify attributeTypes,
3338 * and the func() will be invoked to rewrite the DN with the given
3341 * Otherwise the DN will use shortNames as defined in the OpenSSL
3344 * It's preferable to let slapd do the OID to attributeType mapping,
3345 * because the OpenSSL tables are known to have many typos in versions
3346 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3347 * so we're forced to use OpenSSL's mapping there.
3348 * -- Howard Chu 2002-04-18
3352 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3357 LDAPAVA *newAVA, *baseAVA;
3358 X509_NAME_ENTRY *ne;
3361 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3363 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3365 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3374 /* Get the number of AVAs. This is not necessarily the same as
3375 * the number of RDNs.
3377 navas = X509_NAME_entry_count( x509_name );
3379 /* Get the last element, to see how many RDNs there are */
3380 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3381 nrdns = ne->set + 1;
3383 /* Allocate the DN/RDN/AVA stuff as a single block */
3384 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3385 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3386 dnsize += sizeof(LDAPAVA) * navas;
3387 if (dnsize > sizeof(ptrs)) {
3388 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3389 if ( newDN == NULL )
3390 return LDAP_NO_MEMORY;
3392 newDN = (LDAPDN)ptrs;
3395 newDN[nrdns] = NULL;
3396 newRDN = (LDAPRDN)(newDN + nrdns+1);
3397 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3400 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3401 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3402 ne = X509_NAME_get_entry( x509_name, i );
3403 obj = X509_NAME_ENTRY_get_object( ne );
3404 str = X509_NAME_ENTRY_get_data( ne );
3406 /* If set changed, move to next RDN */
3407 if ( set != ne->set ) {
3408 /* If this is not the first time, end the
3409 * previous RDN and advance.
3415 newDN[j++] = newRDN;
3420 newAVA->la_private = NULL;
3421 newAVA->la_flags = LDAP_AVA_STRING;
3424 int n = OBJ_obj2nid( obj );
3428 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3429 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3431 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3432 __etoa( newAVA->la_attr.bv_val );
3433 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3436 get_oid: newAVA->la_attr.bv_val = oidptr;
3437 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3439 __etoa( newAVA->la_attr.bv_val );
3441 oidptr += newAVA->la_attr.bv_len + 1;
3442 oidrem -= newAVA->la_attr.bv_len + 1;
3444 /* Running out of OID buffer space? */
3446 if ( oidsize == 0 ) {
3447 oidsize = sizeof(oids) * 2;
3449 oidbuf = LDAP_MALLOC( oidsize );
3450 if ( oidbuf == NULL ) goto nomem;
3454 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3455 if ( oidbuf == NULL ) goto nomem;
3456 /* Buffer moved! Fix AVA pointers */
3457 if ( old != oidbuf ) {
3459 long dif = oidbuf - old;
3461 for (a=baseAVA; a<=newAVA; a++){
3462 if (a->la_attr.bv_val >= old &&
3463 a->la_attr.bv_val <= (old + oidsize))
3464 a->la_attr.bv_val += dif;
3467 oidptr = oidbuf + oidsize - oidrem;
3473 Val.bv_val = (char *) str->data;
3474 Val.bv_len = str->length;
3475 switch( str->type ) {
3476 case V_ASN1_UNIVERSALSTRING:
3477 /* This uses 32-bit ISO 10646-1 */
3478 csize = 4; goto to_utf8;
3479 case V_ASN1_BMPSTRING:
3480 /* This uses 16-bit ISO 10646-1 */
3481 csize = 2; goto to_utf8;
3482 case V_ASN1_T61STRING:
3483 /* This uses 8-bit, assume ISO 8859-1 */
3485 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3486 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3487 if (rc != LDAP_SUCCESS) goto nomem;
3488 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3490 case V_ASN1_UTF8STRING:
3491 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3492 /* This is already in UTF-8 encoding */
3493 case V_ASN1_IA5STRING:
3494 case V_ASN1_PRINTABLESTRING:
3495 /* These are always 7-bit strings */
3496 newAVA->la_value = Val;
3507 rc = func( newDN, flags, NULL );
3508 if ( rc != LDAP_SUCCESS )
3512 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3515 for (;baseAVA < newAVA; baseAVA++) {
3516 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3517 LDAP_FREE( baseAVA->la_attr.bv_val );
3518 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3519 LDAP_FREE( baseAVA->la_value.bv_val );
3523 LDAP_FREE( oidbuf );
3524 if ( newDN != (LDAPDN) ptrs )
3528 #endif /* HAVE_TLS */