]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/add.c
Merge remote branch 'mdb/mdb.master'
[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-2011 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         ID eid = NOID, pid = 0;
36         mdb_op_info opinfo = {{{ 0 }}}, *moi = &opinfo;
37         int subentry;
38
39         int             success;
40
41         LDAPControl **postread_ctrl = NULL;
42         LDAPControl *ctrls[SLAP_MAX_RESPONSE_CONTROLS];
43         int num_ctrls = 0;
44
45 #ifdef LDAP_X_TXN
46         int settle = 0;
47 #endif
48
49         Debug(LDAP_DEBUG_ARGS, "==> " LDAP_XSTRING(mdb_add) ": %s\n",
50                 op->ora_e->e_name.bv_val, 0, 0);
51
52 #ifdef LDAP_X_TXN
53         if( op->o_txnSpec ) {
54                 /* acquire connection lock */
55                 ldap_pvt_thread_mutex_lock( &op->o_conn->c_mutex );
56                 if( op->o_conn->c_txn == CONN_TXN_INACTIVE ) {
57                         rs->sr_text = "invalid transaction identifier";
58                         rs->sr_err = LDAP_X_TXN_ID_INVALID;
59                         goto txnReturn;
60                 } else if( op->o_conn->c_txn == CONN_TXN_SETTLE ) {
61                         settle=1;
62                         goto txnReturn;
63                 }
64
65                 if( op->o_conn->c_txn_backend == NULL ) {
66                         op->o_conn->c_txn_backend = op->o_bd;
67
68                 } else if( op->o_conn->c_txn_backend != op->o_bd ) {
69                         rs->sr_text = "transaction cannot span multiple database contexts";
70                         rs->sr_err = LDAP_AFFECTS_MULTIPLE_DSAS;
71                         goto txnReturn;
72                 }
73
74                 /* insert operation into transaction */
75
76                 rs->sr_text = "transaction specified";
77                 rs->sr_err = LDAP_X_TXN_SPECIFY_OKAY;
78
79 txnReturn:
80                 /* release connection lock */
81                 ldap_pvt_thread_mutex_unlock( &op->o_conn->c_mutex );
82
83                 if( !settle ) {
84                         send_ldap_result( op, rs );
85                         return rs->sr_err;
86                 }
87         }
88 #endif
89
90         ctrls[num_ctrls] = 0;
91
92         /* check entry's schema */
93         rs->sr_err = entry_schema_check( op, op->ora_e, NULL,
94                 get_relax(op), 1, NULL, &rs->sr_text, textbuf, textlen );
95         if ( rs->sr_err != LDAP_SUCCESS ) {
96                 Debug( LDAP_DEBUG_TRACE,
97                         LDAP_XSTRING(mdb_add) ": entry failed schema check: "
98                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
99                 goto return_results;
100         }
101
102         /* add opattrs to shadow as well, only missing attrs will actually
103          * be added; helps compatibility with older OL versions */
104         rs->sr_err = slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
105         if ( rs->sr_err != LDAP_SUCCESS ) {
106                 Debug( LDAP_DEBUG_TRACE,
107                         LDAP_XSTRING(mdb_add) ": entry failed op attrs add: "
108                         "%s (%d)\n", rs->sr_text, rs->sr_err, 0 );
109                 goto return_results;
110         }
111
112         if ( get_assert( op ) &&
113                 ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
114         {
115                 rs->sr_err = LDAP_ASSERTION_FAILED;
116                 goto return_results;
117         }
118
119         subentry = is_entry_subentry( op->ora_e );
120
121         /* begin transaction */
122         rs->sr_err = mdb_opinfo_get( op, mdb, 0, &moi );
123         rs->sr_text = NULL;
124         if( rs->sr_err != 0 ) {
125                 Debug( LDAP_DEBUG_TRACE,
126                         LDAP_XSTRING(mdb_add) ": txn_begin failed: %s (%d)\n",
127                         mdb_strerror(rs->sr_err), rs->sr_err, 0 );
128                 rs->sr_err = LDAP_OTHER;
129                 rs->sr_text = "internal error";
130                 goto return_results;
131         }
132
133         txn = moi->moi_txn;
134
135         /*
136          * Get the parent dn and see if the corresponding entry exists.
137          */
138         if ( be_issuffix( op->o_bd, &op->ora_e->e_nname ) ) {
139                 pdn = slap_empty_bv;
140         } else {
141                 dnParent( &op->ora_e->e_nname, &pdn );
142         }
143
144         /* get entry or parent */
145         rs->sr_err = mdb_dn2entry( op, txn, &op->ora_e->e_nname, &p, 1 );
146         switch( rs->sr_err ) {
147         case 0:
148                 rs->sr_err = LDAP_ALREADY_EXISTS;
149                 mdb_entry_return( p );
150                 p = NULL;
151                 goto return_results;
152         case MDB_NOTFOUND:
153                 break;
154         case LDAP_BUSY:
155                 rs->sr_text = "ldap server busy";
156                 goto return_results;
157         default:
158                 rs->sr_err = LDAP_OTHER;
159                 rs->sr_text = "internal error";
160                 goto return_results;
161         }
162
163         if ( !p )
164                 p = (Entry *)&slap_entry_root;
165
166         if ( !bvmatch( &pdn, &p->e_nname ) ) {
167                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
168                         op->o_tmpmemctx );
169                 if ( p != (Entry *)&slap_entry_root && is_entry_referral( p )) {
170                         BerVarray ref = get_entry_referrals( op, p );
171                         rs->sr_ref = referral_rewrite( ref, &p->e_name,
172                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
173                         ber_bvarray_free( ref );
174                 } else {
175                         rs->sr_ref = NULL;
176                 }
177                 if ( p != (Entry *)&slap_entry_root )
178                         mdb_entry_return( p );
179                 p = NULL;
180                 Debug( LDAP_DEBUG_TRACE,
181                         LDAP_XSTRING(mdb_add) ": parent "
182                         "does not exist\n", 0, 0, 0 );
183
184                 rs->sr_err = LDAP_REFERRAL;
185                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
186                 goto return_results;
187         }
188
189         rs->sr_err = access_allowed( op, p,
190                 children, NULL, ACL_WADD, NULL );
191
192         if ( ! rs->sr_err ) {
193                 if ( p != (Entry *)&slap_entry_root )
194                         mdb_entry_return( p );
195                 p = NULL;
196
197                 Debug( LDAP_DEBUG_TRACE,
198                         LDAP_XSTRING(mdb_add) ": no write access to parent\n",
199                         0, 0, 0 );
200                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
201                 rs->sr_text = "no write access to parent";
202                 goto return_results;;
203         }
204
205         if ( p != (Entry *)&slap_entry_root ) {
206                 if ( is_entry_subentry( p ) ) {
207                         mdb_entry_return( p );
208                         p = NULL;
209                         /* parent is a subentry, don't allow add */
210                         Debug( LDAP_DEBUG_TRACE,
211                                 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
212                                 0, 0, 0 );
213                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
214                         rs->sr_text = "parent is a subentry";
215                         goto return_results;;
216                 }
217
218                 if ( is_entry_alias( p ) ) {
219                         mdb_entry_return( p );
220                         p = NULL;
221                         /* parent is an alias, don't allow add */
222                         Debug( LDAP_DEBUG_TRACE,
223                                 LDAP_XSTRING(mdb_add) ": parent is alias\n",
224                                 0, 0, 0 );
225                         rs->sr_err = LDAP_ALIAS_PROBLEM;
226                         rs->sr_text = "parent is an alias";
227                         goto return_results;;
228                 }
229
230                 if ( is_entry_referral( p ) ) {
231                         BerVarray ref = get_entry_referrals( op, p );
232                         /* parent is a referral, don't allow add */
233                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
234                                 op->o_tmpmemctx );
235                         rs->sr_ref = referral_rewrite( ref, &p->e_name,
236                                 &op->o_req_dn, LDAP_SCOPE_DEFAULT );
237                         ber_bvarray_free( ref );
238                         mdb_entry_return( p );
239                         p = NULL;
240                         Debug( LDAP_DEBUG_TRACE,
241                                 LDAP_XSTRING(mdb_add) ": parent is referral\n",
242                                 0, 0, 0 );
243
244                         rs->sr_err = LDAP_REFERRAL;
245                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
246                         goto return_results;
247                 }
248
249         }
250
251         if ( subentry ) {
252                 /* FIXME: */
253                 /* parent must be an administrative point of the required kind */
254         }
255
256         /* free parent */
257         if ( p != (Entry *)&slap_entry_root ) {
258                 pid = p->e_id;
259                 if ( p->e_nname.bv_len ) {
260                         struct berval ppdn;
261
262                         /* ITS#5326: use parent's DN if differs from provided one */
263                         dnParent( &op->ora_e->e_name, &ppdn );
264                         if ( !dn_match( &p->e_name, &ppdn ) ) {
265                                 struct berval rdn;
266                                 struct berval newdn;
267
268                                 dnRdn( &op->ora_e->e_name, &rdn );
269
270                                 build_new_dn( &newdn, &p->e_name, &rdn, NULL ); 
271                                 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
272                                         ber_memfree( op->ora_e->e_name.bv_val );
273                                 op->ora_e->e_name = newdn;
274
275                                 /* FIXME: should check whether
276                                  * dnNormalize(newdn) == e->e_nname ... */
277                         }
278                 }
279
280                 mdb_entry_return( p );
281         }
282         p = NULL;
283
284         rs->sr_err = access_allowed( op, op->ora_e,
285                 entry, NULL, ACL_WADD, NULL );
286
287         if ( ! rs->sr_err ) {
288                 Debug( LDAP_DEBUG_TRACE,
289                         LDAP_XSTRING(mdb_add) ": no write access to entry\n",
290                         0, 0, 0 );
291                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
292                 rs->sr_text = "no write access to entry";
293                 goto return_results;;
294         }
295
296         /* 
297          * Check ACL for attribute write access
298          */
299         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
300                 Debug( LDAP_DEBUG_TRACE,
301                         LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
302                         0, 0, 0 );
303                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
304                 rs->sr_text = "no write access to attribute";
305                 goto return_results;;
306         }
307
308         if ( eid == NOID ) {
309                 rs->sr_err = mdb_next_id( op->o_bd, txn, &eid );
310                 if( rs->sr_err != 0 ) {
311                         Debug( LDAP_DEBUG_TRACE,
312                                 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
313                                 rs->sr_err, 0, 0 );
314                         rs->sr_err = LDAP_OTHER;
315                         rs->sr_text = "internal error";
316                         goto return_results;
317                 }
318                 op->ora_e->e_id = eid;
319         }
320
321         /* dn2id index */
322         rs->sr_err = mdb_dn2id_add( op, txn, pid, op->ora_e );
323         if ( rs->sr_err != 0 ) {
324                 Debug( LDAP_DEBUG_TRACE,
325                         LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
326                         mdb_strerror(rs->sr_err), rs->sr_err, 0 );
327
328                 switch( rs->sr_err ) {
329                 case MDB_KEYEXIST:
330                         rs->sr_err = LDAP_ALREADY_EXISTS;
331                         break;
332                 default:
333                         rs->sr_err = LDAP_OTHER;
334                 }
335                 goto return_results;
336         }
337
338         /* attribute indexes */
339         rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
340         if ( rs->sr_err != LDAP_SUCCESS ) {
341                 Debug( LDAP_DEBUG_TRACE,
342                         LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
343                         0, 0, 0 );
344                 rs->sr_err = LDAP_OTHER;
345                 rs->sr_text = "index generation failed";
346                 goto return_results;
347         }
348
349         /* id2entry index */
350         rs->sr_err = mdb_id2entry_add( op, txn, op->ora_e );
351         if ( rs->sr_err != 0 ) {
352                 Debug( LDAP_DEBUG_TRACE,
353                         LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
354                         0, 0, 0 );
355                 rs->sr_err = LDAP_OTHER;
356                 rs->sr_text = "entry store failed";
357                 goto return_results;
358         }
359
360         /* post-read */
361         if( op->o_postread ) {
362                 if( postread_ctrl == NULL ) {
363                         postread_ctrl = &ctrls[num_ctrls++];
364                         ctrls[num_ctrls] = NULL;
365                 }
366                 if ( slap_read_controls( op, rs, op->ora_e,
367                         &slap_post_read_bv, postread_ctrl ) )
368                 {
369                         Debug( LDAP_DEBUG_TRACE,
370                                 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
371                                 "failed!\n", 0, 0, 0 );
372                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
373                                 /* FIXME: is it correct to abort
374                                  * operation if control fails? */
375                                 goto return_results;
376                         }
377                 }
378         }
379
380         if ( moi == &opinfo ) {
381                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
382                 opinfo.moi_oe.oe_key = NULL;
383                 if ( op->o_noop ) {
384                         mdb_txn_abort( txn );
385                         rs->sr_err = LDAP_X_NO_OPERATION;
386                         txn = NULL;
387                         goto return_results;
388                 }
389
390                 if (( rs->sr_err = mdb_txn_commit( txn )) != 0 ) {
391                         rs->sr_text = "txn_commit failed";
392                         Debug( LDAP_DEBUG_TRACE,
393                                 LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
394                                 rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
395                         rs->sr_err = LDAP_OTHER;
396                         goto return_results;
397                 }
398                 txn = NULL;
399         }
400
401         Debug(LDAP_DEBUG_TRACE,
402                 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
403                 op->o_noop ? " (no-op)" : "",
404                 op->ora_e->e_id, op->ora_e->e_dn );
405
406         rs->sr_text = NULL;
407         if( num_ctrls ) rs->sr_ctrls = ctrls;
408
409 return_results:
410         success = rs->sr_err;
411         send_ldap_result( op, rs );
412
413         if( moi == &opinfo ) {
414                 if( txn != NULL ) {
415                         mdb_txn_abort( txn );
416                 }
417                 if ( opinfo.moi_oe.oe_key ) {
418                         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
419                 }
420         }
421
422         if( success == LDAP_SUCCESS ) {
423 #if 0
424                 if ( mdb->bi_txn_cp_kbyte ) {
425                         TXN_CHECKPOINT( mdb->bi_dbenv,
426                                 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
427                 }
428 #endif
429         }
430
431         slap_graduate_commit_csn( op );
432
433         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
434                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
435                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
436         }
437         return rs->sr_err;
438 }