2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4 * Copyright 1998-2004 The OpenLDAP Foundation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted only as authorized by the OpenLDAP
11 * A copy of this license is available in the file LICENSE in the
12 * top-level directory of the distribution or, alternatively, at
13 * <http://www.OpenLDAP.org/license.html>.
15 /* Portions Copyright (c) 1994 Regents of the University of Michigan.
16 * All rights reserved.
23 #include <ac/stdlib.h>
24 #include <ac/socket.h>
25 #include <ac/string.h>
29 #include "ldap_schema.h"
31 /* extension to UFN that turns trailing "dc=value" rdns in DNS style,
32 * e.g. "ou=People,dc=openldap,dc=org" => "People, openldap.org" */
38 /* parsing/printing routines */
39 static int str2strval( const char *str, ber_len_t stoplen, struct berval *val,
40 const char **next, unsigned flags, int *retFlags, void *ctx );
41 static int DCE2strval( const char *str, struct berval *val,
42 const char **next, unsigned flags, void *ctx );
43 static int IA52strval( const char *str, struct berval *val,
44 const char **next, unsigned flags, void *ctx );
45 static int quotedIA52strval( const char *str, struct berval *val,
46 const char **next, unsigned flags, void *ctx );
47 static int hexstr2binval( const char *str, struct berval *val,
48 const char **next, unsigned flags, void *ctx );
49 static int hexstr2bin( const char *str, char *c );
50 static int byte2hexpair( const char *val, char *pair );
51 static int binval2hexstr( struct berval *val, char *str );
52 static int strval2strlen( struct berval *val, unsigned flags,
54 static int strval2str( struct berval *val, char *str, unsigned flags,
56 static int strval2IA5strlen( struct berval *val, unsigned flags,
58 static int strval2IA5str( struct berval *val, char *str, unsigned flags,
60 static int strval2DCEstrlen( struct berval *val, unsigned flags,
62 static int strval2DCEstr( struct berval *val, char *str, unsigned flags,
64 static int strval2ADstrlen( struct berval *val, unsigned flags,
66 static int strval2ADstr( struct berval *val, char *str, unsigned flags,
68 static int dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN );
71 static LDAPAVA * ldapava_new(
72 const struct berval *attr, const struct berval *val, unsigned flags, void *ctx );
74 /* Higher level helpers */
75 static int rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
76 int ( *s2l )( struct berval *, unsigned, ber_len_t * ) );
77 static int rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
78 int ( *s2s )( struct berval *, char *, unsigned, ber_len_t * ));
79 static int rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
80 static int rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len );
81 static int rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
82 static int rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flag, ber_len_t *len, int first );
83 static int rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len );
84 static int rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first );
87 * RFC 1823 ldap_get_dn
90 ldap_get_dn( LDAP *ld, LDAPMessage *entry )
96 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn\n", 0, 0, 0 );
98 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn\n", 0, 0, 0 );
101 assert( ld != NULL );
102 assert( LDAP_VALID(ld) );
103 assert( entry != NULL );
105 tmp = *entry->lm_ber; /* struct copy */
106 if ( ber_scanf( &tmp, "{a" /*}*/, &dn ) == LBER_ERROR ) {
107 ld->ld_errno = LDAP_DECODING_ERROR;
115 ldap_get_dn_ber( LDAP *ld, LDAPMessage *entry, BerElement **berout,
118 BerElement tmp, *ber;
120 int rc = LDAP_SUCCESS;
123 LDAP_LOG ( OPERATION, ENTRY, "ldap_get_dn_ber\n", 0, 0, 0 );
125 Debug( LDAP_DEBUG_TRACE, "ldap_get_dn_ber\n", 0, 0, 0 );
128 assert( ld != NULL );
129 assert( LDAP_VALID(ld) );
130 assert( entry != NULL );
131 assert( dn != NULL );
138 ber = ldap_alloc_ber_with_options( ld );
140 return LDAP_NO_MEMORY;
147 *ber = *entry->lm_ber; /* struct copy */
148 if ( ber_scanf( ber, "{ml{" /*}*/, dn, &len ) == LBER_ERROR ) {
149 rc = ld->ld_errno = LDAP_DECODING_ERROR;
151 if ( rc == LDAP_SUCCESS ) {
152 /* set the length to avoid overrun */
153 rc = ber_set_option( ber, LBER_OPT_REMAINING_BYTES, &len );
154 if( rc != LBER_OPT_SUCCESS ) {
155 rc = ld->ld_errno = LDAP_LOCAL_ERROR;
158 if ( rc != LDAP_SUCCESS && berout ) {
166 * RFC 1823 ldap_dn2ufn
169 ldap_dn2ufn( LDAP_CONST char *dn )
174 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ufn\n", 0, 0, 0 );
176 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ufn\n", 0, 0, 0 );
179 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
180 &out, LDAP_DN_FORMAT_UFN );
186 * RFC 1823 ldap_explode_dn
189 ldap_explode_dn( LDAP_CONST char *dn, int notypes )
192 char **values = NULL;
194 unsigned flag = notypes ? LDAP_DN_FORMAT_UFN : LDAP_DN_FORMAT_LDAPV3;
197 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_dn\n", 0, 0, 0 );
199 Debug( LDAP_DEBUG_TRACE, "ldap_explode_dn\n", 0, 0, 0 );
202 if ( ldap_str2dn( dn, &tmpDN, LDAP_DN_FORMAT_LDAP )
207 if( tmpDN == NULL ) {
208 values = LDAP_MALLOC( sizeof( char * ) );
209 if( values == NULL ) return NULL;
215 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ );
217 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iRDN ) );
218 if ( values == NULL ) {
219 ldap_dnfree( tmpDN );
223 for ( iRDN = 0; tmpDN[ iRDN ]; iRDN++ ) {
224 ldap_rdn2str( tmpDN[ iRDN ], &values[ iRDN ], flag );
226 ldap_dnfree( tmpDN );
227 values[ iRDN ] = NULL;
233 ldap_explode_rdn( LDAP_CONST char *rdn, int notypes )
236 char **values = NULL;
241 LDAP_LOG ( OPERATION, ENTRY, "ldap_explode_rdn\n", 0, 0, 0 );
243 Debug( LDAP_DEBUG_TRACE, "ldap_explode_rdn\n", 0, 0, 0 );
247 * we only parse the first rdn
248 * FIXME: we prefer efficiency over checking if the _ENTIRE_
251 if ( ldap_str2rdn( rdn, &tmpRDN, (char **) &p, LDAP_DN_FORMAT_LDAP )
256 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) ;
257 values = LDAP_MALLOC( sizeof( char * ) * ( 1 + iAVA ) );
258 if ( values == NULL ) {
259 ldap_rdnfree( tmpRDN );
263 for ( iAVA = 0; tmpRDN[ iAVA ]; iAVA++ ) {
264 ber_len_t l = 0, vl, al = 0;
266 LDAPAVA *ava = tmpRDN[ iAVA ];
268 if ( ava->la_flags & LDAP_AVA_BINARY ) {
269 vl = 1 + 2 * ava->la_value.bv_len;
272 if ( strval2strlen( &ava->la_value,
273 ava->la_flags, &vl ) ) {
279 al = ava->la_attr.bv_len;
280 l = vl + ava->la_attr.bv_len + 1;
282 str = LDAP_MALLOC( l + 1 );
283 AC_MEMCPY( str, ava->la_attr.bv_val,
284 ava->la_attr.bv_len );
289 str = LDAP_MALLOC( l + 1 );
292 if ( ava->la_flags & LDAP_AVA_BINARY ) {
294 if ( binval2hexstr( &ava->la_value, &str[ al ] ) ) {
299 if ( strval2str( &ava->la_value, &str[ al ],
300 ava->la_flags, &vl ) ) {
306 values[ iAVA ] = str;
308 values[ iAVA ] = NULL;
310 ldap_rdnfree( tmpRDN );
315 LBER_VFREE( values );
316 ldap_rdnfree( tmpRDN );
321 ldap_dn2dcedn( LDAP_CONST char *dn )
326 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2dcedn\n", 0, 0, 0 );
328 Debug( LDAP_DEBUG_TRACE, "ldap_dn2dcedn\n", 0, 0, 0 );
331 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
332 &out, LDAP_DN_FORMAT_DCE );
338 ldap_dcedn2dn( LDAP_CONST char *dce )
343 LDAP_LOG ( OPERATION, ENTRY, "ldap_dcedn2dn\n", 0, 0, 0 );
345 Debug( LDAP_DEBUG_TRACE, "ldap_dcedn2dn\n", 0, 0, 0 );
348 ( void )ldap_dn_normalize( dce, LDAP_DN_FORMAT_DCE, &out, LDAP_DN_FORMAT_LDAPV3 );
354 ldap_dn2ad_canonical( LDAP_CONST char *dn )
359 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn2ad_canonical\n", 0, 0, 0 );
361 Debug( LDAP_DEBUG_TRACE, "ldap_dn2ad_canonical\n", 0, 0, 0 );
364 ( void )ldap_dn_normalize( dn, LDAP_DN_FORMAT_LDAP,
365 &out, LDAP_DN_FORMAT_AD_CANONICAL );
371 * function that changes the string representation of dnin
372 * from ( fin & LDAP_DN_FORMAT_MASK ) to ( fout & LDAP_DN_FORMAT_MASK )
375 * LDAP_DN_FORMAT_LDAP (rfc 2253 and ldapbis liberal,
376 * plus some rfc 1779)
377 * LDAP_DN_FORMAT_LDAPV3 (rfc 2253 and ldapbis)
378 * LDAP_DN_FORMAT_LDAPV2 (rfc 1779)
379 * LDAP_DN_FORMAT_DCE (?)
381 * fout can be any of the above except
382 * LDAP_DN_FORMAT_LDAP
384 * LDAP_DN_FORMAT_UFN (rfc 1781, partial and with extensions)
385 * LDAP_DN_FORMAT_AD_CANONICAL (?)
388 ldap_dn_normalize( LDAP_CONST char *dnin,
389 unsigned fin, char **dnout, unsigned fout )
395 LDAP_LOG ( OPERATION, ENTRY, "ldap_dn_normalize\n", 0, 0, 0 );
397 Debug( LDAP_DEBUG_TRACE, "ldap_dn_normalize\n", 0, 0, 0 );
404 if ( dnin == NULL ) {
405 return( LDAP_SUCCESS );
408 rc = ldap_str2dn( dnin , &tmpDN, fin );
409 if ( rc != LDAP_SUCCESS ) {
413 rc = ldap_dn2str( tmpDN, dnout, fout );
415 ldap_dnfree( tmpDN );
423 /* #define B4ATTRTYPE 0x0001 */
424 #define B4OIDATTRTYPE 0x0002
425 #define B4STRINGATTRTYPE 0x0003
427 #define B4AVAEQUALS 0x0100
428 #define B4AVASEP 0x0200
429 #define B4RDNSEP 0x0300
430 #define GOTAVA 0x0400
432 #define B4ATTRVALUE 0x0010
433 #define B4STRINGVALUE 0x0020
434 #define B4IA5VALUEQUOTED 0x0030
435 #define B4IA5VALUE 0x0040
436 #define B4BINARYVALUE 0x0050
439 * Helpers (mostly from slap.h)
440 * c is assumed to Unicode in an ASCII compatible format (UTF-8)
441 * Macros assume "C" Locale (ASCII)
443 #define LDAP_DN_ASCII_SPACE(c) \
444 ( (c) == ' ' || (c) == '\t' || (c) == '\n' || (c) == '\r' )
445 #define LDAP_DN_ASCII_LOWER(c) LDAP_LOWER(c)
446 #define LDAP_DN_ASCII_UPPER(c) LDAP_UPPER(c)
447 #define LDAP_DN_ASCII_ALPHA(c) LDAP_ALPHA(c)
449 #define LDAP_DN_ASCII_DIGIT(c) LDAP_DIGIT(c)
450 #define LDAP_DN_ASCII_LCASE_HEXALPHA(c) LDAP_HEXLOWER(c)
451 #define LDAP_DN_ASCII_UCASE_HEXALPHA(c) LDAP_HEXUPPER(c)
452 #define LDAP_DN_ASCII_HEXDIGIT(c) LDAP_HEX(c)
453 #define LDAP_DN_ASCII_ALNUM(c) LDAP_ALNUM(c)
454 #define LDAP_DN_ASCII_PRINTABLE(c) ( (c) >= ' ' && (c) <= '~' )
457 #define LDAP_DN_OID_LEADCHAR(c) LDAP_DIGIT(c)
458 #define LDAP_DN_DESC_LEADCHAR(c) LDAP_ALPHA(c)
459 #define LDAP_DN_DESC_CHAR(c) LDAP_LDH(c)
460 #define LDAP_DN_LANG_SEP(c) ( (c) == ';' )
461 #define LDAP_DN_ATTRDESC_CHAR(c) \
462 ( LDAP_DN_DESC_CHAR(c) || LDAP_DN_LANG_SEP(c) )
464 /* special symbols */
465 #define LDAP_DN_AVA_EQUALS(c) ( (c) == '=' )
466 #define LDAP_DN_AVA_SEP(c) ( (c) == '+' )
467 #define LDAP_DN_RDN_SEP(c) ( (c) == ',' )
468 #define LDAP_DN_RDN_SEP_V2(c) ( LDAP_DN_RDN_SEP(c) || (c) == ';' )
469 #define LDAP_DN_OCTOTHORPE(c) ( (c) == '#' )
470 #define LDAP_DN_QUOTES(c) ( (c) == '\"' )
471 #define LDAP_DN_ESCAPE(c) ( (c) == '\\' )
472 #define LDAP_DN_VALUE_END(c) \
473 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) )
475 /* NOTE: according to draft-ietf-ldapbis-dn, '=' can be escaped
476 * and treated as special, i.e. escaped both as "\<hexpair>" and
477 * as "\=", but it is treated as a regular char, i.e. it can also
480 * As such, we currently choose to allow reading unescaped '=',
481 * but we always produce escaped '\3D'; this may change in the
482 * future, if compatibility issues do not arise */
484 #define LDAP_DN_NE(c) \
485 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
486 || LDAP_DN_QUOTES(c) \
487 || (c) == '<' || (c) == '>' )
488 #define LDAP_DN_MAYESCAPE(c) \
489 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
490 || LDAP_DN_AVA_EQUALS(c) \
491 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
492 #define LDAP_DN_SHOULDESCAPE(c) ( LDAP_DN_AVA_EQUALS(c) )
493 #else /* ! LDAP_DEVEL */
494 #define LDAP_DN_NE(c) \
495 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) \
496 || LDAP_DN_AVA_EQUALS(c) || LDAP_DN_QUOTES(c) \
497 || (c) == '<' || (c) == '>' )
498 #define LDAP_DN_MAYESCAPE(c) \
499 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) \
500 || LDAP_DN_ASCII_SPACE(c) || LDAP_DN_OCTOTHORPE(c) )
501 #define LDAP_DN_SHOULDESCAPE(c) ( 0 )
502 #endif /* ! LDAP_DEVEL */
504 #define LDAP_DN_NEEDESCAPE(c) \
505 ( LDAP_DN_ESCAPE(c) || LDAP_DN_NE(c) )
506 #define LDAP_DN_NEEDESCAPE_LEAD(c) LDAP_DN_MAYESCAPE(c)
507 #define LDAP_DN_NEEDESCAPE_TRAIL(c) \
508 ( LDAP_DN_ASCII_SPACE(c) || LDAP_DN_NEEDESCAPE(c) )
509 #define LDAP_DN_WILLESCAPE_CHAR(c) \
510 ( LDAP_DN_RDN_SEP(c) || LDAP_DN_AVA_SEP(c) || LDAP_DN_ESCAPE(c) )
511 #define LDAP_DN_IS_PRETTY(f) ( (f) & LDAP_DN_PRETTY )
512 #define LDAP_DN_WILLESCAPE_HEX(f, c) \
513 ( ( !LDAP_DN_IS_PRETTY( f ) ) && LDAP_DN_WILLESCAPE_CHAR(c) )
516 #define LDAP_DN_VALUE_END_V2(c) \
517 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_SEP(c) )
519 #define LDAP_DN_V2_SPECIAL(c) \
520 ( LDAP_DN_RDN_SEP_V2(c) || LDAP_DN_AVA_EQUALS(c) \
521 || LDAP_DN_AVA_SEP(c) || (c) == '<' || (c) == '>' \
522 || LDAP_DN_OCTOTHORPE(c) )
523 #define LDAP_DN_V2_PAIR(c) \
524 ( LDAP_DN_V2_SPECIAL(c) || LDAP_DN_ESCAPE(c) || LDAP_DN_QUOTES(c) )
527 * DCE (mostly from Luke Howard and IBM implementation for AIX)
529 * From: "Application Development Guide - Directory Services" (FIXME: add link?)
530 * Here escapes and valid chars for GDS are considered; as soon as more
531 * specific info is found, the macros will be updated.
533 * Chars: 'a'-'z', 'A'-'Z', '0'-'9',
534 * '.', ':', ',', ''', '+', '-', '=', '(', ')', '?', '/', ' '.
536 * Metachars: '/', ',', '=', '\'.
538 * the '\' is used to escape other metachars.
544 * Attribute types must start with alphabetic chars and can contain
545 * alphabetic chars and digits (FIXME: no '-'?). OIDs are allowed.
547 #define LDAP_DN_RDN_SEP_DCE(c) ( (c) == '/' )
548 #define LDAP_DN_AVA_SEP_DCE(c) ( (c) == ',' )
549 #define LDAP_DN_ESCAPE_DCE(c) ( LDAP_DN_ESCAPE(c) )
550 #define LDAP_DN_VALUE_END_DCE(c) \
551 ( LDAP_DN_RDN_SEP_DCE(c) || LDAP_DN_AVA_SEP_DCE(c) )
552 #define LDAP_DN_NEEDESCAPE_DCE(c) \
553 ( LDAP_DN_VALUE_END_DCE(c) || LDAP_DN_AVA_EQUALS(c) )
556 #define LDAP_DN_RDN_SEP_AD(c) ( (c) == '/' )
557 #define LDAP_DN_ESCAPE_AD(c) ( LDAP_DN_ESCAPE(c) )
558 #define LDAP_DN_AVA_SEP_AD(c) ( (c) == ',' ) /* assume same as DCE */
559 #define LDAP_DN_VALUE_END_AD(c) \
560 ( LDAP_DN_RDN_SEP_AD(c) || LDAP_DN_AVA_SEP_AD(c) )
561 #define LDAP_DN_NEEDESCAPE_AD(c) \
562 ( LDAP_DN_VALUE_END_AD(c) || LDAP_DN_AVA_EQUALS(c) )
565 #define LDAP_DN_HEXPAIR(s) \
566 ( LDAP_DN_ASCII_HEXDIGIT((s)[0]) && LDAP_DN_ASCII_HEXDIGIT((s)[1]) )
567 /* better look at the AttributeDescription? */
569 /* FIXME: no composite rdn or non-"dc" types, right?
570 * (what about "dc" in OID form?) */
571 /* FIXME: we do not allow binary values in domain, right? */
572 /* NOTE: use this macro only when ABSOLUTELY SURE rdn IS VALID! */
573 /* NOTE: don't use strcasecmp() as it is locale specific! */
574 #define LDAP_DC_ATTR "dc"
575 #define LDAP_DC_ATTRU "DC"
576 #define LDAP_DN_IS_RDN_DC( r ) \
577 ( (r) && (r)[0] && !(r)[1] \
578 && ((r)[0]->la_flags & LDAP_AVA_STRING) \
579 && ((r)[0]->la_attr.bv_len == 2) \
580 && (((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTR[0]) \
581 || ((r)[0]->la_attr.bv_val[0] == LDAP_DC_ATTRU[0])) \
582 && (((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTR[1]) \
583 || ((r)[0]->la_attr.bv_val[1] == LDAP_DC_ATTRU[1])))
585 /* Composite rules */
586 #define LDAP_DN_ALLOW_ONE_SPACE(f) \
587 ( LDAP_DN_LDAPV2(f) \
588 || !( (f) & LDAP_DN_P_NOSPACEAFTERRDN ) )
589 #define LDAP_DN_ALLOW_SPACES(f) \
590 ( LDAP_DN_LDAPV2(f) \
591 || !( (f) & ( LDAP_DN_P_NOLEADTRAILSPACES | LDAP_DN_P_NOSPACEAFTERRDN ) ) )
592 #define LDAP_DN_LDAP(f) \
593 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAP )
594 #define LDAP_DN_LDAPV3(f) \
595 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV3 )
596 #define LDAP_DN_LDAPV2(f) \
597 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_LDAPV2 )
598 #define LDAP_DN_DCE(f) \
599 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_DCE )
600 #define LDAP_DN_UFN(f) \
601 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_UFN )
602 #define LDAP_DN_ADC(f) \
603 ( ( (f) & LDAP_DN_FORMAT_MASK ) == LDAP_DN_FORMAT_AD_CANONICAL )
604 #define LDAP_DN_FORMAT(f) ( (f) & LDAP_DN_FORMAT_MASK )
607 * LDAPAVA helpers (will become part of the API for operations
608 * on structural representations of DNs).
611 ldapava_new( const struct berval *attr, const struct berval *val,
612 unsigned flags, void *ctx )
619 ava = LDAP_MALLOCX( sizeof( LDAPAVA ) + attr->bv_len + 1, ctx );
622 ava->la_attr.bv_len = attr->bv_len;
623 ava->la_attr.bv_val = (char *)(ava+1);
624 AC_MEMCPY( ava->la_attr.bv_val, attr->bv_val, attr->bv_len );
625 ava->la_attr.bv_val[attr->bv_len] = '\0';
627 ava->la_value = *val;
628 ava->la_flags = flags | LDAP_AVA_FREE_VALUE;
630 ava->la_private = NULL;
637 ldapava_free( LDAPAVA *ava, void *ctx )
642 /* ava's private must be freed by caller
643 * (at present let's skip this check because la_private
644 * basically holds static data) */
645 assert( ava->la_private == NULL );
648 if (ava->la_flags & LDAP_AVA_FREE_VALUE)
649 LDAP_FREEX( ava->la_value.bv_val, ctx );
651 LDAP_FREEX( ava, ctx );
655 ldap_rdnfree( LDAPRDN rdn )
657 ldap_rdnfree_x( rdn, NULL );
661 ldap_rdnfree_x( LDAPRDN rdn, void *ctx )
669 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
670 ldapava_free( rdn[ iAVA ], ctx );
673 LDAP_FREEX( rdn, ctx );
677 ldap_dnfree( LDAPDN dn )
679 ldap_dnfree_x( dn, NULL );
683 ldap_dnfree_x( LDAPDN dn, void *ctx )
691 for ( iRDN = 0; dn[ iRDN ]; iRDN++ ) {
692 ldap_rdnfree_x( dn[ iRDN ], ctx );
695 LDAP_FREEX( dn, ctx );
699 * Converts a string representation of a DN (in LDAPv3, LDAPv2 or DCE)
700 * into a structural representation of the DN, by separating attribute
701 * types and values encoded in the more appropriate form, which is
702 * string or OID for attribute types and binary form of the BER encoded
703 * value or Unicode string. Formats different from LDAPv3 are parsed
704 * according to their own rules and turned into the more appropriate
705 * form according to LDAPv3.
707 * NOTE: I realize the code is getting spaghettish; it is rather
708 * experimental and will hopefully turn into something more simple
709 * and readable as soon as it works as expected.
713 * Default sizes of AVA and RDN static working arrays; if required
714 * the are dynamically resized. The values can be tuned in case
715 * of special requirements (e.g. very deep DN trees or high number
718 #define TMP_AVA_SLOTS 8
719 #define TMP_RDN_SLOTS 32
722 ldap_str2dn( LDAP_CONST char *str, LDAPDN *dn, unsigned flags )
728 bv.bv_len = strlen( str );
729 bv.bv_val = (char *) str;
731 return ldap_bv2dn_x( &bv, dn, flags, NULL );
735 ldap_bv2dn( struct berval *bv, LDAPDN *dn, unsigned flags )
737 return ldap_bv2dn_x( bv, dn, flags, NULL );
741 ldap_bv2dn_x( struct berval *bvin, LDAPDN *dn, unsigned flags, void *ctx )
744 int rc = LDAP_DECODING_ERROR;
748 LDAPRDN newRDN = NULL, tmpDN_[TMP_RDN_SLOTS], *tmpDN = tmpDN_;
749 int num_slots = TMP_RDN_SLOTS;
751 struct berval bvtmp, *bv = &bvtmp;
754 assert( bvin->bv_val );
759 end = str + bv->bv_len;
762 LDAP_LOG ( OPERATION, ARGS, "ldap_bv2dn(%s,%u)\n", str, flags, 0 );
764 Debug( LDAP_DEBUG_TRACE, "=> ldap_bv2dn(%s,%u)\n", str, flags, 0 );
769 switch ( LDAP_DN_FORMAT( flags ) ) {
770 case LDAP_DN_FORMAT_LDAP:
771 case LDAP_DN_FORMAT_LDAPV3:
772 case LDAP_DN_FORMAT_DCE:
775 /* allow DN enclosed in brackets */
776 case LDAP_DN_FORMAT_LDAPV2:
777 if ( str[0] == '<' ) {
778 if ( bv->bv_len < 2 || end[ -1 ] != '>' ) {
779 rc = LDAP_DECODING_ERROR;
789 /* unsupported in str2dn */
790 case LDAP_DN_FORMAT_UFN:
791 case LDAP_DN_FORMAT_AD_CANONICAL:
792 return LDAP_PARAM_ERROR;
794 case LDAP_DN_FORMAT_LBER:
796 return LDAP_PARAM_ERROR;
799 if ( bv->bv_len == 0 ) {
803 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
804 /* value must have embedded NULs */
805 return LDAP_DECODING_ERROR;
809 if ( LDAP_DN_DCE( flags ) ) {
812 * (from Luke Howard: thnx) A RDN separator is required
813 * at the beginning of an (absolute) DN.
815 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
821 * actually we do not want to accept by default the DCE form,
822 * we do not want to auto-detect it
825 } else if ( LDAP_DN_LDAP( flags ) ) {
827 * if dn starts with '/' let's make it a DCE dn
829 if ( LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
830 flags |= LDAP_DN_FORMAT_DCE;
836 for ( ; p < end; p++ ) {
839 tmpbv.bv_len = bv->bv_len - ( p - str );
840 tmpbv.bv_val = (char *)p;
842 err = ldap_bv2rdn_x( &tmpbv, &newRDN, (char **) &p, flags,ctx);
843 if ( err != LDAP_SUCCESS ) {
848 * We expect a rdn separator
850 if ( p < end && p[ 0 ] ) {
851 switch ( LDAP_DN_FORMAT( flags ) ) {
852 case LDAP_DN_FORMAT_LDAPV3:
853 if ( !LDAP_DN_RDN_SEP( p[ 0 ] ) ) {
854 rc = LDAP_DECODING_ERROR;
859 case LDAP_DN_FORMAT_LDAP:
860 case LDAP_DN_FORMAT_LDAPV2:
861 if ( !LDAP_DN_RDN_SEP_V2( p[ 0 ] ) ) {
862 rc = LDAP_DECODING_ERROR;
867 case LDAP_DN_FORMAT_DCE:
868 if ( !LDAP_DN_RDN_SEP_DCE( p[ 0 ] ) ) {
869 rc = LDAP_DECODING_ERROR;
877 tmpDN[nrdns++] = newRDN;
881 * make the static RDN array dynamically rescalable
883 if ( nrdns == num_slots ) {
886 if ( tmpDN == tmpDN_ ) {
887 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPRDN * ), ctx );
892 AC_MEMCPY( tmp, tmpDN, num_slots * sizeof( LDAPRDN * ) );
895 tmp = LDAP_REALLOCX( tmpDN, num_slots * 2 * sizeof( LDAPRDN * ), ctx );
906 if ( p >= end || p[ 0 ] == '\0' ) {
908 * the DN is over, phew
910 newDN = (LDAPDN)LDAP_MALLOCX( sizeof(LDAPRDN *) * (nrdns+1), ctx );
911 if ( newDN == NULL ) {
917 if ( LDAP_DN_DCE( flags ) ) {
918 /* add in reversed order */
919 for ( i=0; i<nrdns; i++ )
920 newDN[i] = tmpDN[nrdns-1-i];
922 for ( i=0; i<nrdns; i++ )
934 ldap_rdnfree_x( newRDN, ctx );
937 for ( nrdns-- ;nrdns >= 0; nrdns-- ) {
938 ldap_rdnfree_x( tmpDN[nrdns], ctx );
943 if ( tmpDN != tmpDN_ ) {
944 LDAP_FREEX( tmpDN, ctx );
948 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_bv2dn(%s)=%d %s\n",
949 str, rc, ldap_err2string( rc ) );
951 Debug( LDAP_DEBUG_TRACE, "<= ldap_bv2dn(%s)=%d %s\n", str, rc,
952 ldap_err2string( rc ) );
962 * Parses a relative DN according to flags up to a rdn separator
963 * or to the end of str.
964 * Returns the rdn and a pointer to the string continuation, which
965 * corresponds to the rdn separator or to '\0' in case the string is over.
968 ldap_str2rdn( LDAP_CONST char *str, LDAPRDN *rdn,
969 char **n_in, unsigned flags )
974 assert( str[ 0 ] != '\0' ); /* FIXME: is this required? */
976 bv.bv_len = strlen( str );
977 bv.bv_val = (char *) str;
979 return ldap_bv2rdn_x( &bv, rdn, n_in, flags, NULL );
983 ldap_bv2rdn( struct berval *bv, LDAPRDN *rdn,
984 char **n_in, unsigned flags )
986 return ldap_bv2rdn_x( bv, rdn, n_in, flags, NULL );
990 ldap_bv2rdn_x( struct berval *bv, LDAPRDN *rdn,
991 char **n_in, unsigned flags, void *ctx )
993 const char **n = (const char **) n_in;
997 int rc = LDAP_DECODING_ERROR;
998 int attrTypeEncoding = LDAP_AVA_STRING,
999 attrValueEncoding = LDAP_AVA_STRING;
1001 struct berval attrType = BER_BVNULL;
1002 struct berval attrValue = BER_BVNULL;
1004 LDAPRDN newRDN = NULL;
1005 LDAPAVA *tmpRDN_[TMP_AVA_SLOTS], **tmpRDN = tmpRDN_;
1006 int num_slots = TMP_AVA_SLOTS;
1012 assert( bv->bv_len );
1013 assert( bv->bv_val );
1014 assert( rdn || flags & LDAP_DN_SKIP );
1018 stoplen = bv->bv_len;
1025 switch ( LDAP_DN_FORMAT( flags ) ) {
1026 case LDAP_DN_FORMAT_LDAP:
1027 case LDAP_DN_FORMAT_LDAPV3:
1028 case LDAP_DN_FORMAT_LDAPV2:
1029 case LDAP_DN_FORMAT_DCE:
1032 /* unsupported in str2dn */
1033 case LDAP_DN_FORMAT_UFN:
1034 case LDAP_DN_FORMAT_AD_CANONICAL:
1035 return LDAP_PARAM_ERROR;
1037 case LDAP_DN_FORMAT_LBER:
1039 return LDAP_PARAM_ERROR;
1042 if ( bv->bv_len == 0 ) {
1043 return LDAP_SUCCESS;
1047 if( memchr( bv->bv_val, '\0', bv->bv_len ) != NULL ) {
1048 /* value must have embedded NULs */
1049 return LDAP_DECODING_ERROR;
1053 for ( ; p[ 0 ] || state == GOTAVA; ) {
1056 * The parser in principle advances one token a time,
1057 * or toggles state if preferable.
1062 * an AttributeType can be encoded as:
1063 * - its string representation; in detail, implementations
1064 * MUST recognize AttributeType string type names listed
1065 * in section 2.3 of draft-ietf-ldapbis-dn-XX.txt, and
1066 * MAY recognize other names.
1067 * - its numeric OID (a dotted decimal string); in detail
1068 * RFC 2253 asserts that ``Implementations MUST allow
1069 * an oid in the attribute type to be prefixed by one
1070 * of the character strings "oid." or "OID."''. As soon
1071 * as draft-ietf-ldapbis-dn-XX.txt obsoletes RFC 2253
1072 * I'm not sure whether this is required or not any
1073 * longer; to be liberal, we still implement it.
1076 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1077 if ( !LDAP_DN_ALLOW_ONE_SPACE( flags ) ) {
1084 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1085 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1090 /* whitespace is allowed (and trimmed) */
1092 while ( p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1097 /* error: we expected an AVA */
1103 if ( LDAP_DN_OID_LEADCHAR( p[ 0 ] ) ) {
1104 state = B4OIDATTRTYPE;
1108 /* else must be alpha */
1109 if ( !LDAP_DN_DESC_LEADCHAR( p[ 0 ] ) ) {
1113 /* LDAPv2 "oid." prefix */
1114 if ( LDAP_DN_LDAPV2( flags ) ) {
1116 * to be overly pedantic, we only accept
1119 if ( flags & LDAP_DN_PEDANTIC ) {
1120 if ( !strncmp( p, "OID.", 4 )
1121 || !strncmp( p, "oid.", 4 ) ) {
1123 state = B4OIDATTRTYPE;
1127 if ( !strncasecmp( p, "oid.", 4 ) ) {
1129 state = B4OIDATTRTYPE;
1135 state = B4STRINGATTRTYPE;
1138 case B4OIDATTRTYPE: {
1139 int err = LDAP_SUCCESS;
1141 attrType.bv_val = ldap_int_parse_numericoid( &p, &err,
1144 if ( err != LDAP_SUCCESS ) {
1147 attrType.bv_len = p - attrType.bv_val;
1149 attrTypeEncoding = LDAP_AVA_BINARY;
1151 state = B4AVAEQUALS;
1155 case B4STRINGATTRTYPE: {
1156 const char *startPos, *endPos = NULL;
1160 * the starting char has been found to be
1161 * a LDAP_DN_DESC_LEADCHAR so we don't re-check it
1162 * FIXME: DCE attr types seem to have a more
1163 * restrictive syntax (no '-' ...)
1165 for ( startPos = p++; p[ 0 ]; p++ ) {
1166 if ( LDAP_DN_DESC_CHAR( p[ 0 ] ) ) {
1170 if ( LDAP_DN_LANG_SEP( p[ 0 ] ) ) {
1173 * RFC 2253 does not explicitly
1174 * allow lang extensions to attribute
1177 if ( flags & LDAP_DN_PEDANTIC ) {
1182 * we trim ';' and following lang
1183 * and so from attribute types
1186 for ( ; LDAP_DN_ATTRDESC_CHAR( p[ 0 ] )
1187 || LDAP_DN_LANG_SEP( p[ 0 ] ); p++ ) {
1195 len = ( endPos ? endPos : p ) - startPos;
1200 attrTypeEncoding = LDAP_AVA_STRING;
1203 * here we need to decide whether to use it as is
1204 * or turn it in OID form; as a consequence, we
1205 * need to decide whether to binary encode the value
1208 state = B4AVAEQUALS;
1210 if ( flags & LDAP_DN_SKIP ) {
1214 attrType.bv_val = (char *)startPos;
1215 attrType.bv_len = len;
1221 /* spaces may not be allowed */
1222 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1223 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1228 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1233 /* need equal sign */
1234 if ( !LDAP_DN_AVA_EQUALS( p[ 0 ] ) ) {
1239 /* spaces may not be allowed */
1240 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1241 if ( !LDAP_DN_ALLOW_SPACES( flags ) ) {
1246 for ( p++; LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1252 * octothorpe means a BER encoded value will follow
1253 * FIXME: I don't think DCE will allow it
1255 if ( LDAP_DN_OCTOTHORPE( p[ 0 ] ) ) {
1257 attrValueEncoding = LDAP_AVA_BINARY;
1258 state = B4BINARYVALUE;
1262 /* STRING value expected */
1265 * if we're pedantic, an attribute type in OID form
1266 * SHOULD imply a BER encoded attribute value; we
1267 * should at least issue a warning
1269 if ( ( flags & LDAP_DN_PEDANTIC )
1270 && ( attrTypeEncoding == LDAP_AVA_BINARY ) ) {
1271 /* OID attrType SHOULD use binary encoding */
1275 attrValueEncoding = LDAP_AVA_STRING;
1278 * LDAPv2 allows the attribute value to be quoted;
1279 * also, IA5 values are expected, in principle
1281 if ( LDAP_DN_LDAPV2( flags ) || LDAP_DN_LDAP( flags ) ) {
1282 if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1284 state = B4IA5VALUEQUOTED;
1288 if ( LDAP_DN_LDAPV2( flags ) ) {
1295 * here STRING means RFC 2253 string
1296 * FIXME: what about DCE strings?
1302 state = B4STRINGVALUE;
1307 if ( hexstr2binval( p, &attrValue, &p, flags, ctx ) ) {
1315 switch ( LDAP_DN_FORMAT( flags ) ) {
1316 case LDAP_DN_FORMAT_LDAP:
1317 case LDAP_DN_FORMAT_LDAPV3:
1318 if ( str2strval( p, stoplen - ( p - str ),
1319 &attrValue, &p, flags,
1320 &attrValueEncoding, ctx ) ) {
1325 case LDAP_DN_FORMAT_DCE:
1326 if ( DCE2strval( p, &attrValue, &p, flags, ctx ) ) {
1339 if ( IA52strval( p, &attrValue, &p, flags, ctx ) ) {
1346 case B4IA5VALUEQUOTED:
1348 /* lead quote already stripped */
1349 if ( quotedIA52strval( p, &attrValue,
1350 &p, flags, ctx ) ) {
1360 if ( !( flags & LDAP_DN_SKIP ) ) {
1364 * we accept empty values
1366 ava = ldapava_new( &attrType, &attrValue,
1367 attrValueEncoding, ctx );
1368 if ( ava == NULL ) {
1369 rc = LDAP_NO_MEMORY;
1372 tmpRDN[navas++] = ava;
1374 attrValue.bv_val = NULL;
1375 attrValue.bv_len = 0;
1378 * prepare room for new AVAs if needed
1380 if (navas == num_slots) {
1383 if ( tmpRDN == tmpRDN_ ) {
1384 tmp = LDAP_MALLOCX( num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1385 if ( tmp == NULL ) {
1386 rc = LDAP_NO_MEMORY;
1389 AC_MEMCPY( tmp, tmpRDN, num_slots * sizeof( LDAPAVA * ) );
1392 tmp = LDAP_REALLOCX( tmpRDN, num_slots * 2 * sizeof( LDAPAVA * ), ctx );
1393 if ( tmp == NULL ) {
1394 rc = LDAP_NO_MEMORY;
1405 * if we got an AVA separator ('+', or ',' for DCE )
1406 * we expect a new AVA for this RDN; otherwise
1407 * we add the RDN to the DN
1409 switch ( LDAP_DN_FORMAT( flags ) ) {
1410 case LDAP_DN_FORMAT_LDAP:
1411 case LDAP_DN_FORMAT_LDAPV3:
1412 case LDAP_DN_FORMAT_LDAPV2:
1413 if ( !LDAP_DN_AVA_SEP( p[ 0 ] ) ) {
1418 case LDAP_DN_FORMAT_DCE:
1419 if ( !LDAP_DN_AVA_SEP_DCE( p[ 0 ] ) ) {
1427 * the RDN is over, phew
1430 if ( !( flags & LDAP_DN_SKIP ) ) {
1431 newRDN = (LDAPRDN)LDAP_MALLOCX(
1432 sizeof(LDAPAVA) * (navas+1), ctx );
1433 if ( newRDN == NULL ) {
1434 rc = LDAP_NO_MEMORY;
1437 AC_MEMCPY( newRDN, tmpRDN, sizeof(LDAPAVA *) * navas);
1438 newRDN[navas] = NULL;
1446 /* they should have been used in an AVA */
1447 attrType.bv_val = NULL;
1448 attrValue.bv_val = NULL;
1463 /* They are set to NULL after they're used in an AVA */
1465 if ( attrValue.bv_val ) {
1466 LDAP_FREEX( attrValue.bv_val, ctx );
1469 for ( navas-- ; navas >= 0; navas-- ) {
1470 ldapava_free( tmpRDN[navas], ctx );
1475 if ( tmpRDN != tmpRDN_ ) {
1476 LDAP_FREEX( tmpRDN, ctx );
1487 * reads in a UTF-8 string value, unescaping stuff:
1488 * '\' + LDAP_DN_NEEDESCAPE(c) -> 'c'
1489 * '\' + HEXPAIR(p) -> unhex(p)
1492 str2strval( const char *str, ber_len_t stoplen, struct berval *val, const char **next, unsigned flags, int *retFlags, void *ctx )
1494 const char *p, *end, *startPos, *endPos = NULL;
1495 ber_len_t len, escapes;
1502 end = str + stoplen;
1503 for ( startPos = p = str, escapes = 0; p < end; p++ ) {
1504 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1506 if ( p[ 0 ] == '\0' ) {
1509 if ( LDAP_DN_MAYESCAPE( p[ 0 ] ) ) {
1514 if ( LDAP_DN_HEXPAIR( p ) ) {
1517 hexstr2bin( p, &c );
1520 if ( !LDAP_DN_ASCII_PRINTABLE( c ) ) {
1523 * we assume the string is UTF-8
1525 *retFlags = LDAP_AVA_NONPRINTABLE;
1532 if ( LDAP_DN_PEDANTIC & flags ) {
1536 * we do not allow escaping
1537 * of chars that don't need
1538 * to and do not belong to
1543 } else if ( !LDAP_DN_ASCII_PRINTABLE( p[ 0 ] ) ) {
1544 if ( p[ 0 ] == '\0' ) {
1547 *retFlags = LDAP_AVA_NONPRINTABLE;
1549 } else if ( ( LDAP_DN_LDAP( flags ) && LDAP_DN_VALUE_END_V2( p[ 0 ] ) )
1550 || ( LDAP_DN_LDAPV3( flags ) && LDAP_DN_VALUE_END( p[ 0 ] ) ) ) {
1553 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] ) ) {
1555 * FIXME: maybe we can add
1556 * escapes if not pedantic?
1563 * we do allow unescaped spaces at the end
1564 * of the value only in non-pedantic mode
1566 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1567 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1568 if ( flags & LDAP_DN_PEDANTIC ) {
1572 /* strip trailing (unescaped) spaces */
1573 for ( endPos = p - 1;
1574 endPos > startPos + 1 &&
1575 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1576 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1583 if ( flags & LDAP_DN_SKIP ) {
1588 * FIXME: test memory?
1590 len = ( endPos ? endPos : p ) - startPos - escapes;
1593 if ( escapes == 0 ) {
1594 if ( *retFlags & LDAP_AVA_NONPRINTABLE ) {
1595 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1596 AC_MEMCPY( val->bv_val, startPos, len );
1597 val->bv_val[ len ] = '\0';
1599 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1605 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1606 for ( s = 0, d = 0; d < len; ) {
1607 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1609 if ( LDAP_DN_MAYESCAPE( startPos[ s ] ) ) {
1610 val->bv_val[ d++ ] =
1613 } else if ( LDAP_DN_HEXPAIR( &startPos[ s ] ) ) {
1616 hexstr2bin( &startPos[ s ], &c );
1617 val->bv_val[ d++ ] = c;
1621 /* we should never get here */
1626 val->bv_val[ d++ ] = startPos[ s++ ];
1630 val->bv_val[ d ] = '\0';
1638 DCE2strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1640 const char *p, *startPos, *endPos = NULL;
1641 ber_len_t len, escapes;
1649 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1650 if ( LDAP_DN_ESCAPE_DCE( p[ 0 ] ) ) {
1652 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
1659 } else if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1664 * FIXME: can we accept anything else? I guess we need
1665 * to stop if a value is not legal
1670 * (unescaped) trailing spaces are trimmed must be silently ignored;
1673 if ( p > startPos + 1 && LDAP_DN_ASCII_SPACE( p[ -1 ] ) &&
1674 !LDAP_DN_ESCAPE( p[ -2 ] ) ) {
1675 if ( flags & LDAP_DN_PEDANTIC ) {
1679 /* strip trailing (unescaped) spaces */
1680 for ( endPos = p - 1;
1681 endPos > startPos + 1 &&
1682 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1683 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1690 if ( flags & LDAP_DN_SKIP ) {
1694 len = ( endPos ? endPos : p ) - startPos - escapes;
1696 if ( escapes == 0 ){
1697 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1702 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1703 for ( s = 0, d = 0; d < len; ) {
1705 * This point is reached only if escapes
1706 * are properly used, so all we need to
1709 if ( LDAP_DN_ESCAPE_DCE( startPos[ s ] ) ) {
1713 val->bv_val[ d++ ] = startPos[ s++ ];
1715 val->bv_val[ d ] = '\0';
1716 assert( strlen( val->bv_val ) == len );
1723 IA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1725 const char *p, *startPos, *endPos = NULL;
1726 ber_len_t len, escapes;
1738 for ( startPos = p = str, escapes = 0; p[ 0 ]; p++ ) {
1739 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1741 if ( p[ 0 ] == '\0' ) {
1745 if ( !LDAP_DN_NEEDESCAPE( p[ 0 ] )
1746 && ( LDAP_DN_PEDANTIC & flags ) ) {
1751 } else if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1756 * FIXME: can we accept anything else? I guess we need
1757 * to stop if a value is not legal
1761 /* strip trailing (unescaped) spaces */
1763 endPos > startPos + 1 &&
1764 LDAP_DN_ASCII_SPACE( endPos[ -1 ] ) &&
1765 !LDAP_DN_ESCAPE( endPos[ -2 ] );
1771 if ( flags & LDAP_DN_SKIP ) {
1775 len = ( endPos ? endPos : p ) - startPos - escapes;
1777 if ( escapes == 0 ) {
1778 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1783 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1784 for ( s = 0, d = 0; d < len; ) {
1785 if ( LDAP_DN_ESCAPE( startPos[ s ] ) ) {
1788 val->bv_val[ d++ ] = startPos[ s++ ];
1790 val->bv_val[ d ] = '\0';
1791 assert( strlen( val->bv_val ) == len );
1798 quotedIA52strval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1800 const char *p, *startPos, *endPos = NULL;
1802 unsigned escapes = 0;
1810 /* initial quote already eaten */
1811 for ( startPos = p = str; p[ 0 ]; p++ ) {
1813 * According to RFC 1779, the quoted value can
1814 * contain escaped as well as unescaped special values;
1815 * as a consequence we tolerate escaped values
1816 * (e.g. '"\,"' -> '\,') and escape unescaped specials
1817 * (e.g. '","' -> '\,').
1819 if ( LDAP_DN_ESCAPE( p[ 0 ] ) ) {
1820 if ( p[ 1 ] == '\0' ) {
1825 if ( !LDAP_DN_V2_PAIR( p[ 0 ] )
1826 && ( LDAP_DN_PEDANTIC & flags ) ) {
1828 * do we allow to escape normal chars?
1829 * LDAPv2 does not allow any mechanism
1830 * for escaping chars with '\' and hex
1837 } else if ( LDAP_DN_QUOTES( p[ 0 ] ) ) {
1839 /* eat closing quotes */
1845 * FIXME: can we accept anything else? I guess we need
1846 * to stop if a value is not legal
1850 if ( endPos == NULL ) {
1854 /* Strip trailing (unescaped) spaces */
1855 for ( ; p[ 0 ] && LDAP_DN_ASCII_SPACE( p[ 0 ] ); p++ ) {
1860 if ( flags & LDAP_DN_SKIP ) {
1864 len = endPos - startPos - escapes;
1865 assert( endPos >= startPos + escapes );
1867 if ( escapes == 0 ) {
1868 val->bv_val = LDAP_STRNDUPX( startPos, len, ctx );
1873 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
1876 for ( s = d = 0; d < len; ) {
1877 if ( LDAP_DN_ESCAPE( str[ s ] ) ) {
1880 val->bv_val[ d++ ] = str[ s++ ];
1882 val->bv_val[ d ] = '\0';
1883 assert( strlen( val->bv_val ) == len );
1890 hexstr2bin( const char *str, char *c )
1900 if ( LDAP_DN_ASCII_DIGIT( c1 ) ) {
1904 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c1 ) ) {
1907 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c1 ) );
1914 if ( LDAP_DN_ASCII_DIGIT( c2 ) ) {
1918 if ( LDAP_DN_ASCII_UCASE_HEXALPHA( c2 ) ) {
1919 *c += c2 - 'A' + 10;
1921 assert( LDAP_DN_ASCII_LCASE_HEXALPHA( c2 ) );
1922 *c += c2 - 'a' + 10;
1930 hexstr2binval( const char *str, struct berval *val, const char **next, unsigned flags, void *ctx )
1932 const char *p, *startPos, *endPos = NULL;
1942 for ( startPos = p = str; p[ 0 ]; p += 2 ) {
1943 switch ( LDAP_DN_FORMAT( flags ) ) {
1944 case LDAP_DN_FORMAT_LDAPV3:
1945 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1950 case LDAP_DN_FORMAT_LDAP:
1951 case LDAP_DN_FORMAT_LDAPV2:
1952 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1957 case LDAP_DN_FORMAT_DCE:
1958 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1964 if ( LDAP_DN_ASCII_SPACE( p[ 0 ] ) ) {
1965 if ( flags & LDAP_DN_PEDANTIC ) {
1970 for ( ; p[ 0 ]; p++ ) {
1971 switch ( LDAP_DN_FORMAT( flags ) ) {
1972 case LDAP_DN_FORMAT_LDAPV3:
1973 if ( LDAP_DN_VALUE_END( p[ 0 ] ) ) {
1978 case LDAP_DN_FORMAT_LDAP:
1979 case LDAP_DN_FORMAT_LDAPV2:
1980 if ( LDAP_DN_VALUE_END_V2( p[ 0 ] ) ) {
1985 case LDAP_DN_FORMAT_DCE:
1986 if ( LDAP_DN_VALUE_END_DCE( p[ 0 ] ) ) {
1995 if ( !LDAP_DN_HEXPAIR( p ) ) {
2003 if ( flags & LDAP_DN_SKIP ) {
2007 len = ( ( endPos ? endPos : p ) - startPos ) / 2;
2009 assert( 2 * len == (ber_len_t) (( endPos ? endPos : p ) - startPos ));
2012 val->bv_val = LDAP_MALLOCX( len + 1, ctx );
2013 if ( val->bv_val == NULL ) {
2014 return( LDAP_NO_MEMORY );
2017 for ( s = 0, d = 0; d < len; s += 2, d++ ) {
2020 hexstr2bin( &startPos[ s ], &c );
2022 val->bv_val[ d ] = c;
2025 val->bv_val[ d ] = '\0';
2031 * convert a byte in a hexadecimal pair
2034 byte2hexpair( const char *val, char *pair )
2036 static const char hexdig[] = "0123456789ABCDEF";
2042 * we assume the string has enough room for the hex encoding
2046 pair[ 0 ] = hexdig[ 0x0f & ( val[ 0 ] >> 4 ) ];
2047 pair[ 1 ] = hexdig[ 0x0f & val[ 0 ] ];
2053 * convert a binary value in hexadecimal pairs
2056 binval2hexstr( struct berval *val, char *str )
2063 if ( val->bv_len == 0 ) {
2068 * we assume the string has enough room for the hex encoding
2072 for ( s = 0, d = 0; s < val->bv_len; s++, d += 2 ) {
2073 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2080 * Length of the string representation, accounting for escaped hex
2084 strval2strlen( struct berval *val, unsigned flags, ber_len_t *len )
2086 ber_len_t l, cl = 1;
2088 int escaped_byte_len = LDAP_DN_IS_PRETTY( flags ) ? 1 : 3;
2089 #ifdef PRETTY_ESCAPE
2090 int escaped_ascii_len = LDAP_DN_IS_PRETTY( flags ) ? 2 : 3;
2091 #endif /* PRETTY_ESCAPE */
2097 if ( val->bv_len == 0 ) {
2101 for ( l = 0, p = val->bv_val; p < val->bv_val + val->bv_len; p += cl ) {
2106 if ( p[ 0 ] == '\0' ) {
2112 cl = LDAP_UTF8_CHARLEN2( p, cl );
2114 /* illegal utf-8 char! */
2117 } else if ( cl > 1 ) {
2120 for ( cnt = 1; cnt < cl; cnt++ ) {
2121 if ( ( p[ cnt ] & 0xc0 ) != 0x80 ) {
2125 l += escaped_byte_len * cl;
2127 } else if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2128 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2129 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2130 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2131 #ifdef PRETTY_ESCAPE
2133 if ( LDAP_DN_WILLESCAPE_HEX( flags, p[ 0 ] ) ) {
2135 if ( LDAP_DN_WILLESCAPE_CHAR( p[ 0 ] ) ) {
2139 * there might be some chars we want
2140 * to escape in form of a couple
2141 * of hexdigits for optimization purposes
2146 l += escaped_ascii_len;
2148 #else /* ! PRETTY_ESCAPE */
2150 #endif /* ! PRETTY_ESCAPE */
2163 * convert to string representation, escaping with hex the UTF-8 stuff;
2164 * assume the destination has enough room for escaping
2167 strval2str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2169 ber_len_t s, d, end;
2175 if ( val->bv_len == 0 ) {
2181 * we assume the string has enough room for the hex encoding
2184 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2190 if ( val->bv_val[ s ] == '\0' ) {
2200 * The length was checked in strval2strlen();
2201 * LDAP_UTF8_CHARLEN() should suffice
2203 cl = LDAP_UTF8_CHARLEN2( &val->bv_val[ s ], cl );
2207 * there might be some chars we want to escape in form
2208 * of a couple of hexdigits for optimization purposes
2210 if ( ( cl > 1 && !LDAP_DN_IS_PRETTY( flags ) )
2211 #ifdef PRETTY_ESCAPE
2213 || LDAP_DN_WILLESCAPE_HEX( flags, val->bv_val[ s ] )
2215 || LDAP_DN_WILLESCAPE_CHAR( val->bv_val[ s ] )
2217 #else /* ! PRETTY_ESCAPE */
2218 || LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2219 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2220 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2221 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) )
2223 #endif /* ! PRETTY_ESCAPE */
2227 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2232 } else if ( cl > 1 ) {
2234 str[ d++ ] = val->bv_val[ s++ ];
2238 #ifdef PRETTY_ESCAPE
2239 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2240 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2241 || ( d == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2242 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2244 if ( !LDAP_DN_IS_PRETTY( flags ) ) {
2245 byte2hexpair( &val->bv_val[ s ], &str[ d ] );
2251 #endif /* PRETTY_ESCAPE */
2252 str[ d++ ] = val->bv_val[ s++ ];
2262 * Length of the IA5 string representation (no UTF-8 allowed)
2265 strval2IA5strlen( struct berval *val, unsigned flags, ber_len_t *len )
2274 if ( val->bv_len == 0 ) {
2278 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2280 * Turn value into a binary encoded BER
2285 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2286 if ( LDAP_DN_NEEDESCAPE( p[ 0 ] )
2287 || LDAP_DN_SHOULDESCAPE( p[ 0 ] )
2288 || ( p == val->bv_val && LDAP_DN_NEEDESCAPE_LEAD( p[ 0 ] ) )
2289 || ( !p[ 1 ] && LDAP_DN_NEEDESCAPE_TRAIL( p[ 0 ] ) ) ) {
2304 * convert to string representation (np UTF-8)
2305 * assume the destination has enough room for escaping
2308 strval2IA5str( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2310 ber_len_t s, d, end;
2316 if ( val->bv_len == 0 ) {
2321 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2323 * Turn value into a binary encoded BER
2330 * we assume the string has enough room for the hex encoding
2334 for ( s = 0, d = 0, end = val->bv_len - 1; s < val->bv_len; ) {
2335 if ( LDAP_DN_NEEDESCAPE( val->bv_val[ s ] )
2336 || LDAP_DN_SHOULDESCAPE( val->bv_val[ s ] )
2337 || ( s == 0 && LDAP_DN_NEEDESCAPE_LEAD( val->bv_val[ s ] ) )
2338 || ( s == end && LDAP_DN_NEEDESCAPE_TRAIL( val->bv_val[ s ] ) ) ) {
2341 str[ d++ ] = val->bv_val[ s++ ];
2351 * Length of the (supposedly) DCE string representation,
2352 * accounting for escaped hex of UTF-8 chars
2355 strval2DCEstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2364 if ( val->bv_len == 0 ) {
2368 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2370 * FIXME: Turn the value into a binary encoded BER?
2375 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2376 if ( LDAP_DN_NEEDESCAPE_DCE( p[ 0 ] ) ) {
2391 * convert to (supposedly) DCE string representation,
2392 * escaping with hex the UTF-8 stuff;
2393 * assume the destination has enough room for escaping
2396 strval2DCEstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2404 if ( val->bv_len == 0 ) {
2409 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2411 * FIXME: Turn the value into a binary encoded BER?
2419 * we assume the string has enough room for the hex encoding
2423 for ( s = 0, d = 0; s < val->bv_len; ) {
2424 if ( LDAP_DN_NEEDESCAPE_DCE( val->bv_val[ s ] ) ) {
2427 str[ d++ ] = val->bv_val[ s++ ];
2437 * Length of the (supposedly) AD canonical string representation,
2438 * accounting for escaped hex of UTF-8 chars
2441 strval2ADstrlen( struct berval *val, unsigned flags, ber_len_t *len )
2450 if ( val->bv_len == 0 ) {
2454 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2456 * FIXME: Turn the value into a binary encoded BER?
2461 for ( l = 0, p = val->bv_val; p[ 0 ]; p++ ) {
2462 if ( LDAP_DN_NEEDESCAPE_AD( p[ 0 ] ) ) {
2477 * convert to (supposedly) AD string representation,
2478 * escaping with hex the UTF-8 stuff;
2479 * assume the destination has enough room for escaping
2482 strval2ADstr( struct berval *val, char *str, unsigned flags, ber_len_t *len )
2490 if ( val->bv_len == 0 ) {
2495 if ( flags & LDAP_AVA_NONPRINTABLE ) {
2497 * FIXME: Turn the value into a binary encoded BER?
2505 * we assume the string has enough room for the hex encoding
2509 for ( s = 0, d = 0; s < val->bv_len; ) {
2510 if ( LDAP_DN_NEEDESCAPE_AD( val->bv_val[ s ] ) ) {
2513 str[ d++ ] = val->bv_val[ s++ ];
2523 * If the DN is terminated by single-AVA RDNs with attribute type of "dc",
2524 * the first part of the AD representation of the DN is written in DNS
2525 * form, i.e. dot separated domain name components (as suggested
2526 * by Luke Howard, http://www.padl.com/~lukeh)
2529 dn2domain( LDAPDN dn, struct berval *bv, int pos, int *iRDN )
2532 int domain = 0, first = 1;
2533 ber_len_t l = 1; /* we move the null also */
2536 /* we are guaranteed there's enough memory in str */
2542 assert( *iRDN >= 0 );
2544 str = bv->bv_val + pos;
2546 for ( i = *iRDN; i >= 0; i-- ) {
2556 if ( !LDAP_DN_IS_RDN_DC( rdn ) ) {
2564 AC_MEMCPY( str, ava->la_value.bv_val,
2565 ava->la_value.bv_len + 1);
2566 l += ava->la_value.bv_len;
2569 AC_MEMCPY( str + ava->la_value.bv_len + 1, bv->bv_val + pos, l);
2570 AC_MEMCPY( str, ava->la_value.bv_val,
2571 ava->la_value.bv_len );
2572 str[ ava->la_value.bv_len ] = '.';
2573 l += ava->la_value.bv_len + 1;
2578 bv->bv_len = pos + l - 1;
2584 rdn2strlen( LDAPRDN rdn, unsigned flags, ber_len_t *len,
2585 int ( *s2l )( struct berval *v, unsigned f, ber_len_t *l ) )
2592 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2593 LDAPAVA *ava = rdn[ iAVA ];
2595 /* len(type) + '=' + '+' | ',' */
2596 l += ava->la_attr.bv_len + 2;
2598 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2599 /* octothorpe + twice the length */
2600 l += 1 + 2 * ava->la_value.bv_len;
2604 unsigned f = flags | ava->la_flags;
2606 if ( ( *s2l )( &ava->la_value, f, &vl ) ) {
2619 rdn2str( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len,
2620 int ( *s2s ) ( struct berval *v, char * s, unsigned f, ber_len_t *l ) )
2625 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2626 LDAPAVA *ava = rdn[ iAVA ];
2628 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2629 ava->la_attr.bv_len );
2630 l += ava->la_attr.bv_len;
2634 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2636 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2639 l += 2 * ava->la_value.bv_len;
2643 unsigned f = flags | ava->la_flags;
2645 if ( ( *s2s )( &ava->la_value, &str[ l ], f, &vl ) ) {
2650 str[ l++ ] = ( rdn[ iAVA + 1] ? '+' : ',' );
2659 rdn2DCEstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2666 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2667 LDAPAVA *ava = rdn[ iAVA ];
2669 /* len(type) + '=' + ',' | '/' */
2670 l += ava->la_attr.bv_len + 2;
2672 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2673 /* octothorpe + twice the length */
2674 l += 1 + 2 * ava->la_value.bv_len;
2677 unsigned f = flags | ava->la_flags;
2679 if ( strval2DCEstrlen( &ava->la_value, f, &vl ) ) {
2692 rdn2DCEstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2697 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2698 LDAPAVA *ava = rdn[ iAVA ];
2703 str[ l++ ] = ( iAVA ? ',' : '/' );
2706 AC_MEMCPY( &str[ l ], ava->la_attr.bv_val,
2707 ava->la_attr.bv_len );
2708 l += ava->la_attr.bv_len;
2712 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2714 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2717 l += 2 * ava->la_value.bv_len;
2720 unsigned f = flags | ava->la_flags;
2722 if ( strval2DCEstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2735 rdn2UFNstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2745 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2746 LDAPAVA *ava = rdn[ iAVA ];
2749 l += ( rdn[ iAVA + 1 ] ? 3 : 2 );
2751 /* FIXME: are binary values allowed in UFN? */
2752 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2753 /* octothorpe + twice the value */
2754 l += 1 + 2 * ava->la_value.bv_len;
2758 unsigned f = flags | ava->la_flags;
2760 if ( strval2strlen( &ava->la_value, f, &vl ) ) {
2773 rdn2UFNstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len )
2778 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2779 LDAPAVA *ava = rdn[ iAVA ];
2781 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2783 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2786 l += 2 * ava->la_value.bv_len;
2790 unsigned f = flags | ava->la_flags;
2792 if ( strval2str( &ava->la_value, &str[ l ], f, &vl ) ) {
2798 if ( rdn[ iAVA + 1 ] ) {
2799 AC_MEMCPY( &str[ l ], " + ", 3 );
2803 AC_MEMCPY( &str[ l ], ", ", 2 );
2814 rdn2ADstrlen( LDAPRDN rdn, unsigned flags, ber_len_t *len )
2824 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2825 LDAPAVA *ava = rdn[ iAVA ];
2830 /* FIXME: are binary values allowed in UFN? */
2831 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2832 /* octothorpe + twice the value */
2833 l += 1 + 2 * ava->la_value.bv_len;
2836 unsigned f = flags | ava->la_flags;
2838 if ( strval2ADstrlen( &ava->la_value, f, &vl ) ) {
2851 rdn2ADstr( LDAPRDN rdn, char *str, unsigned flags, ber_len_t *len, int first )
2856 for ( iAVA = 0; rdn[ iAVA ]; iAVA++ ) {
2857 LDAPAVA *ava = rdn[ iAVA ];
2862 str[ l++ ] = ( iAVA ? ',' : '/' );
2865 if ( ava->la_flags & LDAP_AVA_BINARY ) {
2867 if ( binval2hexstr( &ava->la_value, &str[ l ] ) ) {
2870 l += 2 * ava->la_value.bv_len;
2873 unsigned f = flags | ava->la_flags;
2875 if ( strval2ADstr( &ava->la_value, &str[ l ], f, &vl ) ) {
2890 * Returns in str a string representation of rdn based on flags.
2891 * There is some duplication of code between this and ldap_dn2str;
2892 * this is wanted to reduce the allocation of temporary buffers.
2895 ldap_rdn2str( LDAPRDN rdn, char **str, unsigned flags )
2902 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
2903 return LDAP_PARAM_ERROR;
2906 rc = ldap_rdn2bv_x( rdn, &bv, flags, NULL );
2912 ldap_rdn2bv( LDAPRDN rdn, struct berval *bv, unsigned flags )
2914 return ldap_rdn2bv_x( rdn, bv, flags, NULL );
2918 ldap_rdn2bv_x( LDAPRDN rdn, struct berval *bv, unsigned flags, void *ctx )
2928 if ( rdn == NULL ) {
2929 bv->bv_val = LDAP_STRDUPX( "", ctx );
2930 return( LDAP_SUCCESS );
2934 * This routine wastes "back" bytes at the end of the string
2937 switch ( LDAP_DN_FORMAT( flags ) ) {
2938 case LDAP_DN_FORMAT_LDAPV3:
2939 if ( rdn2strlen( rdn, flags, &l, strval2strlen ) ) {
2940 return LDAP_DECODING_ERROR;
2944 case LDAP_DN_FORMAT_LDAPV2:
2945 if ( rdn2strlen( rdn, flags, &l, strval2IA5strlen ) ) {
2946 return LDAP_DECODING_ERROR;
2950 case LDAP_DN_FORMAT_UFN:
2951 if ( rdn2UFNstrlen( rdn, flags, &l ) ) {
2952 return LDAP_DECODING_ERROR;
2956 case LDAP_DN_FORMAT_DCE:
2957 if ( rdn2DCEstrlen( rdn, flags, &l ) ) {
2958 return LDAP_DECODING_ERROR;
2962 case LDAP_DN_FORMAT_AD_CANONICAL:
2963 if ( rdn2ADstrlen( rdn, flags, &l ) ) {
2964 return LDAP_DECODING_ERROR;
2969 return LDAP_PARAM_ERROR;
2972 bv->bv_val = LDAP_MALLOCX( l + 1, ctx );
2974 switch ( LDAP_DN_FORMAT( flags ) ) {
2975 case LDAP_DN_FORMAT_LDAPV3:
2976 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2str );
2980 case LDAP_DN_FORMAT_LDAPV2:
2981 rc = rdn2str( rdn, bv->bv_val, flags, &l, strval2IA5str );
2985 case LDAP_DN_FORMAT_UFN:
2986 rc = rdn2UFNstr( rdn, bv->bv_val, flags, &l );
2990 case LDAP_DN_FORMAT_DCE:
2991 rc = rdn2DCEstr( rdn, bv->bv_val, flags, &l, 1 );
2995 case LDAP_DN_FORMAT_AD_CANONICAL:
2996 rc = rdn2ADstr( rdn, bv->bv_val, flags, &l, 1 );
3001 /* need at least one of the previous */
3002 return LDAP_PARAM_ERROR;
3006 LDAP_FREEX( bv->bv_val, ctx );
3010 bv->bv_len = l - back;
3011 bv->bv_val[ bv->bv_len ] = '\0';
3013 return LDAP_SUCCESS;
3017 * Very bulk implementation; many optimizations can be performed
3018 * - a NULL dn results in an empty string ""
3021 * a) what do we do if a UTF-8 string must be converted in LDAPv2?
3022 * we must encode it in binary form ('#' + HEXPAIRs)
3023 * b) does DCE/AD support UTF-8?
3024 * no clue; don't think so.
3025 * c) what do we do when binary values must be converted in UTF/DCE/AD?
3026 * use binary encoded BER
3028 int ldap_dn2str( LDAPDN dn, char **str, unsigned flags )
3035 if((flags & LDAP_DN_FORMAT_MASK) == LDAP_DN_FORMAT_LBER) {
3036 return LDAP_PARAM_ERROR;
3039 rc = ldap_dn2bv_x( dn, &bv, flags, NULL );
3044 int ldap_dn2bv( LDAPDN dn, struct berval *bv, unsigned flags )
3046 return ldap_dn2bv_x( dn, bv, flags, NULL );
3049 int ldap_dn2bv_x( LDAPDN dn, struct berval *bv, unsigned flags, void *ctx )
3052 int rc = LDAP_ENCODING_ERROR;
3055 /* stringifying helpers for LDAPv3/LDAPv2 */
3056 int ( *sv2l ) ( struct berval *v, unsigned f, ber_len_t *l );
3057 int ( *sv2s ) ( struct berval *v, char *s, unsigned f, ber_len_t *l );
3064 LDAP_LOG ( OPERATION, ARGS, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3066 Debug( LDAP_DEBUG_TRACE, "=> ldap_dn2bv(%u)\n", flags, 0, 0 );
3070 * a null dn means an empty dn string
3071 * FIXME: better raise an error?
3074 bv->bv_val = LDAP_STRDUPX( "", ctx );
3075 return( LDAP_SUCCESS );
3078 switch ( LDAP_DN_FORMAT( flags ) ) {
3079 case LDAP_DN_FORMAT_LDAPV3:
3080 sv2l = strval2strlen;
3084 case LDAP_DN_FORMAT_LDAPV2:
3085 sv2l = strval2IA5strlen;
3086 sv2s = strval2IA5str;
3089 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3091 if ( rdn2strlen( dn[ iRDN ], flags, &rdnl, sv2l ) ) {
3092 goto return_results;
3098 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3099 rc = LDAP_NO_MEMORY;
3103 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3106 if ( rdn2str( dn[ iRDN ], &bv->bv_val[ l ], flags,
3108 LDAP_FREEX( bv->bv_val, ctx );
3110 goto return_results;
3118 * trim the last ',' (the allocated memory
3119 * is one byte longer than required)
3121 bv->bv_len = len - 1;
3122 bv->bv_val[ bv->bv_len ] = '\0';
3127 case LDAP_DN_FORMAT_UFN: {
3129 * FIXME: quoting from RFC 1781:
3131 To take a distinguished name, and generate a name of this format with
3132 attribute types omitted, the following steps are followed.
3134 1. If the first attribute is of type CommonName, the type may be
3137 2. If the last attribute is of type Country, the type may be
3140 3. If the last attribute is of type Country, the last
3141 Organisation attribute may have the type omitted.
3143 4. All attributes of type OrganisationalUnit may have the type
3144 omitted, unless they are after an Organisation attribute or
3145 the first attribute is of type OrganisationalUnit.
3147 * this should be the pedantic implementation.
3149 * Here the standard implementation reflects
3150 * the one historically provided by OpenLDAP
3151 * (and UMIch, I presume), with the variant
3152 * of spaces and plusses (' + ') separating
3155 * A non-standard but nice implementation could
3156 * be to turn the final "dc" attributes into a
3157 * dot-separated domain.
3159 * Other improvements could involve the use of
3160 * friendly country names and so.
3163 int leftmost_dc = -1;
3165 #endif /* DC_IN_UFN */
3167 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3170 if ( rdn2UFNstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3171 goto return_results;
3176 if ( LDAP_DN_IS_RDN_DC( dn[ iRDN ] ) ) {
3177 if ( leftmost_dc == -1 ) {
3183 #endif /* DC_IN_UFN */
3186 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3187 rc = LDAP_NO_MEMORY;
3192 if ( leftmost_dc == -1 ) {
3193 #endif /* DC_IN_UFN */
3194 for ( l = 0, iRDN = 0; dn[ iRDN ]; iRDN++ ) {
3197 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3199 LDAP_FREEX( bv->bv_val, ctx );
3201 goto return_results;
3207 * trim the last ', ' (the allocated memory
3208 * is two bytes longer than required)
3210 bv->bv_len = len - 2;
3211 bv->bv_val[ bv->bv_len ] = '\0';
3214 last_iRDN = iRDN - 1;
3216 for ( l = 0, iRDN = 0; iRDN < leftmost_dc; iRDN++ ) {
3219 if ( rdn2UFNstr( dn[ iRDN ], &bv->bv_val[ l ],
3221 LDAP_FREEX( bv->bv_val, ctx );
3223 goto return_results;
3228 if ( !dn2domain( dn, bv, l, &last_iRDN ) ) {
3229 LDAP_FREEX( bv->bv_val, ctx );
3231 goto return_results;
3234 /* the string is correctly terminated by dn2domain */
3236 #endif /* DC_IN_UFN */
3242 case LDAP_DN_FORMAT_DCE:
3243 for ( iRDN = 0, len = 0; dn[ iRDN ]; iRDN++ ) {
3245 if ( rdn2DCEstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3246 goto return_results;
3252 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1, ctx ) ) == NULL ) {
3253 rc = LDAP_NO_MEMORY;
3257 for ( l = 0; iRDN--; ) {
3260 if ( rdn2DCEstr( dn[ iRDN ], &bv->bv_val[ l ], flags,
3262 LDAP_FREEX( bv->bv_val, ctx );
3264 goto return_results;
3272 bv->bv_val[ bv->bv_len ] = '\0';
3277 case LDAP_DN_FORMAT_AD_CANONICAL: {
3278 int trailing_slash = 1;
3281 * Sort of UFN for DCE DNs: a slash ('/') separated
3282 * global->local DN with no types; strictly speaking,
3283 * the naming context should be a domain, which is
3284 * written in DNS-style, e.g. dot-deparated.
3288 * "givenName=Bill+sn=Gates,ou=People,dc=microsoft,dc=com"
3292 * "microsoft.com/People/Bill,Gates"
3294 for ( iRDN = 0, len = -1; dn[ iRDN ]; iRDN++ ) {
3297 if ( rdn2ADstrlen( dn[ iRDN ], flags, &rdnl ) ) {
3298 goto return_results;
3304 /* reserve room for trailing '/' in case the DN
3305 * is exactly a domain */
3306 if ( ( bv->bv_val = LDAP_MALLOCX( len + 1 + 1, ctx ) ) == NULL )
3308 rc = LDAP_NO_MEMORY;
3313 if ( iRDN && dn2domain( dn, bv, 0, &iRDN ) != 0 ) {
3314 for ( l = bv->bv_len; iRDN >= 0 ; iRDN-- ) {
3319 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3320 flags, &rdnl, 0 ) ) {
3321 LDAP_FREEX( bv->bv_val, ctx );
3323 goto return_results;
3332 * Strictly speaking, AD canonical requires
3333 * a DN to be in the form "..., dc=smtg",
3334 * i.e. terminated by a domain component
3336 if ( flags & LDAP_DN_PEDANTIC ) {
3337 LDAP_FREEX( bv->bv_val, ctx );
3339 rc = LDAP_ENCODING_ERROR;
3343 for ( l = 0; iRDN >= 0 ; iRDN-- ) {
3346 if ( rdn2ADstr( dn[ iRDN ], &bv->bv_val[ l ],
3347 flags, &rdnl, first ) ) {
3348 LDAP_FREEX( bv->bv_val, ctx );
3350 goto return_results;
3359 if ( trailing_slash ) {
3360 /* the DN is exactly a domain -- need a trailing
3361 * slash; room was reserved in advance */
3362 bv->bv_val[ len ] = '/';
3367 bv->bv_val[ bv->bv_len ] = '\0';
3373 return LDAP_PARAM_ERROR;
3377 LDAP_LOG ( OPERATION, RESULTS, "<= ldap_dn2bv(%s)=%d %s\n",
3378 bv->bv_val, rc, ldap_err2string( rc ) );
3380 Debug( LDAP_DEBUG_TRACE, "<= ldap_dn2bv(%s)=%d %s\n",
3381 bv->bv_val, rc, ldap_err2string( rc ) );
3389 #include <openssl/x509.h>
3390 #include <openssl/err.h>
3392 /* Convert a structured DN from an X.509 certificate into an LDAPV3 DN.
3393 * x509_name must be an (X509_NAME *). If func is non-NULL, the
3394 * constructed DN will use numeric OIDs to identify attributeTypes,
3395 * and the func() will be invoked to rewrite the DN with the given
3398 * Otherwise the DN will use shortNames as defined in the OpenSSL
3401 * It's preferable to let slapd do the OID to attributeType mapping,
3402 * because the OpenSSL tables are known to have many typos in versions
3403 * up to (at least) 0.9.6c. However, the LDAP client has no schema tables,
3404 * so we're forced to use OpenSSL's mapping there.
3405 * -- Howard Chu 2002-04-18
3409 ldap_X509dn2bv( void *x509_name, struct berval *bv, LDAPDN_rewrite_func *func,
3414 LDAPAVA *newAVA, *baseAVA;
3415 X509_NAME_ENTRY *ne;
3418 char oids[8192], *oidptr = oids, *oidbuf = NULL;
3420 int i, j, k = 0, navas, nrdns, rc = LDAP_SUCCESS;
3422 size_t dnsize, oidrem = sizeof(oids), oidsize = 0;
3431 /* Get the number of AVAs. This is not necessarily the same as
3432 * the number of RDNs.
3434 navas = X509_NAME_entry_count( x509_name );
3436 /* Get the last element, to see how many RDNs there are */
3437 ne = X509_NAME_get_entry( x509_name, navas - 1 );
3438 nrdns = ne->set + 1;
3440 /* Allocate the DN/RDN/AVA stuff as a single block */
3441 dnsize = sizeof(LDAPRDN) * (nrdns+1);
3442 dnsize += sizeof(LDAPAVA *) * (navas+nrdns);
3443 dnsize += sizeof(LDAPAVA) * navas;
3444 if (dnsize > sizeof(ptrs)) {
3445 newDN = (LDAPDN)LDAP_MALLOC( dnsize );
3446 if ( newDN == NULL )
3447 return LDAP_NO_MEMORY;
3449 newDN = (LDAPDN)ptrs;
3452 newDN[nrdns] = NULL;
3453 newRDN = (LDAPRDN)(newDN + nrdns+1);
3454 newAVA = (LDAPAVA *)(newRDN + navas + nrdns);
3457 /* Retrieve RDNs in reverse order; LDAP is backwards from X.500. */
3458 for ( i = nrdns - 1, j = 0; i >= 0; i-- ) {
3459 ne = X509_NAME_get_entry( x509_name, i );
3460 obj = X509_NAME_ENTRY_get_object( ne );
3461 str = X509_NAME_ENTRY_get_data( ne );
3463 /* If set changed, move to next RDN */
3464 if ( set != ne->set ) {
3465 /* If this is not the first time, end the
3466 * previous RDN and advance.
3472 newDN[j++] = newRDN;
3477 newAVA->la_private = NULL;
3478 newAVA->la_flags = LDAP_AVA_STRING;
3481 int n = OBJ_obj2nid( obj );
3485 newAVA->la_attr.bv_val = (char *)OBJ_nid2sn( n );
3486 newAVA->la_attr.bv_len = strlen( newAVA->la_attr.bv_val );
3488 newAVA->la_attr.bv_val = LDAP_STRDUP( newAVA->la_attr.bv_val );
3489 __etoa( newAVA->la_attr.bv_val );
3490 newAVA->la_flags |= LDAP_AVA_FREE_ATTR;
3493 get_oid: newAVA->la_attr.bv_val = oidptr;
3494 newAVA->la_attr.bv_len = OBJ_obj2txt( oidptr, oidrem, obj, 1 );
3496 __etoa( newAVA->la_attr.bv_val );
3498 oidptr += newAVA->la_attr.bv_len + 1;
3499 oidrem -= newAVA->la_attr.bv_len + 1;
3501 /* Running out of OID buffer space? */
3503 if ( oidsize == 0 ) {
3504 oidsize = sizeof(oids) * 2;
3506 oidbuf = LDAP_MALLOC( oidsize );
3507 if ( oidbuf == NULL ) goto nomem;
3511 oidbuf = LDAP_REALLOC( oidbuf, oidsize*2 );
3512 if ( oidbuf == NULL ) goto nomem;
3513 /* Buffer moved! Fix AVA pointers */
3514 if ( old != oidbuf ) {
3516 long dif = oidbuf - old;
3518 for (a=baseAVA; a<=newAVA; a++){
3519 if (a->la_attr.bv_val >= old &&
3520 a->la_attr.bv_val <= (old + oidsize))
3521 a->la_attr.bv_val += dif;
3524 oidptr = oidbuf + oidsize - oidrem;
3530 Val.bv_val = (char *) str->data;
3531 Val.bv_len = str->length;
3532 switch( str->type ) {
3533 case V_ASN1_UNIVERSALSTRING:
3534 /* This uses 32-bit ISO 10646-1 */
3535 csize = 4; goto to_utf8;
3536 case V_ASN1_BMPSTRING:
3537 /* This uses 16-bit ISO 10646-1 */
3538 csize = 2; goto to_utf8;
3539 case V_ASN1_T61STRING:
3540 /* This uses 8-bit, assume ISO 8859-1 */
3542 to_utf8: rc = ldap_ucs_to_utf8s( &Val, csize, &newAVA->la_value );
3543 newAVA->la_flags |= LDAP_AVA_FREE_VALUE;
3544 if (rc != LDAP_SUCCESS) goto nomem;
3545 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3547 case V_ASN1_UTF8STRING:
3548 newAVA->la_flags = LDAP_AVA_NONPRINTABLE;
3549 /* This is already in UTF-8 encoding */
3550 case V_ASN1_IA5STRING:
3551 case V_ASN1_PRINTABLESTRING:
3552 /* These are always 7-bit strings */
3553 newAVA->la_value = Val;
3564 rc = func( newDN, flags, NULL );
3565 if ( rc != LDAP_SUCCESS )
3569 rc = ldap_dn2bv_x( newDN, bv, LDAP_DN_FORMAT_LDAPV3, NULL );
3572 for (;baseAVA < newAVA; baseAVA++) {
3573 if (baseAVA->la_flags & LDAP_AVA_FREE_ATTR)
3574 LDAP_FREE( baseAVA->la_attr.bv_val );
3575 if (baseAVA->la_flags & LDAP_AVA_FREE_VALUE)
3576 LDAP_FREE( baseAVA->la_value.bv_val );
3580 LDAP_FREE( oidbuf );
3581 if ( newDN != (LDAPDN) ptrs )
3585 #endif /* HAVE_TLS */