• 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/user/
1/*
2 *  Unix SMB/CIFS implementation.
3 *  NetUserGetLocalGroups 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 *username = 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 flags = 0;
41	int i;
42
43	struct LOCALGROUP_USERS_INFO_0 *info0 = NULL;
44
45	poptContext pc;
46	int opt;
47
48	struct poptOption long_options[] = {
49		POPT_AUTOHELP
50		POPT_COMMON_LIBNETAPI_EXAMPLES
51		POPT_TABLEEND
52	};
53
54	status = libnetapi_init(&ctx);
55	if (status != 0) {
56		return status;
57	}
58
59	pc = poptGetContext("user_getlocalgroups", argc, argv, long_options, 0);
60
61	poptSetOtherOptionHelp(pc, "hostname username");
62	while((opt = poptGetNextOpt(pc)) != -1) {
63	}
64
65	if (!poptPeekArg(pc)) {
66		poptPrintHelp(pc, stderr, 0);
67		goto out;
68	}
69	hostname = poptGetArg(pc);
70
71	if (!poptPeekArg(pc)) {
72		poptPrintHelp(pc, stderr, 0);
73		goto out;
74	}
75	username = poptGetArg(pc);
76
77	/* NetUserGetLocalGroups */
78
79	do {
80		status = NetUserGetLocalGroups(hostname,
81					       username,
82					       level,
83					       flags,
84					       &buffer,
85					       (uint32_t)-1,
86					       &entries_read,
87					       &total_entries);
88		if (status == 0 || status == ERROR_MORE_DATA) {
89
90			switch (level) {
91				case 0:
92					info0 = (struct LOCALGROUP_USERS_INFO_0 *)buffer;
93					break;
94				default:
95					break;
96			}
97
98			for (i=0; i<entries_read; i++) {
99				switch (level) {
100					case 0:
101						printf("#%d group: %s\n", i, info0->lgrui0_name);
102						info0++;
103						break;
104					default:
105						break;
106				}
107			}
108			NetApiBufferFree(buffer);
109		}
110	} while (status == ERROR_MORE_DATA);
111
112	if (status != 0) {
113		printf("NetUserGetLocalGroups failed with: %s\n",
114			libnetapi_get_error_string(ctx, status));
115	}
116
117 out:
118	libnetapi_free(ctx);
119	poptFreeContext(pc);
120
121	return status;
122}
123