Back to index

lightning-sunbird  0.9+nobinonly
cache.c
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is Mozilla Communicator client code, released
00015  * March 31, 1998.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-1999
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 /*
00038  *  Copyright (c) 1993 The Regents of the University of Michigan.
00039  *  All rights reserved.
00040  */
00041 /*
00042  *  cache.c - generic caching support for LDAP
00043  */
00044 
00045 #include "ldap-int.h"
00046 
00047 /*
00048  * ldap_cache_flush - flush part of the LDAP cache. returns an
00049  * ldap error code (LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT, etc.).
00050  */
00051 
00052 int
00053 LDAP_CALL
00054 ldap_cache_flush( LDAP *ld, const char *dn, const char *filter )
00055 {
00056        if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
00057               return( LDAP_PARAM_ERROR );
00058        }
00059 
00060        if ( dn == NULL ) {
00061               dn = "";
00062        }
00063 
00064        return( (ld->ld_cache_flush)( ld, dn, filter ) );
00065 }
00066 
00067 /*
00068  * nsldapi_add_result_to_cache - add an ldap entry we just read off the network
00069  * to the ldap cache. this routine parses the ber for the entry and
00070  * constructs the appropriate add request. this routine calls the
00071  * cache add routine to actually add the entry.
00072  */
00073 
00074 void
00075 nsldapi_add_result_to_cache( LDAP *ld, LDAPMessage *m )
00076 {
00077        char          *dn;
00078        LDAPMod              **mods;
00079        int           i, max, rc;
00080        char          *a;
00081        BerElement    *ber;
00082        char          buf[50];
00083        struct berval bv;
00084        struct berval *bvp[2];
00085 
00086        LDAPDebug( LDAP_DEBUG_TRACE, "=> nsldapi_add_result_to_cache id %d type %d\n",
00087            m->lm_msgid, m->lm_msgtype, 0 );
00088        if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ||
00089            ld->ld_cache_add == NULL ) {
00090               LDAPDebug( LDAP_DEBUG_TRACE,
00091                   "<= nsldapi_add_result_to_cache not added\n", 0, 0, 0 );
00092               return;
00093        }
00094 
00095 #define GRABSIZE     5
00096 
00097        dn = ldap_get_dn( ld, m );
00098        mods = (LDAPMod **)NSLDAPI_MALLOC( GRABSIZE * sizeof(LDAPMod *) );
00099        max = GRABSIZE;
00100        for ( i = 0, a = ldap_first_attribute( ld, m, &ber ); a != NULL;
00101            a = ldap_next_attribute( ld, m, ber ), i++ ) {
00102               if ( i == (max - 1) ) {
00103                      max += GRABSIZE;
00104                      mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
00105                          sizeof(LDAPMod *) * max );
00106               }
00107 
00108               mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
00109               mods[i]->mod_op = LDAP_MOD_BVALUES;
00110               mods[i]->mod_type = a;
00111               mods[i]->mod_bvalues = ldap_get_values_len( ld, m, a );
00112        }
00113        if ( ber != NULL ) {
00114               ber_free( ber, 0 );
00115        }
00116        if (( rc = LDAP_GET_LDERRNO( ld, NULL, NULL )) != LDAP_SUCCESS ) {
00117               LDAPDebug( LDAP_DEBUG_TRACE,
00118                   "<= nsldapi_add_result_to_cache error: failed to construct mod list (%s)\n",
00119                   ldap_err2string( rc ), 0, 0 );
00120               ldap_mods_free( mods, 1 );
00121               return;
00122        }
00123 
00124        /* update special cachedtime attribute */
00125        if ( i == (max - 1) ) {
00126               max++;
00127               mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
00128                   sizeof(LDAPMod *) * max );
00129        }
00130        mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
00131        mods[i]->mod_op = LDAP_MOD_BVALUES;
00132        mods[i]->mod_type = "cachedtime";
00133        sprintf( buf, "%d", time( NULL ) );
00134        bv.bv_val = buf;
00135        bv.bv_len = strlen( buf );
00136        bvp[0] = &bv;
00137        bvp[1] = NULL;
00138        mods[i]->mod_bvalues = bvp;
00139        mods[++i] = NULL;
00140 
00141        /* msgid of -1 means don't send the result */
00142        rc = (ld->ld_cache_add)( ld, -1, m->lm_msgtype, dn, mods );
00143        LDAPDebug( LDAP_DEBUG_TRACE,
00144            "<= nsldapi_add_result_to_cache added (rc %d)\n", rc, 0, 0 );
00145 }