groups.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. LPLOCALGROUP_USERS_INFO_0 groups,
  35. DWORD entries,
  36. BOOL formatOutput)
  37. {
  38. BOOL ret = TRUE;
  39. LPLOCALGROUP_USERS_INFO_0 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. int argc, wchar_t *argv[], wchar_t **user, BOOL *formatOutput)
  77. {
  78. *formatOutput = FALSE;
  79. assert(argv != NULL);
  80. assert(user != NULL);
  81. if (argc == 1)
  82. {
  83. // implicitly use the current user
  84. *user = NULL;
  85. return TRUE;
  86. }
  87. else if (argc == 2)
  88. {
  89. // check if the second argument is formating
  90. if (wcscmp(argv[1], L"-F") == 0)
  91. {
  92. *user = NULL;
  93. *formatOutput = TRUE;
  94. return TRUE;
  95. }
  96. else
  97. {
  98. *user = argv[1];
  99. return TRUE;
  100. }
  101. }
  102. else if (argc == 3 && wcscmp(argv[1], L"-F") == 0)
  103. {
  104. // if 3 args, the second argument must be "-F"
  105. *user = argv[2];
  106. *formatOutput = TRUE;
  107. return TRUE;
  108. }
  109. return FALSE;
  110. }
  111. //----------------------------------------------------------------------------
  112. // Function: Groups
  113. //
  114. // Description:
  115. // The main method for groups command
  116. //
  117. // Returns:
  118. // 0: on success
  119. //
  120. // Notes:
  121. //
  122. //
  123. int Groups(int argc, wchar_t *argv[])
  124. {
  125. LPWSTR input = NULL;
  126. LPWSTR currentUser = NULL;
  127. DWORD cchCurrentUser = 0;
  128. LPLOCALGROUP_USERS_INFO_0 groups = NULL;
  129. DWORD entries = 0;
  130. DWORD dwRtnCode = ERROR_SUCCESS;
  131. int ret = EXIT_SUCCESS;
  132. BOOL formatOutput = FALSE;
  133. if (!ParseCommandLine(argc, argv, &input, &formatOutput))
  134. {
  135. fwprintf(stderr, L"Incorrect command line arguments.\n\n");
  136. GroupsUsage(argv[0]);
  137. return EXIT_FAILURE;
  138. }
  139. // if username was not specified on the command line, fallback to the
  140. // current user
  141. if (input == NULL)
  142. {
  143. GetUserNameW(currentUser, &cchCurrentUser);
  144. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  145. {
  146. currentUser = (LPWSTR) LocalAlloc(LPTR,
  147. (cchCurrentUser + 1) * sizeof(wchar_t));
  148. if (!currentUser)
  149. {
  150. ReportErrorCode(L"LocalAlloc", GetLastError());
  151. ret = EXIT_FAILURE;
  152. goto GroupsEnd;
  153. }
  154. if (GetUserNameW(currentUser, &cchCurrentUser))
  155. input = currentUser;
  156. else
  157. {
  158. ReportErrorCode(L"GetUserName", GetLastError());
  159. ret = EXIT_FAILURE;
  160. goto GroupsEnd;
  161. }
  162. }
  163. else
  164. {
  165. ReportErrorCode(L"GetUserName", GetLastError());
  166. ret = EXIT_FAILURE;
  167. goto GroupsEnd;
  168. }
  169. }
  170. if ((dwRtnCode = GetLocalGroupsForUser(input, &groups, &entries))
  171. != ERROR_SUCCESS)
  172. {
  173. ReportErrorCode(L"GetLocalGroupsForUser", dwRtnCode);
  174. ret = EXIT_FAILURE;
  175. goto GroupsEnd;
  176. }
  177. if (!PrintGroups(groups, entries, formatOutput))
  178. {
  179. ret = EXIT_FAILURE;
  180. }
  181. GroupsEnd:
  182. LocalFree(currentUser);
  183. if (groups != NULL) NetApiBufferFree(groups);
  184. return ret;
  185. }
  186. void GroupsUsage(LPCWSTR program)
  187. {
  188. fwprintf(stdout, L"\
  189. Usage: %s [OPTIONS] [USERNAME]\n\
  190. Print group information of the specified USERNAME \
  191. (the current user by default).\n\
  192. \n\
  193. OPTIONS: -F format the output by separating tokens with a pipe\n",
  194. program);
  195. }