]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/tools.c
backglue, sasl, and tool fixes
[openldap] / servers / slapd / back-ldbm / tools.c
1 /* tools.c - tools for slap tools */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11
12 #include <ac/string.h>
13 #include <ac/socket.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17
18 static LDBMCursor *cursorp = NULL;
19 static DBCache *id2entry = NULL;
20
21 int ldbm_tool_entry_open(
22         BackendDB *be, int mode )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25         int flags;
26
27         assert( slapMode & SLAP_TOOL_MODE );
28         assert( id2entry == NULL );
29
30         switch( mode ) {
31         case 1:
32                 flags = LDBM_WRCREAT;
33                 break;
34         case 2:
35 #ifdef TRUNCATE_MODE
36                 flags = LDBM_NEWDB;
37 #else
38                 flags = LDBM_WRCREAT;
39 #endif
40                 break;
41         default:
42                 flags = LDBM_READER;
43         }
44
45         li->li_dbwritesync = 0;
46
47         if ( (id2entry = ldbm_cache_open( be, "id2entry", LDBM_SUFFIX, flags ))
48             == NULL ) {
49 #ifdef NEW_LOGGING
50                 LDAP_LOG( BACK_LDBM, CRIT,
51                            "Could not open/create id2entry%s\n", LDBM_SUFFIX, 0, 0 );
52 #else
53                 Debug( LDAP_DEBUG_ANY, "Could not open/create id2entry" LDBM_SUFFIX "\n",
54                     0, 0, 0 );
55 #endif
56
57                 return( -1 );
58         }
59
60         return 0;
61 }
62
63 int ldbm_tool_entry_close(
64         BackendDB *be )
65 {
66         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
67
68         assert( slapMode & SLAP_TOOL_MODE );
69         assert( id2entry != NULL );
70
71         ldbm_cache_close( be, id2entry );
72         li->li_dbwritesync = 1;
73         id2entry = NULL;
74
75         return 0;
76 }
77
78 ID ldbm_tool_entry_first(
79         BackendDB *be )
80 {
81         Datum key;
82         ID id;
83
84         assert( slapMode & SLAP_TOOL_MODE );
85         assert( id2entry != NULL );
86
87         key = ldbm_firstkey( id2entry->dbc_db, &cursorp );
88
89         if( key.dptr == NULL ) {
90                 return NOID;
91         }
92
93         AC_MEMCPY( &id, key.dptr, key.dsize );
94 #ifndef WORDS_BIGENDIAN
95         id = ntohl( id );
96 #endif
97
98         ldbm_datum_free( id2entry->dbc_db, key );
99
100         return id;
101 }
102
103 ID ldbm_tool_entry_next(
104         BackendDB *be )
105 {
106         Datum key;
107         ID id;
108
109         assert( slapMode & SLAP_TOOL_MODE );
110         assert( id2entry != NULL );
111
112         /* allow for NEXTID */
113         ldbm_datum_init( key );
114
115         key = ldbm_nextkey( id2entry->dbc_db, key, cursorp );
116
117         if( key.dptr == NULL ) {
118                 return NOID;
119         }
120
121         AC_MEMCPY( &id, key.dptr, key.dsize );
122 #ifndef WORDS_BIGENDIAN
123         id = ntohl( id );
124 #endif
125
126         ldbm_datum_free( id2entry->dbc_db, key );
127
128         return id;
129 }
130
131 Entry* ldbm_tool_entry_get( BackendDB *be, ID id )
132 {
133         Entry *e;
134         Datum key, data;
135 #ifndef WORDS_BIGENDIAN
136         ID id2;
137 #endif
138         assert( slapMode & SLAP_TOOL_MODE );
139         assert( id2entry != NULL );
140
141         ldbm_datum_init( key );
142
143 #ifndef WORDS_BIGENDIAN
144         id2 = htonl( id );
145         key.dptr = (char *) &id2;
146 #else
147         key.dptr = (char *) &id;
148 #endif
149         key.dsize = sizeof(ID);
150
151         data = ldbm_cache_fetch( id2entry, key );
152
153         if ( data.dptr == NULL ) {
154                 return NULL;
155         }
156
157         e = str2entry( data.dptr );
158         ldbm_datum_free( id2entry->dbc_db, data );
159
160         if( e != NULL ) {
161                 e->e_id = id;
162         }
163
164         return e;
165 }
166
167 ID ldbm_tool_entry_put(
168         BackendDB *be,
169         Entry *e,
170         struct berval *text )
171 {
172         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
173         Datum key, data;
174         int rc, len;
175         ID id;
176
177         assert( slapMode & SLAP_TOOL_MODE );
178         assert( id2entry != NULL );
179
180         assert( text );
181         assert( text->bv_val );
182         assert( text->bv_val[0] == '\0' );      /* overconservative? */
183
184         if ( next_id_get( be, &id ) || id == NOID ) {
185                 strncpy( text->bv_val, "unable to get nextid", text->bv_len );
186                 return NOID;
187         }
188
189         e->e_id = li->li_nextid++;
190
191 #ifdef NEW_LOGGING
192         LDAP_LOG( BACK_LDBM, ENTRY,
193                 "ldbm_tool_entry_put: (%s)%ld\n", e->e_dn, e->e_id ,0 );
194 #else
195         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_put( %ld, \"%s\" )\n",
196                 e->e_id, e->e_dn, 0 );
197 #endif
198
199         if ( dn2id( be, &e->e_nname, &id ) ) {
200                 /* something bad happened to ldbm cache */
201                 strncpy( text->bv_val, "ldbm cache corrupted", text->bv_len );
202                 return NOID;
203         }
204
205         if( id != NOID ) {
206 #ifdef NEW_LOGGING
207                 LDAP_LOG( BACK_LDBM, ENTRY,
208                         "ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
209                         e->e_dn, id, 0 );
210 #else
211                 Debug( LDAP_DEBUG_TRACE,
212                         "<= ldbm_tool_entry_put: \"%s\" already exists (id=%ld)\n",
213                         e->e_ndn, id, 0 );
214 #endif
215                 strncpy( text->bv_val, "already exists", text->bv_len );
216                 return NOID;
217         }
218
219         rc = index_entry_add( be, e, e->e_attrs );
220         if( rc != 0 ) {
221                 strncpy( text->bv_val, "index add failed", text->bv_len );
222                 return NOID;
223         }
224
225         rc = dn2id_add( be, &e->e_nname, e->e_id );
226         if( rc != 0 ) {
227                 strncpy( text->bv_val, "dn2id add failed", text->bv_len );
228                 return NOID;
229         }
230
231         ldbm_datum_init( key );
232         ldbm_datum_init( data );
233
234 #ifndef WORDS_BIGENDIAN
235         id = htonl( e->e_id );
236         key.dptr = (char *) &id;
237 #else
238         key.dptr = (char *) &e->e_id;
239 #endif
240         key.dsize = sizeof(ID);
241
242         data.dptr = entry2str( e, &len );
243         data.dsize = len + 1;
244
245         /* store it */
246         rc = ldbm_cache_store( id2entry, key, data, LDBM_REPLACE );
247
248         if( rc != 0 ) {
249                 (void) dn2id_delete( be, &e->e_nname, e->e_id );
250                 strncpy( text->bv_val, "cache store failed", text->bv_len );
251                 return NOID;
252         }
253
254         return e->e_id;
255 }
256
257 int ldbm_tool_entry_reindex(
258         BackendDB *be,
259         ID id )
260 {
261         int rc;
262         Entry *e;
263
264 #ifdef NEW_LOGGING
265         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_tool_entry_reindex: ID=%ld\n", 
266                 (long)id, 0, 0 );
267 #else
268         Debug( LDAP_DEBUG_ARGS, "=> ldbm_tool_entry_reindex( %ld )\n",
269                 (long) id, 0, 0 );
270 #endif
271
272
273         e = ldbm_tool_entry_get( be, id );
274
275         if( e == NULL ) {
276 #ifdef NEW_LOGGING
277                 LDAP_LOG( BACK_LDBM, INFO,
278                    "ldbm_tool_entry_reindex: could not locate id %ld\n", 
279                    (long)id, 0, 0  );
280 #else
281                 Debug( LDAP_DEBUG_ANY,
282                         "ldbm_tool_entry_reindex:: could not locate id=%ld\n",
283                         (long) id, 0, 0 );
284 #endif
285
286                 return -1;
287         }
288
289         /*
290          * just (re)add them for now
291          * assume that some other routine (not yet implemented)
292          * will zap index databases
293          *
294          */
295
296 #ifdef NEW_LOGGING
297         LDAP_LOG( BACK_LDBM, ENTRY,
298                    "ldbm_tool_entry_reindex: (%s) %ld\n", e->e_dn, id, 0 );
299 #else
300         Debug( LDAP_DEBUG_TRACE, "=> ldbm_tool_entry_reindex( %ld, \"%s\" )\n",
301                 id, e->e_dn, 0 );
302 #endif
303
304         dn2id_add( be, &e->e_nname, e->e_id );
305         rc = index_entry_add( be, e, e->e_attrs );
306
307         entry_free( e );
308
309         return rc;
310 }
311
312 int ldbm_tool_sync( BackendDB *be )
313 {
314         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
315
316         assert( slapMode & SLAP_TOOL_MODE );
317
318         if ( li->li_nextid != NOID ) {
319                 if ( next_id_write( be, li->li_nextid ) ) {
320                         return( -1 );
321                 }
322         }
323
324         return 0;
325 }