]> git.sur5r.net Git - openldap/blob - servers/slapd/back-bdb/add.c
f59e5aad60ecead5e62ea1935854dfd836cfd1a8
[openldap] / servers / slapd / back-bdb / add.c
1 /* add.c - ldap BerkeleyDB back-end add routine */
2 /* $OpenLDAP$ */
3 /*
4  * Copyright 1998-2000 The OpenLDAP Foundation, All Rights Reserved.
5  * COPYING RESTRICTIONS APPLY, see COPYRIGHT file
6  */
7
8 #include "portable.h"
9
10 #include <stdio.h>
11 #include <ac/string.h>
12
13 #include "back-bdb.h"
14
15 int
16 bdb_add(
17         Backend *be,
18         Connection      *conn,
19         Operation       *op,
20         Entry   *e )
21 {
22         struct bdb_info *bdb = (struct bdb_info *) be->be_private;
23         char            *pdn = NULL;
24         Entry           *p = NULL;
25         int                     rootlock = 0;
26         int                     rc; 
27         const char      *text = NULL;
28         AttributeDescription *children = slap_schema.si_ad_children;
29         DB_TXN          *ltid = NULL;
30
31
32         Debug(LDAP_DEBUG_ARGS, "==> bdb_add: %s\n", e->e_dn, 0, 0);
33
34         /* check entry's schema */
35         rc = entry_schema_check( e, NULL, &text );
36         if ( rc != LDAP_SUCCESS ) {
37                 Debug( LDAP_DEBUG_TRACE,
38                         "bdb_add: entry failed schema check: %s (%d)\n",
39                         text, rc, 0 );
40                 goto return_results;
41         }
42
43         /*
44          * acquire an ID outside of the operation transaction
45          * to avoid serializing adds.
46          */
47         rc = bdb_next_id( be, NULL, &e->e_id );
48         if( rc != 0 ) {
49                 Debug( LDAP_DEBUG_TRACE,
50                         "bdb_add: next_id failed (%d)\n",
51                         rc, 0, 0 );
52                 rc = LDAP_OTHER;
53                 text = "internal error";
54                 goto return_results;
55         }
56
57         /* begin transaction */
58         rc = txn_begin( bdb->bi_dbenv, NULL, &ltid, 0 );
59         if( rc != 0 ) {
60                 Debug( LDAP_DEBUG_TRACE,
61                         "bdb_add: txn_begin failed: %s (%d)\n",
62                         db_strerror(rc), rc, 0 );
63                 rc = LDAP_OTHER;
64                 text = "internal error";
65                 goto return_results;
66         }
67         
68         /*
69          * Get the parent dn and see if the corresponding entry exists.
70          * If the parent does not exist, only allow the "root" user to
71          * add the entry.
72          */
73         pdn = dn_parent( be, e->e_ndn );
74
75         if( pdn != NULL && *pdn != '\0' ) {
76                 Entry *matched = NULL;
77
78                 /* get parent with reader lock */
79                 p = dn2entry_r( be, ltid, pdn, &matched );
80                 if ( p == NULL ) {
81                         char *matched_dn;
82                         struct berval **refs;
83
84                         ch_free( pdn );
85
86                         if ( matched != NULL ) {
87                                 matched_dn = ch_strdup( matched->e_dn );
88                                 refs = is_entry_referral( matched )
89                                         ? get_entry_referrals( be, conn, op, matched )
90                                         : NULL;
91                                 bdb_entry_return( be, matched );
92
93                         } else {
94                                 matched_dn = NULL;
95                                 refs = default_referral;
96                         }
97
98                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent does not exist\n",
99                                 0, 0, 0 );
100
101                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
102                             matched_dn, NULL, refs, NULL );
103
104                         if( matched != NULL ) {
105                                 ber_bvecfree( refs );
106                                 ch_free( matched_dn );
107                         }
108
109                         goto done;
110                 }
111
112                 ch_free(pdn);
113
114                 if ( ! access_allowed( be, conn, op, p,
115                         children, NULL, ACL_WRITE ) )
116                 {
117                         Debug( LDAP_DEBUG_TRACE, "bdb_add: no write access to parent\n",
118                                 0, 0, 0 );
119                         rc = LDAP_INSUFFICIENT_ACCESS;
120                         text = "no write access to parent", NULL, NULL;
121                         goto return_results;;
122                 }
123
124                 if ( is_entry_alias( p ) ) {
125                         /* parent is an alias, don't allow add */
126                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is alias\n",
127                                 0, 0, 0 );
128                         rc = LDAP_ALIAS_PROBLEM;
129                         text = "parent is an alias";
130                         goto return_results;;
131                 }
132
133                 if ( is_entry_referral( p ) ) {
134                         /* parent is a referral, don't allow add */
135                         char *matched_dn = ch_strdup( p->e_dn );
136                         struct berval **refs = is_entry_referral( p )
137                                 ? get_entry_referrals( be, conn, op, p )
138                                 : NULL;
139
140                         Debug( LDAP_DEBUG_TRACE, "bdb_add: parent is referral\n",
141                                 0, 0, 0 );
142
143                         send_ldap_result( conn, op, rc = LDAP_REFERRAL,
144                             matched_dn, NULL, refs, NULL );
145
146                         ber_bvecfree( refs );
147                         free( matched_dn );
148                         goto done;
149                 }
150
151         } else {
152                 if( pdn != NULL ) {
153                         free(pdn);
154                 }
155
156                 /*
157                  * no parent!
158                  *      must be adding entry to at suffix
159                  *  or with parent ""
160                  */
161                 if ( !be_isroot( be, op->o_ndn )) {
162                         Debug( LDAP_DEBUG_TRACE, "bdb_add: %s denied\n",
163                                 pdn == NULL ? "suffix" : "entry at root",
164                                 0, 0 );
165                         rc = LDAP_INSUFFICIENT_ACCESS;
166                         goto return_results;
167                 }
168         }
169
170         /* dn2id index */
171         rc = bdb_dn2id_add( be, ltid, e->e_ndn, e->e_id );
172         if ( rc != 0 ) {
173                 Debug( LDAP_DEBUG_TRACE, "bdb_add: dn2id_add failed: %s (%d)\n",
174                         db_strerror(rc), rc, 0 );
175                 if( rc == DB_KEYEXIST ) {
176                         rc = LDAP_ALREADY_EXISTS;
177                 } else {
178                         rc = LDAP_OTHER;
179                 }
180                 goto return_results;
181         }
182
183         /* id2entry index */
184         rc = bdb_id2entry_add( be, ltid, e );
185         if ( rc != 0 ) {
186                 Debug( LDAP_DEBUG_TRACE, "bdb_add: id2entry_add failed\n",
187                         0, 0, 0 );
188                 rc = LDAP_OTHER;
189                 text = "entry store failed";
190                 goto return_results;
191         }
192
193 #if 0
194         /* attribute indexes */
195         if ( index_entry_add( be, e, e->e_attrs ) != LDAP_SUCCESS ) {
196                 Debug( LDAP_DEBUG_TRACE, "bdb_add: index_entry_add failed\n",
197                         0, 0, 0 );
198                 rc = LDAP_OTHER;
199                 text = "index generation failed";
200                 goto return_results;
201         }
202 #endif
203
204         rc = txn_commit( ltid, 0 );
205         ltid = NULL;
206
207         if( rc == 0 ) {
208                 Debug( LDAP_DEBUG_TRACE,
209                         "bdb_add: txn_commit failed: %s (%d)\n",
210                         db_strerror(rc), rc, 0 );
211                 rc = LDAP_OTHER;
212                 text = "commit failed";
213         } else {
214                 Debug( LDAP_DEBUG_TRACE,
215                         "bdb_add: added id=%08x dn=\"%s\"\n",
216                         e->e_id, e->e_dn, 0 );
217                 rc = LDAP_SUCCESS;
218                 text = NULL;
219         }
220
221 return_results:
222         send_ldap_result( conn, op, rc,
223                         NULL, text, NULL, NULL );
224
225
226 done:
227         if (p != NULL) {
228                 /* free parent and writer lock */
229                 bdb_entry_return( be, p ); 
230         }
231
232         if( ltid != NULL ) {
233                 txn_abort( ltid );
234         }
235
236         return rc;
237 }