ReactOS  0.4.12-dev-102-g4b7f1e0
cmdLocalGroup.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS net command
4  * FILE: base/applications/network/net/cmdLocalGroup.c
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
26 {
28  PSERVER_INFO_100 pServer = NULL;
29  DWORD dwRead = 0, dwTotal = 0;
30  DWORD i;
31  DWORD_PTR ResumeHandle = 0;
33 
34 
35  Status = NetServerGetInfo(NULL,
36  100,
37  (LPBYTE*)&pServer);
38  if (Status != NERR_Success)
39  return Status;
40 
41  ConPuts(StdOut, L"\n");
42  ConResPrintf(StdOut, IDS_LOCALGROUP_ALIASES, pServer->sv100_name);
43  ConPuts(StdOut, L"\n\n");
44  PrintPadding(L'-', 79);
45  ConPuts(StdOut, L"\n");
46 
47  NetApiBufferFree(pServer);
48 
49  Status = NetLocalGroupEnum(NULL,
50  0,
51  (LPBYTE*)&pBuffer,
53  &dwRead,
54  &dwTotal,
55  &ResumeHandle);
56  if (Status != NERR_Success)
57  return Status;
58 
59  qsort(pBuffer,
60  dwRead,
61  sizeof(PLOCALGROUP_INFO_0),
62  CompareInfo);
63 
64  for (i = 0; i < dwRead; i++)
65  {
66  if (pBuffer[i].lgrpi0_name)
67  ConPrintf(StdOut, L"*%s\n", pBuffer[i].lgrpi0_name);
68  }
69 
70  NetApiBufferFree(pBuffer);
71 
72  return NERR_Success;
73 }
74 
75 
76 static
79 {
80  PLOCALGROUP_INFO_1 pGroupInfo = NULL;
82  PSERVER_INFO_100 pServer = NULL;
83  LPWSTR *pNames = NULL;
84  DWORD dwRead = 0;
85  DWORD dwTotal = 0;
86  DWORD_PTR ResumeHandle = 0;
87  DWORD i;
88  DWORD len;
89  INT nPaddedLength = 18;
91 
92  Status = NetLocalGroupGetInfo(NULL,
93  lpGroupName,
94  1,
95  (LPBYTE*)&pGroupInfo);
96  if (Status != NERR_Success)
97  return Status;
98 
100  lpGroupName,
101  3,
102  (LPBYTE*)&pMembers,
104  &dwRead,
105  &dwTotal,
106  &ResumeHandle);
107  if (Status != NERR_Success)
108  goto done;
109 
110  Status = NetServerGetInfo(NULL,
111  100,
112  (LPBYTE*)&pServer);
113  if (Status != NERR_Success)
114  goto done;
115 
116  pNames = RtlAllocateHeap(RtlGetProcessHeap(),
118  dwRead * sizeof(LPWSTR));
119  if (pNames == NULL)
120  {
121  Status = ERROR_OUTOFMEMORY;
122  goto done;
123  }
124 
125  len = wcslen(pServer->sv100_name);
126  for (i = 0; i < dwRead; i++)
127  {
128  if (!wcsncmp(pMembers[i].lgrmi3_domainandname, pServer->sv100_name, len))
129  pNames[i] = &pMembers[i].lgrmi3_domainandname[len + 1];
130  else
131  pNames[i] = pMembers[i].lgrmi3_domainandname;
132  }
133 
135  ConPrintf(StdOut, L"%s\n", pGroupInfo->lgrpi1_name);
136 
138  ConPrintf(StdOut, L"%s\n", pGroupInfo->lgrpi1_comment);
139 
140  ConPuts(StdOut, L"\n");
141 
143  ConPuts(StdOut, L"\n\n");
144 
145  PrintPadding(L'-', 79);
146  ConPuts(StdOut, L"\n");
147 
148  for (i = 0; i < dwRead; i++)
149  {
150  if (pNames[i])
151  ConPrintf(StdOut, L"%s\n", pNames[i]);
152  }
153 
154 done:
155  if (pNames != NULL)
156  RtlFreeHeap(RtlGetProcessHeap(), 0, pNames);
157 
158  if (pServer != NULL)
159  NetApiBufferFree(pServer);
160 
161  if (pMembers != NULL)
162  NetApiBufferFree(pMembers);
163 
164  if (pGroupInfo != NULL)
165  NetApiBufferFree(pGroupInfo);
166 
167  return Status;
168 }
169 
170 
171 INT
173  INT argc,
174  WCHAR **argv)
175 {
176  INT i, j;
177  INT result = 0;
178  ULONG dwMemberCount = 0;
179  BOOL bAdd = FALSE;
180  BOOL bDelete = FALSE;
181 #if 0
182  BOOL bDomain = FALSE;
183 #endif
184  LPWSTR lpGroupName = NULL;
185  LPWSTR lpComment = NULL;
186  LPLOCALGROUP_MEMBERS_INFO_3 lpMembers = NULL;
187  LOCALGROUP_INFO_0 Info0;
188  LOCALGROUP_INFO_1 Info1;
189  LOCALGROUP_INFO_1002 Info1002;
191 
192  if (argc == 2)
193  {
194  Status = EnumerateLocalGroups();
195  ConPrintf(StdOut, L"Status: %lu\n", Status);
196  return 0;
197  }
198  else if (argc == 3)
199  {
200  Status = DisplayLocalGroup(argv[2]);
201  ConPrintf(StdOut, L"Status: %lu\n", Status);
202  return 0;
203  }
204 
205  i = 2;
206  if (argv[i][0] != L'/')
207  {
208  lpGroupName = argv[i];
209  i++;
210  }
211 
212  for (j = i; j < argc; j++)
213  {
214  if (argv[j][0] == L'/')
215  break;
216 
217  dwMemberCount++;
218  }
219 
220  ConPrintf(StdOut, L"Member count: %lu\n", dwMemberCount);
221 
222  if (dwMemberCount > 0)
223  {
224  lpMembers = RtlAllocateHeap(RtlGetProcessHeap(),
226  dwMemberCount * sizeof(LPLOCALGROUP_MEMBERS_INFO_3));
227  if (lpMembers == NULL)
228  return 0;
229  }
230 
231  j = 0;
232  for (; i < argc; i++)
233  {
234  if (argv[i][0] == L'/')
235  break;
236 
237  lpMembers[j].lgrmi3_domainandname = argv[i];
238  j++;
239  }
240 
241  for (; i < argc; i++)
242  {
243  if (_wcsicmp(argv[i], L"/help") == 0)
244  {
255  return 0;
256  }
257  else if (_wcsicmp(argv[i], L"/add") == 0)
258  {
259  bAdd = TRUE;
260  }
261  else if (_wcsicmp(argv[i], L"/delete") == 0)
262  {
263  bDelete = TRUE;
264  }
265  else if (_wcsnicmp(argv[i], L"/comment:", 9) == 0)
266  {
267  lpComment = &argv[i][9];
268  }
269  else if (_wcsicmp(argv[i], L"/domain") == 0)
270  {
272 #if 0
273  bDomain = TRUE;
274 #endif
275  }
276  else
277  {
278  result = 1;
279  goto done;
280  }
281  }
282 
283  if (lpGroupName == NULL)
284  {
285  result = 1;
286  goto done;
287  }
288 
289  if (bAdd && bDelete)
290  {
291  result = 1;
292  goto done;
293  }
294 
295 #if 0
296  ConPrintf(StdOut, L"Group:\n %s\n", lpGroupName);
297 
298  if (lpMembers != NULL)
299  {
300  ConPuts(StdOut, L"\nMembers:\n");
301  for (i = 0; i < dwMemberCount; i++)
302  ConPrintf(StdOut, L" %s\n", lpMembers[i].lgrmi3_domainandname);
303  }
304 
305  if (lpComment != NULL)
306  {
307  ConPrintf(StdOut, L"\nComment:\n %s\n", lpComment);
308  }
309 #endif
310 
311  if (lpMembers == NULL)
312  {
313  if (!bAdd && !bDelete && lpComment != NULL)
314  {
315  /* Set group comment */
316  Info1002.lgrpi1002_comment = lpComment;
317  Status = NetLocalGroupSetInfo(NULL,
318  lpGroupName,
319  1002,
320  (LPBYTE)&Info1002,
321  NULL);
322  ConPrintf(StdOut, L"Status: %lu\n", Status);
323  }
324  else if (bAdd && !bDelete)
325  {
326  /* Add the group */
327  if (lpComment == NULL)
328  {
329  Info0.lgrpi0_name = lpGroupName;
330  }
331  else
332  {
333  Info1.lgrpi1_name = lpGroupName;
334  Info1.lgrpi1_comment = lpComment;
335  }
336 
337  Status = NetLocalGroupAdd(NULL,
338  (lpComment == NULL) ? 0 : 1,
339  (lpComment == NULL) ? (LPBYTE)&Info0 : (LPBYTE)&Info1,
340  NULL);
341  ConPrintf(StdOut, L"Status: %lu\n", Status);
342  }
343  else if (!bAdd && bDelete && lpComment == NULL)
344  {
345  /* Delete the group */
346  Status = NetLocalGroupDel(NULL,
347  lpGroupName);
348  ConPrintf(StdOut, L"Status: %lu\n", Status);
349  }
350  else
351  {
352  result = 1;
353  }
354  }
355  else
356  {
357  if (bAdd && !bDelete && lpComment == NULL)
358  {
359  /* Add group members */
360  Status = NetLocalGroupAddMembers(NULL,
361  lpGroupName,
362  3,
363  (LPBYTE)lpMembers,
364  dwMemberCount);
365  ConPrintf(StdOut, L"Status: %lu\n", Status);
366  }
367  else if (!bAdd && bDelete && lpComment == NULL)
368  {
369  /* Delete group members */
370  Status = NetLocalGroupDelMembers(NULL,
371  lpGroupName,
372  3,
373  (LPBYTE)lpMembers,
374  dwMemberCount);
375  ConPrintf(StdOut, L"Status: %lu\n", Status);
376  }
377  else
378  {
379  result = 1;
380  }
381  }
382 
383 done:
384  if (lpMembers != NULL)
385  RtlFreeHeap(RtlGetProcessHeap(), 0, lpMembers);
386 
387  if (result != 0)
388  {
391  }
392 
393  return result;
394 }
395 
396 /* EOF */
NET_API_STATUS WINAPI NetLocalGroupDelMembers(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE buf, DWORD totalentries)
Definition: local_group.c:814
#define IDS_LOCALGROUP_SYNTAX
Definition: resource.h:45
#define MAX_PREFERRED_LENGTH
Definition: lmcons.h:48
static int argc
Definition: ServiceArgs.c:12
GLenum GLclampf GLint GLenum GLuint GLenum GLenum GLsizei GLenum const GLvoid GLfloat GLfloat GLfloat GLfloat GLclampd GLint GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean GLboolean GLboolean GLboolean GLint GLenum GLsizei const GLvoid GLenum GLint GLenum GLint GLint GLsizei GLint GLenum GLint GLint GLint GLint GLsizei GLenum GLsizei const GLuint GLboolean GLenum GLenum GLint GLsizei GLenum GLsizei GLenum const GLvoid GLboolean const GLboolean GLenum const GLdouble const GLfloat const GLdouble const GLfloat GLenum GLint GLint GLint GLint GLint GLint j
Definition: glfuncs.h:98
#define TRUE
Definition: types.h:120
NET_API_STATUS WINAPI NetLocalGroupAddMembers(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE buf, DWORD totalentries)
Definition: local_group.c:517
static int CompareInfo(const void *a, const void *b)
Definition: cmdLocalGroup.c:15
NET_API_STATUS WINAPI NetLocalGroupAdd(LPCWSTR servername, DWORD level, LPBYTE buf, LPDWORD parm_err)
Definition: local_group.c:334
__wchar_t WCHAR
Definition: xmlstorage.h:180
static NET_API_STATUS EnumerateLocalGroups(VOID)
Definition: cmdLocalGroup.c:25
static NET_API_STATUS DisplayLocalGroup(LPWSTR lpGroupName)
Definition: cmdLocalGroup.c:78
BOOLEAN NTAPI RtlFreeHeap(IN PVOID HeapHandle, IN ULONG Flags, IN PVOID HeapBase)
Definition: heap.c:603
_Check_return_ _CRTIMP int __cdecl _wcsnicmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
#define IDS_LOCALGROUP_MEMBERS
Definition: resource.h:127
LPWSTR lgrpi1_comment
Definition: lmaccess.h:510
#define IDS_LOCALGROUP_COMMENT
Definition: resource.h:126
int32_t INT
Definition: typedefs.h:56
static char ** argv
Definition: ServiceArgs.c:11
#define IDS_LOCALGROUP_HELP_4
Definition: resource.h:49
#define NERR_Success
Definition: lmerr.h:5
NET_API_STATUS WINAPI NetLocalGroupSetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE buf, LPDWORD parm_err)
Definition: local_group.c:1729
GLenum GLclampf GLint i
Definition: glfuncs.h:14
#define IDS_GENERIC_SYNTAX
Definition: resource.h:202
unsigned char * LPBYTE
Definition: typedefs.h:52
NET_API_STATUS WINAPI NetLocalGroupGetInfo(LPCWSTR servername, LPCWSTR groupname, DWORD level, LPBYTE *bufptr)
Definition: local_group.c:1222
void __cdecl qsort(_Inout_updates_bytes_(_NumOfElements *_SizeOfElements) void *_Base, _In_ size_t _NumOfElements, _In_ size_t _SizeOfElements, _In_ int(__cdecl *_PtFuncCompare)(const void *, const void *))
INT __cdecl ConPrintf(IN PCON_STREAM Stream, IN LPWSTR szStr,...)
Definition: outstream.c:520
#define IDS_GENERIC_PAGE
Definition: resource.h:203
smooth NULL
Definition: ftsmooth.c:416
PVOID pBuffer
LPWSTR lgrpi1002_comment
Definition: lmaccess.h:513
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
PVOID NTAPI RtlAllocateHeap(IN PVOID HeapHandle, IN ULONG Flags, IN SIZE_T Size)
Definition: heap.c:585
DWORD NET_API_STATUS
Definition: ms-dtyp.idl:91
GLboolean GLboolean GLboolean b
Definition: glext.h:6204
unsigned int BOOL
Definition: ntddk_ex.h:94
NET_API_STATUS WINAPI NetLocalGroupGetMembers(LPCWSTR servername, LPCWSTR localgroupname, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries, PDWORD_PTR resumehandle)
Definition: local_group.c:1351
unsigned long DWORD
Definition: ntddk_ex.h:95
#define StdErr
Definition: stream.h:77
_Check_return_ _CRTIMP int __cdecl wcsncmp(_In_reads_or_z_(_MaxCount) const wchar_t *_Str1, _In_reads_or_z_(_MaxCount) const wchar_t *_Str2, _In_ size_t _MaxCount)
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
LPWSTR lgrpi0_name
Definition: lmaccess.h:506
#define IDS_ERROR_OPTION_NOT_SUPPORTED
Definition: resource.h:205
static const WCHAR L[]
Definition: oid.c:1087
GLenum GLsizei len
Definition: glext.h:6722
NET_API_STATUS WINAPI NetServerGetInfo(LMSTR servername, DWORD level, LPBYTE *bufptr)
Definition: srvsvc.c:369
#define IDS_LOCALGROUP_ALIAS_NAME
Definition: resource.h:125
uint32_t DWORD_PTR
Definition: typedefs.h:63
LPWSTR lgrpi1_name
Definition: lmaccess.h:509
Status
Definition: gdiplustypes.h:24
#define IDS_LOCALGROUP_HELP_6
Definition: resource.h:51
NET_API_STATUS WINAPI NetApiBufferFree(LPVOID Buffer)
Definition: apibuf.c:43
#define IDS_LOCALGROUP_HELP_3
Definition: resource.h:48
VOID PrintPaddedResourceString(UINT uID, INT nPaddedLength)
Definition: main.c:51
#define IDS_LOCALGROUP_HELP_2
Definition: resource.h:47
INT cmdLocalGroup(INT argc, WCHAR **argv)
#define IDS_LOCALGROUP_HELP_7
Definition: resource.h:52
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
VOID PrintPadding(WCHAR chr, INT nPaddedLength)
Definition: main.c:64
INT ConPuts(IN PCON_STREAM Stream, IN LPWSTR szStr)
Definition: outstream.c:427
#define IDS_LOCALGROUP_HELP_1
Definition: resource.h:46
#define StdOut
Definition: stream.h:76
NET_API_STATUS WINAPI NetLocalGroupDel(LPCWSTR servername, LPCWSTR groupname)
Definition: local_group.c:674
unsigned int ULONG
Definition: retypes.h:1
NET_API_STATUS WINAPI NetLocalGroupEnum(LPCWSTR servername, DWORD level, LPBYTE *bufptr, DWORD prefmaxlen, LPDWORD entriesread, LPDWORD totalentries, PDWORD_PTR resumehandle)
Definition: local_group.c:971
#define IDS_LOCALGROUP_ALIASES
Definition: resource.h:124
#define IDS_LOCALGROUP_HELP_5
Definition: resource.h:50
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
WCHAR * LPWSTR
Definition: xmlstorage.h:184
GLuint64EXT * result
Definition: glext.h:11304
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
_Check_return_ _CRTIMP int __cdecl _wcsicmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define ERROR_OUTOFMEMORY
Definition: deptool.c:13