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