groups.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. #include "winutils.h"
  18. //----------------------------------------------------------------------------
  19. // Function: PrintGroups
  20. //
  21. // Description:
  22. // Print group names to the console standard output for the given user
  23. //
  24. // Returns:
  25. // TRUE: on success
  26. //
  27. // Notes:
  28. // This function could fail on first pass when we fail to find groups for
  29. // domain account; so we do not report Windows API errors in this function.
  30. // If formatOutput is true, pipe character is used as separator for groups
  31. // otherwise, space.
  32. //
  33. static BOOL PrintGroups(
  34. LOCALGROUP_USERS_INFO_0 const *groups,
  35. DWORD entries,
  36. BOOL formatOutput)
  37. {
  38. BOOL ret = TRUE;
  39. LOCALGROUP_USERS_INFO_0 const *pTmpBuf = groups;
  40. DWORD i;
  41. for (i = 0; i < entries; i++)
  42. {
  43. if (pTmpBuf == NULL)
  44. {
  45. ret = FALSE;
  46. break;
  47. }
  48. if (i != 0)
  49. {
  50. if (formatOutput)
  51. {
  52. wprintf(L"|");
  53. }
  54. else
  55. {
  56. wprintf(L" ");
  57. }
  58. }
  59. wprintf(L"%s", pTmpBuf->lgrui0_name);
  60. pTmpBuf++;
  61. }
  62. if (ret)
  63. wprintf(L"\n");
  64. return ret;
  65. }
  66. //----------------------------------------------------------------------------
  67. // Function: ParseCommandLine
  68. //
  69. // Description:
  70. // Parses the command line
  71. //
  72. // Returns:
  73. // TRUE on the valid command line, FALSE otherwise
  74. //
  75. static BOOL ParseCommandLine(
  76. __in int argc,
  77. __in_ecount(argc) wchar_t *argv[],
  78. __out PWSTR *user,
  79. __out BOOL *formatOutput)
  80. {
  81. *formatOutput = FALSE;
  82. assert(argv != NULL);
  83. assert(user != NULL);
  84. if (argc == 1)
  85. {
  86. // implicitly use the current user
  87. *user = NULL;
  88. return TRUE;
  89. }
  90. else if (argc == 2)
  91. {
  92. // check if the second argument is formating
  93. if (wcscmp(argv[1], L"-F") == 0)
  94. {
  95. *user = NULL;
  96. *formatOutput = TRUE;
  97. return TRUE;
  98. }
  99. else
  100. {
  101. *user = argv[1];
  102. return TRUE;
  103. }
  104. }
  105. else if (argc == 3 && wcscmp(argv[1], L"-F") == 0)
  106. {
  107. // if 3 args, the second argument must be "-F"
  108. *user = argv[2];
  109. *formatOutput = TRUE;
  110. return TRUE;
  111. }
  112. return FALSE;
  113. }
  114. //----------------------------------------------------------------------------
  115. // Function: Groups
  116. //
  117. // Description:
  118. // The main method for groups command
  119. //
  120. // Returns:
  121. // 0: on success
  122. //
  123. // Notes:
  124. //
  125. //
  126. int Groups(__in int argc, __in_ecount(argc) wchar_t *argv[])
  127. {
  128. LPWSTR input = NULL;
  129. LPWSTR currentUser = NULL;
  130. DWORD cchCurrentUser = 0;
  131. LPLOCALGROUP_USERS_INFO_0 groups = NULL;
  132. DWORD entries = 0;
  133. DWORD dwRtnCode = ERROR_SUCCESS;
  134. int ret = EXIT_SUCCESS;
  135. BOOL formatOutput = FALSE;
  136. if (!ParseCommandLine(argc, argv, &input, &formatOutput))
  137. {
  138. fwprintf(stderr, L"Incorrect command line arguments.\n\n");
  139. GroupsUsage(argv[0]);
  140. return EXIT_FAILURE;
  141. }
  142. // if username was not specified on the command line, fallback to the
  143. // current user
  144. if (input == NULL)
  145. {
  146. GetUserNameW(currentUser, &cchCurrentUser);
  147. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  148. {
  149. currentUser = (LPWSTR) LocalAlloc(LPTR,
  150. (cchCurrentUser + 1) * sizeof(wchar_t));
  151. if (!currentUser)
  152. {
  153. ReportErrorCode(L"LocalAlloc", GetLastError());
  154. ret = EXIT_FAILURE;
  155. goto GroupsEnd;
  156. }
  157. if (GetUserNameW(currentUser, &cchCurrentUser))
  158. input = currentUser;
  159. else
  160. {
  161. ReportErrorCode(L"GetUserName", GetLastError());
  162. ret = EXIT_FAILURE;
  163. goto GroupsEnd;
  164. }
  165. }
  166. else
  167. {
  168. ReportErrorCode(L"GetUserName", GetLastError());
  169. ret = EXIT_FAILURE;
  170. goto GroupsEnd;
  171. }
  172. }
  173. if ((dwRtnCode = GetLocalGroupsForUser(input, &groups, &entries))
  174. != ERROR_SUCCESS)
  175. {
  176. ReportErrorCode(L"GetLocalGroupsForUser", dwRtnCode);
  177. ret = EXIT_FAILURE;
  178. goto GroupsEnd;
  179. }
  180. if (!PrintGroups(groups, entries, formatOutput))
  181. {
  182. ret = EXIT_FAILURE;
  183. }
  184. GroupsEnd:
  185. LocalFree(currentUser);
  186. if (groups != NULL) NetApiBufferFree(groups);
  187. return ret;
  188. }
  189. void GroupsUsage(LPCWSTR program)
  190. {
  191. fwprintf(stdout, L"\
  192. Usage: %s [OPTIONS] [USERNAME]\n\
  193. Print group information of the specified USERNAME \
  194. (the current user by default).\n\
  195. \n\
  196. OPTIONS: -F format the output by separating tokens with a pipe\n",
  197. program);
  198. }