]> git.sur5r.net Git - openldap/blob - servers/slapd/back-null/null.c
Update copyright for next release
[openldap] / servers / slapd / back-null / null.c
1 /* null.c - the null backend */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2002-2009 The OpenLDAP Foundation.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was originally developed by Hallvard Furuseth for inclusion
18  * in OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <ac/string.h>
25
26 #include "slap.h"
27 #include "config.h"
28
29 struct null_info {
30         int     ni_bind_allowed;
31         ID      ni_nextid;
32 };
33
34
35 /* LDAP operations */
36
37 static int
38 null_back_bind( Operation *op, SlapReply *rs )
39 {
40         struct null_info *ni = (struct null_info *) op->o_bd->be_private;
41
42         if ( ni->ni_bind_allowed || be_isroot_pw( op ) ) {
43                 /* front end will send result on success (0) */
44                 return LDAP_SUCCESS;
45         }
46
47         rs->sr_err = LDAP_INVALID_CREDENTIALS;
48         send_ldap_result( op, rs );
49
50         return rs->sr_err;
51 }
52
53
54 static int
55 null_back_respond( Operation *op, SlapReply *rs, int rc )
56 {
57         LDAPControl ctrl[SLAP_MAX_RESPONSE_CONTROLS], *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
58         int c = 0;
59
60         BerElementBuffer        ps_berbuf;
61         BerElement              *ps_ber = NULL;
62         LDAPControl             **preread_ctrl = NULL,
63                                 **postread_ctrl = NULL;
64
65         rs->sr_err = LDAP_OTHER;
66
67         /* this comes first, as in case of assertion failure
68          * any further processing must stop */
69         if ( get_assert( op ) ) {
70                 rs->sr_err = LDAP_ASSERTION_FAILED;
71                 goto respond;
72         }
73
74         if ( op->o_preread ) {
75                 Entry           e = { 0 };
76
77                 switch ( op->o_tag ) {
78                 case LDAP_REQ_MODIFY:
79                 case LDAP_REQ_RENAME:
80                 case LDAP_REQ_DELETE:
81                         e.e_name = op->o_req_dn;
82                         e.e_nname = op->o_req_ndn;
83
84                         preread_ctrl = &ctrls[c];
85                         *preread_ctrl = NULL;
86
87                         if ( slap_read_controls( op, rs, &e,
88                                 &slap_pre_read_bv, preread_ctrl ) )
89                         {
90                                 preread_ctrl = NULL;
91
92                                 Debug( LDAP_DEBUG_TRACE,
93                                         "<=- null_back_respond: pre-read "
94                                         "failed!\n", 0, 0, 0 );
95
96                                 if ( op->o_preread & SLAP_CONTROL_CRITICAL ) {
97                                         /* FIXME: is it correct to abort
98                                          * operation if control fails? */
99                                         goto respond;
100                                 }
101
102                         } else {
103                                 c++;
104                         }
105                         break;
106                 }
107         }
108
109         if ( op->o_postread ) {
110                 Entry           e = { 0 };
111
112                 switch ( op->o_tag ) {
113                 case LDAP_REQ_ADD:
114                 case LDAP_REQ_MODIFY:
115                 case LDAP_REQ_RENAME:
116                         if ( op->o_tag == LDAP_REQ_ADD ) {
117                                 e.e_name = op->ora_e->e_name;
118                                 e.e_nname = op->ora_e->e_nname;
119
120                         } else {
121                                 e.e_name = op->o_req_dn;
122                                 e.e_nname = op->o_req_ndn;
123                         }
124
125                         postread_ctrl = &ctrls[c];
126                         *postread_ctrl = NULL;
127
128                         if ( slap_read_controls( op, rs, &e,
129                                 &slap_post_read_bv, postread_ctrl ) )
130                         {
131                                 postread_ctrl = NULL;
132
133                                 Debug( LDAP_DEBUG_TRACE,
134                                         "<=- null_back_respond: post-read "
135                                         "failed!\n", 0, 0, 0 );
136
137                                 if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
138                                         /* FIXME: is it correct to abort
139                                          * operation if control fails? */
140                                         goto respond;
141                                 }
142
143                         } else {
144                                 c++;
145                         }
146                         break;
147                 }
148         }
149
150         if ( op->o_noop ) {
151                 switch ( op->o_tag ) {
152                 case LDAP_REQ_ADD:
153                 case LDAP_REQ_MODIFY:
154                 case LDAP_REQ_RENAME:
155                 case LDAP_REQ_DELETE:
156                 case LDAP_REQ_EXTENDED:
157                         rc = LDAP_X_NO_OPERATION;
158                         break;
159                 }
160         }
161
162         if ( get_pagedresults( op ) > SLAP_CONTROL_IGNORED ) {
163                 struct berval           cookie = BER_BVC( "" );
164
165                 /* should not be here... */
166                 assert( op->o_tag == LDAP_REQ_SEARCH );
167
168                 ctrl[c].ldctl_oid = LDAP_CONTROL_PAGEDRESULTS;
169                 ctrl[c].ldctl_iscritical = 0;
170
171                 ps_ber = (BerElement *)&ps_berbuf;
172                 ber_init2( ps_ber, NULL, LBER_USE_DER );
173
174                 /* return size of 0 -- no estimate */
175                 ber_printf( ps_ber, "{iO}", 0, &cookie ); 
176
177                 if ( ber_flatten2( ps_ber, &ctrl[c].ldctl_value, 0 ) == -1 ) {
178                         goto done;
179                 }
180                 
181                 ctrls[c] = &ctrl[c];
182                 c++;
183         }
184
185         /* terminate controls array */
186         ctrls[c] = NULL;
187         rs->sr_ctrls = ctrls;
188         rs->sr_err = rc;
189
190 respond:;
191         send_ldap_result( op, rs );
192         rs->sr_ctrls = NULL;
193
194 done:;
195         if ( ps_ber != NULL ) {
196                 (void) ber_free_buf( ps_ber );
197         }
198
199         if( preread_ctrl != NULL && (*preread_ctrl) != NULL ) {
200                 slap_sl_free( (*preread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
201                 slap_sl_free( *preread_ctrl, op->o_tmpmemctx );
202         }
203
204         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
205                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
206                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
207         }
208
209         return rs->sr_err;
210 }
211
212 /* add, delete, modify, modrdn, search */
213 static int
214 null_back_success( Operation *op, SlapReply *rs )
215 {
216         return null_back_respond( op, rs, LDAP_SUCCESS );
217 }
218
219 /* compare */
220 static int
221 null_back_false( Operation *op, SlapReply *rs )
222 {
223         return null_back_respond( op, rs, LDAP_COMPARE_FALSE );
224 }
225
226
227 /* for overlays */
228 static int
229 null_back_entry_get(
230         Operation *op,
231         struct berval *ndn,
232         ObjectClass *oc,
233         AttributeDescription *at,
234         int rw,
235         Entry **ent )
236 {
237         assert( *ent == NULL );
238
239         /* don't admit the object isn't there */
240         return oc || at ? LDAP_NO_SUCH_ATTRIBUTE : LDAP_BUSY;
241 }
242
243
244 /* Slap tools */
245
246 static int
247 null_tool_entry_open( BackendDB *be, int mode )
248 {
249         return 0;
250 }
251
252 static int
253 null_tool_entry_close( BackendDB *be )
254 {
255         assert( be != NULL );
256         return 0;
257 }
258
259 static ID
260 null_tool_entry_next( BackendDB *be )
261 {
262         return NOID;
263 }
264
265 static Entry *
266 null_tool_entry_get( BackendDB *be, ID id )
267 {
268         assert( slapMode & SLAP_TOOL_MODE );
269         return NULL;
270 }
271
272 static ID
273 null_tool_entry_put( BackendDB *be, Entry *e, struct berval *text )
274 {
275         assert( slapMode & SLAP_TOOL_MODE );
276         assert( text != NULL );
277         assert( text->bv_val != NULL );
278         assert( text->bv_val[0] == '\0' );      /* overconservative? */
279
280         e->e_id = ((struct null_info *) be->be_private)->ni_nextid++;
281         return e->e_id;
282 }
283
284
285 /* Setup */
286
287 static int
288 null_back_db_config(
289         BackendDB       *be,
290         const char      *fname,
291         int             lineno,
292         int             argc,
293         char            **argv )
294 {
295         struct null_info *ni = (struct null_info *) be->be_private;
296
297         if ( ni == NULL ) {
298                 fprintf( stderr, "%s: line %d: null database info is null!\n",
299                         fname, lineno );
300                 return 1;
301         }
302
303         /* bind requests allowed */
304         if ( strcasecmp( argv[0], "bind" ) == 0 ) {
305                 if ( argc < 2 ) {
306                         fprintf( stderr,
307         "%s: line %d: missing <on/off> in \"bind <on/off>\" line\n",
308                                  fname, lineno );
309                         return 1;
310                 }
311                 ni->ni_bind_allowed = strcasecmp( argv[1], "off" );
312
313         /* anything else */
314         } else {
315                 return SLAP_CONF_UNKNOWN;
316         }
317
318         return 0;
319 }
320
321 static int
322 null_back_db_init( BackendDB *be, ConfigReply *cr )
323 {
324         struct null_info *ni = ch_calloc( 1, sizeof(struct null_info) );
325         ni->ni_bind_allowed = 0;
326         ni->ni_nextid = 1;
327         be->be_private = ni;
328         return 0;
329 }
330
331 static int
332 null_back_db_destroy( Backend *be, ConfigReply *cr )
333 {
334         free( be->be_private );
335         return 0;
336 }
337
338
339 int
340 null_back_initialize( BackendInfo *bi )
341 {
342         static char *controls[] = {
343                 LDAP_CONTROL_ASSERT,
344                 LDAP_CONTROL_MANAGEDSAIT,
345                 LDAP_CONTROL_NOOP,
346                 LDAP_CONTROL_PAGEDRESULTS,
347                 LDAP_CONTROL_SUBENTRIES,
348                 LDAP_CONTROL_PRE_READ,
349                 LDAP_CONTROL_POST_READ,
350                 LDAP_CONTROL_X_PERMISSIVE_MODIFY,
351                 NULL
352         };
353
354         Debug( LDAP_DEBUG_TRACE,
355                 "null_back_initialize: initialize null backend\n", 0, 0, 0 );
356
357         bi->bi_flags |=
358                 SLAP_BFLAG_INCREMENT |
359                 SLAP_BFLAG_SUBENTRIES |
360                 SLAP_BFLAG_ALIASES |
361                 SLAP_BFLAG_REFERRALS;
362
363         bi->bi_controls = controls;
364
365         bi->bi_open = 0;
366         bi->bi_close = 0;
367         bi->bi_config = 0;
368         bi->bi_destroy = 0;
369
370         bi->bi_db_init = null_back_db_init;
371         bi->bi_db_config = null_back_db_config;
372         bi->bi_db_open = 0;
373         bi->bi_db_close = 0;
374         bi->bi_db_destroy = null_back_db_destroy;
375
376         bi->bi_op_bind = null_back_bind;
377         bi->bi_op_unbind = 0;
378         bi->bi_op_search = null_back_success;
379         bi->bi_op_compare = null_back_false;
380         bi->bi_op_modify = null_back_success;
381         bi->bi_op_modrdn = null_back_success;
382         bi->bi_op_add = null_back_success;
383         bi->bi_op_delete = null_back_success;
384         bi->bi_op_abandon = 0;
385
386         bi->bi_extended = 0;
387
388         bi->bi_chk_referrals = 0;
389
390         bi->bi_connection_init = 0;
391         bi->bi_connection_destroy = 0;
392
393         bi->bi_entry_get_rw = null_back_entry_get;
394
395         bi->bi_tool_entry_open = null_tool_entry_open;
396         bi->bi_tool_entry_close = null_tool_entry_close;
397         bi->bi_tool_entry_first = null_tool_entry_next;
398         bi->bi_tool_entry_next = null_tool_entry_next;
399         bi->bi_tool_entry_get = null_tool_entry_get;
400         bi->bi_tool_entry_put = null_tool_entry_put;
401
402         return 0;
403 }
404
405 #if SLAPD_NULL == SLAPD_MOD_DYNAMIC
406
407 /* conditionally define the init_module() function */
408 SLAP_BACKEND_INIT_MODULE( null )
409
410 #endif /* SLAPD_NULL == SLAPD_MOD_DYNAMIC */