]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
Reset Entry->e_ocflags to 0 if objectClass attr is modified.
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2002 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14 #include "external.h"
15
16 int
17 bdb_add(
18         BackendDB       *be,
19         Connection      *conn,
20         Operation       *op,
21         Entry   *e )
22 {
23         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
24         struct berval   pdn;
25         Entry           *p = NULL;
26         int                     rc; 
27         const char      *text;
28         char textbuf[SLAP_TEXT_BUFLEN];
29         size_t textlen = sizeof textbuf;
30         AttributeDescription *children = slap_schema.si_ad_children;
31         DB_TXN          *ltid = NULL;
32         struct bdb_op_info opinfo;
33         int subentry;
34 #if 0
35         u_int32_t       lockid;
36         DB_LOCK         lock;
37 #endif
38
39         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
40
41         /* check entry's schema */
42         rc = entry_schema_check( be, e, NULL, &text, textbuf, textlen );
43         if ( rc != LDAP_SUCCESS ) {
44                 Debug( LDAP_DEBUG_TRACE,
45                         "bdb_add: entry failed schema check: %s (%d)\n",
46                         text, rc, 0 );
47                 goto return_results;
48         }
49
50         subentry = is_entry_subentry( e );
51
52         /*
53          * acquire an ID outside of the operation transaction
54          * to avoid serializing adds.
55          */
56         rc = bdb_next_id( be, NULL, &e->e_id );
57         if( rc != 0 ) {
58                 Debug( LDAP_DEBUG_TRACE,
59                         "bdb_add: next_id failed (%d)\n",
60                         rc, 0, 0 );
61                 rc = LDAP_OTHER;
62                 text = "internal error";
63                 goto return_results;
64         }
65
66         if( 0 ) {
67                 /* transaction retry */
68 retry:  rc = txn_abort( ltid );
69                 ltid = NULL;
70                 op->o_private = NULL;
71                 if( rc != 0 ) {
72                         rc = LDAP_OTHER;
73                         text = "internal error";
74                         goto return_results;
75                 }
76                 ldap_pvt_thread_yield();
77         }
78
79         /* begin transaction */
80         if( bdb->bi_txn ) {
81                 rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 
82                         bdb->bi_db_opflags );
83                 text = NULL;
84                 if( rc != 0 ) {
85                         Debug( LDAP_DEBUG_TRACE,
86                                 "bdb_add: txn_begin failed: %s (%d)\n",
87                                 db_strerror(rc), rc, 0 );
88                         rc = LDAP_OTHER;
89                         text = "internal error";
90                         goto return_results;
91                 }
92 #if 0
93                 lockid = TXN_ID( ltid );
94 #endif
95         }
96
97         opinfo.boi_bdb = be;
98         opinfo.boi_txn = ltid;
99         opinfo.boi_err = 0;
100         op->o_private = &opinfo;
101         
102         /*
103          * Get the parent dn and see if the corresponding entry exists.
104          * If the parent does not exist, only allow the "root" user to
105          * add the entry.
106          */
107         if ( be_issuffix( be, e->e_nname.bv_val ) ) {
108                 pdn.bv_len = 0;
109                 pdn.bv_val = "";
110         } else {
111                 rc = dnParent( e->e_nname.bv_val, (const char **)&pdn.bv_val );
112                 if ( rc != LDAP_SUCCESS ) {
113                         text = "internal error";
114                         goto return_results;
115                 }
116                 pdn.bv_len = e->e_nname.bv_len - (pdn.bv_val - e->e_nname.bv_val);
117         }
118
119         if( pdn.bv_len != 0 ) {
120                 Entry *matched = NULL;
121
122 #if 0
123                 if ( ltid ) {
124                         DBT obj;
125                         obj.data = pdn.bv_val-1;
126                         obj.size = pdn.bv_len+1;
127                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
128                                 DB_LOCK_WRITE, &lock);
129                 }
130 #endif
131
132                 /* get parent */
133                 rc = bdb_dn2entry( be, ltid, &pdn, &p, &matched, 0 );
134
135                 switch( rc ) {
136                 case 0:
137                 case DB_NOTFOUND:
138                         break;
139                 case DB_LOCK_DEADLOCK:
140                 case DB_LOCK_NOTGRANTED:
141                         goto retry;
142                 default:
143                         rc = LDAP_OTHER;
144                         text = "internal error";
145                         goto return_results;
146                 }
147
148                 if ( p == NULL ) {
149                         char *matched_dn = NULL;
150                         BerVarray refs;
151
152                         if ( matched != NULL ) {
153                                 matched_dn = ch_strdup( matched->e_dn );
154                                 refs = is_entry_referral( matched )
155                                         ? get_entry_referrals( be, conn, op, matched )
156                                         : NULL;
157                                 bdb_entry_return( be, matched );
158                                 matched = NULL;
159
160                         } else {
161                                 refs = referral_rewrite( default_referral,
162                                         NULL, &e->e_name, LDAP_SCOPE_DEFAULT );
163                         }
164
165                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
166                                 0, 0, 0 );
167
168                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
169                                 matched_dn, NULL, refs, NULL );
170
171                         ber_bvarray_free( refs );
172                         ch_free( matched_dn );
173
174                         goto done;
175                 }
176
177                 if ( ! access_allowed( be, conn, op, p,
178                         children, NULL, ACL_WRITE ) )
179                 {
180                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
181                                 0, 0, 0 );
182                         rc = LDAP_INSUFFICIENT_ACCESS;
183                         text = "no write access to parent";
184                         goto return_results;;
185                 }
186
187                 if ( is_entry_subentry( p ) ) {
188                         /* parent is a subentry, don't allow add */
189                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
190                                 0, 0, 0 );
191                         rc = LDAP_OBJECT_CLASS_VIOLATION;
192                         text = "parent is a subentry";
193                         goto return_results;;
194                 }
195
196                 if ( is_entry_alias( p ) ) {
197                         /* parent is an alias, don't allow add */
198                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
199                                 0, 0, 0 );
200                         rc = LDAP_ALIAS_PROBLEM;
201                         text = "parent is an alias";
202                         goto return_results;;
203                 }
204
205                 if ( is_entry_referral( p ) ) {
206                         /* parent is a referral, don't allow add */
207                         char *matched_dn = ch_strdup( p->e_dn );
208                         BerVarray refs = is_entry_referral( p )
209                                 ? get_entry_referrals( be, conn, op, p )
210                                 : NULL;
211
212                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
213                                 0, 0, 0 );
214
215                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
216                                 matched_dn, NULL, refs, NULL );
217
218                         ber_bvarray_free( refs );
219                         free( matched_dn );
220                         goto done;
221                 }
222
223                 if ( subentry ) {
224                         /* FIXME: */
225                         /* parent must be an administrative point of the required kind */
226                 }
227
228                 /* free parent and writer lock */
229                 bdb_entry_return( be, p );
230                 p = NULL;
231
232         } else {
233                 /*
234                  * no parent!
235                  *      must be adding entry at suffix or with parent ""
236                  */
237                 if ( !be_isroot( be, &op->o_ndn )) {
238                         if ( be_issuffix( be, "" ) || be_isupdate( be, &op->o_ndn ) ) {
239                                 p = (Entry *)&slap_entry_root;
240
241                                 /* check parent for "children" acl */
242                                 rc = access_allowed( be, conn, op, p,
243                                         children, NULL, ACL_WRITE );
244                                 p = NULL;
245
246                                 if ( ! rc ) {
247                                         Debug( LDAP_DEBUG_TRACE,
248                                                 "bdb_add: no write access to parent\n",
249                                                 0, 0, 0 );
250                                         rc = LDAP_INSUFFICIENT_ACCESS;
251                                         text = "no write access to parent";
252                                         goto return_results;;
253                                 }
254
255                         } else {
256                                 Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
257                                         pdn.bv_len == 0 ? "suffix" : "entry at root",
258                                         0, 0 );
259                                 rc = LDAP_INSUFFICIENT_ACCESS;
260                                 goto return_results;
261                         }
262                 }
263
264                 if( subentry ) {
265                         Debug( LDAP_DEBUG_TRACE,
266                                 "bdb_add: no parent, cannot add subentry\n",
267                                 0, 0, 0 );
268                         rc = LDAP_INSUFFICIENT_ACCESS;
269                         text = "no parent, cannot add subentry";
270                         goto return_results;;
271                 }
272 #if 0
273                 if ( ltid ) {
274                         DBT obj;
275                         obj.data = ",";
276                         obj.size = 1;
277                         rc = LOCK_GET( bdb->bi_dbenv, lockid, 0, &obj,
278                                 DB_LOCK_WRITE, &lock);
279                 }
280 #endif
281         }
282
283         /* dn2id index */
284         rc = bdb_dn2id_add( be, ltid, &pdn, e );
285         if ( rc != 0 ) {
286                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
287                         db_strerror(rc), rc, 0 );
288
289                 switch( rc ) {
290                 case DB_LOCK_DEADLOCK:
291                 case DB_LOCK_NOTGRANTED:
292                         goto retry;
293                 case DB_KEYEXIST:
294                         rc = LDAP_ALREADY_EXISTS;
295                         break;
296                 default:
297                         rc = LDAP_OTHER;
298                 }
299                 goto return_results;
300         }
301
302         /* id2entry index */
303         rc = bdb_id2entry_add( be, ltid, e );
304         if ( rc != 0 ) {
305                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
306                         0, 0, 0 );
307                 switch( rc ) {
308                 case DB_LOCK_DEADLOCK:
309                 case DB_LOCK_NOTGRANTED:
310                         goto retry;
311                 default:
312                         rc = LDAP_OTHER;
313                 }
314                 text = "entry store failed";
315                 goto return_results;
316         }
317
318         /* attribute indexes */
319         rc = bdb_index_entry_add( be, ltid, e, e->e_attrs );
320         if ( rc != LDAP_SUCCESS ) {
321                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
322                         0, 0, 0 );
323                 switch( rc ) {
324                 case DB_LOCK_DEADLOCK:
325                 case DB_LOCK_NOTGRANTED:
326                         goto retry;
327                 default:
328                         rc = LDAP_OTHER;
329                 }
330                 text = "index generation failed";
331                 goto return_results;
332         }
333
334         if( bdb->bi_txn ) {
335                 rc = txn_commit( ltid, 0 );
336         }
337         ltid = NULL;
338         op->o_private = NULL;
339
340         if( rc != 0 ) {
341                 Debug( LDAP_DEBUG_TRACE,
342                         "bdb_add: txn_commit failed: %s (%d)\n",
343                         db_strerror(rc), rc, 0 );
344                 rc = LDAP_OTHER;
345                 text = "commit failed";
346
347         } else {
348                 Debug( LDAP_DEBUG_TRACE,
349                         "bdb_add: added id=%08lx dn=\"%s\"\n",
350                         e->e_id, e->e_dn, 0 );
351                 rc = LDAP_SUCCESS;
352                 text = NULL;
353         }
354
355 return_results:
356         send_ldap_result( conn, op, rc,
357                 NULL, text, NULL, NULL );
358
359         if( rc == LDAP_SUCCESS && bdb->bi_txn_cp ) {
360                 ldap_pvt_thread_yield();
361                 TXN_CHECKPOINT( bdb->bi_dbenv,
362                         bdb->bi_txn_cp_kbyte, bdb->bi_txn_cp_min, 0 );
363         }
364
365 done:
366         if (p != NULL) {
367                 /* free parent and writer lock */
368                 bdb_entry_return( be, p ); 
369         }
370
371         if( ltid != NULL ) {
372                 txn_abort( ltid );
373                 op->o_private = NULL;
374         }
375
376         return rc;
377 }