]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/modify.c
fix several schema checking issues; add rename specific statement for entry renaming...
[openldap] / servers / slapd / back-sql / modify.c
1 /* $OpenLDAP$ */
2 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
3  *
4  * Copyright 1999-2005 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * Portions Copyright 2002 Pierangelo Masarati.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted only as authorized by the OpenLDAP
11  * Public License.
12  *
13  * A copy of this license is available in the file LICENSE in the
14  * top-level directory of the distribution or, alternatively, at
15  * <http://www.OpenLDAP.org/license.html>.
16  */
17 /* ACKNOWLEDGEMENTS:
18  * This work was initially developed by Dmitry Kovalev for inclusion
19  * by OpenLDAP Software.  Additional significant contributors include
20  * Pierangelo Masarati.
21  */
22
23 #include "portable.h"
24
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include "ac/string.h"
28
29 #include "slap.h"
30 #include "proto-sql.h"
31
32 int
33 backsql_modify( Operation *op, SlapReply *rs )
34 {
35         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
36         SQLHDBC                 dbh = SQL_NULL_HDBC;
37         backsql_oc_map_rec      *oc = NULL;
38         backsql_srch_info       bsi = { 0 };
39         Entry                   m = { 0 }, *e = NULL;
40         int                     manageDSAit = get_manageDSAit( op );
41         SQLUSMALLINT            CompletionType = SQL_ROLLBACK;
42
43         /*
44          * FIXME: in case part of the operation cannot be performed
45          * (missing mapping, SQL write fails or so) the entire operation
46          * should be rolled-back
47          */
48         Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): modifying entry \"%s\"\n",
49                 op->o_req_ndn.bv_val, 0, 0 );
50
51         rs->sr_err = backsql_get_db_conn( op, &dbh );
52         if ( rs->sr_err != LDAP_SUCCESS ) {
53                 Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
54                         "could not get connection handle - exiting\n", 
55                         0, 0, 0 );
56                 /*
57                  * FIXME: we don't want to send back 
58                  * excessively detailed messages
59                  */
60                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
61                         ? "SQL-backend error" : NULL;
62                 goto done;
63         }
64
65         bsi.bsi_e = &m;
66         rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
67                         LDAP_SCOPE_BASE, 
68                         SLAP_NO_LIMIT, SLAP_NO_LIMIT,
69                         (time_t)(-1), NULL, dbh, op, rs,
70                         slap_anlist_all_attributes,
71                         ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
72         switch ( rs->sr_err ) {
73         case LDAP_SUCCESS:
74                 break;
75
76         case LDAP_REFERRAL:
77                 if ( !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
78                                 dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname )
79                                 && manageDSAit )
80                 {
81                         rs->sr_err = LDAP_SUCCESS;
82                         rs->sr_text = NULL;
83                         rs->sr_matched = NULL;
84                         if ( rs->sr_ref ) {
85                                 ber_bvarray_free( rs->sr_ref );
86                                 rs->sr_ref = NULL;
87                         }
88                         break;
89                 }
90                 e = &m;
91                 /* fallthru */
92
93         default:
94                 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
95                         "could not retrieve modifyDN ID - no such entry\n", 
96                         0, 0, 0 );
97                 if ( !BER_BVISNULL( &m.e_nname ) ) {
98                         /* FIXME: should always be true! */
99                         e = &m;
100
101                 } else {
102                         e = NULL;
103                 }
104                 goto done;
105         }
106
107 #ifdef BACKSQL_ARBITRARY_KEY
108         Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
109                 "modifying entry \"%s\" (id=%s)\n", 
110                 bsi.bsi_base_id.eid_dn.bv_val,
111                 bsi.bsi_base_id.eid_id.bv_val, 0 );
112 #else /* ! BACKSQL_ARBITRARY_KEY */
113         Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
114                 "modifying entry \"%s\" (id=%ld)\n", 
115                 bsi.bsi_base_id.eid_dn.bv_val, bsi.bsi_base_id.eid_id, 0 );
116 #endif /* ! BACKSQL_ARBITRARY_KEY */
117
118         if ( get_assert( op ) &&
119                         ( test_filter( op, &m, get_assertion( op ) )
120                           != LDAP_COMPARE_TRUE ))
121         {
122                 rs->sr_err = LDAP_ASSERTION_FAILED;
123                 e = &m;
124                 goto done;
125         }
126
127         oc = backsql_id2oc( bi, bsi.bsi_base_id.eid_oc_id );
128         if ( oc == NULL ) {
129                 Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
130                         "cannot determine objectclass of entry -- aborting\n",
131                         0, 0, 0 );
132                 /*
133                  * FIXME: should never occur, since the entry was built!!!
134                  */
135
136                 /*
137                  * FIXME: we don't want to send back 
138                  * excessively detailed messages
139                  */
140                 rs->sr_err = LDAP_OTHER;
141                 rs->sr_text = "SQL-backend error";
142                 e = NULL;
143                 goto done;
144         }
145
146         if ( !acl_check_modlist( op, &m, op->oq_modify.rs_modlist ) ) {
147                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
148                 e = &m;
149                 goto done;
150         }
151
152         rs->sr_err = backsql_modify_internal( op, rs, dbh, oc,
153                         &bsi.bsi_base_id,
154                         op->oq_modify.rs_modlist );
155         if ( rs->sr_err != LDAP_SUCCESS ) {
156                 e = &m;
157                 goto do_transact;
158         }
159
160         if ( global_schemacheck ) {
161                 char            textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
162
163                 entry_clean( &m );
164
165                 bsi.bsi_e = &m;
166                 rs->sr_err = backsql_id2entry( &bsi, &bsi.bsi_base_id );
167                 if ( rs->sr_err != LDAP_SUCCESS ) {
168                         e = &m;
169                         goto do_transact;
170                 }
171
172                 rs->sr_err = entry_schema_check( op->o_bd, &m,
173                                 NULL,
174                                 &rs->sr_text, textbuf, sizeof( textbuf ) );
175                 if ( rs->sr_err != LDAP_SUCCESS ) {
176                         Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
177                                 "entry failed schema check -- aborting\n",
178                                 m.e_name.bv_val, 0, 0 );
179                         e = NULL;
180                         goto do_transact;
181                 }
182         }
183
184 do_transact:;
185         /*
186          * Commit only if all operations succeed
187          */
188         if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
189                 CompletionType = SQL_COMMIT;
190         }
191
192         SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
193
194 done:;
195 #ifdef SLAP_ACL_HONOR_DISCLOSE
196         if ( e != NULL ) {
197                 if ( !access_allowed( op, e, slap_schema.si_ad_entry, NULL,
198                                         ACL_DISCLOSE, NULL ) )
199                 {
200                         rs->sr_err = LDAP_NO_SUCH_OBJECT;
201                         rs->sr_text = NULL;
202                         rs->sr_matched = NULL;
203                         if ( rs->sr_ref ) {
204                                 ber_bvarray_free( rs->sr_ref );
205                                 rs->sr_ref = NULL;
206                         }
207                 }
208         }
209 #endif /* SLAP_ACL_HONOR_DISCLOSE */
210
211         send_ldap_result( op, rs );
212
213         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
214                 (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
215         }
216
217         if ( !BER_BVISNULL( &m.e_nname ) ) {
218                 entry_clean( &m );
219         }
220
221         if ( bsi.bsi_attrs != NULL ) {
222                 op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
223         }
224
225         Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
226
227         return rs->sr_err;
228 }
229