]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
return structuralObjectClass errors
[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;
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;
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         /* add opattrs to shadow as well, only missing attrs will actually
95          * be added; helps compatibility with older OL versions */
96         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
97         if ( rs->sr_err != LDAP_SUCCESS ) {
98                 Debug( LDAP_DEBUG_TRACE,
99                         LDAP_XSTRING(bdb_add) ": entry failed op attrs add: "
100                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
101                 goto return_results;
102         }
103
104         /* check entry's schema */
105         rs->sr_err = entry_schema_check( op, op->oq_add.rs_e, NULL,
106                 get_relax(op), &rs->sr_text, textbuf, textlen );
107         if ( rs->sr_err != LDAP_SUCCESS ) {
108                 Debug( LDAP_DEBUG_TRACE,
109                         LDAP_XSTRING(bdb_add) ": entry failed schema check: "
110                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
111                 goto return_results;
112         }
113
114         subentry = is_entry_subentry( op->oq_add.rs_e );
115
116         /*
117          * acquire an ID outside of the operation transaction
118          * to avoid serializing adds.
119          */
120         rs->sr_err = bdb_next_id( op->o_bd, NULL, &op->oq_add.rs_e->e_id );
121         if( rs->sr_err != 0 ) {
122                 Debug( LDAP_DEBUG_TRACE,
123                         LDAP_XSTRING(bdb_add) ": next_id failed (%d)\n",
124                         rs->sr_err, 0, 0 );
125                 rs->sr_err = LDAP_OTHER;
126                 rs->sr_text = "internal error";
127                 goto return_results;
128         }
129
130         if( 0 ) {
131 retry:  /* transaction retry */
132                 if( p ) {
133                         /* free parent and reader lock */
134                         if ( p != (Entry *)&slap_entry_root ) {
135                                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
136                         }
137                         p = NULL;
138                 }
139                 rs->sr_err = TXN_ABORT( ltid );
140                 ltid = NULL;
141                 op->o_private = NULL;
142                 op->o_do_not_cache = opinfo.boi_acl_cache;
143                 if( rs->sr_err != 0 ) {
144                         rs->sr_err = LDAP_OTHER;
145                         rs->sr_text = "internal error";
146                         goto return_results;
147                 }
148                 if ( op->o_abandon ) {
149                         rs->sr_err = SLAPD_ABANDON;
150                         goto return_results;
151                 }
152                 bdb_trans_backoff( ++num_retries );
153         }
154
155         /* begin transaction */
156         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, NULL, &ltid, 
157                 bdb->bi_db_opflags );
158         rs->sr_text = NULL;
159         if( rs->sr_err != 0 ) {
160                 Debug( LDAP_DEBUG_TRACE,
161                         LDAP_XSTRING(bdb_add) ": txn_begin failed: %s (%d)\n",
162                         db_strerror(rs->sr_err), rs->sr_err, 0 );
163                 rs->sr_err = LDAP_OTHER;
164                 rs->sr_text = "internal error";
165                 goto return_results;
166         }
167
168         locker = TXN_ID ( ltid );
169
170         opinfo.boi_bdb = op->o_bd;
171         opinfo.boi_txn = ltid;
172         opinfo.boi_locker = locker;
173         opinfo.boi_err = 0;
174         opinfo.boi_acl_cache = op->o_do_not_cache;
175         op->o_private = &opinfo;
176         
177         /*
178          * Get the parent dn and see if the corresponding entry exists.
179          */
180         if ( be_issuffix( op->o_bd, &op->oq_add.rs_e->e_nname ) ) {
181                 pdn = slap_empty_bv;
182         } else {
183                 dnParent( &op->oq_add.rs_e->e_nname, &pdn );
184         }
185
186         /* get entry or parent */
187         rs->sr_err = bdb_dn2entry( op, ltid, &op->ora_e->e_nname, &ei,
188                 1, locker, &lock );
189         switch( rs->sr_err ) {
190         case 0:
191                 rs->sr_err = LDAP_ALREADY_EXISTS;
192                 goto return_results;
193         case DB_NOTFOUND:
194                 break;
195         case DB_LOCK_DEADLOCK:
196         case DB_LOCK_NOTGRANTED:
197                 goto retry;
198         case LDAP_BUSY:
199                 rs->sr_text = "ldap server busy";
200                 goto return_results;
201         default:
202                 rs->sr_err = LDAP_OTHER;
203                 rs->sr_text = "internal error";
204                 goto return_results;
205         }
206
207         p = ei->bei_e;
208         if ( !p )
209                 p = (Entry *)&slap_entry_root;
210
211         if ( !bvmatch( &pdn, &p->e_nname ) ) {
212                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
213                         op->o_tmpmemctx );
214                 rs->sr_ref = is_entry_referral( p )
215                         ? get_entry_referrals( op, p )
216                         : NULL;
217                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
218                 p = NULL;
219                 Debug( LDAP_DEBUG_TRACE,
220                         LDAP_XSTRING(bdb_add) ": parent "
221                         "does not exist\n", 0, 0, 0 );
222
223                 rs->sr_err = LDAP_REFERRAL;
224                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
225                 goto return_results;
226         }
227
228         rs->sr_err = access_allowed( op, p,
229                 children, NULL, ACL_WADD, NULL );
230
231         if ( ! rs->sr_err ) {
232                 switch( opinfo.boi_err ) {
233                 case DB_LOCK_DEADLOCK:
234                 case DB_LOCK_NOTGRANTED:
235                         goto retry;
236                 }
237
238                 Debug( LDAP_DEBUG_TRACE,
239                         LDAP_XSTRING(bdb_add) ": no write access to parent\n",
240                         0, 0, 0 );
241                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
242                 rs->sr_text = "no write access to parent";
243                 goto return_results;;
244         }
245
246         if ( p != (Entry *)&slap_entry_root ) {
247                 if ( is_entry_subentry( p ) ) {
248                         /* parent is a subentry, don't allow add */
249                         Debug( LDAP_DEBUG_TRACE,
250                                 LDAP_XSTRING(bdb_add) ": parent is subentry\n",
251                                 0, 0, 0 );
252                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
253                         rs->sr_text = "parent is a subentry";
254                         goto return_results;;
255                 }
256
257                 if ( is_entry_alias( p ) ) {
258                         /* parent is an alias, don't allow add */
259                         Debug( LDAP_DEBUG_TRACE,
260                                 LDAP_XSTRING(bdb_add) ": parent is alias\n",
261                                 0, 0, 0 );
262                         rs->sr_err = LDAP_ALIAS_PROBLEM;
263                         rs->sr_text = "parent is an alias";
264                         goto return_results;;
265                 }
266
267                 if ( is_entry_referral( p ) ) {
268                         /* parent is a referral, don't allow add */
269                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
270                                 op->o_tmpmemctx );
271                         rs->sr_ref = get_entry_referrals( op, p );
272                         bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
273                         p = NULL;
274                         Debug( LDAP_DEBUG_TRACE,
275                                 LDAP_XSTRING(bdb_add) ": parent is referral\n",
276                                 0, 0, 0 );
277
278                         rs->sr_err = LDAP_REFERRAL;
279                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
280                         goto return_results;
281                 }
282
283                 if ( subentry ) {
284                         /* FIXME: */
285                         /* parent must be an administrative point of the required kind */
286                 }
287         }
288
289         /* free parent and reader lock */
290         if ( p != (Entry *)&slap_entry_root ) {
291                 bdb_unlocked_cache_return_entry_r( &bdb->bi_cache, p );
292         }
293         p = NULL;
294
295         rs->sr_err = access_allowed( op, op->oq_add.rs_e,
296                 entry, NULL, ACL_WADD, NULL );
297
298         if ( ! rs->sr_err ) {
299                 switch( opinfo.boi_err ) {
300                 case DB_LOCK_DEADLOCK:
301                 case DB_LOCK_NOTGRANTED:
302                         goto retry;
303                 }
304
305                 Debug( LDAP_DEBUG_TRACE,
306                         LDAP_XSTRING(bdb_add) ": no write access to entry\n",
307                         0, 0, 0 );
308                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
309                 rs->sr_text = "no write access to entry";
310                 goto return_results;;
311         }
312
313         /* nested transaction */
314         rs->sr_err = TXN_BEGIN( bdb->bi_dbenv, ltid, &lt2, 
315                 bdb->bi_db_opflags );
316         rs->sr_text = NULL;
317         if( rs->sr_err != 0 ) {
318                 Debug( LDAP_DEBUG_TRACE,
319                         LDAP_XSTRING(bdb_add) ": txn_begin(2) failed: "
320                         "%s (%d)\n", db_strerror(rs->sr_err), rs->sr_err, 0 );
321                 rs->sr_err = LDAP_OTHER;
322                 rs->sr_text = "internal error";
323                 goto return_results;
324         }
325
326         /* dn2id index */
327         rs->sr_err = bdb_dn2id_add( op, lt2, ei, op->oq_add.rs_e );
328         if ( rs->sr_err != 0 ) {
329                 Debug( LDAP_DEBUG_TRACE,
330                         LDAP_XSTRING(bdb_add) ": dn2id_add failed: %s (%d)\n",
331                         db_strerror(rs->sr_err), rs->sr_err, 0 );
332
333                 switch( rs->sr_err ) {
334                 case DB_LOCK_DEADLOCK:
335                 case DB_LOCK_NOTGRANTED:
336                         goto retry;
337                 case DB_KEYEXIST:
338                         rs->sr_err = LDAP_ALREADY_EXISTS;
339                         break;
340                 default:
341                         rs->sr_err = LDAP_OTHER;
342                 }
343                 goto return_results;
344         }
345
346         /* attribute indexes */
347         rs->sr_err = bdb_index_entry_add( op, lt2, op->oq_add.rs_e );
348         if ( rs->sr_err != LDAP_SUCCESS ) {
349                 Debug( LDAP_DEBUG_TRACE,
350                         LDAP_XSTRING(bdb_add) ": index_entry_add failed\n",
351                         0, 0, 0 );
352                 switch( rs->sr_err ) {
353                 case DB_LOCK_DEADLOCK:
354                 case DB_LOCK_NOTGRANTED:
355                         goto retry;
356                 default:
357                         rs->sr_err = LDAP_OTHER;
358                 }
359                 rs->sr_text = "index generation failed";
360                 goto return_results;
361         }
362
363         /* id2entry index */
364         rs->sr_err = bdb_id2entry_add( op->o_bd, lt2, op->oq_add.rs_e );
365         if ( rs->sr_err != 0 ) {
366                 Debug( LDAP_DEBUG_TRACE,
367                         LDAP_XSTRING(bdb_add) ": id2entry_add failed\n",
368                         0, 0, 0 );
369                 switch( rs->sr_err ) {
370                 case DB_LOCK_DEADLOCK:
371                 case DB_LOCK_NOTGRANTED:
372                         goto retry;
373                 default:
374                         rs->sr_err = LDAP_OTHER;
375                 }
376                 rs->sr_text = "entry store failed";
377                 goto return_results;
378         }
379
380         if ( TXN_COMMIT( lt2, 0 ) != 0 ) {
381                 rs->sr_err = LDAP_OTHER;
382                 rs->sr_text = "txn_commit(2) failed";
383                 goto return_results;
384         }
385
386         /* post-read */
387         if( op->o_postread ) {
388                 if( postread_ctrl == NULL ) {
389                         postread_ctrl = &ctrls[num_ctrls++];
390                         ctrls[num_ctrls] = NULL;
391                 }
392                 if ( slap_read_controls( op, rs, op->oq_add.rs_e,
393                         &slap_post_read_bv, postread_ctrl ) )
394                 {
395                         Debug( LDAP_DEBUG_TRACE,
396                                 "<=- " LDAP_XSTRING(bdb_add) ": post-read "
397                                 "failed!\n", 0, 0, 0 );
398                         goto return_results;
399                 }
400         }
401
402         if ( op->o_noop ) {
403                 if (( rs->sr_err=TXN_ABORT( ltid )) != 0 ) {
404                         rs->sr_text = "txn_abort (no-op) failed";
405                 } else {
406                         rs->sr_err = LDAP_X_NO_OPERATION;
407                         ltid = NULL;
408                         goto return_results;
409                 }
410
411         } else {
412                 struct berval nrdn;
413                 Entry *e = entry_dup( op->ora_e );
414
415                 /* pick the RDN if not suffix; otherwise pick the entire DN */
416                 if (pdn.bv_len) {
417                         nrdn.bv_val = e->e_nname.bv_val;
418                         nrdn.bv_len = pdn.bv_val - op->ora_e->e_nname.bv_val - 1;
419                 } else {
420                         nrdn = e->e_nname;
421                 }
422
423                 bdb_cache_add( bdb, ei, e, &nrdn, locker );
424
425                 if(( rs->sr_err=TXN_COMMIT( ltid, 0 )) != 0 ) {
426                         rs->sr_text = "txn_commit failed";
427                 } else {
428                         rs->sr_err = LDAP_SUCCESS;
429                 }
430         }
431
432         ltid = NULL;
433         op->o_private = NULL;
434
435         if ( rs->sr_err != LDAP_SUCCESS ) {
436                 Debug( LDAP_DEBUG_TRACE,
437                         LDAP_XSTRING(bdb_add) ": %s : %s (%d)\n",
438                         rs->sr_text, db_strerror(rs->sr_err), rs->sr_err );
439                 rs->sr_err = LDAP_OTHER;
440                 goto return_results;
441         }
442
443         Debug(LDAP_DEBUG_TRACE,
444                 LDAP_XSTRING(bdb_add) ": added%s id=%08lx dn=\"%s\"\n",
445                 op->o_noop ? " (no-op)" : "",
446                 op->oq_add.rs_e->e_id, op->oq_add.rs_e->e_dn );
447
448         rs->sr_text = NULL;
449         if( num_ctrls ) rs->sr_ctrls = ctrls;
450
451 return_results:
452         send_ldap_result( op, rs );
453         slap_graduate_commit_csn( op );
454
455         if( ltid != NULL ) {
456                 TXN_ABORT( ltid );
457         }
458         op->o_private = NULL;
459
460         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
461                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
462                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
463         }
464
465         if( rs->sr_err == LDAP_SUCCESS && bdb->bi_txn_cp ) {
466                 TXN_CHECKPOINT( bdb->bi_dbenv,
467                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
468         }
469         return rs->sr_err;
470 }