1*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
2*7c478bd9Sstevel@tonic-gate
3*7c478bd9Sstevel@tonic-gate /*
4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the Netscape Public
5*7c478bd9Sstevel@tonic-gate * License Version 1.1 (the "License"); you may not use this file
6*7c478bd9Sstevel@tonic-gate * except in compliance with the License. You may obtain a copy of
7*7c478bd9Sstevel@tonic-gate * the License at http://www.mozilla.org/NPL/
8*7c478bd9Sstevel@tonic-gate *
9*7c478bd9Sstevel@tonic-gate * Software distributed under the License is distributed on an "AS
10*7c478bd9Sstevel@tonic-gate * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11*7c478bd9Sstevel@tonic-gate * implied. See the License for the specific language governing
12*7c478bd9Sstevel@tonic-gate * rights and limitations under the License.
13*7c478bd9Sstevel@tonic-gate *
14*7c478bd9Sstevel@tonic-gate * The Original Code is Mozilla Communicator client code, released
15*7c478bd9Sstevel@tonic-gate * March 31, 1998.
16*7c478bd9Sstevel@tonic-gate *
17*7c478bd9Sstevel@tonic-gate * The Initial Developer of the Original Code is Netscape
18*7c478bd9Sstevel@tonic-gate * Communications Corporation. Portions created by Netscape are
19*7c478bd9Sstevel@tonic-gate * Copyright (C) 1998-1999 Netscape Communications Corporation. All
20*7c478bd9Sstevel@tonic-gate * Rights Reserved.
21*7c478bd9Sstevel@tonic-gate *
22*7c478bd9Sstevel@tonic-gate * Contributor(s):
23*7c478bd9Sstevel@tonic-gate */
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate * Copyright (c) 1993 The Regents of the University of Michigan.
26*7c478bd9Sstevel@tonic-gate * All rights reserved.
27*7c478bd9Sstevel@tonic-gate */
28*7c478bd9Sstevel@tonic-gate /*
29*7c478bd9Sstevel@tonic-gate * cache.c - generic caching support for LDAP
30*7c478bd9Sstevel@tonic-gate */
31*7c478bd9Sstevel@tonic-gate
32*7c478bd9Sstevel@tonic-gate #include "ldap-int.h"
33*7c478bd9Sstevel@tonic-gate
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate * ldap_cache_flush - flush part of the LDAP cache. returns an
36*7c478bd9Sstevel@tonic-gate * ldap error code (LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT, etc.).
37*7c478bd9Sstevel@tonic-gate */
38*7c478bd9Sstevel@tonic-gate
39*7c478bd9Sstevel@tonic-gate int
40*7c478bd9Sstevel@tonic-gate LDAP_CALL
ldap_cache_flush(LDAP * ld,const char * dn,const char * filter)41*7c478bd9Sstevel@tonic-gate ldap_cache_flush( LDAP *ld, const char *dn, const char *filter )
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate if ( !NSLDAPI_VALID_LDAP_POINTER( ld )) {
44*7c478bd9Sstevel@tonic-gate return( LDAP_PARAM_ERROR );
45*7c478bd9Sstevel@tonic-gate }
46*7c478bd9Sstevel@tonic-gate
47*7c478bd9Sstevel@tonic-gate if ( dn == NULL ) {
48*7c478bd9Sstevel@tonic-gate dn = "";
49*7c478bd9Sstevel@tonic-gate }
50*7c478bd9Sstevel@tonic-gate
51*7c478bd9Sstevel@tonic-gate return( (ld->ld_cache_flush)( ld, dn, filter ) );
52*7c478bd9Sstevel@tonic-gate }
53*7c478bd9Sstevel@tonic-gate
54*7c478bd9Sstevel@tonic-gate /*
55*7c478bd9Sstevel@tonic-gate * nsldapi_add_result_to_cache - add an ldap entry we just read off the network
56*7c478bd9Sstevel@tonic-gate * to the ldap cache. this routine parses the ber for the entry and
57*7c478bd9Sstevel@tonic-gate * constructs the appropriate add request. this routine calls the
58*7c478bd9Sstevel@tonic-gate * cache add routine to actually add the entry.
59*7c478bd9Sstevel@tonic-gate */
60*7c478bd9Sstevel@tonic-gate
61*7c478bd9Sstevel@tonic-gate void
nsldapi_add_result_to_cache(LDAP * ld,LDAPMessage * m)62*7c478bd9Sstevel@tonic-gate nsldapi_add_result_to_cache( LDAP *ld, LDAPMessage *m )
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate char *dn;
65*7c478bd9Sstevel@tonic-gate LDAPMod **mods;
66*7c478bd9Sstevel@tonic-gate int i, max, rc;
67*7c478bd9Sstevel@tonic-gate char *a;
68*7c478bd9Sstevel@tonic-gate BerElement *ber;
69*7c478bd9Sstevel@tonic-gate char buf[50];
70*7c478bd9Sstevel@tonic-gate struct berval bv;
71*7c478bd9Sstevel@tonic-gate struct berval *bvp[2];
72*7c478bd9Sstevel@tonic-gate
73*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE, "=> nsldapi_add_result_to_cache id %d type %d\n",
74*7c478bd9Sstevel@tonic-gate m->lm_msgid, m->lm_msgtype, 0 );
75*7c478bd9Sstevel@tonic-gate if ( m->lm_msgtype != LDAP_RES_SEARCH_ENTRY ||
76*7c478bd9Sstevel@tonic-gate ld->ld_cache_add == NULL ) {
77*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
78*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache not added\n", 0, 0, 0 );
79*7c478bd9Sstevel@tonic-gate return;
80*7c478bd9Sstevel@tonic-gate }
81*7c478bd9Sstevel@tonic-gate
82*7c478bd9Sstevel@tonic-gate #define GRABSIZE 5
83*7c478bd9Sstevel@tonic-gate
84*7c478bd9Sstevel@tonic-gate dn = ldap_get_dn( ld, m );
85*7c478bd9Sstevel@tonic-gate mods = (LDAPMod **)NSLDAPI_MALLOC( GRABSIZE * sizeof(LDAPMod *) );
86*7c478bd9Sstevel@tonic-gate if (mods == NULL) {
87*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
88*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache malloc failed\n", 0, 0, 0 );
89*7c478bd9Sstevel@tonic-gate return;
90*7c478bd9Sstevel@tonic-gate }
91*7c478bd9Sstevel@tonic-gate max = GRABSIZE;
92*7c478bd9Sstevel@tonic-gate for ( i = 0, a = ldap_first_attribute( ld, m, &ber ); a != NULL;
93*7c478bd9Sstevel@tonic-gate a = ldap_next_attribute( ld, m, ber ), i++ ) {
94*7c478bd9Sstevel@tonic-gate if ( i == (max - 1) ) {
95*7c478bd9Sstevel@tonic-gate max += GRABSIZE;
96*7c478bd9Sstevel@tonic-gate mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
97*7c478bd9Sstevel@tonic-gate sizeof(LDAPMod *) * max );
98*7c478bd9Sstevel@tonic-gate if (mods == NULL) {
99*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
100*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache realloc failed\n",
101*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
102*7c478bd9Sstevel@tonic-gate return;
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate }
105*7c478bd9Sstevel@tonic-gate
106*7c478bd9Sstevel@tonic-gate mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
107*7c478bd9Sstevel@tonic-gate if (mods[i] == NULL) {
108*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
109*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache calloc failed\n",
110*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
111*7c478bd9Sstevel@tonic-gate ldap_mods_free( mods, 1 );
112*7c478bd9Sstevel@tonic-gate return;
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate mods[i]->mod_op = LDAP_MOD_BVALUES;
115*7c478bd9Sstevel@tonic-gate mods[i]->mod_type = a;
116*7c478bd9Sstevel@tonic-gate mods[i]->mod_bvalues = ldap_get_values_len( ld, m, a );
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate if ( ber != NULL ) {
119*7c478bd9Sstevel@tonic-gate ber_free( ber, 0 );
120*7c478bd9Sstevel@tonic-gate }
121*7c478bd9Sstevel@tonic-gate if (( rc = LDAP_GET_LDERRNO( ld, NULL, NULL )) != LDAP_SUCCESS ) {
122*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
123*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache error: failed to construct mod list (%s)\n",
124*7c478bd9Sstevel@tonic-gate ldap_err2string( rc ), 0, 0 );
125*7c478bd9Sstevel@tonic-gate ldap_mods_free( mods, 1 );
126*7c478bd9Sstevel@tonic-gate return;
127*7c478bd9Sstevel@tonic-gate }
128*7c478bd9Sstevel@tonic-gate
129*7c478bd9Sstevel@tonic-gate /* update special cachedtime attribute */
130*7c478bd9Sstevel@tonic-gate if ( i == (max - 1) ) {
131*7c478bd9Sstevel@tonic-gate max++;
132*7c478bd9Sstevel@tonic-gate mods = (LDAPMod **)NSLDAPI_REALLOC( mods,
133*7c478bd9Sstevel@tonic-gate sizeof(LDAPMod *) * max );
134*7c478bd9Sstevel@tonic-gate if (mods == NULL) {
135*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
136*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache calloc failed\n",
137*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
138*7c478bd9Sstevel@tonic-gate ldap_mods_free( mods, 1 );
139*7c478bd9Sstevel@tonic-gate return;
140*7c478bd9Sstevel@tonic-gate }
141*7c478bd9Sstevel@tonic-gate }
142*7c478bd9Sstevel@tonic-gate mods[i] = (LDAPMod *)NSLDAPI_CALLOC( 1, sizeof(LDAPMod) );
143*7c478bd9Sstevel@tonic-gate if (mods[i] == NULL) {
144*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
145*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache calloc failed\n",
146*7c478bd9Sstevel@tonic-gate 0, 0, 0 );
147*7c478bd9Sstevel@tonic-gate ldap_mods_free( mods, 1 );
148*7c478bd9Sstevel@tonic-gate return;
149*7c478bd9Sstevel@tonic-gate }
150*7c478bd9Sstevel@tonic-gate mods[i]->mod_op = LDAP_MOD_BVALUES;
151*7c478bd9Sstevel@tonic-gate mods[i]->mod_type = "cachedtime";
152*7c478bd9Sstevel@tonic-gate sprintf( buf, "%d", time( NULL ) );
153*7c478bd9Sstevel@tonic-gate bv.bv_val = buf;
154*7c478bd9Sstevel@tonic-gate bv.bv_len = strlen( buf );
155*7c478bd9Sstevel@tonic-gate bvp[0] = &bv;
156*7c478bd9Sstevel@tonic-gate bvp[1] = NULL;
157*7c478bd9Sstevel@tonic-gate mods[i]->mod_bvalues = bvp;
158*7c478bd9Sstevel@tonic-gate mods[++i] = NULL;
159*7c478bd9Sstevel@tonic-gate
160*7c478bd9Sstevel@tonic-gate /* msgid of -1 means don't send the result */
161*7c478bd9Sstevel@tonic-gate rc = (ld->ld_cache_add)( ld, -1, m->lm_msgtype, dn, mods );
162*7c478bd9Sstevel@tonic-gate LDAPDebug( LDAP_DEBUG_TRACE,
163*7c478bd9Sstevel@tonic-gate "<= nsldapi_add_result_to_cache added (rc %d)\n", rc, 0, 0 );
164*7c478bd9Sstevel@tonic-gate }
165