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