account.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include "nssm.h"
  2. #include <sddl.h>
  3. extern imports_t imports;
  4. /* Open Policy object. */
  5. int open_lsa_policy(LSA_HANDLE *policy) {
  6. LSA_OBJECT_ATTRIBUTES attributes;
  7. ZeroMemory(&attributes, sizeof(attributes));
  8. NTSTATUS status = LsaOpenPolicy(0, &attributes, POLICY_ALL_ACCESS, policy);
  9. if (status) {
  10. print_message(stderr, NSSM_MESSAGE_LSAOPENPOLICY_FAILED, error_string(LsaNtStatusToWinError(status)));
  11. return 1;
  12. }
  13. return 0;
  14. }
  15. /* Look up SID for an account. */
  16. int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) {
  17. LSA_HANDLE handle;
  18. if (! policy) {
  19. policy = &handle;
  20. if (open_lsa_policy(policy)) return 1;
  21. }
  22. LSA_UNICODE_STRING lsa_username;
  23. #ifdef UNICODE
  24. lsa_username.Buffer = (wchar_t *) username;
  25. lsa_username.Length = (unsigned short) _tcslen(username) * sizeof(TCHAR);
  26. lsa_username.MaximumLength = lsa_username.Length + sizeof(TCHAR);
  27. #else
  28. size_t buflen;
  29. mbstowcs_s(&buflen, NULL, 0, username, _TRUNCATE);
  30. lsa_username.MaximumLength = (unsigned short) buflen * sizeof(wchar_t);
  31. lsa_username.Length = lsa_username.MaximumLength - sizeof(wchar_t);
  32. lsa_username.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), 0, lsa_username.MaximumLength);
  33. if (lsa_username.Buffer) mbstowcs_s(&buflen, lsa_username.Buffer, lsa_username.MaximumLength, username, _TRUNCATE);
  34. else {
  35. if (policy == &handle) LsaClose(handle);
  36. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("LSA_UNICODE_STRING"), _T("username_sid()"));
  37. return 2;
  38. }
  39. #endif
  40. LSA_REFERENCED_DOMAIN_LIST *translated_domains;
  41. LSA_TRANSLATED_SID *translated_sid;
  42. NTSTATUS status = LsaLookupNames(*policy, 1, &lsa_username, &translated_domains, &translated_sid);
  43. #ifndef UNICODE
  44. HeapFree(GetProcessHeap(), 0, lsa_username.Buffer);
  45. #endif
  46. if (policy == &handle) LsaClose(handle);
  47. if (status) {
  48. LsaFreeMemory(translated_domains);
  49. LsaFreeMemory(translated_sid);
  50. print_message(stderr, NSSM_MESSAGE_LSALOOKUPNAMES_FAILED, username, error_string(LsaNtStatusToWinError(status)));
  51. return 3;
  52. }
  53. if (translated_sid->Use != SidTypeUser && translated_sid->Use != SidTypeWellKnownGroup) {
  54. LsaFreeMemory(translated_domains);
  55. LsaFreeMemory(translated_sid);
  56. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  57. return 4;
  58. }
  59. LSA_TRUST_INFORMATION *trust = &translated_domains->Domains[translated_sid->DomainIndex];
  60. if (! trust || ! IsValidSid(trust->Sid)) {
  61. LsaFreeMemory(translated_domains);
  62. LsaFreeMemory(translated_sid);
  63. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  64. return 5;
  65. }
  66. /* GetSidSubAuthority*() return pointers! */
  67. unsigned char *n = GetSidSubAuthorityCount(trust->Sid);
  68. /* Convert translated SID to SID. */
  69. *sid = (SID *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, GetSidLengthRequired(*n + 1));
  70. if (! *sid) {
  71. LsaFreeMemory(translated_domains);
  72. LsaFreeMemory(translated_sid);
  73. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SID"), _T("grant_logon_as_service"));
  74. return 6;
  75. }
  76. unsigned long error;
  77. if (! InitializeSid(*sid, GetSidIdentifierAuthority(trust->Sid), *n + 1)) {
  78. error = GetLastError();
  79. HeapFree(GetProcessHeap(), 0, *sid);
  80. LsaFreeMemory(translated_domains);
  81. LsaFreeMemory(translated_sid);
  82. print_message(stderr, NSSM_MESSAGE_INITIALIZESID_FAILED, username, error_string(error));
  83. return 7;
  84. }
  85. for (unsigned char i = 0; i <= *n; i++) {
  86. unsigned long *sub = GetSidSubAuthority(*sid, i);
  87. if (i < *n) *sub = *GetSidSubAuthority(trust->Sid, i);
  88. else *sub = translated_sid->RelativeId;
  89. }
  90. int ret = 0;
  91. if (translated_sid->Use == SidTypeWellKnownGroup && ! well_known_sid(*sid)) {
  92. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  93. ret = 8;
  94. }
  95. LsaFreeMemory(translated_domains);
  96. LsaFreeMemory(translated_sid);
  97. return ret;
  98. }
  99. int username_sid(const TCHAR *username, SID **sid) {
  100. return username_sid(username, sid, 0);
  101. }
  102. /* Do two usernames map to the same SID? */
  103. int username_equiv(const TCHAR *a, const TCHAR *b) {
  104. SID *sid_a, *sid_b;
  105. if (username_sid(a, &sid_a)) return 0;
  106. if (username_sid(b, &sid_b)) {
  107. FreeSid(sid_a);
  108. return 0;
  109. }
  110. int ret = 0;
  111. if (EqualSid(sid_a, sid_b)) ret = 1;
  112. FreeSid(sid_a);
  113. FreeSid(sid_b);
  114. return ret;
  115. }
  116. /* Does the username represent the LocalSystem account? */
  117. int is_localsystem(const TCHAR *username) {
  118. if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 1;
  119. if (! imports.IsWellKnownSid) return 0;
  120. SID *sid;
  121. if (username_sid(username, &sid)) return 0;
  122. int ret = 0;
  123. if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) ret = 1;
  124. FreeSid(sid);
  125. return ret;
  126. }
  127. /*
  128. Get well-known alias for LocalSystem and friends.
  129. Returns a pointer to a static string. DO NOT try to free it.
  130. */
  131. const TCHAR *well_known_sid(SID *sid) {
  132. if (! imports.IsWellKnownSid) return 0;
  133. if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) return NSSM_LOCALSYSTEM_ACCOUNT;
  134. if (imports.IsWellKnownSid(sid, WinLocalServiceSid)) return NSSM_LOCALSERVICE_ACCOUNT;
  135. if (imports.IsWellKnownSid(sid, WinNetworkServiceSid)) return NSSM_NETWORKSERVICE_ACCOUNT;
  136. return 0;
  137. }
  138. const TCHAR *well_known_username(const TCHAR *username) {
  139. if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return NSSM_LOCALSYSTEM_ACCOUNT;
  140. SID *sid;
  141. int r = username_sid(username, &sid);
  142. if (username_sid(username, &sid)) return 0;
  143. const TCHAR *well_known = well_known_sid(sid);
  144. FreeSid(sid);
  145. return well_known;
  146. }
  147. int grant_logon_as_service(const TCHAR *username) {
  148. if (! username) return 0;
  149. /* Open Policy object. */
  150. LSA_OBJECT_ATTRIBUTES attributes;
  151. ZeroMemory(&attributes, sizeof(attributes));
  152. LSA_HANDLE policy;
  153. NTSTATUS status;
  154. if (open_lsa_policy(&policy)) return 1;
  155. /* Look up SID for the account. */
  156. SID *sid;
  157. if (username_sid(username, &sid, &policy)) {
  158. LsaClose(policy);
  159. return 2;
  160. }
  161. /*
  162. Shouldn't happen because it should have been checked before callling this function.
  163. */
  164. if (well_known_sid(sid)) {
  165. LsaClose(policy);
  166. return 3;
  167. }
  168. /* Check if the SID has the "Log on as a service" right. */
  169. LSA_UNICODE_STRING lsa_right;
  170. lsa_right.Buffer = NSSM_LOGON_AS_SERVICE_RIGHT;
  171. lsa_right.Length = (unsigned short) wcslen(lsa_right.Buffer) * sizeof(wchar_t);
  172. lsa_right.MaximumLength = lsa_right.Length + sizeof(wchar_t);
  173. LSA_UNICODE_STRING *rights;
  174. unsigned long count = ~0;
  175. status = LsaEnumerateAccountRights(policy, sid, &rights, &count);
  176. if (status) {
  177. /*
  178. If the account has no rights set LsaEnumerateAccountRights() will return
  179. STATUS_OBJECT_NAME_NOT_FOUND and set count to 0.
  180. */
  181. unsigned long error = LsaNtStatusToWinError(status);
  182. if (error != ERROR_FILE_NOT_FOUND) {
  183. FreeSid(sid);
  184. LsaClose(policy);
  185. print_message(stderr, NSSM_MESSAGE_LSAENUMERATEACCOUNTRIGHTS_FAILED, username, error_string(error));
  186. return 4;
  187. }
  188. }
  189. for (unsigned long i = 0; i < count; i++) {
  190. if (rights[i].Length != lsa_right.Length) continue;
  191. if (_wcsnicmp(rights[i].Buffer, lsa_right.Buffer, lsa_right.MaximumLength)) continue;
  192. /* The SID has the right. */
  193. FreeSid(sid);
  194. LsaFreeMemory(rights);
  195. LsaClose(policy);
  196. return 0;
  197. }
  198. LsaFreeMemory(rights);
  199. /* Add the right. */
  200. status = LsaAddAccountRights(policy, sid, &lsa_right, 1);
  201. FreeSid(sid);
  202. LsaClose(policy);
  203. if (status) {
  204. print_message(stderr, NSSM_MESSAGE_LSAADDACCOUNTRIGHTS_FAILED, error_string(LsaNtStatusToWinError(status)));
  205. return 5;
  206. }
  207. print_message(stdout, NSSM_MESSAGE_GRANTED_LOGON_AS_SERVICE, username);
  208. return 0;
  209. }