1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A/*
1N/A * The contents of this file are subject to the Netscape Public
1N/A * License Version 1.1 (the "License"); you may not use this file
1N/A * except in compliance with the License. You may obtain a copy of
1N/A * the License at http://www.mozilla.org/NPL/
1N/A *
1N/A * Software distributed under the License is distributed on an "AS
1N/A * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
1N/A * implied. See the License for the specific language governing
1N/A * rights and limitations under the License.
1N/A *
1N/A * The Original Code is Mozilla Communicator client code, released
1N/A * March 31, 1998.
1N/A *
1N/A * The Initial Developer of the Original Code is Netscape
1N/A * Communications Corporation. Portions created by Netscape are
1N/A * Copyright (C) 1998-1999 Netscape Communications Corporation. All
1N/A * Rights Reserved.
1N/A *
1N/A * Contributor(s):
1N/A */
1N/A/*
1N/A * Copyright (c) 1993 The Regents of the University of Michigan.
1N/A * All rights reserved.
1N/A */
1N/A/*
1N/A * cache.c - generic caching support for LDAP
1N/A */
1N/A
1N/A#include "ldap-int.h"
1N/A
1N/A/*
1N/A * ldap_cache_flush - flush part of the LDAP cache. returns an
1N/A * ldap error code (LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT, etc.).
1N/A */
1N/A
1N/Aint
1N/ALDAP_CALL
1N/Aldap_cache_flush( LDAP *ld, const char *dn, const char *filter )
1N/A{
1N/A if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
1N/A return( LDAP_PARAM_ERROR );
1N/A }
1N/A
1N/A if ( dn == NULL ) {
1N/A dn = "";
1N/A }
1N/A
1N/A return( (ld->ld_cache_flush)( ld, dn, filter ) );
1N/A}
1N/A
1N/A/*
1N/A * nsldapi_add_result_to_cache - add an ldap entry we just read off the network
1N/A * to the ldap cache. this routine parses the ber for the entry and
1N/A * constructs the appropriate add request. this routine calls the
1N/A * cache add routine to actually add the entry.
1N/A */
1N/A
1N/Avoid
1N/Ansldapi_add_result_to_cache( LDAP *ld, LDAPMessage *m )
1N/A{
1N/A char *dn;
1N/A LDAPMod **mods;
1N/A int i, max, rc;
1N/A char *a;
1N/A BerElement *ber;
1N/A char buf[50];
1N/A struct berval bv;
1N/A struct berval *bvp[2];
1N/A
1N/A LDAPDebug( LDAP_DEBUG_TRACE, "=> nsldapi_add_result_to_cache id %d type %d\n",
1N/A m->lm_msgid, m->lm_msgtype, 0 );
1N/A if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ||
1N/A ld->ld_cache_add == NULL ) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache not added\n", 0, 0, 0 );
1N/A return;
1N/A }
1N/A
1N/A#define GRABSIZE 5
1N/A
1N/A dn = ldap_get_dn( ld, m );
1N/A mods = (LDAPMod **)NSLDAPI_MALLOC( GRABSIZE * sizeof(LDAPMod *) );
1N/A if (mods == NULL) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache malloc failed\n", 0, 0, 0 );
1N/A return;
1N/A }
1N/A max = GRABSIZE;
1N/A for ( i = 0, a = ldap_first_attribute( ld, m, &ber ); a != NULL;
1N/A a = ldap_next_attribute( ld, m, ber ), i++ ) {
1N/A if ( i == (max - 1) ) {
1N/A max += GRABSIZE;
1N/A mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
1N/A sizeof(LDAPMod *) * max );
1N/A if (mods == NULL) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache realloc failed\n",
1N/A 0, 0, 0 );
1N/A return;
1N/A }
1N/A }
1N/A
1N/A mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
1N/A if (mods[i] == NULL) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache calloc failed\n",
1N/A 0, 0, 0 );
1N/A ldap_mods_free( mods, 1 );
1N/A return;
1N/A }
1N/A mods[i]->mod_op = LDAP_MOD_BVALUES;
1N/A mods[i]->mod_type = a;
1N/A mods[i]->mod_bvalues = ldap_get_values_len( ld, m, a );
1N/A }
1N/A if ( ber != NULL ) {
1N/A ber_free( ber, 0 );
1N/A }
1N/A if (( rc = LDAP_GET_LDERRNO( ld, NULL, NULL )) != LDAP_SUCCESS ) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache error: failed to construct mod list (%s)\n",
1N/A ldap_err2string( rc ), 0, 0 );
1N/A ldap_mods_free( mods, 1 );
1N/A return;
1N/A }
1N/A
1N/A /* update special cachedtime attribute */
1N/A if ( i == (max - 1) ) {
1N/A max++;
1N/A mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
1N/A sizeof(LDAPMod *) * max );
1N/A if (mods == NULL) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache calloc failed\n",
1N/A 0, 0, 0 );
1N/A ldap_mods_free( mods, 1 );
1N/A return;
1N/A }
1N/A }
1N/A mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
1N/A if (mods[i] == NULL) {
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache calloc failed\n",
1N/A 0, 0, 0 );
1N/A ldap_mods_free( mods, 1 );
1N/A return;
1N/A }
1N/A mods[i]->mod_op = LDAP_MOD_BVALUES;
1N/A mods[i]->mod_type = "cachedtime";
1N/A sprintf( buf, "%d", time( NULL ) );
1N/A bv.bv_val = buf;
1N/A bv.bv_len = strlen( buf );
1N/A bvp[0] = &bv;
1N/A bvp[1] = NULL;
1N/A mods[i]->mod_bvalues = bvp;
1N/A mods[++i] = NULL;
1N/A
1N/A /* msgid of -1 means don't send the result */
1N/A rc = (ld->ld_cache_add)( ld, -1, m->lm_msgtype, dn, mods );
1N/A LDAPDebug( LDAP_DEBUG_TRACE,
1N/A "<= nsldapi_add_result_to_cache added (rc %d)\n", rc, 0, 0 );
1N/A}