]> git.sur5r.net Git - openldap/blob - servers/slapd/back-mdb/add.c
More porting
[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         struct mdb_op_info opinfo = {{{ 0 }}};
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_txn_begin( mdb->mi_dbenv, 0, &txn );
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         opinfo.moi_oe.oe_key = mdb;
134         opinfo.moi_txn = txn;
135         LDAP_SLIST_INSERT_HEAD( &op->o_extra, &opinfo.moi_oe, oe_next );
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         /* get entry or parent */
147         rs->sr_err = mdb_dn2entry( op, txn, &op->ora_e->e_nname, &p, 1 );
148         switch( rs->sr_err ) {
149         case 0:
150                 rs->sr_err = LDAP_ALREADY_EXISTS;
151                 mdb_entry_return( p );
152                 p = NULL;
153                 goto return_results;
154         case MDB_NOTFOUND:
155                 break;
156         case LDAP_BUSY:
157                 rs->sr_text = "ldap server busy";
158                 goto return_results;
159         default:
160                 rs->sr_err = LDAP_OTHER;
161                 rs->sr_text = "internal error";
162                 goto return_results;
163         }
164
165         if ( !p )
166                 p = (Entry *)&slap_entry_root;
167
168         if ( !bvmatch( &pdn, &p->e_nname ) ) {
169                 rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
170                         op->o_tmpmemctx );
171                 rs->sr_ref = is_entry_referral( p )
172                         ? get_entry_referrals( op, p )
173                         : NULL;
174                 if ( p != (Entry *)&slap_entry_root )
175                         mdb_entry_return( p );
176                 p = NULL;
177                 Debug( LDAP_DEBUG_TRACE,
178                         LDAP_XSTRING(mdb_add) ": parent "
179                         "does not exist\n", 0, 0, 0 );
180
181                 rs->sr_err = LDAP_REFERRAL;
182                 rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
183                 goto return_results;
184         }
185
186         rs->sr_err = access_allowed( op, p,
187                 children, NULL, ACL_WADD, NULL );
188
189         if ( ! rs->sr_err ) {
190                 if ( p != (Entry *)&slap_entry_root )
191                         mdb_entry_return( p );
192                 p = NULL;
193
194                 Debug( LDAP_DEBUG_TRACE,
195                         LDAP_XSTRING(mdb_add) ": no write access to parent\n",
196                         0, 0, 0 );
197                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
198                 rs->sr_text = "no write access to parent";
199                 goto return_results;;
200         }
201
202         if ( p != (Entry *)&slap_entry_root ) {
203                 if ( is_entry_subentry( p ) ) {
204                         mdb_entry_return( p );
205                         p = NULL;
206                         /* parent is a subentry, don't allow add */
207                         Debug( LDAP_DEBUG_TRACE,
208                                 LDAP_XSTRING(mdb_add) ": parent is subentry\n",
209                                 0, 0, 0 );
210                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
211                         rs->sr_text = "parent is a subentry";
212                         goto return_results;;
213                 }
214
215                 if ( is_entry_alias( p ) ) {
216                         mdb_entry_return( p );
217                         p = NULL;
218                         /* parent is an alias, don't allow add */
219                         Debug( LDAP_DEBUG_TRACE,
220                                 LDAP_XSTRING(mdb_add) ": parent is alias\n",
221                                 0, 0, 0 );
222                         rs->sr_err = LDAP_ALIAS_PROBLEM;
223                         rs->sr_text = "parent is an alias";
224                         goto return_results;;
225                 }
226
227                 if ( is_entry_referral( p ) ) {
228                         /* parent is a referral, don't allow add */
229                         rs->sr_matched = ber_strdup_x( p->e_name.bv_val,
230                                 op->o_tmpmemctx );
231                         rs->sr_ref = get_entry_referrals( op, p );
232                         mdb_entry_return( p );
233                         p = NULL;
234                         Debug( LDAP_DEBUG_TRACE,
235                                 LDAP_XSTRING(mdb_add) ": parent is referral\n",
236                                 0, 0, 0 );
237
238                         rs->sr_err = LDAP_REFERRAL;
239                         rs->sr_flags = REP_MATCHED_MUSTBEFREED | REP_REF_MUSTBEFREED;
240                         goto return_results;
241                 }
242
243         }
244
245         if ( subentry ) {
246                 /* FIXME: */
247                 /* parent must be an administrative point of the required kind */
248         }
249
250         /* free parent and reader lock */
251         if ( p != (Entry *)&slap_entry_root ) {
252                 pid = p->e_id;
253                 if ( p->e_nname.bv_len ) {
254                         struct berval ppdn;
255
256                         /* ITS#5326: use parent's DN if differs from provided one */
257                         dnParent( &op->ora_e->e_name, &ppdn );
258                         if ( !dn_match( &p->e_name, &ppdn ) ) {
259                                 struct berval rdn;
260                                 struct berval newdn;
261
262                                 dnRdn( &op->ora_e->e_name, &rdn );
263
264                                 build_new_dn( &newdn, &p->e_name, &rdn, NULL ); 
265                                 if ( op->ora_e->e_name.bv_val != op->o_req_dn.bv_val )
266                                         ber_memfree( op->ora_e->e_name.bv_val );
267                                 op->ora_e->e_name = newdn;
268
269                                 /* FIXME: should check whether
270                                  * dnNormalize(newdn) == e->e_nname ... */
271                         }
272                 }
273
274                 mdb_entry_return( p );
275         }
276         p = NULL;
277
278         rs->sr_err = access_allowed( op, op->ora_e,
279                 entry, NULL, ACL_WADD, NULL );
280
281         if ( ! rs->sr_err ) {
282                 Debug( LDAP_DEBUG_TRACE,
283                         LDAP_XSTRING(mdb_add) ": no write access to entry\n",
284                         0, 0, 0 );
285                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
286                 rs->sr_text = "no write access to entry";
287                 goto return_results;;
288         }
289
290         /* 
291          * Check ACL for attribute write access
292          */
293         if (!acl_check_modlist(op, oe, op->ora_modlist)) {
294                 Debug( LDAP_DEBUG_TRACE,
295                         LDAP_XSTRING(mdb_add) ": no write access to attribute\n",
296                         0, 0, 0 );
297                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
298                 rs->sr_text = "no write access to attribute";
299                 goto return_results;;
300         }
301
302         if ( eid == NOID ) {
303                 rs->sr_err = mdb_next_id( op->o_bd, txn, &eid );
304                 if( rs->sr_err != 0 ) {
305                         Debug( LDAP_DEBUG_TRACE,
306                                 LDAP_XSTRING(mdb_add) ": next_id failed (%d)\n",
307                                 rs->sr_err, 0, 0 );
308                         rs->sr_err = LDAP_OTHER;
309                         rs->sr_text = "internal error";
310                         goto return_results;
311                 }
312                 op->ora_e->e_id = eid;
313         }
314
315         /* dn2id index */
316         rs->sr_err = mdb_dn2id_add( op, txn, pid, op->ora_e );
317         if ( rs->sr_err != 0 ) {
318                 Debug( LDAP_DEBUG_TRACE,
319                         LDAP_XSTRING(mdb_add) ": dn2id_add failed: %s (%d)\n",
320                         mdb_strerror(rs->sr_err), rs->sr_err, 0 );
321
322                 switch( rs->sr_err ) {
323                 case MDB_KEYEXIST:
324                         rs->sr_err = LDAP_ALREADY_EXISTS;
325                         break;
326                 default:
327                         rs->sr_err = LDAP_OTHER;
328                 }
329                 goto return_results;
330         }
331
332         /* attribute indexes */
333         rs->sr_err = mdb_index_entry_add( op, txn, op->ora_e );
334         if ( rs->sr_err != LDAP_SUCCESS ) {
335                 Debug( LDAP_DEBUG_TRACE,
336                         LDAP_XSTRING(mdb_add) ": index_entry_add failed\n",
337                         0, 0, 0 );
338                 rs->sr_err = LDAP_OTHER;
339                 rs->sr_text = "index generation failed";
340                 goto return_results;
341         }
342
343         /* id2entry index */
344         rs->sr_err = mdb_id2entry_add( op, txn, op->ora_e );
345         if ( rs->sr_err != 0 ) {
346                 Debug( LDAP_DEBUG_TRACE,
347                         LDAP_XSTRING(mdb_add) ": id2entry_add failed\n",
348                         0, 0, 0 );
349                 rs->sr_err = LDAP_OTHER;
350                 rs->sr_text = "entry store failed";
351                 goto return_results;
352         }
353
354         /* post-read */
355         if( op->o_postread ) {
356                 if( postread_ctrl == NULL ) {
357                         postread_ctrl = &ctrls[num_ctrls++];
358                         ctrls[num_ctrls] = NULL;
359                 }
360                 if ( slap_read_controls( op, rs, op->ora_e,
361                         &slap_post_read_bv, postread_ctrl ) )
362                 {
363                         Debug( LDAP_DEBUG_TRACE,
364                                 "<=- " LDAP_XSTRING(mdb_add) ": post-read "
365                                 "failed!\n", 0, 0, 0 );
366                         if ( op->o_postread & SLAP_CONTROL_CRITICAL ) {
367                                 /* FIXME: is it correct to abort
368                                  * operation if control fails? */
369                                 goto return_results;
370                         }
371                 }
372         }
373
374         if ( op->o_noop ) {
375                 mdb_txn_abort( txn );
376                 rs->sr_err = LDAP_X_NO_OPERATION;
377                 txn = NULL;
378                 goto return_results;
379         }
380
381         if (( rs->sr_err = mdb_txn_commit( txn )) != 0 ) {
382                 rs->sr_text = "txn_commit failed";
383                 Debug( LDAP_DEBUG_TRACE,
384                         LDAP_XSTRING(mdb_add) ": %s : %s (%d)\n",
385                         rs->sr_text, mdb_strerror(rs->sr_err), rs->sr_err );
386                 rs->sr_err = LDAP_OTHER;
387                 goto return_results;
388         }
389         txn = NULL;
390
391         LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
392         opinfo.moi_oe.oe_key = NULL;
393
394         Debug(LDAP_DEBUG_TRACE,
395                 LDAP_XSTRING(mdb_add) ": added%s id=%08lx dn=\"%s\"\n",
396                 op->o_noop ? " (no-op)" : "",
397                 op->ora_e->e_id, op->ora_e->e_dn );
398
399         rs->sr_text = NULL;
400         if( num_ctrls ) rs->sr_ctrls = ctrls;
401
402 return_results:
403         success = rs->sr_err;
404         send_ldap_result( op, rs );
405
406         if( txn != NULL ) {
407                 mdb_txn_abort( txn );
408         }
409         if ( opinfo.moi_oe.oe_key ) {
410                 LDAP_SLIST_REMOVE( &op->o_extra, &opinfo.moi_oe, OpExtra, oe_next );
411         }
412
413         if( success == LDAP_SUCCESS ) {
414 #if 0
415                 if ( mdb->bi_txn_cp_kbyte ) {
416                         TXN_CHECKPOINT( mdb->bi_dbenv,
417                                 mdb->bi_txn_cp_kbyte, mdb->bi_txn_cp_min, 0 );
418                 }
419 #endif
420         }
421
422         slap_graduate_commit_csn( op );
423
424         if( postread_ctrl != NULL && (*postread_ctrl) != NULL ) {
425                 slap_sl_free( (*postread_ctrl)->ldctl_value.bv_val, op->o_tmpmemctx );
426                 slap_sl_free( *postread_ctrl, op->o_tmpmemctx );
427         }
428         return rs->sr_err;
429 }