r7864: fixed some const bugs
[kamenim/samba.git] / source4 / torture / libnet / userman.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 "librpc/gen_ndr/ndr_samr.h"
24 #include "libnet/composite.h"
25 #include "libcli/composite/monitor.h"
26
27 #define TEST_USERNAME  "libnetusermantest"
28
29
30 static BOOL test_opendomain(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
31                             struct policy_handle *handle, struct samr_String *domname)
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         return True;
78 }
79
80
81 static BOOL test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
82                          struct policy_handle *domain_handle,
83                          const char *name)
84 {
85         NTSTATUS status;
86         BOOL ret = True;
87         struct libnet_rpc_useradd user;
88         
89         user.in.domain_handle = *domain_handle;
90         user.in.username      = name;
91
92         printf("Testing libnet_rpc_useradd\n");
93
94         status = libnet_rpc_useradd(p, mem_ctx, &user);
95         if (!NT_STATUS_IS_OK(status)) {
96                 printf("Failed to call sync rpc_composite_userinfo - %s\n", nt_errstr(status));
97                 return False;
98         }
99         
100         return ret;
101 }
102
103
104 static void msg_handler(struct monitor_msg *m)
105 {
106         switch (m->type) {
107         case rpc_create_user:
108                 printf("monitor_msg: user created (rid=%d)\n", m->data.rpc_create_user.rid);
109                 break;
110         }
111 }
112
113
114 static BOOL test_useradd_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
115                                struct policy_handle *handle, const char* username)
116 {
117         NTSTATUS status;
118         struct composite_context *c;
119         struct libnet_rpc_useradd user;
120
121         user.in.domain_handle = *handle;
122         user.in.username      = username;
123         
124         printf("Testing async libnet_rpc_useradd\n");
125         
126         c = libnet_rpc_useradd_send(p, &user, msg_handler);
127         if (!c) {
128                 printf("Failed to call async libnet_rpc_useradd\n");
129                 return False;
130         }
131
132         status = libnet_rpc_useradd_recv(c, mem_ctx, &user);
133         if (!NT_STATUS_IS_OK(status)) {
134                 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status));
135                 return False;
136         }
137
138         return True;
139
140 }
141
142
143 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
144                          struct policy_handle *domain_handle, const char *username)
145 {
146         NTSTATUS status;
147         struct samr_LookupNames r1;
148         struct samr_OpenUser r2;
149         struct samr_DeleteUser r3;
150         struct samr_String names[2];
151         uint32_t rid;
152         struct policy_handle user_handle;
153
154         names[0].string = username;
155
156         r1.in.domain_handle  = domain_handle;
157         r1.in.num_names      = 1;
158         r1.in.names          = names;
159         
160         printf("user account lookup '%s'\n", username);
161
162         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
163         if (!NT_STATUS_IS_OK(status)) {
164                 printf("LookupNames failed - %s\n", nt_errstr(status));
165                 return False;
166         }
167
168         rid = r1.out.rids.ids[0];
169         
170         r2.in.domain_handle  = domain_handle;
171         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
172         r2.in.rid            = rid;
173         r2.out.user_handle   = &user_handle;
174
175         printf("opening user account\n");
176
177         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
178         if (!NT_STATUS_IS_OK(status)) {
179                 printf("OpenUser failed - %s\n", nt_errstr(status));
180                 return False;
181         }
182
183         r3.in.user_handle  = &user_handle;
184         r3.out.user_handle = &user_handle;
185
186         printf("deleting user account\n");
187         
188         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
189         if (!NT_STATUS_IS_OK(status)) {
190                 printf("DeleteUser failed - %s\n", nt_errstr(status));
191                 return False;
192         }
193         
194         return True;
195 }
196
197
198 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
199                             struct policy_handle *handle, const char* user)
200 {
201         NTSTATUS status;
202         struct policy_handle user_handle;
203         struct samr_String username;
204         struct samr_CreateUser r1;
205         struct samr_Close r2;
206         uint32_t user_rid;
207
208         username.string = user;
209         
210         r1.in.domain_handle = handle;
211         r1.in.account_name = &username;
212         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
213         r1.out.user_handle = &user_handle;
214         r1.out.rid = &user_rid;
215
216         printf("creating user '%s'\n", username.string);
217         
218         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
219         if (!NT_STATUS_IS_OK(status)) {
220                 printf("CreateUser failed - %s\n", nt_errstr(status));
221
222                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
223                         printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
224                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
225                                 return False;
226                         }
227
228                         printf("creating user account\n");
229                         
230                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
231                         if (!NT_STATUS_IS_OK(status)) {
232                                 printf("CreateUser failed - %s\n", nt_errstr(status));
233                                 return False;
234                         }
235                         return True;
236                 }               
237                 return False;
238         }
239
240         r2.in.handle = &user_handle;
241         r2.out.handle = &user_handle;
242         
243         printf("closing user '%s'\n", username.string);
244
245         status = dcerpc_samr_Close(p, mem_ctx, &r2);
246         if (!NT_STATUS_IS_OK(status)) {
247                 printf("Close failed - %s\n", nt_errstr(status));
248                 return False;
249         }
250
251         return True;
252 }
253
254
255 static BOOL test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
256                          struct policy_handle *handle, const char *username)
257 {
258         NTSTATUS status;
259         struct libnet_rpc_userdel user;
260         
261         user.in.domain_handle = *handle;
262         user.in.username = username;
263         
264         status = libnet_rpc_userdel(p, mem_ctx, &user);
265         if (!NT_STATUS_IS_OK(status)) {
266                 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
267                 return False;
268         }
269
270         return True;
271 }
272
273
274 BOOL torture_useradd(void)
275 {
276         NTSTATUS status;
277         const char *binding;
278         struct dcerpc_pipe *p;
279         struct policy_handle h;
280         struct samr_String domain_name;
281         const char *name = TEST_USERNAME;
282         TALLOC_CTX *mem_ctx;
283         BOOL ret = True;
284
285         mem_ctx = talloc_init("test_useradd");
286         binding = lp_parm_string(-1, "torture", "binding");
287
288         status = torture_rpc_connection(mem_ctx, 
289                                         &p,
290                                         DCERPC_SAMR_NAME,
291                                         DCERPC_SAMR_UUID,
292                                         DCERPC_SAMR_VERSION);
293         
294         if (!NT_STATUS_IS_OK(status)) {
295                 return False;
296         }
297
298         domain_name.string = lp_workgroup();
299         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
300                 ret = False;
301                 goto done;
302         }
303
304         if (!test_useradd(p, mem_ctx, &h, name)) {
305                 ret = False;
306                 goto done;
307         }
308
309         if (!test_cleanup(p, mem_ctx, &h, name)) {
310                 ret = False;
311                 goto done;
312         }
313
314         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
315                 ret = False;
316                 goto done;
317         }
318
319         if (!test_useradd_async(p, mem_ctx, &h, name)) {
320                 ret = False;
321                 goto done;
322         }
323
324         if (!test_cleanup(p, mem_ctx, &h, name)) {
325                 ret = False;
326                 goto done;
327         }
328
329 done:
330         talloc_free(mem_ctx);
331         return ret;
332 }
333
334
335 BOOL torture_userdel(void)
336 {
337         NTSTATUS status;
338         const char *binding;
339         struct dcerpc_pipe *p;
340         struct policy_handle h;
341         struct samr_String domain_name;
342         const char *name = TEST_USERNAME;
343         TALLOC_CTX *mem_ctx;
344         BOOL ret = True;
345
346         mem_ctx = talloc_init("test_userdel");
347         binding = lp_parm_string(-1, "torture", "binding");
348
349         status = torture_rpc_connection(mem_ctx, 
350                                         &p,
351                                         DCERPC_SAMR_NAME,
352                                         DCERPC_SAMR_UUID,
353                                         DCERPC_SAMR_VERSION);
354         
355         if (!NT_STATUS_IS_OK(status)) {
356                 return False;
357         }
358
359         domain_name.string = lp_workgroup();
360         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
361                 ret = False;
362                 goto done;
363         }
364
365         if (!test_createuser(p, mem_ctx, &h, name)) {
366                 ret = False;
367                 goto done;
368         }
369         
370         if (!test_userdel(p, mem_ctx, &h, name)) {
371                 ret = False;
372                 goto done;
373         }
374         
375 done:
376         talloc_free(mem_ctx);
377         return ret;
378 }