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