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" */
36 /* parsing/printing routines */
37 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
38 const char **next, unsigned flags, int *retFlags, void *ctx );
39 static int DCE2strval( const char *str, struct berval *val,
40 const char **next, unsigned flags, void *ctx );
41 static int IA52strval( const char *str, struct berval *val,
42 const char **next, unsigned flags, void *ctx );
43 static int quotedIA52strval( const char *str, struct berval *val,
44 const char **next, unsigned flags, void *ctx );
45 static int hexstr2binval( const char *str, struct berval *val,
46 const char **next, unsigned flags, void *ctx );
47 static int hexstr2bin( const char *str, char *c );
48 static int byte2hexpair( const char *val, char *pair );
49 static int binval2hexstr( struct berval *val, char *str );
50 static int strval2strlen( struct berval *val, unsigned flags,
52 static int strval2str( struct berval *val, char *str, unsigned flags,
54 static int strval2IA5strlen( struct berval *val, unsigned flags,
56 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
58 static int strval2DCEstrlen( struct berval *val, unsigned flags,
60 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
62 static int strval2ADstrlen( struct berval *val, unsigned flags,
64 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
66 static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
69 static LDAPAVA * ldapava_new(
70 const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
72 /* Higher level helpers */
73 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
74 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
75 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
76 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
77 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
78 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
79 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
80 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
81 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
82 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
85 * RFC 1823 ldap_get_dn
88 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
94 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
96 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
100 assert( LDAP_VALID(ld) );
101 assert( entry != NULL );
103 tmp = *entry->lm_ber; /* struct copy */
104 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
105 ld->ld_errno = LDAP_DECODING_ERROR;
113 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
116 BerElement tmp, *ber;
118 int rc = LDAP_SUCCESS;
121 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
123 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
126 assert( ld != NULL );
127 assert( LDAP_VALID(ld) );
128 assert( entry != NULL );
129 assert( dn != NULL );
136 ber = ldap_alloc_ber_with_options( ld );
138 return LDAP_NO_MEMORY;
145 *ber = *entry->lm_ber; /* struct copy */
146 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
147 rc = ld->ld_errno = LDAP_DECODING_ERROR;
149 if ( rc == LDAP_SUCCESS ) {
150 /* set the length to avoid overrun */
151 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
152 if( rc != LBER_OPT_SUCCESS ) {
153 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
156 if ( rc != LDAP_SUCCESS && berout ) {
164 * RFC 1823 ldap_dn2ufn
167 ldap_dn2ufn( LDAP_CONST char *dn )
172 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
174 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
177 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
178 &out, LDAP_DN_FORMAT_UFN );
184 * RFC 1823 ldap_explode_dn
187 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
190 char **values = NULL;
192 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
195 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
197 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
200 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
205 if( tmpDN == NULL ) {
206 values = LDAP_MALLOC( sizeof( char * ) );
207 if( values == NULL ) return NULL;
213 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
215 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
216 if ( values == NULL ) {
217 ldap_dnfree( tmpDN );
221 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
222 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
224 ldap_dnfree( tmpDN );
225 values[ iRDN ] = NULL;
231 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
234 char **values = NULL;
239 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
241 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
245 * we only parse the first rdn
246 * FIXME: we prefer efficiency over checking if the _ENTIRE_
249 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
254 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
255 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
256 if ( values == NULL ) {
257 ldap_rdnfree( tmpRDN );
261 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
262 ber_len_t l = 0, vl, al = 0;
264 LDAPAVA *ava = tmpRDN[ iAVA ];
266 if ( ava->la_flags & LDAP_AVA_BINARY ) {
267 vl = 1 + 2 * ava->la_value.bv_len;
270 if ( strval2strlen( &ava->la_value,
271 ava->la_flags, &vl ) ) {
277 al = ava->la_attr.bv_len;
278 l = vl + ava->la_attr.bv_len + 1;
280 str = LDAP_MALLOC( l + 1 );
281 AC_MEMCPY( str, ava->la_attr.bv_val,
282 ava->la_attr.bv_len );
287 str = LDAP_MALLOC( l + 1 );
290 if ( ava->la_flags & LDAP_AVA_BINARY ) {
292 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
297 if ( strval2str( &ava->la_value, &str[ al ],
298 ava->la_flags, &vl ) ) {
304 values[ iAVA ] = str;
306 values[ iAVA ] = NULL;
308 ldap_rdnfree( tmpRDN );
313 LBER_VFREE( values );
314 ldap_rdnfree( tmpRDN );
319 ldap_dn2dcedn( LDAP_CONST char *dn )
324 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
326 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
329 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
330 &out, LDAP_DN_FORMAT_DCE );
336 ldap_dcedn2dn( LDAP_CONST char *dce )
341 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
343 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
346 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
352 ldap_dn2ad_canonical( LDAP_CONST char *dn )
357 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
359 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
362 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
363 &out, LDAP_DN_FORMAT_AD_CANONICAL );
369 * function that changes the string representation of dnin
370 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
373 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
374 * plus some rfc 1779)
375 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
376 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
377 * LDAP_DN_FORMAT_DCE (?)
379 * fout can be any of the above except
380 * LDAP_DN_FORMAT_LDAP
382 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
383 * LDAP_DN_FORMAT_AD_CANONICAL (?)
386 ldap_dn_normalize( LDAP_CONST char *dnin,
387 unsigned fin, char **dnout, unsigned fout )
393 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
395 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
402 if ( dnin == NULL ) {
403 return( LDAP_SUCCESS );
406 rc = ldap_str2dn( dnin , &tmpDN, fin );
407 if ( rc != LDAP_SUCCESS ) {
411 rc = ldap_dn2str( tmpDN, dnout, fout );
413 ldap_dnfree( tmpDN );
421 /* #define B4ATTRTYPE 0x0001 */
422 #define B4OIDATTRTYPE 0x0002
423 #define B4STRINGATTRTYPE 0x0003
425 #define B4AVAEQUALS 0x0100
426 #define B4AVASEP 0x0200
427 #define B4RDNSEP 0x0300
428 #define GOTAVA 0x0400
430 #define B4ATTRVALUE 0x0010
431 #define B4STRINGVALUE 0x0020
432 #define B4IA5VALUEQUOTED 0x0030
433 #define B4IA5VALUE 0x0040
434 #define B4BINARYVALUE 0x0050
437 * Helpers (mostly from slap.h)
438 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
439 * Macros assume "C" Locale (ASCII)
441 #define LDAP_DN_ASCII_SPACE(c) \
442 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
443 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
444 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
445 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
447 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
448 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
449 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
450 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
451 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
452 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
455 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
456 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
457 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
458 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
459 #define LDAP_DN_ATTRDESC_CHAR(c) \
460 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
462 /* special symbols */
463 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
464 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
465 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
466 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
467 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
468 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
469 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
470 #define LDAP_DN_VALUE_END(c) \
471 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
472 #define LDAP_DN_NE(c) \
473 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
474 || LDAP_DN_AVA_EQUALS(c) || LDAP_DN_QUOTES(c) \
475 || (c) == '<' || (c) == '>' )
476 #define LDAP_DN_MAYESCAPE(c) \
477 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
478 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
479 #define LDAP_DN_NEEDESCAPE(c) \
480 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
481 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
482 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
483 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
484 #define LDAP_DN_WILLESCAPE_CHAR(c) \
485 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
486 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
487 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
488 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
491 #define LDAP_DN_VALUE_END_V2(c) \
492 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
494 #define LDAP_DN_V2_SPECIAL(c) \
495 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
496 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
497 || LDAP_DN_OCTOTHORPE(c) )
498 #define LDAP_DN_V2_PAIR(c) \
499 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
502 * DCE (mostly from Luke Howard and IBM implementation for AIX)
504 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
505 * Here escapes and valid chars for GDS are considered; as soon as more
506 * specific info is found, the macros will be updated.
508 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
509 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
511 * Metachars: '/', ',', '=', '\'.
513 * the '\' is used to escape other metachars.
519 * Attribute types must start with alphabetic chars and can contain
520 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
522 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
523 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
524 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
525 #define LDAP_DN_VALUE_END_DCE(c) \
526 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
527 #define LDAP_DN_NEEDESCAPE_DCE(c) \
528 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
531 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
532 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
533 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
534 #define LDAP_DN_VALUE_END_AD(c) \
535 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
536 #define LDAP_DN_NEEDESCAPE_AD(c) \
537 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
540 #define LDAP_DN_HEXPAIR(s) \
541 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
542 /* better look at the AttributeDescription? */
544 /* FIXME: no composite rdn or non-"dc" types, right?
545 * (what about "dc" in OID form?) */
546 /* FIXME: we do not allow binary values in domain, right? */
547 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
548 /* NOTE: don't use strcasecmp() as it is locale specific! */
549 #define LDAP_DC_ATTR "dc"
550 #define LDAP_DC_ATTRU "DC"
551 #define LDAP_DN_IS_RDN_DC( r ) \
552 ( (r) && (r)[0] && !(r)[1] \
553 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
554 && ((r)[0]->la_attr.bv_len == 2) \
555 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
556 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
557 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
558 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
560 /* Composite rules */
561 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
562 ( LDAP_DN_LDAPV2(f) \
563 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
564 #define LDAP_DN_ALLOW_SPACES(f) \
565 ( LDAP_DN_LDAPV2(f) \
566 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
567 #define LDAP_DN_LDAP(f) \
568 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
569 #define LDAP_DN_LDAPV3(f) \
570 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
571 #define LDAP_DN_LDAPV2(f) \
572 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
573 #define LDAP_DN_DCE(f) \
574 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
575 #define LDAP_DN_UFN(f) \
576 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
577 #define LDAP_DN_ADC(f) \
578 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
579 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
582 * LDAPAVA helpers (will become part of the API for operations
583 * on structural representations of DNs).
586 ldapava_new( const struct berval *attr, const struct berval *val,
587 unsigned flags, void *ctx )
594 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
597 ava->la_attr.bv_len = attr->bv_len;
598 ava->la_attr.bv_val = (char *)(ava+1);
599 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
600 ava->la_attr.bv_val[attr->bv_len] = '\0';
602 ava->la_value = *val;
603 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
605 ava->la_private = NULL;
612 ldapava_free( LDAPAVA *ava, void *ctx )
617 /* ava's private must be freed by caller
618 * (at present let's skip this check because la_private
619 * basically holds static data) */
620 assert( ava->la_private == NULL );
623 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
624 LDAP_FREEX( ava->la_value.bv_val, ctx );
626 LDAP_FREEX( ava, ctx );
630 ldap_rdnfree( LDAPRDN rdn )
632 ldap_rdnfree_x( rdn, NULL );
636 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
644 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
645 ldapava_free( rdn[ iAVA ], ctx );
648 LDAP_FREEX( rdn, ctx );
652 ldap_dnfree( LDAPDN dn )
654 ldap_dnfree_x( dn, NULL );
658 ldap_dnfree_x( LDAPDN dn, void *ctx )
666 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
667 ldap_rdnfree_x( dn[ iRDN ], ctx );
670 LDAP_FREEX( dn, ctx );
674 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
675 * into a structural representation of the DN, by separating attribute
676 * types and values encoded in the more appropriate form, which is
677 * string or OID for attribute types and binary form of the BER encoded
678 * value or Unicode string. Formats different from LDAPv3 are parsed
679 * according to their own rules and turned into the more appropriate
680 * form according to LDAPv3.
682 * NOTE: I realize the code is getting spaghettish; it is rather
683 * experimental and will hopefully turn into something more simple
684 * and readable as soon as it works as expected.
688 * Default sizes of AVA and RDN static working arrays; if required
689 * the are dynamically resized. The values can be tuned in case
690 * of special requirements (e.g. very deep DN trees or high number
693 #define TMP_AVA_SLOTS 8
694 #define TMP_RDN_SLOTS 32
697 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
703 bv.bv_len = strlen( str );
704 bv.bv_val = (char *) str;
706 return ldap_bv2dn_x( &bv, dn, flags, NULL );
710 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
712 return ldap_bv2dn_x( bv, dn, flags, NULL );
716 ldap_bv2dn_x( struct berval *bv, LDAPDN *dn, unsigned flags, void *ctx )
719 int rc = LDAP_DECODING_ERROR;
723 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
724 int num_slots = TMP_RDN_SLOTS;
725 char *str = bv->bv_val;
726 char *end = str + bv->bv_len;
729 assert( bv->bv_val );
733 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
735 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n%s", str, flags, "" );
740 switch ( LDAP_DN_FORMAT( flags ) ) {
741 case LDAP_DN_FORMAT_LDAP:
742 case LDAP_DN_FORMAT_LDAPV3:
743 case LDAP_DN_FORMAT_LDAPV2:
744 case LDAP_DN_FORMAT_DCE:
747 /* unsupported in str2dn */
748 case LDAP_DN_FORMAT_UFN:
749 case LDAP_DN_FORMAT_AD_CANONICAL:
750 return LDAP_PARAM_ERROR;
752 case LDAP_DN_FORMAT_LBER:
754 return LDAP_PARAM_ERROR;
757 if ( bv->bv_len == 0 ) {
761 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
762 /* value must have embedded NULs */
763 return LDAP_DECODING_ERROR;
767 if ( LDAP_DN_DCE( flags ) ) {
770 * (from Luke Howard: thnx) A RDN separator is required
771 * at the beginning of an (absolute) DN.
773 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
779 * actually we do not want to accept by default the DCE form,
780 * we do not want to auto-detect it
783 } else if ( LDAP_DN_LDAP( flags ) ) {
785 * if dn starts with '/' let's make it a DCE dn
787 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
788 flags |= LDAP_DN_FORMAT_DCE;
794 for ( ; p < end; p++ ) {
797 tmpbv.bv_len = bv->bv_len - ( p - str );
798 tmpbv.bv_val = (char *)p;
800 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
801 if ( err != LDAP_SUCCESS ) {
806 * We expect a rdn separator
808 if ( p < end && p[ 0 ] ) {
809 switch ( LDAP_DN_FORMAT( flags ) ) {
810 case LDAP_DN_FORMAT_LDAPV3:
811 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
812 rc = LDAP_DECODING_ERROR;
817 case LDAP_DN_FORMAT_LDAP:
818 case LDAP_DN_FORMAT_LDAPV2:
819 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
820 rc = LDAP_DECODING_ERROR;
825 case LDAP_DN_FORMAT_DCE:
826 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
827 rc = LDAP_DECODING_ERROR;
835 tmpDN[nrdns++] = newRDN;
839 * make the static RDN array dynamically rescalable
841 if ( nrdns == num_slots ) {
844 if ( tmpDN == tmpDN_ ) {
845 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
850 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
853 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
864 if ( p >= end || p[ 0 ] == '\0' ) {
866 * the DN is over, phew
868 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
869 if ( newDN == NULL ) {
875 if ( LDAP_DN_DCE( flags ) ) {
876 /* add in reversed order */
877 for ( i=0; i<nrdns; i++ )
878 newDN[i] = tmpDN[nrdns-1-i];
880 for ( i=0; i<nrdns; i++ )
892 ldap_rdnfree_x( newRDN, ctx );
895 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
896 ldap_rdnfree_x( tmpDN[nrdns], ctx );
901 if ( tmpDN != tmpDN_ ) {
902 LDAP_FREEX( tmpDN, ctx );
906 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s,%u)=%d\n",
909 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s,%u)=%d\n", str, flags, rc );
919 * Parses a relative DN according to flags up to a rdn separator
920 * or to the end of str.
921 * Returns the rdn and a pointer to the string continuation, which
922 * corresponds to the rdn separator or to '\0' in case the string is over.
925 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
926 char **n_in, unsigned flags )
931 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
933 bv.bv_len = strlen( str );
934 bv.bv_val = (char *) str;
936 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
940 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
941 char **n_in, unsigned flags )
943 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
947 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
948 char **n_in, unsigned flags, void *ctx )
950 const char **n = (const char **) n_in;
954 int rc = LDAP_DECODING_ERROR;
955 int attrTypeEncoding = LDAP_AVA_STRING,
956 attrValueEncoding = LDAP_AVA_STRING;
958 struct berval attrType = BER_BVNULL;
959 struct berval attrValue = BER_BVNULL;
961 LDAPRDN newRDN = NULL;
962 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
963 int num_slots = TMP_AVA_SLOTS;
969 assert( bv->bv_len );
970 assert( bv->bv_val );
971 assert( rdn || flags & LDAP_DN_SKIP );
975 stoplen = bv->bv_len;
982 switch ( LDAP_DN_FORMAT( flags ) ) {
983 case LDAP_DN_FORMAT_LDAP:
984 case LDAP_DN_FORMAT_LDAPV3:
985 case LDAP_DN_FORMAT_LDAPV2:
986 case LDAP_DN_FORMAT_DCE:
989 /* unsupported in str2dn */
990 case LDAP_DN_FORMAT_UFN:
991 case LDAP_DN_FORMAT_AD_CANONICAL:
992 return LDAP_PARAM_ERROR;
994 case LDAP_DN_FORMAT_LBER:
996 return LDAP_PARAM_ERROR;
999 if ( bv->bv_len == 0 ) {
1000 return LDAP_SUCCESS;
1004 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1005 /* value must have embedded NULs */
1006 return LDAP_DECODING_ERROR;
1010 for ( ; p[ 0 ] || state == GOTAVA; ) {
1013 * The parser in principle advances one token a time,
1014 * or toggles state if preferable.
1019 * an AttributeType can be encoded as:
1020 * - its string representation; in detail, implementations
1021 * MUST recognize AttributeType string type names listed
1022 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1023 * MAY recognize other names.
1024 * - its numeric OID (a dotted decimal string); in detail
1025 * RFC 2253 asserts that ``Implementations MUST allow
1026 * an oid in the attribute type to be prefixed by one
1027 * of the character strings "oid." or "OID."''. As soon
1028 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1029 * I'm not sure whether this is required or not any
1030 * longer; to be liberal, we still implement it.
1033 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1034 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1041 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1042 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1047 /* whitespace is allowed (and trimmed) */
1049 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1054 /* error: we expected an AVA */
1060 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1061 state = B4OIDATTRTYPE;
1065 /* else must be alpha */
1066 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1070 /* LDAPv2 "oid." prefix */
1071 if ( LDAP_DN_LDAPV2( flags ) ) {
1073 * to be overly pedantic, we only accept
1076 if ( flags & LDAP_DN_PEDANTIC ) {
1077 if ( !strncmp( p, "OID.", 4 )
1078 || !strncmp( p, "oid.", 4 ) ) {
1080 state = B4OIDATTRTYPE;
1084 if ( !strncasecmp( p, "oid.", 4 ) ) {
1086 state = B4OIDATTRTYPE;
1092 state = B4STRINGATTRTYPE;
1095 case B4OIDATTRTYPE: {
1096 int err = LDAP_SUCCESS;
1098 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1101 if ( err != LDAP_SUCCESS ) {
1104 attrType.bv_len = p - attrType.bv_val;
1106 attrTypeEncoding = LDAP_AVA_BINARY;
1108 state = B4AVAEQUALS;
1112 case B4STRINGATTRTYPE: {
1113 const char *startPos, *endPos = NULL;
1117 * the starting char has been found to be
1118 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1119 * FIXME: DCE attr types seem to have a more
1120 * restrictive syntax (no '-' ...)
1122 for ( startPos = p++; p[ 0 ]; p++ ) {
1123 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1127 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1130 * RFC 2253 does not explicitly
1131 * allow lang extensions to attribute
1134 if ( flags & LDAP_DN_PEDANTIC ) {
1139 * we trim ';' and following lang
1140 * and so from attribute types
1143 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1144 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1152 len = ( endPos ? endPos : p ) - startPos;
1157 attrTypeEncoding = LDAP_AVA_STRING;
1160 * here we need to decide whether to use it as is
1161 * or turn it in OID form; as a consequence, we
1162 * need to decide whether to binary encode the value
1165 state = B4AVAEQUALS;
1167 if ( flags & LDAP_DN_SKIP ) {
1171 attrType.bv_val = (char *)startPos;
1172 attrType.bv_len = len;
1178 /* spaces may not be allowed */
1179 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1180 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1185 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1190 /* need equal sign */
1191 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1196 /* spaces may not be allowed */
1197 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1198 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1203 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1209 * octothorpe means a BER encoded value will follow
1210 * FIXME: I don't think DCE will allow it
1212 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1214 attrValueEncoding = LDAP_AVA_BINARY;
1215 state = B4BINARYVALUE;
1219 /* STRING value expected */
1222 * if we're pedantic, an attribute type in OID form
1223 * SHOULD imply a BER encoded attribute value; we
1224 * should at least issue a warning
1226 if ( ( flags & LDAP_DN_PEDANTIC )
1227 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1228 /* OID attrType SHOULD use binary encoding */
1232 attrValueEncoding = LDAP_AVA_STRING;
1235 * LDAPv2 allows the attribute value to be quoted;
1236 * also, IA5 values are expected, in principle
1238 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1239 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1241 state = B4IA5VALUEQUOTED;
1245 if ( LDAP_DN_LDAPV2( flags ) ) {
1252 * here STRING means RFC 2253 string
1253 * FIXME: what about DCE strings?
1259 state = B4STRINGVALUE;
1264 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1272 switch ( LDAP_DN_FORMAT( flags ) ) {
1273 case LDAP_DN_FORMAT_LDAP:
1274 case LDAP_DN_FORMAT_LDAPV3:
1275 if ( str2strval( p, stoplen - ( p - str ),
1276 &attrValue, &p, flags,
1277 &attrValueEncoding, ctx ) ) {
1282 case LDAP_DN_FORMAT_DCE:
1283 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1296 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1303 case B4IA5VALUEQUOTED:
1305 /* lead quote already stripped */
1306 if ( quotedIA52strval( p, &attrValue,
1307 &p, flags, ctx ) ) {
1317 if ( !( flags & LDAP_DN_SKIP ) ) {
1321 * we accept empty values
1323 ava = ldapava_new( &attrType, &attrValue,
1324 attrValueEncoding, ctx );
1325 if ( ava == NULL ) {
1326 rc = LDAP_NO_MEMORY;
1329 tmpRDN[navas++] = ava;
1331 attrValue.bv_val = NULL;
1332 attrValue.bv_len = 0;
1335 * prepare room for new AVAs if needed
1337 if (navas == num_slots) {
1340 if ( tmpRDN == tmpRDN_ ) {
1341 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1342 if ( tmp == NULL ) {
1343 rc = LDAP_NO_MEMORY;
1346 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1349 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1350 if ( tmp == NULL ) {
1351 rc = LDAP_NO_MEMORY;
1362 * if we got an AVA separator ('+', or ',' for DCE )
1363 * we expect a new AVA for this RDN; otherwise
1364 * we add the RDN to the DN
1366 switch ( LDAP_DN_FORMAT( flags ) ) {
1367 case LDAP_DN_FORMAT_LDAP:
1368 case LDAP_DN_FORMAT_LDAPV3:
1369 case LDAP_DN_FORMAT_LDAPV2:
1370 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1375 case LDAP_DN_FORMAT_DCE:
1376 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1384 * the RDN is over, phew
1387 if ( !( flags & LDAP_DN_SKIP ) ) {
1388 newRDN = (LDAPRDN)LDAP_MALLOCX(
1389 sizeof(LDAPAVA) * (navas+1), ctx );
1390 if ( newRDN == NULL ) {
1391 rc = LDAP_NO_MEMORY;
1394 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1395 newRDN[navas] = NULL;
1403 /* they should have been used in an AVA */
1404 attrType.bv_val = NULL;
1405 attrValue.bv_val = NULL;
1420 /* They are set to NULL after they're used in an AVA */
1422 if ( attrValue.bv_val ) {
1423 LDAP_FREEX( attrValue.bv_val, ctx );
1426 for ( navas-- ; navas >= 0; navas-- ) {
1427 ldapava_free( tmpRDN[navas], ctx );
1432 if ( tmpRDN != tmpRDN_ ) {
1433 LDAP_FREEX( tmpRDN, ctx );
1444 * reads in a UTF-8 string value, unescaping stuff:
1445 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1446 * '\' + HEXPAIR(p) -> unhex(p)
1449 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1451 const char *p, *end, *startPos, *endPos = NULL;
1452 ber_len_t len, escapes;
1459 end = str + stoplen;
1460 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1461 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1463 if ( p[ 0 ] == '\0' ) {
1466 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1471 if ( LDAP_DN_HEXPAIR( p ) ) {
1474 hexstr2bin( p, &c );
1477 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1480 * we assume the string is UTF-8
1482 *retFlags = LDAP_AVA_NONPRINTABLE;
1489 if ( LDAP_DN_PEDANTIC & flags ) {
1493 * we do not allow escaping
1494 * of chars that don't need
1495 * to and do not belong to
1500 } else if (!LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1501 if ( p[ 0 ] == '\0' ) {
1504 *retFlags = LDAP_AVA_NONPRINTABLE;
1506 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1507 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1510 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1512 * FIXME: maybe we can add
1513 * escapes if not pedantic?
1520 * we do allow unescaped spaces at the end
1521 * of the value only in non-pedantic mode
1523 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1524 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1525 if ( flags & LDAP_DN_PEDANTIC ) {
1529 /* strip trailing (unescaped) spaces */
1530 for ( endPos = p - 1;
1531 endPos > startPos + 1 &&
1532 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1533 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1540 if ( flags & LDAP_DN_SKIP ) {
1545 * FIXME: test memory?
1547 len = ( endPos ? endPos : p ) - startPos - escapes;
1550 if ( escapes == 0 ) {
1551 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1552 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1553 AC_MEMCPY( val->bv_val, startPos, len );
1554 val->bv_val[ len ] = '\0';
1556 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1562 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1563 for ( s = 0, d = 0; d < len; ) {
1564 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1566 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1567 val->bv_val[ d++ ] =
1570 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1573 hexstr2bin( &startPos[ s ], &c );
1574 val->bv_val[ d++ ] = c;
1578 /* we should never get here */
1583 val->bv_val[ d++ ] = startPos[ s++ ];
1587 val->bv_val[ d ] = '\0';
1595 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1597 const char *p, *startPos, *endPos = NULL;
1598 ber_len_t len, escapes;
1606 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1607 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1609 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1616 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1621 * FIXME: can we accept anything else? I guess we need
1622 * to stop if a value is not legal
1627 * (unescaped) trailing spaces are trimmed must be silently ignored;
1630 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1631 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1632 if ( flags & LDAP_DN_PEDANTIC ) {
1636 /* strip trailing (unescaped) spaces */
1637 for ( endPos = p - 1;
1638 endPos > startPos + 1 &&
1639 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1640 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1647 if ( flags & LDAP_DN_SKIP ) {
1651 len = ( endPos ? endPos : p ) - startPos - escapes;
1653 if ( escapes == 0 ){
1654 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1659 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1660 for ( s = 0, d = 0; d < len; ) {
1662 * This point is reached only if escapes
1663 * are properly used, so all we need to
1666 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1670 val->bv_val[ d++ ] = startPos[ s++ ];
1672 val->bv_val[ d ] = '\0';
1673 assert( strlen( val->bv_val ) == len );
1680 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1682 const char *p, *startPos, *endPos = NULL;
1683 ber_len_t len, escapes;
1695 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1696 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1698 if ( p[ 0 ] == '\0' ) {
1702 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1703 && ( LDAP_DN_PEDANTIC & flags ) ) {
1708 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1713 * FIXME: can we accept anything else? I guess we need
1714 * to stop if a value is not legal
1718 /* strip trailing (unescaped) spaces */
1720 endPos > startPos + 1 &&
1721 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1722 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1728 if ( flags & LDAP_DN_SKIP ) {
1732 len = ( endPos ? endPos : p ) - startPos - escapes;
1734 if ( escapes == 0 ) {
1735 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1740 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1741 for ( s = 0, d = 0; d < len; ) {
1742 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1745 val->bv_val[ d++ ] = startPos[ s++ ];
1747 val->bv_val[ d ] = '\0';
1748 assert( strlen( val->bv_val ) == len );
1755 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1757 const char *p, *startPos, *endPos = NULL;
1759 unsigned escapes = 0;
1767 /* initial quote already eaten */
1768 for ( startPos = p = str; p[ 0 ]; p++ ) {
1770 * According to RFC 1779, the quoted value can
1771 * contain escaped as well as unescaped special values;
1772 * as a consequence we tolerate escaped values
1773 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1774 * (e.g. '","' -> '\,').
1776 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1777 if ( p[ 1 ] == '\0' ) {
1782 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1783 && ( LDAP_DN_PEDANTIC & flags ) ) {
1785 * do we allow to escape normal chars?
1786 * LDAPv2 does not allow any mechanism
1787 * for escaping chars with '\' and hex
1794 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1796 /* eat closing quotes */
1802 * FIXME: can we accept anything else? I guess we need
1803 * to stop if a value is not legal
1807 if ( endPos == NULL ) {
1811 /* Strip trailing (unescaped) spaces */
1812 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1817 if ( flags & LDAP_DN_SKIP ) {
1821 len = endPos - startPos - escapes;
1822 assert( endPos >= startPos + escapes );
1824 if ( escapes == 0 ) {
1825 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1830 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1833 for ( s = d = 0; d < len; ) {
1834 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1837 val->bv_val[ d++ ] = str[ s++ ];
1839 val->bv_val[ d ] = '\0';
1840 assert( strlen( val->bv_val ) == len );
1847 hexstr2bin( const char *str, char *c )
1857 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1861 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1864 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1871 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1875 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1876 *c += c2 - 'A' + 10;
1878 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1879 *c += c2 - 'a' + 10;
1887 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1889 const char *p, *startPos, *endPos = NULL;
1899 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1900 switch ( LDAP_DN_FORMAT( flags ) ) {
1901 case LDAP_DN_FORMAT_LDAPV3:
1902 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1907 case LDAP_DN_FORMAT_LDAP:
1908 case LDAP_DN_FORMAT_LDAPV2:
1909 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1914 case LDAP_DN_FORMAT_DCE:
1915 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1921 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1922 if ( flags & LDAP_DN_PEDANTIC ) {
1927 for ( ; p[ 0 ]; p++ ) {
1928 switch ( LDAP_DN_FORMAT( flags ) ) {
1929 case LDAP_DN_FORMAT_LDAPV3:
1930 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1935 case LDAP_DN_FORMAT_LDAP:
1936 case LDAP_DN_FORMAT_LDAPV2:
1937 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1942 case LDAP_DN_FORMAT_DCE:
1943 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1952 if ( !LDAP_DN_HEXPAIR( p ) ) {
1960 if ( flags & LDAP_DN_SKIP ) {
1964 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1966 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1969 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1970 if ( val->bv_val == NULL ) {
1971 return( LDAP_NO_MEMORY );
1974 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1977 hexstr2bin( &startPos[ s ], &c );
1979 val->bv_val[ d ] = c;
1982 val->bv_val[ d ] = '\0';
1988 * convert a byte in a hexadecimal pair
1991 byte2hexpair( const char *val, char *pair )
1993 static const char hexdig[] = "0123456789ABCDEF";
1999 * we assume the string has enough room for the hex encoding
2003 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2004 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2010 * convert a binary value in hexadecimal pairs
2013 binval2hexstr( struct berval *val, char *str )
2020 if ( val->bv_len == 0 ) {
2025 * we assume the string has enough room for the hex encoding
2029 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2030 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2037 * Length of the string representation, accounting for escaped hex
2041 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2043 ber_len_t l, cl = 1;
2045 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2046 #ifdef PRETTY_ESCAPE
2047 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2048 #endif /* PRETTY_ESCAPE */
2054 if ( val->bv_len == 0 ) {
2058 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2063 if ( p[ 0 ] == '\0' ) {
2069 cl = LDAP_UTF8_CHARLEN2( p, cl );
2071 /* illegal utf-8 char! */
2074 } else if ( cl > 1 ) {
2077 for ( cnt = 1; cnt < cl; cnt++ ) {
2078 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2082 l += escaped_byte_len * cl;
2084 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2085 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2086 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2087 #ifdef PRETTY_ESCAPE
2089 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2091 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2095 * there might be some chars we want
2096 * to escape in form of a couple
2097 * of hexdigits for optimization purposes
2102 l += escaped_ascii_len;
2104 #else /* ! PRETTY_ESCAPE */
2106 #endif /* ! PRETTY_ESCAPE */
2119 * convert to string representation, escaping with hex the UTF-8 stuff;
2120 * assume the destination has enough room for escaping
2123 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2125 ber_len_t s, d, end;
2131 if ( val->bv_len == 0 ) {
2137 * we assume the string has enough room for the hex encoding
2140 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2146 if ( val->bv_val[ s ] == '\0' ) {
2156 * The length was checked in strval2strlen();
2157 * LDAP_UTF8_CHARLEN() should suffice
2159 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2163 * there might be some chars we want to escape in form
2164 * of a couple of hexdigits for optimization purposes
2166 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2167 #ifdef PRETTY_ESCAPE
2169 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2171 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2173 #else /* ! PRETTY_ESCAPE */
2174 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2175 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2176 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2178 #endif /* ! PRETTY_ESCAPE */
2182 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2187 } else if ( cl > 1 ) {
2189 str[ d++ ] = val->bv_val[ s++ ];
2193 #ifdef PRETTY_ESCAPE
2194 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2195 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2196 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2198 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2199 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2205 #endif /* PRETTY_ESCAPE */
2206 str[ d++ ] = val->bv_val[ s++ ];
2216 * Length of the IA5 string representation (no UTF-8 allowed)
2219 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2228 if ( val->bv_len == 0 ) {
2232 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2234 * Turn value into a binary encoded BER
2239 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2240 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2241 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2242 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2257 * convert to string representation (np UTF-8)
2258 * assume the destination has enough room for escaping
2261 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2263 ber_len_t s, d, end;
2269 if ( val->bv_len == 0 ) {
2274 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2276 * Turn value into a binary encoded BER
2283 * we assume the string has enough room for the hex encoding
2287 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2288 if ( LDAP_DN_NEEDESCAPE( 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 );
3016 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n%s%s",
3019 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n%s%s", flags, "", "" );
3023 * a null dn means an empty dn string
3024 * FIXME: better raise an error?
3027 bv->bv_val = LDAP_STRDUPX( "", ctx );
3028 return( LDAP_SUCCESS );
3031 switch ( LDAP_DN_FORMAT( flags ) ) {
3032 case LDAP_DN_FORMAT_LDAPV3:
3033 sv2l = strval2strlen;
3037 case LDAP_DN_FORMAT_LDAPV2:
3038 sv2l = strval2IA5strlen;
3039 sv2s = strval2IA5str;
3042 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3044 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3045 goto return_results;
3051 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3052 rc = LDAP_NO_MEMORY;
3056 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3059 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3061 LDAP_FREEX( bv->bv_val, ctx );
3063 goto return_results;
3071 * trim the last ',' (the allocated memory
3072 * is one byte longer than required)
3074 bv->bv_len = len - 1;
3075 bv->bv_val[ bv->bv_len ] = '\0';
3080 case LDAP_DN_FORMAT_UFN: {
3082 * FIXME: quoting from RFC 1781:
3084 To take a distinguished name, and generate a name of this format with
3085 attribute types omitted, the following steps are followed.
3087 1. If the first attribute is of type CommonName, the type may be
3090 2. If the last attribute is of type Country, the type may be
3093 3. If the last attribute is of type Country, the last
3094 Organisation attribute may have the type omitted.
3096 4. All attributes of type OrganisationalUnit may have the type
3097 omitted, unless they are after an Organisation attribute or
3098 the first attribute is of type OrganisationalUnit.
3100 * this should be the pedantic implementation.
3102 * Here the standard implementation reflects
3103 * the one historically provided by OpenLDAP
3104 * (and UMIch, I presume), with the variant
3105 * of spaces and plusses (' + ') separating
3108 * A non-standard but nice implementation could
3109 * be to turn the final "dc" attributes into a
3110 * dot-separated domain.
3112 * Other improvements could involve the use of
3113 * friendly country names and so.
3116 int leftmost_dc = -1;
3118 #endif /* DC_IN_UFN */
3120 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3123 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3124 goto return_results;
3129 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3130 if ( leftmost_dc == -1 ) {
3136 #endif /* DC_IN_UFN */
3139 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3140 rc = LDAP_NO_MEMORY;
3145 if ( leftmost_dc == -1 ) {
3146 #endif /* DC_IN_UFN */
3147 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3150 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3152 LDAP_FREEX( bv->bv_val, ctx );
3154 goto return_results;
3160 * trim the last ', ' (the allocated memory
3161 * is two bytes longer than required)
3163 bv->bv_len = len - 2;
3164 bv->bv_val[ bv->bv_len ] = '\0';
3167 last_iRDN = iRDN - 1;
3169 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3172 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3174 LDAP_FREEX( bv->bv_val, ctx );
3176 goto return_results;
3181 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3182 LDAP_FREEX( bv->bv_val, ctx );
3184 goto return_results;
3187 /* the string is correctly terminated by dn2domain */
3189 #endif /* DC_IN_UFN */
3195 case LDAP_DN_FORMAT_DCE:
3196 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3198 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3199 goto return_results;
3205 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3206 rc = LDAP_NO_MEMORY;
3210 for ( l = 0; iRDN--; ) {
3213 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3215 LDAP_FREEX( bv->bv_val, ctx );
3217 goto return_results;
3225 bv->bv_val[ bv->bv_len ] = '\0';
3230 case LDAP_DN_FORMAT_AD_CANONICAL: {
3231 int trailing_slash = 1;
3234 * Sort of UFN for DCE DNs: a slash ('/') separated
3235 * global->local DN with no types; strictly speaking,
3236 * the naming context should be a domain, which is
3237 * written in DNS-style, e.g. dot-deparated.
3241 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3245 * "microsoft.com/People/Bill,Gates"
3247 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3250 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3251 goto return_results;
3257 /* reserve room for trailing '/' in case the DN
3258 * is exactly a domain */
3259 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3261 rc = LDAP_NO_MEMORY;
3266 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3267 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3272 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3273 flags, &rdnl, 0 ) ) {
3274 LDAP_FREEX( bv->bv_val, ctx );
3276 goto return_results;
3285 * Strictly speaking, AD canonical requires
3286 * a DN to be in the form "..., dc=smtg",
3287 * i.e. terminated by a domain component
3289 if ( flags & LDAP_DN_PEDANTIC ) {
3290 LDAP_FREEX( bv->bv_val, ctx );
3292 rc = LDAP_ENCODING_ERROR;
3296 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3299 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3300 flags, &rdnl, first ) ) {
3301 LDAP_FREEX( bv->bv_val, ctx );
3303 goto return_results;
3312 if ( trailing_slash ) {
3313 /* the DN is exactly a domain -- need a trailing
3314 * slash; room was reserved in advance */
3315 bv->bv_val[ len ] = '/';
3320 bv->bv_val[ bv->bv_len ] = '\0';
3326 return LDAP_PARAM_ERROR;
3330 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s,%u)=%d\n",
3331 bv->bv_val, flags, rc );
3333 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s,%u)=%d\n",
3334 bv->bv_val, flags, rc );
3342 #include <openssl/x509.h>
3343 #include <openssl/err.h>
3345 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3346 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3347 * constructed DN will use numeric OIDs to identify attributeTypes,
3348 * and the func() will be invoked to rewrite the DN with the given
3351 * Otherwise the DN will use shortNames as defined in the OpenSSL
3354 * It's preferable to let slapd do the OID to attributeType mapping,
3355 * because the OpenSSL tables are known to have many typos in versions
3356 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3357 * so we're forced to use OpenSSL's mapping there.
3358 * -- Howard Chu 2002-04-18
3362 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3367 LDAPAVA *newAVA, *baseAVA;
3368 X509_NAME_ENTRY *ne;
3371 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3373 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3375 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3384 /* Get the number of AVAs. This is not necessarily the same as
3385 * the number of RDNs.
3387 navas = X509_NAME_entry_count( x509_name );
3389 /* Get the last element, to see how many RDNs there are */
3390 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3391 nrdns = ne->set + 1;
3393 /* Allocate the DN/RDN/AVA stuff as a single block */
3394 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3395 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3396 dnsize += sizeof(LDAPAVA) * navas;
3397 if (dnsize > sizeof(ptrs)) {
3398 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3399 if ( newDN == NULL )
3400 return LDAP_NO_MEMORY;
3402 newDN = (LDAPDN)ptrs;
3405 newDN[nrdns] = NULL;
3406 newRDN = (LDAPRDN)(newDN + nrdns+1);
3407 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3410 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3411 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3412 ne = X509_NAME_get_entry( x509_name, i );
3413 obj = X509_NAME_ENTRY_get_object( ne );
3414 str = X509_NAME_ENTRY_get_data( ne );
3416 /* If set changed, move to next RDN */
3417 if ( set != ne->set ) {
3418 /* If this is not the first time, end the
3419 * previous RDN and advance.
3425 newDN[j++] = newRDN;
3430 newAVA->la_private = NULL;
3431 newAVA->la_flags = LDAP_AVA_STRING;
3434 int n = OBJ_obj2nid( obj );
3438 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3439 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3441 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3442 __etoa( newAVA->la_attr.bv_val );
3443 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3446 get_oid: newAVA->la_attr.bv_val = oidptr;
3447 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3449 __etoa( newAVA->la_attr.bv_val );
3451 oidptr += newAVA->la_attr.bv_len + 1;
3452 oidrem -= newAVA->la_attr.bv_len + 1;
3454 /* Running out of OID buffer space? */
3456 if ( oidsize == 0 ) {
3457 oidsize = sizeof(oids) * 2;
3459 oidbuf = LDAP_MALLOC( oidsize );
3460 if ( oidbuf == NULL ) goto nomem;
3464 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3465 if ( oidbuf == NULL ) goto nomem;
3466 /* Buffer moved! Fix AVA pointers */
3467 if ( old != oidbuf ) {
3469 long dif = oidbuf - old;
3471 for (a=baseAVA; a<=newAVA; a++){
3472 if (a->la_attr.bv_val >= old &&
3473 a->la_attr.bv_val <= (old + oidsize))
3474 a->la_attr.bv_val += dif;
3477 oidptr = oidbuf + oidsize - oidrem;
3483 Val.bv_val = (char *) str->data;
3484 Val.bv_len = str->length;
3485 switch( str->type ) {
3486 case V_ASN1_UNIVERSALSTRING:
3487 /* This uses 32-bit ISO 10646-1 */
3488 csize = 4; goto to_utf8;
3489 case V_ASN1_BMPSTRING:
3490 /* This uses 16-bit ISO 10646-1 */
3491 csize = 2; goto to_utf8;
3492 case V_ASN1_T61STRING:
3493 /* This uses 8-bit, assume ISO 8859-1 */
3495 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3496 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3497 if (rc != LDAP_SUCCESS) goto nomem;
3498 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3500 case V_ASN1_UTF8STRING:
3501 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3502 /* This is already in UTF-8 encoding */
3503 case V_ASN1_IA5STRING:
3504 case V_ASN1_PRINTABLESTRING:
3505 /* These are always 7-bit strings */
3506 newAVA->la_value = Val;
3517 rc = func( newDN, flags, NULL );
3518 if ( rc != LDAP_SUCCESS )
3522 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3525 for (;baseAVA < newAVA; baseAVA++) {
3526 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3527 LDAP_FREE( baseAVA->la_attr.bv_val );
3528 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3529 LDAP_FREE( baseAVA->la_value.bv_val );
3533 LDAP_FREE( oidbuf );
3534 if ( newDN != (LDAPDN) ptrs )
3538 #endif /* HAVE_TLS */