]> git.sur5r.net Git - openldap/blobdiff - servers/slapd/back-sql/add.c
Merge remote branch 'origin/mdb.master'
[openldap] / servers / slapd / back-sql / add.c
index 9a6e6a56f9ff51be5879b2250d788e1dfd12e97d..7fd485988bc71581d74f17d57d7b1784c65ef5c2 100644 (file)
@@ -1,7 +1,7 @@
 /* $OpenLDAP$ */
 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  *
- * Copyright 1999-2005 The OpenLDAP Foundation.
+ * Copyright 1999-2012 The OpenLDAP Foundation.
  * Portions Copyright 1999 Dmitry Kovalev.
  * Portions Copyright 2002 Pierangelo Masarati.
  * Portions Copyright 2004 Mark Adamson.
@@ -131,7 +131,7 @@ backsql_modify_delete_all_values(
        {
                int             i;
                /* first parameter no, parameter order */
-               SQLUSMALLINT    pno,
+               SQLUSMALLINT    pno = 0,
                                po = 0;
                /* procedure return code */
                int             prc = LDAP_SUCCESS;
@@ -190,17 +190,12 @@ backsql_modify_delete_all_values(
                                rs->sr_err = LDAP_OTHER;
                                goto done;
                        }
-#ifdef BACKSQL_ARBITRARY_KEY
-                       Debug( LDAP_DEBUG_TRACE,
-                               "   backsql_modify_delete_all_values() "
-                               "arg%d=%s\n",
-                               pno + 1 + po, e_id->eid_keyval.bv_val, 0 );
-#else /* ! BACKSQL_ARBITRARY_KEY */
+
                        Debug( LDAP_DEBUG_TRACE,
                                "   backsql_modify_delete_all_values() "
-                               "arg%d=%lu\n",
-                               pno + 1 + po, e_id->eid_keyval, 0 );
-#endif /* ! BACKSQL_ARBITRARY_KEY */
+                               "arg(%d)=" BACKSQL_IDFMT "\n",
+                               pno + 1 + po,
+                               BACKSQL_IDARG(e_id->eid_keyval), 0 );
 
                        /*
                         * check for syntax needed here 
@@ -225,7 +220,7 @@ backsql_modify_delete_all_values(
         
                        Debug( LDAP_DEBUG_TRACE, 
                                "   backsql_modify_delete_all_values(): "
-                               "arg%d=%s; executing \"%s\"\n",
+                               "arg(%d)=%s; executing \"%s\"\n",
                                pno + 2 - po, row.cols[ i ],
                                at->bam_delete_proc );
                        rc = SQLExecute( sth );
@@ -394,6 +389,7 @@ del_all:
                 */
                case LDAP_MOD_ADD:
                /* case SLAP_MOD_SOFTADD: */
+               /* case SLAP_MOD_ADD_IF_NOT_PRESENT: */
 add_only:;
                        if ( at->bam_add_proc == NULL ) {
                                Debug( LDAP_DEBUG_TRACE,
@@ -477,17 +473,12 @@ add_only:;
                                        rs->sr_err = LDAP_OTHER;
                                        goto done;
                                }
-#ifdef BACKSQL_ARBITRARY_KEY
-                               Debug( LDAP_DEBUG_TRACE,
-                                       "   backsql_modify_internal(): "
-                                       "arg%d=\"%s\"\n", 
-                                       pno + 1 + po, e_id->eid_keyval.bv_val, 0 );
-#else /* ! BACKSQL_ARBITRARY_KEY */
+
                                Debug( LDAP_DEBUG_TRACE,
                                        "   backsql_modify_internal(): "
-                                       "arg%d=\"%lu\"\n", 
-                                       pno + 1 + po, e_id->eid_keyval, 0 );
-#endif /* ! BACKSQL_ARBITRARY_KEY */
+                                       "arg(%d)=" BACKSQL_IDFMT "\n", 
+                                       pno + 1 + po,
+                                       BACKSQL_IDARG(e_id->eid_keyval), 0 );
 
                                /*
                                 * check for syntax needed here
@@ -510,7 +501,7 @@ add_only:;
                                }
                                Debug( LDAP_DEBUG_TRACE,
                                        "   backsql_modify_internal(): "
-                                       "arg%d=\"%s\"; executing \"%s\"\n", 
+                                       "arg(%d)=\"%s\"; executing \"%s\"\n", 
                                        pno + 2 - po, at_val->bv_val,
                                        at->bam_add_proc );
 
@@ -551,6 +542,7 @@ add_only:;
                        break;
                        
                case LDAP_MOD_DELETE:
+               /* case SLAP_MOD_SOFTDEL: */
                        if ( at->bam_delete_proc == NULL ) {
                                Debug( LDAP_DEBUG_TRACE,
                                        "   backsql_modify_internal(): "
@@ -640,17 +632,12 @@ add_only:;
                                        rs->sr_err = LDAP_OTHER;
                                        goto done;
                                }
-#ifdef BACKSQL_ARBITRARY_KEY
-                               Debug( LDAP_DEBUG_TRACE,
-                                       "   backsql_modify_internal(): "
-                                       "arg%d=\"%s\"\n", 
-                                       pno + 1 + po, e_id->eid_keyval.bv_val, 0 );
-#else /* ! BACKSQL_ARBITRARY_KEY */
+
                                Debug( LDAP_DEBUG_TRACE,
                                        "   backsql_modify_internal(): "
-                                       "arg%d=\"%lu\"\n", 
-                                       pno + 1 + po, e_id->eid_keyval, 0 );
-#endif /* ! BACKSQL_ARBITRARY_KEY */
+                                       "arg(%d)=" BACKSQL_IDFMT "\n", 
+                                       pno + 1 + po,
+                                       BACKSQL_IDARG(e_id->eid_keyval), 0 );
 
                                /*
                                 * check for syntax needed here 
@@ -739,7 +726,7 @@ backsql_add_attr(
        SQLHDBC                 dbh,
        backsql_oc_map_rec      *oc,
        Attribute               *at,
-       unsigned long           new_keyval )
+       backsql_key_t           new_keyval )
 {
        backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        backsql_at_map_rec      *at_rec = NULL;
@@ -794,7 +781,7 @@ backsql_add_attr(
                int             prc = LDAP_SUCCESS;
                /* first parameter #, parameter order */
                SQLUSMALLINT    pno, po;
-               char            logbuf[] = "val[18446744073709551615UL], id=18446744073709551615UL";
+               char            logbuf[ STRLENOF("val[], id=") + 2*LDAP_PVT_INTTYPE_CHARS(unsigned long)];
                
                /*
                 * Do not deal with the objectClass that is used
@@ -819,7 +806,7 @@ backsql_add_attr(
                        if ( rc != SQL_SUCCESS ) {
                                Debug( LDAP_DEBUG_TRACE,
                                        "   backsql_add_attr(): "
-                                       "error binding output parameter for %s[%d]\n",
+                                       "error binding output parameter for %s[%lu]\n",
                                        at_rec->bam_ad->ad_cname.bv_val, i, 0 );
                                backsql_PrintErrors( bi->sql_db_env, dbh, 
                                        sth, rc );
@@ -835,12 +822,12 @@ backsql_add_attr(
 
                po = ( BACKSQL_IS_ADD( at_rec->bam_param_order ) ) > 0;
                currpos = pno + 1 + po;
-               rc = backsql_BindParamInt( sth, currpos,
+               rc = backsql_BindParamNumID( sth, currpos,
                                SQL_PARAM_INPUT, &new_keyval );
                if ( rc != SQL_SUCCESS ) {
                        Debug( LDAP_DEBUG_TRACE,
                                "   backsql_add_attr(): "
-                               "error binding keyval parameter for %s[%d]\n",
+                               "error binding keyval parameter for %s[%lu]\n",
                                at_rec->bam_ad->ad_cname.bv_val, i, 0 );
                        backsql_PrintErrors( bi->sql_db_env, dbh, 
                                sth, rc );
@@ -861,7 +848,7 @@ backsql_add_attr(
                if ( rc != SQL_SUCCESS ) {
                        Debug( LDAP_DEBUG_TRACE,
                                "   backsql_add_attr(): "
-                               "error binding value parameter for %s[%d]\n",
+                               "error binding value parameter for %s[%lu]\n",
                                at_rec->bam_ad->ad_cname.bv_val, i, 0 );
                        backsql_PrintErrors( bi->sql_db_env, dbh, 
                                sth, rc );
@@ -872,12 +859,14 @@ backsql_add_attr(
                }
 
 #ifdef LDAP_DEBUG
-               snprintf( logbuf, sizeof( logbuf ), "val[%lu], id=%lu",
-                               i, new_keyval );
-               Debug( LDAP_DEBUG_TRACE, "   backsql_add_attr(\"%s\"): "
-                       "executing \"%s\" %s\n", 
-                       op->ora_e->e_name.bv_val,
-                       at_rec->bam_add_proc, logbuf );
+               if ( LogTest( LDAP_DEBUG_TRACE ) ) {
+                       snprintf( logbuf, sizeof( logbuf ), "val[%lu], id=" BACKSQL_IDNUMFMT,
+                                       i, new_keyval );
+                       Debug( LDAP_DEBUG_TRACE, "   backsql_add_attr(\"%s\"): "
+                               "executing \"%s\" %s\n", 
+                               op->ora_e->e_name.bv_val,
+                               at_rec->bam_add_proc, logbuf );
+               }
 #endif
                rc = SQLExecute( sth );
                if ( rc == SQL_SUCCESS && prc == LDAP_SUCCESS ) {
@@ -917,28 +906,34 @@ backsql_add( Operation *op, SlapReply *rs )
        backsql_info            *bi = (backsql_info*)op->o_bd->be_private;
        SQLHDBC                 dbh = SQL_NULL_HDBC;
        SQLHSTMT                sth = SQL_NULL_HSTMT;
-       unsigned long           new_keyval = 0;
+       backsql_key_t           new_keyval = 0;
        RETCODE                 rc;
        backsql_oc_map_rec      *oc = NULL;
        backsql_srch_info       bsi = { 0 };
        Entry                   p = { 0 }, *e = NULL;
        Attribute               *at,
                                *at_objectClass = NULL;
+       ObjectClass             *soc = NULL;
        struct berval           scname = BER_BVNULL;
        struct berval           pdn;
        struct berval           realdn = BER_BVNULL;
        int                     colnum;
        slap_mask_t             mask;
 
+       char                    textbuf[ SLAP_TEXT_BUFLEN ];
+       size_t                  textlen = sizeof( textbuf );
+
 #ifdef BACKSQL_SYNCPROV
        /*
         * NOTE: fake successful result to force contextCSN to be bumped up
         */
        if ( op->o_sync ) {
-               char            buf[ LDAP_LUTIL_CSNSTR_BUFSIZE ];
-               struct berval   csn = BER_BVNULL;
+               char            buf[ LDAP_PVT_CSNSTR_BUFSIZE ];
+               struct berval   csn;
 
-               slap_get_csn( op, buf, sizeof( buf ), &csn, 1 );
+               csn.bv_val = buf;
+               csn.bv_len = sizeof( buf );
+               slap_get_csn( op, &csn, 1 );
 
                rs->sr_err = LDAP_SUCCESS;
                send_ldap_result( op, rs );
@@ -956,7 +951,7 @@ backsql_add( Operation *op, SlapReply *rs )
        if ( BACKSQL_CHECK_SCHEMA( bi ) ) {
                char            textbuf[ SLAP_TEXT_BUFLEN ] = { '\0' };
 
-               rs->sr_err = entry_schema_check( op, op->ora_e, NULL, 0,
+               rs->sr_err = entry_schema_check( op, op->ora_e, NULL, 0, 1, NULL,
                        &rs->sr_text, textbuf, sizeof( textbuf ) );
                if ( rs->sr_err != LDAP_SUCCESS ) {
                        Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
@@ -967,6 +962,19 @@ backsql_add( Operation *op, SlapReply *rs )
                }
        }
 
+       slap_add_opattrs( op, &rs->sr_text, textbuf, textlen, 1 );
+
+       if ( get_assert( op ) &&
+               ( test_filter( op, op->ora_e, get_assertion( op )) != LDAP_COMPARE_TRUE ))
+       {
+               Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
+                       "assertion control failed -- aborting\n",
+                       op->ora_e->e_name.bv_val, 0, 0 );
+               e = NULL;
+               rs->sr_err = LDAP_ASSERTION_FAILED;
+               goto done;
+       }
+
        /* search structuralObjectClass */
        for ( at = op->ora_e->e_attrs; at != NULL; at = at->a_next ) {
                if ( at->a_desc == slap_schema.si_ad_structuralObjectClass ) {
@@ -995,8 +1003,8 @@ backsql_add( Operation *op, SlapReply *rs )
                        goto done;
                }
 
-               rs->sr_err = structural_class( at->a_vals, &scname, NULL,
-                               &text, buf, sizeof( buf ) );
+               rs->sr_err = structural_class( at->a_vals, &soc, NULL,
+                               &text, buf, sizeof( buf ), op->o_tmpmemctx );
                if ( rs->sr_err != LDAP_SUCCESS ) {
                        Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
                                "%s (%d)\n",
@@ -1004,6 +1012,7 @@ backsql_add( Operation *op, SlapReply *rs )
                        e = NULL;
                        goto done;
                }
+               scname = soc->soc_cname;
 
        } else {
                scname = at->a_vals[0];
@@ -1048,6 +1057,16 @@ backsql_add( Operation *op, SlapReply *rs )
                goto done;
        }
 
+       /* check write access */
+       if ( !access_allowed_mask( op, op->ora_e,
+                               slap_schema.si_ad_entry,
+                               NULL, ACL_WADD, NULL, &mask ) )
+       {
+               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
+               e = op->ora_e;
+               goto done;
+       }
+
        rs->sr_err = backsql_get_db_conn( op, &dbh );
        if ( rs->sr_err != LDAP_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
@@ -1061,7 +1080,7 @@ backsql_add( Operation *op, SlapReply *rs )
 
        /*
         * Check if entry exists
-       *
+        *
         * NOTE: backsql_api_dn2odbc() is called explicitly because
         * we need the mucked DN to pass it to the create procedure.
         */
@@ -1094,53 +1113,34 @@ backsql_add( Operation *op, SlapReply *rs )
 
        } else {
                dnParent( &op->ora_e->e_nname, &pdn );
-       }
-
-       /*
-        * Get the parent
-        */
-       bsi.bsi_e = &p;
-       rs->sr_err = backsql_init_search( &bsi, &pdn,
-                       LDAP_SCOPE_BASE, 
-                       SLAP_NO_LIMIT, SLAP_NO_LIMIT,
-                       (time_t)(-1), NULL, dbh, op, rs, slap_anlist_no_attrs,
-                       ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
-       if ( rs->sr_err != LDAP_SUCCESS ) {
-               Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
-                       "could not retrieve addDN parent "
-                       "\"%s\" ID - %s matched=\"%s\"\n", 
-                       pdn.bv_val,
-                       rs->sr_err == LDAP_REFERRAL ? "referral" : "no such entry",
-                       rs->sr_matched ? rs->sr_matched : "(null)" );
-               e = &p;
-               goto done;
-       }
-
-       /* check "children" pseudo-attribute access to parent */
-       if ( !access_allowed( op, &p, slap_schema.si_ad_children,
-                               NULL, ACL_WADD, NULL ) )
-       {
-               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
-               e = &p;
-               goto done;
-       }
 
-       if ( get_assert( op ) &&
-               ( test_filter( op, op->oq_add.rs_e, get_assertion( op ) )
-                       != LDAP_COMPARE_TRUE ) )
-       {
-               rs->sr_err = LDAP_ASSERTION_FAILED;
-               e = op->ora_e;
-               goto done;
-       }
+               /*
+                * Get the parent
+                */
+               bsi.bsi_e = &p;
+               rs->sr_err = backsql_init_search( &bsi, &pdn,
+                               LDAP_SCOPE_BASE, 
+                               (time_t)(-1), NULL, dbh, op, rs, slap_anlist_no_attrs,
+                               ( BACKSQL_ISF_MATCHED | BACKSQL_ISF_GET_ENTRY ) );
+               if ( rs->sr_err != LDAP_SUCCESS ) {
+                       Debug( LDAP_DEBUG_TRACE, "backsql_add(): "
+                               "could not retrieve addDN parent "
+                               "\"%s\" ID - %s matched=\"%s\"\n", 
+                               pdn.bv_val,
+                               rs->sr_err == LDAP_REFERRAL ? "referral" : "no such entry",
+                               rs->sr_matched ? rs->sr_matched : "(null)" );
+                       e = &p;
+                       goto done;
+               }
 
-       if ( !access_allowed_mask( op, op->ora_e,
-                               slap_schema.si_ad_entry,
-                               NULL, ACL_WADD, NULL, &mask ) )
-       {
-               rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
-               e = op->ora_e;
-               goto done;
+               /* check "children" pseudo-attribute access to parent */
+               if ( !access_allowed( op, &p, slap_schema.si_ad_children,
+                                       NULL, ACL_WADD, NULL ) )
+               {
+                       rs->sr_err = LDAP_INSUFFICIENT_ACCESS;
+                       e = &p;
+                       goto done;
+               }
        }
 
        /*
@@ -1159,7 +1159,7 @@ backsql_add( Operation *op, SlapReply *rs )
 
        colnum = 1;
        if ( BACKSQL_IS_ADD( oc->bom_expect_return ) ) {
-               rc = backsql_BindParamInt( sth, 1, SQL_PARAM_OUTPUT, &new_keyval );
+               rc = backsql_BindParamNumID( sth, 1, SQL_PARAM_OUTPUT, &new_keyval );
                if ( rc != SQL_SUCCESS ) {
                        Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
                                "error binding keyval parameter "
@@ -1219,7 +1219,7 @@ backsql_add( Operation *op, SlapReply *rs )
 
        if ( !BACKSQL_IS_ADD( oc->bom_expect_return ) ) {
                SWORD           ncols;
-               SQLINTEGER      value_len;
+               SQLLEN          value_len;
 
                if ( BACKSQL_CREATE_NEEDS_SELECT( bi ) ) {
                        SQLFreeStmt( sth, SQL_DROP );
@@ -1310,7 +1310,7 @@ backsql_add( Operation *op, SlapReply *rs )
        SQLFreeStmt( sth, SQL_DROP );
 
        Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
-               "create_proc returned keyval=%ld\n",
+               "create_proc returned keyval=" BACKSQL_IDNUMFMT "\n",
                op->ora_e->e_name.bv_val, new_keyval, 0 );
 
        rc = backsql_Prepare( dbh, &sth, bi->sql_insentry_stmt, 0 );
@@ -1337,7 +1337,7 @@ backsql_add( Operation *op, SlapReply *rs )
                goto done;
        }
 
-       rc = backsql_BindParamInt( sth, 2, SQL_PARAM_INPUT, &oc->bom_id );
+       rc = backsql_BindParamNumID( sth, 2, SQL_PARAM_INPUT, &oc->bom_id );
        if ( rc != SQL_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
                        "error binding objectClass ID parameter "
@@ -1371,7 +1371,7 @@ backsql_add( Operation *op, SlapReply *rs )
                goto done;
        }
 
-       rc = backsql_BindParamInt( sth, 4, SQL_PARAM_INPUT, &new_keyval );
+       rc = backsql_BindParamNumID( sth, 4, SQL_PARAM_INPUT, &new_keyval );
        if ( rc != SQL_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
                        "error binding entry ID parameter "
@@ -1388,17 +1388,17 @@ backsql_add( Operation *op, SlapReply *rs )
                goto done;
        }
 
-       Debug( LDAP_DEBUG_TRACE, "   backsql_add(): executing \"%s\" for dn \"%s\"\n",
-                       bi->sql_insentry_stmt, op->ora_e->e_name.bv_val, 0 );
-#ifdef BACKSQL_ARBITRARY_KEY
-       Debug( LDAP_DEBUG_TRACE, "                  for oc_map_id=%ld, "
-                       "p_id=%s, keyval=%ld\n",
-                       oc->bom_id, bsi.bsi_base_id.eid_id.bv_val, new_keyval );
-#else /* ! BACKSQL_ARBITRARY_KEY */
-       Debug( LDAP_DEBUG_TRACE, "                  for oc_map_id=%ld, "
-                       "p_id=%ld, keyval=%ld\n",
-                       oc->bom_id, bsi.bsi_base_id.eid_id, new_keyval );
-#endif /* ! BACKSQL_ARBITRARY_KEY */
+       if ( LogTest( LDAP_DEBUG_TRACE ) ) {
+               char buf[ SLAP_TEXT_BUFLEN ];
+
+               snprintf( buf, sizeof(buf),
+                       "executing \"%s\" for dn=\"%s\"  oc_map_id=" BACKSQL_IDNUMFMT " p_id=" BACKSQL_IDFMT " keyval=" BACKSQL_IDNUMFMT,
+                       bi->sql_insentry_stmt, op->ora_e->e_name.bv_val,
+                       oc->bom_id, BACKSQL_IDARG(bsi.bsi_base_id.eid_id),
+                       new_keyval );
+               Debug( LDAP_DEBUG_TRACE, "   backsql_add(): %s\n", buf, 0, 0 );
+       }
+
        rc = SQLExecute( sth );
        if ( rc != SQL_SUCCESS ) {
                Debug( LDAP_DEBUG_TRACE, "   backsql_add(\"%s\"): "
@@ -1463,6 +1463,7 @@ done:;
                SQLUSMALLINT    CompletionType = SQL_ROLLBACK;
 
                if ( rs->sr_err == LDAP_SUCCESS && !op->o_noop ) {
+                       assert( e == NULL );
                        CompletionType = SQL_COMMIT;
                }
 
@@ -1480,7 +1481,6 @@ done:;
         * in deleting that row.
         */
 
-#ifdef SLAP_ACL_HONOR_DISCLOSE
        if ( e != NULL ) {
                int     disclose = 1;
 
@@ -1505,9 +1505,13 @@ done:;
                        }
                }
        }
-#endif /* SLAP_ACL_HONOR_DISCLOSE */
+
+       if ( op->o_noop && rs->sr_err == LDAP_SUCCESS ) {
+               rs->sr_err = LDAP_X_NO_OPERATION;
+       }
 
        send_ldap_result( op, rs );
+       slap_graduate_commit_csn( op );
 
        if ( !BER_BVISNULL( &realdn )
                        && realdn.bv_val != op->ora_e->e_name.bv_val )
@@ -1516,7 +1520,7 @@ done:;
        }
 
        if ( !BER_BVISNULL( &bsi.bsi_base_id.eid_ndn ) ) {
-               (void)backsql_free_entryID( op, &bsi.bsi_base_id, 0 );
+               (void)backsql_free_entryID( &bsi.bsi_base_id, 0, op->o_tmpmemctx );
        }
 
        if ( !BER_BVISNULL( &p.e_nname ) ) {