1 /* add.c - ldap ldbm back-end add routine */
6 #include <sys/socket.h>
10 extern int global_schemacheck;
11 extern char *dn_parent();
12 extern char *dn_normalize();
13 extern Entry *dn2entry();
23 struct ldbminfo *li = (struct ldbminfo *) be->be_private;
25 char *dn = NULL, *pdn = NULL;
28 dn = dn_normalize( strdup( e->e_dn ) );
30 if ( (p = dn2entry( be, dn, &matched )) != NULL ) {
31 cache_return_entry( &li->li_cache, p );
34 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
37 if ( matched != NULL ) {
40 /* XXX race condition here til we cache_add_entry_lock below XXX */
42 if ( global_schemacheck && oc_schema_check( e ) != 0 ) {
43 Debug( LDAP_DEBUG_TRACE, "entry failed schema check\n", 0, 0,
47 send_ldap_result( conn, op, LDAP_OBJECT_CLASS_VIOLATION, "",
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
58 e->e_id = next_id( be );
59 if ( cache_add_entry_lock( &li->li_cache, e, ENTRY_STATE_CREATING )
61 Debug( LDAP_DEBUG_ANY, "cache_add_entry_lock failed\n", 0, 0,
63 next_id_return( be, e->e_id );
66 send_ldap_result( conn, op, LDAP_ALREADY_EXISTS, "", "" );
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
76 if ( (pdn = dn_parent( be, dn )) != NULL ) {
79 if ( (p = dn2entry( be, pdn, &matched )) == NULL ) {
80 send_ldap_result( conn, op, LDAP_NO_SUCH_OBJECT,
82 if ( matched != NULL ) {
85 Debug( LDAP_DEBUG_TRACE, "parent does not exist\n", 0,
89 if ( matched != NULL ) {
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,
97 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
102 if ( ! be_isroot( be, op->o_dn ) ) {
103 Debug( LDAP_DEBUG_TRACE, "no parent & not root\n", 0,
105 send_ldap_result( conn, op, LDAP_INSUFFICIENT_ACCESS,
113 * add it to the id2children index for the parent
116 if ( id2children_add( be, p, e ) != 0 ) {
117 Debug( LDAP_DEBUG_TRACE, "id2children_add failed\n", 0,
119 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "",
125 * Add the entry to the attribute indexes, then add it to
126 * the id2children index, dn2id index, and the id2entry index.
129 /* attribute indexes */
130 if ( index_add_entry( be, e ) != 0 ) {
131 Debug( LDAP_DEBUG_TRACE, "index_add_entry failed\n", 0,
133 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
138 if ( dn2id_add( be, dn, e->e_id ) != 0 ) {
139 Debug( LDAP_DEBUG_TRACE, "dn2id_add failed\n", 0,
141 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
146 if ( id2entry_add( be, e ) != 0 ) {
147 Debug( LDAP_DEBUG_TRACE, "id2entry_add failed\n", 0,
149 (void) dn2id_delete( be, dn );
150 send_ldap_result( conn, op, LDAP_OPERATIONS_ERROR, "", "" );
154 send_ldap_result( conn, op, LDAP_SUCCESS, "", "" );
159 cache_set_state( &li->li_cache, e, 0 );
160 cache_return_entry( &li->li_cache, e );
168 next_id_return( be, e->e_id );
169 cache_delete_entry( &li->li_cache, e );
170 cache_return_entry( &li->li_cache, e );