]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/add.c
Happy New Year!
[openldap] / servers / slapd / back-mdb / add.c
1 /* add.c - ldap mdb back-end add routine */
2 /* $OpenLDAP$ */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4  *
5  * Copyright 2000-2012 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-mdb.h"
23
24 int
25 mdb_add(Operation *op, SlapReply *rs )
26 {
27         struct mdb_info *mdb = (struct mdb_info *) op->o_bd->be_private;
28         struct berval   pdn;
29         Entry           *p = NULL, *oe = op->ora_e;
30         char textbuf[SLAP_TEXT_BUFLEN];
31         size_t textlen = sizeof textbuf;
32         AttributeDescription *children = slap_schema.si_ad_children;
33         AttributeDescription *entry = slap_schema.si_ad_entry;
34         MDB_txn         *txn = NULL;
35         MDB_cursor      *mc = NULL;
36         MDB_cursor      *mcd;
37         ID eid, pid = 0;
38         mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
39         int subentry;
40
41         int             success;
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(mdb_add) ": %s\n",
52                 op->ora_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         /* check entry's schema */
95         rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
96                 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
97         if ( rs->sr_err != LDAP_SUCCESS ) {
98                 Debug( LDAP_DEBUG_TRACE,
99                         LDAP_XSTRING(mdb_add) ": entry failed schema check: "
100                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
101                 goto return_results;
102         }
103
104         /* add opattrs to shadow as well, only missing attrs will actually
105          * be added; helps compatibility with older OL versions */
106         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
107         if ( rs->sr_err != LDAP_SUCCESS ) {
108                 Debug( LDAP_DEBUG_TRACE,
109                         LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
110                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
111                 goto return_results;
112         }
113
114         if ( get_assert( op ) &&
115                 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
116         {
117                 rs->sr_err = LDAP_ASSERTION_FAILED;
118                 goto return_results;
119         }
120
121         subentry = is_entry_subentry( op->ora_e );
122
123         /* begin transaction */
124         rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
125         rs->sr_text = NULL;
126         if( rs->sr_err != 0 ) {
127                 Debug( LDAP_DEBUG_TRACE,
128                         LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
129                         mdb_strerror(rs->sr_err), rs->sr_err, 0 );
130                 rs->sr_err = LDAP_OTHER;
131                 rs->sr_text = "internal error";
132                 goto return_results;
133         }
134
135         txn = moi->moi_txn;
136
137         /*
138          * Get the parent dn and see if the corresponding entry exists.
139          */
140         if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
141                 pdn = slap_empty_bv;
142         } else {
143                 dnParent( &op->ora_e->e_nname, &pdn );
144         }
145
146         rs->sr_err = mdb_cursor_open( txn, mdb->mi_dn2id, &mcd );
147         if( rs->sr_err != 0 ) {
148                 Debug( LDAP_DEBUG_TRACE,
149                         LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
150                         rs->sr_err, 0, 0 );
151                 rs->sr_err = LDAP_OTHER;
152                 rs->sr_text = "internal error";
153                 goto return_results;
154         }
155
156         /* get entry or parent */
157         rs->sr_err = mdb_dn2entry( op, txn, mcd, &op->ora_e->e_nname, &p, 1 );
158         switch( rs->sr_err ) {
159         case 0:
160                 rs->sr_err = LDAP_ALREADY_EXISTS;
161                 mdb_entry_return( op, p );
162                 p = NULL;
163                 goto return_results;
164         case MDB_NOTFOUND:
165                 break;
166         case LDAP_BUSY:
167                 rs->sr_text = "ldap server busy";
168                 goto return_results;
169         default:
170                 rs->sr_err = LDAP_OTHER;
171                 rs->sr_text = "internal error";
172                 goto return_results;
173         }
174
175         if ( !p )
176                 p = (Entry *)&slap_entry_root;
177
178         if ( !bvmatch( &pdn, &p->e_nname ) ) {
179                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
180                         op->o_tmpmemctx );
181                 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
182                         BerVarray ref = get_entry_referrals( op, p );
183                         rs->sr_ref = referral_rewrite( ref, &p->e_name,
184                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
185                         ber_bvarray_free( ref );
186                 } else {
187                         rs->sr_ref = NULL;
188                 }
189                 if ( p != (Entry *)&slap_entry_root )
190                         mdb_entry_return( op, p );
191                 p = NULL;
192                 Debug( LDAP_DEBUG_TRACE,
193                         LDAP_XSTRING(mdb_add) ": parent "
194                         "does not exist\n", 0, 0, 0 );
195
196                 rs->sr_err = LDAP_REFERRAL;
197                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
198                 goto return_results;
199         }
200
201         rs->sr_err = access_allowed( op, p,
202                 children, NULL, ACL_WADD, NULL );
203
204         if ( ! rs->sr_err ) {
205                 if ( p != (Entry *)&slap_entry_root )
206                         mdb_entry_return( op, p );
207                 p = NULL;
208
209                 Debug( LDAP_DEBUG_TRACE,
210                         LDAP_XSTRING(mdb_add) ": no write access to parent\n",
211                         0, 0, 0 );
212                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
213                 rs->sr_text = "no write access to parent";
214                 goto return_results;;
215         }
216
217         if ( p != (Entry *)&slap_entry_root ) {
218                 if ( is_entry_subentry( p ) ) {
219                         mdb_entry_return( op, p );
220                         p = NULL;
221                         /* parent is a subentry, don't allow add */
222                         Debug( LDAP_DEBUG_TRACE,
223                                 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
224                                 0, 0, 0 );
225                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
226                         rs->sr_text = "parent is a subentry";
227                         goto return_results;;
228                 }
229
230                 if ( is_entry_alias( p ) ) {
231                         mdb_entry_return( op, p );
232                         p = NULL;
233                         /* parent is an alias, don't allow add */
234                         Debug( LDAP_DEBUG_TRACE,
235                                 LDAP_XSTRING(mdb_add) ": parent is alias\n",
236                                 0, 0, 0 );
237                         rs->sr_err = LDAP_ALIAS_PROBLEM;
238                         rs->sr_text = "parent is an alias";
239                         goto return_results;;
240                 }
241
242                 if ( is_entry_referral( p ) ) {
243                         BerVarray ref = get_entry_referrals( op, p );
244                         /* parent is a referral, don't allow add */
245                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
246                                 op->o_tmpmemctx );
247                         rs->sr_ref = referral_rewrite( ref, &p->e_name,
248                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
249                         ber_bvarray_free( ref );
250                         mdb_entry_return( op, p );
251                         p = NULL;
252                         Debug( LDAP_DEBUG_TRACE,
253                                 LDAP_XSTRING(mdb_add) ": parent is referral\n",
254                                 0, 0, 0 );
255
256                         rs->sr_err = LDAP_REFERRAL;
257                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
258                         goto return_results;
259                 }
260
261         }
262
263         if ( subentry ) {
264                 /* FIXME: */
265                 /* parent must be an administrative point of the required kind */
266         }
267
268         /* free parent */
269         if ( p != (Entry *)&slap_entry_root ) {
270                 pid = p->e_id;
271                 if ( p->e_nname.bv_len ) {
272                         struct berval ppdn;
273
274                         /* ITS#5326: use parent's DN if differs from provided one */
275                         dnParent( &op->ora_e->e_name, &ppdn );
276                         if ( !dn_match( &p->e_name, &ppdn ) ) {
277                                 struct berval rdn;
278                                 struct berval newdn;
279
280                                 dnRdn( &op->ora_e->e_name, &rdn );
281
282                                 build_new_dn( &newdn, &p->e_name, &rdn, NULL ); 
283                                 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
284                                         ber_memfree( op->ora_e->e_name.bv_val );
285                                 op->ora_e->e_name = newdn;
286
287                                 /* FIXME: should check whether
288                                  * dnNormalize(newdn) == e->e_nname ... */
289                         }
290                 }
291
292                 mdb_entry_return( op, p );
293         }
294         p = NULL;
295
296         rs->sr_err = access_allowed( op, op->ora_e,
297                 entry, NULL, ACL_WADD, NULL );
298
299         if ( ! rs->sr_err ) {
300                 Debug( LDAP_DEBUG_TRACE,
301                         LDAP_XSTRING(mdb_add) ": no write access to entry\n",
302                         0, 0, 0 );
303                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
304                 rs->sr_text = "no write access to entry";
305                 goto return_results;;
306         }
307
308         /* 
309          * Check ACL for attribute write access
310          */
311         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
312                 Debug( LDAP_DEBUG_TRACE,
313                         LDAP_XSTRING(mdb_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         rs->sr_err = mdb_cursor_open( txn, mdb->mi_id2entry, &mc );
321         if( rs->sr_err != 0 ) {
322                 Debug( LDAP_DEBUG_TRACE,
323                         LDAP_XSTRING(mdb_add) ": mdb_cursor_open failed (%d)\n",
324                         rs->sr_err, 0, 0 );
325                 rs->sr_err = LDAP_OTHER;
326                 rs->sr_text = "internal error";
327                 goto return_results;
328         }
329
330         rs->sr_err = mdb_next_id( op->o_bd, mc, &eid );
331         if( rs->sr_err != 0 ) {
332                 Debug( LDAP_DEBUG_TRACE,
333                         LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
334                         rs->sr_err, 0, 0 );
335                 rs->sr_err = LDAP_OTHER;
336                 rs->sr_text = "internal error";
337                 goto return_results;
338         }
339         op->ora_e->e_id = eid;
340
341         /* dn2id index */
342         rs->sr_err = mdb_dn2id_add( op, mcd, mcd, pid, op->ora_e );
343         mdb_cursor_close( mcd );
344         if ( rs->sr_err != 0 ) {
345                 Debug( LDAP_DEBUG_TRACE,
346                         LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
347                         mdb_strerror(rs->sr_err), rs->sr_err, 0 );
348
349                 switch( rs->sr_err ) {
350                 case MDB_KEYEXIST:
351                         rs->sr_err = LDAP_ALREADY_EXISTS;
352                         break;
353                 default:
354                         rs->sr_err = LDAP_OTHER;
355                 }
356                 goto return_results;
357         }
358
359         /* attribute indexes */
360         rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
361         if ( rs->sr_err != LDAP_SUCCESS ) {
362                 Debug( LDAP_DEBUG_TRACE,
363                         LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
364                         0, 0, 0 );
365                 rs->sr_err = LDAP_OTHER;
366                 rs->sr_text = "index generation failed";
367                 goto return_results;
368         }
369
370         /* id2entry index */
371         rs->sr_err = mdb_id2entry_add( op, txn, mc, op->ora_e );
372         if ( rs->sr_err != 0 ) {
373                 Debug( LDAP_DEBUG_TRACE,
374                         LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
375                         0, 0, 0 );
376                 rs->sr_err = LDAP_OTHER;
377                 rs->sr_text = "entry store failed";
378                 goto return_results;
379         }
380
381         /* post-read */
382         if( op->o_postread ) {
383                 if( postread_ctrl == NULL ) {
384                         postread_ctrl = &ctrls[num_ctrls++];
385                         ctrls[num_ctrls] = NULL;
386                 }
387                 if ( slap_read_controls( op, rs, op->ora_e,
388                         &slap_post_read_bv, postread_ctrl ) )
389                 {
390                         Debug( LDAP_DEBUG_TRACE,
391                                 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
392                                 "failed!\n", 0, 0, 0 );
393                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
394                                 /* FIXME: is it correct to abort
395                                  * operation if control fails? */
396                                 goto return_results;
397                         }
398                 }
399         }
400
401         if ( moi == &opinfo ) {
402                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
403                 opinfo.moi_oe.oe_key = NULL;
404                 if ( op->o_noop ) {
405                         mdb_txn_abort( txn );
406                         rs->sr_err = LDAP_X_NO_OPERATION;
407                         txn = NULL;
408                         goto return_results;
409                 }
410
411                 if (( rs->sr_err = mdb_txn_commit( txn )) != 0 ) {
412                         rs->sr_text = "txn_commit failed";
413                         Debug( LDAP_DEBUG_TRACE,
414                                 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
415                                 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
416                         rs->sr_err = LDAP_OTHER;
417                         goto return_results;
418                 }
419                 txn = NULL;
420         }
421
422         Debug(LDAP_DEBUG_TRACE,
423                 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
424                 op->o_noop ? " (no-op)" : "",
425                 op->ora_e->e_id, op->ora_e->e_dn );
426
427         rs->sr_text = NULL;
428         if( num_ctrls ) rs->sr_ctrls = ctrls;
429
430 return_results:
431         success = rs->sr_err;
432         send_ldap_result( op, rs );
433
434         if( moi == &opinfo ) {
435                 if( txn != NULL ) {
436                         mdb_txn_abort( txn );
437                 }
438                 if ( opinfo.moi_oe.oe_key ) {
439                         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
440                 }
441         }
442
443         if( success == LDAP_SUCCESS ) {
444 #if 0
445                 if ( mdb->bi_txn_cp_kbyte ) {
446                         TXN_CHECKPOINT( mdb->bi_dbenv,
447                                 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
448                 }
449 #endif
450         }
451
452         slap_graduate_commit_csn( op );
453
454         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
455                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
456                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
457         }
458         return rs->sr_err;
459 }