r24755: Use common code for finding the RPC binding in the torture tests.
[gd/samba-autobuild/.git] / source4 / torture / libnet / userinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Test suite for libnet calls.
4
5    Copyright (C) Rafal Szczesniak 2005
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/rpc.h"
23 #include "libnet/libnet.h"
24 #include "libcli/security/security.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26
27 #define TEST_USERNAME  "libnetuserinfotest"
28
29 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
30                             struct policy_handle *handle, struct lsa_String *domname,
31                             struct dom_sid2 *sid)
32 {
33         NTSTATUS status;
34         struct policy_handle h, domain_handle;
35         struct samr_Connect r1;
36         struct samr_LookupDomain r2;
37         struct samr_OpenDomain r3;
38         
39         printf("connecting\n");
40         
41         r1.in.system_name = 0;
42         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
43         r1.out.connect_handle = &h;
44         
45         status = dcerpc_samr_Connect(p, mem_ctx, &r1);
46         if (!NT_STATUS_IS_OK(status)) {
47                 printf("Connect failed - %s\n", nt_errstr(status));
48                 return False;
49         }
50         
51         r2.in.connect_handle = &h;
52         r2.in.domain_name = domname;
53
54         printf("domain lookup on %s\n", domname->string);
55
56         status = dcerpc_samr_LookupDomain(p, mem_ctx, &r2);
57         if (!NT_STATUS_IS_OK(status)) {
58                 printf("LookupDomain failed - %s\n", nt_errstr(status));
59                 return False;
60         }
61
62         r3.in.connect_handle = &h;
63         r3.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
64         r3.in.sid = r2.out.sid;
65         r3.out.domain_handle = &domain_handle;
66
67         printf("opening domain\n");
68
69         status = dcerpc_samr_OpenDomain(p, mem_ctx, &r3);
70         if (!NT_STATUS_IS_OK(status)) {
71                 printf("OpenDomain failed - %s\n", nt_errstr(status));
72                 return False;
73         } else {
74                 *handle = domain_handle;
75         }
76
77         *sid = *r2.out.sid;
78         return True;
79 }
80
81
82 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
83                          struct policy_handle *domain_handle, const char *username)
84 {
85         NTSTATUS status;
86         struct samr_LookupNames r1;
87         struct samr_OpenUser r2;
88         struct samr_DeleteUser r3;
89         struct lsa_String names[2];
90         uint32_t rid;
91         struct policy_handle user_handle;
92
93         names[0].string = username;
94
95         r1.in.domain_handle  = domain_handle;
96         r1.in.num_names      = 1;
97         r1.in.names          = names;
98         
99         printf("user account lookup '%s'\n", username);
100
101         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
102         if (!NT_STATUS_IS_OK(status)) {
103                 printf("LookupNames failed - %s\n", nt_errstr(status));
104                 return False;
105         }
106
107         rid = r1.out.rids.ids[0];
108         
109         r2.in.domain_handle  = domain_handle;
110         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
111         r2.in.rid            = rid;
112         r2.out.user_handle   = &user_handle;
113
114         printf("opening user account\n");
115
116         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
117         if (!NT_STATUS_IS_OK(status)) {
118                 printf("OpenUser failed - %s\n", nt_errstr(status));
119                 return False;
120         }
121
122         r3.in.user_handle  = &user_handle;
123         r3.out.user_handle = &user_handle;
124
125         printf("deleting user account\n");
126         
127         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
128         if (!NT_STATUS_IS_OK(status)) {
129                 printf("DeleteUser failed - %s\n", nt_errstr(status));
130                 return False;
131         }
132         
133         return True;
134 }
135
136
137 static BOOL test_create(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
138                         struct policy_handle *handle, const char *name, uint32_t *rid)
139 {
140         NTSTATUS status;
141         struct lsa_String username;
142         struct samr_CreateUser r;
143         struct policy_handle user_handle;
144         
145         username.string = name;
146         
147         r.in.domain_handle = handle;
148         r.in.account_name  = &username;
149         r.in.access_mask   = SEC_FLAG_MAXIMUM_ALLOWED;
150         r.out.user_handle  = &user_handle;
151         r.out.rid          = rid;
152
153         printf("creating user account %s\n", name);
154
155         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
156         if (!NT_STATUS_IS_OK(status)) {
157                 printf("CreateUser failed - %s\n", nt_errstr(status));
158
159                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
160                         printf("User (%s) already exists - attempting to delete and recreate account again\n", name);
161                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
162                                 return False;
163                         }
164
165                         printf("creating user account\n");
166                         
167                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r);
168                         if (!NT_STATUS_IS_OK(status)) {
169                                 printf("CreateUser failed - %s\n", nt_errstr(status));
170                                 return False;
171                         }
172                         return True;
173                 }
174                 return False;
175         }
176
177         return True;
178 }
179
180
181 static BOOL test_userinfo(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
182                           struct policy_handle *domain_handle,
183                           struct dom_sid2 *domain_sid, const char* user_name,
184                           uint32_t *rid)
185 {
186         const uint16_t level = 5;
187         NTSTATUS status;
188         struct libnet_rpc_userinfo user;
189         struct dom_sid *user_sid;
190         
191         user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
192         
193         user.in.domain_handle = *domain_handle;
194         user.in.sid           = dom_sid_string(mem_ctx, user_sid);
195         user.in.level         = level;       /* this should be extended */
196
197         printf("Testing sync libnet_rpc_userinfo (SID argument)\n");
198         status = libnet_rpc_userinfo(p, mem_ctx, &user);
199         if (!NT_STATUS_IS_OK(status)) {
200                 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
201                 return False;
202         }
203
204         ZERO_STRUCT(user);
205
206         user.in.domain_handle = *domain_handle;
207         user.in.sid           = NULL;
208         user.in.username      = TEST_USERNAME;
209         user.in.level         = level;
210
211         printf("Testing sync libnet_rpc_userinfo (username argument)\n");
212         status = libnet_rpc_userinfo(p, mem_ctx, &user);
213         if (!NT_STATUS_IS_OK(status)) {
214                 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
215                 return False;
216         }
217
218         return True;
219 }
220
221
222 static void msg_handler(struct monitor_msg *m)
223 {
224         struct msg_rpc_open_user *msg_open;
225         struct msg_rpc_query_user *msg_query;
226         struct msg_rpc_close_user *msg_close;
227
228         switch (m->type) {
229         case mon_SamrOpenUser:
230                 msg_open = (struct msg_rpc_open_user*)m->data;
231                 printf("monitor_msg: user opened (rid=%d, access_mask=0x%08x)\n",
232                        msg_open->rid, msg_open->access_mask);
233                 break;
234         case mon_SamrQueryUser:
235                 msg_query = (struct msg_rpc_query_user*)m->data;
236                 printf("monitor_msg: user queried (level=%d)\n", msg_query->level);
237                 break;
238         case mon_SamrCloseUser:
239                 msg_close = (struct msg_rpc_close_user*)m->data;
240                 printf("monitor_msg: user closed (rid=%d)\n", msg_close->rid);
241                 break;
242         }
243 }
244
245
246 static BOOL test_userinfo_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
247                                 struct policy_handle *domain_handle,
248                                 struct dom_sid2 *domain_sid, const char* user_name,
249                                 uint32_t *rid)
250 {
251         const uint16_t level = 10;
252         NTSTATUS status;
253         struct composite_context *c;
254         struct libnet_rpc_userinfo user;
255         struct dom_sid *user_sid;
256
257         user_sid = dom_sid_add_rid(mem_ctx, domain_sid, *rid);
258
259         user.in.domain_handle = *domain_handle;
260         user.in.sid           = dom_sid_string(mem_ctx, user_sid);
261         user.in.level         = level;       /* this should be extended */
262
263         printf("Testing async libnet_rpc_userinfo (SID argument)\n");
264
265         c = libnet_rpc_userinfo_send(p, &user, msg_handler);
266         if (!c) {
267                 printf("Failed to call sync libnet_rpc_userinfo_send\n");
268                 return False;
269         }
270
271         status = libnet_rpc_userinfo_recv(c, mem_ctx, &user);
272         if (!NT_STATUS_IS_OK(status)) {
273                 printf("Calling async libnet_rpc_userinfo failed - %s\n", nt_errstr(status));
274                 return False;
275         }
276
277         ZERO_STRUCT(user);
278
279         user.in.domain_handle = *domain_handle;
280         user.in.sid           = NULL;
281         user.in.username      = TEST_USERNAME;
282         user.in.level         = level;
283
284         printf("Testing async libnet_rpc_userinfo (username argument)\n");
285
286         c = libnet_rpc_userinfo_send(p, &user, msg_handler);
287         if (!c) {
288                 printf("Failed to call sync libnet_rpc_userinfo_send\n");
289                 return False;
290         }
291
292         status = libnet_rpc_userinfo_recv(c, mem_ctx, &user);
293         if (!NT_STATUS_IS_OK(status)) {
294                 printf("Calling async libnet_rpc_userinfo failed - %s\n", nt_errstr(status));
295                 return False;
296         }
297
298         return True;
299 }
300
301
302 BOOL torture_userinfo(struct torture_context *torture)
303 {
304         NTSTATUS status;
305         struct dcerpc_pipe *p;
306         TALLOC_CTX *mem_ctx;
307         BOOL ret = True;
308         struct policy_handle h;
309         struct lsa_String name;
310         struct dom_sid2 sid;
311         uint32_t rid;
312
313         mem_ctx = talloc_init("test_userinfo");
314
315         status = torture_rpc_connection(torture, 
316                                         &p,
317                                         &ndr_table_samr);
318         
319         if (!NT_STATUS_IS_OK(status)) {
320                 return False;
321         }
322
323         name.string = lp_workgroup();
324
325         /*
326          * Testing synchronous version
327          */
328         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
329                 ret = False;
330                 goto done;
331         }
332
333         if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
334                 ret = False;
335                 goto done;
336         }
337
338         if (!test_userinfo(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
339                 ret = False;
340                 goto done;
341         }
342
343         if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
344                 ret = False;
345                 goto done;
346         }
347
348         /*
349          * Testing asynchronous version and monitor messages
350          */
351         if (!test_opendomain(p, mem_ctx, &h, &name, &sid)) {
352                 ret = False;
353                 goto done;
354         }
355
356         if (!test_create(p, mem_ctx, &h, TEST_USERNAME, &rid)) {
357                 ret = False;
358                 goto done;
359         }
360
361         if (!test_userinfo_async(p, mem_ctx, &h, &sid, TEST_USERNAME, &rid)) {
362                 ret = False;
363                 goto done;
364         }
365
366         if (!test_cleanup(p, mem_ctx, &h, TEST_USERNAME)) {
367                 ret = False;
368                 goto done;
369         }
370
371 done:
372         talloc_free(mem_ctx);
373
374         return ret;
375 }