r10504: - seperate implementation specific stuff, from the generic composite
[jelmer/samba4-debian.git] / source / 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 "libnet/userman.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 lsa_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         struct msg_rpc_create_user *msg_create;
107
108         switch (m->type) {
109         case rpc_create_user:
110                 msg_create = (struct msg_rpc_create_user*)m->data;
111                 printf("monitor_msg: user created (rid=%d)\n", msg_create->rid);
112                 break;
113         }
114 }
115
116
117 static BOOL test_useradd_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
118                                struct policy_handle *handle, const char* username)
119 {
120         NTSTATUS status;
121         struct composite_context *c;
122         struct libnet_rpc_useradd user;
123
124         user.in.domain_handle = *handle;
125         user.in.username      = username;
126         
127         printf("Testing async libnet_rpc_useradd\n");
128         
129         c = libnet_rpc_useradd_send(p, &user, msg_handler);
130         if (!c) {
131                 printf("Failed to call async libnet_rpc_useradd\n");
132                 return False;
133         }
134
135         status = libnet_rpc_useradd_recv(c, mem_ctx, &user);
136         if (!NT_STATUS_IS_OK(status)) {
137                 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status));
138                 return False;
139         }
140
141         return True;
142
143 }
144
145
146 static BOOL test_cleanup(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
147                          struct policy_handle *domain_handle, const char *username)
148 {
149         NTSTATUS status;
150         struct samr_LookupNames r1;
151         struct samr_OpenUser r2;
152         struct samr_DeleteUser r3;
153         struct lsa_String names[2];
154         uint32_t rid;
155         struct policy_handle user_handle;
156
157         names[0].string = username;
158
159         r1.in.domain_handle  = domain_handle;
160         r1.in.num_names      = 1;
161         r1.in.names          = names;
162         
163         printf("user account lookup '%s'\n", username);
164
165         status = dcerpc_samr_LookupNames(p, mem_ctx, &r1);
166         if (!NT_STATUS_IS_OK(status)) {
167                 printf("LookupNames failed - %s\n", nt_errstr(status));
168                 return False;
169         }
170
171         rid = r1.out.rids.ids[0];
172         
173         r2.in.domain_handle  = domain_handle;
174         r2.in.access_mask    = SEC_FLAG_MAXIMUM_ALLOWED;
175         r2.in.rid            = rid;
176         r2.out.user_handle   = &user_handle;
177
178         printf("opening user account\n");
179
180         status = dcerpc_samr_OpenUser(p, mem_ctx, &r2);
181         if (!NT_STATUS_IS_OK(status)) {
182                 printf("OpenUser failed - %s\n", nt_errstr(status));
183                 return False;
184         }
185
186         r3.in.user_handle  = &user_handle;
187         r3.out.user_handle = &user_handle;
188
189         printf("deleting user account\n");
190         
191         status = dcerpc_samr_DeleteUser(p, mem_ctx, &r3);
192         if (!NT_STATUS_IS_OK(status)) {
193                 printf("DeleteUser failed - %s\n", nt_errstr(status));
194                 return False;
195         }
196         
197         return True;
198 }
199
200
201 static BOOL test_createuser(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
202                             struct policy_handle *handle, const char* user)
203 {
204         NTSTATUS status;
205         struct policy_handle user_handle;
206         struct lsa_String username;
207         struct samr_CreateUser r1;
208         struct samr_Close r2;
209         uint32_t user_rid;
210
211         username.string = user;
212         
213         r1.in.domain_handle = handle;
214         r1.in.account_name = &username;
215         r1.in.access_mask = SEC_FLAG_MAXIMUM_ALLOWED;
216         r1.out.user_handle = &user_handle;
217         r1.out.rid = &user_rid;
218
219         printf("creating user '%s'\n", username.string);
220         
221         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
222         if (!NT_STATUS_IS_OK(status)) {
223                 printf("CreateUser failed - %s\n", nt_errstr(status));
224
225                 if (NT_STATUS_EQUAL(status, NT_STATUS_USER_EXISTS)) {
226                         printf("User (%s) already exists - attempting to delete and recreate account again\n", user);
227                         if (!test_cleanup(p, mem_ctx, handle, TEST_USERNAME)) {
228                                 return False;
229                         }
230
231                         printf("creating user account\n");
232                         
233                         status = dcerpc_samr_CreateUser(p, mem_ctx, &r1);
234                         if (!NT_STATUS_IS_OK(status)) {
235                                 printf("CreateUser failed - %s\n", nt_errstr(status));
236                                 return False;
237                         }
238                         return True;
239                 }               
240                 return False;
241         }
242
243         r2.in.handle = &user_handle;
244         r2.out.handle = &user_handle;
245         
246         printf("closing user '%s'\n", username.string);
247
248         status = dcerpc_samr_Close(p, mem_ctx, &r2);
249         if (!NT_STATUS_IS_OK(status)) {
250                 printf("Close failed - %s\n", nt_errstr(status));
251                 return False;
252         }
253
254         return True;
255 }
256
257
258 static BOOL test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
259                          struct policy_handle *handle, const char *username)
260 {
261         NTSTATUS status;
262         struct libnet_rpc_userdel user;
263         
264         user.in.domain_handle = *handle;
265         user.in.username = username;
266         
267         status = libnet_rpc_userdel(p, mem_ctx, &user);
268         if (!NT_STATUS_IS_OK(status)) {
269                 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
270                 return False;
271         }
272
273         return True;
274 }
275
276
277 static BOOL test_usermod(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
278                          struct policy_handle *handle, const char *username,
279                          struct usermod_change *change)
280 {
281         NTSTATUS status;
282         struct libnet_rpc_usermod user;
283         
284         user.in.domain_handle = *handle;
285         user.in.username = username;
286         user.in.change = *change;
287
288         printf("modifying user\n");
289
290         status = libnet_rpc_usermod(p, mem_ctx, &user);
291         if (!NT_STATUS_IS_OK(status)) {
292                 printf("Failed to call sync libnet_rpc_usermod - %s\n", nt_errstr(status));
293                 return False;
294         }
295
296         return True;
297 }
298
299
300 BOOL torture_useradd(void)
301 {
302         NTSTATUS status;
303         const char *binding;
304         struct dcerpc_pipe *p;
305         struct policy_handle h;
306         struct lsa_String domain_name;
307         const char *name = TEST_USERNAME;
308         TALLOC_CTX *mem_ctx;
309         BOOL ret = True;
310
311         mem_ctx = talloc_init("test_useradd");
312         binding = lp_parm_string(-1, "torture", "binding");
313
314         status = torture_rpc_connection(mem_ctx, 
315                                         &p,
316                                         DCERPC_SAMR_NAME,
317                                         DCERPC_SAMR_UUID,
318                                         DCERPC_SAMR_VERSION);
319         
320         if (!NT_STATUS_IS_OK(status)) {
321                 return False;
322         }
323
324         domain_name.string = lp_workgroup();
325         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
326                 ret = False;
327                 goto done;
328         }
329
330         if (!test_useradd(p, mem_ctx, &h, name)) {
331                 ret = False;
332                 goto done;
333         }
334
335         if (!test_cleanup(p, mem_ctx, &h, name)) {
336                 ret = False;
337                 goto done;
338         }
339
340         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
341                 ret = False;
342                 goto done;
343         }
344
345         if (!test_useradd_async(p, mem_ctx, &h, name)) {
346                 ret = False;
347                 goto done;
348         }
349
350         if (!test_cleanup(p, mem_ctx, &h, name)) {
351                 ret = False;
352                 goto done;
353         }
354
355 done:
356         talloc_free(mem_ctx);
357         return ret;
358 }
359
360
361 BOOL torture_userdel(void)
362 {
363         NTSTATUS status;
364         const char *binding;
365         struct dcerpc_pipe *p;
366         struct policy_handle h;
367         struct lsa_String domain_name;
368         const char *name = TEST_USERNAME;
369         TALLOC_CTX *mem_ctx;
370         BOOL ret = True;
371
372         mem_ctx = talloc_init("test_userdel");
373         binding = lp_parm_string(-1, "torture", "binding");
374
375         status = torture_rpc_connection(mem_ctx, 
376                                         &p,
377                                         DCERPC_SAMR_NAME,
378                                         DCERPC_SAMR_UUID,
379                                         DCERPC_SAMR_VERSION);
380         
381         if (!NT_STATUS_IS_OK(status)) {
382                 return False;
383         }
384
385         domain_name.string = lp_workgroup();
386         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
387                 ret = False;
388                 goto done;
389         }
390
391         if (!test_createuser(p, mem_ctx, &h, name)) {
392                 ret = False;
393                 goto done;
394         }
395         
396         if (!test_userdel(p, mem_ctx, &h, name)) {
397                 ret = False;
398                 goto done;
399         }
400         
401 done:
402         talloc_free(mem_ctx);
403         return ret;
404 }
405
406
407 BOOL torture_usermod(void)
408 {
409         NTSTATUS status;
410         const char *binding;
411         struct dcerpc_pipe *p;
412         struct policy_handle h;
413         struct lsa_String domain_name;
414         const char *name = TEST_USERNAME;
415         TALLOC_CTX *mem_ctx;
416         BOOL ret = True;
417         int i;
418
419         struct timeval expiry = { 12345, 67890 };
420         struct timeval allow  = { 67890, 12345 };
421         struct timeval force  = { 33333, 55444 };
422
423         struct usermod_change changes[] = {
424                 { USERMOD_FIELD_ACCOUNT_NAME,   "changed", NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0 },
425                 { USERMOD_FIELD_FULL_NAME,      NULL, "Testing full account name", NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0 },
426                 { USERMOD_FIELD_DESCRIPTION,    NULL, NULL, "Description of tested account", NULL, NULL, NULL, NULL, NULL, NULL, 0 },
427                 { USERMOD_FIELD_COMMENT,        NULL, NULL, NULL, "Comment for the tested account", NULL, NULL, NULL, NULL, NULL, 0 },
428                 { USERMOD_FIELD_LOGON_SCRIPT,   NULL, NULL, NULL, NULL, "test_logon.cmd", NULL, NULL, NULL, NULL, 0 },
429                 { USERMOD_FIELD_PROFILE_PATH,   NULL, NULL, NULL, NULL, NULL, "\\\\TESTSRV\\profiles\\test", NULL, NULL, NULL, 0 },
430                 { USERMOD_FIELD_ACCT_EXPIRY,    NULL, NULL, NULL, NULL, NULL, NULL, &expiry, NULL, NULL, 0 },
431                 { USERMOD_FIELD_ALLOW_PASS_CHG, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &allow, NULL, 0 },
432                 { USERMOD_FIELD_FORCE_PASS_CHG, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, &force, ACB_NORMAL }
433         };
434         
435         mem_ctx = talloc_init("test_userdel");
436         binding = lp_parm_string(-1, "torture", "binding");
437
438         status = torture_rpc_connection(mem_ctx, 
439                                         &p,
440                                         DCERPC_SAMR_NAME,
441                                         DCERPC_SAMR_UUID,
442                                         DCERPC_SAMR_VERSION);
443         
444         if (!NT_STATUS_IS_OK(status)) {
445                 return False;
446         }
447
448         domain_name.string = lp_workgroup();
449
450         if (!test_opendomain(p, mem_ctx, &h, &domain_name)) {
451                 ret = False;
452                 goto done;
453         }
454
455         if (!test_createuser(p, mem_ctx, &h, name)) {
456                 ret = False;
457                 goto done;
458         }
459
460         for (i = 0; i < (sizeof(changes)/sizeof(struct usermod_change)); i++) {
461                 if (!test_usermod(p, mem_ctx, &h, name, &changes[i])) {
462                         ret = False;
463                         goto done;
464                 }
465
466                 if (changes[i].fields & USERMOD_FIELD_ACCOUNT_NAME) {
467                         name = talloc_strdup(mem_ctx, changes[i].account_name);
468                 }
469         }
470
471         if (!test_cleanup(p, mem_ctx, &h, name)) {
472                 ret = False;
473                 goto done;
474         }
475
476 done:
477         talloc_free(mem_ctx);
478         return ret;
479 }