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