]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
75dd12b1b2506bae216d78f2358a0635931b7f70
[openldap] / servers / slapd / back-ldbm / add.c
1 /* add.c - ldap ldbm back-end add routine */
2
3 #include "portable.h"
4
5 #include <stdio.h>
6
7 #include <ac/socket.h>
8 #include <ac/string.h>
9
10 #include "slap.h"
11 #include "back-ldbm.h"
12 #include "proto-back-ldbm.h"
13
14 int
15 ldbm_back_add(
16     Backend     *be,
17     Connection  *conn,
18     Operation   *op,
19     Entry       *e
20 )
21 {
22         struct ldbminfo *li = (struct ldbminfo *) be->be_private;
23         char            *dn = NULL, *pdn = NULL;
24         Entry           *p = NULL;
25         int                     rc;
26
27         dn = dn_normalize( strdup( e->e_dn ) );
28
29         Debug(LDAP_DEBUG_ARGS, "==> ldbm_back_add: %s\n", dn, 0, 0);
30
31         if ( ( dn2id( be, dn ) ) != NOID ) {
32                 entry_free( e );
33                 free( dn );
34                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
35                 return( -1 );
36         }
37
38         /* XXX race condition here til we cache_add_entry_lock below XXX */
39
40         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
41                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n",
42                         0, 0, 0 );
43
44                 /* XXX this should be ok, no other thread should have access
45                  * because e hasn't been added to the cache yet
46                  */
47                 entry_free( e );
48                 free( dn );
49                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
50                     "" );
51                 return( -1 );
52         }
53
54         /*
55          * Try to add the entry to the cache, assign it a new dnid
56          * and mark it locked.  This should only fail if the entry
57          * already exists.
58          */
59
60         e->e_id = next_id( be );
61         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
62             != 0 ) {
63                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
64                     0 );
65                 next_id_return( be, e->e_id );
66                 
67                 /* XXX this should be ok, no other thread should have access
68                  * because e hasn't been added to the cache yet
69                  */
70                 entry_free( e );
71                 free( dn );
72                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
73                 return( -1 );
74         }
75
76         /*
77          * Get the parent dn and see if the corresponding entry exists.
78          * If the parent does not exist, only allow the "root" user to
79          * add the entry.
80          */
81
82         if ( (pdn = dn_parent( be, dn )) != NULL ) {
83                 char *matched;
84                 /* no parent */
85                 matched = NULL;
86
87                 /* get entry with reader lock */
88                 if ( (p = dn2entry_r( be, pdn, &matched )) == NULL ) {
89                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
90                             0, 0 );
91                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
92                             matched, "" );
93                         if ( matched != NULL ) {
94                                 free( matched );
95                         }
96
97                         rc = -1;
98                         goto return_results;
99                 }
100                 if ( matched != NULL ) {
101                         free( matched );
102                 }
103
104                 if ( ! access_allowed( be, conn, op, p, "children", NULL,
105                     op->o_dn, ACL_WRITE ) ) {
106                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
107                             0, 0 );
108                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
109                             "", "" );
110
111                         rc = -1;
112                         goto return_results;
113                 }
114         } else {
115                 if ( ! be_isroot( be, op->o_dn ) ) {
116                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
117                             0, 0 );
118                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
119                             "", "" );
120
121                         rc = -1;
122                         goto return_results;
123                 }
124         }
125
126         /*
127          * add it to the id2children index for the parent
128          */
129
130         if ( id2children_add( be, p, e ) != 0 ) {
131                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
132                     0, 0 );
133                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
134
135                 rc = -1;
136                 goto return_results;
137         }
138
139         /*
140          * Add the entry to the attribute indexes, then add it to
141          * the id2children index, dn2id index, and the id2entry index.
142          */
143
144         /* attribute indexes */
145         if ( index_add_entry( be, e ) != 0 ) {
146                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
147                     0, 0 );
148                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
149
150                 rc = -1;
151                 goto return_results;
152         }
153
154         /* dn2id index */
155         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
156                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
157                     0, 0 );
158                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
159
160                 rc = -1;
161                 goto return_results;
162         }
163
164         /* acquire writer lock */
165         entry_rdwr_lock(e, 1);
166
167         /* id2entry index */
168         if ( id2entry_add( be, e ) != 0 ) {
169                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
170                     0, 0 );
171                 (void) dn2id_delete( be, dn );
172                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
173
174                 rc = -1;
175                 goto return_results;
176         }
177
178         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
179         rc = 0;
180
181 return_results:;
182
183         if ( dn != NULL )
184                 free( dn );
185         if ( pdn != NULL )
186                 free( pdn );
187
188         cache_set_state( &li->li_cache, e, 0 );
189
190         /* free entry and writer lock */
191         cache_return_entry_w( &li->li_cache, e ); 
192
193         /* free entry and reader lock */
194         if (p != NULL) {
195                 cache_return_entry_r( &li->li_cache, p ); 
196         }
197
198         return( rc );
199 }