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"
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
,
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
;
43 uint8_t *buffer
= NULL
;
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
);
59 status
= NetUserEnum(hostname
,
61 FILTER_NORMAL_ACCOUNT
,
67 if (status
== 0 || status
== ERROR_MORE_DATA
) {
70 info0
= (struct USER_INFO_0
*)buffer
;
73 info1
= (struct USER_INFO_1
*)buffer
;
76 info2
= (struct USER_INFO_2
*)buffer
;
79 info3
= (struct USER_INFO_3
*)buffer
;
82 info4
= (struct USER_INFO_4
*)buffer
;
85 info10
= (struct USER_INFO_10
*)buffer
;
88 info11
= (struct USER_INFO_11
*)buffer
;
91 info20
= (struct USER_INFO_20
*)buffer
;
94 info23
= (struct USER_INFO_23
*)buffer
;
100 for (i
=0; i
<entries_read
; i
++) {
104 current_name
= info0
->usri0_name
;
107 current_name
= info1
->usri1_name
;
110 current_name
= info2
->usri2_name
;
113 current_name
= info3
->usri3_name
;
116 current_name
= info4
->usri4_name
;
119 current_name
= info10
->usri10_name
;
122 current_name
= info11
->usri11_name
;
125 current_name
= info20
->usri20_name
;
128 current_name
= info23
->usri23_name
;
134 if (strcasecmp(current_name
, username
) == 0) {
170 NetApiBufferFree(buffer
);
172 } while (status
== ERROR_MORE_DATA
);
179 torture_comment(tctx
, "failed to get user\n");
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;
195 torture_comment(tctx
, "Testing NetUserAdd\n");
197 u1
.usri1_name
= username
;
198 u1
.usri1_password
= "W297!832jD8J";
199 u1
.usri1_password_age
= 0;
201 u1
.usri1_home_dir
= NULL
;
202 u1
.usri1_comment
= "User created using Samba NetApi Example code";
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 };
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
);
227 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserModalsGet");
232 status
= NetUserModalsGet(hostname
, 0, (uint8_t **)&u0
);
234 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserModalsGet");
238 torture_comment(tctx
, "Testing NetUserModalsSet\n");
240 status
= NetUserModalsSet(hostname
, 0, (uint8_t *)u0
, &parm_err
);
242 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserModalsSet");
246 status
= NetUserModalsGet(hostname
, 0, (uint8_t **)&_u0
);
248 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserModalsGet");
252 if (memcmp(u0
, _u0
, sizeof(u0
) != 0)) {
253 torture_comment(tctx
, "USER_MODALS_INFO_0 struct has changed!!!!\n");
260 static NET_API_STATUS
test_netusergetgroups(struct torture_context
*tctx
,
261 const char *hostname
,
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
;
271 uint8_t *buffer
= NULL
;
274 struct GROUP_USERS_INFO_0
*i0
;
275 struct GROUP_USERS_INFO_1
*i1
;
277 torture_comment(tctx
, "Testing NetUserGetGroups level %d\n", level
);
280 status
= NetUserGetGroups(hostname
,
287 if (status
== 0 || status
== ERROR_MORE_DATA
) {
290 i0
= (struct GROUP_USERS_INFO_0
*)buffer
;
293 i1
= (struct GROUP_USERS_INFO_1
*)buffer
;
299 for (i
=0; i
<entries_read
; i
++) {
303 current_name
= i0
->grui0_name
;
306 current_name
= i1
->grui1_name
;
312 if (groupname
&& strcasecmp(current_name
, groupname
) == 0) {
327 NetApiBufferFree(buffer
);
329 } while (status
== ERROR_MORE_DATA
);
335 if (groupname
&& !found_group
) {
336 torture_comment(tctx
, "failed to get membership\n");
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 };
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");
366 NetUserDel(hostname
, TORTURE_TEST_USER
);
367 NetUserDel(hostname
, TORTURE_TEST_USER2
);
371 status
= test_netuseradd(tctx
, hostname
, TORTURE_TEST_USER
);
373 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserAdd");
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
);
383 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserEnum");
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");
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
);
407 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserGetGroups");
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
);
420 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserSetInfo");
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");
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
);
440 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserSetInfo");
446 torture_comment(tctx
, "Testing NetUserDel\n");
448 status
= NetUserDel(hostname
, TORTURE_TEST_USER2
);
450 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserDel");
454 /* should not exist anymore */
456 status
= NetUserGetInfo(hostname
, TORTURE_TEST_USER2
, 0, &buffer
);
458 NETAPI_STATUS(tctx
, ctx
, status
, "NetUserGetInfo");
463 status
= test_netusermodals(tctx
, ctx
, hostname
);
470 torture_comment(tctx
, "NetUser tests succeeded\n");
473 NetUserDel(hostname
, TORTURE_TEST_USER
);
474 NetUserDel(hostname
, TORTURE_TEST_USER2
);
477 torture_comment(tctx
, "NetUser testsuite failed with: %s\n",
478 libnetapi_get_error_string(ctx
, status
));