43400d1c3b0f8e8634db26cf0012c207211d9e87
[reactos.git] / base / applications / network / net / cmdLocalGroup.c
1 /*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE:
5 * PURPOSE:
6 *
7 * PROGRAMMERS: Eric Kohl
8 */
9
10 #include "net.h"
11
12
13 static
14 int
15 CompareInfo(const void *a,
16 const void *b)
17 {
18 return _wcsicmp(((PLOCALGROUP_INFO_0)a)->lgrpi0_name,
19 ((PLOCALGROUP_INFO_0)b)->lgrpi0_name);
20 }
21
22
23 static
24 NET_API_STATUS
25 EnumerateLocalGroups(VOID)
26 {
27 PLOCALGROUP_INFO_0 pBuffer = NULL;
28 PSERVER_INFO_100 pServer = NULL;
29 DWORD dwRead = 0, dwTotal = 0;
30 DWORD i;
31 DWORD_PTR ResumeHandle = 0;
32 NET_API_STATUS Status;
33
34
35 Status = NetServerGetInfo(NULL,
36 100,
37 (LPBYTE*)&pServer);
38 if (Status != NERR_Success)
39 return Status;
40
41 printf("\nAliases for \\\\%S\n\n", pServer->sv100_name);
42 printf("------------------------------------------\n");
43
44 NetApiBufferFree(pServer);
45
46 Status = NetLocalGroupEnum(NULL,
47 0,
48 (LPBYTE*)&pBuffer,
49 MAX_PREFERRED_LENGTH,
50 &dwRead,
51 &dwTotal,
52 &ResumeHandle);
53 if (Status != NERR_Success)
54 return Status;
55
56 qsort(pBuffer,
57 dwRead,
58 sizeof(PLOCALGROUP_INFO_0),
59 CompareInfo);
60
61 // printf("dwRead: %lu dwTotal: %lu\n", dwRead, dwTotal);
62 for (i = 0; i < dwRead; i++)
63 {
64 // printf("%p\n", pBuffer[i].lgrpi0_name);
65 if (pBuffer[i].lgrpi0_name)
66 printf("*%S\n", pBuffer[i].lgrpi0_name);
67 }
68
69 NetApiBufferFree(pBuffer);
70
71 return NERR_Success;
72 }
73
74
75 INT
76 cmdLocalGroup(
77 INT argc,
78 WCHAR **argv)
79 {
80 INT i, j;
81 INT result = 0;
82 ULONG dwMemberCount = 0;
83 BOOL bAdd = FALSE;
84 BOOL bDelete = FALSE;
85 #if 0
86 BOOL bDomain = FALSE;
87 #endif
88 LPWSTR lpGroupName = NULL;
89 LPWSTR lpComment = NULL;
90 LPLOCALGROUP_MEMBERS_INFO_3 lpMembers = NULL;
91 LOCALGROUP_INFO_0 Info0;
92 LOCALGROUP_INFO_1 Info1;
93 LOCALGROUP_INFO_1002 Info1002;
94 NET_API_STATUS Status;
95
96 if (argc == 2)
97 {
98 Status = EnumerateLocalGroups();
99 printf("Status: %lu\n", Status);
100 return 0;
101 }
102
103 i = 2;
104 if (argv[i][0] != L'/')
105 {
106 lpGroupName = argv[i];
107 i++;
108 }
109
110 for (j = i; j < argc; j++)
111 {
112 if (argv[j][0] == L'/')
113 break;
114
115 dwMemberCount++;
116 }
117
118 printf("Member count: %lu\n", dwMemberCount);
119
120 if (dwMemberCount > 0)
121 {
122 lpMembers = RtlAllocateHeap(RtlGetProcessHeap(),
123 HEAP_ZERO_MEMORY,
124 dwMemberCount * sizeof(LPLOCALGROUP_MEMBERS_INFO_3));
125 if (lpMembers == NULL)
126 return 0;
127 }
128
129 j = 0;
130 for (; i < argc; i++)
131 {
132 if (argv[i][0] == L'/')
133 break;
134
135 lpMembers[j].lgrmi3_domainandname = argv[i];
136 j++;
137 }
138
139 for (; i < argc; i++)
140 {
141 if (_wcsicmp(argv[i], L"/help") == 0)
142 {
143 PrintResourceString(IDS_LOCALGROUP_HELP);
144 return 0;
145 }
146 else if (_wcsicmp(argv[i], L"/add") == 0)
147 {
148 bAdd = TRUE;
149 }
150 else if (_wcsicmp(argv[i], L"/delete") == 0)
151 {
152 bDelete = TRUE;
153 }
154 else if (_wcsnicmp(argv[i], L"/comment:", 9) == 0)
155 {
156 lpComment = &argv[i][9];
157 }
158 else if (_wcsicmp(argv[i], L"/domain") == 0)
159 {
160 printf("The /DOMAIN option is not supported yet!\n");
161 #if 0
162 bDomain = TRUE;
163 #endif
164 }
165 else
166 {
167 result = 1;
168 goto done;
169 }
170 }
171
172 if (lpGroupName == NULL)
173 {
174 result = 1;
175 goto done;
176 }
177
178 if (bAdd && bDelete)
179 {
180 result = 1;
181 goto done;
182 }
183
184 #if 0
185 printf("Group:\n %S\n", lpGroupName);
186
187 if (lpMembers != NULL)
188 {
189 printf("\nMembers:\n");
190 for (i = 0; i < dwMemberCount; i++)
191 printf(" %S\n", lpMembers[i].lgrmi3_domainandname);
192 }
193
194 if (lpComment != NULL)
195 {
196 printf("\nComment:\n %S\n", lpComment);
197 }
198 #endif
199
200 if (lpMembers == NULL)
201 {
202 if (!bAdd && !bDelete && lpComment != NULL)
203 {
204 /* Set group comment */
205 Info1002.lgrpi1002_comment = lpComment;
206 Status = NetLocalGroupSetInfo(NULL,
207 lpGroupName,
208 1002,
209 (LPBYTE)&Info1002,
210 NULL);
211 printf("Status: %lu\n", Status);
212 }
213 else if (bAdd && !bDelete)
214 {
215 /* Add the group */
216 if (lpComment == NULL)
217 {
218 Info0.lgrpi0_name = lpGroupName;
219 }
220 else
221 {
222 Info1.lgrpi1_name = lpGroupName;
223 Info1.lgrpi1_comment = lpComment;
224 }
225
226 Status = NetLocalGroupAdd(NULL,
227 (lpComment == NULL) ? 0 : 1,
228 (lpComment == NULL) ? (LPBYTE)&Info0 : (LPBYTE)&Info1,
229 NULL);
230 printf("Status: %lu\n", Status);
231 }
232 else if (!bAdd && bDelete && lpComment == NULL)
233 {
234 /* Delete the group */
235 Status = NetLocalGroupDel(NULL,
236 lpGroupName);
237 printf("Status: %lu\n", Status);
238 }
239 else
240 {
241 result = 1;
242 }
243 }
244 else
245 {
246 if (bAdd && !bDelete && lpComment == NULL)
247 {
248 /* Add group members */
249 Status = NetLocalGroupAddMembers(NULL,
250 lpGroupName,
251 3,
252 (LPBYTE)lpMembers,
253 dwMemberCount);
254 printf("Status: %lu\n", Status);
255 }
256 else if (!bAdd && bDelete && lpComment == NULL)
257 {
258 /* Delete group members */
259 Status = NetLocalGroupDelMembers(NULL,
260 lpGroupName,
261 3,
262 (LPBYTE)lpMembers,
263 dwMemberCount);
264 printf("Status: %lu\n", Status);
265 }
266 else
267 {
268 result = 1;
269 }
270 }
271
272 done:
273 if (lpMembers != NULL)
274 RtlFreeHeap(RtlGetProcessHeap(), 0, lpMembers);
275
276 if (result != 0)
277 PrintResourceString(IDS_LOCALGROUP_SYNTAX);
278
279 return result;
280 }
281
282 /* EOF */