s4: fix various warnings (not "const" related ones)
[sfrench/samba-autobuild/.git] / source4 / torture / rpc / samsync.c
index 2d2aaa80d6e3633e5f2f5a42625ff67f360af841..2ee8e59e5aec6e2e2577825af1bb75b3a2bee60d 100644 (file)
 #include "../lib/crypto/crypto.h"
 #include "system/time.h"
 #include "torture/rpc/rpc.h"
-#include "auth/gensec/schannel_proto.h"
+#include "../libcli/auth/schannel.h"
 #include "auth/gensec/gensec.h"
 #include "libcli/auth/libcli_auth.h"
+#include "libcli/samsync/samsync.h"
 #include "libcli/security/security.h"
 #include "librpc/gen_ndr/ndr_netlogon.h"
 #include "librpc/gen_ndr/ndr_netlogon_c.h"
@@ -47,7 +48,7 @@
   try a netlogon SamLogon
 */
 static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
-                             struct creds_CredentialState *creds, 
+                             struct netlogon_creds_CredentialState *creds, 
                              const char *domain, const char *account_name,
                              const char *workstation, 
                              struct samr_Password *lm_hash, 
@@ -58,6 +59,9 @@ static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
        struct netr_LogonSamLogon r;
        struct netr_Authenticator auth, auth2;
        struct netr_NetworkInfo ninfo;
+       union netr_LogonLevel logon;
+       union netr_Validation validation;
+       uint8_t authoritative;
 
        ninfo.identity_info.domain_name.string = domain;
        ninfo.identity_info.parameter_control = 0;
@@ -85,26 +89,30 @@ static NTSTATUS test_SamLogon(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
                ninfo.lm.data = NULL;
        }
 
+       logon.network = &ninfo;
+
        r.in.server_name = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
        r.in.computer_name = workstation;
        r.in.credential = &auth;
        r.in.return_authenticator = &auth2;
        r.in.logon_level = 2;
-       r.in.logon.network = &ninfo;
+       r.in.logon = &logon;
+       r.out.validation = &validation;
+       r.out.authoritative = &authoritative;
 
        ZERO_STRUCT(auth2);
-       creds_client_authenticator(creds, &auth);
+       netlogon_creds_client_authenticator(creds, &auth);
        
        r.in.validation_level = 3;
        
        status = dcerpc_netr_LogonSamLogon(p, mem_ctx, &r);
 
-       if (!creds_client_check(creds, &r.out.return_authenticator->cred)) {
+       if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
                printf("Credential chaining failed\n");
        }
 
        if (info3) {
-               *info3 = r.out.validation.sam3;
+               *info3 = validation.sam3;
        }
 
        return status;
@@ -116,8 +124,8 @@ struct samsync_state {
        const char *domain_name[2];
        struct samsync_secret *secrets;
        struct samsync_trusted_domain *trusted_domains;
-       struct creds_CredentialState *creds;
-       struct creds_CredentialState *creds_netlogon_wksta;
+       struct netlogon_creds_CredentialState *creds;
+       struct netlogon_creds_CredentialState *creds_netlogon_wksta;
        struct policy_handle *connect_handle;
        struct policy_handle *domain_handle[2];
        struct dom_sid *sid[2];
@@ -144,17 +152,19 @@ struct samsync_trusted_domain {
 static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx, 
                                                 struct samsync_state *samsync_state, 
                                                 const char *domain, 
-                                                struct dom_sid **sid)
+                                                struct dom_sid **sid_p)
 {
        struct lsa_String name;
        struct samr_OpenDomain o;
        struct samr_LookupDomain l;
+       struct dom_sid2 *sid = NULL;
        struct policy_handle *domain_handle = talloc(mem_ctx, struct policy_handle);
        NTSTATUS nt_status;
 
        name.string = domain;
        l.in.connect_handle = samsync_state->connect_handle;
        l.in.domain_name = &name;
+       l.out.sid = &sid;
 
        nt_status = dcerpc_samr_LookupDomain(samsync_state->p_samr, mem_ctx, &l);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -164,11 +174,11 @@ static struct policy_handle *samsync_open_domain(TALLOC_CTX *mem_ctx,
 
        o.in.connect_handle = samsync_state->connect_handle;
        o.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
-       o.in.sid = l.out.sid;
+       o.in.sid = *l.out.sid;
        o.out.domain_handle = domain_handle;
        
-       if (sid) {
-               *sid = l.out.sid;
+       if (sid_p) {
+               *sid_p = *l.out.sid;
        }
 
        nt_status = dcerpc_samr_OpenDomain(samsync_state->p_samr, mem_ctx, &o);
@@ -185,10 +195,12 @@ static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
                                                        struct policy_handle *handle) 
 {
        struct samr_QuerySecurity r;
+       struct sec_desc_buf *sdbuf = NULL;
        NTSTATUS status;
 
        r.in.handle = handle;
        r.in.sec_info = 0x7;
+       r.out.sdbuf = &sdbuf;
 
        status = dcerpc_samr_QuerySecurity(samsync_state->p_samr, mem_ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
@@ -196,7 +208,7 @@ static struct sec_desc_buf *samsync_query_samr_sec_desc(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       return r.out.sdbuf;
+       return sdbuf;
 }
 
 static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx, 
@@ -204,10 +216,12 @@ static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
                                                       struct policy_handle *handle) 
 {
        struct lsa_QuerySecurity r;
+       struct sec_desc_buf *sdbuf = NULL;
        NTSTATUS status;
 
        r.in.handle = handle;
        r.in.sec_info = 0x7;
+       r.out.sdbuf = &sdbuf;
 
        status = dcerpc_lsa_QuerySecurity(samsync_state->p_lsa, mem_ctx, &r);
        if (!NT_STATUS_IS_OK(status)) {
@@ -215,7 +229,7 @@ static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
-       return r.out.sdbuf;
+       return sdbuf;
 }
 
 #define TEST_UINT64_EQUAL(i1, i2) do {\
@@ -251,6 +265,15 @@ static struct sec_desc_buf *samsync_query_lsa_sec_desc(TALLOC_CTX *mem_ctx,
        } \
 } while (0)
 
+#define TEST_BINARY_STRING_EQUAL(s1, s2) do {\
+       if (!((!s1.array || s1.array[0]=='\0') && (!s2.array || s2.array[0]=='\0')) \
+           && memcmp(s1.array, s2.array, s1.length * 2) != 0) {\
+             printf("%s: string mismatch: " #s1 ":%s != " #s2 ": %s\n", \
+                    __location__, (const char *)s1.array, (const char *)s2.array);\
+             ret = false;\
+       } \
+} while (0)
+
 #define TEST_SID_EQUAL(s1, s2) do {\
        if (!dom_sid_equal(s1, s2)) {\
              printf("%s: dom_sid mismatch: " #s1 ":%s != " #s2 ": %s\n", \
@@ -285,6 +308,7 @@ static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *sam
        struct netr_DELTA_DOMAIN *domain = delta->delta_union.domain;
        struct dom_sid *dom_sid;
        struct samr_QueryDomainInfo q[14]; /* q[0] will be unused simple for clarity */
+       union samr_DomainInfo *info[14];
        uint16_t levels[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 13};
        NTSTATUS nt_status;
        int i;
@@ -332,8 +356,10 @@ static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *sam
               (long long)samsync_state->seq_num[database_id]);
 
        for (i=0;i<ARRAY_SIZE(levels);i++) {
+
                q[levels[i]].in.domain_handle = samsync_state->domain_handle[database_id];
                q[levels[i]].in.level = levels[i];
+               q[levels[i]].out.info = &info[levels[i]];
 
                nt_status = dcerpc_samr_QueryDomainInfo(samsync_state->p_samr, mem_ctx, &q[levels[i]]);
 
@@ -344,23 +370,23 @@ static bool samsync_handle_domain(TALLOC_CTX *mem_ctx, struct samsync_state *sam
                }
        }
 
-       TEST_STRING_EQUAL(q[5].out.info->info5.domain_name, domain->domain_name);
+       TEST_STRING_EQUAL(info[5]->info5.domain_name, domain->domain_name);
        
-       TEST_STRING_EQUAL(q[2].out.info->general.oem_information, domain->oem_information);
-       TEST_STRING_EQUAL(q[4].out.info->oem.oem_information, domain->oem_information);
-       TEST_TIME_EQUAL(q[2].out.info->general.force_logoff_time, domain->force_logoff_time);
-       TEST_TIME_EQUAL(q[3].out.info->info3.force_logoff_time, domain->force_logoff_time);
+       TEST_STRING_EQUAL(info[2]->general.oem_information, domain->oem_information);
+       TEST_STRING_EQUAL(info[4]->oem.oem_information, domain->oem_information);
+       TEST_TIME_EQUAL(info[2]->general.force_logoff_time, domain->force_logoff_time);
+       TEST_TIME_EQUAL(info[3]->info3.force_logoff_time, domain->force_logoff_time);
 
-       TEST_TIME_EQUAL(q[1].out.info->info1.min_password_length, domain->min_password_length);
-       TEST_TIME_EQUAL(q[1].out.info->info1.password_history_length, domain->password_history_length);
-       TEST_TIME_EQUAL(q[1].out.info->info1.max_password_age, domain->max_password_age);
-       TEST_TIME_EQUAL(q[1].out.info->info1.min_password_age, domain->min_password_age);
+       TEST_TIME_EQUAL(info[1]->info1.min_password_length, domain->min_password_length);
+       TEST_TIME_EQUAL(info[1]->info1.password_history_length, domain->password_history_length);
+       TEST_TIME_EQUAL(info[1]->info1.max_password_age, domain->max_password_age);
+       TEST_TIME_EQUAL(info[1]->info1.min_password_age, domain->min_password_age);
 
-       TEST_UINT64_EQUAL(q[8].out.info->info8.sequence_num, 
+       TEST_UINT64_EQUAL(info[8]->info8.sequence_num,
                        domain->sequence_num);
-       TEST_TIME_EQUAL(q[8].out.info->info8.domain_create_time, 
+       TEST_TIME_EQUAL(info[8]->info8.domain_create_time,
                        domain->domain_create_time);
-       TEST_TIME_EQUAL(q[13].out.info->info13.domain_create_time, 
+       TEST_TIME_EQUAL(info[13]->info13.domain_create_time,
                        domain->domain_create_time);
 
        TEST_SEC_DESC_EQUAL(domain->sdbuf, samr, samsync_state->domain_handle[database_id]);
@@ -416,9 +442,12 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
 
        struct samr_OpenUser r;
        struct samr_QueryUserInfo q;
+       union samr_UserInfo *info;
        struct policy_handle user_handle;
 
        struct samr_GetGroupsForUser getgroups;
+       struct samr_RidWithAttributeArray *rids;
+
        if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
                printf("SamSync needs domain information before the users\n");
                return false;
@@ -437,6 +466,7 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
 
        q.in.user_handle = &user_handle;
        q.in.level = 21;
+       q.out.info = &info;
 
        TEST_SEC_DESC_EQUAL(user->sdbuf, samr, &user_handle);
 
@@ -448,6 +478,7 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
        }
 
        getgroups.in.user_handle = &user_handle;
+       getgroups.out.rids = &rids;
        
        nt_status = dcerpc_samr_GetGroupsForUser(samsync_state->p_samr, mem_ctx, &getgroups);
        if (!NT_STATUS_IS_OK(nt_status)) {
@@ -471,74 +502,72 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
                return false;
        }
 
-       TEST_STRING_EQUAL(q.out.info->info21.account_name, user->account_name);
-       TEST_STRING_EQUAL(q.out.info->info21.full_name, user->full_name);
-       TEST_INT_EQUAL(q.out.info->info21.rid, user->rid);
-       TEST_INT_EQUAL(q.out.info->info21.primary_gid, user->primary_gid);
-       TEST_STRING_EQUAL(q.out.info->info21.home_directory, user->home_directory);
-       TEST_STRING_EQUAL(q.out.info->info21.home_drive, user->home_drive);
-       TEST_STRING_EQUAL(q.out.info->info21.logon_script, user->logon_script);
-       TEST_STRING_EQUAL(q.out.info->info21.description, user->description);
-       TEST_STRING_EQUAL(q.out.info->info21.workstations, user->workstations);
+       TEST_STRING_EQUAL(info->info21.account_name, user->account_name);
+       TEST_STRING_EQUAL(info->info21.full_name, user->full_name);
+       TEST_INT_EQUAL(info->info21.rid, user->rid);
+       TEST_INT_EQUAL(info->info21.primary_gid, user->primary_gid);
+       TEST_STRING_EQUAL(info->info21.home_directory, user->home_directory);
+       TEST_STRING_EQUAL(info->info21.home_drive, user->home_drive);
+       TEST_STRING_EQUAL(info->info21.logon_script, user->logon_script);
+       TEST_STRING_EQUAL(info->info21.description, user->description);
+       TEST_STRING_EQUAL(info->info21.workstations, user->workstations);
 
-       TEST_TIME_EQUAL(q.out.info->info21.last_logon, user->last_logon);
-       TEST_TIME_EQUAL(q.out.info->info21.last_logoff, user->last_logoff);
+       TEST_TIME_EQUAL(info->info21.last_logon, user->last_logon);
+       TEST_TIME_EQUAL(info->info21.last_logoff, user->last_logoff);
 
 
-       TEST_INT_EQUAL(q.out.info->info21.logon_hours.units_per_week, 
+       TEST_INT_EQUAL(info->info21.logon_hours.units_per_week,
                       user->logon_hours.units_per_week);
        if (ret) {
-               if (memcmp(q.out.info->info21.logon_hours.bits, user->logon_hours.bits, 
-                          q.out.info->info21.logon_hours.units_per_week/8) != 0) {
+               if (memcmp(info->info21.logon_hours.bits, user->logon_hours.bits,
+                          info->info21.logon_hours.units_per_week/8) != 0) {
                        printf("Logon hours mismatch\n");
                        ret = false;
                }
        }
 
-       TEST_INT_EQUAL(q.out.info->info21.bad_password_count,
+       TEST_INT_EQUAL(info->info21.bad_password_count,
                       user->bad_password_count);
-       TEST_INT_EQUAL(q.out.info->info21.logon_count,
+       TEST_INT_EQUAL(info->info21.logon_count,
                       user->logon_count);
 
-       TEST_TIME_EQUAL(q.out.info->info21.last_password_change,
+       TEST_TIME_EQUAL(info->info21.last_password_change,
                       user->last_password_change);
-       TEST_TIME_EQUAL(q.out.info->info21.acct_expiry,
+       TEST_TIME_EQUAL(info->info21.acct_expiry,
                       user->acct_expiry);
 
-       TEST_INT_EQUAL((q.out.info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
+       TEST_INT_EQUAL((info->info21.acct_flags & ~ACB_PW_EXPIRED), user->acct_flags);
        if (user->acct_flags & ACB_PWNOEXP) {
-               if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
+               if (info->info21.acct_flags & ACB_PW_EXPIRED) {
                        printf("ACB flags mismatch: both expired and no expiry!\n");
                        ret = false;
                }
-               if (q.out.info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
+               if (info->info21.force_password_change != (NTTIME)0x7FFFFFFFFFFFFFFFULL) {
                        printf("ACB flags mismatch: no password expiry, but force password change 0x%016llx (%lld) != 0x%016llx (%lld)\n",
-                              (unsigned long long)q.out.info->info21.force_password_change, 
-                              (unsigned long long)q.out.info->info21.force_password_change,
+                              (unsigned long long)info->info21.force_password_change,
+                              (unsigned long long)info->info21.force_password_change,
                               (unsigned long long)0x7FFFFFFFFFFFFFFFULL, (unsigned long long)0x7FFFFFFFFFFFFFFFULL
                                );
                        ret = false;
                }
        }
 
-       TEST_INT_EQUAL(q.out.info->info21.nt_password_set, user->nt_password_present);
-       TEST_INT_EQUAL(q.out.info->info21.lm_password_set, user->lm_password_present);
-       TEST_INT_EQUAL(q.out.info->info21.password_expired, user->password_expired);
+       TEST_INT_EQUAL(info->info21.nt_password_set, user->nt_password_present);
+       TEST_INT_EQUAL(info->info21.lm_password_set, user->lm_password_present);
+       TEST_INT_EQUAL(info->info21.password_expired, user->password_expired);
 
-       TEST_STRING_EQUAL(q.out.info->info21.comment, user->comment);
-       TEST_STRING_EQUAL(q.out.info->info21.parameters, user->parameters);
+       TEST_STRING_EQUAL(info->info21.comment, user->comment);
+       TEST_BINARY_STRING_EQUAL(info->info21.parameters, user->parameters);
 
-       TEST_INT_EQUAL(q.out.info->info21.country_code, user->country_code);
-       TEST_INT_EQUAL(q.out.info->info21.code_page, user->code_page);
+       TEST_INT_EQUAL(info->info21.country_code, user->country_code);
+       TEST_INT_EQUAL(info->info21.code_page, user->code_page);
 
-       TEST_STRING_EQUAL(q.out.info->info21.profile_path, user->profile_path);
+       TEST_STRING_EQUAL(info->info21.profile_path, user->profile_path);
 
        if (user->lm_password_present) {
-               sam_rid_crypt(rid, user->lmpassword.hash, lm_hash.hash, 0);
                lm_hash_p = &lm_hash;
        }
        if (user->nt_password_present) {
-               sam_rid_crypt(rid, user->ntpassword.hash, nt_hash.hash, 0);
                nt_hash_p = &nt_hash;
        }
 
@@ -548,15 +577,12 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
                enum ndr_err_code ndr_err;
                data.data = user->user_private_info.SensitiveData;
                data.length = user->user_private_info.DataLength;
-               creds_arcfour_crypt(samsync_state->creds, data.data, data.length);
                ndr_err = ndr_pull_struct_blob(&data, mem_ctx, lp_iconv_convenience(tctx->lp_ctx), &keys, (ndr_pull_flags_fn_t)ndr_pull_netr_USER_KEYS);
                if (NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
                        if (keys.keys.keys2.lmpassword.length == 16) {
-                               sam_rid_crypt(rid, keys.keys.keys2.lmpassword.pwd.hash, lm_hash.hash, 0);
                                lm_hash_p = &lm_hash;
                        }
                        if (keys.keys.keys2.ntpassword.length == 16) {
-                               sam_rid_crypt(rid, keys.keys.keys2.ntpassword.pwd.hash, nt_hash.hash, 0);
                                nt_hash_p = &nt_hash;
                        }
                } else {
@@ -610,7 +636,7 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
                        return true;
                }
        } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_PASSWORD_EXPIRED)) {
-               if (q.out.info->info21.acct_flags & ACB_PW_EXPIRED) {
+               if (info->info21.acct_flags & ACB_PW_EXPIRED) {
                        return true;
                }
        } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_WRONG_PASSWORD)) {
@@ -645,7 +671,7 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
                TEST_TIME_EQUAL(user->last_logon, info3->base.last_logon);
                TEST_TIME_EQUAL(user->acct_expiry, info3->base.acct_expiry);
                TEST_TIME_EQUAL(user->last_password_change, info3->base.last_password_change);
-               TEST_TIME_EQUAL(q.out.info->info21.force_password_change, info3->base.force_password_change);
+               TEST_TIME_EQUAL(info->info21.force_password_change, info3->base.force_password_change);
 
                /* Does the concept of a logoff time ever really
                 * exist? (not in any sensible way, according to the
@@ -658,28 +684,28 @@ static bool samsync_handle_user(struct torture_context *tctx, TALLOC_CTX *mem_ct
                        TEST_TIME_EQUAL(user->last_logoff, info3->base.last_logoff);
                }
 
-               TEST_INT_EQUAL(getgroups.out.rids->count, info3->base.groups.count);
-               if (getgroups.out.rids->count == info3->base.groups.count) {
+               TEST_INT_EQUAL(rids->count, info3->base.groups.count);
+               if (rids->count == info3->base.groups.count) {
                        int i, j;
-                       int count = getgroups.out.rids->count;
-                       bool *matched = talloc_zero_array(mem_ctx, bool, getgroups.out.rids->count);
+                       int count = rids->count;
+                       bool *matched = talloc_zero_array(mem_ctx, bool, rids->count);
                                
                        for (i = 0; i < count; i++) {
                                for (j = 0; j < count; j++) {
-                                       if ((getgroups.out.rids->rids[i].rid == 
+                                       if ((rids->rids[i].rid ==
                                             info3->base.groups.rids[j].rid)
-                                           && (getgroups.out.rids->rids[i].attributes == 
+                                           && (rids->rids[i].attributes ==
                                                info3->base.groups.rids[j].attributes)) {
                                                        matched[i] = true;
                                                }
                                }
                        }
 
-                       for (i = 0; i < getgroups.out.rids->count; i++) {
+                       for (i = 0; i < rids->count; i++) {
                                if (matched[i] == false) {
                                        ret = false;
                                        printf("Could not find group RID %u found in getgroups in NETLOGON reply\n",
-                                              getgroups.out.rids->rids[i].rid); 
+                                              rids->rids[i].rid);
                                }
                        }
                }
@@ -702,6 +728,7 @@ static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *sams
 
        struct samr_OpenAlias r;
        struct samr_QueryAliasInfo q;
+       union samr_AliasInfo *info;
        struct policy_handle alias_handle;
 
        if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
@@ -722,6 +749,7 @@ static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *sams
 
        q.in.alias_handle = &alias_handle;
        q.in.level = 1;
+       q.out.info = &info;
 
        TEST_SEC_DESC_EQUAL(alias->sdbuf, samr, &alias_handle);
 
@@ -736,8 +764,8 @@ static bool samsync_handle_alias(TALLOC_CTX *mem_ctx, struct samsync_state *sams
                return false;
        }
 
-       TEST_STRING_EQUAL(q.out.info->all.name, alias->alias_name);
-       TEST_STRING_EQUAL(q.out.info->all.description, alias->description);
+       TEST_STRING_EQUAL(info->all.name, alias->alias_name);
+       TEST_STRING_EQUAL(info->all.description, alias->description);
        return ret;
 }
 
@@ -751,6 +779,7 @@ static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *sams
 
        struct samr_OpenGroup r;
        struct samr_QueryGroupInfo q;
+       union samr_GroupInfo *info;
        struct policy_handle group_handle;
 
        if (!samsync_state->domain_name || !samsync_state->domain_handle[database_id]) {
@@ -771,6 +800,7 @@ static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *sams
 
        q.in.group_handle = &group_handle;
        q.in.level = 1;
+       q.out.info = &info;
 
        TEST_SEC_DESC_EQUAL(group->sdbuf, samr, &group_handle);
 
@@ -785,9 +815,9 @@ static bool samsync_handle_group(TALLOC_CTX *mem_ctx, struct samsync_state *sams
                return false;
        }
 
-       TEST_STRING_EQUAL(q.out.info->all.name, group->group_name);
-       TEST_INT_EQUAL(q.out.info->all.attributes, group->attributes);
-       TEST_STRING_EQUAL(q.out.info->all.description, group->description);
+       TEST_STRING_EQUAL(info->all.name, group->group_name);
+       TEST_INT_EQUAL(info->all.attributes, group->attributes);
+       TEST_STRING_EQUAL(info->all.description, group->description);
        return ret;
 }
 
@@ -796,31 +826,25 @@ static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *sam
 {
        struct netr_DELTA_SECRET *secret = delta->delta_union.secret;
        const char *name = delta->delta_id_union.name;
-       struct samsync_secret *new = talloc(samsync_state, struct samsync_secret);
+       struct samsync_secret *nsec = talloc(samsync_state, struct samsync_secret);
        struct samsync_secret *old = talloc(mem_ctx, struct samsync_secret);
        struct lsa_QuerySecret q;
        struct lsa_OpenSecret o;
        struct policy_handle sec_handle;
        struct lsa_DATA_BUF_PTR bufp1;
        struct lsa_DATA_BUF_PTR bufp2;
-       NTTIME new_mtime;
+       NTTIME nsec_mtime;
        NTTIME old_mtime;
        bool ret = true;
        DATA_BLOB lsa_blob1, lsa_blob_out, session_key;
        NTSTATUS status;
 
-       creds_arcfour_crypt(samsync_state->creds, secret->current_cipher.cipher_data, 
-                           secret->current_cipher.maxlen); 
+       nsec->name = talloc_reference(nsec, name);
+       nsec->secret = data_blob_talloc(nsec, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
+       nsec->mtime = secret->current_cipher_set_time;
 
-       creds_arcfour_crypt(samsync_state->creds, secret->old_cipher.cipher_data, 
-                           secret->old_cipher.maxlen); 
-
-       new->name = talloc_reference(new, name);
-       new->secret = data_blob_talloc(new, secret->current_cipher.cipher_data, secret->current_cipher.maxlen);
-       new->mtime = secret->current_cipher_set_time;
-
-       new = talloc_reference(samsync_state, new);
-       DLIST_ADD(samsync_state->secrets, new);
+       nsec = talloc_reference(samsync_state, nsec);
+       DLIST_ADD(samsync_state->secrets, nsec);
 
        old->name = talloc_reference(old, name);
        old->secret = data_blob_const(secret->old_cipher.cipher_data, secret->old_cipher.maxlen);
@@ -848,13 +872,13 @@ static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *sam
        }
 
 
-       ZERO_STRUCT(new_mtime);
+       ZERO_STRUCT(nsec_mtime);
        ZERO_STRUCT(old_mtime);
 
        /* fetch the secret back again */
        q.in.sec_handle = &sec_handle;
        q.in.new_val = &bufp1;
-       q.in.new_mtime = &new_mtime;
+       q.in.new_mtime = &nsec_mtime;
        q.in.old_val = &bufp2;
        q.in.old_mtime = &old_mtime;
 
@@ -923,26 +947,26 @@ static bool samsync_handle_secret(TALLOC_CTX *mem_ctx, struct samsync_state *sam
                }
                
                if (!q.out.new_mtime) {
-                       printf("NEW mtime not available on LSA for secret %s\n", new->name);
+                       printf("NEW mtime not available on LSA for secret %s\n", nsec->name);
                        ret = false;
                }
-               if (new->mtime != *q.out.new_mtime) {
+               if (nsec->mtime != *q.out.new_mtime) {
                        printf("NEW mtime on secret %s does not match between SAMSYNC (%s) and LSA (%s)\n", 
-                              new->name, nt_time_string(mem_ctx, new->mtime), 
+                              nsec->name, nt_time_string(mem_ctx, nsec->mtime),
                               nt_time_string(mem_ctx, *q.out.new_mtime)); 
                        ret = false;
                }
 
-               if (new->secret.length != lsa_blob_out.length) {
+               if (nsec->secret.length != lsa_blob_out.length) {
                        printf("Returned secret %s doesn't match: %d != %d\n",
-                              new->name, (int)new->secret.length, (int)lsa_blob_out.length);
+                              nsec->name, (int)nsec->secret.length, (int)lsa_blob_out.length);
                        ret = false;
                } else if (memcmp(lsa_blob_out.data, 
-                          new->secret.data, new->secret.length) != 0) {
+                          nsec->secret.data, nsec->secret.length) != 0) {
                        printf("Returned secret %s doesn't match: \n",
-                              new->name);
+                              nsec->name);
                        DEBUG(1, ("SamSync Secret:\n"));
-                       dump_data(1, new->secret.data, new->secret.length);
+                       dump_data(1, nsec->secret.data, nsec->secret.length);
                        DEBUG(1, ("LSA Secret:\n"));
                        dump_data(1, lsa_blob_out.data, lsa_blob_out.length);
                        ret = false;
@@ -960,16 +984,17 @@ static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_st
        struct netr_DELTA_TRUSTED_DOMAIN *trusted_domain = delta->delta_union.trusted_domain;
        struct dom_sid *dom_sid = delta->delta_id_union.sid;
 
-       struct samsync_trusted_domain *new = talloc(samsync_state, struct samsync_trusted_domain);
+       struct samsync_trusted_domain *ndom = talloc(samsync_state, struct samsync_trusted_domain);
        struct lsa_OpenTrustedDomain t;
        struct policy_handle trustdom_handle;
        struct lsa_QueryTrustedDomainInfo q;
        union lsa_TrustedDomainInfo *info[9];
+       union lsa_TrustedDomainInfo *_info = NULL;
        int levels [] = {1, 3, 8};
        int i;
 
-       new->name = talloc_reference(new, trusted_domain->domain_name.string);
-       new->sid = talloc_reference(new, dom_sid);
+       ndom->name = talloc_reference(ndom, trusted_domain->domain_name.string);
+       ndom->sid = talloc_reference(ndom, dom_sid);
 
        t.in.handle = samsync_state->lsa_handle;
        t.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
@@ -985,6 +1010,7 @@ static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_st
        for (i=0; i< ARRAY_SIZE(levels); i++) {
                q.in.trustdom_handle = &trustdom_handle;
                q.in.level = levels[i];
+               q.out.info = &_info;
                status = dcerpc_lsa_QueryTrustedDomainInfo(samsync_state->p_lsa, mem_ctx, &q);
                if (!NT_STATUS_IS_OK(status)) {
                        if (q.in.level == 8 && NT_STATUS_EQUAL(status,NT_STATUS_INVALID_PARAMETER)) {
@@ -995,7 +1021,7 @@ static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_st
                               levels[i], nt_errstr(status));
                        return false;
                }
-               info[levels[i]]  = q.out.info;
+               info[levels[i]]  = _info;
        }
 
        if (info[8]) {
@@ -1008,8 +1034,8 @@ static bool samsync_handle_trusted_domain(TALLOC_CTX *mem_ctx, struct samsync_st
   We would like to do this, but it is NOT_SUPPORTED on win2k3
        TEST_SEC_DESC_EQUAL(trusted_domain->sdbuf, lsa, &trustdom_handle);
 */
-       new = talloc_reference(samsync_state, new);
-       DLIST_ADD(samsync_state->trusted_domains, new);
+       ndom = talloc_reference(samsync_state, ndom);
+       DLIST_ADD(samsync_state->trusted_domains, ndom);
 
        return ret;
 }
@@ -1025,6 +1051,7 @@ static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *sa
        struct lsa_OpenAccount a;
        struct policy_handle acct_handle;
        struct lsa_EnumPrivsAccount e;
+       struct lsa_PrivilegeSet *privs = NULL;
        struct lsa_LookupPrivName r;
 
        int i, j;
@@ -1047,6 +1074,7 @@ static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *sa
        found_priv_in_lsa = talloc_zero_array(mem_ctx, bool, account->privilege_entries);
 
        e.in.handle = &acct_handle;
+       e.out.privs = &privs;
 
        status = dcerpc_lsa_EnumPrivsAccount(samsync_state->p_lsa, mem_ctx, &e);
        if (!NT_STATUS_IS_OK(status)) {
@@ -1054,23 +1082,27 @@ static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *sa
                return false;
        }
 
-       if ((account->privilege_entries && !e.out.privs)) {
+       if ((account->privilege_entries && !privs)) {
                printf("Account %s has privileges in SamSync, but not LSA\n",
                       dom_sid_string(mem_ctx, dom_sid));
                return false;
        }
 
-       if (!account->privilege_entries && e.out.privs && e.out.privs->count) {
+       if (!account->privilege_entries && privs && privs->count) {
                printf("Account %s has privileges in LSA, but not SamSync\n",
                       dom_sid_string(mem_ctx, dom_sid));
                return false;
        }
 
-       TEST_INT_EQUAL(account->privilege_entries, e.out.privs->count);
+       TEST_INT_EQUAL(account->privilege_entries, privs->count);
        
-       for (i=0;i< e.out.privs->count; i++) {
+       for (i=0;i< privs->count; i++) {
+
+               struct lsa_StringLarge *name = NULL;
+
                r.in.handle = samsync_state->lsa_handle;
-               r.in.luid = &e.out.privs->set[i].luid;
+               r.in.luid = &privs->set[i].luid;
+               r.out.name = &name;
                
                status = dcerpc_lsa_LookupPrivName(samsync_state->p_lsa, mem_ctx, &r);
                if (!NT_STATUS_IS_OK(status)) {
@@ -1083,7 +1115,7 @@ static bool samsync_handle_account(TALLOC_CTX *mem_ctx, struct samsync_state *sa
                        return false;
                }
                for (j=0;j<account->privilege_entries; j++) {
-                       if (strcmp(r.out.name->string, account->privilege_name[j].string) == 0) {
+                       if (strcmp(name->string, account->privilege_name[j].string) == 0) {
                                found_priv_in_lsa[j] = true;
                                break;
                        }
@@ -1114,23 +1146,35 @@ static bool test_DatabaseSync(struct torture_context *tctx,
        bool ret = true;
        struct samsync_trusted_domain *t;
        struct samsync_secret *s;
+       struct netr_Authenticator return_authenticator, credential;
+       struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
        
        const char *domain, *username;
 
+       ZERO_STRUCT(return_authenticator);
+
        r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
        r.in.computername = TEST_MACHINE_NAME;
        r.in.preferredmaximumlength = (uint32_t)-1;
-       ZERO_STRUCT(r.in.return_authenticator);
+       r.in.return_authenticator = &return_authenticator;
+       r.out.return_authenticator = &return_authenticator;
+       r.out.delta_enum_array = &delta_enum_array;
 
        for (i=0;i<ARRAY_SIZE(database_ids);i++) {
-               r.in.sync_context = 0;
+
+               uint32_t sync_context = 0;
+
                r.in.database_id = database_ids[i];
+               r.in.sync_context = &sync_context;
+               r.out.sync_context = &sync_context;
 
                printf("Testing DatabaseSync of id %d\n", r.in.database_id);
 
                do {
                        loop_ctx = talloc_named(mem_ctx, 0, "DatabaseSync loop context");
-                       creds_client_authenticator(samsync_state->creds, &r.in.credential);
+                       netlogon_creds_client_authenticator(samsync_state->creds, &credential);
+
+                       r.in.credential = &credential;
 
                        status = dcerpc_netr_DatabaseSync(samsync_state->p, loop_ctx, &r);
                        if (!NT_STATUS_IS_OK(status) &&
@@ -1140,67 +1184,75 @@ static bool test_DatabaseSync(struct torture_context *tctx,
                                break;
                        }
 
-                       if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
+                       if (!netlogon_creds_client_check(samsync_state->creds, &r.out.return_authenticator->cred)) {
                                printf("Credential chaining failed\n");
                        }
 
                        r.in.sync_context = r.out.sync_context;
 
-                       for (d=0; d < r.out.delta_enum_array->num_deltas; d++) {
+                       for (d=0; d < delta_enum_array->num_deltas; d++) {
                                delta_ctx = talloc_named(loop_ctx, 0, "DatabaseSync delta context");
-                               switch (r.out.delta_enum_array->delta_enum[d].delta_type) {
+
+                               if (!NT_STATUS_IS_OK(samsync_fix_delta(delta_ctx, samsync_state->creds, 
+                                                                      r.in.database_id, 
+                                                                      &delta_enum_array->delta_enum[d]))) {
+                                       printf("Failed to decrypt delta\n");
+                                       ret = false;
+                               }
+
+                               switch (delta_enum_array->delta_enum[d].delta_type) {
                                case NETR_DELTA_DOMAIN:
                                        if (!samsync_handle_domain(delta_ctx, samsync_state, 
-                                                                  r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                  r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_DOMAIN\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_GROUP:
                                        if (!samsync_handle_group(delta_ctx, samsync_state, 
-                                                                 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                 r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_USER\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_USER:
                                        if (!samsync_handle_user(tctx, delta_ctx, samsync_state, 
-                                                                r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_USER\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_ALIAS:
                                        if (!samsync_handle_alias(delta_ctx, samsync_state, 
-                                                                 r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                 r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_ALIAS\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_POLICY:
                                        if (!samsync_handle_policy(delta_ctx, samsync_state, 
-                                                                  r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                  r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_POLICY\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_TRUSTED_DOMAIN:
                                        if (!samsync_handle_trusted_domain(delta_ctx, samsync_state, 
-                                                                          r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                          r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_TRUSTED_DOMAIN\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_ACCOUNT:
                                        if (!samsync_handle_account(delta_ctx, samsync_state, 
-                                                                   r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                   r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_ACCOUNT\n");
                                                ret = false;
                                        }
                                        break;
                                case NETR_DELTA_SECRET:
                                        if (!samsync_handle_secret(delta_ctx, samsync_state, 
-                                                                  r.in.database_id, &r.out.delta_enum_array->delta_enum[d])) {
+                                                                  r.in.database_id, &delta_enum_array->delta_enum[d])) {
                                                printf("Failed to handle DELTA_SECRET\n");
                                                ret = false;
                                        }
@@ -1222,7 +1274,7 @@ static bool test_DatabaseSync(struct torture_context *tctx,
                                case NETR_DELTA_DELETE_USER2:
                                case NETR_DELTA_MODIFY_COUNT:
                                default:
-                                       printf("Uxpected delta type %d\n", r.out.delta_enum_array->delta_enum[d].delta_type);
+                                       printf("Uxpected delta type %d\n", delta_enum_array->delta_enum[d].delta_type);
                                        ret = false;
                                        break;
                                }
@@ -1303,34 +1355,45 @@ static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX
        NTSTATUS status;
        TALLOC_CTX *loop_ctx;
        struct netr_DatabaseDeltas r;
+       struct netr_Authenticator credential;
+       struct netr_Authenticator return_authenticator;
+       struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
        const uint32_t database_ids[] = {0, 1, 2}; 
        int i;
        bool ret = true;
 
+       ZERO_STRUCT(return_authenticator);
+
        r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(samsync_state->p));
        r.in.computername = TEST_MACHINE_NAME;
+       r.in.credential = &credential;
        r.in.preferredmaximumlength = (uint32_t)-1;
-       ZERO_STRUCT(r.in.return_authenticator);
+       r.in.return_authenticator = &return_authenticator;
+       r.out.return_authenticator = &return_authenticator;
+       r.out.delta_enum_array = &delta_enum_array;
 
        for (i=0;i<ARRAY_SIZE(database_ids);i++) {
+
+               uint64_t seq_num = samsync_state->seq_num[i];
+
                r.in.database_id = database_ids[i];
-               r.in.sequence_num = samsync_state->seq_num[i];
+               r.in.sequence_num = &seq_num;
+               r.out.sequence_num = &seq_num;
 
-               if (r.in.sequence_num == 0) continue;
+               if (seq_num == 0) continue;
 
                /* this shows that the bdc doesn't need to do a single call for
                 * each seqnumber, and the pdc doesn't need to know about old values
                 * -- metze
                 */
-               r.in.sequence_num -= 10;
-
+               seq_num -= 10;
 
                printf("Testing DatabaseDeltas of id %d at %llu\n", 
-                      r.in.database_id, (long long)r.in.sequence_num);
+                      r.in.database_id, (long long)seq_num);
 
                do {
                        loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseDeltas loop context");
-                       creds_client_authenticator(samsync_state->creds, &r.in.credential);
+                       netlogon_creds_client_authenticator(samsync_state->creds, &credential);
 
                        status = dcerpc_netr_DatabaseDeltas(samsync_state->p, loop_ctx, &r);
                        if (!NT_STATUS_IS_OK(status) &&
@@ -1340,11 +1403,11 @@ static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX
                                ret = false;
                        }
 
-                       if (!creds_client_check(samsync_state->creds, &r.out.return_authenticator.cred)) {
+                       if (!netlogon_creds_client_check(samsync_state->creds, &return_authenticator.cred)) {
                                printf("Credential chaining failed\n");
                        }
 
-                       r.in.sequence_num++;
+                       seq_num++;
                        talloc_free(loop_ctx);
                } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
        }
@@ -1357,7 +1420,7 @@ static bool test_DatabaseDeltas(struct samsync_state *samsync_state, TALLOC_CTX
   try a netlogon DatabaseSync2
 */
 static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx, 
-                              struct creds_CredentialState *creds)
+                              struct netlogon_creds_CredentialState *creds)
 {
        NTSTATUS status;
        TALLOC_CTX *loop_ctx;
@@ -1365,22 +1428,34 @@ static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
        const uint32_t database_ids[] = {0, 1, 2}; 
        int i;
        bool ret = true;
+       struct netr_Authenticator return_authenticator, credential;
+       struct netr_DELTA_ENUM_ARRAY *delta_enum_array = NULL;
+
+       ZERO_STRUCT(return_authenticator);
 
        r.in.logon_server = talloc_asprintf(mem_ctx, "\\\\%s", dcerpc_server_name(p));
        r.in.computername = TEST_MACHINE_NAME;
        r.in.preferredmaximumlength = (uint32_t)-1;
-       ZERO_STRUCT(r.in.return_authenticator);
+       r.in.return_authenticator = &return_authenticator;
+       r.out.return_authenticator = &return_authenticator;
+       r.out.delta_enum_array = &delta_enum_array;
 
        for (i=0;i<ARRAY_SIZE(database_ids);i++) {
-               r.in.sync_context = 0;
+
+               uint32_t sync_context = 0;
+
                r.in.database_id = database_ids[i];
+               r.in.sync_context = &sync_context;
+               r.out.sync_context = &sync_context;
                r.in.restart_state = 0;
 
                printf("Testing DatabaseSync2 of id %d\n", r.in.database_id);
 
                do {
                        loop_ctx = talloc_named(mem_ctx, 0, "test_DatabaseSync2 loop context");
-                       creds_client_authenticator(creds, &r.in.credential);
+                       netlogon_creds_client_authenticator(creds, &credential);
+
+                       r.in.credential = &credential;
 
                        status = dcerpc_netr_DatabaseSync2(p, loop_ctx, &r);
                        if (!NT_STATUS_IS_OK(status) &&
@@ -1389,11 +1464,10 @@ static bool test_DatabaseSync2(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
                                ret = false;
                        }
 
-                       if (!creds_client_check(creds, &r.out.return_authenticator.cred)) {
+                       if (!netlogon_creds_client_check(creds, &r.out.return_authenticator->cred)) {
                                printf("Credential chaining failed\n");
                        }
 
-                       r.in.sync_context = r.out.sync_context;
                        talloc_free(loop_ctx);
                } while (NT_STATUS_EQUAL(status, STATUS_MORE_ENTRIES));
        }