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_netgroupenum(const char *hostname,
32 const char *groupname)
34 NET_API_STATUS status;
35 uint32_t entries_read = 0;
36 uint32_t total_entries = 0;
37 uint32_t resume_handle = 0;
39 const char *current_name = NULL;
40 uint8_t *buffer = NULL;
43 struct GROUP_INFO_0 *info0 = NULL;
44 struct GROUP_INFO_1 *info1 = NULL;
45 struct GROUP_INFO_2 *info2 = NULL;
46 struct GROUP_INFO_3 *info3 = NULL;
48 printf("testing NetGroupEnum level %d\n", level);
51 status = NetGroupEnum(hostname,
58 if (status == 0 || status == ERROR_MORE_DATA) {
61 info0 = (struct GROUP_INFO_0 *)buffer;
64 info1 = (struct GROUP_INFO_1 *)buffer;
67 info2 = (struct GROUP_INFO_2 *)buffer;
70 info3 = (struct GROUP_INFO_3 *)buffer;
76 for (i=0; i<entries_read; i++) {
80 current_name = info0->grpi0_name;
83 current_name = info1->grpi1_name;
86 current_name = info2->grpi2_name;
89 current_name = info3->grpi3_name;
95 if (strcasecmp(current_name, groupname) == 0) {
114 NetApiBufferFree(buffer);
116 } while (status == ERROR_MORE_DATA);
123 printf("failed to get group\n");
130 static NET_API_STATUS test_netgroupgetusers(const char *hostname,
132 const char *groupname,
133 const char *username)
135 NET_API_STATUS status;
136 uint32_t entries_read = 0;
137 uint32_t total_entries = 0;
138 uint32_t resume_handle = 0;
140 const char *current_name = NULL;
141 uint8_t *buffer = NULL;
144 struct GROUP_USERS_INFO_0 *info0 = NULL;
145 struct GROUP_USERS_INFO_1 *info1 = NULL;
147 printf("testing NetGroupGetUsers level %d\n", level);
150 status = NetGroupGetUsers(hostname,
158 if (status == 0 || status == ERROR_MORE_DATA) {
162 info0 = (struct GROUP_USERS_INFO_0 *)buffer;
165 info1 = (struct GROUP_USERS_INFO_1 *)buffer;
170 for (i=0; i<entries_read; i++) {
173 current_name = info0->grui0_name;
176 current_name = info1->grui1_name;
182 if (username && strcasecmp(current_name, username) == 0) {
195 NetApiBufferFree(buffer);
197 } while (status == ERROR_MORE_DATA);
203 if (username && !found_user) {
204 printf("failed to get user\n");
211 static NET_API_STATUS test_netgroupsetusers(const char *hostname,
212 const char *groupname,
217 NET_API_STATUS status;
218 uint8_t *buffer = NULL;
222 struct GROUP_USERS_INFO_0 *g0 = NULL;
223 struct GROUP_USERS_INFO_1 *g1 = NULL;
225 printf("testing NetGroupSetUsers level %d\n", level);
229 buf_size = sizeof(struct GROUP_USERS_INFO_0) * num_entries;
231 status = NetApiBufferAllocate(buf_size, (void **)&g0);
236 for (i=0; i<num_entries; i++) {
237 g0[i].grui0_name = names[i];
240 buffer = (uint8_t *)g0;
243 buf_size = sizeof(struct GROUP_USERS_INFO_1) * num_entries;
245 status = NetApiBufferAllocate(buf_size, (void **)&g1);
250 for (i=0; i<num_entries; i++) {
251 g1[i].grui1_name = names[i];
254 buffer = (uint8_t *)g1;
260 /* NetGroupSetUsers */
262 status = NetGroupSetUsers(hostname,
272 NetApiBufferFree(buffer);
276 NET_API_STATUS netapitest_group(struct libnetapi_ctx *ctx,
277 const char *hostname)
279 NET_API_STATUS status = 0;
280 const char *username, *groupname, *groupname2;
281 uint8_t *buffer = NULL;
282 struct GROUP_INFO_0 g0;
283 uint32_t parm_err = 0;
284 uint32_t levels[] = { 0, 1, 2, 3};
285 uint32_t enum_levels[] = { 0, 1, 2, 3};
286 uint32_t getmem_levels[] = { 0, 1};
289 printf("NetGroup tests\n");
291 username = "torture_test_user";
292 groupname = "torture_test_group";
293 groupname2 = "torture_test_group2";
296 NetGroupDel(hostname, groupname);
297 NetGroupDel(hostname, groupname2);
298 NetUserDel(hostname, username);
302 g0.grpi0_name = groupname;
304 printf("testing NetGroupAdd\n");
306 status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
308 NETAPI_STATUS(ctx, status, "NetGroupAdd");
312 /* 2nd add must fail */
314 status = NetGroupAdd(hostname, 0, (uint8_t *)&g0, &parm_err);
316 NETAPI_STATUS(ctx, status, "NetGroupAdd");
322 for (i=0; i<ARRAY_SIZE(enum_levels); i++) {
324 status = test_netgroupenum(hostname, enum_levels[i], groupname);
326 NETAPI_STATUS(ctx, status, "NetGroupEnum");
333 for (i=0; i<ARRAY_SIZE(levels); i++) {
335 printf("testing NetGroupGetInfo level %d\n", levels[i]);
337 status = NetGroupGetInfo(hostname, groupname, levels[i], &buffer);
338 if (status && status != 124) {
339 NETAPI_STATUS(ctx, status, "NetGroupGetInfo");
346 g0.grpi0_name = groupname2;
348 printf("testing NetGroupSetInfo level 0\n");
350 status = NetGroupSetInfo(hostname, groupname, 0, (uint8_t *)&g0, &parm_err);
354 case 50: /* not supported */
355 case 124: /* not implemented */
356 groupname2 = groupname;
359 NETAPI_STATUS(ctx, status, "NetGroupSetInfo");
363 /* should not exist anymore */
365 status = NetGroupDel(hostname, groupname);
367 NETAPI_STATUS(ctx, status, "NetGroupDel");
374 for (i=0; i<ARRAY_SIZE(levels); i++) {
376 status = NetGroupGetInfo(hostname, groupname2, levels[i], &buffer);
377 if (status && status != 124) {
378 NETAPI_STATUS(ctx, status, "NetGroupGetInfo");
383 /* add user to group */
385 status = test_netuseradd(hostname, username);
387 NETAPI_STATUS(ctx, status, "NetUserAdd");
391 /* should not be member */
393 for (i=0; i<ARRAY_SIZE(getmem_levels); i++) {
395 status = test_netgroupgetusers(hostname, getmem_levels[i], groupname2, NULL);
397 NETAPI_STATUS(ctx, status, "NetGroupGetUsers");
402 printf("testing NetGroupAddUser\n");
404 status = NetGroupAddUser(hostname, groupname2, username);
406 NETAPI_STATUS(ctx, status, "NetGroupAddUser");
410 /* should be member */
412 for (i=0; i<ARRAY_SIZE(getmem_levels); i++) {
414 status = test_netgroupgetusers(hostname, getmem_levels[i], groupname2, username);
416 NETAPI_STATUS(ctx, status, "NetGroupGetUsers");
421 printf("testing NetGroupDelUser\n");
423 status = NetGroupDelUser(hostname, groupname2, username);
425 NETAPI_STATUS(ctx, status, "NetGroupDelUser");
429 /* should not be member */
431 status = test_netgroupgetusers(hostname, 0, groupname2, NULL);
433 NETAPI_STATUS(ctx, status, "NetGroupGetUsers");
437 /* set it again via exlicit member set */
439 status = test_netgroupsetusers(hostname, groupname2, 0, 1, &username);
441 NETAPI_STATUS(ctx, status, "NetGroupSetUsers");
445 /* should be member */
447 status = test_netgroupgetusers(hostname, 0, groupname2, username);
449 NETAPI_STATUS(ctx, status, "NetGroupGetUsers");
453 /* wipe out member list */
455 status = test_netgroupsetusers(hostname, groupname2, 0, 0, NULL);
457 NETAPI_STATUS(ctx, status, "NetGroupSetUsers");
461 /* should not be member */
463 status = test_netgroupgetusers(hostname, 0, groupname2, NULL);
465 NETAPI_STATUS(ctx, status, "NetGroupGetUsers");
469 status = NetUserDel(hostname, username);
471 NETAPI_STATUS(ctx, status, "NetUserDel");
477 printf("testing NetGroupDel\n");
479 status = NetGroupDel(hostname, groupname2);
481 NETAPI_STATUS(ctx, status, "NetGroupDel");
485 /* should not exist anymore */
487 status = NetGroupGetInfo(hostname, groupname2, 0, &buffer);
489 NETAPI_STATUS_MSG(ctx, status, "NetGroupGetInfo", "expected failure and error code");
495 printf("NetGroup tests succeeded\n");
498 printf("NetGroup testsuite failed with: %s\n",
499 libnetapi_get_error_string(ctx, status));