2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2007 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1994 Regents of the University of Michigan.
16 * All rights reserved.
23 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 #include "ldap_schema.h"
31 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
32 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
35 /* parsing/printing routines */
36 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
37 const char **next, unsigned flags, int *retFlags, void *ctx );
38 static int DCE2strval( const char *str, struct berval *val,
39 const char **next, unsigned flags, void *ctx );
40 static int IA52strval( const char *str, struct berval *val,
41 const char **next, unsigned flags, void *ctx );
42 static int quotedIA52strval( const char *str, struct berval *val,
43 const char **next, unsigned flags, void *ctx );
44 static int hexstr2binval( const char *str, struct berval *val,
45 const char **next, unsigned flags, void *ctx );
46 static int hexstr2bin( const char *str, char *c );
47 static int byte2hexpair( const char *val, char *pair );
48 static int binval2hexstr( struct berval *val, char *str );
49 static int strval2strlen( struct berval *val, unsigned flags,
51 static int strval2str( struct berval *val, char *str, unsigned flags,
53 static int strval2IA5strlen( struct berval *val, unsigned flags,
55 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
57 static int strval2DCEstrlen( struct berval *val, unsigned flags,
59 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
61 static int strval2ADstrlen( struct berval *val, unsigned flags,
63 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
65 static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
68 static LDAPAVA * ldapava_new(
69 const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
71 /* Higher level helpers */
72 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
73 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
74 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
75 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
76 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
77 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
78 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
79 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
80 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
81 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
84 * RFC 1823 ldap_get_dn
87 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
92 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
95 assert( LDAP_VALID(ld) );
96 assert( entry != NULL );
98 tmp = *entry->lm_ber; /* struct copy */
99 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
100 ld->ld_errno = LDAP_DECODING_ERROR;
108 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
111 BerElement tmp, *ber;
113 int rc = LDAP_SUCCESS;
115 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
117 assert( ld != NULL );
118 assert( LDAP_VALID(ld) );
119 assert( entry != NULL );
120 assert( dn != NULL );
127 ber = ldap_alloc_ber_with_options( ld );
129 return LDAP_NO_MEMORY;
136 *ber = *entry->lm_ber; /* struct copy */
137 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
138 rc = ld->ld_errno = LDAP_DECODING_ERROR;
140 if ( rc == LDAP_SUCCESS ) {
141 /* set the length to avoid overrun */
142 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
143 if( rc != LBER_OPT_SUCCESS ) {
144 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
147 if ( rc != LDAP_SUCCESS && berout ) {
155 * RFC 1823 ldap_dn2ufn
158 ldap_dn2ufn( LDAP_CONST char *dn )
162 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
164 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
165 &out, LDAP_DN_FORMAT_UFN );
171 * RFC 1823 ldap_explode_dn
174 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
177 char **values = NULL;
179 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
181 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
183 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
188 if( tmpDN == NULL ) {
189 values = LDAP_MALLOC( sizeof( char * ) );
190 if( values == NULL ) return NULL;
196 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
198 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
199 if ( values == NULL ) {
200 ldap_dnfree( tmpDN );
204 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
205 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
207 ldap_dnfree( tmpDN );
208 values[ iRDN ] = NULL;
214 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
217 char **values = NULL;
221 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
224 * we only parse the first rdn
225 * FIXME: we prefer efficiency over checking if the _ENTIRE_
228 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
233 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
234 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
235 if ( values == NULL ) {
236 ldap_rdnfree( tmpRDN );
240 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
241 ber_len_t l = 0, vl, al = 0;
243 LDAPAVA *ava = tmpRDN[ iAVA ];
245 if ( ava->la_flags & LDAP_AVA_BINARY ) {
246 vl = 1 + 2 * ava->la_value.bv_len;
249 if ( strval2strlen( &ava->la_value,
250 ava->la_flags, &vl ) ) {
256 al = ava->la_attr.bv_len;
257 l = vl + ava->la_attr.bv_len + 1;
259 str = LDAP_MALLOC( l + 1 );
260 AC_MEMCPY( str, ava->la_attr.bv_val,
261 ava->la_attr.bv_len );
266 str = LDAP_MALLOC( l + 1 );
269 if ( ava->la_flags & LDAP_AVA_BINARY ) {
271 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
276 if ( strval2str( &ava->la_value, &str[ al ],
277 ava->la_flags, &vl ) ) {
283 values[ iAVA ] = str;
285 values[ iAVA ] = NULL;
287 ldap_rdnfree( tmpRDN );
292 LBER_VFREE( values );
293 ldap_rdnfree( tmpRDN );
298 ldap_dn2dcedn( LDAP_CONST char *dn )
302 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
304 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
305 &out, LDAP_DN_FORMAT_DCE );
311 ldap_dcedn2dn( LDAP_CONST char *dce )
315 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
317 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
323 ldap_dn2ad_canonical( LDAP_CONST char *dn )
327 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
329 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
330 &out, LDAP_DN_FORMAT_AD_CANONICAL );
336 * function that changes the string representation of dnin
337 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
340 * LDAP_DN_FORMAT_LDAP (RFC 4514 liberal, plus some RFC 1779)
341 * LDAP_DN_FORMAT_LDAPV3 (RFC 4514)
342 * LDAP_DN_FORMAT_LDAPV2 (RFC 1779)
343 * LDAP_DN_FORMAT_DCE (?)
345 * fout can be any of the above except
346 * LDAP_DN_FORMAT_LDAP
348 * LDAP_DN_FORMAT_UFN (RFC 1781, partial and with extensions)
349 * LDAP_DN_FORMAT_AD_CANONICAL (?)
352 ldap_dn_normalize( LDAP_CONST char *dnin,
353 unsigned fin, char **dnout, unsigned fout )
358 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
360 assert( dnout != NULL );
364 if ( dnin == NULL ) {
365 return( LDAP_SUCCESS );
368 rc = ldap_str2dn( dnin , &tmpDN, fin );
369 if ( rc != LDAP_SUCCESS ) {
373 rc = ldap_dn2str( tmpDN, dnout, fout );
375 ldap_dnfree( tmpDN );
383 /* #define B4ATTRTYPE 0x0001 */
384 #define B4OIDATTRTYPE 0x0002
385 #define B4STRINGATTRTYPE 0x0003
387 #define B4AVAEQUALS 0x0100
388 #define B4AVASEP 0x0200
389 #define B4RDNSEP 0x0300
390 #define GOTAVA 0x0400
392 #define B4ATTRVALUE 0x0010
393 #define B4STRINGVALUE 0x0020
394 #define B4IA5VALUEQUOTED 0x0030
395 #define B4IA5VALUE 0x0040
396 #define B4BINARYVALUE 0x0050
399 * Helpers (mostly from slap.h)
400 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
401 * Macros assume "C" Locale (ASCII)
403 #define LDAP_DN_ASCII_SPACE(c) \
404 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
405 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
406 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
407 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
409 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
410 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
411 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
412 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
413 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
414 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
417 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
418 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
419 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
420 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
421 #define LDAP_DN_ATTRDESC_CHAR(c) \
422 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
424 /* special symbols */
425 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
426 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
427 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
428 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
429 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
430 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
431 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
432 #define LDAP_DN_VALUE_END(c) \
433 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
435 /* NOTE: according to RFC 4514, '=' can be escaped and treated as special,
436 * i.e. escaped both as "\<hexpair>" and * as "\=", but it is treated as
437 * a regular char, i.e. it can also appear as '='.
439 * As such, in 2.2 we used to allow reading unescaped '=', but we always
440 * produced escaped '\3D'; this changes since 2.3, if compatibility issues
443 #define LDAP_DN_NE(c) \
444 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
445 || LDAP_DN_QUOTES(c) \
446 || (c) == '<' || (c) == '>' )
447 #define LDAP_DN_MAYESCAPE(c) \
448 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
449 || LDAP_DN_AVA_EQUALS(c) \
450 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
451 #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
453 #define LDAP_DN_NEEDESCAPE(c) \
454 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
455 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
456 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
457 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
458 #define LDAP_DN_WILLESCAPE_CHAR(c) \
459 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
460 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
461 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
462 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
465 #define LDAP_DN_VALUE_END_V2(c) \
466 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
468 #define LDAP_DN_V2_SPECIAL(c) \
469 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
470 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
471 || LDAP_DN_OCTOTHORPE(c) )
472 #define LDAP_DN_V2_PAIR(c) \
473 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
476 * DCE (mostly from Luke Howard and IBM implementation for AIX)
478 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
479 * Here escapes and valid chars for GDS are considered; as soon as more
480 * specific info is found, the macros will be updated.
482 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
483 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
485 * Metachars: '/', ',', '=', '\'.
487 * the '\' is used to escape other metachars.
493 * Attribute types must start with alphabetic chars and can contain
494 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
496 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
497 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
498 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
499 #define LDAP_DN_VALUE_END_DCE(c) \
500 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
501 #define LDAP_DN_NEEDESCAPE_DCE(c) \
502 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
505 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
506 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
507 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
508 #define LDAP_DN_VALUE_END_AD(c) \
509 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
510 #define LDAP_DN_NEEDESCAPE_AD(c) \
511 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
514 #define LDAP_DN_HEXPAIR(s) \
515 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
516 /* better look at the AttributeDescription? */
518 /* FIXME: no composite rdn or non-"dc" types, right?
519 * (what about "dc" in OID form?) */
520 /* FIXME: we do not allow binary values in domain, right? */
521 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
522 /* NOTE: don't use strcasecmp() as it is locale specific! */
523 #define LDAP_DC_ATTR "dc"
524 #define LDAP_DC_ATTRU "DC"
525 #define LDAP_DN_IS_RDN_DC( r ) \
526 ( (r) && (r)[0] && !(r)[1] \
527 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
528 && ((r)[0]->la_attr.bv_len == 2) \
529 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
530 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
531 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
532 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
534 /* Composite rules */
535 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
536 ( LDAP_DN_LDAPV2(f) \
537 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
538 #define LDAP_DN_ALLOW_SPACES(f) \
539 ( LDAP_DN_LDAPV2(f) \
540 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
541 #define LDAP_DN_LDAP(f) \
542 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
543 #define LDAP_DN_LDAPV3(f) \
544 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
545 #define LDAP_DN_LDAPV2(f) \
546 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
547 #define LDAP_DN_DCE(f) \
548 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
549 #define LDAP_DN_UFN(f) \
550 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
551 #define LDAP_DN_ADC(f) \
552 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
553 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
556 * LDAPAVA helpers (will become part of the API for operations
557 * on structural representations of DNs).
560 ldapava_new( const struct berval *attr, const struct berval *val,
561 unsigned flags, void *ctx )
565 assert( attr != NULL );
566 assert( val != NULL );
568 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
571 ava->la_attr.bv_len = attr->bv_len;
572 ava->la_attr.bv_val = (char *)(ava+1);
573 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
574 ava->la_attr.bv_val[attr->bv_len] = '\0';
576 ava->la_value = *val;
577 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
579 ava->la_private = NULL;
586 ldapava_free( LDAPAVA *ava, void *ctx )
588 assert( ava != NULL );
591 /* ava's private must be freed by caller
592 * (at present let's skip this check because la_private
593 * basically holds static data) */
594 assert( ava->la_private == NULL );
597 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
598 LDAP_FREEX( ava->la_value.bv_val, ctx );
600 LDAP_FREEX( ava, ctx );
604 ldap_rdnfree( LDAPRDN rdn )
606 ldap_rdnfree_x( rdn, NULL );
610 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
618 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
619 ldapava_free( rdn[ iAVA ], ctx );
622 LDAP_FREEX( rdn, ctx );
626 ldap_dnfree( LDAPDN dn )
628 ldap_dnfree_x( dn, NULL );
632 ldap_dnfree_x( LDAPDN dn, void *ctx )
640 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
641 ldap_rdnfree_x( dn[ iRDN ], ctx );
644 LDAP_FREEX( dn, ctx );
648 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
649 * into a structural representation of the DN, by separating attribute
650 * types and values encoded in the more appropriate form, which is
651 * string or OID for attribute types and binary form of the BER encoded
652 * value or Unicode string. Formats different from LDAPv3 are parsed
653 * according to their own rules and turned into the more appropriate
654 * form according to LDAPv3.
656 * NOTE: I realize the code is getting spaghettish; it is rather
657 * experimental and will hopefully turn into something more simple
658 * and readable as soon as it works as expected.
662 * Default sizes of AVA and RDN static working arrays; if required
663 * the are dynamically resized. The values can be tuned in case
664 * of special requirements (e.g. very deep DN trees or high number
667 #define TMP_AVA_SLOTS 8
668 #define TMP_RDN_SLOTS 32
671 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
675 assert( str != NULL );
677 bv.bv_len = strlen( str );
678 bv.bv_val = (char *) str;
680 return ldap_bv2dn_x( &bv, dn, flags, NULL );
684 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
686 return ldap_bv2dn_x( bv, dn, flags, NULL );
690 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
693 int rc = LDAP_DECODING_ERROR;
697 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
698 int num_slots = TMP_RDN_SLOTS;
700 struct berval bvtmp, *bv = &bvtmp;
702 assert( bvin != NULL );
703 assert( bvin->bv_val != NULL );
704 assert( dn != NULL );
708 end = str + bv->bv_len;
710 Debug( LDAP_DEBUG_ARGS, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
714 switch ( LDAP_DN_FORMAT( flags ) ) {
715 case LDAP_DN_FORMAT_LDAP:
716 case LDAP_DN_FORMAT_LDAPV3:
717 case LDAP_DN_FORMAT_DCE:
720 /* allow DN enclosed in brackets */
721 case LDAP_DN_FORMAT_LDAPV2:
722 if ( str[0] == '<' ) {
723 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
724 rc = LDAP_DECODING_ERROR;
734 /* unsupported in str2dn */
735 case LDAP_DN_FORMAT_UFN:
736 case LDAP_DN_FORMAT_AD_CANONICAL:
737 return LDAP_PARAM_ERROR;
739 case LDAP_DN_FORMAT_LBER:
741 return LDAP_PARAM_ERROR;
744 if ( bv->bv_len == 0 ) {
748 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
749 /* value must have embedded NULs */
750 return LDAP_DECODING_ERROR;
754 if ( LDAP_DN_DCE( flags ) ) {
757 * (from Luke Howard: thnx) A RDN separator is required
758 * at the beginning of an (absolute) DN.
760 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
766 * actually we do not want to accept by default the DCE form,
767 * we do not want to auto-detect it
770 } else if ( LDAP_DN_LDAP( flags ) ) {
772 * if dn starts with '/' let's make it a DCE dn
774 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
775 flags |= LDAP_DN_FORMAT_DCE;
781 for ( ; p < end; p++ ) {
784 tmpbv.bv_len = bv->bv_len - ( p - str );
785 tmpbv.bv_val = (char *)p;
787 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
788 if ( err != LDAP_SUCCESS ) {
793 * We expect a rdn separator
795 if ( p < end && p[ 0 ] ) {
796 switch ( LDAP_DN_FORMAT( flags ) ) {
797 case LDAP_DN_FORMAT_LDAPV3:
798 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
799 rc = LDAP_DECODING_ERROR;
804 case LDAP_DN_FORMAT_LDAP:
805 case LDAP_DN_FORMAT_LDAPV2:
806 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
807 rc = LDAP_DECODING_ERROR;
812 case LDAP_DN_FORMAT_DCE:
813 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
814 rc = LDAP_DECODING_ERROR;
822 tmpDN[nrdns++] = newRDN;
826 * make the static RDN array dynamically rescalable
828 if ( nrdns == num_slots ) {
831 if ( tmpDN == tmpDN_ ) {
832 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
837 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
840 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
851 if ( p >= end || p[ 0 ] == '\0' ) {
853 * the DN is over, phew
855 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
856 if ( newDN == NULL ) {
862 if ( LDAP_DN_DCE( flags ) ) {
863 /* add in reversed order */
864 for ( i=0; i<nrdns; i++ )
865 newDN[i] = tmpDN[nrdns-1-i];
867 for ( i=0; i<nrdns; i++ )
879 ldap_rdnfree_x( newRDN, ctx );
882 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
883 ldap_rdnfree_x( tmpDN[nrdns], ctx );
888 if ( tmpDN != tmpDN_ ) {
889 LDAP_FREEX( tmpDN, ctx );
892 Debug( LDAP_DEBUG_ARGS, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
893 rc ? ldap_err2string( rc ) : "" );
902 * Parses a relative DN according to flags up to a rdn separator
903 * or to the end of str.
904 * Returns the rdn and a pointer to the string continuation, which
905 * corresponds to the rdn separator or to '\0' in case the string is over.
908 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
909 char **n_in, unsigned flags )
913 assert( str != NULL );
914 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
916 bv.bv_len = strlen( str );
917 bv.bv_val = (char *) str;
919 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
923 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
924 char **n_in, unsigned flags )
926 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
930 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
931 char **n_in, unsigned flags, void *ctx )
933 const char **n = (const char **) n_in;
937 int rc = LDAP_DECODING_ERROR;
938 int attrTypeEncoding = LDAP_AVA_STRING,
939 attrValueEncoding = LDAP_AVA_STRING;
941 struct berval attrType = BER_BVNULL;
942 struct berval attrValue = BER_BVNULL;
944 LDAPRDN newRDN = NULL;
945 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
946 int num_slots = TMP_AVA_SLOTS;
951 assert( bv != NULL );
952 assert( bv->bv_len != 0 );
953 assert( bv->bv_val != NULL );
954 assert( rdn || flags & LDAP_DN_SKIP );
958 stoplen = bv->bv_len;
965 switch ( LDAP_DN_FORMAT( flags ) ) {
966 case LDAP_DN_FORMAT_LDAP:
967 case LDAP_DN_FORMAT_LDAPV3:
968 case LDAP_DN_FORMAT_LDAPV2:
969 case LDAP_DN_FORMAT_DCE:
972 /* unsupported in str2dn */
973 case LDAP_DN_FORMAT_UFN:
974 case LDAP_DN_FORMAT_AD_CANONICAL:
975 return LDAP_PARAM_ERROR;
977 case LDAP_DN_FORMAT_LBER:
979 return LDAP_PARAM_ERROR;
982 if ( bv->bv_len == 0 ) {
987 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
988 /* value must have embedded NULs */
989 return LDAP_DECODING_ERROR;
993 for ( ; p[ 0 ] || state == GOTAVA; ) {
996 * The parser in principle advances one token a time,
997 * or toggles state if preferable.
1002 * an AttributeType can be encoded as:
1003 * - its string representation; in detail, implementations
1004 * MUST recognize AttributeType string type names listed
1005 * in Section 3 of RFC 4514, and MAY recognize other names.
1006 * - its numeric OID (a dotted decimal string)
1009 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1010 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1017 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1018 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1023 /* whitespace is allowed (and trimmed) */
1025 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1030 /* error: we expected an AVA */
1036 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1037 state = B4OIDATTRTYPE;
1041 /* else must be alpha */
1042 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1046 /* LDAPv2 "oid." prefix */
1047 if ( LDAP_DN_LDAPV2( flags ) ) {
1049 * to be overly pedantic, we only accept
1052 if ( flags & LDAP_DN_PEDANTIC ) {
1053 if ( !strncmp( p, "OID.", 4 )
1054 || !strncmp( p, "oid.", 4 ) ) {
1056 state = B4OIDATTRTYPE;
1060 if ( !strncasecmp( p, "oid.", 4 ) ) {
1062 state = B4OIDATTRTYPE;
1068 state = B4STRINGATTRTYPE;
1071 case B4OIDATTRTYPE: {
1072 int err = LDAP_SUCCESS;
1074 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1077 if ( err != LDAP_SUCCESS ) {
1080 attrType.bv_len = p - attrType.bv_val;
1082 attrTypeEncoding = LDAP_AVA_BINARY;
1084 state = B4AVAEQUALS;
1088 case B4STRINGATTRTYPE: {
1089 const char *startPos, *endPos = NULL;
1093 * the starting char has been found to be
1094 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1095 * FIXME: DCE attr types seem to have a more
1096 * restrictive syntax (no '-' ...)
1098 for ( startPos = p++; p[ 0 ]; p++ ) {
1099 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1103 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1106 * RFC 4514 explicitly does not allow attribute
1107 * description options, such as language tags.
1109 if ( flags & LDAP_DN_PEDANTIC ) {
1114 * we trim ';' and following lang
1115 * and so from attribute types
1118 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1119 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1127 len = ( endPos ? endPos : p ) - startPos;
1132 attrTypeEncoding = LDAP_AVA_STRING;
1135 * here we need to decide whether to use it as is
1136 * or turn it in OID form; as a consequence, we
1137 * need to decide whether to binary encode the value
1140 state = B4AVAEQUALS;
1142 if ( flags & LDAP_DN_SKIP ) {
1146 attrType.bv_val = (char *)startPos;
1147 attrType.bv_len = len;
1153 /* spaces may not be allowed */
1154 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1155 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1160 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1165 /* need equal sign */
1166 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1171 /* spaces may not be allowed */
1172 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1173 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1178 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1184 * octothorpe means a BER encoded value will follow
1185 * FIXME: I don't think DCE will allow it
1187 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1189 attrValueEncoding = LDAP_AVA_BINARY;
1190 state = B4BINARYVALUE;
1194 /* STRING value expected */
1197 * if we're pedantic, an attribute type in OID form
1198 * SHOULD imply a BER encoded attribute value; we
1199 * should at least issue a warning
1201 if ( ( flags & LDAP_DN_PEDANTIC )
1202 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1203 /* OID attrType SHOULD use binary encoding */
1207 attrValueEncoding = LDAP_AVA_STRING;
1210 * LDAPv2 allows the attribute value to be quoted;
1211 * also, IA5 values are expected, in principle
1213 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1214 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1216 state = B4IA5VALUEQUOTED;
1220 if ( LDAP_DN_LDAPV2( flags ) ) {
1227 * here STRING means RFC 4514 string
1228 * FIXME: what about DCE strings?
1234 state = B4STRINGVALUE;
1239 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1247 switch ( LDAP_DN_FORMAT( flags ) ) {
1248 case LDAP_DN_FORMAT_LDAP:
1249 case LDAP_DN_FORMAT_LDAPV3:
1250 if ( str2strval( p, stoplen - ( p - str ),
1251 &attrValue, &p, flags,
1252 &attrValueEncoding, ctx ) ) {
1257 case LDAP_DN_FORMAT_DCE:
1258 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1271 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1278 case B4IA5VALUEQUOTED:
1280 /* lead quote already stripped */
1281 if ( quotedIA52strval( p, &attrValue,
1282 &p, flags, ctx ) ) {
1292 if ( !( flags & LDAP_DN_SKIP ) ) {
1296 * we accept empty values
1298 ava = ldapava_new( &attrType, &attrValue,
1299 attrValueEncoding, ctx );
1300 if ( ava == NULL ) {
1301 rc = LDAP_NO_MEMORY;
1304 tmpRDN[navas++] = ava;
1306 attrValue.bv_val = NULL;
1307 attrValue.bv_len = 0;
1310 * prepare room for new AVAs if needed
1312 if (navas == num_slots) {
1315 if ( tmpRDN == tmpRDN_ ) {
1316 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1317 if ( tmp == NULL ) {
1318 rc = LDAP_NO_MEMORY;
1321 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1324 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1325 if ( tmp == NULL ) {
1326 rc = LDAP_NO_MEMORY;
1337 * if we got an AVA separator ('+', or ',' for DCE )
1338 * we expect a new AVA for this RDN; otherwise
1339 * we add the RDN to the DN
1341 switch ( LDAP_DN_FORMAT( flags ) ) {
1342 case LDAP_DN_FORMAT_LDAP:
1343 case LDAP_DN_FORMAT_LDAPV3:
1344 case LDAP_DN_FORMAT_LDAPV2:
1345 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1350 case LDAP_DN_FORMAT_DCE:
1351 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1359 * the RDN is over, phew
1362 if ( !( flags & LDAP_DN_SKIP ) ) {
1363 newRDN = (LDAPRDN)LDAP_MALLOCX(
1364 sizeof(LDAPAVA) * (navas+1), ctx );
1365 if ( newRDN == NULL ) {
1366 rc = LDAP_NO_MEMORY;
1369 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1370 newRDN[navas] = NULL;
1378 /* they should have been used in an AVA */
1379 attrType.bv_val = NULL;
1380 attrValue.bv_val = NULL;
1395 /* They are set to NULL after they're used in an AVA */
1397 if ( attrValue.bv_val ) {
1398 LDAP_FREEX( attrValue.bv_val, ctx );
1401 for ( navas-- ; navas >= 0; navas-- ) {
1402 ldapava_free( tmpRDN[navas], ctx );
1407 if ( tmpRDN != tmpRDN_ ) {
1408 LDAP_FREEX( tmpRDN, ctx );
1419 * reads in a UTF-8 string value, unescaping stuff:
1420 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1421 * '\' + HEXPAIR(p) -> unhex(p)
1424 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1426 const char *p, *end, *startPos, *endPos = NULL;
1427 ber_len_t len, escapes;
1429 assert( str != NULL );
1430 assert( val != NULL );
1431 assert( next != NULL );
1434 end = str + stoplen;
1435 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1436 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1438 if ( p[ 0 ] == '\0' ) {
1441 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1446 if ( LDAP_DN_HEXPAIR( p ) ) {
1449 hexstr2bin( p, &c );
1452 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1455 * we assume the string is UTF-8
1457 *retFlags = LDAP_AVA_NONPRINTABLE;
1464 if ( LDAP_DN_PEDANTIC & flags ) {
1468 * we do not allow escaping
1469 * of chars that don't need
1470 * to and do not belong to
1475 } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1476 if ( p[ 0 ] == '\0' ) {
1479 *retFlags = LDAP_AVA_NONPRINTABLE;
1481 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1482 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1485 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1487 * FIXME: maybe we can add
1488 * escapes if not pedantic?
1495 * we do allow unescaped spaces at the end
1496 * of the value only in non-pedantic mode
1498 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1499 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1500 if ( flags & LDAP_DN_PEDANTIC ) {
1504 /* strip trailing (unescaped) spaces */
1505 for ( endPos = p - 1;
1506 endPos > startPos + 1 &&
1507 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1508 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1515 if ( flags & LDAP_DN_SKIP ) {
1520 * FIXME: test memory?
1522 len = ( endPos ? endPos : p ) - startPos - escapes;
1525 if ( escapes == 0 ) {
1526 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1527 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1528 AC_MEMCPY( val->bv_val, startPos, len );
1529 val->bv_val[ len ] = '\0';
1531 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1537 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1538 for ( s = 0, d = 0; d < len; ) {
1539 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1541 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1542 val->bv_val[ d++ ] =
1545 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1548 hexstr2bin( &startPos[ s ], &c );
1549 val->bv_val[ d++ ] = c;
1553 /* we should never get here */
1558 val->bv_val[ d++ ] = startPos[ s++ ];
1562 val->bv_val[ d ] = '\0';
1570 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1572 const char *p, *startPos, *endPos = NULL;
1573 ber_len_t len, escapes;
1575 assert( str != NULL );
1576 assert( val != NULL );
1577 assert( next != NULL );
1581 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1582 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1584 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1591 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1596 * FIXME: can we accept anything else? I guess we need
1597 * to stop if a value is not legal
1602 * (unescaped) trailing spaces are trimmed must be silently ignored;
1605 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1606 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1607 if ( flags & LDAP_DN_PEDANTIC ) {
1611 /* strip trailing (unescaped) spaces */
1612 for ( endPos = p - 1;
1613 endPos > startPos + 1 &&
1614 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1615 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1622 if ( flags & LDAP_DN_SKIP ) {
1626 len = ( endPos ? endPos : p ) - startPos - escapes;
1628 if ( escapes == 0 ){
1629 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1634 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1635 for ( s = 0, d = 0; d < len; ) {
1637 * This point is reached only if escapes
1638 * are properly used, so all we need to
1641 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1645 val->bv_val[ d++ ] = startPos[ s++ ];
1647 val->bv_val[ d ] = '\0';
1648 assert( strlen( val->bv_val ) == len );
1655 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1657 const char *p, *startPos, *endPos = NULL;
1658 ber_len_t len, escapes;
1660 assert( str != NULL );
1661 assert( val != NULL );
1662 assert( next != NULL );
1670 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1671 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1673 if ( p[ 0 ] == '\0' ) {
1677 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1678 && ( LDAP_DN_PEDANTIC & flags ) ) {
1683 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1688 * FIXME: can we accept anything else? I guess we need
1689 * to stop if a value is not legal
1693 /* strip trailing (unescaped) spaces */
1695 endPos > startPos + 1 &&
1696 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1697 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1703 if ( flags & LDAP_DN_SKIP ) {
1707 len = ( endPos ? endPos : p ) - startPos - escapes;
1709 if ( escapes == 0 ) {
1710 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1715 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1716 for ( s = 0, d = 0; d < len; ) {
1717 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1720 val->bv_val[ d++ ] = startPos[ s++ ];
1722 val->bv_val[ d ] = '\0';
1723 assert( strlen( val->bv_val ) == len );
1730 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1732 const char *p, *startPos, *endPos = NULL;
1734 unsigned escapes = 0;
1736 assert( str != NULL );
1737 assert( val != NULL );
1738 assert( next != NULL );
1742 /* initial quote already eaten */
1743 for ( startPos = p = str; p[ 0 ]; p++ ) {
1745 * According to RFC 1779, the quoted value can
1746 * contain escaped as well as unescaped special values;
1747 * as a consequence we tolerate escaped values
1748 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1749 * (e.g. '","' -> '\,').
1751 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1752 if ( p[ 1 ] == '\0' ) {
1757 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1758 && ( LDAP_DN_PEDANTIC & flags ) ) {
1760 * do we allow to escape normal chars?
1761 * LDAPv2 does not allow any mechanism
1762 * for escaping chars with '\' and hex
1769 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1771 /* eat closing quotes */
1777 * FIXME: can we accept anything else? I guess we need
1778 * to stop if a value is not legal
1782 if ( endPos == NULL ) {
1786 /* Strip trailing (unescaped) spaces */
1787 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1792 if ( flags & LDAP_DN_SKIP ) {
1796 len = endPos - startPos - escapes;
1797 assert( endPos >= startPos + escapes );
1799 if ( escapes == 0 ) {
1800 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1805 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1808 for ( s = d = 0; d < len; ) {
1809 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1812 val->bv_val[ d++ ] = str[ s++ ];
1814 val->bv_val[ d ] = '\0';
1815 assert( strlen( val->bv_val ) == len );
1822 hexstr2bin( const char *str, char *c )
1826 assert( str != NULL );
1827 assert( c != NULL );
1832 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1836 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1839 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1846 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1850 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1851 *c += c2 - 'A' + 10;
1853 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1854 *c += c2 - 'a' + 10;
1862 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1864 const char *p, *startPos, *endPos = NULL;
1868 assert( str != NULL );
1869 assert( val != NULL );
1870 assert( next != NULL );
1874 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1875 switch ( LDAP_DN_FORMAT( flags ) ) {
1876 case LDAP_DN_FORMAT_LDAPV3:
1877 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1882 case LDAP_DN_FORMAT_LDAP:
1883 case LDAP_DN_FORMAT_LDAPV2:
1884 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1889 case LDAP_DN_FORMAT_DCE:
1890 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1896 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1897 if ( flags & LDAP_DN_PEDANTIC ) {
1902 for ( ; p[ 0 ]; p++ ) {
1903 switch ( LDAP_DN_FORMAT( flags ) ) {
1904 case LDAP_DN_FORMAT_LDAPV3:
1905 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1910 case LDAP_DN_FORMAT_LDAP:
1911 case LDAP_DN_FORMAT_LDAPV2:
1912 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1917 case LDAP_DN_FORMAT_DCE:
1918 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1927 if ( !LDAP_DN_HEXPAIR( p ) ) {
1935 if ( flags & LDAP_DN_SKIP ) {
1939 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1941 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1944 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1945 if ( val->bv_val == NULL ) {
1946 return( LDAP_NO_MEMORY );
1949 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1952 hexstr2bin( &startPos[ s ], &c );
1954 val->bv_val[ d ] = c;
1957 val->bv_val[ d ] = '\0';
1963 * convert a byte in a hexadecimal pair
1966 byte2hexpair( const char *val, char *pair )
1968 static const char hexdig[] = "0123456789ABCDEF";
1970 assert( val != NULL );
1971 assert( pair != NULL );
1974 * we assume the string has enough room for the hex encoding
1978 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1979 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1985 * convert a binary value in hexadecimal pairs
1988 binval2hexstr( struct berval *val, char *str )
1992 assert( val != NULL );
1993 assert( str != NULL );
1995 if ( val->bv_len == 0 ) {
2000 * we assume the string has enough room for the hex encoding
2004 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2005 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2012 * Length of the string representation, accounting for escaped hex
2016 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2018 ber_len_t l, cl = 1;
2020 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2021 #ifdef PRETTY_ESCAPE
2022 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2023 #endif /* PRETTY_ESCAPE */
2025 assert( val != NULL );
2026 assert( len != NULL );
2029 if ( val->bv_len == 0 ) {
2033 end = val->bv_val + val->bv_len - 1;
2034 for ( l = 0, p = val->bv_val; p <= end; p += cl ) {
2039 if ( p[ 0 ] == '\0' ) {
2045 cl = LDAP_UTF8_CHARLEN2( p, cl );
2047 /* illegal utf-8 char! */
2050 } else if ( cl > 1 ) {
2053 for ( cnt = 1; cnt < cl; cnt++ ) {
2054 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2058 l += escaped_byte_len * cl;
2060 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2061 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2062 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2063 || ( p == end && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2064 #ifdef PRETTY_ESCAPE
2066 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2068 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2072 * there might be some chars we want
2073 * to escape in form of a couple
2074 * of hexdigits for optimization purposes
2079 l += escaped_ascii_len;
2081 #else /* ! PRETTY_ESCAPE */
2083 #endif /* ! PRETTY_ESCAPE */
2096 * convert to string representation, escaping with hex the UTF-8 stuff;
2097 * assume the destination has enough room for escaping
2100 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2102 ber_len_t s, d, end;
2104 assert( val != NULL );
2105 assert( str != NULL );
2106 assert( len != NULL );
2108 if ( val->bv_len == 0 ) {
2114 * we assume the string has enough room for the hex encoding
2117 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2123 if ( val->bv_val[ s ] == '\0' ) {
2133 * The length was checked in strval2strlen();
2134 * LDAP_UTF8_CHARLEN() should suffice
2136 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2140 * there might be some chars we want to escape in form
2141 * of a couple of hexdigits for optimization purposes
2143 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2144 #ifdef PRETTY_ESCAPE
2146 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2148 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2150 #else /* ! PRETTY_ESCAPE */
2151 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2152 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2153 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2154 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2156 #endif /* ! PRETTY_ESCAPE */
2160 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2165 } else if ( cl > 1 ) {
2167 str[ d++ ] = val->bv_val[ s++ ];
2171 #ifdef PRETTY_ESCAPE
2172 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2173 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2174 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2175 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2177 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2178 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2184 #endif /* PRETTY_ESCAPE */
2185 str[ d++ ] = val->bv_val[ s++ ];
2195 * Length of the IA5 string representation (no UTF-8 allowed)
2198 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2203 assert( val != NULL );
2204 assert( len != NULL );
2207 if ( val->bv_len == 0 ) {
2211 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2213 * Turn value into a binary encoded BER
2218 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2219 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2220 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2221 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2222 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2237 * convert to string representation (np UTF-8)
2238 * assume the destination has enough room for escaping
2241 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2243 ber_len_t s, d, end;
2245 assert( val != NULL );
2246 assert( str != NULL );
2247 assert( len != NULL );
2249 if ( val->bv_len == 0 ) {
2254 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2256 * Turn value into a binary encoded BER
2263 * we assume the string has enough room for the hex encoding
2267 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2268 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2269 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2270 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2271 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2274 str[ d++ ] = val->bv_val[ s++ ];
2284 * Length of the (supposedly) DCE string representation,
2285 * accounting for escaped hex of UTF-8 chars
2288 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2293 assert( val != NULL );
2294 assert( len != NULL );
2297 if ( val->bv_len == 0 ) {
2301 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2303 * FIXME: Turn the value into a binary encoded BER?
2308 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2309 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2324 * convert to (supposedly) DCE string representation,
2325 * escaping with hex the UTF-8 stuff;
2326 * assume the destination has enough room for escaping
2329 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2333 assert( val != NULL );
2334 assert( str != NULL );
2335 assert( len != NULL );
2337 if ( val->bv_len == 0 ) {
2342 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2344 * FIXME: Turn the value into a binary encoded BER?
2352 * we assume the string has enough room for the hex encoding
2356 for ( s = 0, d = 0; s < val->bv_len; ) {
2357 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2360 str[ d++ ] = val->bv_val[ s++ ];
2370 * Length of the (supposedly) AD canonical string representation,
2371 * accounting for escaped hex of UTF-8 chars
2374 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2379 assert( val != NULL );
2380 assert( len != NULL );
2383 if ( val->bv_len == 0 ) {
2387 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2389 * FIXME: Turn the value into a binary encoded BER?
2394 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2395 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2410 * convert to (supposedly) AD string representation,
2411 * escaping with hex the UTF-8 stuff;
2412 * assume the destination has enough room for escaping
2415 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2419 assert( val != NULL );
2420 assert( str != NULL );
2421 assert( len != NULL );
2423 if ( val->bv_len == 0 ) {
2428 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2430 * FIXME: Turn the value into a binary encoded BER?
2438 * we assume the string has enough room for the hex encoding
2442 for ( s = 0, d = 0; s < val->bv_len; ) {
2443 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2446 str[ d++ ] = val->bv_val[ s++ ];
2456 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2457 * the first part of the AD representation of the DN is written in DNS
2458 * form, i.e. dot separated domain name components (as suggested
2459 * by Luke Howard, http://www.padl.com/~lukeh)
2462 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2465 int domain = 0, first = 1;
2466 ber_len_t l = 1; /* we move the null also */
2469 /* we are guaranteed there's enough memory in str */
2472 assert( dn != NULL );
2473 assert( bv != NULL );
2474 assert( iRDN != NULL );
2475 assert( *iRDN >= 0 );
2477 str = bv->bv_val + pos;
2479 for ( i = *iRDN; i >= 0; i-- ) {
2483 assert( dn[ i ] != NULL );
2486 assert( rdn[ 0 ] != NULL );
2489 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2497 AC_MEMCPY( str, ava->la_value.bv_val,
2498 ava->la_value.bv_len + 1);
2499 l += ava->la_value.bv_len;
2502 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2503 AC_MEMCPY( str, ava->la_value.bv_val,
2504 ava->la_value.bv_len );
2505 str[ ava->la_value.bv_len ] = '.';
2506 l += ava->la_value.bv_len + 1;
2511 bv->bv_len = pos + l - 1;
2517 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2518 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2525 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2526 LDAPAVA *ava = rdn[ iAVA ];
2528 /* len(type) + '=' + '+' | ',' */
2529 l += ava->la_attr.bv_len + 2;
2531 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2532 /* octothorpe + twice the length */
2533 l += 1 + 2 * ava->la_value.bv_len;
2537 unsigned f = flags | ava->la_flags;
2539 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2552 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2553 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2558 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2559 LDAPAVA *ava = rdn[ iAVA ];
2561 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2562 ava->la_attr.bv_len );
2563 l += ava->la_attr.bv_len;
2567 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2569 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2572 l += 2 * ava->la_value.bv_len;
2576 unsigned f = flags | ava->la_flags;
2578 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2583 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2592 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2599 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2600 LDAPAVA *ava = rdn[ iAVA ];
2602 /* len(type) + '=' + ',' | '/' */
2603 l += ava->la_attr.bv_len + 2;
2605 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2606 /* octothorpe + twice the length */
2607 l += 1 + 2 * ava->la_value.bv_len;
2610 unsigned f = flags | ava->la_flags;
2612 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2625 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2630 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2631 LDAPAVA *ava = rdn[ iAVA ];
2636 str[ l++ ] = ( iAVA ? ',' : '/' );
2639 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2640 ava->la_attr.bv_len );
2641 l += ava->la_attr.bv_len;
2645 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2647 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2650 l += 2 * ava->la_value.bv_len;
2653 unsigned f = flags | ava->la_flags;
2655 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2668 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2673 assert( rdn != NULL );
2674 assert( len != NULL );
2678 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2679 LDAPAVA *ava = rdn[ iAVA ];
2682 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2684 /* FIXME: are binary values allowed in UFN? */
2685 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2686 /* octothorpe + twice the value */
2687 l += 1 + 2 * ava->la_value.bv_len;
2691 unsigned f = flags | ava->la_flags;
2693 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2706 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2711 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2712 LDAPAVA *ava = rdn[ iAVA ];
2714 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2716 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2719 l += 2 * ava->la_value.bv_len;
2723 unsigned f = flags | ava->la_flags;
2725 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2731 if ( rdn[ iAVA + 1 ] ) {
2732 AC_MEMCPY( &str[ l ], " + ", 3 );
2736 AC_MEMCPY( &str[ l ], ", ", 2 );
2747 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2752 assert( rdn != NULL );
2753 assert( len != NULL );
2757 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2758 LDAPAVA *ava = rdn[ iAVA ];
2763 /* FIXME: are binary values allowed in UFN? */
2764 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2765 /* octothorpe + twice the value */
2766 l += 1 + 2 * ava->la_value.bv_len;
2769 unsigned f = flags | ava->la_flags;
2771 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2784 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2789 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2790 LDAPAVA *ava = rdn[ iAVA ];
2795 str[ l++ ] = ( iAVA ? ',' : '/' );
2798 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2800 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2803 l += 2 * ava->la_value.bv_len;
2806 unsigned f = flags | ava->la_flags;
2808 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2823 * Returns in str a string representation of rdn based on flags.
2824 * There is some duplication of code between this and ldap_dn2str;
2825 * this is wanted to reduce the allocation of temporary buffers.
2828 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2833 assert( str != NULL );
2835 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2836 return LDAP_PARAM_ERROR;
2839 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2845 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2847 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2851 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2856 assert( bv != NULL );
2861 if ( rdn == NULL ) {
2862 bv->bv_val = LDAP_STRDUPX( "", ctx );
2863 return( LDAP_SUCCESS );
2867 * This routine wastes "back" bytes at the end of the string
2870 switch ( LDAP_DN_FORMAT( flags ) ) {
2871 case LDAP_DN_FORMAT_LDAPV3:
2872 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2873 return LDAP_DECODING_ERROR;
2877 case LDAP_DN_FORMAT_LDAPV2:
2878 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2879 return LDAP_DECODING_ERROR;
2883 case LDAP_DN_FORMAT_UFN:
2884 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2885 return LDAP_DECODING_ERROR;
2889 case LDAP_DN_FORMAT_DCE:
2890 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2891 return LDAP_DECODING_ERROR;
2895 case LDAP_DN_FORMAT_AD_CANONICAL:
2896 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2897 return LDAP_DECODING_ERROR;
2902 return LDAP_PARAM_ERROR;
2905 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2907 switch ( LDAP_DN_FORMAT( flags ) ) {
2908 case LDAP_DN_FORMAT_LDAPV3:
2909 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2913 case LDAP_DN_FORMAT_LDAPV2:
2914 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2918 case LDAP_DN_FORMAT_UFN:
2919 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2923 case LDAP_DN_FORMAT_DCE:
2924 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2928 case LDAP_DN_FORMAT_AD_CANONICAL:
2929 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2934 /* need at least one of the previous */
2935 return LDAP_PARAM_ERROR;
2939 LDAP_FREEX( bv->bv_val, ctx );
2943 bv->bv_len = l - back;
2944 bv->bv_val[ bv->bv_len ] = '\0';
2946 return LDAP_SUCCESS;
2950 * Very bulk implementation; many optimizations can be performed
2951 * - a NULL dn results in an empty string ""
2954 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2955 * we must encode it in binary form ('#' + HEXPAIRs)
2956 * b) does DCE/AD support UTF-8?
2957 * no clue; don't think so.
2958 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2959 * use binary encoded BER
2961 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
2966 assert( str != NULL );
2968 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2969 return LDAP_PARAM_ERROR;
2972 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
2977 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
2979 return ldap_dn2bv_x( dn, bv, flags, NULL );
2982 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
2985 int rc = LDAP_ENCODING_ERROR;
2988 /* stringifying helpers for LDAPv3/LDAPv2 */
2989 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2990 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
2992 assert( bv != NULL );
2996 Debug( LDAP_DEBUG_ARGS, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
2999 * a null dn means an empty dn string
3000 * FIXME: better raise an error?
3003 bv->bv_val = LDAP_STRDUPX( "", ctx );
3004 return( LDAP_SUCCESS );
3007 switch ( LDAP_DN_FORMAT( flags ) ) {
3008 case LDAP_DN_FORMAT_LDAPV3:
3009 sv2l = strval2strlen;
3013 case LDAP_DN_FORMAT_LDAPV2:
3014 sv2l = strval2IA5strlen;
3015 sv2s = strval2IA5str;
3018 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3020 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3021 goto return_results;
3027 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3028 rc = LDAP_NO_MEMORY;
3032 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3035 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3037 LDAP_FREEX( bv->bv_val, ctx );
3039 goto return_results;
3047 * trim the last ',' (the allocated memory
3048 * is one byte longer than required)
3050 bv->bv_len = len - 1;
3051 bv->bv_val[ bv->bv_len ] = '\0';
3056 case LDAP_DN_FORMAT_UFN: {
3058 * FIXME: quoting from RFC 1781:
3060 To take a distinguished name, and generate a name of this format with
3061 attribute types omitted, the following steps are followed.
3063 1. If the first attribute is of type CommonName, the type may be
3066 2. If the last attribute is of type Country, the type may be
3069 3. If the last attribute is of type Country, the last
3070 Organisation attribute may have the type omitted.
3072 4. All attributes of type OrganisationalUnit may have the type
3073 omitted, unless they are after an Organisation attribute or
3074 the first attribute is of type OrganisationalUnit.
3076 * this should be the pedantic implementation.
3078 * Here the standard implementation reflects
3079 * the one historically provided by OpenLDAP
3080 * (and UMIch, I presume), with the variant
3081 * of spaces and plusses (' + ') separating
3084 * A non-standard but nice implementation could
3085 * be to turn the final "dc" attributes into a
3086 * dot-separated domain.
3088 * Other improvements could involve the use of
3089 * friendly country names and so.
3092 int leftmost_dc = -1;
3094 #endif /* DC_IN_UFN */
3096 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3099 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3100 goto return_results;
3105 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3106 if ( leftmost_dc == -1 ) {
3112 #endif /* DC_IN_UFN */
3115 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3116 rc = LDAP_NO_MEMORY;
3121 if ( leftmost_dc == -1 ) {
3122 #endif /* DC_IN_UFN */
3123 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3126 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3128 LDAP_FREEX( bv->bv_val, ctx );
3130 goto return_results;
3136 * trim the last ', ' (the allocated memory
3137 * is two bytes longer than required)
3139 bv->bv_len = len - 2;
3140 bv->bv_val[ bv->bv_len ] = '\0';
3143 last_iRDN = iRDN - 1;
3145 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3148 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3150 LDAP_FREEX( bv->bv_val, ctx );
3152 goto return_results;
3157 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3158 LDAP_FREEX( bv->bv_val, ctx );
3160 goto return_results;
3163 /* the string is correctly terminated by dn2domain */
3165 #endif /* DC_IN_UFN */
3171 case LDAP_DN_FORMAT_DCE:
3172 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3174 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3175 goto return_results;
3181 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3182 rc = LDAP_NO_MEMORY;
3186 for ( l = 0; iRDN--; ) {
3189 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3191 LDAP_FREEX( bv->bv_val, ctx );
3193 goto return_results;
3201 bv->bv_val[ bv->bv_len ] = '\0';
3206 case LDAP_DN_FORMAT_AD_CANONICAL: {
3207 int trailing_slash = 1;
3210 * Sort of UFN for DCE DNs: a slash ('/') separated
3211 * global->local DN with no types; strictly speaking,
3212 * the naming context should be a domain, which is
3213 * written in DNS-style, e.g. dot-deparated.
3217 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3221 * "microsoft.com/People/Bill,Gates"
3223 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3226 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3227 goto return_results;
3233 /* reserve room for trailing '/' in case the DN
3234 * is exactly a domain */
3235 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3237 rc = LDAP_NO_MEMORY;
3242 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3243 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3248 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3249 flags, &rdnl, 0 ) ) {
3250 LDAP_FREEX( bv->bv_val, ctx );
3252 goto return_results;
3261 * Strictly speaking, AD canonical requires
3262 * a DN to be in the form "..., dc=smtg",
3263 * i.e. terminated by a domain component
3265 if ( flags & LDAP_DN_PEDANTIC ) {
3266 LDAP_FREEX( bv->bv_val, ctx );
3268 rc = LDAP_ENCODING_ERROR;
3272 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3275 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3276 flags, &rdnl, first ) ) {
3277 LDAP_FREEX( bv->bv_val, ctx );
3279 goto return_results;
3288 if ( trailing_slash ) {
3289 /* the DN is exactly a domain -- need a trailing
3290 * slash; room was reserved in advance */
3291 bv->bv_val[ len ] = '/';
3296 bv->bv_val[ bv->bv_len ] = '\0';
3302 return LDAP_PARAM_ERROR;
3305 Debug( LDAP_DEBUG_ARGS, "<= ldap_dn2bv(%s)=%d %s\n",
3306 bv->bv_val, rc, rc ? ldap_err2string( rc ) : "" );
3313 #include <openssl/x509.h>
3314 #include <openssl/err.h>
3316 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3317 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3318 * constructed DN will use numeric OIDs to identify attributeTypes,
3319 * and the func() will be invoked to rewrite the DN with the given
3322 * Otherwise the DN will use shortNames as defined in the OpenSSL
3325 * It's preferable to let slapd do the OID to attributeType mapping,
3326 * because the OpenSSL tables are known to have many typos in versions
3327 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3328 * so we're forced to use OpenSSL's mapping there.
3329 * -- Howard Chu 2002-04-18
3333 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3338 LDAPAVA *newAVA, *baseAVA;
3339 X509_NAME_ENTRY *ne;
3342 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3344 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3346 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3351 assert( bv != NULL );
3355 /* Get the number of AVAs. This is not necessarily the same as
3356 * the number of RDNs.
3358 navas = X509_NAME_entry_count( x509_name );
3360 /* Get the last element, to see how many RDNs there are */
3361 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3362 nrdns = ne->set + 1;
3364 /* Allocate the DN/RDN/AVA stuff as a single block */
3365 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3366 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3367 dnsize += sizeof(LDAPAVA) * navas;
3368 if (dnsize > sizeof(ptrs)) {
3369 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3370 if ( newDN == NULL )
3371 return LDAP_NO_MEMORY;
3373 newDN = (LDAPDN)(char *)ptrs;
3376 newDN[nrdns] = NULL;
3377 newRDN = (LDAPRDN)(newDN + nrdns+1);
3378 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3381 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3382 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3383 ne = X509_NAME_get_entry( x509_name, i );
3384 obj = X509_NAME_ENTRY_get_object( ne );
3385 str = X509_NAME_ENTRY_get_data( ne );
3387 /* If set changed, move to next RDN */
3388 if ( set != ne->set ) {
3389 /* If this is not the first time, end the
3390 * previous RDN and advance.
3396 newDN[j++] = newRDN;
3401 newAVA->la_private = NULL;
3402 newAVA->la_flags = LDAP_AVA_STRING;
3405 int n = OBJ_obj2nid( obj );
3409 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3410 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3412 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3413 __etoa( newAVA->la_attr.bv_val );
3414 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3417 get_oid: newAVA->la_attr.bv_val = oidptr;
3418 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3420 __etoa( newAVA->la_attr.bv_val );
3422 oidptr += newAVA->la_attr.bv_len + 1;
3423 oidrem -= newAVA->la_attr.bv_len + 1;
3425 /* Running out of OID buffer space? */
3427 if ( oidsize == 0 ) {
3428 oidsize = sizeof(oids) * 2;
3430 oidbuf = LDAP_MALLOC( oidsize );
3431 if ( oidbuf == NULL ) goto nomem;
3435 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3436 if ( oidbuf == NULL ) goto nomem;
3437 /* Buffer moved! Fix AVA pointers */
3438 if ( old != oidbuf ) {
3440 long dif = oidbuf - old;
3442 for (a=baseAVA; a<=newAVA; a++){
3443 if (a->la_attr.bv_val >= old &&
3444 a->la_attr.bv_val <= (old + oidsize))
3445 a->la_attr.bv_val += dif;
3448 oidptr = oidbuf + oidsize - oidrem;
3454 Val.bv_val = (char *) str->data;
3455 Val.bv_len = str->length;
3456 switch( str->type ) {
3457 case V_ASN1_UNIVERSALSTRING:
3458 /* This uses 32-bit ISO 10646-1 */
3459 csize = 4; goto to_utf8;
3460 case V_ASN1_BMPSTRING:
3461 /* This uses 16-bit ISO 10646-1 */
3462 csize = 2; goto to_utf8;
3463 case V_ASN1_T61STRING:
3464 /* This uses 8-bit, assume ISO 8859-1 */
3466 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3467 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3468 if (rc != LDAP_SUCCESS) goto nomem;
3469 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3471 case V_ASN1_UTF8STRING:
3472 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3473 /* This is already in UTF-8 encoding */
3474 case V_ASN1_IA5STRING:
3475 case V_ASN1_PRINTABLESTRING:
3476 /* These are always 7-bit strings */
3477 newAVA->la_value = Val;
3488 rc = func( newDN, flags, NULL );
3489 if ( rc != LDAP_SUCCESS )
3493 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3496 for (;baseAVA < newAVA; baseAVA++) {
3497 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3498 LDAP_FREE( baseAVA->la_attr.bv_val );
3499 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3500 LDAP_FREE( baseAVA->la_value.bv_val );
3504 LDAP_FREE( oidbuf );
3505 if ( newDN != (LDAPDN)(char *) ptrs )
3509 #endif /* HAVE_TLS */