/* [<][>][^][v][top][bottom][index][help] */
DEFINITIONS
This source file includes following definitions.
- main
1 /*
2 * Unix SMB/CIFS implementation.
3 * NetLocalGroupSetMembers 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
30 int main(int argc, const char **argv)
/* [<][>][^][v][top][bottom][index][help] */
31 {
32 NET_API_STATUS status;
33 struct libnetapi_ctx *ctx = NULL;
34 const char *hostname = NULL;
35 const char *groupname = NULL;
36 struct LOCALGROUP_MEMBERS_INFO_0 *g0;
37 struct LOCALGROUP_MEMBERS_INFO_3 *g3;
38 uint32_t total_entries = 0;
39 uint8_t *buffer = NULL;
40 uint32_t level = 3;
41 const char **names = NULL;
42 int i = 0;
43 size_t buf_size = 0;
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("localgroup_setmembers", argc, argv, long_options, 0);
60
61 poptSetOtherOptionHelp(pc, "hostname groupname member1 member2 ...");
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 groupname = poptGetArg(pc);
76
77 if (!poptPeekArg(pc)) {
78 poptPrintHelp(pc, stderr, 0);
79 goto out;
80 }
81
82 names = poptGetArgs(pc);
83 for (i=0; names[i] != NULL; i++) {
84 total_entries++;
85 }
86
87 switch (level) {
88 case 0:
89 buf_size = sizeof(struct LOCALGROUP_MEMBERS_INFO_0) * total_entries;
90
91 status = NetApiBufferAllocate(buf_size, (void **)&g0);
92 if (status) {
93 printf("NetApiBufferAllocate failed with: %s\n",
94 libnetapi_get_error_string(ctx, status));
95 goto out;
96 }
97
98 for (i=0; i<total_entries; i++) {
99 if (!ConvertStringSidToSid(names[i], &g0[i].lgrmi0_sid)) {
100 printf("could not convert sid\n");
101 goto out;
102 }
103 }
104
105 buffer = (uint8_t *)g0;
106 break;
107 case 3:
108 buf_size = sizeof(struct LOCALGROUP_MEMBERS_INFO_3) * total_entries;
109
110 status = NetApiBufferAllocate(buf_size, (void **)&g3);
111 if (status) {
112 printf("NetApiBufferAllocate failed with: %s\n",
113 libnetapi_get_error_string(ctx, status));
114 goto out;
115 }
116
117 for (i=0; i<total_entries; i++) {
118 g3[i].lgrmi3_domainandname = names[i];
119 }
120
121 buffer = (uint8_t *)g3;
122 break;
123 default:
124 break;
125 }
126
127 /* NetLocalGroupSetMembers */
128
129 status = NetLocalGroupSetMembers(hostname,
130 groupname,
131 level,
132 buffer,
133 total_entries);
134 if (status != 0) {
135 printf("NetLocalGroupSetMembers failed with: %s\n",
136 libnetapi_get_error_string(ctx, status));
137 }
138
139 NetApiBufferFree(buffer);
140
141 out:
142 libnetapi_free(ctx);
143 poptFreeContext(pc);
144
145 return status;
146 }