2 * Unix SMB/CIFS implementation.
4 * Copyright (C) Guenther Deschner 2008
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 <sys/types.h>
30 static NET_API_STATUS test_netuserenum(const char *hostname,
34 NET_API_STATUS status;
35 uint32_t entries_read = 0;
36 uint32_t total_entries = 0;
37 uint32_t resume_handle = 0;
38 const char *current_name = NULL;
40 uint8_t *buffer = NULL;
43 struct USER_INFO_0 *info0 = NULL;
44 struct USER_INFO_1 *info1 = NULL;
45 struct USER_INFO_2 *info2 = NULL;
46 struct USER_INFO_3 *info3 = NULL;
47 struct USER_INFO_4 *info4 = NULL;
48 struct USER_INFO_10 *info10 = NULL;
49 struct USER_INFO_11 *info11 = NULL;
50 struct USER_INFO_20 *info20 = NULL;
51 struct USER_INFO_23 *info23 = NULL;
53 printf("testing NetUserEnum level %d\n", level);
56 status = NetUserEnum(hostname,
58 FILTER_NORMAL_ACCOUNT,
64 if (status == 0 || status == ERROR_MORE_DATA) {
67 info0 = (struct USER_INFO_0 *)buffer;
70 info1 = (struct USER_INFO_1 *)buffer;
73 info2 = (struct USER_INFO_2 *)buffer;
76 info3 = (struct USER_INFO_3 *)buffer;
79 info4 = (struct USER_INFO_4 *)buffer;
82 info10 = (struct USER_INFO_10 *)buffer;
85 info11 = (struct USER_INFO_11 *)buffer;
88 info20 = (struct USER_INFO_20 *)buffer;
91 info23 = (struct USER_INFO_23 *)buffer;
97 for (i=0; i<entries_read; i++) {
101 current_name = info0->usri0_name;
104 current_name = info1->usri1_name;
107 current_name = info2->usri2_name;
110 current_name = info3->usri3_name;
113 current_name = info4->usri4_name;
116 current_name = info10->usri10_name;
119 current_name = info11->usri11_name;
122 current_name = info20->usri20_name;
125 current_name = info23->usri23_name;
131 if (strcasecmp(current_name, username) == 0) {
167 NetApiBufferFree(buffer);
169 } while (status == ERROR_MORE_DATA);
176 printf("failed to get user\n");
183 NET_API_STATUS test_netuseradd(const char *hostname,
184 const char *username)
186 struct USER_INFO_1 u1;
187 uint32_t parm_err = 0;
191 printf("testing NetUserAdd\n");
193 u1.usri1_name = username;
194 u1.usri1_password = "W297!832jD8J";
195 u1.usri1_password_age = 0;
197 u1.usri1_home_dir = NULL;
198 u1.usri1_comment = "User created using Samba NetApi Example code";
200 u1.usri1_script_path = NULL;
202 return NetUserAdd(hostname, 1, (uint8_t *)&u1, &parm_err);
205 static NET_API_STATUS test_netusermodals(struct libnetapi_ctx *ctx,
206 const char *hostname)
208 NET_API_STATUS status;
209 struct USER_MODALS_INFO_0 *u0 = NULL;
210 struct USER_MODALS_INFO_0 *_u0 = NULL;
211 uint8_t *buffer = NULL;
212 uint32_t parm_err = 0;
213 uint32_t levels[] = { 0, 1, 2, 3 };
216 for (i=0; i<ARRAY_SIZE(levels); i++) {
218 printf("testing NetUserModalsGet level %d\n", levels[i]);
220 status = NetUserModalsGet(hostname, levels[i], &buffer);
222 NETAPI_STATUS(ctx, status, "NetUserModalsGet");
227 status = NetUserModalsGet(hostname, 0, (uint8_t **)&u0);
229 NETAPI_STATUS(ctx, status, "NetUserModalsGet");
233 printf("testing NetUserModalsSet\n");
235 status = NetUserModalsSet(hostname, 0, (uint8_t *)u0, &parm_err);
237 NETAPI_STATUS(ctx, status, "NetUserModalsSet");
241 status = NetUserModalsGet(hostname, 0, (uint8_t **)&_u0);
243 NETAPI_STATUS(ctx, status, "NetUserModalsGet");
247 if (memcmp(u0, _u0, sizeof(u0) != 0)) {
248 printf("USER_MODALS_INFO_0 struct has changed!!!!\n");
255 static NET_API_STATUS test_netusergetgroups(const char *hostname,
257 const char *username,
258 const char *groupname)
260 NET_API_STATUS status;
261 uint32_t entries_read = 0;
262 uint32_t total_entries = 0;
263 const char *current_name;
265 uint8_t *buffer = NULL;
268 struct GROUP_USERS_INFO_0 *i0;
269 struct GROUP_USERS_INFO_1 *i1;
271 printf("testing NetUserGetGroups level %d\n", level);
274 status = NetUserGetGroups(hostname,
281 if (status == 0 || status == ERROR_MORE_DATA) {
284 i0 = (struct GROUP_USERS_INFO_0 *)buffer;
287 i1 = (struct GROUP_USERS_INFO_1 *)buffer;
293 for (i=0; i<entries_read; i++) {
297 current_name = i0->grui0_name;
300 current_name = i1->grui1_name;
306 if (groupname && strcasecmp(current_name, groupname) == 0) {
321 NetApiBufferFree(buffer);
323 } while (status == ERROR_MORE_DATA);
329 if (groupname && !found_group) {
330 printf("failed to get membership\n");
337 NET_API_STATUS netapitest_user(struct libnetapi_ctx *ctx,
338 const char *hostname)
340 NET_API_STATUS status = 0;
341 const char *username, *username2;
342 uint8_t *buffer = NULL;
343 uint32_t levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
344 uint32_t enum_levels[] = { 0, 1, 2, 3, 4, 10, 11, 20, 23 };
345 uint32_t getgr_levels[] = { 0, 1 };
348 struct USER_INFO_1007 u1007;
349 uint32_t parm_err = 0;
351 printf("NetUser tests\n");
353 username = "torture_test_user";
354 username2 = "torture_test_user2";
357 NetUserDel(hostname, username);
358 NetUserDel(hostname, username2);
362 status = test_netuseradd(hostname, username);
364 NETAPI_STATUS(ctx, status, "NetUserAdd");
368 /* enum the new user */
370 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
372 status = test_netuserenum(hostname, enum_levels[i], username);
374 NETAPI_STATUS(ctx, status, "NetUserEnum");
381 for (i=0; i<ARRAY_SIZE(levels); i++) {
383 printf("testing NetUserGetInfo level %d\n", levels[i]);
385 status = NetUserGetInfo(hostname, username, levels[i], &buffer);
386 if (status && status != 124) {
387 NETAPI_STATUS(ctx, status, "NetUserGetInfo");
392 /* testing getgroups */
394 for (i=0; i<ARRAY_SIZE(getgr_levels); i++) {
396 status = test_netusergetgroups(hostname, getgr_levels[i], username, NULL);
398 NETAPI_STATUS(ctx, status, "NetUserGetGroups");
403 /* modify description */
405 printf("testing NetUserSetInfo level %d\n", 1007);
407 u1007.usri1007_comment = "NetApi modified user";
409 status = NetUserSetInfo(hostname, username, 1007, (uint8_t *)&u1007, &parm_err);
411 NETAPI_STATUS(ctx, status, "NetUserSetInfo");
417 for (i=0; i<ARRAY_SIZE(levels); i++) {
418 status = NetUserGetInfo(hostname, username, levels[i], &buffer);
419 if (status && status != 124) {
420 NETAPI_STATUS(ctx, status, "NetUserGetInfo");
427 printf("testing NetUserDel\n");
429 status = NetUserDel(hostname, username);
431 NETAPI_STATUS(ctx, status, "NetUserDel");
435 /* should not exist anymore */
437 status = NetUserGetInfo(hostname, username, 0, &buffer);
439 NETAPI_STATUS(ctx, status, "NetUserGetInfo");
443 status = test_netusermodals(ctx, hostname);
450 printf("NetUser tests succeeded\n");
453 printf("NetUser testsuite failed with: %s\n",
454 libnetapi_get_error_string(ctx, status));