libcli/auth Merge source4/libcli/security and util_sid.c into the common code
[kai/samba.git] / source4 / torture / rpc / lsa_lookup.c
1 /* 
2    Unix SMB/CIFS implementation.
3    test suite for lsa rpc lookup operations
4
5    Copyright (C) Volker Lendecke 2006
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #include "torture/rpc/torture_rpc.h"
23 #include "librpc/gen_ndr/ndr_lsa_c.h"
24 #include "libcli/security/security.h"
25
26 static bool open_policy(struct torture_context *tctx,
27                         struct dcerpc_binding_handle *b,
28                         struct policy_handle **handle)
29 {
30         struct lsa_ObjectAttribute attr;
31         struct lsa_QosInfo qos;
32         struct lsa_OpenPolicy2 r;
33
34         *handle = talloc(tctx, struct policy_handle);
35         if (!*handle) {
36                 return false;
37         }
38
39         qos.len = 0;
40         qos.impersonation_level = 2;
41         qos.context_mode = 1;
42         qos.effective_only = 0;
43
44         attr.len = 0;
45         attr.root_dir = NULL;
46         attr.object_name = NULL;
47         attr.attributes = 0;
48         attr.sec_desc = NULL;
49         attr.sec_qos = &qos;
50
51         r.in.system_name = "\\";
52         r.in.attr = &attr;
53         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
54         r.out.handle = *handle;
55
56         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_OpenPolicy2_r(b, tctx, &r),
57                 "OpenPolicy2 failed");
58
59         return NT_STATUS_IS_OK(r.out.result);
60 }
61
62 static bool get_domainsid(struct torture_context *tctx,
63                           struct dcerpc_binding_handle *b,
64                           struct policy_handle *handle,
65                           struct dom_sid **sid)
66 {
67         struct lsa_QueryInfoPolicy r;
68         union lsa_PolicyInformation *info = NULL;
69
70         r.in.level = LSA_POLICY_INFO_DOMAIN;
71         r.in.handle = handle;
72         r.out.info = &info;
73
74         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryInfoPolicy_r(b, tctx, &r),
75                 "QueryInfoPolicy failed");
76         torture_assert_ntstatus_ok(tctx, r.out.result, "QueryInfoPolicy failed");
77
78         *sid = info->domain.sid;
79         return true;
80 }
81
82 static NTSTATUS lookup_sids(struct torture_context *tctx,
83                             uint16_t level,
84                             struct dcerpc_binding_handle *b,
85                             struct policy_handle *handle,
86                             struct dom_sid **sids, uint32_t num_sids,
87                             struct lsa_TransNameArray *names)
88 {
89         struct lsa_LookupSids r;
90         struct lsa_SidArray sidarray;
91         struct lsa_RefDomainList *domains;
92         uint32_t count = 0;
93         uint32_t i;
94         NTSTATUS status;
95
96         names->count = 0;
97         names->names = NULL;
98
99         sidarray.num_sids = num_sids;
100         sidarray.sids = talloc_array(tctx, struct lsa_SidPtr, num_sids);
101
102         for (i=0; i<num_sids; i++) {
103                 sidarray.sids[i].sid = sids[i];
104         }
105
106         r.in.handle = handle;
107         r.in.sids = &sidarray;
108         r.in.names = names;
109         r.in.level = level;
110         r.in.count = &count;
111         r.out.names = names;
112         r.out.count = &count;
113         r.out.domains = &domains;
114
115         status = dcerpc_lsa_LookupSids_r(b, tctx, &r);
116         if (!NT_STATUS_IS_OK(status)) {
117                 return status;
118         }
119         return r.out.result;
120 }
121
122 static bool test_lookupsids(struct torture_context *tctx,
123                             struct dcerpc_binding_handle *b,
124                             struct policy_handle *handle,
125                             struct dom_sid **sids, uint32_t num_sids,
126                             int level, NTSTATUS expected_result, 
127                             enum lsa_SidType *types)
128 {
129         struct lsa_TransNameArray names;
130         NTSTATUS status;
131         uint32_t i;
132         bool ret = true;
133
134         status = lookup_sids(tctx, level, b, handle, sids, num_sids,
135                              &names);
136         if (!NT_STATUS_EQUAL(status, expected_result)) {
137                 printf("For level %d expected %s, got %s\n",
138                        level, nt_errstr(expected_result),
139                        nt_errstr(status));
140                 return false;
141         }
142
143         if (!NT_STATUS_EQUAL(status, NT_STATUS_OK) &&
144             !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
145                 return true;
146         }
147
148         for (i=0; i<num_sids; i++) {
149                 if (names.names[i].sid_type != types[i]) {
150                         printf("In level %d, for sid %s expected %s, "
151                                "got %s\n", level,
152                                dom_sid_string(tctx, sids[i]),
153                                sid_type_lookup(types[i]),
154                                sid_type_lookup(names.names[i].sid_type));
155                         ret = false;
156                 }
157         }
158         return ret;
159 }
160
161 static bool get_downleveltrust(struct torture_context *tctx, struct dcerpc_binding_handle *b,
162                                struct policy_handle *handle,
163                                struct dom_sid **sid)
164 {
165         struct lsa_EnumTrustDom r;
166         uint32_t resume_handle = 0;
167         struct lsa_DomainList domains;
168         int i;
169
170         r.in.handle = handle;
171         r.in.resume_handle = &resume_handle;
172         r.in.max_size = 1000;
173         r.out.domains = &domains;
174         r.out.resume_handle = &resume_handle;
175
176         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_EnumTrustDom_r(b, tctx, &r),
177                 "EnumTrustDom failed");
178
179         if (NT_STATUS_EQUAL(r.out.result, NT_STATUS_NO_MORE_ENTRIES))
180                 torture_fail(tctx, "no trusts");
181
182         if (domains.count == 0) {
183                 torture_fail(tctx, "no trusts");
184         }
185
186         for (i=0; i<domains.count; i++) {
187                 struct lsa_QueryTrustedDomainInfoBySid q;
188                 union lsa_TrustedDomainInfo *info = NULL;
189
190                 if (domains.domains[i].sid == NULL)
191                         continue;
192
193                 q.in.handle = handle;
194                 q.in.dom_sid = domains.domains[i].sid;
195                 q.in.level = 6;
196                 q.out.info = &info;
197
198                 torture_assert_ntstatus_ok(tctx, dcerpc_lsa_QueryTrustedDomainInfoBySid_r(b, tctx, &q),
199                         "QueryTrustedDomainInfoBySid failed");
200                 if (!NT_STATUS_IS_OK(q.out.result)) continue;
201
202                 if ((info->info_ex.trust_direction & 2) &&
203                     (info->info_ex.trust_type == 1)) {
204                         *sid = domains.domains[i].sid;
205                         return true;
206                 }
207         }
208
209         torture_fail(tctx, "I need a AD DC with an outgoing trust to NT4");
210 }
211
212 #define NUM_SIDS 8
213
214 bool torture_rpc_lsa_lookup(struct torture_context *torture)
215 {
216         NTSTATUS status;
217         struct dcerpc_pipe *p;
218         bool ret = true;
219         struct policy_handle *handle;
220         struct dom_sid *dom_sid = NULL;
221         struct dom_sid *trusted_sid = NULL;
222         struct dom_sid *sids[NUM_SIDS];
223         struct dcerpc_binding_handle *b;
224
225         status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
226         if (!NT_STATUS_IS_OK(status)) {
227                 torture_fail(torture, "unable to connect to table");
228         }
229         b = p->binding_handle;
230
231         ret &= open_policy(torture, b, &handle);
232         if (!ret) return false;
233
234         ret &= get_domainsid(torture, b, handle, &dom_sid);
235         if (!ret) return false;
236
237         ret &= get_downleveltrust(torture, b, handle, &trusted_sid);
238         if (!ret) return false;
239
240         torture_comment(torture, "domain sid: %s\n", 
241                                         dom_sid_string(torture, dom_sid));
242
243         sids[0] = dom_sid_parse_talloc(torture, "S-1-1-0");
244         sids[1] = dom_sid_parse_talloc(torture, "S-1-5-4");
245         sids[2] = dom_sid_parse_talloc(torture, "S-1-5-32");
246         sids[3] = dom_sid_parse_talloc(torture, "S-1-5-32-545");
247         sids[4] = dom_sid_dup(torture, dom_sid);
248         sids[5] = dom_sid_add_rid(torture, dom_sid, 512);
249         sids[6] = dom_sid_dup(torture, trusted_sid);
250         sids[7] = dom_sid_add_rid(torture, trusted_sid, 512);
251
252         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 0,
253                                NT_STATUS_INVALID_PARAMETER, NULL);
254
255         {
256                 enum lsa_SidType types[NUM_SIDS] =
257                         { SID_NAME_WKN_GRP, SID_NAME_WKN_GRP, SID_NAME_DOMAIN,
258                           SID_NAME_ALIAS, SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
259                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP };
260
261                 ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 1,
262                                        NT_STATUS_OK, types);
263         }
264
265         {
266                 enum lsa_SidType types[NUM_SIDS] =
267                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
268                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
269                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
270                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP };
271                 ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 2,
272                                        STATUS_SOME_UNMAPPED, types);
273         }
274
275         {
276                 enum lsa_SidType types[NUM_SIDS] =
277                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
278                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
279                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
280                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
281                 ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 3,
282                                        STATUS_SOME_UNMAPPED, types);
283         }
284
285         {
286                 enum lsa_SidType types[NUM_SIDS] =
287                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
288                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
289                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
290                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
291                 ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 4,
292                                        STATUS_SOME_UNMAPPED, types);
293         }
294
295         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 5,
296                                NT_STATUS_NONE_MAPPED, NULL);
297
298         {
299                 enum lsa_SidType types[NUM_SIDS] =
300                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
301                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
302                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
303                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
304                 ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 6,
305                                        STATUS_SOME_UNMAPPED, types);
306         }
307
308         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 7,
309                                NT_STATUS_INVALID_PARAMETER, NULL);
310         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 8,
311                                NT_STATUS_INVALID_PARAMETER, NULL);
312         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 9,
313                                NT_STATUS_INVALID_PARAMETER, NULL);
314         ret &= test_lookupsids(torture, b, handle, sids, NUM_SIDS, 10,
315                                NT_STATUS_INVALID_PARAMETER, NULL);
316
317         return ret;
318 }
319
320 static bool test_LookupSidsReply(struct torture_context *tctx,
321                                  struct dcerpc_pipe *p)
322 {
323         struct policy_handle *handle;
324
325         struct dom_sid **sids;
326         uint32_t num_sids = 1;
327
328         struct lsa_LookupSids r;
329         struct lsa_SidArray sidarray;
330         struct lsa_RefDomainList *domains = NULL;
331         struct lsa_TransNameArray names;
332         uint32_t count = 0;
333
334         uint32_t i;
335         const char *dom_sid = "S-1-5-21-1111111111-2222222222-3333333333";
336         const char *dom_admin_sid;
337         struct dcerpc_binding_handle *b = p->binding_handle;
338
339         if (!open_policy(tctx, b, &handle)) {
340                 return false;
341         }
342
343         dom_admin_sid = talloc_asprintf(tctx, "%s-%d", dom_sid, 512);
344
345         sids = talloc_array(tctx, struct dom_sid *, num_sids);
346
347         sids[0] = dom_sid_parse_talloc(tctx, dom_admin_sid);
348
349         names.count = 0;
350         names.names = NULL;
351
352         sidarray.num_sids = num_sids;
353         sidarray.sids = talloc_array(tctx, struct lsa_SidPtr, num_sids);
354
355         for (i=0; i<num_sids; i++) {
356                 sidarray.sids[i].sid = sids[i];
357         }
358
359         r.in.handle     = handle;
360         r.in.sids       = &sidarray;
361         r.in.names      = &names;
362         r.in.level      = LSA_LOOKUP_NAMES_ALL;
363         r.in.count      = &count;
364         r.out.names     = &names;
365         r.out.count     = &count;
366         r.out.domains   = &domains;
367
368         torture_assert_ntstatus_ok(tctx, dcerpc_lsa_LookupSids_r(b, tctx, &r),
369                 "LookupSids failed");
370
371         torture_assert_ntstatus_equal(tctx, r.out.result, NT_STATUS_NONE_MAPPED,
372                 "unexpected error code");
373
374         torture_assert_int_equal(tctx, names.count, num_sids,
375                 "unexpected names count");
376         torture_assert(tctx, names.names,
377                 "unexpected names pointer");
378         torture_assert_str_equal(tctx, names.names[0].name.string, dom_admin_sid,
379                 "unexpected names[0].string");
380
381 #if 0
382         /* vista sp1 passes, w2k3 sp2 fails */
383         torture_assert_int_equal(tctx, domains->count, num_sids,
384                 "unexpected domains count");
385         torture_assert(tctx, domains->domains,
386                 "unexpected domains pointer");
387         torture_assert_str_equal(tctx, dom_sid_string(tctx, domains->domains[0].sid), dom_sid,
388                 "unexpected domain sid");
389 #endif
390
391         return true;
392 }
393
394 /* check for lookup sids results */
395 struct torture_suite *torture_rpc_lsa_lookup_sids(TALLOC_CTX *mem_ctx)
396 {
397         struct torture_suite *suite;
398         struct torture_rpc_tcase *tcase;
399
400         suite = torture_suite_create(mem_ctx, "LSA-LOOKUPSIDS");
401         tcase = torture_suite_add_rpc_iface_tcase(suite, "lsa",
402                                                   &ndr_table_lsarpc);
403
404         torture_rpc_tcase_add_test(tcase, "LookupSidsReply", test_LookupSidsReply);
405
406         return suite;
407 }