1 #pragma ident "%Z%%M% %I% %E% SMI"
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/
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.
14 * The Original Code is Mozilla Communicator client code, released
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
25 * Copyright (c) 1993 The Regents of the University of Michigan.
26 * All rights reserved.
29 * cache.c - generic caching support for LDAP
35 * ldap_cache_flush - flush part of the LDAP cache. returns an
36 * ldap error code (LDAP_SUCCESS, LDAP_NO_SUCH_OBJECT, etc.).
41 ldap_cache_flush( LDAP
*ld
, const char *dn
, const char *filter
)
43 if ( !NSLDAPI_VALID_LDAP_POINTER( ld
)) {
44 return( LDAP_PARAM_ERROR
);
51 return( (ld
->ld_cache_flush
)( ld
, dn
, filter
) );
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.
62 nsldapi_add_result_to_cache( LDAP
*ld
, LDAPMessage
*m
)
71 struct berval
*bvp
[2];
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 );
84 dn
= ldap_get_dn( ld
, m
);
85 mods
= (LDAPMod
**)NSLDAPI_MALLOC( GRABSIZE
* sizeof(LDAPMod
*) );
87 LDAPDebug( LDAP_DEBUG_TRACE
,
88 "<= nsldapi_add_result_to_cache malloc failed\n", 0, 0, 0 );
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) ) {
96 mods
= (LDAPMod
**)NSLDAPI_REALLOC( mods
,
97 sizeof(LDAPMod
*) * max
);
99 LDAPDebug( LDAP_DEBUG_TRACE
,
100 "<= nsldapi_add_result_to_cache realloc failed\n",
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",
111 ldap_mods_free( mods
, 1 );
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
);
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 );
129 /* update special cachedtime attribute */
130 if ( i
== (max
- 1) ) {
132 mods
= (LDAPMod
**)NSLDAPI_REALLOC( mods
,
133 sizeof(LDAPMod
*) * max
);
135 LDAPDebug( LDAP_DEBUG_TRACE
,
136 "<= nsldapi_add_result_to_cache calloc failed\n",
138 ldap_mods_free( mods
, 1 );
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",
147 ldap_mods_free( mods
, 1 );
150 mods
[i
]->mod_op
= LDAP_MOD_BVALUES
;
151 mods
[i
]->mod_type
= "cachedtime";
152 sprintf( buf
, "%d", time( NULL
) );
154 bv
.bv_len
= strlen( buf
);
157 mods
[i
]->mod_bvalues
= bvp
;
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 );