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.

332 lines
8.2 KiB

  1. /*++
  2. Managing user privileges can be achieved programmatically using the
  3. following steps:
  4. 1. Open the policy on the target machine with LsaOpenPolicy(). To grant
  5. privileges, open the policy with POLICY_CREATE_ACCOUNT and
  6. POLICY_LOOKUP_NAMES access. To revoke privileges, open the policy with
  7. POLICY_LOOKUP_NAMES access.
  8. 2. Obtain a SID (security identifier) representing the user/group of
  9. interest. The LookupAccountName() and LsaLookupNames() APIs can obtain a
  10. SID from an account name.
  11. 3. Call LsaAddAccountRights() to grant privileges to the user(s)
  12. represented by the supplied SID.
  13. 4. Call LsaRemoveAccountRights() to revoke privileges from the user(s)
  14. represented by the supplied SID.
  15. 5. Close the policy with LsaClose().
  16. To successfully grant and revoke privileges, the caller needs to be an
  17. administrator on the target system.
  18. The LSA API LsaEnumerateAccountRights() can be used to determine which
  19. privileges have been granted to an account.
  20. The LSA API LsaEnumerateAccountsWithUserRight() can be used to determine
  21. which accounts have been granted a specified privilege.
  22. Documentation and header files for these LSA APIs is provided in the
  23. Windows 32 SDK in the MSTOOLS\SECURITY directory.
  24. NOTE: These LSA APIs are currently implemented as Unicode only.
  25. This sample will grant the privilege SeServiceLogonRight to the account
  26. specified on argv[1].
  27. This sample is dependant on these import libs
  28. advapi32.lib (for LsaXxx)
  29. user32.lib (for wsprintf)
  30. This sample will work properly compiled ANSI or Unicode.
  31. You can use domain\account as argv[1]. For instance, mydomain\scott will
  32. grant the privilege to the mydomain domain account scott.
  33. The optional target machine is specified as argv[2], otherwise, the
  34. account database is updated on the local machine.
  35. The LSA APIs used by this sample are Unicode only.
  36. Use LsaRemoveAccountRights() to remove account rights.
  37. Scott Field (sfield) 12-Jul-95
  38. --*/
  39. #ifndef UNICODE
  40. #define UNICODE
  41. #endif // UNICODE
  42. #include <windows.h>
  43. #include <stdio.h>
  44. #include "ntsecapi.h"
  45. NTSTATUS
  46. OpenPolicy(
  47. LPWSTR ServerName, // machine to open policy on (Unicode)
  48. DWORD DesiredAccess, // desired access to policy
  49. PLSA_HANDLE PolicyHandle // resultant policy handle
  50. );
  51. LPTSTR
  52. ConvertTimeToString(
  53. LARGE_INTEGER time // Kerberos time value
  54. );
  55. void
  56. InitLsaString(
  57. PLSA_UNICODE_STRING LsaString, // destination
  58. LPWSTR String // source (Unicode)
  59. );
  60. void
  61. DisplayNtStatus(
  62. LPSTR szAPI, // pointer to function name (ANSI)
  63. NTSTATUS Status // NTSTATUS error value
  64. );
  65. void
  66. DisplayWinError(
  67. LPSTR szAPI, // pointer to function name (ANSI)
  68. DWORD WinError // DWORD WinError
  69. );
  70. #define RTN_OK 0
  71. #define RTN_USAGE 1
  72. #define RTN_ERROR 13
  73. //
  74. // If you have the ddk, include ntstatus.h.
  75. //
  76. #ifndef STATUS_SUCCESS
  77. #define STATUS_SUCCESS ((NTSTATUS)0x00000000L)
  78. #endif
  79. static LPCTSTR dt_output_dhms = TEXT("%d %s %02d:%02d:%02d");
  80. static LPCTSTR dt_day_plural = TEXT("days");
  81. static LPCTSTR dt_day_singular = TEXT("day");
  82. static LPCTSTR dt_output_donly = TEXT("%d %s");
  83. static LPCTSTR dt_output_hms = TEXT("%d:%02d:%02d");
  84. LPTSTR
  85. ConvertTimeToString(LARGE_INTEGER time)
  86. {
  87. int days, hours, minutes, seconds;
  88. DWORD tt;
  89. static TCHAR buf2[40];
  90. #define TPS (10*1000*1000)
  91. DWORD dt = (long)(time.QuadPart / TPS);
  92. days = (int) (dt / (24*3600l));
  93. tt = dt % (24*3600l);
  94. hours = (int) (tt / 3600);
  95. tt %= 3600;
  96. minutes = (int) (tt / 60);
  97. seconds = (int) (tt % 60);
  98. if (days) {
  99. if (hours || minutes || seconds) {
  100. wsprintf(buf2, dt_output_dhms, days,
  101. (days > 1) ? dt_day_plural : dt_day_singular,
  102. hours, minutes, seconds);
  103. }
  104. else {
  105. wsprintf(buf2, dt_output_donly, days,
  106. (days > 1) ? dt_day_plural : dt_day_singular);
  107. }
  108. }
  109. else {
  110. wsprintf(buf2, dt_output_hms, hours, minutes, seconds);
  111. }
  112. return(buf2);
  113. }
  114. int _cdecl
  115. main(int argc, char *argv[])
  116. {
  117. LSA_HANDLE PolicyHandle;
  118. WCHAR wComputerName[256]=L""; // static machine name buffer
  119. NTSTATUS Status;
  120. int iRetVal=RTN_ERROR; // assume error from main
  121. PPOLICY_DOMAIN_KERBEROS_TICKET_INFO KerbInfo;
  122. if(argc > 2)
  123. {
  124. fprintf(stderr,"Usage: %s [TargetMachine]\n",
  125. argv[0]);
  126. return RTN_USAGE;
  127. }
  128. //
  129. // Pick up machine name on argv[2], if appropriate
  130. // assumes source is ANSI. Resultant string is Unicode.
  131. //
  132. if(argc == 2) wsprintfW(wComputerName, L"%hS", argv[1]);
  133. //
  134. // Open the policy on the target machine.
  135. //
  136. if((Status=OpenPolicy(
  137. wComputerName, // target machine
  138. MAXIMUM_ALLOWED,
  139. &PolicyHandle // resultant policy handle
  140. )) != STATUS_SUCCESS) {
  141. DisplayNtStatus("OpenPolicy", Status);
  142. return RTN_ERROR;
  143. }
  144. //
  145. // Get the Kerberos policy
  146. //
  147. if ((Status=LsaQueryDomainInformationPolicy(
  148. PolicyHandle,
  149. PolicyDomainKerberosTicketInformation,
  150. &KerbInfo)) != STATUS_SUCCESS)
  151. {
  152. DisplayNtStatus("LsaQueryDomainInformationPolicy", Status);
  153. return RTN_ERROR;
  154. }
  155. //
  156. // Print out the Kerberos information
  157. //
  158. printf("Authentication options: 0x%x\n", KerbInfo->AuthenticationOptions);
  159. printf("MaxServiceTicketAge: %S\n",
  160. ConvertTimeToString(KerbInfo->MaxServiceTicketAge));
  161. printf("MaxTicketAge: %S\n", ConvertTimeToString(KerbInfo->MaxTicketAge));
  162. printf("MaxRenewAge: %S\n", ConvertTimeToString(KerbInfo->MaxRenewAge));
  163. printf("MaxClockSkew: %S\n", ConvertTimeToString(KerbInfo->MaxClockSkew));
  164. //
  165. // Free buffer
  166. //
  167. LsaFreeMemory(KerbInfo);
  168. //
  169. // Close the policy handle.
  170. //
  171. LsaClose(PolicyHandle);
  172. return iRetVal;
  173. }
  174. void
  175. InitLsaString(
  176. PLSA_UNICODE_STRING LsaString,
  177. LPWSTR String
  178. )
  179. {
  180. DWORD StringLength;
  181. if (String == NULL) {
  182. LsaString->Buffer = NULL;
  183. LsaString->Length = 0;
  184. LsaString->MaximumLength = 0;
  185. return;
  186. }
  187. StringLength = wcslen(String);
  188. LsaString->Buffer = String;
  189. LsaString->Length = (USHORT) StringLength * sizeof(WCHAR);
  190. LsaString->MaximumLength=(USHORT)(StringLength+1) * sizeof(WCHAR);
  191. }
  192. NTSTATUS
  193. OpenPolicy(
  194. LPWSTR ServerName,
  195. DWORD DesiredAccess,
  196. PLSA_HANDLE PolicyHandle
  197. )
  198. {
  199. LSA_OBJECT_ATTRIBUTES ObjectAttributes;
  200. LSA_UNICODE_STRING ServerString;
  201. PLSA_UNICODE_STRING Server = NULL;
  202. //
  203. // Always initialize the object attributes to all zeroes.
  204. //
  205. ZeroMemory(&ObjectAttributes, sizeof(ObjectAttributes));
  206. if (ServerName != NULL) {
  207. //
  208. // Make a LSA_UNICODE_STRING out of the LPWSTR passed in
  209. //
  210. InitLsaString(&ServerString, ServerName);
  211. Server = &ServerString;
  212. }
  213. //
  214. // Attempt to open the policy.
  215. //
  216. return LsaOpenPolicy(
  217. Server,
  218. &ObjectAttributes,
  219. DesiredAccess,
  220. PolicyHandle
  221. );
  222. }
  223. void
  224. DisplayNtStatus(
  225. LPSTR szAPI,
  226. NTSTATUS Status
  227. )
  228. {
  229. //
  230. // Convert the NTSTATUS to Winerror. Then call DisplayWinError().
  231. //
  232. DisplayWinError(szAPI, LsaNtStatusToWinError(Status));
  233. }
  234. void
  235. DisplayWinError(
  236. LPSTR szAPI,
  237. DWORD WinError
  238. )
  239. {
  240. LPSTR MessageBuffer;
  241. DWORD dwBufferLength;
  242. //
  243. // TODO: Get this fprintf out of here!
  244. //
  245. fprintf(stderr,"%s error! (0x%x)\n", szAPI, WinError);
  246. if(dwBufferLength=FormatMessageA(
  247. FORMAT_MESSAGE_ALLOCATE_BUFFER |
  248. FORMAT_MESSAGE_FROM_SYSTEM,
  249. NULL,
  250. WinError,
  251. GetUserDefaultLangID(),
  252. (LPSTR) &MessageBuffer,
  253. 0,
  254. NULL
  255. ))
  256. {
  257. DWORD dwBytesWritten; // unused
  258. //
  259. // Output message string on stderr.
  260. //
  261. WriteFile(
  262. GetStdHandle(STD_ERROR_HANDLE),
  263. MessageBuffer,
  264. dwBufferLength,
  265. &dwBytesWritten,
  266. NULL
  267. );
  268. //
  269. // Free the buffer allocated by the system.
  270. //
  271. LocalFree(MessageBuffer);
  272. }
  273. }