]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
unifdef LDAP_SYNC and LDAP_SYNCREPL
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2003 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
12 #include <ac/socket.h>
13 #include <ac/string.h>
14
15 #include "slap.h"
16 #include "back-ldbm.h"
17 #include "proto-back-ldbm.h"
18
19 int
20 ldbm_back_add(
21     Operation   *op,
22     SlapReply   *rs )
23 {
24         struct ldbminfo *li = (struct ldbminfo *) op->o_bd->be_private;
25         struct berval   pdn;
26         Entry           *p = NULL;
27         ID               id = NOID;
28         AttributeDescription *children = slap_schema.si_ad_children;
29         AttributeDescription *entry = slap_schema.si_ad_entry;
30         char textbuf[SLAP_TEXT_BUFLEN];
31         size_t textlen = sizeof textbuf;
32 #ifdef LDBM_SUBENTRIES
33         int subentry;
34 #endif
35
36 #ifdef NEW_LOGGING
37         LDAP_LOG( BACK_LDBM, ENTRY, "ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0 );
38 #else
39         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", op->o_req_dn.bv_val, 0, 0);
40 #endif
41         
42         rs->sr_err = entry_schema_check( op->o_bd, op->oq_add.rs_e, NULL, &rs->sr_text, textbuf, textlen );
43
44         if ( rs->sr_err != LDAP_SUCCESS ) {
45 #ifdef NEW_LOGGING
46                 LDAP_LOG( BACK_LDBM, ERR, 
47                         "ldbm_back_add: entry (%s) failed schema check.\n", op->o_req_dn.bv_val, 0, 0 );
48 #else
49                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check: %s\n",
50                         rs->sr_text, 0, 0 );
51 #endif
52
53                 send_ldap_result( op, rs );
54                 return rs->sr_err;
55         }
56
57 #ifdef LDBM_SUBENTRIES
58         subentry = is_entry_subentry( op->oq_add.rs_e );
59 #endif
60
61         if ( !access_allowed( op, op->oq_add.rs_e,
62                                 entry, NULL, ACL_WRITE, NULL ) )
63         {
64 #ifdef NEW_LOGGING
65                 LDAP_LOG( BACK_LDBM, ERR, 
66                         "ldbm_back_add: No write access to entry (%s).\n", 
67                         op->o_req_dn.bv_val, 0, 0 );
68 #else
69                 Debug( LDAP_DEBUG_TRACE, "no write access to entry\n", 0,
70                     0, 0 );
71 #endif
72
73                 send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
74                     "no write access to entry" );
75
76                 return LDAP_INSUFFICIENT_ACCESS;
77         }
78
79         /* grab giant lock for writing */
80         ldap_pvt_thread_rdwr_wlock(&li->li_giant_rwlock);
81
82         if ( ( rs->sr_err = dn2id( op->o_bd, &op->o_req_ndn, &id ) ) || id != NOID ) {
83                 /* if (rs->sr_err) something bad happened to ldbm cache */
84                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
85                 rs->sr_err = rs->sr_err ? LDAP_OTHER : LDAP_ALREADY_EXISTS;
86                 send_ldap_result( op, rs );
87                 return rs->sr_err;
88         }
89
90         /*
91          * Get the parent dn and see if the corresponding entry exists.
92          * If the parent does not exist, only allow the "root" user to
93          * add the entry.
94          */
95
96         if ( be_issuffix( op->o_bd, &op->o_req_ndn ) ) {
97                 pdn = slap_empty_bv;
98         } else {
99                 dnParent( &op->o_req_ndn, &pdn );
100         }
101
102         if( pdn.bv_len )
103         {
104                 Entry *matched = NULL;
105
106                 /* get parent with writer lock */
107                 if ( (p = dn2entry_w( op->o_bd, &pdn, &matched )) == NULL ) {
108                         if ( matched != NULL ) {
109                                 rs->sr_matched = ch_strdup( matched->e_dn );
110                                 rs->sr_ref = is_entry_referral( matched )
111                                         ? get_entry_referrals( op, matched )
112                                         : NULL;
113                                 cache_return_entry_r( &li->li_cache, matched );
114
115                         } else {
116                                 rs->sr_ref = referral_rewrite( default_referral,
117                                         NULL, &op->o_req_dn, LDAP_SCOPE_DEFAULT );
118                         }
119
120                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
121
122 #ifdef NEW_LOGGING
123                         LDAP_LOG( BACK_LDBM, ERR, 
124                                 "ldbm_back_add: Parent of (%s) does not exist.\n", 
125                                 op->o_req_dn.bv_val, 0, 0 );
126 #else
127                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n",
128                                 0, 0, 0 );
129 #endif
130
131                         rs->sr_text = rs->sr_ref ? "parent is referral" : "parent does not exist";
132                         rs->sr_err = LDAP_REFERRAL;
133                         send_ldap_result( op, rs );
134
135                         ber_bvarray_free( rs->sr_ref );
136                         free( (char *)rs->sr_matched );
137
138                         return rs->sr_err;
139                 }
140
141                 if ( ! access_allowed( op, p,
142                         children, NULL, ACL_WRITE, NULL ) )
143                 {
144                         /* free parent and writer lock */
145                         cache_return_entry_w( &li->li_cache, p ); 
146                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
147
148 #ifdef NEW_LOGGING
149                         LDAP_LOG( BACK_LDBM, ERR, 
150                                 "ldbm_back_add: No write access to parent (%s).\n", 
151                                 op->o_req_dn.bv_val, 0, 0 );
152 #else
153                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
154                             0, 0 );
155 #endif
156
157                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
158                             "no write access to parent" );
159
160                         return LDAP_INSUFFICIENT_ACCESS;
161                 }
162
163 #ifdef LDBM_SUBENTRIES
164                 if ( is_entry_subentry( p )) {
165 #ifdef NEW_LOGGING
166                         LDAP_LOG( OPERATION, DETAIL1,
167                                 "bdb_add: parent is subentry\n", 0, 0, 0 );
168 #else
169                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
170                                 0, 0, 0 );
171 #endif
172                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
173                         rs->sr_text = "parent is a subentry";
174                         goto return_results;
175                 }
176 #endif
177
178                 if ( is_entry_alias( p ) ) {
179                         /* parent is an alias, don't allow add */
180
181                         /* free parent and writer lock */
182                         cache_return_entry_w( &li->li_cache, p );
183                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
184
185 #ifdef NEW_LOGGING
186                         LDAP_LOG(BACK_LDBM, ERR, 
187                                 "ldbm_back_add:  Parent is an alias.\n", 0, 0, 0 );
188 #else
189                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
190                             0, 0 );
191 #endif
192
193
194                         send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
195                             "parent is an alias" );
196
197                         return LDAP_ALIAS_PROBLEM;
198                 }
199
200                 if ( is_entry_referral( p ) ) {
201                         /* parent is a referral, don't allow add */
202                         rs->sr_matched = ch_strdup( p->e_dn );
203                         rs->sr_ref = is_entry_referral( p )
204                                 ? get_entry_referrals( op, p )
205                                 : NULL;
206
207                         /* free parent and writer lock */
208                         cache_return_entry_w( &li->li_cache, p );
209                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
210
211 #ifdef NEW_LOGGING
212                         LDAP_LOG( BACK_LDBM, ERR,
213                                    "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
214 #else
215                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
216                             0, 0 );
217 #endif
218                         rs->sr_err = LDAP_REFERRAL;
219                         send_ldap_result( op, rs );
220
221                         ber_bvarray_free( rs->sr_ref );
222                         free( (char *)rs->sr_matched );
223                         return rs->sr_err;
224                 }
225
226 #ifdef LDBM_SUBENTRIES
227                 if ( subentry ) {
228                         /* FIXME: */
229                         /* parent must be an administrative point of the required kind */
230                 }
231 #endif
232
233         } else {
234                 if( pdn.bv_val != NULL )
235                 {
236                         assert( *pdn.bv_val == '\0' );
237                 }
238
239                 /* no parent, must be adding entry to root */
240                 if ( !be_isroot( op->o_bd, &op->o_ndn ) )
241                 {
242                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
243                                 p = (Entry *)&slap_entry_root;
244                                 
245                                 rs->sr_err = access_allowed( op, p,
246                                         children, NULL, ACL_WRITE, NULL );
247                                 p = NULL;
248                                 
249                                 if ( ! rs->sr_err ) {
250                                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
251
252 #ifdef NEW_LOGGING
253                                         LDAP_LOG( BACK_LDBM, ERR,
254                                                 "ldbm_back_add: No write "
255                                                 "access to parent (\"\").\n", 0, 0, 0 );
256 #else
257                                         Debug( LDAP_DEBUG_TRACE, 
258                                                 "no write access to parent\n", 
259                                                 0, 0, 0 );
260 #endif
261
262                                         send_ldap_error( op, rs,
263                                                 LDAP_INSUFFICIENT_ACCESS,
264                                                 "no write access to parent" );
265
266                                         return LDAP_INSUFFICIENT_ACCESS;
267                                 }
268                         } else if ( !is_entry_glue( op->oq_add.rs_e )) {
269                                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
270
271 #ifdef NEW_LOGGING
272                                 LDAP_LOG( BACK_LDBM, ERR,
273                                            "ldbm_back_add: %s add denied.\n",
274                                            pdn.bv_val == NULL ? "suffix" 
275                                            : "entry at root", 0, 0 );
276 #else
277                                 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
278                                                 pdn.bv_val == NULL ? "suffix" 
279                                                 : "entry at root", 0, 0 );
280 #endif
281
282                                 send_ldap_error( op, rs,
283                                                 LDAP_NO_SUCH_OBJECT, NULL );
284
285                                         return LDAP_NO_SUCH_OBJECT;
286                         }
287                 }
288
289 #ifdef LDBM_SUBENTRIES
290                         if( subentry ) {
291 #ifdef NEW_LOGGING
292                                         LDAP_LOG ( OPERATION, DETAIL1,
293                                                 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
294 #else
295                                         Debug( LDAP_DEBUG_TRACE,
296                                                 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
297 #endif
298                                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
299                                         rs->sr_text = "no parent, cannot add subentry";
300                                         goto return_results;
301                                 }
302 #endif
303
304         }
305
306         if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
307                 if( p != NULL) {
308                         /* free parent and writer lock */
309                         cache_return_entry_w( &li->li_cache, p ); 
310                 }
311
312                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
313
314 #ifdef NEW_LOGGING
315                 LDAP_LOG( BACK_LDBM, ERR,
316                         "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
317 #else
318                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
319                         0, 0, 0 );
320 #endif
321
322                 send_ldap_error( op, rs, LDAP_OTHER,
323                         "next_id add failed" );
324
325                 return LDAP_OTHER;
326         }
327
328         /*
329          * Try to add the entry to the cache, assign it a new dnid.
330          */
331         rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
332
333         if ( rs->sr_err != 0 ) {
334                 if( p != NULL) {
335                         /* free parent and writer lock */
336                         cache_return_entry_w( &li->li_cache, p ); 
337                 }
338
339                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
340
341 #ifdef NEW_LOGGING
342                 LDAP_LOG( BACK_LDBM, ERR,
343                         "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
344 #else
345                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
346                     0 );
347 #endif
348
349                 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
350                 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
351                 send_ldap_result( op, rs );
352
353                 return rs->sr_err;
354         }
355
356         rs->sr_err = -1;
357
358         /* attribute indexes */
359         if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
360 #ifdef NEW_LOGGING
361                 LDAP_LOG( BACK_LDBM, ERR,
362                         "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
363 #else
364                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
365                     0, 0 );
366 #endif
367                 
368                 send_ldap_error( op, rs, LDAP_OTHER,
369                         "index generation failed" );
370
371                 goto return_results;
372         }
373
374         /* dn2id index */
375         if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
376 #ifdef NEW_LOGGING
377                 LDAP_LOG( BACK_LDBM, ERR,
378                         "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
379 #else
380                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
381                     0, 0 );
382 #endif
383                 /* FIXME: delete attr indices? */
384
385                 send_ldap_error( op, rs, LDAP_OTHER,
386                         "DN index generation failed" );
387
388                 goto return_results;
389         }
390
391         /* id2entry index */
392         if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
393 #ifdef NEW_LOGGING
394                 LDAP_LOG( BACK_LDBM, ERR,
395                            "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
396 #else
397                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
398                     0, 0 );
399 #endif
400
401                 /* FIXME: delete attr indices? */
402                 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
403                 
404                 send_ldap_error( op, rs, LDAP_OTHER,
405                         "entry store failed" );
406
407                 goto return_results;
408         }
409
410         rs->sr_err = LDAP_SUCCESS;
411         send_ldap_result( op, rs );
412
413         /* marks the entry as committed, so it is added to the cache;
414          * otherwise it is removed from the cache, but not destroyed;
415          * it will be destroyed by the caller */
416         cache_entry_commit( op->oq_add.rs_e );
417
418 return_results:;
419         if (p != NULL) {
420                 /* free parent and writer lock */
421                 cache_return_entry_w( &li->li_cache, p ); 
422         }
423
424         if ( rs->sr_err ) {
425                 /*
426                  * in case of error, writer lock is freed 
427                  * and entry's private data is destroyed.
428                  * otherwise, this is done when entry is released
429                  */
430                 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
431                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
432         }
433
434         return( rs->sr_err );
435 }