]> git.sur5r.net Git - openldap/blob - servers/slapd/back-sql/modify.c
ffffbcd344e24f647ef13985d2e55fd0e1512a00
[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-2004 The OpenLDAP Foundation.
5  * Portions Copyright 1999 Dmitry Kovalev.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted only as authorized by the OpenLDAP
10  * Public License.
11  *
12  * A copy of this license is available in the file LICENSE in the
13  * top-level directory of the distribution or, alternatively, at
14  * <http://www.OpenLDAP.org/license.html>.
15  */
16 /* ACKNOWLEDGEMENTS:
17  * This work was initially developed by Dmitry Kovalev for inclusion
18  * by OpenLDAP Software.
19  */
20
21 #include "portable.h"
22
23 #include <stdio.h>
24 #include <sys/types.h>
25 #include "ac/string.h"
26
27 #include "slap.h"
28 #include "proto-sql.h"
29
30 int
31 backsql_modify( Operation *op, SlapReply *rs )
32 {
33         backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
34         SQLHDBC                 dbh = SQL_NULL_HDBC;
35         backsql_oc_map_rec      *oc = NULL;
36         backsql_srch_info       bsi = { 0 };
37         Entry                   e = { 0 };
38
39         /*
40          * FIXME: in case part of the operation cannot be performed
41          * (missing mapping, SQL write fails or so) the entire operation
42          * should be rolled-back
43          */
44         Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): modifying entry \"%s\"\n",
45                 op->o_req_ndn.bv_val, 0, 0 );
46
47         rs->sr_err = backsql_get_db_conn( op, &dbh );
48         if ( rs->sr_err != LDAP_SUCCESS ) {
49                 Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
50                         "could not get connection handle - exiting\n", 
51                         0, 0, 0 );
52                 /*
53                  * FIXME: we don't want to send back 
54                  * excessively detailed messages
55                  */
56                 rs->sr_text = ( rs->sr_err == LDAP_OTHER )
57                         ? "SQL-backend error" : NULL;
58                 goto done;
59         }
60
61         /* FIXME: using all attributes because of access control later ... */
62         rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
63                         LDAP_SCOPE_BASE, 
64                         SLAP_NO_LIMIT, SLAP_NO_LIMIT,
65                         (time_t)(-1), NULL, dbh, op, rs,
66                         slap_anlist_all_attributes,
67                         BACKSQL_ISF_GET_ID );
68         if ( rs->sr_err != LDAP_SUCCESS ) {
69                 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
70                         "could not retrieve modifyDN ID - no such entry\n", 
71                         0, 0, 0 );
72                 rs->sr_err = LDAP_NO_SUCH_OBJECT;
73                 goto done;
74         }
75
76         bsi.bsi_e = &e;
77         rs->sr_err = backsql_id2entry( &bsi, &bsi.bsi_base_id );
78         if ( rs->sr_err != LDAP_SUCCESS ) {
79                 Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
80                         "error %d in backsql_id2entry()\n",
81                         rs->sr_err, 0, 0 );
82                 goto done;
83         }
84
85 #ifdef BACKSQL_ARBITRARY_KEY
86         Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
87                 "modifying entry \"%s\" (id=%s)\n", 
88                 bsi.bsi_base_id.eid_dn.bv_val,
89                 bsi.bsi_base_id.eid_id.bv_val, 0 );
90 #else /* ! BACKSQL_ARBITRARY_KEY */
91         Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
92                 "modifying entry \"%s\" (id=%ld)\n", 
93                 bsi.bsi_base_id.eid_dn.bv_val, bsi.bsi_base_id.eid_id, 0 );
94 #endif /* ! BACKSQL_ARBITRARY_KEY */
95
96         oc = backsql_id2oc( bi, bsi.bsi_base_id.eid_oc_id );
97         if ( oc == NULL ) {
98                 Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
99                         "cannot determine objectclass of entry -- aborting\n",
100                         0, 0, 0 );
101                 /*
102                  * FIXME: should never occur, since the entry was built!!!
103                  */
104
105                 /*
106                  * FIXME: we don't want to send back 
107                  * excessively detailed messages
108                  */
109                 rs->sr_err = LDAP_OTHER;
110                 rs->sr_text = "SQL-backend error";
111                 goto done;
112         }
113
114         e.e_attrs = NULL;
115         e.e_name = op->o_req_dn;
116         e.e_nname = op->o_req_ndn;
117         if ( !acl_check_modlist( op, &e, op->oq_modify.rs_modlist ) ) {
118                 rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
119
120         } else {
121                 rs->sr_err = backsql_modify_internal( op, rs, dbh, oc,
122                                 &bsi.bsi_base_id,
123                                 op->oq_modify.rs_modlist );
124         }
125
126         if ( rs->sr_err == LDAP_SUCCESS ) {
127                 /*
128                  * Commit only if all operations succeed
129                  */
130                 SQLTransact( SQL_NULL_HENV, dbh, 
131                                 op->o_noop ? SQL_ROLLBACK : SQL_COMMIT );
132         }
133
134 done:;
135         send_ldap_result( op, rs );
136
137         if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
138                 (void)backsql_free_entryID( &bsi.bsi_base_id, 0 );
139         }
140
141         if ( bsi.bsi_e != NULL ) {
142                 entry_clean( bsi.bsi_e );
143         }
144
145         Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
146
147         return rs->sr_err != LDAP_SUCCESS ? rs->sr_err : op->o_noop;
148 }
149