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