Merge Samba3 and Samba4 together
[kai/samba-autobuild/.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/torture.h"
23 #include "lib/events/events.h"
24 #include "libnet/libnet_join.h"
25 #include "torture/rpc/rpc.h"
26 #include "librpc/gen_ndr/ndr_lsa_c.h"
27 #include "libcli/security/security.h"
28
29 static bool open_policy(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *p,
30                         struct policy_handle **handle)
31 {
32         struct lsa_ObjectAttribute attr;
33         struct lsa_QosInfo qos;
34         struct lsa_OpenPolicy2 r;
35         NTSTATUS status;
36
37         *handle = talloc(mem_ctx, struct policy_handle);
38         if (!*handle) {
39                 return false;
40         }
41
42         qos.len = 0;
43         qos.impersonation_level = 2;
44         qos.context_mode = 1;
45         qos.effective_only = 0;
46
47         attr.len = 0;
48         attr.root_dir = NULL;
49         attr.object_name = NULL;
50         attr.attributes = 0;
51         attr.sec_desc = NULL;
52         attr.sec_qos = &qos;
53
54         r.in.system_name = "\\";
55         r.in.attr = &attr;
56         r.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
57         r.out.handle = *handle;
58
59         status = dcerpc_lsa_OpenPolicy2(p, mem_ctx, &r);
60
61         return NT_STATUS_IS_OK(status);
62 }
63
64 static bool get_domainsid(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *p,
65                           struct policy_handle *handle,
66                           struct dom_sid **sid)
67 {
68         struct lsa_QueryInfoPolicy r;
69         NTSTATUS status;
70
71         r.in.level = LSA_POLICY_INFO_DOMAIN;
72         r.in.handle = handle;
73
74         status = dcerpc_lsa_QueryInfoPolicy(p, mem_ctx, &r);
75         if (!NT_STATUS_IS_OK(status)) return false;
76
77         *sid = r.out.info->domain.sid;
78         return true;
79 }
80
81 static NTSTATUS lookup_sids(TALLOC_CTX *mem_ctx, uint16_t level,
82                             struct dcerpc_pipe *p,
83                             struct policy_handle *handle,
84                             struct dom_sid **sids, uint32_t num_sids,
85                             struct lsa_TransNameArray *names)
86 {
87         struct lsa_LookupSids r;
88         struct lsa_SidArray sidarray;
89         uint32_t count = 0;
90         uint32_t i;
91
92         names->count = 0;
93         names->names = NULL;
94
95         sidarray.num_sids = num_sids;
96         sidarray.sids = talloc_array(mem_ctx, struct lsa_SidPtr, num_sids);
97
98         for (i=0; i<num_sids; i++) {
99                 sidarray.sids[i].sid = sids[i];
100         }
101
102         r.in.handle = handle;
103         r.in.sids = &sidarray;
104         r.in.names = names;
105         r.in.level = level;
106         r.in.count = &count;
107         r.out.names = names;
108         r.out.count = &count;
109
110         return dcerpc_lsa_LookupSids(p, mem_ctx, &r);
111 }
112
113 static const char *sid_type_lookup(enum lsa_SidType r)
114 {
115         switch (r) {
116                 case SID_NAME_USE_NONE: return "SID_NAME_USE_NONE"; break;
117                 case SID_NAME_USER: return "SID_NAME_USER"; break;
118                 case SID_NAME_DOM_GRP: return "SID_NAME_DOM_GRP"; break;
119                 case SID_NAME_DOMAIN: return "SID_NAME_DOMAIN"; break;
120                 case SID_NAME_ALIAS: return "SID_NAME_ALIAS"; break;
121                 case SID_NAME_WKN_GRP: return "SID_NAME_WKN_GRP"; break;
122                 case SID_NAME_DELETED: return "SID_NAME_DELETED"; break;
123                 case SID_NAME_INVALID: return "SID_NAME_INVALID"; break;
124                 case SID_NAME_UNKNOWN: return "SID_NAME_UNKNOWN"; break;
125                 case SID_NAME_COMPUTER: return "SID_NAME_COMPUTER"; break;
126         }
127         return "Invalid sid type\n";
128 }
129
130 static bool test_lookupsids(TALLOC_CTX *mem_ctx, struct dcerpc_pipe *p,
131                             struct policy_handle *handle,
132                             struct dom_sid **sids, uint32_t num_sids,
133                             int level, NTSTATUS expected_result, 
134                             enum lsa_SidType *types)
135 {
136         struct lsa_TransNameArray names;
137         NTSTATUS status;
138         uint32_t i;
139         bool ret = true;
140
141         status = lookup_sids(mem_ctx, level, p, handle, sids, num_sids,
142                              &names);
143         if (!NT_STATUS_EQUAL(status, expected_result)) {
144                 printf("For level %d expected %s, got %s\n",
145                        level, nt_errstr(expected_result),
146                        nt_errstr(status));
147                 return false;
148         }
149
150         if (!NT_STATUS_EQUAL(status, NT_STATUS_OK) &&
151             !NT_STATUS_EQUAL(status, STATUS_SOME_UNMAPPED)) {
152                 return true;
153         }
154
155         for (i=0; i<num_sids; i++) {
156                 if (names.names[i].sid_type != types[i]) {
157                         printf("In level %d, for sid %s expected %s, "
158                                "got %s\n", level,
159                                dom_sid_string(mem_ctx, sids[i]),
160                                sid_type_lookup(types[i]),
161                                sid_type_lookup(names.names[i].sid_type));
162                         ret = false;
163                 }
164         }
165         return ret;
166 }
167
168 static bool get_downleveltrust(struct torture_context *tctx, struct dcerpc_pipe *p,
169                                struct policy_handle *handle,
170                                struct dom_sid **sid)
171 {
172         struct lsa_EnumTrustDom r;
173         uint32_t resume_handle = 0;
174         struct lsa_DomainList domains;
175         NTSTATUS status;
176         int i;
177
178         r.in.handle = handle;
179         r.in.resume_handle = &resume_handle;
180         r.in.max_size = 1000;
181         r.out.domains = &domains;
182         r.out.resume_handle = &resume_handle;
183
184         status = dcerpc_lsa_EnumTrustDom(p, tctx, &r);
185
186         if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MORE_ENTRIES))
187                 torture_fail(tctx, "no trusts");
188
189         if (domains.count == 0) {
190                 torture_fail(tctx, "no trusts");
191         }
192
193         for (i=0; i<domains.count; i++) {
194                 struct lsa_QueryTrustedDomainInfoBySid q;
195
196                 if (domains.domains[i].sid == NULL)
197                         continue;
198
199                 q.in.handle = handle;
200                 q.in.dom_sid = domains.domains[i].sid;
201                 q.in.level = 6;
202                 status = dcerpc_lsa_QueryTrustedDomainInfoBySid(p, tctx, &q);
203                 if (!NT_STATUS_IS_OK(status)) continue;
204
205                 if ((q.out.info->info_ex.trust_direction & 2) &&
206                     (q.out.info->info_ex.trust_type == 1)) {
207                         *sid = domains.domains[i].sid;
208                         return true;
209                 }
210         }
211
212         torture_fail(tctx, "I need a AD DC with an outgoing trust to NT4");
213 }
214
215 #define NUM_SIDS 8
216
217 bool torture_rpc_lsa_lookup(struct torture_context *torture)
218 {
219         NTSTATUS status;
220         struct dcerpc_pipe *p;
221         bool ret = true;
222         struct policy_handle *handle;
223         struct dom_sid *dom_sid;
224         struct dom_sid *trusted_sid;
225         struct dom_sid *sids[NUM_SIDS];
226
227         status = torture_rpc_connection(torture, &p, &ndr_table_lsarpc);
228         if (!NT_STATUS_IS_OK(status)) {
229                 torture_fail(torture, "unable to connect to table");
230         }
231
232         ret &= open_policy(torture, p, &handle);
233         if (!ret) return false;
234
235         ret &= get_domainsid(torture, p, handle, &dom_sid);
236         if (!ret) return false;
237
238         ret &= get_downleveltrust(torture, p, handle, &trusted_sid);
239         if (!ret) return false;
240
241         torture_comment(torture, "domain sid: %s\n", 
242                                         dom_sid_string(torture, dom_sid));
243
244         sids[0] = dom_sid_parse_talloc(torture, "S-1-1-0");
245         sids[1] = dom_sid_parse_talloc(torture, "S-1-5-4");
246         sids[2] = dom_sid_parse_talloc(torture, "S-1-5-32");
247         sids[3] = dom_sid_parse_talloc(torture, "S-1-5-32-545");
248         sids[4] = dom_sid_dup(torture, dom_sid);
249         sids[5] = dom_sid_add_rid(torture, dom_sid, 512);
250         sids[6] = dom_sid_dup(torture, trusted_sid);
251         sids[7] = dom_sid_add_rid(torture, trusted_sid, 512);
252
253         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 0,
254                                NT_STATUS_INVALID_PARAMETER, NULL);
255
256         {
257                 enum lsa_SidType types[NUM_SIDS] =
258                         { SID_NAME_WKN_GRP, SID_NAME_WKN_GRP, SID_NAME_DOMAIN,
259                           SID_NAME_ALIAS, SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
260                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP };
261
262                 ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 1,
263                                        NT_STATUS_OK, types);
264         }
265
266         {
267                 enum lsa_SidType types[NUM_SIDS] =
268                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
269                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
270                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
271                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP };
272                 ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 2,
273                                        STATUS_SOME_UNMAPPED, types);
274         }
275
276         {
277                 enum lsa_SidType types[NUM_SIDS] =
278                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
279                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
280                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
281                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
282                 ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 3,
283                                        STATUS_SOME_UNMAPPED, types);
284         }
285
286         {
287                 enum lsa_SidType types[NUM_SIDS] =
288                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
289                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
290                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
291                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
292                 ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 4,
293                                        STATUS_SOME_UNMAPPED, types);
294         }
295
296         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 5,
297                                NT_STATUS_NONE_MAPPED, NULL);
298
299         {
300                 enum lsa_SidType types[NUM_SIDS] =
301                         { SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
302                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN,
303                           SID_NAME_DOMAIN, SID_NAME_DOM_GRP,
304                           SID_NAME_UNKNOWN, SID_NAME_UNKNOWN };
305                 ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 6,
306                                        STATUS_SOME_UNMAPPED, types);
307         }
308
309         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 7,
310                                NT_STATUS_INVALID_PARAMETER, NULL);
311         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 8,
312                                NT_STATUS_INVALID_PARAMETER, NULL);
313         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 9,
314                                NT_STATUS_INVALID_PARAMETER, NULL);
315         ret &= test_lookupsids(torture, p, handle, sids, NUM_SIDS, 10,
316                                NT_STATUS_INVALID_PARAMETER, NULL);
317
318         return ret;
319 }