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