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