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