]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
ITS#2736 clear result strings
[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                         rs->sr_ref = NULL;
138                         rs->sr_matched = NULL;
139                         return rs->sr_err;
140                 }
141
142                 if ( ! access_allowed( op, p,
143                         children, NULL, ACL_WRITE, NULL ) )
144                 {
145                         /* free parent and writer lock */
146                         cache_return_entry_w( &li->li_cache, p ); 
147                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
148
149 #ifdef NEW_LOGGING
150                         LDAP_LOG( BACK_LDBM, ERR, 
151                                 "ldbm_back_add: No write access to parent (%s).\n", 
152                                 op->o_req_dn.bv_val, 0, 0 );
153 #else
154                         Debug( LDAP_DEBUG_TRACE, "no write access to parent\n", 0,
155                             0, 0 );
156 #endif
157
158                         send_ldap_error( op, rs, LDAP_INSUFFICIENT_ACCESS,
159                             "no write access to parent" );
160
161                         return LDAP_INSUFFICIENT_ACCESS;
162                 }
163
164 #ifdef LDBM_SUBENTRIES
165                 if ( is_entry_subentry( p )) {
166 #ifdef NEW_LOGGING
167                         LDAP_LOG( OPERATION, DETAIL1,
168                                 "bdb_add: parent is subentry\n", 0, 0, 0 );
169 #else
170                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is subentry\n",
171                                 0, 0, 0 );
172 #endif
173                         rs->sr_err = LDAP_OBJECT_CLASS_VIOLATION;
174                         rs->sr_text = "parent is a subentry";
175                         goto return_results;
176                 }
177 #endif
178
179                 if ( is_entry_alias( p ) ) {
180                         /* parent is an alias, don't allow add */
181
182                         /* free parent and writer lock */
183                         cache_return_entry_w( &li->li_cache, p );
184                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
185
186 #ifdef NEW_LOGGING
187                         LDAP_LOG(BACK_LDBM, ERR, 
188                                 "ldbm_back_add:  Parent is an alias.\n", 0, 0, 0 );
189 #else
190                         Debug( LDAP_DEBUG_TRACE, "parent is alias\n", 0,
191                             0, 0 );
192 #endif
193
194
195                         send_ldap_error( op, rs, LDAP_ALIAS_PROBLEM,
196                             "parent is an alias" );
197
198                         return LDAP_ALIAS_PROBLEM;
199                 }
200
201                 if ( is_entry_referral( p ) ) {
202                         /* parent is a referral, don't allow add */
203                         rs->sr_matched = ch_strdup( p->e_dn );
204                         rs->sr_ref = is_entry_referral( p )
205                                 ? get_entry_referrals( op, p )
206                                 : NULL;
207
208                         /* free parent and writer lock */
209                         cache_return_entry_w( &li->li_cache, p );
210                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
211
212 #ifdef NEW_LOGGING
213                         LDAP_LOG( BACK_LDBM, ERR,
214                                    "ldbm_back_add: Parent is referral.\n", 0, 0, 0 );
215 #else
216                         Debug( LDAP_DEBUG_TRACE, "parent is referral\n", 0,
217                             0, 0 );
218 #endif
219                         rs->sr_err = LDAP_REFERRAL;
220                         send_ldap_result( op, rs );
221
222                         ber_bvarray_free( rs->sr_ref );
223                         free( (char *)rs->sr_matched );
224                         rs->sr_ref = NULL;
225                         rs->sr_matched = NULL;
226                         return rs->sr_err;
227                 }
228
229 #ifdef LDBM_SUBENTRIES
230                 if ( subentry ) {
231                         /* FIXME: */
232                         /* parent must be an administrative point of the required kind */
233                 }
234 #endif
235
236         } else {
237                 if( pdn.bv_val != NULL )
238                 {
239                         assert( *pdn.bv_val == '\0' );
240                 }
241
242                 /* no parent, must be adding entry to root */
243                 if ( !be_isroot( op->o_bd, &op->o_ndn ) )
244                 {
245                         if ( be_issuffix( op->o_bd, (struct berval *)&slap_empty_bv ) || be_isupdate( op->o_bd, &op->o_ndn ) ) {
246                                 p = (Entry *)&slap_entry_root;
247                                 
248                                 rs->sr_err = access_allowed( op, p,
249                                         children, NULL, ACL_WRITE, NULL );
250                                 p = NULL;
251                                 
252                                 if ( ! rs->sr_err ) {
253                                         ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
254
255 #ifdef NEW_LOGGING
256                                         LDAP_LOG( BACK_LDBM, ERR,
257                                                 "ldbm_back_add: No write "
258                                                 "access to parent (\"\").\n", 0, 0, 0 );
259 #else
260                                         Debug( LDAP_DEBUG_TRACE, 
261                                                 "no write access to parent\n", 
262                                                 0, 0, 0 );
263 #endif
264
265                                         send_ldap_error( op, rs,
266                                                 LDAP_INSUFFICIENT_ACCESS,
267                                                 "no write access to parent" );
268
269                                         return LDAP_INSUFFICIENT_ACCESS;
270                                 }
271                         } else if ( !is_entry_glue( op->oq_add.rs_e )) {
272                                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
273
274 #ifdef NEW_LOGGING
275                                 LDAP_LOG( BACK_LDBM, ERR,
276                                            "ldbm_back_add: %s add denied.\n",
277                                            pdn.bv_val == NULL ? "suffix" 
278                                            : "entry at root", 0, 0 );
279 #else
280                                 Debug( LDAP_DEBUG_TRACE, "%s add denied\n",
281                                                 pdn.bv_val == NULL ? "suffix" 
282                                                 : "entry at root", 0, 0 );
283 #endif
284
285                                 send_ldap_error( op, rs,
286                                                 LDAP_NO_SUCH_OBJECT, NULL );
287
288                                         return LDAP_NO_SUCH_OBJECT;
289                         }
290                 }
291
292 #ifdef LDBM_SUBENTRIES
293                         if( subentry ) {
294 #ifdef NEW_LOGGING
295                                         LDAP_LOG ( OPERATION, DETAIL1,
296                                                 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
297 #else
298                                         Debug( LDAP_DEBUG_TRACE,
299                                                 "bdb_add: no parent, cannot add subentry\n", 0, 0, 0 );
300 #endif
301                                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
302                                         rs->sr_text = "no parent, cannot add subentry";
303                                         goto return_results;
304                                 }
305 #endif
306
307         }
308
309         if ( next_id( op->o_bd, &op->oq_add.rs_e->e_id ) ) {
310                 if( p != NULL) {
311                         /* free parent and writer lock */
312                         cache_return_entry_w( &li->li_cache, p ); 
313                 }
314
315                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
316
317 #ifdef NEW_LOGGING
318                 LDAP_LOG( BACK_LDBM, ERR,
319                         "ldbm_back_add: next_id failed.\n", 0, 0, 0 );
320 #else
321                 Debug( LDAP_DEBUG_ANY, "ldbm_add: next_id failed\n",
322                         0, 0, 0 );
323 #endif
324
325                 send_ldap_error( op, rs, LDAP_OTHER,
326                         "next_id add failed" );
327
328                 return LDAP_OTHER;
329         }
330
331         /*
332          * Try to add the entry to the cache, assign it a new dnid.
333          */
334         rs->sr_err = cache_add_entry_rw(&li->li_cache, op->oq_add.rs_e, CACHE_WRITE_LOCK);
335
336         if ( rs->sr_err != 0 ) {
337                 if( p != NULL) {
338                         /* free parent and writer lock */
339                         cache_return_entry_w( &li->li_cache, p ); 
340                 }
341
342                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
343
344 #ifdef NEW_LOGGING
345                 LDAP_LOG( BACK_LDBM, ERR,
346                         "ldbm_back_add: cache_add_entry_lock failed.\n", 0, 0, 0 );
347 #else
348                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
349                     0 );
350 #endif
351
352                 rs->sr_text = rs->sr_err > 0 ? NULL : "cache add failed";
353                 rs->sr_err = rs->sr_err > 0 ? LDAP_ALREADY_EXISTS : LDAP_OTHER;
354                 send_ldap_result( op, rs );
355
356                 return rs->sr_err;
357         }
358
359         rs->sr_err = -1;
360
361         /* attribute indexes */
362         if ( index_entry_add( op, op->oq_add.rs_e ) != LDAP_SUCCESS ) {
363 #ifdef NEW_LOGGING
364                 LDAP_LOG( BACK_LDBM, ERR,
365                         "ldbm_back_add: index_entry_add failed.\n", 0, 0, 0 );
366 #else
367                 Debug( LDAP_DEBUG_TRACE, "index_entry_add failed\n", 0,
368                     0, 0 );
369 #endif
370                 
371                 send_ldap_error( op, rs, LDAP_OTHER,
372                         "index generation failed" );
373
374                 goto return_results;
375         }
376
377         /* dn2id index */
378         if ( dn2id_add( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id ) != 0 ) {
379 #ifdef NEW_LOGGING
380                 LDAP_LOG( BACK_LDBM, ERR,
381                         "ldbm_back_add: dn2id_add failed.\n", 0, 0, 0 );
382 #else
383                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
384                     0, 0 );
385 #endif
386                 /* FIXME: delete attr indices? */
387
388                 send_ldap_error( op, rs, LDAP_OTHER,
389                         "DN index generation failed" );
390
391                 goto return_results;
392         }
393
394         /* id2entry index */
395         if ( id2entry_add( op->o_bd, op->oq_add.rs_e ) != 0 ) {
396 #ifdef NEW_LOGGING
397                 LDAP_LOG( BACK_LDBM, ERR,
398                            "ldbm_back_add: id2entry_add failed.\n", 0, 0, 0 );
399 #else
400                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
401                     0, 0 );
402 #endif
403
404                 /* FIXME: delete attr indices? */
405                 (void) dn2id_delete( op->o_bd, &op->oq_add.rs_e->e_nname, op->oq_add.rs_e->e_id );
406                 
407                 send_ldap_error( op, rs, LDAP_OTHER,
408                         "entry store failed" );
409
410                 goto return_results;
411         }
412
413         rs->sr_err = LDAP_SUCCESS;
414         rs->sr_text = NULL;
415         send_ldap_result( op, rs );
416
417         /* marks the entry as committed, so it is added to the cache;
418          * otherwise it is removed from the cache, but not destroyed;
419          * it will be destroyed by the caller */
420         cache_entry_commit( op->oq_add.rs_e );
421
422 return_results:;
423         if (p != NULL) {
424                 /* free parent and writer lock */
425                 cache_return_entry_w( &li->li_cache, p ); 
426         }
427
428         if ( rs->sr_err ) {
429                 /*
430                  * in case of error, writer lock is freed 
431                  * and entry's private data is destroyed.
432                  * otherwise, this is done when entry is released
433                  */
434                 cache_return_entry_w( &li->li_cache, op->oq_add.rs_e );
435                 ldap_pvt_thread_rdwr_wunlock(&li->li_giant_rwlock);
436         }
437
438         return( rs->sr_err );
439 }