Back to index

openldap  2.4.31
entry.c
Go to the documentation of this file.
00001 /* entry.c - routines for dealing with entries */
00002 /* $OpenLDAP$ */
00003 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00004  *
00005  * Copyright 1998-2012 The OpenLDAP Foundation.
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted only as authorized by the OpenLDAP
00010  * Public License.
00011  *
00012  * A copy of this license is available in the file LICENSE in the
00013  * top-level directory of the distribution or, alternatively, at
00014  * <http://www.OpenLDAP.org/license.html>.
00015  */
00016 /* Portions Copyright (c) 1995 Regents of the University of Michigan.
00017  * All rights reserved.
00018  *
00019  * Redistribution and use in source and binary forms are permitted
00020  * provided that this notice is preserved and that due credit is given
00021  * to the University of Michigan at Ann Arbor. The name of the University
00022  * may not be used to endorse or promote products derived from this
00023  * software without specific prior written permission. This software
00024  * is provided ``as is'' without express or implied warranty.
00025  */
00026 
00027 #include "portable.h"
00028 
00029 #include <stdio.h>
00030 
00031 #include <ac/ctype.h>
00032 #include <ac/errno.h>
00033 #include <ac/socket.h>
00034 #include <ac/string.h>
00035 
00036 #include "slap.h"
00037 #include "ldif.h"
00038 
00039 static char          *ebuf; /* buf returned by entry2str               */
00040 static char          *ecur; /* pointer to end of currently used ebuf */
00041 static int           emaxsize;/* max size of ebuf                      */
00042 
00043 /*
00044  * Empty root entry
00045  */
00046 const Entry slap_entry_root = {
00047        NOID, { 0, "" }, { 0, "" }, NULL, 0, { 0, "" }, NULL
00048 };
00049 
00050 /*
00051  * these mutexes must be used when calling the entry2str()
00052  * routine since it returns a pointer to static data.
00053  */
00054 ldap_pvt_thread_mutex_t     entry2str_mutex;
00055 
00056 static const struct berval dn_bv = BER_BVC("dn");
00057 
00058 /*
00059  * Entry free list
00060  *
00061  * Allocate in chunks, minimum of 1000 at a time.
00062  */
00063 #define       CHUNK_SIZE    1000
00064 typedef struct slap_list {
00065        struct slap_list *next;
00066 } slap_list;
00067 static slap_list *entry_chunks;
00068 static Entry *entry_list;
00069 static ldap_pvt_thread_mutex_t entry_mutex;
00070 
00071 int entry_destroy(void)
00072 {
00073        slap_list *e;
00074        if ( ebuf ) free( ebuf );
00075        ebuf = NULL;
00076        ecur = NULL;
00077        emaxsize = 0;
00078 
00079        for ( e=entry_chunks; e; e=entry_chunks ) {
00080               entry_chunks = e->next;
00081               free( e );
00082        }
00083 
00084        ldap_pvt_thread_mutex_destroy( &entry_mutex );
00085        ldap_pvt_thread_mutex_destroy( &entry2str_mutex );
00086        return attr_destroy();
00087 }
00088 
00089 int
00090 entry_init(void)
00091 {
00092        ldap_pvt_thread_mutex_init( &entry2str_mutex );
00093        ldap_pvt_thread_mutex_init( &entry_mutex );
00094        return attr_init();
00095 }
00096 
00097 Entry *
00098 str2entry( char *s )
00099 {
00100        return str2entry2( s, 1 );
00101 }
00102 
00103 #define bvcasematch(bv1, bv2)      (ber_bvstrcasecmp(bv1, bv2) == 0)
00104 
00105 Entry *
00106 str2entry2( char *s, int checkvals )
00107 {
00108        int rc;
00109        Entry         *e;
00110        struct berval *type, *vals, *nvals;
00111        char   *freeval;
00112        AttributeDescription *ad, *ad_prev;
00113        const char *text;
00114        char   *next;
00115        int           attr_cnt;
00116        int           i, lines;
00117        Attribute     ahead, *atail;
00118 
00119        /*
00120         * LDIF is used as the string format.
00121         * An entry looks like this:
00122         *
00123         *     dn: <dn>\n
00124         *     [<attr>:[:] <value>\n]
00125         *     [<tab><continuedvalue>\n]*
00126         *     ...
00127         *
00128         * If a double colon is used after a type, it means the
00129         * following value is encoded as a base 64 string.  This
00130         * happens if the value contains a non-printing character
00131         * or newline.
00132         */
00133 
00134        Debug( LDAP_DEBUG_TRACE, "=> str2entry: \"%s\"\n",
00135               s ? s : "NULL", 0, 0 );
00136 
00137        e = entry_alloc();
00138 
00139        if( e == NULL ) {
00140               Debug( LDAP_DEBUG_ANY,
00141                      "<= str2entry NULL (entry allocation failed)\n",
00142                      0, 0, 0 );
00143               return( NULL );
00144        }
00145 
00146        /* initialize entry */
00147        e->e_id = NOID;
00148 
00149        /* dn + attributes */
00150        atail = &ahead;
00151        ahead.a_next = NULL;
00152        ad = NULL;
00153        ad_prev = NULL;
00154        attr_cnt = 0;
00155        next = s;
00156 
00157        lines = ldif_countlines( s );
00158        type = ch_calloc( 1, (lines+1)*3*sizeof(struct berval)+lines );
00159        vals = type+lines+1;
00160        nvals = vals+lines+1;
00161        freeval = (char *)(nvals+lines+1);
00162        i = -1;
00163 
00164        /* parse into individual values, record DN */
00165        while ( (s = ldif_getline( &next )) != NULL ) {
00166               int freev;
00167               if ( *s == '\n' || *s == '\0' ) {
00168                      break;
00169               }
00170               i++;
00171               if (i >= lines) {
00172                      Debug( LDAP_DEBUG_TRACE,
00173                             "<= str2entry ran past end of entry\n", 0, 0, 0 );
00174                      goto fail;
00175               }
00176 
00177               rc = ldif_parse_line2( s, type+i, vals+i, &freev );
00178               freeval[i] = freev;
00179               if ( rc ) {
00180                      Debug( LDAP_DEBUG_TRACE,
00181                             "<= str2entry NULL (parse_line)\n", 0, 0, 0 );
00182                      continue;
00183               }
00184 
00185               if ( bvcasematch( &type[i], &dn_bv ) ) {
00186                      if ( e->e_dn != NULL ) {
00187                             Debug( LDAP_DEBUG_ANY, "str2entry: "
00188                                    "entry %ld has multiple DNs \"%s\" and \"%s\"\n",
00189                                    (long) e->e_id, e->e_dn, vals[i].bv_val );
00190                             goto fail;
00191                      }
00192 
00193                      rc = dnPrettyNormal( NULL, &vals[i], &e->e_name, &e->e_nname, NULL );
00194                      if( rc != LDAP_SUCCESS ) {
00195                             Debug( LDAP_DEBUG_ANY, "str2entry: "
00196                                    "entry %ld has invalid DN \"%s\"\n",
00197                                    (long) e->e_id, vals[i].bv_val, 0 );
00198                             goto fail;
00199                      }
00200                      if ( freeval[i] ) free( vals[i].bv_val );
00201                      vals[i].bv_val = NULL;
00202                      i--;
00203                      continue;
00204               }
00205        }
00206        lines = i+1;
00207 
00208        /* check to make sure there was a dn: line */
00209        if ( BER_BVISNULL( &e->e_name )) {
00210               Debug( LDAP_DEBUG_ANY, "str2entry: entry %ld has no dn\n",
00211                      (long) e->e_id, 0, 0 );
00212               goto fail;
00213        }
00214 
00215        /* Make sure all attributes with multiple values are contiguous */
00216        if ( checkvals ) {
00217               int j, k;
00218               struct berval bv;
00219               int fv;
00220 
00221               for (i=0; i<lines; i++) {
00222                      for ( j=i+1; j<lines; j++ ) {
00223                             if ( bvcasematch( type+i, type+j )) {
00224                                    /* out of order, move intervening attributes down */
00225                                    if ( j != i+1 ) {
00226                                           bv = vals[j];
00227                                           fv = freeval[j];
00228                                           for ( k=j; k>i; k-- ) {
00229                                                  type[k] = type[k-1];
00230                                                  vals[k] = vals[k-1];
00231                                                  freeval[k] = freeval[k-1];
00232                                           }
00233                                           k++;
00234                                           type[k] = type[i];
00235                                           vals[k] = bv;
00236                                           freeval[k] = fv;
00237                                    }
00238                                    i++;
00239                             }
00240                      }
00241               }
00242        }
00243 
00244        if ( lines > 0 ) {
00245               for ( i=0; i<=lines; i++ ) {
00246                      ad_prev = ad;
00247                      if ( !ad || ( i<lines && !bvcasematch( type+i, &ad->ad_cname ))) {
00248                             ad = NULL;
00249                             rc = slap_bv2ad( type+i, &ad, &text );
00250        
00251                             if( rc != LDAP_SUCCESS ) {
00252                                    Debug( slapMode & SLAP_TOOL_MODE
00253                                           ? LDAP_DEBUG_ANY : LDAP_DEBUG_TRACE,
00254                                           "<= str2entry: str2ad(%s): %s\n", type[i].bv_val, text, 0 );
00255                                    if( slapMode & SLAP_TOOL_MODE ) {
00256                                           goto fail;
00257                                    }
00258        
00259                                    rc = slap_bv2undef_ad( type+i, &ad, &text, 0 );
00260                                    if( rc != LDAP_SUCCESS ) {
00261                                           Debug( LDAP_DEBUG_ANY,
00262                                                  "<= str2entry: slap_str2undef_ad(%s): %s\n",
00263                                                         type[i].bv_val, text, 0 );
00264                                           goto fail;
00265                                    }
00266                             }
00267        
00268                             /* require ';binary' when appropriate (ITS#5071) */
00269                             if ( slap_syntax_is_binary( ad->ad_type->sat_syntax ) && !slap_ad_is_binary( ad ) ) {
00270                                    Debug( LDAP_DEBUG_ANY,
00271                                           "str2entry: attributeType %s #%d: "
00272                                           "needs ';binary' transfer as per syntax %s\n", 
00273                                           ad->ad_cname.bv_val, 0,
00274                                           ad->ad_type->sat_syntax->ssyn_oid );
00275                                    goto fail;
00276                             }
00277                      }
00278        
00279                      if (( ad_prev && ad != ad_prev ) || ( i == lines )) {
00280                             int j, k;
00281                             /* FIXME: we only need this when migrating from an unsorted DB */
00282                             if ( atail != &ahead && atail->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
00283                                    rc = slap_sort_vals( (Modifications *)atail, &text, &j, NULL );
00284                                    if ( rc == LDAP_SUCCESS ) {
00285                                           atail->a_flags |= SLAP_ATTR_SORTED_VALS;
00286                                    } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
00287                                           Debug( LDAP_DEBUG_ANY,
00288                                                  "str2entry: attributeType %s value #%d provided more than once\n",
00289                                                  atail->a_desc->ad_cname.bv_val, j, 0 );
00290                                           goto fail;
00291                                    }
00292                             }
00293                             atail->a_next = attr_alloc( NULL );
00294                             atail = atail->a_next;
00295                             atail->a_flags = 0;
00296                             atail->a_numvals = attr_cnt;
00297                             atail->a_desc = ad_prev;
00298                             atail->a_vals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
00299                             if( ad_prev->ad_type->sat_equality &&
00300                                    ad_prev->ad_type->sat_equality->smr_normalize )
00301                                    atail->a_nvals = ch_malloc( (attr_cnt + 1) * sizeof(struct berval));
00302                             else
00303                                    atail->a_nvals = NULL;
00304                             k = i - attr_cnt;
00305                             for ( j=0; j<attr_cnt; j++ ) {
00306                                    if ( freeval[k] )
00307                                           atail->a_vals[j] = vals[k];
00308                                    else
00309                                           ber_dupbv( atail->a_vals+j, &vals[k] );
00310                                    vals[k].bv_val = NULL;
00311                                    if ( atail->a_nvals ) {
00312                                           atail->a_nvals[j] = nvals[k];
00313                                           nvals[k].bv_val = NULL;
00314                                    }
00315                                    k++;
00316                             }
00317                             BER_BVZERO( &atail->a_vals[j] );
00318                             if ( atail->a_nvals ) {
00319                                    BER_BVZERO( &atail->a_nvals[j] );
00320                             } else {
00321                                    atail->a_nvals = atail->a_vals;
00322                             }
00323                             attr_cnt = 0;
00324                             if ( i == lines ) break;
00325                      }
00326        
00327                      if ( BER_BVISNULL( &vals[i] ) ) {
00328                             Debug( LDAP_DEBUG_ANY,
00329                                    "str2entry: attributeType %s #%d: "
00330                                    "no value\n", 
00331                                    ad->ad_cname.bv_val, attr_cnt, 0 );
00332                             goto fail;
00333                      }
00334        
00335                      if( slapMode & SLAP_TOOL_MODE ) {
00336                             struct berval pval;
00337                             slap_syntax_validate_func *validate =
00338                                    ad->ad_type->sat_syntax->ssyn_validate;
00339                             slap_syntax_transform_func *pretty =
00340                                    ad->ad_type->sat_syntax->ssyn_pretty;
00341        
00342                             if ( pretty ) {
00343                                    rc = ordered_value_pretty( ad,
00344                                           &vals[i], &pval, NULL );
00345        
00346                             } else if ( validate ) {
00347                                    /*
00348                                     * validate value per syntax
00349                                     */
00350                                    rc = ordered_value_validate( ad, &vals[i], LDAP_MOD_ADD );
00351        
00352                             } else {
00353                                    Debug( LDAP_DEBUG_ANY,
00354                                           "str2entry: attributeType %s #%d: "
00355                                           "no validator for syntax %s\n", 
00356                                           ad->ad_cname.bv_val, attr_cnt,
00357                                           ad->ad_type->sat_syntax->ssyn_oid );
00358                                    goto fail;
00359                             }
00360        
00361                             if( rc != 0 ) {
00362                                    Debug( LDAP_DEBUG_ANY,
00363                                           "str2entry: invalid value "
00364                                           "for attributeType %s #%d (syntax %s)\n",
00365                                           ad->ad_cname.bv_val, attr_cnt,
00366                                           ad->ad_type->sat_syntax->ssyn_oid );
00367                                    goto fail;
00368                             }
00369        
00370                             if( pretty ) {
00371                                    if ( freeval[i] ) free( vals[i].bv_val );
00372                                    vals[i] = pval;
00373                                    freeval[i] = 1;
00374                             }
00375                      }
00376        
00377                      if ( ad->ad_type->sat_equality &&
00378                             ad->ad_type->sat_equality->smr_normalize )
00379                      {
00380                             rc = ordered_value_normalize(
00381                                    SLAP_MR_VALUE_OF_ATTRIBUTE_SYNTAX,
00382                                    ad,
00383                                    ad->ad_type->sat_equality,
00384                                    &vals[i], &nvals[i], NULL );
00385        
00386                             if ( rc ) {
00387                                    Debug( LDAP_DEBUG_ANY,
00388                                           "<= str2entry NULL (smr_normalize %s %d)\n", ad->ad_cname.bv_val, rc, 0 );
00389                                    goto fail;
00390                             }
00391                      }
00392        
00393                      attr_cnt++;
00394               }
00395        }
00396 
00397        free( type );
00398        atail->a_next = NULL;
00399        e->e_attrs = ahead.a_next;
00400 
00401        Debug(LDAP_DEBUG_TRACE, "<= str2entry(%s) -> 0x%lx\n",
00402               e->e_dn, (unsigned long) e, 0 );
00403        return( e );
00404 
00405 fail:
00406        for ( i=0; i<lines; i++ ) {
00407               if ( freeval[i] ) free( vals[i].bv_val );
00408               free( nvals[i].bv_val );
00409        }
00410        free( type );
00411        entry_free( e );
00412        return NULL;
00413 }
00414 
00415 
00416 #define GRABSIZE     BUFSIZ
00417 
00418 #define MAKE_SPACE( n )     { \
00419               while ( ecur + (n) > ebuf + emaxsize ) { \
00420                      ptrdiff_t     offset; \
00421                      offset = (int) (ecur - ebuf); \
00422                      ebuf = ch_realloc( ebuf, \
00423                             emaxsize + GRABSIZE ); \
00424                      emaxsize += GRABSIZE; \
00425                      ecur = ebuf + offset; \
00426               } \
00427        }
00428 
00429 /* NOTE: only preserved for binary compatibility */
00430 char *
00431 entry2str(
00432        Entry  *e,
00433        int           *len )
00434 {
00435        return entry2str_wrap( e, len, LDIF_LINE_WIDTH );
00436 }
00437 
00438 char *
00439 entry2str_wrap(
00440        Entry         *e,
00441        int                  *len,
00442        ber_len_t     wrap )
00443 {
00444        Attribute     *a;
00445        struct berval *bv;
00446        int           i;
00447        ber_len_t tmplen;
00448 
00449        assert( e != NULL );
00450 
00451        /*
00452         * In string format, an entry looks like this:
00453         *     dn: <dn>\n
00454         *     [<attr>: <value>\n]*
00455         */
00456 
00457        ecur = ebuf;
00458 
00459        /* put the dn */
00460        if ( e->e_dn != NULL ) {
00461               /* put "dn: <dn>" */
00462               tmplen = e->e_name.bv_len;
00463               MAKE_SPACE( LDIF_SIZE_NEEDED( 2, tmplen ));
00464               ldif_sput_wrap( &ecur, LDIF_PUT_VALUE, "dn", e->e_dn, tmplen, wrap );
00465        }
00466 
00467        /* put the attributes */
00468        for ( a = e->e_attrs; a != NULL; a = a->a_next ) {
00469               /* put "<type>:[:] <value>" line for each value */
00470               for ( i = 0; a->a_vals[i].bv_val != NULL; i++ ) {
00471                      bv = &a->a_vals[i];
00472                      tmplen = a->a_desc->ad_cname.bv_len;
00473                      MAKE_SPACE( LDIF_SIZE_NEEDED( tmplen, bv->bv_len ));
00474                      ldif_sput_wrap( &ecur, LDIF_PUT_VALUE,
00475                             a->a_desc->ad_cname.bv_val,
00476                             bv->bv_val, bv->bv_len, wrap );
00477               }
00478        }
00479        MAKE_SPACE( 1 );
00480        *ecur = '\0';
00481        *len = ecur - ebuf;
00482 
00483        return( ebuf );
00484 }
00485 
00486 void
00487 entry_clean( Entry *e )
00488 {
00489        /* free an entry structure */
00490        assert( e != NULL );
00491 
00492        /* e_private must be freed by the caller */
00493        assert( e->e_private == NULL );
00494 
00495        e->e_id = 0;
00496 
00497        /* free DNs */
00498        if ( !BER_BVISNULL( &e->e_name ) ) {
00499               free( e->e_name.bv_val );
00500               BER_BVZERO( &e->e_name );
00501        }
00502        if ( !BER_BVISNULL( &e->e_nname ) ) {
00503               free( e->e_nname.bv_val );
00504               BER_BVZERO( &e->e_nname );
00505        }
00506 
00507        if ( !BER_BVISNULL( &e->e_bv ) ) {
00508               free( e->e_bv.bv_val );
00509               BER_BVZERO( &e->e_bv );
00510        }
00511 
00512        /* free attributes */
00513        if ( e->e_attrs ) {
00514               attrs_free( e->e_attrs );
00515               e->e_attrs = NULL;
00516        }
00517 
00518        e->e_ocflags = 0;
00519 }
00520 
00521 void
00522 entry_free( Entry *e )
00523 {
00524        entry_clean( e );
00525 
00526        ldap_pvt_thread_mutex_lock( &entry_mutex );
00527        e->e_private = entry_list;
00528        entry_list = e;
00529        ldap_pvt_thread_mutex_unlock( &entry_mutex );
00530 }
00531 
00532 /* These parameters work well on AMD64 */
00533 #if 0
00534 #define       STRIDE 8
00535 #define       STRIPE 5
00536 #else
00537 #define       STRIDE 1
00538 #define       STRIPE 1
00539 #endif
00540 #define       STRIDE_FACTOR (STRIDE*STRIPE)
00541 
00542 int
00543 entry_prealloc( int num )
00544 {
00545        Entry *e, **prev, *tmp;
00546        slap_list *s;
00547        int i, j;
00548 
00549        if (!num) return 0;
00550 
00551 #if STRIDE_FACTOR > 1
00552        /* Round up to our stride factor */
00553        num += STRIDE_FACTOR-1;
00554        num /= STRIDE_FACTOR;
00555        num *= STRIDE_FACTOR;
00556 #endif
00557 
00558        s = ch_calloc( 1, sizeof(slap_list) + num * sizeof(Entry));
00559        s->next = entry_chunks;
00560        entry_chunks = s;
00561 
00562        prev = &tmp;
00563        for (i=0; i<STRIPE; i++) {
00564               e = (Entry *)(s+1);
00565               e += i;
00566               for (j=i; j<num; j+= STRIDE) {
00567                      *prev = e;
00568                      prev = (Entry **)&e->e_private;
00569                      e += STRIDE;
00570               }
00571        }
00572        *prev = entry_list;
00573        entry_list = (Entry *)(s+1);
00574 
00575        return 0;
00576 }
00577 
00578 Entry *
00579 entry_alloc( void )
00580 {
00581        Entry *e;
00582 
00583        ldap_pvt_thread_mutex_lock( &entry_mutex );
00584        if ( !entry_list )
00585               entry_prealloc( CHUNK_SIZE );
00586        e = entry_list;
00587        entry_list = e->e_private;
00588        e->e_private = NULL;
00589        ldap_pvt_thread_mutex_unlock( &entry_mutex );
00590 
00591        return e;
00592 }
00593 
00594 
00595 /*
00596  * These routines are used only by Backend.
00597  *
00598  * the Entry has three entry points (ways to find things):
00599  *
00600  *     by entry      e.g., if you already have an entry from the cache
00601  *                   and want to delete it. (really by entry ptr)
00602  *     by dn         e.g., when looking for the base object of a search
00603  *     by id         e.g., for search candidates
00604  *
00605  * these correspond to three different avl trees that are maintained.
00606  */
00607 
00608 int
00609 entry_cmp( Entry *e1, Entry *e2 )
00610 {
00611        return SLAP_PTRCMP( e1, e2 );
00612 }
00613 
00614 int
00615 entry_dn_cmp( const void *v_e1, const void *v_e2 )
00616 {
00617        /* compare their normalized UPPERCASED dn's */
00618        const Entry *e1 = v_e1, *e2 = v_e2;
00619 
00620        return ber_bvcmp( &e1->e_nname, &e2->e_nname );
00621 }
00622 
00623 int
00624 entry_id_cmp( const void *v_e1, const void *v_e2 )
00625 {
00626        const Entry *e1 = v_e1, *e2 = v_e2;
00627        return( e1->e_id < e2->e_id ? -1 : (e1->e_id > e2->e_id ? 1 : 0) );
00628 }
00629 
00630 /* This is like a ber_len */
00631 #define entry_lenlen(l)     (((l) < 0x80) ? 1 : ((l) < 0x100) ? 2 : \
00632        ((l) < 0x10000) ? 3 : ((l) < 0x1000000) ? 4 : 5)
00633 
00634 static void
00635 entry_putlen(unsigned char **buf, ber_len_t len)
00636 {
00637        ber_len_t lenlen = entry_lenlen(len);
00638 
00639        if (lenlen == 1) {
00640               **buf = (unsigned char) len;
00641        } else {
00642               int i;
00643               **buf = 0x80 | ((unsigned char) lenlen - 1);
00644               for (i=lenlen-1; i>0; i--) {
00645                      (*buf)[i] = (unsigned char) len;
00646                      len >>= 8;
00647               }
00648        }
00649        *buf += lenlen;
00650 }
00651 
00652 static ber_len_t
00653 entry_getlen(unsigned char **buf)
00654 {
00655        ber_len_t len;
00656        int i;
00657 
00658        len = *(*buf)++;
00659        if (len <= 0x7f)
00660               return len;
00661        i = len & 0x7f;
00662        len = 0;
00663        for (;i > 0; i--) {
00664               len <<= 8;
00665               len |= *(*buf)++;
00666        }
00667        return len;
00668 }
00669 
00670 /* Count up the sizes of the components of an entry */
00671 void entry_partsize(Entry *e, ber_len_t *plen,
00672        int *pnattrs, int *pnvals, int norm)
00673 {
00674        ber_len_t len, dnlen, ndnlen;
00675        int i, nat = 0, nval = 0;
00676        Attribute *a;
00677 
00678        dnlen = e->e_name.bv_len;
00679        len = dnlen + 1;     /* trailing NUL byte */
00680        len += entry_lenlen(dnlen);
00681        if (norm) {
00682               ndnlen = e->e_nname.bv_len;
00683               len += ndnlen + 1;
00684               len += entry_lenlen(ndnlen);
00685        }
00686        for (a=e->e_attrs; a; a=a->a_next) {
00687               /* For AttributeDesc, we only store the attr name */
00688               nat++;
00689               len += a->a_desc->ad_cname.bv_len+1;
00690               len += entry_lenlen(a->a_desc->ad_cname.bv_len);
00691               for (i=0; a->a_vals[i].bv_val; i++) {
00692                      nval++;
00693                      len += a->a_vals[i].bv_len + 1;
00694                      len += entry_lenlen(a->a_vals[i].bv_len);
00695               }
00696               len += entry_lenlen(i);
00697               nval++;       /* empty berval at end */
00698               if (norm && a->a_nvals != a->a_vals) {
00699                      for (i=0; a->a_nvals[i].bv_val; i++) {
00700                             nval++;
00701                             len += a->a_nvals[i].bv_len + 1;
00702                             len += entry_lenlen(a->a_nvals[i].bv_len);
00703                      }
00704                      len += entry_lenlen(i);     /* i nvals */
00705                      nval++;
00706               } else {
00707                      len += entry_lenlen(0);     /* 0 nvals */
00708               }
00709        }
00710        len += entry_lenlen(nat);
00711        len += entry_lenlen(nval);
00712        *plen = len;
00713        *pnattrs = nat;
00714        *pnvals = nval;
00715 }
00716 
00717 /* Add up the size of the entry for a flattened buffer */
00718 ber_len_t entry_flatsize(Entry *e, int norm)
00719 {
00720        ber_len_t len;
00721        int nattrs, nvals;
00722 
00723        entry_partsize(e, &len, &nattrs, &nvals, norm);
00724        len += sizeof(Entry) + (nattrs * sizeof(Attribute)) +
00725               (nvals * sizeof(struct berval));
00726        return len;
00727 }
00728 
00729 /* Flatten an Entry into a buffer. The buffer is filled with just the
00730  * strings/bervals of all the entry components. Each field is preceded
00731  * by its length, encoded the way ber_put_len works. Every field is NUL
00732  * terminated.  The entire buffer size is precomputed so that a single
00733  * malloc can be performed. The entry size is also recorded,
00734  * to aid in entry_decode.
00735  */
00736 int entry_encode(Entry *e, struct berval *bv)
00737 {
00738        ber_len_t len, dnlen, ndnlen, i;
00739        int nattrs, nvals;
00740        Attribute *a;
00741        unsigned char *ptr;
00742 
00743        Debug( LDAP_DEBUG_TRACE, "=> entry_encode(0x%08lx): %s\n",
00744               (long) e->e_id, e->e_dn, 0 );
00745 
00746        dnlen = e->e_name.bv_len;
00747        ndnlen = e->e_nname.bv_len;
00748 
00749        entry_partsize( e, &len, &nattrs, &nvals, 1 );
00750 
00751        bv->bv_len = len;
00752        bv->bv_val = ch_malloc(len);
00753        ptr = (unsigned char *)bv->bv_val;
00754        entry_putlen(&ptr, nattrs);
00755        entry_putlen(&ptr, nvals);
00756        entry_putlen(&ptr, dnlen);
00757        AC_MEMCPY(ptr, e->e_dn, dnlen);
00758        ptr += dnlen;
00759        *ptr++ = '\0';
00760        entry_putlen(&ptr, ndnlen);
00761        AC_MEMCPY(ptr, e->e_ndn, ndnlen);
00762        ptr += ndnlen;
00763        *ptr++ = '\0';
00764 
00765        for (a=e->e_attrs; a; a=a->a_next) {
00766               entry_putlen(&ptr, a->a_desc->ad_cname.bv_len);
00767               AC_MEMCPY(ptr, a->a_desc->ad_cname.bv_val,
00768                      a->a_desc->ad_cname.bv_len);
00769               ptr += a->a_desc->ad_cname.bv_len;
00770               *ptr++ = '\0';
00771               if (a->a_vals) {
00772                      for (i=0; a->a_vals[i].bv_val; i++);
00773                      assert( i == a->a_numvals );
00774                      entry_putlen(&ptr, i);
00775                      for (i=0; a->a_vals[i].bv_val; i++) {
00776                             entry_putlen(&ptr, a->a_vals[i].bv_len);
00777                             AC_MEMCPY(ptr, a->a_vals[i].bv_val,
00778                                    a->a_vals[i].bv_len);
00779                             ptr += a->a_vals[i].bv_len;
00780                             *ptr++ = '\0';
00781                      }
00782                      if (a->a_nvals != a->a_vals) {
00783                             entry_putlen(&ptr, i);
00784                             for (i=0; a->a_nvals[i].bv_val; i++) {
00785                                    entry_putlen(&ptr, a->a_nvals[i].bv_len);
00786                                    AC_MEMCPY(ptr, a->a_nvals[i].bv_val,
00787                                    a->a_nvals[i].bv_len);
00788                                    ptr += a->a_nvals[i].bv_len;
00789                                    *ptr++ = '\0';
00790                             }
00791                      } else {
00792                             entry_putlen(&ptr, 0);
00793                      }
00794               }
00795        }
00796 
00797        Debug( LDAP_DEBUG_TRACE, "<= entry_encode(0x%08lx): %s\n",
00798               (long) e->e_id, e->e_dn, 0 );
00799 
00800        return 0;
00801 }
00802 
00803 /* Retrieve an Entry that was stored using entry_encode above.
00804  * First entry_header must be called to decode the size of the entry.
00805  * Then a single block of memory must be malloc'd to accomodate the
00806  * bervals and the bulk data. Next the bulk data is retrieved from
00807  * the DB and parsed by entry_decode.
00808  *
00809  * Note: everything is stored in a single contiguous block, so
00810  * you can not free individual attributes or names from this
00811  * structure. Attempting to do so will likely corrupt memory.
00812  */
00813 int entry_header(EntryHeader *eh)
00814 {
00815        unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
00816 
00817        /* Some overlays can create empty entries
00818         * so don't check for zeros here.
00819         */
00820        eh->nattrs = entry_getlen(&ptr);
00821        eh->nvals = entry_getlen(&ptr);
00822        eh->data = (char *)ptr;
00823        return LDAP_SUCCESS;
00824 }
00825 
00826 int
00827 entry_decode_dn( EntryHeader *eh, struct berval *dn, struct berval *ndn )
00828 {
00829        int i;
00830        unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
00831 
00832        assert( dn != NULL || ndn != NULL );
00833 
00834        ptr = (unsigned char *)eh->data;
00835        i = entry_getlen(&ptr);
00836        if ( dn != NULL ) {
00837               dn->bv_val = (char *) ptr;
00838               dn->bv_len = i;
00839        }
00840 
00841        if ( ndn != NULL ) {
00842               ptr += i + 1;
00843               i = entry_getlen(&ptr);
00844               ndn->bv_val = (char *) ptr;
00845               ndn->bv_len = i;
00846        }
00847 
00848        Debug( LDAP_DEBUG_TRACE,
00849               "entry_decode_dn: \"%s\"\n",
00850               dn ? dn->bv_val : ndn->bv_val, 0, 0 );
00851 
00852        return 0;
00853 }
00854 
00855 #ifdef SLAP_ZONE_ALLOC
00856 int entry_decode(EntryHeader *eh, Entry **e, void *ctx)
00857 #else
00858 int entry_decode(EntryHeader *eh, Entry **e)
00859 #endif
00860 {
00861        int i, j, nattrs, nvals;
00862        int rc;
00863        Attribute *a;
00864        Entry *x;
00865        const char *text;
00866        AttributeDescription *ad;
00867        unsigned char *ptr = (unsigned char *)eh->bv.bv_val;
00868        BerVarray bptr;
00869 
00870        nattrs = eh->nattrs;
00871        nvals = eh->nvals;
00872        x = entry_alloc();
00873        x->e_attrs = attrs_alloc( nattrs );
00874        ptr = (unsigned char *)eh->data;
00875        i = entry_getlen(&ptr);
00876        x->e_name.bv_val = (char *) ptr;
00877        x->e_name.bv_len = i;
00878        ptr += i+1;
00879        i = entry_getlen(&ptr);
00880        x->e_nname.bv_val = (char *) ptr;
00881        x->e_nname.bv_len = i;
00882        ptr += i+1;
00883        Debug( LDAP_DEBUG_TRACE,
00884               "entry_decode: \"%s\"\n",
00885               x->e_dn, 0, 0 );
00886        x->e_bv = eh->bv;
00887 
00888        a = x->e_attrs;
00889        bptr = (BerVarray)eh->bv.bv_val;
00890 
00891        while ((i = entry_getlen(&ptr))) {
00892               struct berval bv;
00893               bv.bv_len = i;
00894               bv.bv_val = (char *) ptr;
00895               ad = NULL;
00896               rc = slap_bv2ad( &bv, &ad, &text );
00897 
00898               if( rc != LDAP_SUCCESS ) {
00899                      Debug( LDAP_DEBUG_TRACE,
00900                             "<= entry_decode: str2ad(%s): %s\n", ptr, text, 0 );
00901                      rc = slap_bv2undef_ad( &bv, &ad, &text, 0 );
00902 
00903                      if( rc != LDAP_SUCCESS ) {
00904                             Debug( LDAP_DEBUG_ANY,
00905                                    "<= entry_decode: slap_str2undef_ad(%s): %s\n",
00906                                           ptr, text, 0 );
00907                             return rc;
00908                      }
00909               }
00910               ptr += i + 1;
00911               a->a_desc = ad;
00912               a->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
00913               j = entry_getlen(&ptr);
00914               a->a_numvals = j;
00915               a->a_vals = bptr;
00916 
00917               while (j) {
00918                      i = entry_getlen(&ptr);
00919                      bptr->bv_len = i;
00920                      bptr->bv_val = (char *)ptr;
00921                      ptr += i+1;
00922                      bptr++;
00923                      j--;
00924               }
00925               bptr->bv_val = NULL;
00926               bptr->bv_len = 0;
00927               bptr++;
00928 
00929               j = entry_getlen(&ptr);
00930               if (j) {
00931                      a->a_nvals = bptr;
00932                      while (j) {
00933                             i = entry_getlen(&ptr);
00934                             bptr->bv_len = i;
00935                             bptr->bv_val = (char *)ptr;
00936                             ptr += i+1;
00937                             bptr++;
00938                             j--;
00939                      }
00940                      bptr->bv_val = NULL;
00941                      bptr->bv_len = 0;
00942                      bptr++;
00943               } else {
00944                      a->a_nvals = a->a_vals;
00945               }
00946               /* FIXME: This is redundant once a sorted entry is saved into the DB */
00947               if ( a->a_desc->ad_type->sat_flags & SLAP_AT_SORTED_VAL ) {
00948                      rc = slap_sort_vals( (Modifications *)a, &text, &j, NULL );
00949                      if ( rc == LDAP_SUCCESS ) {
00950                             a->a_flags |= SLAP_ATTR_SORTED_VALS;
00951                      } else if ( rc == LDAP_TYPE_OR_VALUE_EXISTS ) {
00952                             /* should never happen */
00953                             Debug( LDAP_DEBUG_ANY,
00954                                    "entry_decode: attributeType %s value #%d provided more than once\n",
00955                                    a->a_desc->ad_cname.bv_val, j, 0 );
00956                             return rc;
00957                      }
00958               }
00959               a = a->a_next;
00960               nattrs--;
00961               if ( !nattrs )
00962                      break;
00963        }
00964 
00965        Debug(LDAP_DEBUG_TRACE, "<= entry_decode(%s)\n",
00966               x->e_dn, 0, 0 );
00967        *e = x;
00968        return 0;
00969 }
00970 
00971 Entry *
00972 entry_dup2( Entry *dest, Entry *source )
00973 {
00974        assert( dest != NULL );
00975        assert( source != NULL );
00976 
00977        assert( dest->e_private == NULL );
00978 
00979        dest->e_id = source->e_id;
00980        ber_dupbv( &dest->e_name, &source->e_name );
00981        ber_dupbv( &dest->e_nname, &source->e_nname );
00982        dest->e_attrs = attrs_dup( source->e_attrs );
00983        dest->e_ocflags = source->e_ocflags;
00984 
00985        return dest;
00986 }
00987 
00988 Entry *
00989 entry_dup( Entry *e )
00990 {
00991        return entry_dup2( entry_alloc(), e );
00992 }
00993 
00994 #if 1
00995 /* Duplicates an entry using a single malloc. Saves CPU time, increases
00996  * heap usage because a single large malloc is harder to satisfy than
00997  * lots of small ones, and the freed space isn't as easily reusable.
00998  *
00999  * Probably not worth using this function.
01000  */
01001 Entry *entry_dup_bv( Entry *e )
01002 {
01003        ber_len_t len;
01004        int nattrs, nvals;
01005        Entry *ret;
01006        struct berval *bvl;
01007        char *ptr;
01008        Attribute *src, *dst;
01009 
01010        ret = entry_alloc();
01011 
01012        entry_partsize(e, &len, &nattrs, &nvals, 1);
01013        ret->e_id = e->e_id;
01014        ret->e_attrs = attrs_alloc( nattrs );
01015        ret->e_ocflags = e->e_ocflags;
01016        ret->e_bv.bv_len = len + nvals * sizeof(struct berval);
01017        ret->e_bv.bv_val = ch_malloc( ret->e_bv.bv_len );
01018 
01019        bvl = (struct berval *)ret->e_bv.bv_val;
01020        ptr = (char *)(bvl + nvals);
01021 
01022        ret->e_name.bv_len = e->e_name.bv_len;
01023        ret->e_name.bv_val = ptr;
01024        AC_MEMCPY( ptr, e->e_name.bv_val, e->e_name.bv_len );
01025        ptr += e->e_name.bv_len;
01026        *ptr++ = '\0';
01027 
01028        ret->e_nname.bv_len = e->e_nname.bv_len;
01029        ret->e_nname.bv_val = ptr;
01030        AC_MEMCPY( ptr, e->e_nname.bv_val, e->e_nname.bv_len );
01031        ptr += e->e_name.bv_len;
01032        *ptr++ = '\0';
01033 
01034        dst = ret->e_attrs;
01035        for (src = e->e_attrs; src; src=src->a_next,dst=dst->a_next ) {
01036               int i;
01037               dst->a_desc = src->a_desc;
01038               dst->a_flags = SLAP_ATTR_DONT_FREE_DATA | SLAP_ATTR_DONT_FREE_VALS;
01039               dst->a_vals = bvl;
01040               dst->a_numvals = src->a_numvals;
01041               for ( i=0; src->a_vals[i].bv_val; i++ ) {
01042                      bvl->bv_len = src->a_vals[i].bv_len;
01043                      bvl->bv_val = ptr;
01044                      AC_MEMCPY( ptr, src->a_vals[i].bv_val, bvl->bv_len );
01045                      ptr += bvl->bv_len;
01046                      *ptr++ = '\0';
01047                      bvl++;
01048               }
01049               BER_BVZERO(bvl);
01050               bvl++;
01051               if ( src->a_vals != src->a_nvals ) {
01052                      dst->a_nvals = bvl;
01053                      for ( i=0; src->a_nvals[i].bv_val; i++ ) {
01054                             bvl->bv_len = src->a_nvals[i].bv_len;
01055                             bvl->bv_val = ptr;
01056                             AC_MEMCPY( ptr, src->a_nvals[i].bv_val, bvl->bv_len );
01057                             ptr += bvl->bv_len;
01058                             *ptr++ = '\0';
01059                             bvl++;
01060                      }
01061                      BER_BVZERO(bvl);
01062                      bvl++;
01063               }
01064        }
01065        return ret;
01066 }
01067 #endif