lib/replace: define HAVE_WORKING_STRPTIME instead of REPLACE_STRPTIME
[Samba/gebeck_regimport.git] / source4 / torture / libnetapi / libnetapi_user.c
blob2afc1e59169f4bd6c916bbc73b51ea9d57e5b0a7
1 /*
2 Unix SMB/CIFS implementation.
3 SMB torture tester
4 Copyright (C) Guenther Deschner 2009
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "includes.h"
21 #include "torture/smbtorture.h"
22 #include <netapi.h>
23 #include "torture/libnetapi/proto.h"
25 #define TORTURE_TEST_USER "torture_testuser"
26 #define TORTURE_TEST_USER2 "torture_testuser2"
28 #define NETAPI_STATUS(tctx, x,y,fn) \
29 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d)\n", \
30 __LINE__, fn, libnetapi_get_error_string(x,y), y);
32 static NET_API_STATUS test_netuserenum(struct torture_context *tctx,
33 const char *hostname,
34 uint32_t level,
35 const char *username)
37 NET_API_STATUS status;
38 uint32_t entries_read = 0;
39 uint32_t total_entries = 0;
40 uint32_t resume_handle = 0;
41 const char *current_name = NULL;
42 int found_user = 0;
43 uint8_t *buffer = NULL;
44 int i;
46 struct USER_INFO_0 *info0 = NULL;
47 struct USER_INFO_1 *info1 = NULL;
48 struct USER_INFO_2 *info2 = NULL;
49 struct USER_INFO_3 *info3 = NULL;
50 struct USER_INFO_4 *info4 = NULL;
51 struct USER_INFO_10 *info10 = NULL;
52 struct USER_INFO_11 *info11 = NULL;
53 struct USER_INFO_20 *info20 = NULL;
54 struct USER_INFO_23 *info23 = NULL;
56 torture_comment(tctx, "Testing NetUserEnum level %d\n", level);
58 do {
59 status = NetUserEnum(hostname,
60 level,
61 FILTER_NORMAL_ACCOUNT,
62 &buffer,
63 (uint32_t)-1,
64 &entries_read,
65 &total_entries,
66 &resume_handle);
67 if (status == 0 || status == ERROR_MORE_DATA) {
68 switch (level) {
69 case 0:
70 info0 = (struct USER_INFO_0 *)buffer;
71 break;
72 case 1:
73 info1 = (struct USER_INFO_1 *)buffer;
74 break;
75 case 2:
76 info2 = (struct USER_INFO_2 *)buffer;
77 break;
78 case 3:
79 info3 = (struct USER_INFO_3 *)buffer;
80 break;
81 case 4:
82 info4 = (struct USER_INFO_4 *)buffer;
83 break;
84 case 10:
85 info10 = (struct USER_INFO_10 *)buffer;
86 break;
87 case 11:
88 info11 = (struct USER_INFO_11 *)buffer;
89 break;
90 case 20:
91 info20 = (struct USER_INFO_20 *)buffer;
92 break;
93 case 23:
94 info23 = (struct USER_INFO_23 *)buffer;
95 break;
96 default:
97 return -1;
100 for (i=0; i<entries_read; i++) {
102 switch (level) {
103 case 0:
104 current_name = info0->usri0_name;
105 break;
106 case 1:
107 current_name = info1->usri1_name;
108 break;
109 case 2:
110 current_name = info2->usri2_name;
111 break;
112 case 3:
113 current_name = info3->usri3_name;
114 break;
115 case 4:
116 current_name = info4->usri4_name;
117 break;
118 case 10:
119 current_name = info10->usri10_name;
120 break;
121 case 11:
122 current_name = info11->usri11_name;
123 break;
124 case 20:
125 current_name = info20->usri20_name;
126 break;
127 case 23:
128 current_name = info23->usri23_name;
129 break;
130 default:
131 return -1;
134 if (strcasecmp(current_name, username) == 0) {
135 found_user = 1;
138 switch (level) {
139 case 0:
140 info0++;
141 break;
142 case 1:
143 info1++;
144 break;
145 case 2:
146 info2++;
147 break;
148 case 3:
149 info3++;
150 break;
151 case 4:
152 info4++;
153 break;
154 case 10:
155 info10++;
156 break;
157 case 11:
158 info11++;
159 break;
160 case 20:
161 info20++;
162 break;
163 case 23:
164 info23++;
165 break;
166 default:
167 break;
170 NetApiBufferFree(buffer);
172 } while (status == ERROR_MORE_DATA);
174 if (status) {
175 return status;
178 if (!found_user) {
179 torture_comment(tctx, "failed to get user\n");
180 return -1;
183 return 0;
186 NET_API_STATUS test_netuseradd(struct torture_context *tctx,
187 const char *hostname,
188 const char *username)
190 struct USER_INFO_1 u1;
191 uint32_t parm_err = 0;
193 ZERO_STRUCT(u1);
195 torture_comment(tctx, "Testing NetUserAdd\n");
197 u1.usri1_name = username;
198 u1.usri1_password = "W297!832jD8J";
199 u1.usri1_password_age = 0;
200 u1.usri1_priv = 0;
201 u1.usri1_home_dir = NULL;
202 u1.usri1_comment = "User created using Samba NetApi Example code";
203 u1.usri1_flags = 0;
204 u1.usri1_script_path = NULL;
206 return NetUserAdd(hostname, 1, (uint8_t *)&u1, &parm_err);
209 static NET_API_STATUS test_netusermodals(struct torture_context *tctx,
210 struct libnetapi_ctx *ctx,
211 const char *hostname)
213 NET_API_STATUS status;
214 struct USER_MODALS_INFO_0 *u0 = NULL;
215 struct USER_MODALS_INFO_0 *_u0 = NULL;
216 uint8_t *buffer = NULL;
217 uint32_t parm_err = 0;
218 uint32_t levels[] = { 0, 1, 2, 3 };
219 int i = 0;
221 for (i=0; i<ARRAY_SIZE(levels); i++) {
223 torture_comment(tctx, "Testing NetUserModalsGet level %d\n", levels[i]);
225 status = NetUserModalsGet(hostname, levels[i], &buffer);
226 if (status) {
227 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
228 return status;
232 status = NetUserModalsGet(hostname, 0, (uint8_t **)&u0);
233 if (status) {
234 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
235 return status;
238 torture_comment(tctx, "Testing NetUserModalsSet\n");
240 status = NetUserModalsSet(hostname, 0, (uint8_t *)u0, &parm_err);
241 if (status) {
242 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsSet");
243 return status;
246 status = NetUserModalsGet(hostname, 0, (uint8_t **)&_u0);
247 if (status) {
248 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
249 return status;
252 if (memcmp(u0, _u0, sizeof(u0) != 0)) {
253 torture_comment(tctx, "USER_MODALS_INFO_0 struct has changed!!!!\n");
254 return -1;
257 return 0;
260 static NET_API_STATUS test_netusergetgroups(struct torture_context *tctx,
261 const char *hostname,
262 uint32_t level,
263 const char *username,
264 const char *groupname)
266 NET_API_STATUS status;
267 uint32_t entries_read = 0;
268 uint32_t total_entries = 0;
269 const char *current_name;
270 int found_group = 0;
271 uint8_t *buffer = NULL;
272 int i;
274 struct GROUP_USERS_INFO_0 *i0;
275 struct GROUP_USERS_INFO_1 *i1;
277 torture_comment(tctx, "Testing NetUserGetGroups level %d\n", level);
279 do {
280 status = NetUserGetGroups(hostname,
281 username,
282 level,
283 &buffer,
284 (uint32_t)-1,
285 &entries_read,
286 &total_entries);
287 if (status == 0 || status == ERROR_MORE_DATA) {
288 switch (level) {
289 case 0:
290 i0 = (struct GROUP_USERS_INFO_0 *)buffer;
291 break;
292 case 1:
293 i1 = (struct GROUP_USERS_INFO_1 *)buffer;
294 break;
295 default:
296 return -1;
299 for (i=0; i<entries_read; i++) {
301 switch (level) {
302 case 0:
303 current_name = i0->grui0_name;
304 break;
305 case 1:
306 current_name = i1->grui1_name;
307 break;
308 default:
309 return -1;
312 if (groupname && strcasecmp(current_name, groupname) == 0) {
313 found_group = 1;
316 switch (level) {
317 case 0:
318 i0++;
319 break;
320 case 1:
321 i1++;
322 break;
323 default:
324 break;
327 NetApiBufferFree(buffer);
329 } while (status == ERROR_MORE_DATA);
331 if (status) {
332 return status;
335 if (groupname && !found_group) {
336 torture_comment(tctx, "failed to get membership\n");
337 return -1;
340 return 0;
343 bool torture_libnetapi_user(struct torture_context *tctx)
345 NET_API_STATUS status = 0;
346 uint8_t *buffer = NULL;
347 uint32_t levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
348 uint32_t enum_levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
349 uint32_t getgr_levels[] = { 0, 1 };
350 int i;
352 struct USER_INFO_0 u0;
353 struct USER_INFO_1007 u1007;
354 uint32_t parm_err = 0;
356 const char *hostname = torture_setting_string(tctx, "host", NULL);
357 struct libnetapi_ctx *ctx;
359 torture_assert(tctx, torture_libnetapi_init_context(tctx, &ctx),
360 "failed to initialize libnetapi");
362 torture_comment(tctx, "NetUser tests\n");
364 /* cleanup */
366 NetUserDel(hostname, TORTURE_TEST_USER);
367 NetUserDel(hostname, TORTURE_TEST_USER2);
369 /* add a user */
371 status = test_netuseradd(tctx, hostname, TORTURE_TEST_USER);
372 if (status) {
373 NETAPI_STATUS(tctx, ctx, status, "NetUserAdd");
374 goto out;
377 /* enum the new user */
379 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
381 status = test_netuserenum(tctx, hostname, enum_levels[i], TORTURE_TEST_USER);
382 if (status) {
383 NETAPI_STATUS(tctx, ctx, status, "NetUserEnum");
384 goto out;
388 /* basic queries */
390 for (i=0; i<ARRAY_SIZE(levels); i++) {
392 torture_comment(tctx, "Testing NetUserGetInfo level %d\n", levels[i]);
394 status = NetUserGetInfo(hostname, TORTURE_TEST_USER, levels[i], &buffer);
395 if (status && status != 124) {
396 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
397 goto out;
401 /* testing getgroups */
403 for (i=0; i<ARRAY_SIZE(getgr_levels); i++) {
405 status = test_netusergetgroups(tctx, hostname, getgr_levels[i], TORTURE_TEST_USER, NULL);
406 if (status) {
407 NETAPI_STATUS(tctx, ctx, status, "NetUserGetGroups");
408 goto out;
412 /* modify description */
414 torture_comment(tctx, "Testing NetUserSetInfo level %d\n", 1007);
416 u1007.usri1007_comment = "NetApi modified user";
418 status = NetUserSetInfo(hostname, TORTURE_TEST_USER, 1007, (uint8_t *)&u1007, &parm_err);
419 if (status) {
420 NETAPI_STATUS(tctx, ctx, status, "NetUserSetInfo");
421 goto out;
424 /* query info */
426 for (i=0; i<ARRAY_SIZE(levels); i++) {
427 status = NetUserGetInfo(hostname, TORTURE_TEST_USER, levels[i], &buffer);
428 if (status && status != 124) {
429 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
430 goto out;
434 torture_comment(tctx, "Testing NetUserSetInfo level 0\n");
436 u0.usri0_name = TORTURE_TEST_USER2;
438 status = NetUserSetInfo(hostname, TORTURE_TEST_USER, 0, (uint8_t *)&u0, &parm_err);
439 if (status) {
440 NETAPI_STATUS(tctx, ctx, status, "NetUserSetInfo");
441 goto out;
444 /* delete */
446 torture_comment(tctx, "Testing NetUserDel\n");
448 status = NetUserDel(hostname, TORTURE_TEST_USER2);
449 if (status) {
450 NETAPI_STATUS(tctx, ctx, status, "NetUserDel");
451 goto out;
454 /* should not exist anymore */
456 status = NetUserGetInfo(hostname, TORTURE_TEST_USER2, 0, &buffer);
457 if (status == 0) {
458 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
459 status = -1;
460 goto out;
463 status = test_netusermodals(tctx, ctx, hostname);
464 if (status) {
465 goto out;
468 status = 0;
470 torture_comment(tctx, "NetUser tests succeeded\n");
471 out:
472 /* cleanup */
473 NetUserDel(hostname, TORTURE_TEST_USER);
474 NetUserDel(hostname, TORTURE_TEST_USER2);
476 if (status != 0) {
477 torture_comment(tctx, "NetUser testsuite failed with: %s\n",
478 libnetapi_get_error_string(ctx, status));
479 libnetapi_free(ctx);
480 return false;
483 libnetapi_free(ctx);
484 return true;