netapi: add NetLocalGroupGetMembers example code.
[Samba/gebeck_regimport.git] / source3 / lib / netapi / examples / localgroup / localgroup_getmembers.c
blob0589870d02b30b401aa6bc2b05b5d61a50293fda
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetLocalGroupGetMembers query
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>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <netapi.h>
28 #include "common.h"
30 int main(int argc, const char **argv)
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 char *sid_str = NULL;
42 int i;
44 struct LOCALGROUP_MEMBERS_INFO_0 *info0 = NULL;
45 struct LOCALGROUP_MEMBERS_INFO_1 *info1 = NULL;
46 struct LOCALGROUP_MEMBERS_INFO_2 *info2 = NULL;
47 struct LOCALGROUP_MEMBERS_INFO_3 *info3 = NULL;
49 poptContext pc;
50 int opt;
52 struct poptOption long_options[] = {
53 POPT_AUTOHELP
54 POPT_COMMON_LIBNETAPI_EXAMPLES
55 POPT_TABLEEND
58 status = libnetapi_init(&ctx);
59 if (status != 0) {
60 return status;
63 pc = poptGetContext("localgroup_getmembers", argc, argv, long_options, 0);
65 poptSetOtherOptionHelp(pc, "hostname groupname level");
66 while((opt = poptGetNextOpt(pc)) != -1) {
69 if (!poptPeekArg(pc)) {
70 poptPrintHelp(pc, stderr, 0);
71 goto out;
73 hostname = poptGetArg(pc);
74 if (!poptPeekArg(pc)) {
75 poptPrintHelp(pc, stderr, 0);
76 goto out;
78 groupname = poptGetArg(pc);
80 if (poptPeekArg(pc)) {
81 level = atoi(poptGetArg(pc));
84 /* NetLocalGroupGetMembers */
86 do {
87 status = NetLocalGroupGetMembers(hostname,
88 groupname,
89 level,
90 &buffer,
91 (uint32_t)-1,
92 &entries_read,
93 &total_entries,
94 &resume_handle);
95 if (status == 0 || status == ERROR_MORE_DATA) {
96 printf("total entries: %d\n", total_entries);
97 switch (level) {
98 case 0:
99 info0 = (struct LOCALGROUP_MEMBERS_INFO_0 *)buffer;
100 break;
101 case 1:
102 info1 = (struct LOCALGROUP_MEMBERS_INFO_1 *)buffer;
103 break;
104 case 2:
105 info2 = (struct LOCALGROUP_MEMBERS_INFO_2 *)buffer;
106 break;
107 case 3:
108 info3 = (struct LOCALGROUP_MEMBERS_INFO_3 *)buffer;
109 break;
110 default:
111 break;
113 for (i=0; i<entries_read; i++) {
114 switch (level) {
115 case 0:
116 if (ConvertSidToStringSid(info0->lgrmi0_sid,
117 &sid_str)) {
118 printf("#%d member sid: %s\n", i, sid_str);
119 free(sid_str);
121 info0++;
122 break;
123 case 1:
124 if (ConvertSidToStringSid(info1->lgrmi1_sid,
125 &sid_str)) {
126 printf("#%d member sid: %s\n", i, sid_str);
127 free(sid_str);
129 printf("#%d sid type: %d\n", i, info1->lgrmi1_sidusage);
130 printf("#%d name: %s\n", i, info1->lgrmi1_name);
131 info1++;
132 break;
133 case 2:
134 if (ConvertSidToStringSid(info2->lgrmi2_sid,
135 &sid_str)) {
136 printf("#%d member sid: %s\n", i, sid_str);
137 free(sid_str);
139 printf("#%d sid type: %d\n", i, info2->lgrmi2_sidusage);
140 printf("#%d full name: %s\n", i, info2->lgrmi2_domainandname);
141 info2++;
142 break;
143 case 3:
144 printf("#%d full name: %s\n", i, info3->lgrmi3_domainandname);
145 info3++;
146 break;
147 default:
148 break;
151 NetApiBufferFree(buffer);
153 } while (status == ERROR_MORE_DATA);
155 if (status != 0) {
156 printf("NetLocalGroupGetMembers failed with: %s\n",
157 libnetapi_get_error_string(ctx, status));
160 out:
161 libnetapi_free(ctx);
162 poptFreeContext(pc);
164 return status;