Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

378 lines
7.7 KiB

  1. /*++
  2. Copyright (c) 1992 Microsoft Corporation
  3. Module Name:
  4. ctlkacct.c
  5. Abstract:
  6. Local Security Authority Subsystem - Short CT for Win 32 LookupAccountName/Sid
  7. This is a small test that simply calls the Win 32 LookupAccountName/Sid
  8. API once.
  9. Usage:
  10. ctlkacct \\ServerName AccountName
  11. Building Instructions:
  12. nmake UMTEST=ctlkacc UMTYPE=console
  13. Author:
  14. Scott Birrell (ScottBi) July 20, 1992
  15. [ Adapted from a test written by TimF, not completely to Nt standards ]
  16. Environment:
  17. Revision History:
  18. --*/
  19. #include <stdio.h>
  20. #include <windows.h>
  21. VOID
  22. DumpSID(
  23. IN PSID s
  24. );
  25. #define LSA_WIN_STANDARD_BUFFER_SIZE 0x000000200L
  26. VOID __cdecl
  27. main(argc, argv)
  28. int argc;
  29. char **argv;
  30. {
  31. char SidFromLookupName[LSA_WIN_STANDARD_BUFFER_SIZE];
  32. char RefDFromLookupName[LSA_WIN_STANDARD_BUFFER_SIZE];
  33. DWORD cbSidFromLookupName, cchRefDFromLookupName;
  34. SID_NAME_USE UseFromLookupName;
  35. DWORD cchNameFromLookupSid;
  36. char NameFromLookupSid[LSA_WIN_STANDARD_BUFFER_SIZE];
  37. char RefDFromLookupSid[LSA_WIN_STANDARD_BUFFER_SIZE];
  38. DWORD cchRefDFromLookupSid;
  39. SID_NAME_USE UseFromLookupSid;
  40. BOOL BoolStatus = TRUE;
  41. DWORD LastError;
  42. if (argc != 3) {
  43. printf("usage: ctlkacct <(char *)SystemName> <(char *)AccountName>\n");
  44. return(0);
  45. }
  46. cbSidFromLookupName = 0;
  47. cchRefDFromLookupName = 0;
  48. BoolStatus = LookupAccountName(argv[1],
  49. argv[2],
  50. (PSID)SidFromLookupName,
  51. &cbSidFromLookupName,
  52. RefDFromLookupName,
  53. &cchRefDFromLookupName,
  54. &UseFromLookupName
  55. );
  56. if (BoolStatus) {
  57. printf(
  58. "LookupAccountName() with zero buffer sizes returned TRUE\n"
  59. );
  60. }
  61. //
  62. // Get the Last Error (in DOS errorcode from).
  63. //
  64. LastError = GetLastError();
  65. if (LastError != ERROR_INSUFFICIENT_BUFFER) {
  66. printf(
  67. "Unexpected Last Error %d returned from LookupAccountName\n"
  68. "Expected %d (ERROR_INSUFFICIENT_BUFFER)\n",
  69. LastError
  70. );
  71. }
  72. //
  73. // Now call LookupAccountName() again, using the buffer sizes returned.
  74. //
  75. BoolStatus = LookupAccountName(argv[1],
  76. argv[2],
  77. (PSID)SidFromLookupName,
  78. &cbSidFromLookupName,
  79. RefDFromLookupName,
  80. &cchRefDFromLookupName,
  81. &UseFromLookupName
  82. );
  83. if (!BoolStatus) {
  84. printf(
  85. "LookupAccountName failed - \n"
  86. "LastError = %d\n",
  87. GetLastError()
  88. );
  89. return(0);
  90. }
  91. /*
  92. * Print information returned by LookupAccountName
  93. */
  94. printf(
  95. "*********************************************\n"
  96. "Information returned by LookupAccountName\n"
  97. "*********************************************\n\n"
  98. );
  99. printf( "Sid = " );
  100. DumpSID((PSID) SidFromLookupName);
  101. printf(
  102. "Size of Sid = %d\n",
  103. cbSidFromLookupName
  104. );
  105. printf(
  106. "Referenced Domain Name = %s\n"
  107. "Size of Referenced Domain Name = %d\n",
  108. RefDFromLookupName,
  109. cchRefDFromLookupName
  110. );
  111. printf("Name Use = ");
  112. switch (UseFromLookupName) {
  113. case SidTypeUser:
  114. printf("SidTypeUser\n");
  115. break;
  116. case SidTypeGroup:
  117. printf("SidTypeGroup\n");
  118. break;
  119. case SidTypeDomain:
  120. printf("SidTypeDomain\n");
  121. break;
  122. case SidTypeAlias:
  123. printf("SidTypeAlias\n");
  124. break;
  125. case SidTypeWellKnownGroup:
  126. printf("SidTypeWellKnownGroup\n");
  127. break;
  128. case SidTypeDeletedAccount:
  129. printf("SidTypeDeletedAccount\n");
  130. break;
  131. case SidTypeInvalid:
  132. printf("SidTypeInvalid\n");
  133. break;
  134. case SidTypeUnknown:
  135. printf("SidTypeUnknown\n");
  136. break;
  137. default:
  138. break;
  139. }
  140. cchNameFromLookupSid = 0;
  141. cchRefDFromLookupSid = 0;
  142. //
  143. // Now lookup the Sid we just obtained and see if we get the name back.
  144. // First, provide zero buffer sizes so that we get the sizes needed
  145. // returned.
  146. //
  147. cchNameFromLookupSid = 0;
  148. cchRefDFromLookupSid = 0;
  149. BoolStatus = LookupAccountSid(argv[1],
  150. (PSID) SidFromLookupName,
  151. NameFromLookupSid,
  152. &cchNameFromLookupSid,
  153. RefDFromLookupSid,
  154. &cchRefDFromLookupSid,
  155. &UseFromLookupSid
  156. );
  157. if (BoolStatus) {
  158. printf("LookupAccountSid() with zero buffer sizes returned TRUE\n");
  159. }
  160. //
  161. // Get the Last Error (in DOS errorcode from).
  162. //
  163. LastError = GetLastError();
  164. if (LastError != ERROR_INSUFFICIENT_BUFFER) {
  165. printf(
  166. "Unexpected Last Error %d returned from LookupAccountSid\n"
  167. "Expected %d (ERROR_INSUFFICIENT_BUFFER)\n",
  168. LastError
  169. );
  170. }
  171. //
  172. // Now call LookupAccountSid() again, using the buffer sizes obtained
  173. // from the previous call.
  174. //
  175. if (!LookupAccountSid(argv[1],
  176. (PSID) SidFromLookupName,
  177. NameFromLookupSid,
  178. &cchNameFromLookupSid,
  179. RefDFromLookupSid,
  180. &cchRefDFromLookupSid,
  181. &UseFromLookupSid
  182. )) {
  183. printf(
  184. "LookupAccountSid failed\n"
  185. "LastError = %d\n",
  186. GetLastError()
  187. );
  188. return(0);
  189. }
  190. /*
  191. * Print information returned by LookupAccountSid
  192. */
  193. printf(
  194. "*********************************************\n"
  195. "Information returned by LookupAccountSid\n"
  196. "*********************************************\n\n"
  197. );
  198. printf(
  199. "Account Name = %s\n"
  200. "Account Name Size (chars) = %d\n"
  201. "Referenced Domain Name = %s\n"
  202. "Referenced Domain Size (chars) = %d\n",
  203. NameFromLookupSid,
  204. cchNameFromLookupSid,
  205. RefDFromLookupSid,
  206. cchRefDFromLookupSid
  207. );
  208. printf("Sid Use = ");
  209. switch (UseFromLookupSid) {
  210. case SidTypeUser:
  211. printf("SidTypeUser\n");
  212. break;
  213. case SidTypeGroup:
  214. printf("SidTypeGroup\n");
  215. break;
  216. case SidTypeDomain:
  217. printf("SidTypeDomain\n");
  218. break;
  219. case SidTypeAlias:
  220. printf("SidTypeAlias\n");
  221. break;
  222. case SidTypeWellKnownGroup:
  223. printf("SidTypeWellKnownGroup\n");
  224. break;
  225. case SidTypeDeletedAccount:
  226. printf("SidTypeDeletedAccount\n");
  227. break;
  228. case SidTypeInvalid:
  229. printf("SidTypeInvalid\n");
  230. break;
  231. case SidTypeUnknown:
  232. printf("SidTypeUnknown\n");
  233. break;
  234. default:
  235. break;
  236. }
  237. return(0);
  238. }
  239. VOID
  240. DumpSID(
  241. IN PSID s
  242. )
  243. {
  244. static char b[128];
  245. SID_IDENTIFIER_AUTHORITY *a;
  246. ULONG id = 0, i;
  247. try {
  248. b[0] = '\0';
  249. a = GetSidIdentifierAuthority(s);
  250. sprintf(b, "s-0x1-%02x%02x%02x%02x%02x%02x", a -> Value[0],
  251. a -> Value[1], a -> Value[2], a -> Value[3], a ->
  252. Value[4], a -> Value[5]);
  253. for (i = 0; i < *GetSidSubAuthorityCount(s); i++) {
  254. sprintf(b, "%s-0x%lx", b, *GetSidSubAuthority(s, i));
  255. }
  256. printf("%s\n", b);
  257. } except (EXCEPTION_EXECUTE_HANDLER) {
  258. if (*b) {
  259. printf("%s", b);
  260. }
  261. printf("<invalid pointer (0x%lx)>\n", s);
  262. }
  263. }