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