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