2 Unix SMB/CIFS implementation.
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/>.
21 #include "torture/smbtorture.h"
23 #include "torture/libnetapi/proto.h"
27 #define TORTURE_TEST_USER "torture_testuser"
28 #define TORTURE_TEST_USER2 "torture_testuser2"
30 #define NETAPI_STATUS(tctx, x,y,fn) \
31 torture_warning(tctx, "FAILURE: line %d: %s failed with status: %s (%d)\n", \
32 __LINE__, fn, libnetapi_get_error_string(x,y), y);
34 static NET_API_STATUS test_netuserenum(struct torture_context *tctx,
39 NET_API_STATUS status;
40 uint32_t entries_read = 0;
41 uint32_t total_entries = 0;
42 uint32_t resume_handle = 0;
43 const char *current_name = NULL;
45 uint8_t *buffer = NULL;
48 struct USER_INFO_0 *info0 = NULL;
49 struct USER_INFO_1 *info1 = NULL;
50 struct USER_INFO_2 *info2 = NULL;
51 struct USER_INFO_3 *info3 = NULL;
52 struct USER_INFO_4 *info4 = NULL;
53 struct USER_INFO_10 *info10 = NULL;
54 struct USER_INFO_11 *info11 = NULL;
55 struct USER_INFO_20 *info20 = NULL;
56 struct USER_INFO_23 *info23 = NULL;
58 torture_comment(tctx, "Testing NetUserEnum level %d\n", level);
61 status = NetUserEnum(hostname,
63 FILTER_NORMAL_ACCOUNT,
69 if (status == 0 || status == ERROR_MORE_DATA) {
72 info0 = (struct USER_INFO_0 *)buffer;
75 info1 = (struct USER_INFO_1 *)buffer;
78 info2 = (struct USER_INFO_2 *)buffer;
81 info3 = (struct USER_INFO_3 *)buffer;
84 info4 = (struct USER_INFO_4 *)buffer;
87 info10 = (struct USER_INFO_10 *)buffer;
90 info11 = (struct USER_INFO_11 *)buffer;
93 info20 = (struct USER_INFO_20 *)buffer;
96 info23 = (struct USER_INFO_23 *)buffer;
102 for (i=0; i<entries_read; i++) {
106 current_name = info0->usri0_name;
109 current_name = info1->usri1_name;
112 current_name = info2->usri2_name;
115 current_name = info3->usri3_name;
118 current_name = info4->usri4_name;
121 current_name = info10->usri10_name;
124 current_name = info11->usri11_name;
127 current_name = info20->usri20_name;
130 current_name = info23->usri23_name;
136 if (strcasecmp(current_name, username) == 0) {
172 NetApiBufferFree(buffer);
174 } while (status == ERROR_MORE_DATA);
181 torture_comment(tctx, "failed to get user\n");
188 NET_API_STATUS test_netuseradd(struct torture_context *tctx,
189 const char *hostname,
190 const char *username)
192 struct USER_INFO_1 u1;
193 uint32_t parm_err = 0;
197 torture_comment(tctx, "Testing NetUserAdd\n");
199 u1.usri1_name = username;
200 u1.usri1_password = "W297!832jD8J";
201 u1.usri1_password_age = 0;
203 u1.usri1_home_dir = NULL;
204 u1.usri1_comment = "User created using Samba NetApi Example code";
206 u1.usri1_script_path = NULL;
208 return NetUserAdd(hostname, 1, (uint8_t *)&u1, &parm_err);
211 static NET_API_STATUS test_netusermodals(struct torture_context *tctx,
212 struct libnetapi_ctx *ctx,
213 const char *hostname)
215 NET_API_STATUS status;
216 struct USER_MODALS_INFO_0 *u0 = NULL;
217 struct USER_MODALS_INFO_0 *_u0 = NULL;
218 uint8_t *buffer = NULL;
219 uint32_t parm_err = 0;
220 uint32_t levels[] = { 0, 1, 2, 3 };
223 for (i=0; i<ARRAY_SIZE(levels); i++) {
225 torture_comment(tctx, "Testing NetUserModalsGet level %d\n", levels[i]);
227 status = NetUserModalsGet(hostname, levels[i], &buffer);
229 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
234 status = NetUserModalsGet(hostname, 0, (uint8_t **)&u0);
236 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
240 torture_comment(tctx, "Testing NetUserModalsSet\n");
242 status = NetUserModalsSet(hostname, 0, (uint8_t *)u0, &parm_err);
244 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsSet");
248 status = NetUserModalsGet(hostname, 0, (uint8_t **)&_u0);
250 NETAPI_STATUS(tctx, ctx, status, "NetUserModalsGet");
254 if (memcmp(u0, _u0, sizeof(*u0)) != 0) {
255 torture_comment(tctx, "USER_MODALS_INFO_0 struct has changed!!!!\n");
262 static NET_API_STATUS test_netusergetgroups(struct torture_context *tctx,
263 const char *hostname,
265 const char *username,
266 const char *groupname)
268 NET_API_STATUS status;
269 uint32_t entries_read = 0;
270 uint32_t total_entries = 0;
271 const char *current_name;
273 uint8_t *buffer = NULL;
276 struct GROUP_USERS_INFO_0 *i0 = NULL;
277 struct GROUP_USERS_INFO_1 *i1 = NULL;
279 torture_comment(tctx, "Testing NetUserGetGroups level %d\n", level);
282 status = NetUserGetGroups(hostname,
289 if (status == 0 || status == ERROR_MORE_DATA) {
292 i0 = (struct GROUP_USERS_INFO_0 *)buffer;
295 i1 = (struct GROUP_USERS_INFO_1 *)buffer;
301 for (i=0; i<entries_read; i++) {
305 current_name = i0->grui0_name;
308 current_name = i1->grui1_name;
314 if (groupname && strcasecmp(current_name, groupname) == 0) {
329 NetApiBufferFree(buffer);
331 } while (status == ERROR_MORE_DATA);
337 if (groupname && !found_group) {
338 torture_comment(tctx, "failed to get membership\n");
345 bool torture_libnetapi_user(struct torture_context *tctx)
347 NET_API_STATUS status = 0;
348 uint8_t *buffer = NULL;
349 uint32_t levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
350 uint32_t enum_levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
351 uint32_t getgr_levels[] = { 0, 1 };
354 struct USER_INFO_0 u0;
355 struct USER_INFO_1007 u1007;
356 uint32_t parm_err = 0;
358 const char *hostname = torture_setting_string(tctx, "host", NULL);
359 struct libnetapi_ctx *ctx;
361 torture_assert(tctx, torture_libnetapi_init_context(tctx, &ctx),
362 "failed to initialize libnetapi");
364 torture_comment(tctx, "NetUser tests\n");
368 NetUserDel(hostname, TORTURE_TEST_USER);
369 NetUserDel(hostname, TORTURE_TEST_USER2);
373 status = test_netuseradd(tctx, hostname, TORTURE_TEST_USER);
375 NETAPI_STATUS(tctx, ctx, status, "NetUserAdd");
379 /* enum the new user */
381 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
383 status = test_netuserenum(tctx, hostname, enum_levels[i], TORTURE_TEST_USER);
385 NETAPI_STATUS(tctx, ctx, status, "NetUserEnum");
392 for (i=0; i<ARRAY_SIZE(levels); i++) {
394 torture_comment(tctx, "Testing NetUserGetInfo level %d\n", levels[i]);
396 status = NetUserGetInfo(hostname, TORTURE_TEST_USER, levels[i], &buffer);
397 if (status && status != 124) {
398 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
403 /* testing getgroups */
405 for (i=0; i<ARRAY_SIZE(getgr_levels); i++) {
407 status = test_netusergetgroups(tctx, hostname, getgr_levels[i], TORTURE_TEST_USER, NULL);
409 NETAPI_STATUS(tctx, ctx, status, "NetUserGetGroups");
414 /* modify description */
416 torture_comment(tctx, "Testing NetUserSetInfo level %d\n", 1007);
418 u1007.usri1007_comment = "NetApi modified user";
420 status = NetUserSetInfo(hostname, TORTURE_TEST_USER, 1007, (uint8_t *)&u1007, &parm_err);
422 NETAPI_STATUS(tctx, ctx, status, "NetUserSetInfo");
428 for (i=0; i<ARRAY_SIZE(levels); i++) {
429 status = NetUserGetInfo(hostname, TORTURE_TEST_USER, levels[i], &buffer);
430 if (status && status != 124) {
431 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
436 torture_comment(tctx, "Testing NetUserSetInfo level 0\n");
438 u0.usri0_name = TORTURE_TEST_USER2;
440 status = NetUserSetInfo(hostname, TORTURE_TEST_USER, 0, (uint8_t *)&u0, &parm_err);
442 NETAPI_STATUS(tctx, ctx, status, "NetUserSetInfo");
448 torture_comment(tctx, "Testing NetUserDel\n");
450 status = NetUserDel(hostname, TORTURE_TEST_USER2);
452 NETAPI_STATUS(tctx, ctx, status, "NetUserDel");
456 /* should not exist anymore */
458 status = NetUserGetInfo(hostname, TORTURE_TEST_USER2, 0, &buffer);
460 NETAPI_STATUS(tctx, ctx, status, "NetUserGetInfo");
465 status = test_netusermodals(tctx, ctx, hostname);
472 torture_comment(tctx, "NetUser tests succeeded\n");
475 NetUserDel(hostname, TORTURE_TEST_USER);
476 NetUserDel(hostname, TORTURE_TEST_USER2);
479 torture_comment(tctx, "NetUser testsuite failed with: %s\n",
480 libnetapi_get_error_string(ctx, status));