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