]> git.sur5r.net Git - openldap/blob - servers/slapd/back-ldbm/add.c
Suck in HEAD changes since 2.1alpha
[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
10 extern int      global_schemacheck;
11 extern char     *dn_parent();
12 extern char     *dn_normalize();
13 extern Entry    *dn2entry();
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            *matched;
25         char            *dn = NULL, *pdn = NULL;
26         Entry           *p;
27
28         dn = dn_normalize( strdup( e->e_dn ) );
29         matched = NULL;
30         if ( (p = dn2entry( be, dn, &matched )) != NULL ) {
31                 cache_return_entry( &li->li_cache, p );
32                 entry_free( e );
33                 free( dn );
34                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
35                 return( -1 );
36         }
37         if ( matched != NULL ) {
38                 free( matched );
39         }
40         /* XXX race condition here til we cache_add_entry_lock below XXX */
41
42         if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
43                 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0,
44                     0 );
45                 entry_free( e );
46                 free( dn );
47                 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
48                     "" );
49                 return( -1 );
50         }
51
52         /*
53          * Try to add the entry to the cache, assign it a new dnid
54          * and mark it locked.  This should only fail if the entry
55          * already exists.
56          */
57
58         e->e_id = next_id( be );
59         if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
60             != 0 ) {
61                 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
62                     0 );
63                 next_id_return( be, e->e_id );
64                 entry_free( e );
65                 free( dn );
66                 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
67                 return( -1 );
68         }
69
70         /*
71          * Get the parent dn and see if the corresponding entry exists.
72          * If the parent does not exist, only allow the "root" user to
73          * add the entry.
74          */
75
76         if ( (pdn = dn_parent( be, dn )) != NULL ) {
77                 /* no parent */
78                 matched = NULL;
79                 if ( (p = dn2entry( be, pdn, &matched )) == NULL ) {
80                         send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
81                             matched, "" );
82                         if ( matched != NULL ) {
83                                 free( matched );
84                         }
85                         Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
86                             0, 0 );
87                         goto error_return;
88                 }
89                 if ( matched != NULL ) {
90                         free( matched );
91                 }
92
93                 if ( ! access_allowed( be, conn, op, p, "children", NULL,
94                     op->o_dn, ACL_WRITE ) ) {
95                         Debug( LDAP_DEBUG_TRACE, "no access to parent\n", 0,
96                             0, 0 );
97                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
98                             "", "" );
99                         goto error_return;
100                 }
101         } else {
102                 if ( ! be_isroot( be, op->o_dn ) ) {
103                         Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
104                             0, 0 );
105                         send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
106                             "", "" );
107                         goto error_return;
108                 }
109                 p = NULL;
110         }
111
112         /*
113          * add it to the id2children index for the parent
114          */
115
116         if ( id2children_add( be, p, e ) != 0 ) {
117                 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
118                     0, 0 );
119                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
120                     "" );
121                 goto error_return;
122         }
123
124         /*
125          * Add the entry to the attribute indexes, then add it to
126          * the id2children index, dn2id index, and the id2entry index.
127          */
128
129         /* attribute indexes */
130         if ( index_add_entry( be, e ) != 0 ) {
131                 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
132                     0, 0 );
133                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
134                 goto error_return;
135         }
136
137         /* dn2id index */
138         if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
139                 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
140                     0, 0 );
141                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
142                 goto error_return;
143         }
144
145         /* id2entry index */
146         if ( id2entry_add( be, e ) != 0 ) {
147                 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
148                     0, 0 );
149                 (void) dn2id_delete( be, dn );
150                 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
151                 goto error_return;
152         }
153
154         send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
155         if ( dn != NULL )
156                 free( dn );
157         if ( pdn != NULL )
158                 free( pdn );
159         cache_set_state( &li->li_cache, e, 0 );
160         cache_return_entry( &li->li_cache, e );
161         return( 0 );
162
163 error_return:;
164         if ( dn != NULL )
165                 free( dn );
166         if ( pdn != NULL )
167                 free( pdn );
168         next_id_return( be, e->e_id );
169         cache_delete_entry( &li->li_cache, e );
170         cache_return_entry( &li->li_cache, e );
171
172         return( -1 );
173 }