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" */
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 )
93 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
95 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
99 assert( LDAP_VALID(ld) );
100 assert( entry != NULL );
102 tmp = *entry->lm_ber; /* struct copy */
103 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
104 ld->ld_errno = LDAP_DECODING_ERROR;
112 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
115 BerElement tmp, *ber;
117 int rc = LDAP_SUCCESS;
120 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
122 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
125 assert( ld != NULL );
126 assert( LDAP_VALID(ld) );
127 assert( entry != NULL );
128 assert( dn != NULL );
135 ber = ldap_alloc_ber_with_options( ld );
137 return LDAP_NO_MEMORY;
144 *ber = *entry->lm_ber; /* struct copy */
145 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
146 rc = ld->ld_errno = LDAP_DECODING_ERROR;
148 if ( rc == LDAP_SUCCESS ) {
149 /* set the length to avoid overrun */
150 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
151 if( rc != LBER_OPT_SUCCESS ) {
152 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
155 if ( rc != LDAP_SUCCESS && berout ) {
163 * RFC 1823 ldap_dn2ufn
166 ldap_dn2ufn( LDAP_CONST char *dn )
171 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
173 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
176 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
177 &out, LDAP_DN_FORMAT_UFN );
183 * RFC 1823 ldap_explode_dn
186 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
189 char **values = NULL;
191 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
194 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
196 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
199 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
204 if( tmpDN == NULL ) {
205 values = LDAP_MALLOC( sizeof( char * ) );
206 if( values == NULL ) return NULL;
212 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
214 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
215 if ( values == NULL ) {
216 ldap_dnfree( tmpDN );
220 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
221 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
223 ldap_dnfree( tmpDN );
224 values[ iRDN ] = NULL;
230 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
233 char **values = NULL;
238 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
240 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
244 * we only parse the first rdn
245 * FIXME: we prefer efficiency over checking if the _ENTIRE_
248 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
253 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
254 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
255 if ( values == NULL ) {
256 ldap_rdnfree( tmpRDN );
260 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
261 ber_len_t l = 0, vl, al = 0;
263 LDAPAVA *ava = tmpRDN[ iAVA ];
265 if ( ava->la_flags & LDAP_AVA_BINARY ) {
266 vl = 1 + 2 * ava->la_value.bv_len;
269 if ( strval2strlen( &ava->la_value,
270 ava->la_flags, &vl ) ) {
276 al = ava->la_attr.bv_len;
277 l = vl + ava->la_attr.bv_len + 1;
279 str = LDAP_MALLOC( l + 1 );
280 AC_MEMCPY( str, ava->la_attr.bv_val,
281 ava->la_attr.bv_len );
286 str = LDAP_MALLOC( l + 1 );
289 if ( ava->la_flags & LDAP_AVA_BINARY ) {
291 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
296 if ( strval2str( &ava->la_value, &str[ al ],
297 ava->la_flags, &vl ) ) {
303 values[ iAVA ] = str;
305 values[ iAVA ] = NULL;
307 ldap_rdnfree( tmpRDN );
312 LBER_VFREE( values );
313 ldap_rdnfree( tmpRDN );
318 ldap_dn2dcedn( LDAP_CONST char *dn )
323 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
325 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
328 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
329 &out, LDAP_DN_FORMAT_DCE );
335 ldap_dcedn2dn( LDAP_CONST char *dce )
340 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
342 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
345 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
351 ldap_dn2ad_canonical( LDAP_CONST char *dn )
356 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
358 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
361 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
362 &out, LDAP_DN_FORMAT_AD_CANONICAL );
368 * function that changes the string representation of dnin
369 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
372 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
373 * plus some rfc 1779)
374 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
375 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
376 * LDAP_DN_FORMAT_DCE (?)
378 * fout can be any of the above except
379 * LDAP_DN_FORMAT_LDAP
381 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
382 * LDAP_DN_FORMAT_AD_CANONICAL (?)
385 ldap_dn_normalize( LDAP_CONST char *dnin,
386 unsigned fin, char **dnout, unsigned fout )
392 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
394 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
401 if ( dnin == NULL ) {
402 return( LDAP_SUCCESS );
405 rc = ldap_str2dn( dnin , &tmpDN, fin );
406 if ( rc != LDAP_SUCCESS ) {
410 rc = ldap_dn2str( tmpDN, dnout, fout );
412 ldap_dnfree( tmpDN );
420 /* #define B4ATTRTYPE 0x0001 */
421 #define B4OIDATTRTYPE 0x0002
422 #define B4STRINGATTRTYPE 0x0003
424 #define B4AVAEQUALS 0x0100
425 #define B4AVASEP 0x0200
426 #define B4RDNSEP 0x0300
427 #define GOTAVA 0x0400
429 #define B4ATTRVALUE 0x0010
430 #define B4STRINGVALUE 0x0020
431 #define B4IA5VALUEQUOTED 0x0030
432 #define B4IA5VALUE 0x0040
433 #define B4BINARYVALUE 0x0050
436 * Helpers (mostly from slap.h)
437 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
438 * Macros assume "C" Locale (ASCII)
440 #define LDAP_DN_ASCII_SPACE(c) \
441 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
442 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
443 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
444 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
446 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
447 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
448 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
449 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
450 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
451 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
454 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
455 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
456 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
457 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
458 #define LDAP_DN_ATTRDESC_CHAR(c) \
459 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
461 /* special symbols */
462 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
463 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
464 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
465 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
466 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
467 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
468 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
469 #define LDAP_DN_VALUE_END(c) \
470 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
472 /* NOTE: according to draft-ietf-ldapbis-dn, '=' can be escaped
473 * and treated as special, i.e. escaped both as "\<hexpair>" and
474 * as "\=", but it is treated as a regular char, i.e. it can also
477 * As such, we currently choose to allow reading unescaped '=',
478 * but we always produce escaped '\3D'; this may change in the
479 * future, if compatibility issues do not arise */
481 #define LDAP_DN_NE(c) \
482 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
483 || LDAP_DN_QUOTES(c) \
484 || (c) == '<' || (c) == '>' )
485 #define LDAP_DN_MAYESCAPE(c) \
486 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
487 || LDAP_DN_AVA_EQUALS(c) \
488 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
489 #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
490 #else /* ! LDAP_DEVEL */
491 #define LDAP_DN_NE(c) \
492 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
493 || LDAP_DN_AVA_EQUALS(c) || LDAP_DN_QUOTES(c) \
494 || (c) == '<' || (c) == '>' )
495 #define LDAP_DN_MAYESCAPE(c) \
496 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
497 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
498 #define LDAP_DN_SHOULDESCAPE(c) ( 0 )
499 #endif /* ! LDAP_DEVEL */
501 #define LDAP_DN_NEEDESCAPE(c) \
502 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
503 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
504 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
505 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
506 #define LDAP_DN_WILLESCAPE_CHAR(c) \
507 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
508 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
509 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
510 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
513 #define LDAP_DN_VALUE_END_V2(c) \
514 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
516 #define LDAP_DN_V2_SPECIAL(c) \
517 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
518 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
519 || LDAP_DN_OCTOTHORPE(c) )
520 #define LDAP_DN_V2_PAIR(c) \
521 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
524 * DCE (mostly from Luke Howard and IBM implementation for AIX)
526 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
527 * Here escapes and valid chars for GDS are considered; as soon as more
528 * specific info is found, the macros will be updated.
530 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
531 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
533 * Metachars: '/', ',', '=', '\'.
535 * the '\' is used to escape other metachars.
541 * Attribute types must start with alphabetic chars and can contain
542 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
544 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
545 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
546 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
547 #define LDAP_DN_VALUE_END_DCE(c) \
548 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
549 #define LDAP_DN_NEEDESCAPE_DCE(c) \
550 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
553 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
554 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
555 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
556 #define LDAP_DN_VALUE_END_AD(c) \
557 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
558 #define LDAP_DN_NEEDESCAPE_AD(c) \
559 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
562 #define LDAP_DN_HEXPAIR(s) \
563 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
564 /* better look at the AttributeDescription? */
566 /* FIXME: no composite rdn or non-"dc" types, right?
567 * (what about "dc" in OID form?) */
568 /* FIXME: we do not allow binary values in domain, right? */
569 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
570 /* NOTE: don't use strcasecmp() as it is locale specific! */
571 #define LDAP_DC_ATTR "dc"
572 #define LDAP_DC_ATTRU "DC"
573 #define LDAP_DN_IS_RDN_DC( r ) \
574 ( (r) && (r)[0] && !(r)[1] \
575 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
576 && ((r)[0]->la_attr.bv_len == 2) \
577 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
578 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
579 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
580 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
582 /* Composite rules */
583 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
584 ( LDAP_DN_LDAPV2(f) \
585 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
586 #define LDAP_DN_ALLOW_SPACES(f) \
587 ( LDAP_DN_LDAPV2(f) \
588 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
589 #define LDAP_DN_LDAP(f) \
590 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
591 #define LDAP_DN_LDAPV3(f) \
592 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
593 #define LDAP_DN_LDAPV2(f) \
594 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
595 #define LDAP_DN_DCE(f) \
596 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
597 #define LDAP_DN_UFN(f) \
598 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
599 #define LDAP_DN_ADC(f) \
600 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
601 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
604 * LDAPAVA helpers (will become part of the API for operations
605 * on structural representations of DNs).
608 ldapava_new( const struct berval *attr, const struct berval *val,
609 unsigned flags, void *ctx )
616 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
619 ava->la_attr.bv_len = attr->bv_len;
620 ava->la_attr.bv_val = (char *)(ava+1);
621 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
622 ava->la_attr.bv_val[attr->bv_len] = '\0';
624 ava->la_value = *val;
625 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
627 ava->la_private = NULL;
634 ldapava_free( LDAPAVA *ava, void *ctx )
639 /* ava's private must be freed by caller
640 * (at present let's skip this check because la_private
641 * basically holds static data) */
642 assert( ava->la_private == NULL );
645 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
646 LDAP_FREEX( ava->la_value.bv_val, ctx );
648 LDAP_FREEX( ava, ctx );
652 ldap_rdnfree( LDAPRDN rdn )
654 ldap_rdnfree_x( rdn, NULL );
658 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
666 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
667 ldapava_free( rdn[ iAVA ], ctx );
670 LDAP_FREEX( rdn, ctx );
674 ldap_dnfree( LDAPDN dn )
676 ldap_dnfree_x( dn, NULL );
680 ldap_dnfree_x( LDAPDN dn, void *ctx )
688 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
689 ldap_rdnfree_x( dn[ iRDN ], ctx );
692 LDAP_FREEX( dn, ctx );
696 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
697 * into a structural representation of the DN, by separating attribute
698 * types and values encoded in the more appropriate form, which is
699 * string or OID for attribute types and binary form of the BER encoded
700 * value or Unicode string. Formats different from LDAPv3 are parsed
701 * according to their own rules and turned into the more appropriate
702 * form according to LDAPv3.
704 * NOTE: I realize the code is getting spaghettish; it is rather
705 * experimental and will hopefully turn into something more simple
706 * and readable as soon as it works as expected.
710 * Default sizes of AVA and RDN static working arrays; if required
711 * the are dynamically resized. The values can be tuned in case
712 * of special requirements (e.g. very deep DN trees or high number
715 #define TMP_AVA_SLOTS 8
716 #define TMP_RDN_SLOTS 32
719 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
725 bv.bv_len = strlen( str );
726 bv.bv_val = (char *) str;
728 return ldap_bv2dn_x( &bv, dn, flags, NULL );
732 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
734 return ldap_bv2dn_x( bv, dn, flags, NULL );
738 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
741 int rc = LDAP_DECODING_ERROR;
745 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
746 int num_slots = TMP_RDN_SLOTS;
748 struct berval bvtmp, *bv = &bvtmp;
751 assert( bvin->bv_val );
756 end = str + bv->bv_len;
759 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n", str, flags, 0 );
761 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
766 switch ( LDAP_DN_FORMAT( flags ) ) {
767 case LDAP_DN_FORMAT_LDAP:
768 case LDAP_DN_FORMAT_LDAPV3:
769 case LDAP_DN_FORMAT_DCE:
772 /* allow DN enclosed in brackets */
773 case LDAP_DN_FORMAT_LDAPV2:
774 if ( str[0] == '<' ) {
775 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
776 rc = LDAP_DECODING_ERROR;
786 /* unsupported in str2dn */
787 case LDAP_DN_FORMAT_UFN:
788 case LDAP_DN_FORMAT_AD_CANONICAL:
789 return LDAP_PARAM_ERROR;
791 case LDAP_DN_FORMAT_LBER:
793 return LDAP_PARAM_ERROR;
796 if ( bv->bv_len == 0 ) {
800 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
801 /* value must have embedded NULs */
802 return LDAP_DECODING_ERROR;
806 if ( LDAP_DN_DCE( flags ) ) {
809 * (from Luke Howard: thnx) A RDN separator is required
810 * at the beginning of an (absolute) DN.
812 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
818 * actually we do not want to accept by default the DCE form,
819 * we do not want to auto-detect it
822 } else if ( LDAP_DN_LDAP( flags ) ) {
824 * if dn starts with '/' let's make it a DCE dn
826 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
827 flags |= LDAP_DN_FORMAT_DCE;
833 for ( ; p < end; p++ ) {
836 tmpbv.bv_len = bv->bv_len - ( p - str );
837 tmpbv.bv_val = (char *)p;
839 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
840 if ( err != LDAP_SUCCESS ) {
845 * We expect a rdn separator
847 if ( p < end && p[ 0 ] ) {
848 switch ( LDAP_DN_FORMAT( flags ) ) {
849 case LDAP_DN_FORMAT_LDAPV3:
850 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
851 rc = LDAP_DECODING_ERROR;
856 case LDAP_DN_FORMAT_LDAP:
857 case LDAP_DN_FORMAT_LDAPV2:
858 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
859 rc = LDAP_DECODING_ERROR;
864 case LDAP_DN_FORMAT_DCE:
865 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
866 rc = LDAP_DECODING_ERROR;
874 tmpDN[nrdns++] = newRDN;
878 * make the static RDN array dynamically rescalable
880 if ( nrdns == num_slots ) {
883 if ( tmpDN == tmpDN_ ) {
884 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
889 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
892 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
903 if ( p >= end || p[ 0 ] == '\0' ) {
905 * the DN is over, phew
907 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
908 if ( newDN == NULL ) {
914 if ( LDAP_DN_DCE( flags ) ) {
915 /* add in reversed order */
916 for ( i=0; i<nrdns; i++ )
917 newDN[i] = tmpDN[nrdns-1-i];
919 for ( i=0; i<nrdns; i++ )
931 ldap_rdnfree_x( newRDN, ctx );
934 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
935 ldap_rdnfree_x( tmpDN[nrdns], ctx );
940 if ( tmpDN != tmpDN_ ) {
941 LDAP_FREEX( tmpDN, ctx );
945 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s)=%d %s\n",
946 str, rc, ldap_err2string( rc ) );
948 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
949 ldap_err2string( rc ) );
959 * Parses a relative DN according to flags up to a rdn separator
960 * or to the end of str.
961 * Returns the rdn and a pointer to the string continuation, which
962 * corresponds to the rdn separator or to '\0' in case the string is over.
965 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
966 char **n_in, unsigned flags )
971 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
973 bv.bv_len = strlen( str );
974 bv.bv_val = (char *) str;
976 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
980 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
981 char **n_in, unsigned flags )
983 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
987 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
988 char **n_in, unsigned flags, void *ctx )
990 const char **n = (const char **) n_in;
994 int rc = LDAP_DECODING_ERROR;
995 int attrTypeEncoding = LDAP_AVA_STRING,
996 attrValueEncoding = LDAP_AVA_STRING;
998 struct berval attrType = BER_BVNULL;
999 struct berval attrValue = BER_BVNULL;
1001 LDAPRDN newRDN = NULL;
1002 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
1003 int num_slots = TMP_AVA_SLOTS;
1009 assert( bv->bv_len );
1010 assert( bv->bv_val );
1011 assert( rdn || flags & LDAP_DN_SKIP );
1015 stoplen = bv->bv_len;
1022 switch ( LDAP_DN_FORMAT( flags ) ) {
1023 case LDAP_DN_FORMAT_LDAP:
1024 case LDAP_DN_FORMAT_LDAPV3:
1025 case LDAP_DN_FORMAT_LDAPV2:
1026 case LDAP_DN_FORMAT_DCE:
1029 /* unsupported in str2dn */
1030 case LDAP_DN_FORMAT_UFN:
1031 case LDAP_DN_FORMAT_AD_CANONICAL:
1032 return LDAP_PARAM_ERROR;
1034 case LDAP_DN_FORMAT_LBER:
1036 return LDAP_PARAM_ERROR;
1039 if ( bv->bv_len == 0 ) {
1040 return LDAP_SUCCESS;
1044 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1045 /* value must have embedded NULs */
1046 return LDAP_DECODING_ERROR;
1050 for ( ; p[ 0 ] || state == GOTAVA; ) {
1053 * The parser in principle advances one token a time,
1054 * or toggles state if preferable.
1059 * an AttributeType can be encoded as:
1060 * - its string representation; in detail, implementations
1061 * MUST recognize AttributeType string type names listed
1062 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1063 * MAY recognize other names.
1064 * - its numeric OID (a dotted decimal string); in detail
1065 * RFC 2253 asserts that ``Implementations MUST allow
1066 * an oid in the attribute type to be prefixed by one
1067 * of the character strings "oid." or "OID."''. As soon
1068 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1069 * I'm not sure whether this is required or not any
1070 * longer; to be liberal, we still implement it.
1073 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1074 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1081 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1082 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1087 /* whitespace is allowed (and trimmed) */
1089 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1094 /* error: we expected an AVA */
1100 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1101 state = B4OIDATTRTYPE;
1105 /* else must be alpha */
1106 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1110 /* LDAPv2 "oid." prefix */
1111 if ( LDAP_DN_LDAPV2( flags ) ) {
1113 * to be overly pedantic, we only accept
1116 if ( flags & LDAP_DN_PEDANTIC ) {
1117 if ( !strncmp( p, "OID.", 4 )
1118 || !strncmp( p, "oid.", 4 ) ) {
1120 state = B4OIDATTRTYPE;
1124 if ( !strncasecmp( p, "oid.", 4 ) ) {
1126 state = B4OIDATTRTYPE;
1132 state = B4STRINGATTRTYPE;
1135 case B4OIDATTRTYPE: {
1136 int err = LDAP_SUCCESS;
1138 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1141 if ( err != LDAP_SUCCESS ) {
1144 attrType.bv_len = p - attrType.bv_val;
1146 attrTypeEncoding = LDAP_AVA_BINARY;
1148 state = B4AVAEQUALS;
1152 case B4STRINGATTRTYPE: {
1153 const char *startPos, *endPos = NULL;
1157 * the starting char has been found to be
1158 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1159 * FIXME: DCE attr types seem to have a more
1160 * restrictive syntax (no '-' ...)
1162 for ( startPos = p++; p[ 0 ]; p++ ) {
1163 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1167 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1170 * RFC 2253 does not explicitly
1171 * allow lang extensions to attribute
1174 if ( flags & LDAP_DN_PEDANTIC ) {
1179 * we trim ';' and following lang
1180 * and so from attribute types
1183 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1184 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1192 len = ( endPos ? endPos : p ) - startPos;
1197 attrTypeEncoding = LDAP_AVA_STRING;
1200 * here we need to decide whether to use it as is
1201 * or turn it in OID form; as a consequence, we
1202 * need to decide whether to binary encode the value
1205 state = B4AVAEQUALS;
1207 if ( flags & LDAP_DN_SKIP ) {
1211 attrType.bv_val = (char *)startPos;
1212 attrType.bv_len = len;
1218 /* spaces may not be allowed */
1219 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1220 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1225 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1230 /* need equal sign */
1231 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1236 /* spaces may not be allowed */
1237 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1238 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1243 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1249 * octothorpe means a BER encoded value will follow
1250 * FIXME: I don't think DCE will allow it
1252 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1254 attrValueEncoding = LDAP_AVA_BINARY;
1255 state = B4BINARYVALUE;
1259 /* STRING value expected */
1262 * if we're pedantic, an attribute type in OID form
1263 * SHOULD imply a BER encoded attribute value; we
1264 * should at least issue a warning
1266 if ( ( flags & LDAP_DN_PEDANTIC )
1267 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1268 /* OID attrType SHOULD use binary encoding */
1272 attrValueEncoding = LDAP_AVA_STRING;
1275 * LDAPv2 allows the attribute value to be quoted;
1276 * also, IA5 values are expected, in principle
1278 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1279 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1281 state = B4IA5VALUEQUOTED;
1285 if ( LDAP_DN_LDAPV2( flags ) ) {
1292 * here STRING means RFC 2253 string
1293 * FIXME: what about DCE strings?
1299 state = B4STRINGVALUE;
1304 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1312 switch ( LDAP_DN_FORMAT( flags ) ) {
1313 case LDAP_DN_FORMAT_LDAP:
1314 case LDAP_DN_FORMAT_LDAPV3:
1315 if ( str2strval( p, stoplen - ( p - str ),
1316 &attrValue, &p, flags,
1317 &attrValueEncoding, ctx ) ) {
1322 case LDAP_DN_FORMAT_DCE:
1323 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1336 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1343 case B4IA5VALUEQUOTED:
1345 /* lead quote already stripped */
1346 if ( quotedIA52strval( p, &attrValue,
1347 &p, flags, ctx ) ) {
1357 if ( !( flags & LDAP_DN_SKIP ) ) {
1361 * we accept empty values
1363 ava = ldapava_new( &attrType, &attrValue,
1364 attrValueEncoding, ctx );
1365 if ( ava == NULL ) {
1366 rc = LDAP_NO_MEMORY;
1369 tmpRDN[navas++] = ava;
1371 attrValue.bv_val = NULL;
1372 attrValue.bv_len = 0;
1375 * prepare room for new AVAs if needed
1377 if (navas == num_slots) {
1380 if ( tmpRDN == tmpRDN_ ) {
1381 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1382 if ( tmp == NULL ) {
1383 rc = LDAP_NO_MEMORY;
1386 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1389 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1390 if ( tmp == NULL ) {
1391 rc = LDAP_NO_MEMORY;
1402 * if we got an AVA separator ('+', or ',' for DCE )
1403 * we expect a new AVA for this RDN; otherwise
1404 * we add the RDN to the DN
1406 switch ( LDAP_DN_FORMAT( flags ) ) {
1407 case LDAP_DN_FORMAT_LDAP:
1408 case LDAP_DN_FORMAT_LDAPV3:
1409 case LDAP_DN_FORMAT_LDAPV2:
1410 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1415 case LDAP_DN_FORMAT_DCE:
1416 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1424 * the RDN is over, phew
1427 if ( !( flags & LDAP_DN_SKIP ) ) {
1428 newRDN = (LDAPRDN)LDAP_MALLOCX(
1429 sizeof(LDAPAVA) * (navas+1), ctx );
1430 if ( newRDN == NULL ) {
1431 rc = LDAP_NO_MEMORY;
1434 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1435 newRDN[navas] = NULL;
1443 /* they should have been used in an AVA */
1444 attrType.bv_val = NULL;
1445 attrValue.bv_val = NULL;
1460 /* They are set to NULL after they're used in an AVA */
1462 if ( attrValue.bv_val ) {
1463 LDAP_FREEX( attrValue.bv_val, ctx );
1466 for ( navas-- ; navas >= 0; navas-- ) {
1467 ldapava_free( tmpRDN[navas], ctx );
1472 if ( tmpRDN != tmpRDN_ ) {
1473 LDAP_FREEX( tmpRDN, ctx );
1484 * reads in a UTF-8 string value, unescaping stuff:
1485 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1486 * '\' + HEXPAIR(p) -> unhex(p)
1489 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1491 const char *p, *end, *startPos, *endPos = NULL;
1492 ber_len_t len, escapes;
1499 end = str + stoplen;
1500 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1501 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1503 if ( p[ 0 ] == '\0' ) {
1506 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1511 if ( LDAP_DN_HEXPAIR( p ) ) {
1514 hexstr2bin( p, &c );
1517 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1520 * we assume the string is UTF-8
1522 *retFlags = LDAP_AVA_NONPRINTABLE;
1529 if ( LDAP_DN_PEDANTIC & flags ) {
1533 * we do not allow escaping
1534 * of chars that don't need
1535 * to and do not belong to
1540 } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1541 if ( p[ 0 ] == '\0' ) {
1544 *retFlags = LDAP_AVA_NONPRINTABLE;
1546 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1547 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1550 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1552 * FIXME: maybe we can add
1553 * escapes if not pedantic?
1560 * we do allow unescaped spaces at the end
1561 * of the value only in non-pedantic mode
1563 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1564 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1565 if ( flags & LDAP_DN_PEDANTIC ) {
1569 /* strip trailing (unescaped) spaces */
1570 for ( endPos = p - 1;
1571 endPos > startPos + 1 &&
1572 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1573 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1580 if ( flags & LDAP_DN_SKIP ) {
1585 * FIXME: test memory?
1587 len = ( endPos ? endPos : p ) - startPos - escapes;
1590 if ( escapes == 0 ) {
1591 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1592 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1593 AC_MEMCPY( val->bv_val, startPos, len );
1594 val->bv_val[ len ] = '\0';
1596 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1602 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1603 for ( s = 0, d = 0; d < len; ) {
1604 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1606 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1607 val->bv_val[ d++ ] =
1610 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1613 hexstr2bin( &startPos[ s ], &c );
1614 val->bv_val[ d++ ] = c;
1618 /* we should never get here */
1623 val->bv_val[ d++ ] = startPos[ s++ ];
1627 val->bv_val[ d ] = '\0';
1635 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1637 const char *p, *startPos, *endPos = NULL;
1638 ber_len_t len, escapes;
1646 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1647 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1649 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1656 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1661 * FIXME: can we accept anything else? I guess we need
1662 * to stop if a value is not legal
1667 * (unescaped) trailing spaces are trimmed must be silently ignored;
1670 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1671 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1672 if ( flags & LDAP_DN_PEDANTIC ) {
1676 /* strip trailing (unescaped) spaces */
1677 for ( endPos = p - 1;
1678 endPos > startPos + 1 &&
1679 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1680 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1687 if ( flags & LDAP_DN_SKIP ) {
1691 len = ( endPos ? endPos : p ) - startPos - escapes;
1693 if ( escapes == 0 ){
1694 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1699 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1700 for ( s = 0, d = 0; d < len; ) {
1702 * This point is reached only if escapes
1703 * are properly used, so all we need to
1706 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1710 val->bv_val[ d++ ] = startPos[ s++ ];
1712 val->bv_val[ d ] = '\0';
1713 assert( strlen( val->bv_val ) == len );
1720 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1722 const char *p, *startPos, *endPos = NULL;
1723 ber_len_t len, escapes;
1735 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1736 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1738 if ( p[ 0 ] == '\0' ) {
1742 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1743 && ( LDAP_DN_PEDANTIC & flags ) ) {
1748 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1753 * FIXME: can we accept anything else? I guess we need
1754 * to stop if a value is not legal
1758 /* strip trailing (unescaped) spaces */
1760 endPos > startPos + 1 &&
1761 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1762 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1768 if ( flags & LDAP_DN_SKIP ) {
1772 len = ( endPos ? endPos : p ) - startPos - escapes;
1774 if ( escapes == 0 ) {
1775 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1780 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1781 for ( s = 0, d = 0; d < len; ) {
1782 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1785 val->bv_val[ d++ ] = startPos[ s++ ];
1787 val->bv_val[ d ] = '\0';
1788 assert( strlen( val->bv_val ) == len );
1795 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1797 const char *p, *startPos, *endPos = NULL;
1799 unsigned escapes = 0;
1807 /* initial quote already eaten */
1808 for ( startPos = p = str; p[ 0 ]; p++ ) {
1810 * According to RFC 1779, the quoted value can
1811 * contain escaped as well as unescaped special values;
1812 * as a consequence we tolerate escaped values
1813 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1814 * (e.g. '","' -> '\,').
1816 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1817 if ( p[ 1 ] == '\0' ) {
1822 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1823 && ( LDAP_DN_PEDANTIC & flags ) ) {
1825 * do we allow to escape normal chars?
1826 * LDAPv2 does not allow any mechanism
1827 * for escaping chars with '\' and hex
1834 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1836 /* eat closing quotes */
1842 * FIXME: can we accept anything else? I guess we need
1843 * to stop if a value is not legal
1847 if ( endPos == NULL ) {
1851 /* Strip trailing (unescaped) spaces */
1852 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1857 if ( flags & LDAP_DN_SKIP ) {
1861 len = endPos - startPos - escapes;
1862 assert( endPos >= startPos + escapes );
1864 if ( escapes == 0 ) {
1865 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1870 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1873 for ( s = d = 0; d < len; ) {
1874 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1877 val->bv_val[ d++ ] = str[ s++ ];
1879 val->bv_val[ d ] = '\0';
1880 assert( strlen( val->bv_val ) == len );
1887 hexstr2bin( const char *str, char *c )
1897 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1901 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1904 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1911 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1915 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1916 *c += c2 - 'A' + 10;
1918 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1919 *c += c2 - 'a' + 10;
1927 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1929 const char *p, *startPos, *endPos = NULL;
1939 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1940 switch ( LDAP_DN_FORMAT( flags ) ) {
1941 case LDAP_DN_FORMAT_LDAPV3:
1942 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1947 case LDAP_DN_FORMAT_LDAP:
1948 case LDAP_DN_FORMAT_LDAPV2:
1949 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1954 case LDAP_DN_FORMAT_DCE:
1955 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1961 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1962 if ( flags & LDAP_DN_PEDANTIC ) {
1967 for ( ; p[ 0 ]; p++ ) {
1968 switch ( LDAP_DN_FORMAT( flags ) ) {
1969 case LDAP_DN_FORMAT_LDAPV3:
1970 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1975 case LDAP_DN_FORMAT_LDAP:
1976 case LDAP_DN_FORMAT_LDAPV2:
1977 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1982 case LDAP_DN_FORMAT_DCE:
1983 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1992 if ( !LDAP_DN_HEXPAIR( p ) ) {
2000 if ( flags & LDAP_DN_SKIP ) {
2004 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2006 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
2009 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
2010 if ( val->bv_val == NULL ) {
2011 return( LDAP_NO_MEMORY );
2014 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2017 hexstr2bin( &startPos[ s ], &c );
2019 val->bv_val[ d ] = c;
2022 val->bv_val[ d ] = '\0';
2028 * convert a byte in a hexadecimal pair
2031 byte2hexpair( const char *val, char *pair )
2033 static const char hexdig[] = "0123456789ABCDEF";
2039 * we assume the string has enough room for the hex encoding
2043 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2044 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2050 * convert a binary value in hexadecimal pairs
2053 binval2hexstr( struct berval *val, char *str )
2060 if ( val->bv_len == 0 ) {
2065 * we assume the string has enough room for the hex encoding
2069 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2070 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2077 * Length of the string representation, accounting for escaped hex
2081 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2083 ber_len_t l, cl = 1;
2085 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2086 #ifdef PRETTY_ESCAPE
2087 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2088 #endif /* PRETTY_ESCAPE */
2094 if ( val->bv_len == 0 ) {
2098 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2103 if ( p[ 0 ] == '\0' ) {
2109 cl = LDAP_UTF8_CHARLEN2( p, cl );
2111 /* illegal utf-8 char! */
2114 } else if ( cl > 1 ) {
2117 for ( cnt = 1; cnt < cl; cnt++ ) {
2118 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2122 l += escaped_byte_len * cl;
2124 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2125 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2126 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2127 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2128 #ifdef PRETTY_ESCAPE
2130 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2132 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2136 * there might be some chars we want
2137 * to escape in form of a couple
2138 * of hexdigits for optimization purposes
2143 l += escaped_ascii_len;
2145 #else /* ! PRETTY_ESCAPE */
2147 #endif /* ! PRETTY_ESCAPE */
2160 * convert to string representation, escaping with hex the UTF-8 stuff;
2161 * assume the destination has enough room for escaping
2164 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2166 ber_len_t s, d, end;
2172 if ( val->bv_len == 0 ) {
2178 * we assume the string has enough room for the hex encoding
2181 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2187 if ( val->bv_val[ s ] == '\0' ) {
2197 * The length was checked in strval2strlen();
2198 * LDAP_UTF8_CHARLEN() should suffice
2200 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2204 * there might be some chars we want to escape in form
2205 * of a couple of hexdigits for optimization purposes
2207 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2208 #ifdef PRETTY_ESCAPE
2210 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2212 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2214 #else /* ! PRETTY_ESCAPE */
2215 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2216 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2217 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2218 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2220 #endif /* ! PRETTY_ESCAPE */
2224 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2229 } else if ( cl > 1 ) {
2231 str[ d++ ] = val->bv_val[ s++ ];
2235 #ifdef PRETTY_ESCAPE
2236 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2237 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2238 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2239 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2241 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2242 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2248 #endif /* PRETTY_ESCAPE */
2249 str[ d++ ] = val->bv_val[ s++ ];
2259 * Length of the IA5 string representation (no UTF-8 allowed)
2262 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2271 if ( val->bv_len == 0 ) {
2275 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2277 * Turn value into a binary encoded BER
2282 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2283 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2284 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2285 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2286 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2301 * convert to string representation (np UTF-8)
2302 * assume the destination has enough room for escaping
2305 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2307 ber_len_t s, d, end;
2313 if ( val->bv_len == 0 ) {
2318 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2320 * Turn value into a binary encoded BER
2327 * we assume the string has enough room for the hex encoding
2331 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2332 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2333 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2334 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2335 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2338 str[ d++ ] = val->bv_val[ s++ ];
2348 * Length of the (supposedly) DCE string representation,
2349 * accounting for escaped hex of UTF-8 chars
2352 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2361 if ( val->bv_len == 0 ) {
2365 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2367 * FIXME: Turn the value into a binary encoded BER?
2372 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2373 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2388 * convert to (supposedly) DCE string representation,
2389 * escaping with hex the UTF-8 stuff;
2390 * assume the destination has enough room for escaping
2393 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2401 if ( val->bv_len == 0 ) {
2406 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2408 * FIXME: Turn the value into a binary encoded BER?
2416 * we assume the string has enough room for the hex encoding
2420 for ( s = 0, d = 0; s < val->bv_len; ) {
2421 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2424 str[ d++ ] = val->bv_val[ s++ ];
2434 * Length of the (supposedly) AD canonical string representation,
2435 * accounting for escaped hex of UTF-8 chars
2438 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2447 if ( val->bv_len == 0 ) {
2451 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2453 * FIXME: Turn the value into a binary encoded BER?
2458 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2459 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2474 * convert to (supposedly) AD string representation,
2475 * escaping with hex the UTF-8 stuff;
2476 * assume the destination has enough room for escaping
2479 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2487 if ( val->bv_len == 0 ) {
2492 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2494 * FIXME: Turn the value into a binary encoded BER?
2502 * we assume the string has enough room for the hex encoding
2506 for ( s = 0, d = 0; s < val->bv_len; ) {
2507 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2510 str[ d++ ] = val->bv_val[ s++ ];
2520 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2521 * the first part of the AD representation of the DN is written in DNS
2522 * form, i.e. dot separated domain name components (as suggested
2523 * by Luke Howard, http://www.padl.com/~lukeh)
2526 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2529 int domain = 0, first = 1;
2530 ber_len_t l = 1; /* we move the null also */
2533 /* we are guaranteed there's enough memory in str */
2539 assert( *iRDN >= 0 );
2541 str = bv->bv_val + pos;
2543 for ( i = *iRDN; i >= 0; i-- ) {
2553 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2561 AC_MEMCPY( str, ava->la_value.bv_val,
2562 ava->la_value.bv_len + 1);
2563 l += ava->la_value.bv_len;
2566 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2567 AC_MEMCPY( str, ava->la_value.bv_val,
2568 ava->la_value.bv_len );
2569 str[ ava->la_value.bv_len ] = '.';
2570 l += ava->la_value.bv_len + 1;
2575 bv->bv_len = pos + l - 1;
2581 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2582 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2589 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2590 LDAPAVA *ava = rdn[ iAVA ];
2592 /* len(type) + '=' + '+' | ',' */
2593 l += ava->la_attr.bv_len + 2;
2595 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2596 /* octothorpe + twice the length */
2597 l += 1 + 2 * ava->la_value.bv_len;
2601 unsigned f = flags | ava->la_flags;
2603 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2616 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2617 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2622 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2623 LDAPAVA *ava = rdn[ iAVA ];
2625 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2626 ava->la_attr.bv_len );
2627 l += ava->la_attr.bv_len;
2631 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2633 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2636 l += 2 * ava->la_value.bv_len;
2640 unsigned f = flags | ava->la_flags;
2642 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2647 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2656 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2663 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2664 LDAPAVA *ava = rdn[ iAVA ];
2666 /* len(type) + '=' + ',' | '/' */
2667 l += ava->la_attr.bv_len + 2;
2669 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2670 /* octothorpe + twice the length */
2671 l += 1 + 2 * ava->la_value.bv_len;
2674 unsigned f = flags | ava->la_flags;
2676 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2689 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2694 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2695 LDAPAVA *ava = rdn[ iAVA ];
2700 str[ l++ ] = ( iAVA ? ',' : '/' );
2703 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2704 ava->la_attr.bv_len );
2705 l += ava->la_attr.bv_len;
2709 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2711 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2714 l += 2 * ava->la_value.bv_len;
2717 unsigned f = flags | ava->la_flags;
2719 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2732 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2742 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2743 LDAPAVA *ava = rdn[ iAVA ];
2746 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2748 /* FIXME: are binary values allowed in UFN? */
2749 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2750 /* octothorpe + twice the value */
2751 l += 1 + 2 * ava->la_value.bv_len;
2755 unsigned f = flags | ava->la_flags;
2757 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2770 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2775 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2776 LDAPAVA *ava = rdn[ iAVA ];
2778 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2780 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2783 l += 2 * ava->la_value.bv_len;
2787 unsigned f = flags | ava->la_flags;
2789 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2795 if ( rdn[ iAVA + 1 ] ) {
2796 AC_MEMCPY( &str[ l ], " + ", 3 );
2800 AC_MEMCPY( &str[ l ], ", ", 2 );
2811 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2821 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2822 LDAPAVA *ava = rdn[ iAVA ];
2827 /* FIXME: are binary values allowed in UFN? */
2828 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2829 /* octothorpe + twice the value */
2830 l += 1 + 2 * ava->la_value.bv_len;
2833 unsigned f = flags | ava->la_flags;
2835 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2848 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2853 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2854 LDAPAVA *ava = rdn[ iAVA ];
2859 str[ l++ ] = ( iAVA ? ',' : '/' );
2862 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2864 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2867 l += 2 * ava->la_value.bv_len;
2870 unsigned f = flags | ava->la_flags;
2872 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2887 * Returns in str a string representation of rdn based on flags.
2888 * There is some duplication of code between this and ldap_dn2str;
2889 * this is wanted to reduce the allocation of temporary buffers.
2892 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2899 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2900 return LDAP_PARAM_ERROR;
2903 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2909 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2911 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2915 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2925 if ( rdn == NULL ) {
2926 bv->bv_val = LDAP_STRDUPX( "", ctx );
2927 return( LDAP_SUCCESS );
2931 * This routine wastes "back" bytes at the end of the string
2934 switch ( LDAP_DN_FORMAT( flags ) ) {
2935 case LDAP_DN_FORMAT_LDAPV3:
2936 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2937 return LDAP_DECODING_ERROR;
2941 case LDAP_DN_FORMAT_LDAPV2:
2942 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2943 return LDAP_DECODING_ERROR;
2947 case LDAP_DN_FORMAT_UFN:
2948 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2949 return LDAP_DECODING_ERROR;
2953 case LDAP_DN_FORMAT_DCE:
2954 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2955 return LDAP_DECODING_ERROR;
2959 case LDAP_DN_FORMAT_AD_CANONICAL:
2960 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2961 return LDAP_DECODING_ERROR;
2966 return LDAP_PARAM_ERROR;
2969 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2971 switch ( LDAP_DN_FORMAT( flags ) ) {
2972 case LDAP_DN_FORMAT_LDAPV3:
2973 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2977 case LDAP_DN_FORMAT_LDAPV2:
2978 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2982 case LDAP_DN_FORMAT_UFN:
2983 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2987 case LDAP_DN_FORMAT_DCE:
2988 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2992 case LDAP_DN_FORMAT_AD_CANONICAL:
2993 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2998 /* need at least one of the previous */
2999 return LDAP_PARAM_ERROR;
3003 LDAP_FREEX( bv->bv_val, ctx );
3007 bv->bv_len = l - back;
3008 bv->bv_val[ bv->bv_len ] = '\0';
3010 return LDAP_SUCCESS;
3014 * Very bulk implementation; many optimizations can be performed
3015 * - a NULL dn results in an empty string ""
3018 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
3019 * we must encode it in binary form ('#' + HEXPAIRs)
3020 * b) does DCE/AD support UTF-8?
3021 * no clue; don't think so.
3022 * c) what do we do when binary values must be converted in UTF/DCE/AD?
3023 * use binary encoded BER
3025 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
3032 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
3033 return LDAP_PARAM_ERROR;
3036 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
3041 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
3043 return ldap_dn2bv_x( dn, bv, flags, NULL );
3046 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
3049 int rc = LDAP_ENCODING_ERROR;
3052 /* stringifying helpers for LDAPv3/LDAPv2 */
3053 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3054 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3061 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3063 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3067 * a null dn means an empty dn string
3068 * FIXME: better raise an error?
3071 bv->bv_val = LDAP_STRDUPX( "", ctx );
3072 return( LDAP_SUCCESS );
3075 switch ( LDAP_DN_FORMAT( flags ) ) {
3076 case LDAP_DN_FORMAT_LDAPV3:
3077 sv2l = strval2strlen;
3081 case LDAP_DN_FORMAT_LDAPV2:
3082 sv2l = strval2IA5strlen;
3083 sv2s = strval2IA5str;
3086 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3088 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3089 goto return_results;
3095 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3096 rc = LDAP_NO_MEMORY;
3100 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3103 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3105 LDAP_FREEX( bv->bv_val, ctx );
3107 goto return_results;
3115 * trim the last ',' (the allocated memory
3116 * is one byte longer than required)
3118 bv->bv_len = len - 1;
3119 bv->bv_val[ bv->bv_len ] = '\0';
3124 case LDAP_DN_FORMAT_UFN: {
3126 * FIXME: quoting from RFC 1781:
3128 To take a distinguished name, and generate a name of this format with
3129 attribute types omitted, the following steps are followed.
3131 1. If the first attribute is of type CommonName, the type may be
3134 2. If the last attribute is of type Country, the type may be
3137 3. If the last attribute is of type Country, the last
3138 Organisation attribute may have the type omitted.
3140 4. All attributes of type OrganisationalUnit may have the type
3141 omitted, unless they are after an Organisation attribute or
3142 the first attribute is of type OrganisationalUnit.
3144 * this should be the pedantic implementation.
3146 * Here the standard implementation reflects
3147 * the one historically provided by OpenLDAP
3148 * (and UMIch, I presume), with the variant
3149 * of spaces and plusses (' + ') separating
3152 * A non-standard but nice implementation could
3153 * be to turn the final "dc" attributes into a
3154 * dot-separated domain.
3156 * Other improvements could involve the use of
3157 * friendly country names and so.
3160 int leftmost_dc = -1;
3162 #endif /* DC_IN_UFN */
3164 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3167 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3168 goto return_results;
3173 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3174 if ( leftmost_dc == -1 ) {
3180 #endif /* DC_IN_UFN */
3183 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3184 rc = LDAP_NO_MEMORY;
3189 if ( leftmost_dc == -1 ) {
3190 #endif /* DC_IN_UFN */
3191 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3194 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3196 LDAP_FREEX( bv->bv_val, ctx );
3198 goto return_results;
3204 * trim the last ', ' (the allocated memory
3205 * is two bytes longer than required)
3207 bv->bv_len = len - 2;
3208 bv->bv_val[ bv->bv_len ] = '\0';
3211 last_iRDN = iRDN - 1;
3213 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3216 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3218 LDAP_FREEX( bv->bv_val, ctx );
3220 goto return_results;
3225 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3226 LDAP_FREEX( bv->bv_val, ctx );
3228 goto return_results;
3231 /* the string is correctly terminated by dn2domain */
3233 #endif /* DC_IN_UFN */
3239 case LDAP_DN_FORMAT_DCE:
3240 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3242 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3243 goto return_results;
3249 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3250 rc = LDAP_NO_MEMORY;
3254 for ( l = 0; iRDN--; ) {
3257 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3259 LDAP_FREEX( bv->bv_val, ctx );
3261 goto return_results;
3269 bv->bv_val[ bv->bv_len ] = '\0';
3274 case LDAP_DN_FORMAT_AD_CANONICAL: {
3275 int trailing_slash = 1;
3278 * Sort of UFN for DCE DNs: a slash ('/') separated
3279 * global->local DN with no types; strictly speaking,
3280 * the naming context should be a domain, which is
3281 * written in DNS-style, e.g. dot-deparated.
3285 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3289 * "microsoft.com/People/Bill,Gates"
3291 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3294 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3295 goto return_results;
3301 /* reserve room for trailing '/' in case the DN
3302 * is exactly a domain */
3303 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3305 rc = LDAP_NO_MEMORY;
3310 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3311 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3316 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3317 flags, &rdnl, 0 ) ) {
3318 LDAP_FREEX( bv->bv_val, ctx );
3320 goto return_results;
3329 * Strictly speaking, AD canonical requires
3330 * a DN to be in the form "..., dc=smtg",
3331 * i.e. terminated by a domain component
3333 if ( flags & LDAP_DN_PEDANTIC ) {
3334 LDAP_FREEX( bv->bv_val, ctx );
3336 rc = LDAP_ENCODING_ERROR;
3340 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3343 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3344 flags, &rdnl, first ) ) {
3345 LDAP_FREEX( bv->bv_val, ctx );
3347 goto return_results;
3356 if ( trailing_slash ) {
3357 /* the DN is exactly a domain -- need a trailing
3358 * slash; room was reserved in advance */
3359 bv->bv_val[ len ] = '/';
3364 bv->bv_val[ bv->bv_len ] = '\0';
3370 return LDAP_PARAM_ERROR;
3374 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s)=%d %s\n",
3375 bv->bv_val, rc, ldap_err2string( rc ) );
3377 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s)=%d %s\n",
3378 bv->bv_val, rc, ldap_err2string( rc ) );
3386 #include <openssl/x509.h>
3387 #include <openssl/err.h>
3389 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3390 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3391 * constructed DN will use numeric OIDs to identify attributeTypes,
3392 * and the func() will be invoked to rewrite the DN with the given
3395 * Otherwise the DN will use shortNames as defined in the OpenSSL
3398 * It's preferable to let slapd do the OID to attributeType mapping,
3399 * because the OpenSSL tables are known to have many typos in versions
3400 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3401 * so we're forced to use OpenSSL's mapping there.
3402 * -- Howard Chu 2002-04-18
3406 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3411 LDAPAVA *newAVA, *baseAVA;
3412 X509_NAME_ENTRY *ne;
3415 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3417 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3419 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3428 /* Get the number of AVAs. This is not necessarily the same as
3429 * the number of RDNs.
3431 navas = X509_NAME_entry_count( x509_name );
3433 /* Get the last element, to see how many RDNs there are */
3434 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3435 nrdns = ne->set + 1;
3437 /* Allocate the DN/RDN/AVA stuff as a single block */
3438 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3439 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3440 dnsize += sizeof(LDAPAVA) * navas;
3441 if (dnsize > sizeof(ptrs)) {
3442 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3443 if ( newDN == NULL )
3444 return LDAP_NO_MEMORY;
3446 newDN = (LDAPDN)ptrs;
3449 newDN[nrdns] = NULL;
3450 newRDN = (LDAPRDN)(newDN + nrdns+1);
3451 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3454 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3455 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3456 ne = X509_NAME_get_entry( x509_name, i );
3457 obj = X509_NAME_ENTRY_get_object( ne );
3458 str = X509_NAME_ENTRY_get_data( ne );
3460 /* If set changed, move to next RDN */
3461 if ( set != ne->set ) {
3462 /* If this is not the first time, end the
3463 * previous RDN and advance.
3469 newDN[j++] = newRDN;
3474 newAVA->la_private = NULL;
3475 newAVA->la_flags = LDAP_AVA_STRING;
3478 int n = OBJ_obj2nid( obj );
3482 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3483 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3485 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3486 __etoa( newAVA->la_attr.bv_val );
3487 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3490 get_oid: newAVA->la_attr.bv_val = oidptr;
3491 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3493 __etoa( newAVA->la_attr.bv_val );
3495 oidptr += newAVA->la_attr.bv_len + 1;
3496 oidrem -= newAVA->la_attr.bv_len + 1;
3498 /* Running out of OID buffer space? */
3500 if ( oidsize == 0 ) {
3501 oidsize = sizeof(oids) * 2;
3503 oidbuf = LDAP_MALLOC( oidsize );
3504 if ( oidbuf == NULL ) goto nomem;
3508 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3509 if ( oidbuf == NULL ) goto nomem;
3510 /* Buffer moved! Fix AVA pointers */
3511 if ( old != oidbuf ) {
3513 long dif = oidbuf - old;
3515 for (a=baseAVA; a<=newAVA; a++){
3516 if (a->la_attr.bv_val >= old &&
3517 a->la_attr.bv_val <= (old + oidsize))
3518 a->la_attr.bv_val += dif;
3521 oidptr = oidbuf + oidsize - oidrem;
3527 Val.bv_val = (char *) str->data;
3528 Val.bv_len = str->length;
3529 switch( str->type ) {
3530 case V_ASN1_UNIVERSALSTRING:
3531 /* This uses 32-bit ISO 10646-1 */
3532 csize = 4; goto to_utf8;
3533 case V_ASN1_BMPSTRING:
3534 /* This uses 16-bit ISO 10646-1 */
3535 csize = 2; goto to_utf8;
3536 case V_ASN1_T61STRING:
3537 /* This uses 8-bit, assume ISO 8859-1 */
3539 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3540 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3541 if (rc != LDAP_SUCCESS) goto nomem;
3542 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3544 case V_ASN1_UTF8STRING:
3545 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3546 /* This is already in UTF-8 encoding */
3547 case V_ASN1_IA5STRING:
3548 case V_ASN1_PRINTABLESTRING:
3549 /* These are always 7-bit strings */
3550 newAVA->la_value = Val;
3561 rc = func( newDN, flags, NULL );
3562 if ( rc != LDAP_SUCCESS )
3566 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3569 for (;baseAVA < newAVA; baseAVA++) {
3570 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3571 LDAP_FREE( baseAVA->la_attr.bv_val );
3572 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3573 LDAP_FREE( baseAVA->la_value.bv_val );
3577 LDAP_FREE( oidbuf );
3578 if ( newDN != (LDAPDN) ptrs )
3582 #endif /* HAVE_TLS */