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