]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Make sur attribute ACL are evaluated on ADD operations (see ITS#4556)
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2008 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
17 #include "portable.h"
18
19 #include <stdio.h>
20 #include <ac/string.h>
21
22 #include "back-bdb.h"
23
24 int
25 bdb_add(Operation *op, SlapReply *rs )
26 {
27         struct bdb_info *bdb = (struct bdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL, *oe = op->ora_e;
30         EntryInfo       *ei;
31         char textbuf[SLAP_TEXT_BUFLEN];
32         size_t textlen = sizeof textbuf;
33         AttributeDescription *children = slap_schema.si_ad_children;
34         AttributeDescription *entry = slap_schema.si_ad_entry;
35         DB_TXN          *ltid = NULL, *lt2, *rtxn;
36         ID eid = NOID;
37         struct bdb_op_info opinfo = {0};
38         int subentry;
39         DB_LOCK         lock;
40
41         int             num_retries = 0;
42         int             success;
43
44         LDAPControl **postread_ctrl = NULL;
45         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
46         int num_ctrls = 0;
47
48 #ifdef LDAP_X_TXN
49         int settle = 0;
50 #endif
51
52         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(bdb_add) ": %s\n",
53                 op->oq_add.rs_e->e_name.bv_val, 0, 0);
54
55 #ifdef LDAP_X_TXN
56         if( op->o_txnSpec ) {
57                 /* acquire connection lock */
58                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
59                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
60                         rs->sr_text = "invalid transaction identifier";
61                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
62                         goto txnReturn;
63                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
64                         settle=1;
65                         goto txnReturn;
66                 }
67
68                 if( op->o_conn->c_txn_backend == NULL ) {
69                         op->o_conn->c_txn_backend = op->o_bd;
70
71                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
72                         rs->sr_text = "transaction cannot span multiple database contexts";
73                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
74                         goto txnReturn;
75                 }
76
77                 /* insert operation into transaction */
78
79                 rs->sr_text = "transaction specified";
80                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
81
82 txnReturn:
83                 /* release connection lock */
84                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
85
86                 if( !settle ) {
87                         send_ldap_result( op, rs );
88                         return rs->sr_err;
89                 }
90         }
91 #endif
92
93         ctrls[num_ctrls] = 0;
94
95         /* check entry's schema */
96         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
97                 get_relax(op), 1, &rs->sr_text, textbuf, textlen );
98         if ( rs->sr_err != LDAP_SUCCESS ) {
99                 Debug( LDAP_DEBUG_TRACE,
100                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
101                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
102                 goto return_results;
103         }
104
105         /* add opattrs to shadow as well, only missing attrs will actually
106          * be added; helps compatibility with older OL versions */
107         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
108         if ( rs->sr_err != LDAP_SUCCESS ) {
109                 Debug( LDAP_DEBUG_TRACE,
110                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
111                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
112                 goto return_results;
113         }
114
115         subentry = is_entry_subentry( op->oq_add.rs_e );
116
117         /* Get our reader TXN */
118         rs->sr_err = bdb_reader_get( op, bdb->bi_dbenv, &rtxn );
119
120         if( 0 ) {
121 retry:  /* transaction retry */
122                 if( p ) {
123                         /* free parent and reader lock */
124                         if ( p != (Entry *)&slap_entry_root ) {
125                                 bdb_unlocked_cache_return_entry_r( bdb, p );
126                         }
127                         p = NULL;
128                 }
129                 rs->sr_err = TXN_ABORT( ltid );
130                 ltid = NULL;
131                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
132                 opinfo.boi_oe.oe_key = NULL;
133                 op->o_do_not_cache = opinfo.boi_acl_cache;
134                 if( rs->sr_err != 0 ) {
135                         rs->sr_err = LDAP_OTHER;
136                         rs->sr_text = "internal error";
137                         goto return_results;
138                 }
139                 if ( op->o_abandon ) {
140                         rs->sr_err = SLAPD_ABANDON;
141                         goto return_results;
142                 }
143                 bdb_trans_backoff( ++num_retries );
144         }
145
146         /* begin transaction */
147         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
148                 bdb->bi_db_opflags );
149         rs->sr_text = NULL;
150         if( rs->sr_err != 0 ) {
151                 Debug( LDAP_DEBUG_TRACE,
152                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
153                         db_strerror(rs->sr_err), rs->sr_err, 0 );
154                 rs->sr_err = LDAP_OTHER;
155                 rs->sr_text = "internal error";
156                 goto return_results;
157         }
158
159         opinfo.boi_oe.oe_key = bdb;
160         opinfo.boi_txn = ltid;
161         opinfo.boi_err = 0;
162         opinfo.boi_acl_cache = op->o_do_not_cache;
163         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.boi_oe, oe_next );
164
165         /*
166          * Get the parent dn and see if the corresponding entry exists.
167          */
168         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
169                 pdn = slap_empty_bv;
170         } else {
171                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
172         }
173
174         /* get entry or parent */
175         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
176                 1, &lock );
177         switch( rs->sr_err ) {
178         case 0:
179                 rs->sr_err = LDAP_ALREADY_EXISTS;
180                 goto return_results;
181         case DB_NOTFOUND:
182                 break;
183         case DB_LOCK_DEADLOCK:
184         case DB_LOCK_NOTGRANTED:
185                 goto retry;
186         case LDAP_BUSY:
187                 rs->sr_text = "ldap server busy";
188                 goto return_results;
189         default:
190                 rs->sr_err = LDAP_OTHER;
191                 rs->sr_text = "internal error";
192                 goto return_results;
193         }
194
195         p = ei->bei_e;
196         if ( !p )
197                 p = (Entry *)&slap_entry_root;
198
199         if ( !bvmatch( &pdn, &p->e_nname ) ) {
200                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
201                         op->o_tmpmemctx );
202                 rs->sr_ref = is_entry_referral( p )
203                         ? get_entry_referrals( op, p )
204                         : NULL;
205                 bdb_unlocked_cache_return_entry_r( bdb, p );
206                 p = NULL;
207                 Debug( LDAP_DEBUG_TRACE,
208                         LDAP_XSTRING(bdb_add) ": parent "
209                         "does not exist\n", 0, 0, 0 );
210
211                 rs->sr_err = LDAP_REFERRAL;
212                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
213                 goto return_results;
214         }
215
216         rs->sr_err = access_allowed( op, p,
217                 children, NULL, ACL_WADD, NULL );
218
219         if ( ! rs->sr_err ) {
220                 switch( opinfo.boi_err ) {
221                 case DB_LOCK_DEADLOCK:
222                 case DB_LOCK_NOTGRANTED:
223                         goto retry;
224                 }
225
226                 Debug( LDAP_DEBUG_TRACE,
227                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
228                         0, 0, 0 );
229                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
230                 rs->sr_text = "no write access to parent";
231                 goto return_results;;
232         }
233
234         if ( p != (Entry *)&slap_entry_root ) {
235                 if ( is_entry_subentry( p ) ) {
236                         /* parent is a subentry, don't allow add */
237                         Debug( LDAP_DEBUG_TRACE,
238                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
239                                 0, 0, 0 );
240                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
241                         rs->sr_text = "parent is a subentry";
242                         goto return_results;;
243                 }
244
245                 if ( is_entry_alias( p ) ) {
246                         /* parent is an alias, don't allow add */
247                         Debug( LDAP_DEBUG_TRACE,
248                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
249                                 0, 0, 0 );
250                         rs->sr_err = LDAP_ALIAS_PROBLEM;
251                         rs->sr_text = "parent is an alias";
252                         goto return_results;;
253                 }
254
255                 if ( is_entry_referral( p ) ) {
256                         /* parent is a referral, don't allow add */
257                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
258                                 op->o_tmpmemctx );
259                         rs->sr_ref = get_entry_referrals( op, p );
260                         bdb_unlocked_cache_return_entry_r( bdb, p );
261                         p = NULL;
262                         Debug( LDAP_DEBUG_TRACE,
263                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
264                                 0, 0, 0 );
265
266                         rs->sr_err = LDAP_REFERRAL;
267                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
268                         goto return_results;
269                 }
270
271         }
272
273         if ( subentry ) {
274                 /* FIXME: */
275                 /* parent must be an administrative point of the required kind */
276         }
277
278         /* free parent and reader lock */
279         if ( p != (Entry *)&slap_entry_root ) {
280                 bdb_unlocked_cache_return_entry_r( bdb, p );
281         }
282         p = NULL;
283
284         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
285                 entry, NULL, ACL_WADD, NULL );
286
287         if ( ! rs->sr_err ) {
288                 switch( opinfo.boi_err ) {
289                 case DB_LOCK_DEADLOCK:
290                 case DB_LOCK_NOTGRANTED:
291                         goto retry;
292                 }
293
294                 Debug( LDAP_DEBUG_TRACE,
295                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
296                         0, 0, 0 );
297                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
298                 rs->sr_text = "no write access to entry";
299                 goto return_results;;
300         }
301
302         /* 
303          * Check ACL for attribute write access
304          */
305         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
306                 switch( opinfo.boi_err ) {
307                 case DB_LOCK_DEADLOCK:
308                 case DB_LOCK_NOTGRANTED:
309                         goto retry;
310                 }
311
312                 Debug( LDAP_DEBUG_TRACE,
313                         LDAP_XSTRING(bdb_add) ": no write access to attribute\n",
314                         0, 0, 0 );
315                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
316                 rs->sr_text = "no write access to attribute";
317                 goto return_results;;
318         }
319
320         if ( eid == NOID ) {
321                 rs->sr_err = bdb_next_id( op->o_bd, &eid );
322                 if( rs->sr_err != 0 ) {
323                         Debug( LDAP_DEBUG_TRACE,
324                                 LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
325                                 rs->sr_err, 0, 0 );
326                         rs->sr_err = LDAP_OTHER;
327                         rs->sr_text = "internal error";
328                         goto return_results;
329                 }
330                 op->oq_add.rs_e->e_id = eid;
331         }
332
333         /* nested transaction */
334         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
335                 bdb->bi_db_opflags );
336         rs->sr_text = NULL;
337         if( rs->sr_err != 0 ) {
338                 Debug( LDAP_DEBUG_TRACE,
339                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
340                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
341                 rs->sr_err = LDAP_OTHER;
342                 rs->sr_text = "internal error";
343                 goto return_results;
344         }
345
346         /* dn2id index */
347         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
348         if ( rs->sr_err != 0 ) {
349                 Debug( LDAP_DEBUG_TRACE,
350                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
351                         db_strerror(rs->sr_err), rs->sr_err, 0 );
352
353                 switch( rs->sr_err ) {
354                 case DB_LOCK_DEADLOCK:
355                 case DB_LOCK_NOTGRANTED:
356                         goto retry;
357                 case DB_KEYEXIST:
358                         rs->sr_err = LDAP_ALREADY_EXISTS;
359                         break;
360                 default:
361                         rs->sr_err = LDAP_OTHER;
362                 }
363                 goto return_results;
364         }
365
366         /* attribute indexes */
367         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
368         if ( rs->sr_err != LDAP_SUCCESS ) {
369                 Debug( LDAP_DEBUG_TRACE,
370                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
371                         0, 0, 0 );
372                 switch( rs->sr_err ) {
373                 case DB_LOCK_DEADLOCK:
374                 case DB_LOCK_NOTGRANTED:
375                         goto retry;
376                 default:
377                         rs->sr_err = LDAP_OTHER;
378                 }
379                 rs->sr_text = "index generation failed";
380                 goto return_results;
381         }
382
383         /* id2entry index */
384         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
385         if ( rs->sr_err != 0 ) {
386                 Debug( LDAP_DEBUG_TRACE,
387                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
388                         0, 0, 0 );
389                 switch( rs->sr_err ) {
390                 case DB_LOCK_DEADLOCK:
391                 case DB_LOCK_NOTGRANTED:
392                         goto retry;
393                 default:
394                         rs->sr_err = LDAP_OTHER;
395                 }
396                 rs->sr_text = "entry store failed";
397                 goto return_results;
398         }
399
400         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
401                 rs->sr_err = LDAP_OTHER;
402                 rs->sr_text = "txn_commit(2) failed";
403                 goto return_results;
404         }
405
406         /* post-read */
407         if( op->o_postread ) {
408                 if( postread_ctrl == NULL ) {
409                         postread_ctrl = &ctrls[num_ctrls++];
410                         ctrls[num_ctrls] = NULL;
411                 }
412                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
413                         &slap_post_read_bv, postread_ctrl ) )
414                 {
415                         Debug( LDAP_DEBUG_TRACE,
416                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
417                                 "failed!\n", 0, 0, 0 );
418                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
419                                 /* FIXME: is it correct to abort
420                                  * operation if control fails? */
421                                 goto return_results;
422                         }
423                 }
424         }
425
426         if ( op->o_noop ) {
427                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
428                         rs->sr_text = "txn_abort (no-op) failed";
429                 } else {
430                         rs->sr_err = LDAP_X_NO_OPERATION;
431                         ltid = NULL;
432                         goto return_results;
433                 }
434
435         } else {
436                 struct berval nrdn;
437
438                 /* pick the RDN if not suffix; otherwise pick the entire DN */
439                 if (pdn.bv_len) {
440                         nrdn.bv_val = op->ora_e->e_nname.bv_val;
441                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
442                 } else {
443                         nrdn = op->ora_e->e_nname;
444                 }
445
446                 /* Use the reader txn here, outside the add txn */
447                 bdb_cache_add( bdb, ei, op->ora_e, &nrdn, rtxn, &lock );
448
449                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
450                         rs->sr_text = "txn_commit failed";
451                 } else {
452                         rs->sr_err = LDAP_SUCCESS;
453                 }
454         }
455
456         ltid = NULL;
457         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
458         opinfo.boi_oe.oe_key = NULL;
459
460         if ( rs->sr_err != LDAP_SUCCESS ) {
461                 Debug( LDAP_DEBUG_TRACE,
462                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
463                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
464                 rs->sr_err = LDAP_OTHER;
465                 goto return_results;
466         }
467
468         Debug(LDAP_DEBUG_TRACE,
469                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
470                 op->o_noop ? " (no-op)" : "",
471                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
472
473         rs->sr_text = NULL;
474         if( num_ctrls ) rs->sr_ctrls = ctrls;
475
476 return_results:
477         success = rs->sr_err;
478         send_ldap_result( op, rs );
479         slap_graduate_commit_csn( op );
480
481         if( ltid != NULL ) {
482                 TXN_ABORT( ltid );
483         }
484         if ( opinfo.boi_oe.oe_key ) {
485                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.boi_oe, OpExtra, oe_next );
486         }
487
488         if( success == LDAP_SUCCESS ) {
489                 /* We own the entry now, and it can be purged at will
490                  * Check to make sure it's the same entry we entered with.
491                  * Possibly a callback may have mucked with it, although
492                  * in general callbacks should treat the entry as read-only.
493                  */
494                 bdb_cache_return_entry_r( bdb, oe, &lock );
495                 if ( op->ora_e == oe )
496                         op->ora_e = NULL;
497
498                 if ( bdb->bi_txn_cp_kbyte ) {
499                         TXN_CHECKPOINT( bdb->bi_dbenv,
500                                 bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
501                 }
502         }
503
504         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
505                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
506                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
507         }
508
509         return rs->sr_err;
510 }