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 2253 and ldapbis liberal,
341 * plus some rfc 1779)
342 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
343 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
344 * LDAP_DN_FORMAT_DCE (?)
346 * fout can be any of the above except
347 * LDAP_DN_FORMAT_LDAP
349 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
350 * LDAP_DN_FORMAT_AD_CANONICAL (?)
353 ldap_dn_normalize( LDAP_CONST char *dnin,
354 unsigned fin, char **dnout, unsigned fout )
359 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
361 assert( dnout != NULL );
365 if ( dnin == NULL ) {
366 return( LDAP_SUCCESS );
369 rc = ldap_str2dn( dnin , &tmpDN, fin );
370 if ( rc != LDAP_SUCCESS ) {
374 rc = ldap_dn2str( tmpDN, dnout, fout );
376 ldap_dnfree( tmpDN );
384 /* #define B4ATTRTYPE 0x0001 */
385 #define B4OIDATTRTYPE 0x0002
386 #define B4STRINGATTRTYPE 0x0003
388 #define B4AVAEQUALS 0x0100
389 #define B4AVASEP 0x0200
390 #define B4RDNSEP 0x0300
391 #define GOTAVA 0x0400
393 #define B4ATTRVALUE 0x0010
394 #define B4STRINGVALUE 0x0020
395 #define B4IA5VALUEQUOTED 0x0030
396 #define B4IA5VALUE 0x0040
397 #define B4BINARYVALUE 0x0050
400 * Helpers (mostly from slap.h)
401 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
402 * Macros assume "C" Locale (ASCII)
404 #define LDAP_DN_ASCII_SPACE(c) \
405 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
406 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
407 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
408 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
410 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
411 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
412 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
413 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
414 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
415 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
418 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
419 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
420 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
421 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
422 #define LDAP_DN_ATTRDESC_CHAR(c) \
423 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
425 /* special symbols */
426 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
427 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
428 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
429 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
430 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
431 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
432 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
433 #define LDAP_DN_VALUE_END(c) \
434 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
436 /* NOTE: according to draft-ietf-ldapbis-dn, '=' can be escaped
437 * and treated as special, i.e. escaped both as "\<hexpair>" and
438 * as "\=", but it is treated as a regular char, i.e. it can also
441 * As such, in 2.2 we used to allow reading unescaped '=',
442 * but we always produced escaped '\3D'; this changes
443 * since 2.3, if compatibility issues do not arise */
444 #define LDAP_DN_NE(c) \
445 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
446 || LDAP_DN_QUOTES(c) \
447 || (c) == '<' || (c) == '>' )
448 #define LDAP_DN_MAYESCAPE(c) \
449 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
450 || LDAP_DN_AVA_EQUALS(c) \
451 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
452 #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
454 #define LDAP_DN_NEEDESCAPE(c) \
455 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
456 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
457 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
458 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
459 #define LDAP_DN_WILLESCAPE_CHAR(c) \
460 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
461 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
462 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
463 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
466 #define LDAP_DN_VALUE_END_V2(c) \
467 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
469 #define LDAP_DN_V2_SPECIAL(c) \
470 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
471 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
472 || LDAP_DN_OCTOTHORPE(c) )
473 #define LDAP_DN_V2_PAIR(c) \
474 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
477 * DCE (mostly from Luke Howard and IBM implementation for AIX)
479 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
480 * Here escapes and valid chars for GDS are considered; as soon as more
481 * specific info is found, the macros will be updated.
483 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
484 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
486 * Metachars: '/', ',', '=', '\'.
488 * the '\' is used to escape other metachars.
494 * Attribute types must start with alphabetic chars and can contain
495 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
497 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
498 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
499 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
500 #define LDAP_DN_VALUE_END_DCE(c) \
501 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
502 #define LDAP_DN_NEEDESCAPE_DCE(c) \
503 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
506 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
507 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
508 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
509 #define LDAP_DN_VALUE_END_AD(c) \
510 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
511 #define LDAP_DN_NEEDESCAPE_AD(c) \
512 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
515 #define LDAP_DN_HEXPAIR(s) \
516 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
517 /* better look at the AttributeDescription? */
519 /* FIXME: no composite rdn or non-"dc" types, right?
520 * (what about "dc" in OID form?) */
521 /* FIXME: we do not allow binary values in domain, right? */
522 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
523 /* NOTE: don't use strcasecmp() as it is locale specific! */
524 #define LDAP_DC_ATTR "dc"
525 #define LDAP_DC_ATTRU "DC"
526 #define LDAP_DN_IS_RDN_DC( r ) \
527 ( (r) && (r)[0] && !(r)[1] \
528 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
529 && ((r)[0]->la_attr.bv_len == 2) \
530 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
531 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
532 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
533 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
535 /* Composite rules */
536 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
537 ( LDAP_DN_LDAPV2(f) \
538 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
539 #define LDAP_DN_ALLOW_SPACES(f) \
540 ( LDAP_DN_LDAPV2(f) \
541 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
542 #define LDAP_DN_LDAP(f) \
543 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
544 #define LDAP_DN_LDAPV3(f) \
545 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
546 #define LDAP_DN_LDAPV2(f) \
547 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
548 #define LDAP_DN_DCE(f) \
549 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
550 #define LDAP_DN_UFN(f) \
551 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
552 #define LDAP_DN_ADC(f) \
553 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
554 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
557 * LDAPAVA helpers (will become part of the API for operations
558 * on structural representations of DNs).
561 ldapava_new( const struct berval *attr, const struct berval *val,
562 unsigned flags, void *ctx )
566 assert( attr != NULL );
567 assert( val != NULL );
569 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
572 ava->la_attr.bv_len = attr->bv_len;
573 ava->la_attr.bv_val = (char *)(ava+1);
574 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
575 ava->la_attr.bv_val[attr->bv_len] = '\0';
577 ava->la_value = *val;
578 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
580 ava->la_private = NULL;
587 ldapava_free( LDAPAVA *ava, void *ctx )
589 assert( ava != NULL );
592 /* ava's private must be freed by caller
593 * (at present let's skip this check because la_private
594 * basically holds static data) */
595 assert( ava->la_private == NULL );
598 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
599 LDAP_FREEX( ava->la_value.bv_val, ctx );
601 LDAP_FREEX( ava, ctx );
605 ldap_rdnfree( LDAPRDN rdn )
607 ldap_rdnfree_x( rdn, NULL );
611 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
619 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
620 ldapava_free( rdn[ iAVA ], ctx );
623 LDAP_FREEX( rdn, ctx );
627 ldap_dnfree( LDAPDN dn )
629 ldap_dnfree_x( dn, NULL );
633 ldap_dnfree_x( LDAPDN dn, void *ctx )
641 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
642 ldap_rdnfree_x( dn[ iRDN ], ctx );
645 LDAP_FREEX( dn, ctx );
649 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
650 * into a structural representation of the DN, by separating attribute
651 * types and values encoded in the more appropriate form, which is
652 * string or OID for attribute types and binary form of the BER encoded
653 * value or Unicode string. Formats different from LDAPv3 are parsed
654 * according to their own rules and turned into the more appropriate
655 * form according to LDAPv3.
657 * NOTE: I realize the code is getting spaghettish; it is rather
658 * experimental and will hopefully turn into something more simple
659 * and readable as soon as it works as expected.
663 * Default sizes of AVA and RDN static working arrays; if required
664 * the are dynamically resized. The values can be tuned in case
665 * of special requirements (e.g. very deep DN trees or high number
668 #define TMP_AVA_SLOTS 8
669 #define TMP_RDN_SLOTS 32
672 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
676 assert( str != NULL );
678 bv.bv_len = strlen( str );
679 bv.bv_val = (char *) str;
681 return ldap_bv2dn_x( &bv, dn, flags, NULL );
685 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
687 return ldap_bv2dn_x( bv, dn, flags, NULL );
691 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
694 int rc = LDAP_DECODING_ERROR;
698 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
699 int num_slots = TMP_RDN_SLOTS;
701 struct berval bvtmp, *bv = &bvtmp;
703 assert( bvin != NULL );
704 assert( bvin->bv_val != NULL );
705 assert( dn != NULL );
709 end = str + bv->bv_len;
711 Debug( LDAP_DEBUG_ARGS, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
715 switch ( LDAP_DN_FORMAT( flags ) ) {
716 case LDAP_DN_FORMAT_LDAP:
717 case LDAP_DN_FORMAT_LDAPV3:
718 case LDAP_DN_FORMAT_DCE:
721 /* allow DN enclosed in brackets */
722 case LDAP_DN_FORMAT_LDAPV2:
723 if ( str[0] == '<' ) {
724 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
725 rc = LDAP_DECODING_ERROR;
735 /* unsupported in str2dn */
736 case LDAP_DN_FORMAT_UFN:
737 case LDAP_DN_FORMAT_AD_CANONICAL:
738 return LDAP_PARAM_ERROR;
740 case LDAP_DN_FORMAT_LBER:
742 return LDAP_PARAM_ERROR;
745 if ( bv->bv_len == 0 ) {
749 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
750 /* value must have embedded NULs */
751 return LDAP_DECODING_ERROR;
755 if ( LDAP_DN_DCE( flags ) ) {
758 * (from Luke Howard: thnx) A RDN separator is required
759 * at the beginning of an (absolute) DN.
761 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
767 * actually we do not want to accept by default the DCE form,
768 * we do not want to auto-detect it
771 } else if ( LDAP_DN_LDAP( flags ) ) {
773 * if dn starts with '/' let's make it a DCE dn
775 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
776 flags |= LDAP_DN_FORMAT_DCE;
782 for ( ; p < end; p++ ) {
785 tmpbv.bv_len = bv->bv_len - ( p - str );
786 tmpbv.bv_val = (char *)p;
788 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
789 if ( err != LDAP_SUCCESS ) {
794 * We expect a rdn separator
796 if ( p < end && p[ 0 ] ) {
797 switch ( LDAP_DN_FORMAT( flags ) ) {
798 case LDAP_DN_FORMAT_LDAPV3:
799 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
800 rc = LDAP_DECODING_ERROR;
805 case LDAP_DN_FORMAT_LDAP:
806 case LDAP_DN_FORMAT_LDAPV2:
807 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
808 rc = LDAP_DECODING_ERROR;
813 case LDAP_DN_FORMAT_DCE:
814 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
815 rc = LDAP_DECODING_ERROR;
823 tmpDN[nrdns++] = newRDN;
827 * make the static RDN array dynamically rescalable
829 if ( nrdns == num_slots ) {
832 if ( tmpDN == tmpDN_ ) {
833 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
838 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
841 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
852 if ( p >= end || p[ 0 ] == '\0' ) {
854 * the DN is over, phew
856 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
857 if ( newDN == NULL ) {
863 if ( LDAP_DN_DCE( flags ) ) {
864 /* add in reversed order */
865 for ( i=0; i<nrdns; i++ )
866 newDN[i] = tmpDN[nrdns-1-i];
868 for ( i=0; i<nrdns; i++ )
880 ldap_rdnfree_x( newRDN, ctx );
883 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
884 ldap_rdnfree_x( tmpDN[nrdns], ctx );
889 if ( tmpDN != tmpDN_ ) {
890 LDAP_FREEX( tmpDN, ctx );
893 Debug( LDAP_DEBUG_ARGS, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
894 rc ? ldap_err2string( rc ) : "" );
903 * Parses a relative DN according to flags up to a rdn separator
904 * or to the end of str.
905 * Returns the rdn and a pointer to the string continuation, which
906 * corresponds to the rdn separator or to '\0' in case the string is over.
909 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
910 char **n_in, unsigned flags )
914 assert( str != NULL );
915 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
917 bv.bv_len = strlen( str );
918 bv.bv_val = (char *) str;
920 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
924 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
925 char **n_in, unsigned flags )
927 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
931 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
932 char **n_in, unsigned flags, void *ctx )
934 const char **n = (const char **) n_in;
938 int rc = LDAP_DECODING_ERROR;
939 int attrTypeEncoding = LDAP_AVA_STRING,
940 attrValueEncoding = LDAP_AVA_STRING;
942 struct berval attrType = BER_BVNULL;
943 struct berval attrValue = BER_BVNULL;
945 LDAPRDN newRDN = NULL;
946 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
947 int num_slots = TMP_AVA_SLOTS;
952 assert( bv != NULL );
953 assert( bv->bv_len != 0 );
954 assert( bv->bv_val != NULL );
955 assert( rdn || flags & LDAP_DN_SKIP );
959 stoplen = bv->bv_len;
966 switch ( LDAP_DN_FORMAT( flags ) ) {
967 case LDAP_DN_FORMAT_LDAP:
968 case LDAP_DN_FORMAT_LDAPV3:
969 case LDAP_DN_FORMAT_LDAPV2:
970 case LDAP_DN_FORMAT_DCE:
973 /* unsupported in str2dn */
974 case LDAP_DN_FORMAT_UFN:
975 case LDAP_DN_FORMAT_AD_CANONICAL:
976 return LDAP_PARAM_ERROR;
978 case LDAP_DN_FORMAT_LBER:
980 return LDAP_PARAM_ERROR;
983 if ( bv->bv_len == 0 ) {
988 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
989 /* value must have embedded NULs */
990 return LDAP_DECODING_ERROR;
994 for ( ; p[ 0 ] || state == GOTAVA; ) {
997 * The parser in principle advances one token a time,
998 * or toggles state if preferable.
1003 * an AttributeType can be encoded as:
1004 * - its string representation; in detail, implementations
1005 * MUST recognize AttributeType string type names listed
1006 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1007 * MAY recognize other names.
1008 * - its numeric OID (a dotted decimal string); in detail
1009 * RFC 2253 asserts that ``Implementations MUST allow
1010 * an oid in the attribute type to be prefixed by one
1011 * of the character strings "oid." or "OID."''. As soon
1012 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1013 * I'm not sure whether this is required or not any
1014 * longer; to be liberal, we still implement it.
1017 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1018 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1025 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1026 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1031 /* whitespace is allowed (and trimmed) */
1033 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1038 /* error: we expected an AVA */
1044 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1045 state = B4OIDATTRTYPE;
1049 /* else must be alpha */
1050 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1054 /* LDAPv2 "oid." prefix */
1055 if ( LDAP_DN_LDAPV2( flags ) ) {
1057 * to be overly pedantic, we only accept
1060 if ( flags & LDAP_DN_PEDANTIC ) {
1061 if ( !strncmp( p, "OID.", 4 )
1062 || !strncmp( p, "oid.", 4 ) ) {
1064 state = B4OIDATTRTYPE;
1068 if ( !strncasecmp( p, "oid.", 4 ) ) {
1070 state = B4OIDATTRTYPE;
1076 state = B4STRINGATTRTYPE;
1079 case B4OIDATTRTYPE: {
1080 int err = LDAP_SUCCESS;
1082 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1085 if ( err != LDAP_SUCCESS ) {
1088 attrType.bv_len = p - attrType.bv_val;
1090 attrTypeEncoding = LDAP_AVA_BINARY;
1092 state = B4AVAEQUALS;
1096 case B4STRINGATTRTYPE: {
1097 const char *startPos, *endPos = NULL;
1101 * the starting char has been found to be
1102 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1103 * FIXME: DCE attr types seem to have a more
1104 * restrictive syntax (no '-' ...)
1106 for ( startPos = p++; p[ 0 ]; p++ ) {
1107 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1111 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1114 * RFC 2253 does not explicitly
1115 * allow lang extensions to attribute
1118 if ( flags & LDAP_DN_PEDANTIC ) {
1123 * we trim ';' and following lang
1124 * and so from attribute types
1127 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1128 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1136 len = ( endPos ? endPos : p ) - startPos;
1141 attrTypeEncoding = LDAP_AVA_STRING;
1144 * here we need to decide whether to use it as is
1145 * or turn it in OID form; as a consequence, we
1146 * need to decide whether to binary encode the value
1149 state = B4AVAEQUALS;
1151 if ( flags & LDAP_DN_SKIP ) {
1155 attrType.bv_val = (char *)startPos;
1156 attrType.bv_len = len;
1162 /* spaces may not be allowed */
1163 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1164 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1169 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1174 /* need equal sign */
1175 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1180 /* spaces may not be allowed */
1181 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1182 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1187 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1193 * octothorpe means a BER encoded value will follow
1194 * FIXME: I don't think DCE will allow it
1196 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1198 attrValueEncoding = LDAP_AVA_BINARY;
1199 state = B4BINARYVALUE;
1203 /* STRING value expected */
1206 * if we're pedantic, an attribute type in OID form
1207 * SHOULD imply a BER encoded attribute value; we
1208 * should at least issue a warning
1210 if ( ( flags & LDAP_DN_PEDANTIC )
1211 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1212 /* OID attrType SHOULD use binary encoding */
1216 attrValueEncoding = LDAP_AVA_STRING;
1219 * LDAPv2 allows the attribute value to be quoted;
1220 * also, IA5 values are expected, in principle
1222 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1223 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1225 state = B4IA5VALUEQUOTED;
1229 if ( LDAP_DN_LDAPV2( flags ) ) {
1236 * here STRING means RFC 2253 string
1237 * FIXME: what about DCE strings?
1243 state = B4STRINGVALUE;
1248 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1256 switch ( LDAP_DN_FORMAT( flags ) ) {
1257 case LDAP_DN_FORMAT_LDAP:
1258 case LDAP_DN_FORMAT_LDAPV3:
1259 if ( str2strval( p, stoplen - ( p - str ),
1260 &attrValue, &p, flags,
1261 &attrValueEncoding, ctx ) ) {
1266 case LDAP_DN_FORMAT_DCE:
1267 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1280 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1287 case B4IA5VALUEQUOTED:
1289 /* lead quote already stripped */
1290 if ( quotedIA52strval( p, &attrValue,
1291 &p, flags, ctx ) ) {
1301 if ( !( flags & LDAP_DN_SKIP ) ) {
1305 * we accept empty values
1307 ava = ldapava_new( &attrType, &attrValue,
1308 attrValueEncoding, ctx );
1309 if ( ava == NULL ) {
1310 rc = LDAP_NO_MEMORY;
1313 tmpRDN[navas++] = ava;
1315 attrValue.bv_val = NULL;
1316 attrValue.bv_len = 0;
1319 * prepare room for new AVAs if needed
1321 if (navas == num_slots) {
1324 if ( tmpRDN == tmpRDN_ ) {
1325 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1326 if ( tmp == NULL ) {
1327 rc = LDAP_NO_MEMORY;
1330 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1333 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1334 if ( tmp == NULL ) {
1335 rc = LDAP_NO_MEMORY;
1346 * if we got an AVA separator ('+', or ',' for DCE )
1347 * we expect a new AVA for this RDN; otherwise
1348 * we add the RDN to the DN
1350 switch ( LDAP_DN_FORMAT( flags ) ) {
1351 case LDAP_DN_FORMAT_LDAP:
1352 case LDAP_DN_FORMAT_LDAPV3:
1353 case LDAP_DN_FORMAT_LDAPV2:
1354 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1359 case LDAP_DN_FORMAT_DCE:
1360 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1368 * the RDN is over, phew
1371 if ( !( flags & LDAP_DN_SKIP ) ) {
1372 newRDN = (LDAPRDN)LDAP_MALLOCX(
1373 sizeof(LDAPAVA) * (navas+1), ctx );
1374 if ( newRDN == NULL ) {
1375 rc = LDAP_NO_MEMORY;
1378 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1379 newRDN[navas] = NULL;
1387 /* they should have been used in an AVA */
1388 attrType.bv_val = NULL;
1389 attrValue.bv_val = NULL;
1404 /* They are set to NULL after they're used in an AVA */
1406 if ( attrValue.bv_val ) {
1407 LDAP_FREEX( attrValue.bv_val, ctx );
1410 for ( navas-- ; navas >= 0; navas-- ) {
1411 ldapava_free( tmpRDN[navas], ctx );
1416 if ( tmpRDN != tmpRDN_ ) {
1417 LDAP_FREEX( tmpRDN, ctx );
1428 * reads in a UTF-8 string value, unescaping stuff:
1429 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1430 * '\' + HEXPAIR(p) -> unhex(p)
1433 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1435 const char *p, *end, *startPos, *endPos = NULL;
1436 ber_len_t len, escapes;
1438 assert( str != NULL );
1439 assert( val != NULL );
1440 assert( next != NULL );
1443 end = str + stoplen;
1444 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1445 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1447 if ( p[ 0 ] == '\0' ) {
1450 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1455 if ( LDAP_DN_HEXPAIR( p ) ) {
1458 hexstr2bin( p, &c );
1461 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1464 * we assume the string is UTF-8
1466 *retFlags = LDAP_AVA_NONPRINTABLE;
1473 if ( LDAP_DN_PEDANTIC & flags ) {
1477 * we do not allow escaping
1478 * of chars that don't need
1479 * to and do not belong to
1484 } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1485 if ( p[ 0 ] == '\0' ) {
1488 *retFlags = LDAP_AVA_NONPRINTABLE;
1490 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1491 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1494 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1496 * FIXME: maybe we can add
1497 * escapes if not pedantic?
1504 * we do allow unescaped spaces at the end
1505 * of the value only in non-pedantic mode
1507 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1508 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1509 if ( flags & LDAP_DN_PEDANTIC ) {
1513 /* strip trailing (unescaped) spaces */
1514 for ( endPos = p - 1;
1515 endPos > startPos + 1 &&
1516 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1517 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1524 if ( flags & LDAP_DN_SKIP ) {
1529 * FIXME: test memory?
1531 len = ( endPos ? endPos : p ) - startPos - escapes;
1534 if ( escapes == 0 ) {
1535 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1536 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1537 AC_MEMCPY( val->bv_val, startPos, len );
1538 val->bv_val[ len ] = '\0';
1540 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1546 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1547 for ( s = 0, d = 0; d < len; ) {
1548 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1550 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1551 val->bv_val[ d++ ] =
1554 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1557 hexstr2bin( &startPos[ s ], &c );
1558 val->bv_val[ d++ ] = c;
1562 /* we should never get here */
1567 val->bv_val[ d++ ] = startPos[ s++ ];
1571 val->bv_val[ d ] = '\0';
1579 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1581 const char *p, *startPos, *endPos = NULL;
1582 ber_len_t len, escapes;
1584 assert( str != NULL );
1585 assert( val != NULL );
1586 assert( next != NULL );
1590 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1591 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1593 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1600 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1605 * FIXME: can we accept anything else? I guess we need
1606 * to stop if a value is not legal
1611 * (unescaped) trailing spaces are trimmed must be silently ignored;
1614 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1615 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1616 if ( flags & LDAP_DN_PEDANTIC ) {
1620 /* strip trailing (unescaped) spaces */
1621 for ( endPos = p - 1;
1622 endPos > startPos + 1 &&
1623 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1624 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1631 if ( flags & LDAP_DN_SKIP ) {
1635 len = ( endPos ? endPos : p ) - startPos - escapes;
1637 if ( escapes == 0 ){
1638 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1643 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1644 for ( s = 0, d = 0; d < len; ) {
1646 * This point is reached only if escapes
1647 * are properly used, so all we need to
1650 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1654 val->bv_val[ d++ ] = startPos[ s++ ];
1656 val->bv_val[ d ] = '\0';
1657 assert( strlen( val->bv_val ) == len );
1664 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1666 const char *p, *startPos, *endPos = NULL;
1667 ber_len_t len, escapes;
1669 assert( str != NULL );
1670 assert( val != NULL );
1671 assert( next != NULL );
1679 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1680 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1682 if ( p[ 0 ] == '\0' ) {
1686 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1687 && ( LDAP_DN_PEDANTIC & flags ) ) {
1692 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1697 * FIXME: can we accept anything else? I guess we need
1698 * to stop if a value is not legal
1702 /* strip trailing (unescaped) spaces */
1704 endPos > startPos + 1 &&
1705 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1706 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1712 if ( flags & LDAP_DN_SKIP ) {
1716 len = ( endPos ? endPos : p ) - startPos - escapes;
1718 if ( escapes == 0 ) {
1719 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1724 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1725 for ( s = 0, d = 0; d < len; ) {
1726 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1729 val->bv_val[ d++ ] = startPos[ s++ ];
1731 val->bv_val[ d ] = '\0';
1732 assert( strlen( val->bv_val ) == len );
1739 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1741 const char *p, *startPos, *endPos = NULL;
1743 unsigned escapes = 0;
1745 assert( str != NULL );
1746 assert( val != NULL );
1747 assert( next != NULL );
1751 /* initial quote already eaten */
1752 for ( startPos = p = str; p[ 0 ]; p++ ) {
1754 * According to RFC 1779, the quoted value can
1755 * contain escaped as well as unescaped special values;
1756 * as a consequence we tolerate escaped values
1757 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1758 * (e.g. '","' -> '\,').
1760 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1761 if ( p[ 1 ] == '\0' ) {
1766 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1767 && ( LDAP_DN_PEDANTIC & flags ) ) {
1769 * do we allow to escape normal chars?
1770 * LDAPv2 does not allow any mechanism
1771 * for escaping chars with '\' and hex
1778 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1780 /* eat closing quotes */
1786 * FIXME: can we accept anything else? I guess we need
1787 * to stop if a value is not legal
1791 if ( endPos == NULL ) {
1795 /* Strip trailing (unescaped) spaces */
1796 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1801 if ( flags & LDAP_DN_SKIP ) {
1805 len = endPos - startPos - escapes;
1806 assert( endPos >= startPos + escapes );
1808 if ( escapes == 0 ) {
1809 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1814 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1817 for ( s = d = 0; d < len; ) {
1818 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1821 val->bv_val[ d++ ] = str[ s++ ];
1823 val->bv_val[ d ] = '\0';
1824 assert( strlen( val->bv_val ) == len );
1831 hexstr2bin( const char *str, char *c )
1835 assert( str != NULL );
1836 assert( c != NULL );
1841 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1845 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1848 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1855 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1859 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1860 *c += c2 - 'A' + 10;
1862 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1863 *c += c2 - 'a' + 10;
1871 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1873 const char *p, *startPos, *endPos = NULL;
1877 assert( str != NULL );
1878 assert( val != NULL );
1879 assert( next != NULL );
1883 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1884 switch ( LDAP_DN_FORMAT( flags ) ) {
1885 case LDAP_DN_FORMAT_LDAPV3:
1886 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1891 case LDAP_DN_FORMAT_LDAP:
1892 case LDAP_DN_FORMAT_LDAPV2:
1893 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1898 case LDAP_DN_FORMAT_DCE:
1899 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1905 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1906 if ( flags & LDAP_DN_PEDANTIC ) {
1911 for ( ; p[ 0 ]; p++ ) {
1912 switch ( LDAP_DN_FORMAT( flags ) ) {
1913 case LDAP_DN_FORMAT_LDAPV3:
1914 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1919 case LDAP_DN_FORMAT_LDAP:
1920 case LDAP_DN_FORMAT_LDAPV2:
1921 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1926 case LDAP_DN_FORMAT_DCE:
1927 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1936 if ( !LDAP_DN_HEXPAIR( p ) ) {
1944 if ( flags & LDAP_DN_SKIP ) {
1948 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
1950 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
1953 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1954 if ( val->bv_val == NULL ) {
1955 return( LDAP_NO_MEMORY );
1958 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
1961 hexstr2bin( &startPos[ s ], &c );
1963 val->bv_val[ d ] = c;
1966 val->bv_val[ d ] = '\0';
1972 * convert a byte in a hexadecimal pair
1975 byte2hexpair( const char *val, char *pair )
1977 static const char hexdig[] = "0123456789ABCDEF";
1979 assert( val != NULL );
1980 assert( pair != NULL );
1983 * we assume the string has enough room for the hex encoding
1987 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
1988 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
1994 * convert a binary value in hexadecimal pairs
1997 binval2hexstr( struct berval *val, char *str )
2001 assert( val != NULL );
2002 assert( str != NULL );
2004 if ( val->bv_len == 0 ) {
2009 * we assume the string has enough room for the hex encoding
2013 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2014 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2021 * Length of the string representation, accounting for escaped hex
2025 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2027 ber_len_t l, cl = 1;
2029 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2030 #ifdef PRETTY_ESCAPE
2031 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2032 #endif /* PRETTY_ESCAPE */
2034 assert( val != NULL );
2035 assert( len != NULL );
2038 if ( val->bv_len == 0 ) {
2042 end = val->bv_val + val->bv_len - 1;
2043 for ( l = 0, p = val->bv_val; p <= end; p += cl ) {
2048 if ( p[ 0 ] == '\0' ) {
2054 cl = LDAP_UTF8_CHARLEN2( p, cl );
2056 /* illegal utf-8 char! */
2059 } else if ( cl > 1 ) {
2062 for ( cnt = 1; cnt < cl; cnt++ ) {
2063 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2067 l += escaped_byte_len * cl;
2069 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2070 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2071 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2072 || ( p == end && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2073 #ifdef PRETTY_ESCAPE
2075 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2077 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2081 * there might be some chars we want
2082 * to escape in form of a couple
2083 * of hexdigits for optimization purposes
2088 l += escaped_ascii_len;
2090 #else /* ! PRETTY_ESCAPE */
2092 #endif /* ! PRETTY_ESCAPE */
2105 * convert to string representation, escaping with hex the UTF-8 stuff;
2106 * assume the destination has enough room for escaping
2109 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2111 ber_len_t s, d, end;
2113 assert( val != NULL );
2114 assert( str != NULL );
2115 assert( len != NULL );
2117 if ( val->bv_len == 0 ) {
2123 * we assume the string has enough room for the hex encoding
2126 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2132 if ( val->bv_val[ s ] == '\0' ) {
2142 * The length was checked in strval2strlen();
2143 * LDAP_UTF8_CHARLEN() should suffice
2145 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2149 * there might be some chars we want to escape in form
2150 * of a couple of hexdigits for optimization purposes
2152 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2153 #ifdef PRETTY_ESCAPE
2155 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2157 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2159 #else /* ! PRETTY_ESCAPE */
2160 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2161 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2162 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2163 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2165 #endif /* ! PRETTY_ESCAPE */
2169 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2174 } else if ( cl > 1 ) {
2176 str[ d++ ] = val->bv_val[ s++ ];
2180 #ifdef PRETTY_ESCAPE
2181 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2182 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2183 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2184 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2186 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2187 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2193 #endif /* PRETTY_ESCAPE */
2194 str[ d++ ] = val->bv_val[ s++ ];
2204 * Length of the IA5 string representation (no UTF-8 allowed)
2207 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2212 assert( val != NULL );
2213 assert( len != NULL );
2216 if ( val->bv_len == 0 ) {
2220 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2222 * Turn value into a binary encoded BER
2227 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2228 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2229 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2230 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2231 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2246 * convert to string representation (np UTF-8)
2247 * assume the destination has enough room for escaping
2250 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2252 ber_len_t s, d, end;
2254 assert( val != NULL );
2255 assert( str != NULL );
2256 assert( len != NULL );
2258 if ( val->bv_len == 0 ) {
2263 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2265 * Turn value into a binary encoded BER
2272 * we assume the string has enough room for the hex encoding
2276 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2277 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2278 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2279 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2280 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2283 str[ d++ ] = val->bv_val[ s++ ];
2293 * Length of the (supposedly) DCE string representation,
2294 * accounting for escaped hex of UTF-8 chars
2297 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2302 assert( val != NULL );
2303 assert( len != NULL );
2306 if ( val->bv_len == 0 ) {
2310 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2312 * FIXME: Turn the value into a binary encoded BER?
2317 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2318 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2333 * convert to (supposedly) DCE string representation,
2334 * escaping with hex the UTF-8 stuff;
2335 * assume the destination has enough room for escaping
2338 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2342 assert( val != NULL );
2343 assert( str != NULL );
2344 assert( len != NULL );
2346 if ( val->bv_len == 0 ) {
2351 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2353 * FIXME: Turn the value into a binary encoded BER?
2361 * we assume the string has enough room for the hex encoding
2365 for ( s = 0, d = 0; s < val->bv_len; ) {
2366 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2369 str[ d++ ] = val->bv_val[ s++ ];
2379 * Length of the (supposedly) AD canonical string representation,
2380 * accounting for escaped hex of UTF-8 chars
2383 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2388 assert( val != NULL );
2389 assert( len != NULL );
2392 if ( val->bv_len == 0 ) {
2396 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2398 * FIXME: Turn the value into a binary encoded BER?
2403 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2404 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2419 * convert to (supposedly) AD string representation,
2420 * escaping with hex the UTF-8 stuff;
2421 * assume the destination has enough room for escaping
2424 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2428 assert( val != NULL );
2429 assert( str != NULL );
2430 assert( len != NULL );
2432 if ( val->bv_len == 0 ) {
2437 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2439 * FIXME: Turn the value into a binary encoded BER?
2447 * we assume the string has enough room for the hex encoding
2451 for ( s = 0, d = 0; s < val->bv_len; ) {
2452 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2455 str[ d++ ] = val->bv_val[ s++ ];
2465 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2466 * the first part of the AD representation of the DN is written in DNS
2467 * form, i.e. dot separated domain name components (as suggested
2468 * by Luke Howard, http://www.padl.com/~lukeh)
2471 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2474 int domain = 0, first = 1;
2475 ber_len_t l = 1; /* we move the null also */
2478 /* we are guaranteed there's enough memory in str */
2481 assert( dn != NULL );
2482 assert( bv != NULL );
2483 assert( iRDN != NULL );
2484 assert( *iRDN >= 0 );
2486 str = bv->bv_val + pos;
2488 for ( i = *iRDN; i >= 0; i-- ) {
2492 assert( dn[ i ] != NULL );
2495 assert( rdn[ 0 ] != NULL );
2498 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2506 AC_MEMCPY( str, ava->la_value.bv_val,
2507 ava->la_value.bv_len + 1);
2508 l += ava->la_value.bv_len;
2511 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2512 AC_MEMCPY( str, ava->la_value.bv_val,
2513 ava->la_value.bv_len );
2514 str[ ava->la_value.bv_len ] = '.';
2515 l += ava->la_value.bv_len + 1;
2520 bv->bv_len = pos + l - 1;
2526 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2527 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2534 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2535 LDAPAVA *ava = rdn[ iAVA ];
2537 /* len(type) + '=' + '+' | ',' */
2538 l += ava->la_attr.bv_len + 2;
2540 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2541 /* octothorpe + twice the length */
2542 l += 1 + 2 * ava->la_value.bv_len;
2546 unsigned f = flags | ava->la_flags;
2548 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2561 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2562 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2567 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2568 LDAPAVA *ava = rdn[ iAVA ];
2570 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2571 ava->la_attr.bv_len );
2572 l += ava->la_attr.bv_len;
2576 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2578 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2581 l += 2 * ava->la_value.bv_len;
2585 unsigned f = flags | ava->la_flags;
2587 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2592 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2601 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2608 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2609 LDAPAVA *ava = rdn[ iAVA ];
2611 /* len(type) + '=' + ',' | '/' */
2612 l += ava->la_attr.bv_len + 2;
2614 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2615 /* octothorpe + twice the length */
2616 l += 1 + 2 * ava->la_value.bv_len;
2619 unsigned f = flags | ava->la_flags;
2621 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2634 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2639 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2640 LDAPAVA *ava = rdn[ iAVA ];
2645 str[ l++ ] = ( iAVA ? ',' : '/' );
2648 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2649 ava->la_attr.bv_len );
2650 l += ava->la_attr.bv_len;
2654 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2656 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2659 l += 2 * ava->la_value.bv_len;
2662 unsigned f = flags | ava->la_flags;
2664 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2677 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2682 assert( rdn != NULL );
2683 assert( len != NULL );
2687 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2688 LDAPAVA *ava = rdn[ iAVA ];
2691 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2693 /* FIXME: are binary values allowed in UFN? */
2694 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2695 /* octothorpe + twice the value */
2696 l += 1 + 2 * ava->la_value.bv_len;
2700 unsigned f = flags | ava->la_flags;
2702 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2715 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2720 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2721 LDAPAVA *ava = rdn[ iAVA ];
2723 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2725 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2728 l += 2 * ava->la_value.bv_len;
2732 unsigned f = flags | ava->la_flags;
2734 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2740 if ( rdn[ iAVA + 1 ] ) {
2741 AC_MEMCPY( &str[ l ], " + ", 3 );
2745 AC_MEMCPY( &str[ l ], ", ", 2 );
2756 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2761 assert( rdn != NULL );
2762 assert( len != NULL );
2766 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2767 LDAPAVA *ava = rdn[ iAVA ];
2772 /* FIXME: are binary values allowed in UFN? */
2773 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2774 /* octothorpe + twice the value */
2775 l += 1 + 2 * ava->la_value.bv_len;
2778 unsigned f = flags | ava->la_flags;
2780 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2793 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2798 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2799 LDAPAVA *ava = rdn[ iAVA ];
2804 str[ l++ ] = ( iAVA ? ',' : '/' );
2807 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2809 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2812 l += 2 * ava->la_value.bv_len;
2815 unsigned f = flags | ava->la_flags;
2817 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2832 * Returns in str a string representation of rdn based on flags.
2833 * There is some duplication of code between this and ldap_dn2str;
2834 * this is wanted to reduce the allocation of temporary buffers.
2837 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2842 assert( str != NULL );
2844 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2845 return LDAP_PARAM_ERROR;
2848 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2854 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2856 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2860 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2865 assert( bv != NULL );
2870 if ( rdn == NULL ) {
2871 bv->bv_val = LDAP_STRDUPX( "", ctx );
2872 return( LDAP_SUCCESS );
2876 * This routine wastes "back" bytes at the end of the string
2879 switch ( LDAP_DN_FORMAT( flags ) ) {
2880 case LDAP_DN_FORMAT_LDAPV3:
2881 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2882 return LDAP_DECODING_ERROR;
2886 case LDAP_DN_FORMAT_LDAPV2:
2887 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2888 return LDAP_DECODING_ERROR;
2892 case LDAP_DN_FORMAT_UFN:
2893 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2894 return LDAP_DECODING_ERROR;
2898 case LDAP_DN_FORMAT_DCE:
2899 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2900 return LDAP_DECODING_ERROR;
2904 case LDAP_DN_FORMAT_AD_CANONICAL:
2905 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2906 return LDAP_DECODING_ERROR;
2911 return LDAP_PARAM_ERROR;
2914 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2916 switch ( LDAP_DN_FORMAT( flags ) ) {
2917 case LDAP_DN_FORMAT_LDAPV3:
2918 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2922 case LDAP_DN_FORMAT_LDAPV2:
2923 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2927 case LDAP_DN_FORMAT_UFN:
2928 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2932 case LDAP_DN_FORMAT_DCE:
2933 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2937 case LDAP_DN_FORMAT_AD_CANONICAL:
2938 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
2943 /* need at least one of the previous */
2944 return LDAP_PARAM_ERROR;
2948 LDAP_FREEX( bv->bv_val, ctx );
2952 bv->bv_len = l - back;
2953 bv->bv_val[ bv->bv_len ] = '\0';
2955 return LDAP_SUCCESS;
2959 * Very bulk implementation; many optimizations can be performed
2960 * - a NULL dn results in an empty string ""
2963 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
2964 * we must encode it in binary form ('#' + HEXPAIRs)
2965 * b) does DCE/AD support UTF-8?
2966 * no clue; don't think so.
2967 * c) what do we do when binary values must be converted in UTF/DCE/AD?
2968 * use binary encoded BER
2970 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
2975 assert( str != NULL );
2977 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2978 return LDAP_PARAM_ERROR;
2981 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
2986 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
2988 return ldap_dn2bv_x( dn, bv, flags, NULL );
2991 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
2994 int rc = LDAP_ENCODING_ERROR;
2997 /* stringifying helpers for LDAPv3/LDAPv2 */
2998 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
2999 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3001 assert( bv != NULL );
3005 Debug( LDAP_DEBUG_ARGS, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3008 * a null dn means an empty dn string
3009 * FIXME: better raise an error?
3012 bv->bv_val = LDAP_STRDUPX( "", ctx );
3013 return( LDAP_SUCCESS );
3016 switch ( LDAP_DN_FORMAT( flags ) ) {
3017 case LDAP_DN_FORMAT_LDAPV3:
3018 sv2l = strval2strlen;
3022 case LDAP_DN_FORMAT_LDAPV2:
3023 sv2l = strval2IA5strlen;
3024 sv2s = strval2IA5str;
3027 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3029 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3030 goto return_results;
3036 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3037 rc = LDAP_NO_MEMORY;
3041 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3044 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3046 LDAP_FREEX( bv->bv_val, ctx );
3048 goto return_results;
3056 * trim the last ',' (the allocated memory
3057 * is one byte longer than required)
3059 bv->bv_len = len - 1;
3060 bv->bv_val[ bv->bv_len ] = '\0';
3065 case LDAP_DN_FORMAT_UFN: {
3067 * FIXME: quoting from RFC 1781:
3069 To take a distinguished name, and generate a name of this format with
3070 attribute types omitted, the following steps are followed.
3072 1. If the first attribute is of type CommonName, the type may be
3075 2. If the last attribute is of type Country, the type may be
3078 3. If the last attribute is of type Country, the last
3079 Organisation attribute may have the type omitted.
3081 4. All attributes of type OrganisationalUnit may have the type
3082 omitted, unless they are after an Organisation attribute or
3083 the first attribute is of type OrganisationalUnit.
3085 * this should be the pedantic implementation.
3087 * Here the standard implementation reflects
3088 * the one historically provided by OpenLDAP
3089 * (and UMIch, I presume), with the variant
3090 * of spaces and plusses (' + ') separating
3093 * A non-standard but nice implementation could
3094 * be to turn the final "dc" attributes into a
3095 * dot-separated domain.
3097 * Other improvements could involve the use of
3098 * friendly country names and so.
3101 int leftmost_dc = -1;
3103 #endif /* DC_IN_UFN */
3105 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3108 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3109 goto return_results;
3114 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3115 if ( leftmost_dc == -1 ) {
3121 #endif /* DC_IN_UFN */
3124 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3125 rc = LDAP_NO_MEMORY;
3130 if ( leftmost_dc == -1 ) {
3131 #endif /* DC_IN_UFN */
3132 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3135 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3137 LDAP_FREEX( bv->bv_val, ctx );
3139 goto return_results;
3145 * trim the last ', ' (the allocated memory
3146 * is two bytes longer than required)
3148 bv->bv_len = len - 2;
3149 bv->bv_val[ bv->bv_len ] = '\0';
3152 last_iRDN = iRDN - 1;
3154 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3157 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3159 LDAP_FREEX( bv->bv_val, ctx );
3161 goto return_results;
3166 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3167 LDAP_FREEX( bv->bv_val, ctx );
3169 goto return_results;
3172 /* the string is correctly terminated by dn2domain */
3174 #endif /* DC_IN_UFN */
3180 case LDAP_DN_FORMAT_DCE:
3181 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3183 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3184 goto return_results;
3190 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3191 rc = LDAP_NO_MEMORY;
3195 for ( l = 0; iRDN--; ) {
3198 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3200 LDAP_FREEX( bv->bv_val, ctx );
3202 goto return_results;
3210 bv->bv_val[ bv->bv_len ] = '\0';
3215 case LDAP_DN_FORMAT_AD_CANONICAL: {
3216 int trailing_slash = 1;
3219 * Sort of UFN for DCE DNs: a slash ('/') separated
3220 * global->local DN with no types; strictly speaking,
3221 * the naming context should be a domain, which is
3222 * written in DNS-style, e.g. dot-deparated.
3226 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3230 * "microsoft.com/People/Bill,Gates"
3232 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3235 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3236 goto return_results;
3242 /* reserve room for trailing '/' in case the DN
3243 * is exactly a domain */
3244 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3246 rc = LDAP_NO_MEMORY;
3251 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3252 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3257 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3258 flags, &rdnl, 0 ) ) {
3259 LDAP_FREEX( bv->bv_val, ctx );
3261 goto return_results;
3270 * Strictly speaking, AD canonical requires
3271 * a DN to be in the form "..., dc=smtg",
3272 * i.e. terminated by a domain component
3274 if ( flags & LDAP_DN_PEDANTIC ) {
3275 LDAP_FREEX( bv->bv_val, ctx );
3277 rc = LDAP_ENCODING_ERROR;
3281 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3284 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3285 flags, &rdnl, first ) ) {
3286 LDAP_FREEX( bv->bv_val, ctx );
3288 goto return_results;
3297 if ( trailing_slash ) {
3298 /* the DN is exactly a domain -- need a trailing
3299 * slash; room was reserved in advance */
3300 bv->bv_val[ len ] = '/';
3305 bv->bv_val[ bv->bv_len ] = '\0';
3311 return LDAP_PARAM_ERROR;
3314 Debug( LDAP_DEBUG_ARGS, "<= ldap_dn2bv(%s)=%d %s\n",
3315 bv->bv_val, rc, rc ? ldap_err2string( rc ) : "" );
3322 #include <openssl/x509.h>
3323 #include <openssl/err.h>
3325 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3326 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3327 * constructed DN will use numeric OIDs to identify attributeTypes,
3328 * and the func() will be invoked to rewrite the DN with the given
3331 * Otherwise the DN will use shortNames as defined in the OpenSSL
3334 * It's preferable to let slapd do the OID to attributeType mapping,
3335 * because the OpenSSL tables are known to have many typos in versions
3336 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3337 * so we're forced to use OpenSSL's mapping there.
3338 * -- Howard Chu 2002-04-18
3342 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3347 LDAPAVA *newAVA, *baseAVA;
3348 X509_NAME_ENTRY *ne;
3351 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3353 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3355 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3360 assert( bv != NULL );
3364 /* Get the number of AVAs. This is not necessarily the same as
3365 * the number of RDNs.
3367 navas = X509_NAME_entry_count( x509_name );
3369 /* Get the last element, to see how many RDNs there are */
3370 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3371 nrdns = ne->set + 1;
3373 /* Allocate the DN/RDN/AVA stuff as a single block */
3374 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3375 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3376 dnsize += sizeof(LDAPAVA) * navas;
3377 if (dnsize > sizeof(ptrs)) {
3378 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3379 if ( newDN == NULL )
3380 return LDAP_NO_MEMORY;
3382 newDN = (LDAPDN)ptrs;
3385 newDN[nrdns] = NULL;
3386 newRDN = (LDAPRDN)(newDN + nrdns+1);
3387 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3390 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3391 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3392 ne = X509_NAME_get_entry( x509_name, i );
3393 obj = X509_NAME_ENTRY_get_object( ne );
3394 str = X509_NAME_ENTRY_get_data( ne );
3396 /* If set changed, move to next RDN */
3397 if ( set != ne->set ) {
3398 /* If this is not the first time, end the
3399 * previous RDN and advance.
3405 newDN[j++] = newRDN;
3410 newAVA->la_private = NULL;
3411 newAVA->la_flags = LDAP_AVA_STRING;
3414 int n = OBJ_obj2nid( obj );
3418 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3419 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3421 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3422 __etoa( newAVA->la_attr.bv_val );
3423 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3426 get_oid: newAVA->la_attr.bv_val = oidptr;
3427 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3429 __etoa( newAVA->la_attr.bv_val );
3431 oidptr += newAVA->la_attr.bv_len + 1;
3432 oidrem -= newAVA->la_attr.bv_len + 1;
3434 /* Running out of OID buffer space? */
3436 if ( oidsize == 0 ) {
3437 oidsize = sizeof(oids) * 2;
3439 oidbuf = LDAP_MALLOC( oidsize );
3440 if ( oidbuf == NULL ) goto nomem;
3444 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3445 if ( oidbuf == NULL ) goto nomem;
3446 /* Buffer moved! Fix AVA pointers */
3447 if ( old != oidbuf ) {
3449 long dif = oidbuf - old;
3451 for (a=baseAVA; a<=newAVA; a++){
3452 if (a->la_attr.bv_val >= old &&
3453 a->la_attr.bv_val <= (old + oidsize))
3454 a->la_attr.bv_val += dif;
3457 oidptr = oidbuf + oidsize - oidrem;
3463 Val.bv_val = (char *) str->data;
3464 Val.bv_len = str->length;
3465 switch( str->type ) {
3466 case V_ASN1_UNIVERSALSTRING:
3467 /* This uses 32-bit ISO 10646-1 */
3468 csize = 4; goto to_utf8;
3469 case V_ASN1_BMPSTRING:
3470 /* This uses 16-bit ISO 10646-1 */
3471 csize = 2; goto to_utf8;
3472 case V_ASN1_T61STRING:
3473 /* This uses 8-bit, assume ISO 8859-1 */
3475 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3476 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3477 if (rc != LDAP_SUCCESS) goto nomem;
3478 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3480 case V_ASN1_UTF8STRING:
3481 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3482 /* This is already in UTF-8 encoding */
3483 case V_ASN1_IA5STRING:
3484 case V_ASN1_PRINTABLESTRING:
3485 /* These are always 7-bit strings */
3486 newAVA->la_value = Val;
3497 rc = func( newDN, flags, NULL );
3498 if ( rc != LDAP_SUCCESS )
3502 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3505 for (;baseAVA < newAVA; baseAVA++) {
3506 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3507 LDAP_FREE( baseAVA->la_attr.bv_val );
3508 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3509 LDAP_FREE( baseAVA->la_value.bv_val );
3513 LDAP_FREE( oidbuf );
3514 if ( newDN != (LDAPDN) ptrs )
3518 #endif /* HAVE_TLS */