Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[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 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 "torture/libnet/usertest.h"
24 #include "libnet/libnet.h"
25 #include "librpc/gen_ndr/ndr_samr_c.h"
26 #include "param/param.h"
27
28 #include "torture/libnet/utils.h"
29
30
31 static bool test_useradd(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
32                          struct policy_handle *domain_handle,
33                          const char *name)
34 {
35         NTSTATUS status;
36         bool ret = true;
37         struct libnet_rpc_useradd user;
38         
39         user.in.domain_handle = *domain_handle;
40         user.in.username      = name;
41
42         printf("Testing libnet_rpc_useradd\n");
43
44         status = libnet_rpc_useradd(p, mem_ctx, &user);
45         if (!NT_STATUS_IS_OK(status)) {
46                 printf("Failed to call libnet_rpc_useradd - %s\n", nt_errstr(status));
47                 return false;
48         }
49         
50         return ret;
51 }
52
53
54 static bool test_useradd_async(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
55                                struct policy_handle *handle, const char* username)
56 {
57         NTSTATUS status;
58         struct composite_context *c;
59         struct libnet_rpc_useradd user;
60
61         user.in.domain_handle = *handle;
62         user.in.username      = username;
63         
64         printf("Testing async libnet_rpc_useradd\n");
65         
66         c = libnet_rpc_useradd_send(p, &user, msg_handler);
67         if (!c) {
68                 printf("Failed to call async libnet_rpc_useradd\n");
69                 return false;
70         }
71
72         status = libnet_rpc_useradd_recv(c, mem_ctx, &user);
73         if (!NT_STATUS_IS_OK(status)) {
74                 printf("Calling async libnet_rpc_useradd failed - %s\n", nt_errstr(status));
75                 return false;
76         }
77
78         return true;
79
80 }
81
82 static bool test_usermod(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
83                          struct policy_handle *handle, int num_changes,
84                          struct libnet_rpc_usermod *mod, char **username)
85 {
86         const char* logon_scripts[] = { "start_login.cmd", "login.bat", "start.cmd" };
87         const char* home_dirs[] = { "\\\\srv\\home", "\\\\homesrv\\home\\user", "\\\\pdcsrv\\domain" };
88         const char* home_drives[] = { "H:", "z:", "I:", "J:", "n:" };
89         const char *homedir, *homedrive, *logonscript;
90         const uint32_t flags[] = { (ACB_DISABLED | ACB_NORMAL),
91                                    (ACB_NORMAL | ACB_PWNOEXP),
92                                    (ACB_NORMAL) };
93
94         NTSTATUS status;
95         struct timeval now;
96         enum test_fields testfld;
97         int i;
98
99         ZERO_STRUCT(*mod);
100         srandom((unsigned)time(NULL));
101
102         mod->in.username = talloc_strdup(mem_ctx, *username);
103         mod->in.domain_handle = *handle;
104
105         printf("modifying user (%d simultaneous change(s))\n", num_changes);
106
107         printf("fields to change: [");
108
109         for (i = 0; i < num_changes && i < FIELDS_NUM - 1; i++) {
110                 const char *fldname;
111
112                 testfld = (random() % (FIELDS_NUM - 1)) + 1;
113
114                 gettimeofday(&now, NULL);
115
116                 switch (testfld) {
117                 case account_name:
118                         continue_if_field_set(mod->in.change.account_name);
119                         mod->in.change.account_name = talloc_asprintf(mem_ctx, TEST_CHG_ACCOUNTNAME,
120                                                                       (int)(random() % 100));
121                         mod->in.change.fields |= USERMOD_FIELD_ACCOUNT_NAME;
122                         fldname = "account_name";
123                         *username = talloc_strdup(mem_ctx, mod->in.change.account_name);
124                         break;
125
126                 case full_name:
127                         continue_if_field_set(mod->in.change.full_name);
128                         mod->in.change.full_name = talloc_asprintf(mem_ctx, TEST_CHG_FULLNAME,
129                                                                   (int)random(), (int)random());
130                         mod->in.change.fields |= USERMOD_FIELD_FULL_NAME;
131                         fldname = "full_name";
132                         break;
133
134                 case description:
135                         continue_if_field_set(mod->in.change.description);
136                         mod->in.change.description = talloc_asprintf(mem_ctx, TEST_CHG_DESCRIPTION,
137                                                                     random());
138                         mod->in.change.fields |= USERMOD_FIELD_DESCRIPTION;
139                         fldname = "description";
140                         break;
141                         
142                 case home_directory:
143                         continue_if_field_set(mod->in.change.home_directory);
144                         homedir = home_dirs[random() % (sizeof(home_dirs)/sizeof(char*))];
145                         mod->in.change.home_directory = talloc_strdup(mem_ctx, homedir);
146                         mod->in.change.fields |= USERMOD_FIELD_HOME_DIRECTORY;
147                         fldname = "home_directory";
148                         break;
149
150                 case home_drive:
151                         continue_if_field_set(mod->in.change.home_drive);
152                         homedrive = home_drives[random() % (sizeof(home_drives)/sizeof(char*))];
153                         mod->in.change.home_drive = talloc_strdup(mem_ctx, homedrive);
154                         mod->in.change.fields |= USERMOD_FIELD_HOME_DRIVE;
155                         fldname = "home_drive";
156                         break;
157
158                 case comment:
159                         continue_if_field_set(mod->in.change.comment);
160                         mod->in.change.comment = talloc_asprintf(mem_ctx, TEST_CHG_COMMENT,
161                                                                 random(), random());
162                         mod->in.change.fields |= USERMOD_FIELD_COMMENT;
163                         fldname = "comment";
164                         break;
165
166                 case logon_script:
167                         continue_if_field_set(mod->in.change.logon_script);
168                         logonscript = logon_scripts[random() % (sizeof(logon_scripts)/sizeof(char*))];
169                         mod->in.change.logon_script = talloc_strdup(mem_ctx, logonscript);
170                         mod->in.change.fields |= USERMOD_FIELD_LOGON_SCRIPT;
171                         fldname = "logon_script";
172                         break;
173
174                 case profile_path:
175                         continue_if_field_set(mod->in.change.profile_path);
176                         mod->in.change.profile_path = talloc_asprintf(mem_ctx, TEST_CHG_PROFILEPATH,
177                                                                      (long int)random(), (unsigned int)random());
178                         mod->in.change.fields |= USERMOD_FIELD_PROFILE_PATH;
179                         fldname = "profile_path";
180                         break;
181
182                 case acct_expiry:
183                         continue_if_field_set(mod->in.change.acct_expiry);
184                         now = timeval_add(&now, (random() % (31*24*60*60)), 0);
185                         mod->in.change.acct_expiry = (struct timeval *)talloc_memdup(mem_ctx, &now, sizeof(now));
186                         mod->in.change.fields |= USERMOD_FIELD_ACCT_EXPIRY;
187                         fldname = "acct_expiry";
188                         break;
189
190                 case acct_flags:
191                         continue_if_field_set(mod->in.change.acct_flags);
192                         mod->in.change.acct_flags = flags[random() % ARRAY_SIZE(flags)];
193                         mod->in.change.fields |= USERMOD_FIELD_ACCT_FLAGS;
194                         fldname = "acct_flags";
195                         break;
196
197                 default:
198                         fldname = talloc_asprintf(mem_ctx, "unknown_field (%d)", testfld);
199                         break;
200                 }
201
202                 printf(((i < num_changes - 1) ? "%s," : "%s"), fldname);
203         }
204         printf("]\n");
205
206         status = libnet_rpc_usermod(p, mem_ctx, mod);
207         if (!NT_STATUS_IS_OK(status)) {
208                 printf("Failed to call sync libnet_rpc_usermod - %s\n", nt_errstr(status));
209                 return false;
210         }
211
212         return true;
213 }
214
215
216 static bool test_userdel(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
217                          struct policy_handle *handle, const char *username)
218 {
219         NTSTATUS status;
220         struct libnet_rpc_userdel user;
221         
222         user.in.domain_handle = *handle;
223         user.in.username = username;
224         
225         status = libnet_rpc_userdel(p, mem_ctx, &user);
226         if (!NT_STATUS_IS_OK(status)) {
227                 printf("Failed to call sync libnet_rpc_userdel - %s\n", nt_errstr(status));
228                 return false;
229         }
230
231         return true;
232 }
233
234
235 #define CMP_LSA_STRING_FLD(fld, flags) \
236         if ((mod->in.change.fields & flags) && \
237             !strequal(i->fld.string, mod->in.change.fld)) { \
238                 printf("'%s' field does not match\n", #fld); \
239                 printf("received: '%s'\n", i->fld.string); \
240                 printf("expected: '%s'\n", mod->in.change.fld); \
241                 return false; \
242         }
243
244
245 #define CMP_TIME_FLD(fld, flags) \
246         if (mod->in.change.fields & flags) { \
247                 nttime_to_timeval(&t, i->fld); \
248                 if (timeval_compare(&t, mod->in.change.fld)) { \
249                         printf("'%s' field does not match\n", #fld); \
250                         printf("received: '%s (+%ld us)'\n", \
251                                timestring(mem_ctx, t.tv_sec), t.tv_usec); \
252                         printf("expected: '%s (+%ld us)'\n", \
253                                timestring(mem_ctx, mod->in.change.fld->tv_sec), \
254                                mod->in.change.fld->tv_usec); \
255                         return false; \
256                 } \
257         }
258
259 #define CMP_NUM_FLD(fld, flags) \
260         if ((mod->in.change.fields & flags) && \
261             (i->fld != mod->in.change.fld)) { \
262                 printf("'%s' field does not match\n", #fld); \
263                 printf("received: '%04x'\n", i->fld); \
264                 printf("expected: '%04x'\n", mod->in.change.fld); \
265                 return false; \
266         }
267
268
269 static bool test_compare(struct dcerpc_pipe *p, TALLOC_CTX *mem_ctx,
270                          struct policy_handle *handle, struct libnet_rpc_usermod *mod,
271                          const char *username)
272 {
273         NTSTATUS status;
274         struct libnet_rpc_userinfo info;
275         struct samr_UserInfo21 *i;
276         struct timeval t;
277
278         ZERO_STRUCT(info);
279
280         info.in.username = username;
281         info.in.domain_handle = *handle;
282         info.in.level = 21;             /* the most rich infolevel available */
283
284         status = libnet_rpc_userinfo(p, mem_ctx, &info);
285         if (!NT_STATUS_IS_OK(status)) {
286                 printf("Failed to call sync libnet_rpc_userinfo - %s\n", nt_errstr(status));
287                 return false;
288         }
289
290         i = &info.out.info.info21;
291
292         CMP_LSA_STRING_FLD(account_name, USERMOD_FIELD_ACCOUNT_NAME);
293         CMP_LSA_STRING_FLD(full_name, USERMOD_FIELD_FULL_NAME);
294         CMP_LSA_STRING_FLD(description, USERMOD_FIELD_DESCRIPTION);
295         CMP_LSA_STRING_FLD(comment, USERMOD_FIELD_COMMENT);
296         CMP_LSA_STRING_FLD(logon_script, USERMOD_FIELD_LOGON_SCRIPT);
297         CMP_LSA_STRING_FLD(profile_path, USERMOD_FIELD_PROFILE_PATH);
298         CMP_LSA_STRING_FLD(home_directory, USERMOD_FIELD_HOME_DIRECTORY);
299         CMP_LSA_STRING_FLD(home_drive, USERMOD_FIELD_HOME_DRIVE);
300         CMP_TIME_FLD(acct_expiry, USERMOD_FIELD_ACCT_EXPIRY);
301         CMP_NUM_FLD(acct_flags, USERMOD_FIELD_ACCT_FLAGS)
302
303         return true;
304 }
305
306
307 bool torture_useradd(struct torture_context *torture)
308 {
309         NTSTATUS status;
310         struct dcerpc_pipe *p;
311         struct policy_handle h;
312         struct lsa_String domain_name;
313         struct dom_sid2 sid;
314         const char *name = TEST_USERNAME;
315         TALLOC_CTX *mem_ctx;
316         bool ret = true;
317
318         mem_ctx = talloc_init("test_useradd");
319
320         status = torture_rpc_connection(torture, 
321                                         &p,
322                                         &ndr_table_samr);
323         
324         if (!NT_STATUS_IS_OK(status)) {
325                 return false;
326         }
327
328         domain_name.string = lp_workgroup(torture->lp_ctx);
329         if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
330                 ret = false;
331                 goto done;
332         }
333
334         if (!test_useradd(p, mem_ctx, &h, name)) {
335                 ret = false;
336                 goto done;
337         }
338
339         if (!test_user_cleanup(p, mem_ctx, &h, name)) {
340                 ret = false;
341                 goto done;
342         }
343
344         if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
345                 ret = false;
346                 goto done;
347         }
348
349         if (!test_useradd_async(p, mem_ctx, &h, name)) {
350                 ret = false;
351                 goto done;
352         }
353
354         if (!test_user_cleanup(p, mem_ctx, &h, name)) {
355                 ret = false;
356                 goto done;
357         }
358
359 done:
360         talloc_free(mem_ctx);
361         return ret;
362 }
363
364
365 bool torture_userdel(struct torture_context *torture)
366 {
367         NTSTATUS status;
368         struct dcerpc_pipe *p;
369         struct policy_handle h;
370         struct lsa_String domain_name;
371         struct dom_sid2 sid;
372         uint32_t rid;
373         const char *name = TEST_USERNAME;
374         TALLOC_CTX *mem_ctx;
375         bool ret = true;
376
377         mem_ctx = talloc_init("test_userdel");
378
379         status = torture_rpc_connection(torture, 
380                                         &p,
381                                         &ndr_table_samr);
382         
383         if (!NT_STATUS_IS_OK(status)) {
384                 return false;
385         }
386
387         domain_name.string = lp_workgroup(torture->lp_ctx);
388         if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
389                 ret = false;
390                 goto done;
391         }
392
393         if (!test_user_create(p, mem_ctx, &h, name, &rid)) {
394                 ret = false;
395                 goto done;
396         }
397         
398         if (!test_userdel(p, mem_ctx, &h, name)) {
399                 ret = false;
400                 goto done;
401         }
402         
403 done:
404         talloc_free(mem_ctx);
405         return ret;
406 }
407
408
409 bool torture_usermod(struct torture_context *torture)
410 {
411         NTSTATUS status;
412         struct dcerpc_pipe *p;
413         struct policy_handle h;
414         struct lsa_String domain_name;
415         struct dom_sid2 sid;
416         uint32_t rid;
417         int i;
418         char *name;
419         TALLOC_CTX *mem_ctx;
420         bool ret = true;
421
422         mem_ctx = talloc_init("test_userdel");
423
424         status = torture_rpc_connection(torture, 
425                                         &p,
426                                         &ndr_table_samr);
427         
428         if (!NT_STATUS_IS_OK(status)) {
429                 ret = false;
430                 goto done;
431         }
432
433         domain_name.string = lp_workgroup(torture->lp_ctx);
434         name = talloc_strdup(mem_ctx, TEST_USERNAME);
435
436         if (!test_opendomain(p, mem_ctx, &h, &domain_name, &sid)) {
437                 ret = false;
438                 goto done;
439         }
440
441         if (!test_user_create(p, mem_ctx, &h, name, &rid)) {
442                 ret = false;
443                 goto done;
444         }
445         
446         for (i = 1; i < FIELDS_NUM; i++) {
447                 struct libnet_rpc_usermod m;
448
449                 if (!test_usermod(p, mem_ctx, &h, i, &m, &name)) {
450                         ret = false;
451                         goto cleanup;
452                 }
453
454                 if (!test_compare(p, mem_ctx, &h, &m, name)) {
455                         ret = false;
456                         goto cleanup;
457                 }
458         }
459         
460 cleanup:        
461         if (!test_user_cleanup(p, mem_ctx, &h, name)) {
462                 ret = false;
463                 goto done;
464         }
465
466 done:
467         talloc_free(mem_ctx);
468         return ret;
469 }