Back to index

openldap  2.4.31
modify.c
Go to the documentation of this file.
00001 /* $OpenLDAP$ */
00002 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
00003  *
00004  * Copyright 1999-2012 The OpenLDAP Foundation.
00005  * Portions Copyright 1999 Dmitry Kovalev.
00006  * Portions Copyright 2002 Pierangelo Masarati.
00007  * All rights reserved.
00008  *
00009  * Redistribution and use in source and binary forms, with or without
00010  * modification, are permitted only as authorized by the OpenLDAP
00011  * Public License.
00012  *
00013  * A copy of this license is available in the file LICENSE in the
00014  * top-level directory of the distribution or, alternatively, at
00015  * <http://www.OpenLDAP.org/license.html>.
00016  */
00017 /* ACKNOWLEDGEMENTS:
00018  * This work was initially developed by Dmitry Kovalev for inclusion
00019  * by OpenLDAP Software.  Additional significant contributors include
00020  * Pierangelo Masarati.
00021  */
00022 
00023 #include "portable.h"
00024 
00025 #include <stdio.h>
00026 #include <sys/types.h>
00027 #include "ac/string.h"
00028 
00029 #include "slap.h"
00030 #include "proto-sql.h"
00031 
00032 int
00033 backsql_modify( Operation *op, SlapReply *rs )
00034 {
00035        backsql_info         *bi = (backsql_info*)op->o_bd->be_private;
00036        SQLHDBC              dbh = SQL_NULL_HDBC;
00037        backsql_oc_map_rec   *oc = NULL;
00038        backsql_srch_info    bsi = { 0 };
00039        Entry                m = { 0 }, *e = NULL;
00040        int                  manageDSAit = get_manageDSAit( op );
00041        SQLUSMALLINT         CompletionType = SQL_ROLLBACK;
00042 
00043        /*
00044         * FIXME: in case part of the operation cannot be performed
00045         * (missing mapping, SQL write fails or so) the entire operation
00046         * should be rolled-back
00047         */
00048        Debug( LDAP_DEBUG_TRACE, "==>backsql_modify(): modifying entry \"%s\"\n",
00049               op->o_req_ndn.bv_val, 0, 0 );
00050 
00051        rs->sr_err = backsql_get_db_conn( op, &dbh );
00052        if ( rs->sr_err != LDAP_SUCCESS ) {
00053               Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
00054                      "could not get connection handle - exiting\n", 
00055                      0, 0, 0 );
00056               /*
00057                * FIXME: we don't want to send back 
00058                * excessively detailed messages
00059                */
00060               rs->sr_text = ( rs->sr_err == LDAP_OTHER )
00061                      ? "SQL-backend error" : NULL;
00062               goto done;
00063        }
00064 
00065        bsi.bsi_e = &m;
00066        rs->sr_err = backsql_init_search( &bsi, &op->o_req_ndn,
00067                      LDAP_SCOPE_BASE, 
00068                      (time_t)(-1), NULL, dbh, op, rs,
00069                      slap_anlist_all_attributes,
00070                      ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY | BACKSQL_ISF_GET_OC ) );
00071        switch ( rs->sr_err ) {
00072        case LDAP_SUCCESS:
00073               break;
00074 
00075        case LDAP_REFERRAL:
00076               if ( manageDSAit && !BER_BVISNULL( &bsi.bsi_e->e_nname ) &&
00077                             dn_match( &op->o_req_ndn, &bsi.bsi_e->e_nname ) )
00078               {
00079                      rs->sr_err = LDAP_SUCCESS;
00080                      rs->sr_text = NULL;
00081                      rs->sr_matched = NULL;
00082                      if ( rs->sr_ref ) {
00083                             ber_bvarray_free( rs->sr_ref );
00084                             rs->sr_ref = NULL;
00085                      }
00086                      break;
00087               }
00088               e = &m;
00089               /* fallthru */
00090 
00091        default:
00092               Debug( LDAP_DEBUG_TRACE, "backsql_modify(): "
00093                      "could not retrieve modifyDN ID - no such entry\n", 
00094                      0, 0, 0 );
00095               if ( !BER_BVISNULL( &m.e_nname ) ) {
00096                      /* FIXME: should always be true! */
00097                      e = &m;
00098 
00099               } else {
00100                      e = NULL;
00101               }
00102               goto done;
00103        }
00104 
00105        Debug( LDAP_DEBUG_TRACE, "   backsql_modify(): "
00106               "modifying entry \"%s\" (id=" BACKSQL_IDFMT ")\n", 
00107               bsi.bsi_base_id.eid_dn.bv_val,
00108               BACKSQL_IDARG(bsi.bsi_base_id.eid_id), 0 );
00109 
00110        if ( get_assert( op ) &&
00111                      ( test_filter( op, &m, get_assertion( op ) )
00112                        != LDAP_COMPARE_TRUE ))
00113        {
00114               rs->sr_err = LDAP_ASSERTION_FAILED;
00115               e = &m;
00116               goto done;
00117        }
00118 
00119        slap_mods_opattrs( op, &op->orm_modlist, 1 );
00120 
00121        assert( bsi.bsi_base_id.eid_oc != NULL );
00122        oc = bsi.bsi_base_id.eid_oc;
00123 
00124        if ( !acl_check_modlist( op, &m, op->orm_modlist ) ) {
00125               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
00126               e = &m;
00127               goto done;
00128        }
00129 
00130        rs->sr_err = backsql_modify_internal( op, rs, dbh, oc,
00131                      &bsi.bsi_base_id, op->orm_modlist );
00132        if ( rs->sr_err != LDAP_SUCCESS ) {
00133               e = &m;
00134               goto do_transact;
00135        }
00136 
00137        if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
00138               char          textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
00139 
00140               backsql_entry_clean( op, &m );
00141 
00142               bsi.bsi_e = &m;
00143               rs->sr_err = backsql_id2entry( &bsi, &bsi.bsi_base_id );
00144               if ( rs->sr_err != LDAP_SUCCESS ) {
00145                      e = &m;
00146                      goto do_transact;
00147               }
00148 
00149               rs->sr_err = entry_schema_check( op, &m, NULL, 0, 0, NULL,
00150                      &rs->sr_text, textbuf, sizeof( textbuf ) );
00151               if ( rs->sr_err != LDAP_SUCCESS ) {
00152                      Debug( LDAP_DEBUG_TRACE, "   backsql_modify(\"%s\"): "
00153                             "entry failed schema check -- aborting\n",
00154                             m.e_name.bv_val, 0, 0 );
00155                      e = NULL;
00156                      goto do_transact;
00157               }
00158        }
00159 
00160 do_transact:;
00161        /*
00162         * Commit only if all operations succeed
00163         */
00164        if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
00165               assert( e == NULL );
00166               CompletionType = SQL_COMMIT;
00167        }
00168 
00169        SQLTransact( SQL_NULL_HENV, dbh, CompletionType );
00170 
00171 done:;
00172        if ( e != NULL ) {
00173               if ( !access_allowed( op, e, slap_schema.si_ad_entry, NULL,
00174                                    ACL_DISCLOSE, NULL ) )
00175               {
00176                      rs->sr_err = LDAP_NO_SUCH_OBJECT;
00177                      rs->sr_text = NULL;
00178                      rs->sr_matched = NULL;
00179                      if ( rs->sr_ref ) {
00180                             ber_bvarray_free( rs->sr_ref );
00181                             rs->sr_ref = NULL;
00182                      }
00183               }
00184        }
00185 
00186        if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
00187               rs->sr_err = LDAP_X_NO_OPERATION;
00188        }
00189 
00190        send_ldap_result( op, rs );
00191        slap_graduate_commit_csn( op );
00192 
00193        if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
00194               (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
00195        }
00196 
00197        if ( !BER_BVISNULL( &m.e_nname ) ) {
00198               backsql_entry_clean( op, &m );
00199        }
00200 
00201        if ( bsi.bsi_attrs != NULL ) {
00202               op->o_tmpfree( bsi.bsi_attrs, op->o_tmpmemctx );
00203        }
00204 
00205        if ( rs->sr_ref ) {
00206               ber_bvarray_free( rs->sr_ref );
00207               rs->sr_ref = NULL;
00208        }
00209 
00210        Debug( LDAP_DEBUG_TRACE, "<==backsql_modify()\n", 0, 0, 0 );
00211 
00212        return rs->sr_err;
00213 }
00214