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