Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[ira/wip.git] / source4 / torture / rpc / winreg.c
index 634dcc8cfd7a115e4a63ac0af5cf79ac8e9e8db8..469573367102b7d34982150718e8fcd5db21f23d 100644 (file)
@@ -4,6 +4,7 @@
 
    Copyright (C) Tim Potter 2003
    Copyright (C) Jelmer Vernooij 2004-2007
+   Copyright (C) Günther Deschner 2007
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #define TEST_SID "S-1-5-21-1234567890-1234567890-1234567890-500"
 
-static void init_initshutdown_String(TALLOC_CTX *mem_ctx,
-                                    struct initshutdown_String *name,
-                                    const char *s)
+static void init_lsa_StringLarge(struct lsa_StringLarge *name, const char *s)
 {
-       name->name = talloc(mem_ctx, struct initshutdown_String_sub);
-       name->name->name = s;
+       name->string = s;
 }
 
 static void init_winreg_String(struct winreg_String *name, const char *s)
@@ -154,8 +152,8 @@ static bool test_CreateKey_sd(struct dcerpc_pipe *p,
                                        SEC_ACE_FLAG_CONTAINER_INHERIT,
                                        NULL);
 
-       torture_assert_ntstatus_ok(tctx,
-               ndr_push_struct_blob(&sdblob, tctx, sd,
+       torture_assert_ndr_success(tctx,
+               ndr_push_struct_blob(&sdblob, tctx, NULL, sd,
                                     (ndr_push_flags_fn_t)ndr_push_security_descriptor),
                                     "Failed to push security_descriptor ?!\n");
 
@@ -219,8 +217,8 @@ static bool _test_GetKeySecurity(struct dcerpc_pipe *p,
 
        sd = talloc_zero(tctx, struct security_descriptor);
 
-       torture_assert_ntstatus_ok(tctx,
-               ndr_pull_struct_blob(&sdblob, tctx, sd,
+       torture_assert_ndr_success(tctx,
+               ndr_pull_struct_blob(&sdblob, tctx, NULL, sd,
                                     (ndr_pull_flags_fn_t)ndr_pull_security_descriptor),
                                     "pull_security_descriptor failed");
 
@@ -263,8 +261,8 @@ static bool _test_SetKeySecurity(struct dcerpc_pipe *p,
                NDR_PRINT_DEBUG(security_descriptor, sd);
        }
 
-       torture_assert_ntstatus_ok(tctx,
-               ndr_push_struct_blob(&sdblob, tctx, sd,
+       torture_assert_ndr_success(tctx,
+               ndr_push_struct_blob(&sdblob, tctx, NULL, sd,
                                     (ndr_push_flags_fn_t)ndr_push_security_descriptor),
                                     "push_security_descriptor failed");
 
@@ -439,6 +437,40 @@ static bool test_SecurityDescriptor(struct dcerpc_pipe *p,
        return ret;
 }
 
+static bool _test_SecurityDescriptor(struct dcerpc_pipe *p,
+                                    struct torture_context *tctx,
+                                    struct policy_handle *handle,
+                                    uint32_t access_mask,
+                                    const char *key,
+                                    WERROR open_werr,
+                                    WERROR get_werr,
+                                    WERROR set_werr)
+{
+       struct policy_handle new_handle;
+       bool ret = true;
+       bool got_key = false;
+
+       if (!_test_OpenKey(p, tctx, handle, key, access_mask, &new_handle,
+                          open_werr, &got_key)) {
+               return false;
+       }
+
+       if (!got_key) {
+               return true;
+       }
+
+       if (!_test_GetSetSecurityDescriptor(p, tctx, &new_handle,
+                                           get_werr, set_werr)) {
+               ret = false;
+       }
+
+       if (!test_CloseKey(p, tctx, &new_handle)) {
+               return false;
+       }
+
+       return ret;
+}
+
 static bool test_dacl_trustee_present(struct dcerpc_pipe *p,
                                      struct torture_context *tctx,
                                      struct policy_handle *handle,
@@ -946,6 +978,362 @@ static bool test_SecurityDescriptorBlockInheritance(struct dcerpc_pipe *p,
        return ret;
 }
 
+static bool test_SecurityDescriptorsMasks(struct dcerpc_pipe *p,
+                                         struct torture_context *tctx,
+                                         struct policy_handle *handle,
+                                         const char *key)
+{
+       bool ret = true;
+       int i;
+
+       struct winreg_mask_result_table {
+               uint32_t access_mask;
+               WERROR open_werr;
+               WERROR get_werr;
+               WERROR set_werr;
+       } sd_mask_tests[] = {
+               { 0,
+                       WERR_ACCESS_DENIED, WERR_BADFILE, WERR_FOOBAR },
+               { SEC_FLAG_MAXIMUM_ALLOWED,
+                       WERR_OK, WERR_OK, WERR_OK },
+               { SEC_STD_WRITE_DAC,
+                       WERR_OK, WERR_ACCESS_DENIED, WERR_FOOBAR },
+               { SEC_FLAG_SYSTEM_SECURITY,
+                       WERR_OK, WERR_ACCESS_DENIED, WERR_FOOBAR }
+       };
+
+       /* FIXME: before this test can ever run successfully we need a way to
+        * correctly read a NULL security_descritpor in ndr, get the required
+        * length, requery, etc.
+        */
+
+       return true;
+
+       for (i=0; i < ARRAY_SIZE(sd_mask_tests); i++) {
+
+               torture_comment(tctx,
+                               "SecurityDescriptor get & set with access_mask: 0x%08x\n",
+                               sd_mask_tests[i].access_mask);
+               torture_comment(tctx,
+                               "expecting: open %s, get: %s, set: %s\n",
+                               win_errstr(sd_mask_tests[i].open_werr),
+                               win_errstr(sd_mask_tests[i].get_werr),
+                               win_errstr(sd_mask_tests[i].set_werr));
+
+               if (_test_SecurityDescriptor(p, tctx, handle,
+                                            sd_mask_tests[i].access_mask, key,
+                                            sd_mask_tests[i].open_werr,
+                                            sd_mask_tests[i].get_werr,
+                                            sd_mask_tests[i].set_werr)) {
+                       ret = false;
+               }
+       }
+
+       return ret;
+}
+
+typedef bool (*secinfo_verify_fn)(struct dcerpc_pipe *,
+                                 struct torture_context *,
+                                 struct policy_handle *,
+                                 const char *,
+                                 const struct dom_sid *);
+
+static bool test_SetSecurityDescriptor_SecInfo(struct dcerpc_pipe *p,
+                                              struct torture_context *tctx,
+                                              struct policy_handle *handle,
+                                              const char *key,
+                                              const char *test,
+                                              uint32_t access_mask,
+                                              uint32_t sec_info,
+                                              struct security_descriptor *sd,
+                                              WERROR set_werr,
+                                              bool expect_present,
+                                              bool (*fn) (struct dcerpc_pipe *,
+                                                          struct torture_context *,
+                                                          struct policy_handle *,
+                                                          const char *,
+                                                          const struct dom_sid *),
+                                              const struct dom_sid *sid)
+{
+       struct policy_handle new_handle;
+       bool open_success = false;
+
+       torture_comment(tctx, "SecurityDescriptor (%s) sets for secinfo: "
+                       "0x%08x, access_mask: 0x%08x\n",
+                       test, sec_info, access_mask);
+
+       if (!_test_OpenKey(p, tctx, handle, key,
+                          access_mask,
+                          &new_handle,
+                          WERR_OK,
+                          &open_success)) {
+               return false;
+       }
+
+       if (!open_success) {
+               printf("key did not open\n");
+               test_CloseKey(p, tctx, &new_handle);
+               return false;
+       }
+
+       if (!_test_SetKeySecurity(p, tctx, &new_handle, &sec_info,
+                                 sd,
+                                 set_werr)) {
+               torture_warning(tctx,
+                               "SetKeySecurity with secinfo: 0x%08x has failed\n",
+                               sec_info);
+               smb_panic("");
+               test_CloseKey(p, tctx, &new_handle);
+               return false;
+       }
+
+       test_CloseKey(p, tctx, &new_handle);
+
+       if (W_ERROR_IS_OK(set_werr)) {
+               bool present;
+               present = fn(p, tctx, handle, key, sid);
+               if ((expect_present) && (!present)) {
+                       torture_warning(tctx,
+                                       "%s sid is not present!\n",
+                                       test);
+                       return false;
+               }
+               if ((!expect_present) && (present)) {
+                       torture_warning(tctx,
+                                       "%s sid is present but not expected!\n",
+                                       test);
+                       return false;
+               }
+       }
+
+       return true;
+}
+
+static bool test_SecurityDescriptorsSecInfo(struct dcerpc_pipe *p,
+                                           struct torture_context *tctx,
+                                           struct policy_handle *handle,
+                                           const char *key)
+{
+       struct security_descriptor *sd_orig = NULL;
+       struct dom_sid *sid = NULL;
+       bool ret = true;
+       int i, a;
+
+       struct security_descriptor *sd_owner =
+               security_descriptor_dacl_create(tctx,
+                                               0,
+                                               TEST_SID, NULL, NULL);
+
+       struct security_descriptor *sd_group =
+               security_descriptor_dacl_create(tctx,
+                                               0,
+                                               NULL, TEST_SID, NULL);
+
+       struct security_descriptor *sd_dacl =
+               security_descriptor_dacl_create(tctx,
+                                               0,
+                                               NULL, NULL,
+                                               TEST_SID,
+                                               SEC_ACE_TYPE_ACCESS_ALLOWED,
+                                               SEC_GENERIC_ALL,
+                                               0,
+                                               SID_NT_AUTHENTICATED_USERS,
+                                               SEC_ACE_TYPE_ACCESS_ALLOWED,
+                                               SEC_GENERIC_ALL,
+                                               0,
+                                               NULL);
+
+       struct security_descriptor *sd_sacl =
+               security_descriptor_sacl_create(tctx,
+                                               0,
+                                               NULL, NULL,
+                                               TEST_SID,
+                                               SEC_ACE_TYPE_SYSTEM_AUDIT,
+                                               SEC_GENERIC_ALL,
+                                               SEC_ACE_FLAG_SUCCESSFUL_ACCESS,
+                                               NULL);
+
+       struct winreg_secinfo_table {
+               struct security_descriptor *sd;
+               uint32_t sec_info;
+               WERROR set_werr;
+               bool sid_present;
+               secinfo_verify_fn fn;
+       };
+
+       struct winreg_secinfo_table sec_info_owner_tests[] = {
+               { sd_owner, 0, WERR_OK,
+                       false, (secinfo_verify_fn)_test_owner_present },
+               { sd_owner, SECINFO_OWNER, WERR_OK,
+                       true, (secinfo_verify_fn)_test_owner_present },
+               { sd_owner, SECINFO_GROUP, WERR_INVALID_PARAM },
+               { sd_owner, SECINFO_DACL, WERR_OK,
+                       true, (secinfo_verify_fn)_test_owner_present },
+               { sd_owner, SECINFO_SACL, WERR_ACCESS_DENIED },
+       };
+
+       uint32_t sd_owner_good_access_masks[] = {
+               SEC_FLAG_MAXIMUM_ALLOWED,
+               /* SEC_STD_WRITE_OWNER, */
+       };
+
+       struct winreg_secinfo_table sec_info_group_tests[] = {
+               { sd_group, 0, WERR_OK,
+                       false, (secinfo_verify_fn)_test_group_present },
+               { sd_group, SECINFO_OWNER, WERR_INVALID_PARAM },
+               { sd_group, SECINFO_GROUP, WERR_OK,
+                       true, (secinfo_verify_fn)_test_group_present },
+               { sd_group, SECINFO_DACL, WERR_OK,
+                       true, (secinfo_verify_fn)_test_group_present },
+               { sd_group, SECINFO_SACL, WERR_ACCESS_DENIED },
+       };
+
+       uint32_t sd_group_good_access_masks[] = {
+               SEC_FLAG_MAXIMUM_ALLOWED,
+       };
+
+       struct winreg_secinfo_table sec_info_dacl_tests[] = {
+               { sd_dacl, 0, WERR_OK,
+                       false, (secinfo_verify_fn)_test_dacl_trustee_present },
+               { sd_dacl, SECINFO_OWNER, WERR_INVALID_PARAM },
+               { sd_dacl, SECINFO_GROUP, WERR_INVALID_PARAM },
+               { sd_dacl, SECINFO_DACL, WERR_OK,
+                       true, (secinfo_verify_fn)_test_dacl_trustee_present },
+               { sd_dacl, SECINFO_SACL, WERR_ACCESS_DENIED },
+       };
+
+       uint32_t sd_dacl_good_access_masks[] = {
+               SEC_FLAG_MAXIMUM_ALLOWED,
+               SEC_STD_WRITE_DAC,
+       };
+
+       struct winreg_secinfo_table sec_info_sacl_tests[] = {
+               { sd_sacl, 0, WERR_OK,
+                       false, (secinfo_verify_fn)_test_sacl_trustee_present },
+               { sd_sacl, SECINFO_OWNER, WERR_INVALID_PARAM },
+               { sd_sacl, SECINFO_GROUP, WERR_INVALID_PARAM },
+               { sd_sacl, SECINFO_DACL, WERR_OK,
+                       false, (secinfo_verify_fn)_test_sacl_trustee_present },
+               { sd_sacl, SECINFO_SACL, WERR_OK,
+                       true, (secinfo_verify_fn)_test_sacl_trustee_present },
+       };
+
+       uint32_t sd_sacl_good_access_masks[] = {
+               SEC_FLAG_MAXIMUM_ALLOWED | SEC_FLAG_SYSTEM_SECURITY,
+               /* SEC_FLAG_SYSTEM_SECURITY, */
+       };
+
+       sid = dom_sid_parse_talloc(tctx, TEST_SID);
+       if (sid == NULL) {
+               return false;
+       }
+
+       if (!test_BackupSecurity(p, tctx, handle, key, &sd_orig)) {
+               return false;
+       }
+
+       /* OWNER */
+
+       for (i=0; i < ARRAY_SIZE(sec_info_owner_tests); i++) {
+
+               for (a=0; a < ARRAY_SIZE(sd_owner_good_access_masks); a++) {
+
+                       if (!test_SetSecurityDescriptor_SecInfo(p, tctx, handle,
+                                       key,
+                                       "OWNER",
+                                       sd_owner_good_access_masks[a],
+                                       sec_info_owner_tests[i].sec_info,
+                                       sec_info_owner_tests[i].sd,
+                                       sec_info_owner_tests[i].set_werr,
+                                       sec_info_owner_tests[i].sid_present,
+                                       sec_info_owner_tests[i].fn,
+                                       sid))
+                       {
+                               printf("test_SetSecurityDescriptor_SecInfo failed for OWNER\n");
+                               ret = false;
+                               goto out;
+                       }
+               }
+       }
+
+       /* GROUP */
+
+       for (i=0; i < ARRAY_SIZE(sec_info_group_tests); i++) {
+
+               for (a=0; a < ARRAY_SIZE(sd_group_good_access_masks); a++) {
+
+                       if (!test_SetSecurityDescriptor_SecInfo(p, tctx, handle,
+                                       key,
+                                       "GROUP",
+                                       sd_group_good_access_masks[a],
+                                       sec_info_group_tests[i].sec_info,
+                                       sec_info_group_tests[i].sd,
+                                       sec_info_group_tests[i].set_werr,
+                                       sec_info_group_tests[i].sid_present,
+                                       sec_info_group_tests[i].fn,
+                                       sid))
+                       {
+                               printf("test_SetSecurityDescriptor_SecInfo failed for GROUP\n");
+                               ret = false;
+                               goto out;
+                       }
+               }
+       }
+
+       /* DACL */
+
+       for (i=0; i < ARRAY_SIZE(sec_info_dacl_tests); i++) {
+
+               for (a=0; a < ARRAY_SIZE(sd_dacl_good_access_masks); a++) {
+
+                       if (!test_SetSecurityDescriptor_SecInfo(p, tctx, handle,
+                                       key,
+                                       "DACL",
+                                       sd_dacl_good_access_masks[a],
+                                       sec_info_dacl_tests[i].sec_info,
+                                       sec_info_dacl_tests[i].sd,
+                                       sec_info_dacl_tests[i].set_werr,
+                                       sec_info_dacl_tests[i].sid_present,
+                                       sec_info_dacl_tests[i].fn,
+                                       sid))
+                       {
+                               printf("test_SetSecurityDescriptor_SecInfo failed for DACL\n");
+                               ret = false;
+                               goto out;
+                       }
+               }
+       }
+
+       /* SACL */
+
+       for (i=0; i < ARRAY_SIZE(sec_info_sacl_tests); i++) {
+
+               for (a=0; a < ARRAY_SIZE(sd_sacl_good_access_masks); a++) {
+
+                       if (!test_SetSecurityDescriptor_SecInfo(p, tctx, handle,
+                                       key,
+                                       "SACL",
+                                       sd_sacl_good_access_masks[a],
+                                       sec_info_sacl_tests[i].sec_info,
+                                       sec_info_sacl_tests[i].sd,
+                                       sec_info_sacl_tests[i].set_werr,
+                                       sec_info_sacl_tests[i].sid_present,
+                                       sec_info_sacl_tests[i].fn,
+                                       sid))
+                       {
+                               printf("test_SetSecurityDescriptor_SecInfo failed for SACL\n");
+                               ret = false;
+                               goto out;
+                       }
+               }
+       }
+
+ out:
+       test_RestoreSecurity(p, tctx, handle, key, sd_orig);
+
+       return ret;
+}
+
 static bool test_SecurityDescriptors(struct dcerpc_pipe *p,
                                     struct torture_context *tctx,
                                     struct policy_handle *handle,
@@ -968,6 +1356,16 @@ static bool test_SecurityDescriptors(struct dcerpc_pipe *p,
                ret = false;
        }
 
+       if (!test_SecurityDescriptorsSecInfo(p, tctx, handle, key)) {
+               printf("test_SecurityDescriptorsSecInfo failed\n");
+               ret = false;
+       }
+
+       if (!test_SecurityDescriptorsMasks(p, tctx, handle, key)) {
+               printf("test_SecurityDescriptorsMasks failed\n");
+               ret = false;
+       }
+
        return ret;
 }
 
@@ -1235,8 +1633,8 @@ static bool test_InitiateSystemShutdown(struct torture_context *tctx,
        uint16_t hostname = 0x0;
 
        r.in.hostname = &hostname;
-       r.in.message = talloc(tctx, struct initshutdown_String);
-       init_initshutdown_String(tctx, r.in.message, "spottyfood");
+       r.in.message = talloc(tctx, struct lsa_StringLarge);
+       init_lsa_StringLarge(r.in.message, "spottyfood");
        r.in.force_apps = 1;
        r.in.timeout = 30;
        r.in.reboot = 1;
@@ -1259,8 +1657,8 @@ static bool test_InitiateSystemShutdownEx(struct torture_context *tctx,
        uint16_t hostname = 0x0;
 
        r.in.hostname = &hostname;
-       r.in.message = talloc(tctx, struct initshutdown_String);
-       init_initshutdown_String(tctx, r.in.message, "spottyfood");
+       r.in.message = talloc(tctx, struct lsa_StringLarge);
+       init_lsa_StringLarge(r.in.message, "spottyfood");
        r.in.force_apps = 1;
        r.in.timeout = 30;
        r.in.reboot = 1;