• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/router/samba-3.5.8/source3/lib/netapi/examples/group/
1/*
2 *  Unix SMB/CIFS implementation.
3 *  NetGroupGetUsers query
4 *  Copyright (C) Guenther Deschner 2008
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <sys/types.h>
21#include <inttypes.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25
26#include <netapi.h>
27
28#include "common.h"
29
30int main(int argc, const char **argv)
31{
32	NET_API_STATUS status;
33	struct libnetapi_ctx *ctx = NULL;
34	const char *hostname = NULL;
35	const char *groupname = NULL;
36	uint32_t level = 0;
37	uint8_t *buffer = NULL;
38	uint32_t entries_read = 0;
39	uint32_t total_entries = 0;
40	uint32_t resume_handle = 0;
41	int i;
42
43	struct GROUP_USERS_INFO_0 *info0 = NULL;
44	struct GROUP_USERS_INFO_1 *info1 = NULL;
45
46	poptContext pc;
47	int opt;
48
49	struct poptOption long_options[] = {
50		POPT_AUTOHELP
51		POPT_COMMON_LIBNETAPI_EXAMPLES
52		POPT_TABLEEND
53	};
54
55	status = libnetapi_init(&ctx);
56	if (status != 0) {
57		return status;
58	}
59
60	pc = poptGetContext("group_getusers", argc, argv, long_options, 0);
61
62	poptSetOtherOptionHelp(pc, "hostname groupname level");
63	while((opt = poptGetNextOpt(pc)) != -1) {
64	}
65
66	if (!poptPeekArg(pc)) {
67		poptPrintHelp(pc, stderr, 0);
68		goto out;
69	}
70	hostname = poptGetArg(pc);
71
72	if (!poptPeekArg(pc)) {
73		poptPrintHelp(pc, stderr, 0);
74		goto out;
75	}
76	groupname = poptGetArg(pc);
77
78	if (poptPeekArg(pc)) {
79		level = atoi(poptGetArg(pc));
80	}
81
82	/* NetGroupGetUsers */
83
84	do {
85		status = NetGroupGetUsers(hostname,
86					  groupname,
87					  level,
88					  &buffer,
89					  (uint32_t)-1,
90					  &entries_read,
91					  &total_entries,
92					  &resume_handle);
93		if (status == 0 || status == ERROR_MORE_DATA) {
94			printf("total entries: %d\n", total_entries);
95			switch (level) {
96				case 0:
97					info0 = (struct GROUP_USERS_INFO_0 *)buffer;
98					break;
99				case 1:
100					info1 = (struct GROUP_USERS_INFO_1 *)buffer;
101					break;
102				default:
103					break;
104			}
105			for (i=0; i<entries_read; i++) {
106				switch (level) {
107					case 0:
108						printf("#%d member: %s\n", i, info0->grui0_name);
109						info0++;
110						break;
111					case 1:
112						printf("#%d member: %s\n", i, info1->grui1_name);
113						printf("#%d attributes: %d\n", i, info1->grui1_attributes);
114						info1++;
115						break;
116				}
117			}
118			NetApiBufferFree(buffer);
119		}
120	} while (status == ERROR_MORE_DATA);
121
122	if (status != 0) {
123		printf("NetGroupGetUsers failed with: %s\n",
124			libnetapi_get_error_string(ctx, status));
125	}
126
127 out:
128	libnetapi_free(ctx);
129	poptFreeContext(pc);
130
131	return status;
132}
133