r4585: don't consider LookupSids3 failing with NT_STATUS_ACCESS_DENIED (as w2k3 does) or
[samba.git] / source / torture / rpc / lsa.c
index f816b5dc1a0d2903114fdb19e19e1507e698fac5..84f56fed71970fddb87d2afc0452e70c6fd8454d 100644 (file)
@@ -188,6 +188,51 @@ static BOOL test_LookupNames2(struct dcerpc_pipe *p,
 }
 
 
+static BOOL test_LookupNames3(struct dcerpc_pipe *p, 
+                             TALLOC_CTX *mem_ctx, 
+                             struct policy_handle *handle,
+                             struct lsa_TransNameArray2 *tnames)
+{
+       struct lsa_LookupNames3 r;
+       struct lsa_TransSidArray3 sids;
+       struct lsa_String *names;
+       uint32_t count = 0;
+       NTSTATUS status;
+       int i;
+
+       printf("\nTesting LookupNames3 with %d names\n", tnames->count);
+
+       sids.count = 0;
+       sids.sids = NULL;
+
+       names = talloc_array_p(mem_ctx, struct lsa_String, tnames->count);
+       for (i=0;i<tnames->count;i++) {
+               init_lsa_String(&names[i], tnames->names[i].name.string);
+       }
+
+       r.in.handle = handle;
+       r.in.num_names = tnames->count;
+       r.in.names = names;
+       r.in.sids = &sids;
+       r.in.level = 1;
+       r.in.count = &count;
+       r.in.unknown1 = 0;
+       r.in.unknown2 = 0;
+       r.out.count = &count;
+       r.out.sids = &sids;
+
+       status = dcerpc_lsa_LookupNames3(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
+               printf("LookupNames3 failed - %s\n", nt_errstr(status));
+               return False;
+       }
+
+       printf("\n");
+
+       return True;
+}
+
+
 static BOOL test_LookupSids(struct dcerpc_pipe *p, 
                            TALLOC_CTX *mem_ctx, 
                            struct policy_handle *handle,
@@ -264,6 +309,55 @@ static BOOL test_LookupSids2(struct dcerpc_pipe *p,
                return False;
        }
 
+       if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
+               return False;
+       }
+
+       return True;
+}
+
+static BOOL test_LookupSids3(struct dcerpc_pipe *p, 
+                           TALLOC_CTX *mem_ctx, 
+                           struct policy_handle *handle,
+                           struct lsa_SidArray *sids)
+{
+       struct lsa_LookupSids3 r;
+       struct lsa_TransNameArray2 names;
+       uint32_t count = sids->num_sids;
+       NTSTATUS status;
+
+       printf("\nTesting LookupSids3\n");
+
+       names.count = 0;
+       names.names = NULL;
+
+       r.in.sids = sids;
+       r.in.names = &names;
+       r.in.level = 1;
+       r.in.count = &count;
+       r.in.unknown1 = 0;
+       r.in.unknown2 = 0;
+       r.out.count = &count;
+       r.out.names = &names;
+
+       status = dcerpc_lsa_LookupSids3(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status) && !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
+               if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) ||
+                   NT_STATUS_EQUAL(status, NT_STATUS_RPC_PROTSEQ_NOT_SUPPORTED)) {
+                       printf("not considering %s to be an error\n", nt_errstr(status));
+                       return True;
+               }
+               printf("LookupSids3 failed - %s - not considered an error\n", 
+                      nt_errstr(status));
+               return False;
+       }
+
+       printf("\n");
+
+       if (!test_LookupNames3(p, mem_ctx, handle, &names)) {
+               return False;
+       }
+
        return True;
 }
 
@@ -283,7 +377,7 @@ static BOOL test_many_LookupSids(struct dcerpc_pipe *p,
        names.count = 0;
        names.names = NULL;
 
-       sids.num_sids = 1000;
+       sids.num_sids = 100;
 
        sids.sids = talloc_array_p(mem_ctx, struct lsa_SidPtr, sids.num_sids);
 
@@ -317,6 +411,28 @@ static BOOL test_many_LookupSids(struct dcerpc_pipe *p,
        return True;
 }
 
+static BOOL test_LookupPrivValue(struct dcerpc_pipe *p, 
+                                TALLOC_CTX *mem_ctx, 
+                                struct policy_handle *handle,
+                                struct lsa_String *name)
+{
+       NTSTATUS status;
+       struct lsa_LookupPrivValue r;
+       struct lsa_LUID luid;
+
+       r.in.handle = handle;
+       r.in.name = name;
+       r.out.luid = &luid;
+
+       status = dcerpc_lsa_LookupPrivValue(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("\nLookupPrivValue failed - %s\n", nt_errstr(status));
+               return False;
+       }
+
+       return True;
+}
+
 static BOOL test_LookupPrivName(struct dcerpc_pipe *p, 
                                TALLOC_CTX *mem_ctx, 
                                struct policy_handle *handle,
@@ -337,6 +453,67 @@ static BOOL test_LookupPrivName(struct dcerpc_pipe *p,
        return True;
 }
 
+static BOOL test_RemovePrivilegesFromAccount(struct dcerpc_pipe *p, 
+                                            TALLOC_CTX *mem_ctx,                                 
+                                            struct policy_handle *acct_handle,
+                                            struct lsa_LUID *luid)
+{
+       NTSTATUS status;
+       struct lsa_RemovePrivilegesFromAccount r;
+       struct lsa_PrivilegeSet privs;
+       BOOL ret = True;
+
+       printf("Testing RemovePrivilegesFromAccount\n");
+
+       r.in.handle = acct_handle;
+       r.in.remove_all = 0;
+       r.in.privs = &privs;
+
+       privs.count = 1;
+       privs.unknown = 0;
+       privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
+       privs.set[0].luid = *luid;
+       privs.set[0].attribute = 0;
+
+       status = dcerpc_lsa_RemovePrivilegesFromAccount(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("RemovePrivilegesFromAccount failed - %s\n", nt_errstr(status));
+               return False;
+       }
+
+       return ret;
+}
+
+static BOOL test_AddPrivilegesToAccount(struct dcerpc_pipe *p, 
+                                       TALLOC_CTX *mem_ctx,                              
+                                       struct policy_handle *acct_handle,
+                                       struct lsa_LUID *luid)
+{
+       NTSTATUS status;
+       struct lsa_AddPrivilegesToAccount r;
+       struct lsa_PrivilegeSet privs;
+       BOOL ret = True;
+
+       printf("Testing AddPrivilegesToAccount\n");
+
+       r.in.handle = acct_handle;
+       r.in.privs = &privs;
+
+       privs.count = 1;
+       privs.unknown = 0;
+       privs.set = talloc_array_p(mem_ctx, struct lsa_LUIDAttribute, 1);
+       privs.set[0].luid = *luid;
+       privs.set[0].attribute = 0;
+
+       status = dcerpc_lsa_AddPrivilegesToAccount(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("AddPrivilegesToAccount failed - %s\n", nt_errstr(status));
+               return False;
+       }
+
+       return ret;
+}
+
 static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p, 
                                  TALLOC_CTX *mem_ctx,                            
                                  struct policy_handle *handle,
@@ -344,6 +521,7 @@ static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p,
 {
        NTSTATUS status;
        struct lsa_EnumPrivsAccount r;
+       BOOL ret = True;
 
        printf("Testing EnumPrivsAccount\n");
 
@@ -355,15 +533,20 @@ static BOOL test_EnumPrivsAccount(struct dcerpc_pipe *p,
                return False;
        }
 
-       if (r.out.privs) {
+       if (r.out.privs && r.out.privs->count > 0) {
                int i;
                for (i=0;i<r.out.privs->count;i++) {
                        test_LookupPrivName(p, mem_ctx, handle, 
                                            &r.out.privs->set[i].luid);
                }
+
+               ret &= test_RemovePrivilegesFromAccount(p, mem_ctx, acct_handle, 
+                                                       &r.out.privs->set[0].luid);
+               ret &= test_AddPrivilegesToAccount(p, mem_ctx, acct_handle, 
+                                                  &r.out.privs->set[0].luid);
        }
 
-       return True;
+       return ret;
 }
 
 static BOOL test_Delete(struct dcerpc_pipe *p, 
@@ -419,6 +602,33 @@ static BOOL test_CreateAccount(struct dcerpc_pipe *p,
        return True;
 }
 
+static BOOL test_DeleteTrustedDomain(struct dcerpc_pipe *p, 
+                                    TALLOC_CTX *mem_ctx, 
+                                    struct policy_handle *handle,
+                                    struct lsa_String name)
+{
+       NTSTATUS status;
+       struct lsa_OpenTrustedDomainByName r;
+       struct policy_handle trustdom_handle;
+
+       r.in.handle = handle;
+       r.in.name = name;
+       r.in.access_mask = SEC_STD_DELETE;
+       r.out.trustdom_handle = &trustdom_handle;
+
+       status = dcerpc_lsa_OpenTrustedDomainByName(p, mem_ctx, &r);
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("lsa_OpenTrustedDomainByName failed - %s\n", nt_errstr(status));
+               return False;
+       }
+
+       if (!test_Delete(p, mem_ctx, &trustdom_handle)) {
+               return False;
+       }
+
+       return True;
+}
+
 
 static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p, 
                                     TALLOC_CTX *mem_ctx, 
@@ -443,6 +653,10 @@ static BOOL test_CreateTrustedDomain(struct dcerpc_pipe *p,
        r.out.dom_handle = &dom_handle;
 
        status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
+       if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_COLLISION)) {
+               test_DeleteTrustedDomain(p, mem_ctx, handle, trustinfo.name);
+               status = dcerpc_lsa_CreateTrustedDomain(p, mem_ctx, &r);
+       }
        if (!NT_STATUS_IS_OK(status)) {
                printf("CreateTrustedDomain failed - %s\n", nt_errstr(status));
                return False;
@@ -616,7 +830,7 @@ static BOOL test_QuerySecurity(struct dcerpc_pipe *p,
        NTSTATUS status;
        struct lsa_QuerySecurity r;
 
-       printf("Testing QuerySecuriy\n");
+       printf("Testing QuerySecurity\n");
 
        r.in.handle = acct_handle;
        r.in.sec_info = 7;
@@ -682,26 +896,35 @@ static BOOL test_EnumAccounts(struct dcerpc_pipe *p,
        r.out.sids = &sids1;
 
        resume_handle = 0;
-       status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
-       if (!NT_STATUS_IS_OK(status)) {
-               printf("EnumAccounts failed - %s\n", nt_errstr(status));
-               return False;
-       }
+       while (True) {
+               status = dcerpc_lsa_EnumAccounts(p, mem_ctx, &r);
+               if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES)) {
+                       break;
+               }
+               if (!NT_STATUS_IS_OK(status)) {
+                       printf("EnumAccounts failed - %s\n", nt_errstr(status));
+                       return False;
+               }
 
-       if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
-               return False;
-       }
+               if (!test_LookupSids(p, mem_ctx, handle, &sids1)) {
+                       return False;
+               }
 
-       if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
-               return False;
-       }
+               if (!test_LookupSids2(p, mem_ctx, handle, &sids1)) {
+                       return False;
+               }
 
-       printf("testing all accounts\n");
-       for (i=0;i<sids1.num_sids;i++) {
-               test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
-               test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
+               if (!test_LookupSids3(p, mem_ctx, handle, &sids1)) {
+                       return False;
+               }
+
+               printf("testing all accounts\n");
+               for (i=0;i<sids1.num_sids;i++) {
+                       test_OpenAccount(p, mem_ctx, handle, sids1.sids[i].sid);
+                       test_EnumAccountRights(p, mem_ctx, handle, sids1.sids[i].sid);
+               }
+               printf("\n");
        }
-       printf("\n");
 
        if (sids1.num_sids < 3) {
                return True;
@@ -805,7 +1028,7 @@ static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
 
        r.in.handle = handle;
        r.in.resume_handle = &resume_handle;
-       r.in.max_count = 1000;
+       r.in.max_count = 100;
        r.out.resume_handle = &resume_handle;
        r.out.privs = &privs1;
 
@@ -818,6 +1041,7 @@ static BOOL test_EnumPrivs(struct dcerpc_pipe *p,
 
        for (i = 0; i< privs1.count; i++) {
                test_LookupPrivDisplayName(p, mem_ctx, handle, &privs1.privs[i].name);
+               test_LookupPrivValue(p, mem_ctx, handle, &privs1.privs[i].name);
                if (!test_EnumAccountsWithUserRight(p, mem_ctx, handle, &privs1.privs[i].name)) {
                        ret = False;
                }
@@ -842,7 +1066,7 @@ static BOOL test_EnumTrustDom(struct dcerpc_pipe *p,
 
        r.in.handle = handle;
        r.in.resume_handle = &resume_handle;
-       r.in.num_entries = 1000;
+       r.in.num_entries = 100;
        r.out.domains = &domains;
        r.out.resume_handle = &resume_handle;
 
@@ -1043,6 +1267,32 @@ static BOOL test_QueryInfoPolicy2(struct dcerpc_pipe *p,
        return ret;
 }
 
+static BOOL test_GetUserName(struct dcerpc_pipe *p, 
+                                 TALLOC_CTX *mem_ctx, 
+                                 struct policy_handle *handle)
+{
+       struct lsa_GetUserName r;
+       NTSTATUS status;
+       BOOL ret = True;
+       struct lsa_StringPointer authority_name_p;
+
+       printf("\nTesting GetUserName\n");
+
+       r.in.system_name = "\\";        
+       r.in.account_name = NULL;       
+       r.in.authority_name = &authority_name_p;
+       authority_name_p.string = NULL;
+
+       status = dcerpc_lsa_GetUserName(p, mem_ctx, &r);
+
+       if (!NT_STATUS_IS_OK(status)) {
+               printf("GetUserName failed - %s\n", nt_errstr(status));
+               ret = False;
+       }
+
+       return ret;
+}
+
 static BOOL test_Close(struct dcerpc_pipe *p, 
                       TALLOC_CTX *mem_ctx, 
                       struct policy_handle *handle)
@@ -1135,7 +1385,11 @@ BOOL torture_rpc_lsa(void)
        if (!test_QueryInfoPolicy2(p, mem_ctx, &handle)) {
                ret = False;
        }
-       
+
+       if (!test_GetUserName(p, mem_ctx, &handle)) {
+               ret = False;
+       }
+
 #if 0
        if (!test_Delete(p, mem_ctx, &handle)) {
                ret = False;