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