account.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. #include "nssm.h"
  2. #include <sddl.h>
  3. #ifndef STATUS_SUCCESS
  4. #define STATUS_SUCCESS ERROR_SUCCESS
  5. #endif
  6. extern imports_t imports;
  7. /* Open Policy object. */
  8. int open_lsa_policy(LSA_HANDLE *policy) {
  9. LSA_OBJECT_ATTRIBUTES attributes;
  10. ZeroMemory(&attributes, sizeof(attributes));
  11. NTSTATUS status = LsaOpenPolicy(0, &attributes, POLICY_ALL_ACCESS, policy);
  12. if (status != STATUS_SUCCESS) {
  13. print_message(stderr, NSSM_MESSAGE_LSAOPENPOLICY_FAILED, error_string(LsaNtStatusToWinError(status)));
  14. return 1;
  15. }
  16. return 0;
  17. }
  18. /* Look up SID for an account. */
  19. int username_sid(const TCHAR *username, SID **sid, LSA_HANDLE *policy) {
  20. LSA_HANDLE handle;
  21. if (! policy) {
  22. policy = &handle;
  23. if (open_lsa_policy(policy)) return 1;
  24. }
  25. /*
  26. LsaLookupNames() can't look up .\username but can look up
  27. %COMPUTERNAME%\username. ChangeServiceConfig() writes .\username to the
  28. registry when %COMPUTERNAME%\username is a passed as a parameter. We
  29. need to preserve .\username when calling ChangeServiceConfig() without
  30. changing the username, but expand to %COMPUTERNAME%\username when calling
  31. LsaLookupNames().
  32. */
  33. TCHAR *expanded;
  34. unsigned long expandedlen;
  35. if (_tcsnicmp(_T(".\\"), username, 2)) {
  36. expandedlen = (unsigned long) (_tcslen(username) + 1) * sizeof(TCHAR);
  37. expanded = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, expandedlen);
  38. if (! expanded) {
  39. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("expanded"), _T("username_sid"));
  40. if (policy == &handle) LsaClose(handle);
  41. return 2;
  42. }
  43. memmove(expanded, username, expandedlen);
  44. }
  45. else {
  46. TCHAR computername[MAX_COMPUTERNAME_LENGTH + 1];
  47. expandedlen = _countof(computername);
  48. GetComputerName(computername, &expandedlen);
  49. expandedlen += (unsigned long) _tcslen(username);
  50. expanded = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, expandedlen * sizeof(TCHAR));
  51. if (! expanded) {
  52. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("expanded"), _T("username_sid"));
  53. if (policy == &handle) LsaClose(handle);
  54. return 2;
  55. }
  56. _sntprintf_s(expanded, expandedlen, _TRUNCATE, _T("%s\\%s"), computername, username + 2);
  57. }
  58. LSA_UNICODE_STRING lsa_username;
  59. int ret = to_utf16(expanded, &lsa_username.Buffer, (unsigned long *) &lsa_username.Length);
  60. HeapFree(GetProcessHeap(), 0, expanded);
  61. if (ret) {
  62. if (policy == &handle) LsaClose(handle);
  63. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("LSA_UNICODE_STRING"), _T("username_sid()"));
  64. return 4;
  65. }
  66. lsa_username.Length *= sizeof(wchar_t);
  67. lsa_username.MaximumLength = lsa_username.Length + sizeof(wchar_t);
  68. LSA_REFERENCED_DOMAIN_LIST *translated_domains;
  69. LSA_TRANSLATED_SID *translated_sid;
  70. NTSTATUS status = LsaLookupNames(*policy, 1, &lsa_username, &translated_domains, &translated_sid);
  71. HeapFree(GetProcessHeap(), 0, lsa_username.Buffer);
  72. if (policy == &handle) LsaClose(handle);
  73. if (status != STATUS_SUCCESS) {
  74. LsaFreeMemory(translated_domains);
  75. LsaFreeMemory(translated_sid);
  76. print_message(stderr, NSSM_MESSAGE_LSALOOKUPNAMES_FAILED, username, error_string(LsaNtStatusToWinError(status)));
  77. return 5;
  78. }
  79. if (translated_sid->Use != SidTypeUser && translated_sid->Use != SidTypeWellKnownGroup) {
  80. if (translated_sid->Use != SidTypeUnknown || _tcsnicmp(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN _T("\\"), username, _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + 1)) {
  81. LsaFreeMemory(translated_domains);
  82. LsaFreeMemory(translated_sid);
  83. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  84. return 6;
  85. }
  86. }
  87. LSA_TRUST_INFORMATION *trust = &translated_domains->Domains[translated_sid->DomainIndex];
  88. if (! trust || ! IsValidSid(trust->Sid)) {
  89. LsaFreeMemory(translated_domains);
  90. LsaFreeMemory(translated_sid);
  91. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  92. return 7;
  93. }
  94. /* GetSidSubAuthority*() return pointers! */
  95. unsigned char *n = GetSidSubAuthorityCount(trust->Sid);
  96. /* Convert translated SID to SID. */
  97. *sid = (SID *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, GetSidLengthRequired(*n + 1));
  98. if (! *sid) {
  99. LsaFreeMemory(translated_domains);
  100. LsaFreeMemory(translated_sid);
  101. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("SID"), _T("username_sid"));
  102. return 8;
  103. }
  104. unsigned long error;
  105. if (! InitializeSid(*sid, GetSidIdentifierAuthority(trust->Sid), *n + 1)) {
  106. error = GetLastError();
  107. HeapFree(GetProcessHeap(), 0, *sid);
  108. LsaFreeMemory(translated_domains);
  109. LsaFreeMemory(translated_sid);
  110. print_message(stderr, NSSM_MESSAGE_INITIALIZESID_FAILED, username, error_string(error));
  111. return 9;
  112. }
  113. for (unsigned char i = 0; i <= *n; i++) {
  114. unsigned long *sub = GetSidSubAuthority(*sid, i);
  115. if (i < *n) *sub = *GetSidSubAuthority(trust->Sid, i);
  116. else *sub = translated_sid->RelativeId;
  117. }
  118. ret = 0;
  119. if (translated_sid->Use == SidTypeWellKnownGroup && ! well_known_sid(*sid)) {
  120. print_message(stderr, NSSM_GUI_INVALID_USERNAME, username);
  121. ret = 10;
  122. }
  123. LsaFreeMemory(translated_domains);
  124. LsaFreeMemory(translated_sid);
  125. return ret;
  126. }
  127. int username_sid(const TCHAR *username, SID **sid) {
  128. return username_sid(username, sid, 0);
  129. }
  130. int canonicalise_username(const TCHAR *username, TCHAR **canon) {
  131. LSA_HANDLE policy;
  132. if (open_lsa_policy(&policy)) return 1;
  133. SID *sid;
  134. if (username_sid(username, &sid, &policy)) return 2;
  135. PSID sids = { sid };
  136. LSA_REFERENCED_DOMAIN_LIST *translated_domains;
  137. LSA_TRANSLATED_NAME *translated_name;
  138. NTSTATUS status = LsaLookupSids(policy, 1, &sids, &translated_domains, &translated_name);
  139. if (status != STATUS_SUCCESS) {
  140. LsaFreeMemory(translated_domains);
  141. LsaFreeMemory(translated_name);
  142. print_message(stderr, NSSM_MESSAGE_LSALOOKUPSIDS_FAILED, error_string(LsaNtStatusToWinError(status)));
  143. return 3;
  144. }
  145. LSA_TRUST_INFORMATION *trust = &translated_domains->Domains[translated_name->DomainIndex];
  146. LSA_UNICODE_STRING lsa_canon;
  147. lsa_canon.Length = translated_name->Name.Length + trust->Name.Length + sizeof(wchar_t);
  148. lsa_canon.MaximumLength = lsa_canon.Length + sizeof(wchar_t);
  149. lsa_canon.Buffer = (wchar_t *) HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, lsa_canon.MaximumLength);
  150. if (! lsa_canon.Buffer) {
  151. LsaFreeMemory(translated_domains);
  152. LsaFreeMemory(translated_name);
  153. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("lsa_canon"), _T("username_sid"));
  154. return 9;
  155. }
  156. /* Buffer is wchar_t but Length is in bytes. */
  157. memmove((char *) lsa_canon.Buffer, trust->Name.Buffer, trust->Name.Length);
  158. memmove((char *) lsa_canon.Buffer + trust->Name.Length, L"\\", sizeof(wchar_t));
  159. memmove((char *) lsa_canon.Buffer + trust->Name.Length + sizeof(wchar_t), translated_name->Name.Buffer, translated_name->Name.Length);
  160. unsigned long canonlen;
  161. if (from_utf16(lsa_canon.Buffer, canon, &canonlen)) {
  162. LsaFreeMemory(translated_domains);
  163. LsaFreeMemory(translated_name);
  164. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("username_sid"));
  165. return 10;
  166. }
  167. HeapFree(GetProcessHeap(), 0, lsa_canon.Buffer);
  168. LsaFreeMemory(translated_domains);
  169. LsaFreeMemory(translated_name);
  170. return 0;
  171. }
  172. /* Do two usernames map to the same SID? */
  173. int username_equiv(const TCHAR *a, const TCHAR *b) {
  174. SID *sid_a, *sid_b;
  175. if (username_sid(a, &sid_a)) return 0;
  176. if (username_sid(b, &sid_b)) {
  177. FreeSid(sid_a);
  178. return 0;
  179. }
  180. int ret = 0;
  181. if (EqualSid(sid_a, sid_b)) ret = 1;
  182. FreeSid(sid_a);
  183. FreeSid(sid_b);
  184. return ret;
  185. }
  186. /* Does the username represent the LocalSystem account? */
  187. int is_localsystem(const TCHAR *username) {
  188. if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return 1;
  189. if (! imports.IsWellKnownSid) return 0;
  190. SID *sid;
  191. if (username_sid(username, &sid)) return 0;
  192. int ret = 0;
  193. if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) ret = 1;
  194. FreeSid(sid);
  195. return ret;
  196. }
  197. /* Does the username represent a virtual account for the service? */
  198. int is_virtual_account(const TCHAR *service_name, const TCHAR *username) {
  199. if (! imports.IsWellKnownSid) return 0;
  200. if (! service_name) return 0;
  201. if (! username) return 0;
  202. size_t len = _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + _tcslen(service_name) + 2;
  203. TCHAR *canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  204. if (! canon) {
  205. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("is_virtual_account"));
  206. return 0;
  207. }
  208. _sntprintf_s(canon, len, _TRUNCATE, _T("%s\\%s"), NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, service_name);
  209. int ret = str_equiv(canon, username);
  210. HeapFree(GetProcessHeap(), 0, canon);
  211. return ret;
  212. }
  213. /*
  214. Get well-known alias for LocalSystem and friends.
  215. Returns a pointer to a static string. DO NOT try to free it.
  216. */
  217. const TCHAR *well_known_sid(SID *sid) {
  218. if (! imports.IsWellKnownSid) return 0;
  219. if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) return NSSM_LOCALSYSTEM_ACCOUNT;
  220. if (imports.IsWellKnownSid(sid, WinLocalServiceSid)) return NSSM_LOCALSERVICE_ACCOUNT;
  221. if (imports.IsWellKnownSid(sid, WinNetworkServiceSid)) return NSSM_NETWORKSERVICE_ACCOUNT;
  222. return 0;
  223. }
  224. const TCHAR *well_known_username(const TCHAR *username) {
  225. if (! username) return NSSM_LOCALSYSTEM_ACCOUNT;
  226. if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return NSSM_LOCALSYSTEM_ACCOUNT;
  227. SID *sid;
  228. if (username_sid(username, &sid)) return 0;
  229. const TCHAR *well_known = well_known_sid(sid);
  230. FreeSid(sid);
  231. return well_known;
  232. }
  233. int grant_logon_as_service(const TCHAR *username) {
  234. if (! username) return 0;
  235. /* Open Policy object. */
  236. LSA_OBJECT_ATTRIBUTES attributes;
  237. ZeroMemory(&attributes, sizeof(attributes));
  238. LSA_HANDLE policy;
  239. NTSTATUS status;
  240. if (open_lsa_policy(&policy)) return 1;
  241. /* Look up SID for the account. */
  242. SID *sid;
  243. if (username_sid(username, &sid, &policy)) {
  244. LsaClose(policy);
  245. return 2;
  246. }
  247. /*
  248. Shouldn't happen because it should have been checked before callling this function.
  249. */
  250. if (well_known_sid(sid)) {
  251. LsaClose(policy);
  252. return 3;
  253. }
  254. /* Check if the SID has the "Log on as a service" right. */
  255. LSA_UNICODE_STRING lsa_right;
  256. lsa_right.Buffer = NSSM_LOGON_AS_SERVICE_RIGHT;
  257. lsa_right.Length = (unsigned short) wcslen(lsa_right.Buffer) * sizeof(wchar_t);
  258. lsa_right.MaximumLength = lsa_right.Length + sizeof(wchar_t);
  259. LSA_UNICODE_STRING *rights;
  260. unsigned long count = ~0;
  261. status = LsaEnumerateAccountRights(policy, sid, &rights, &count);
  262. if (status != STATUS_SUCCESS) {
  263. /*
  264. If the account has no rights set LsaEnumerateAccountRights() will return
  265. STATUS_OBJECT_NAME_NOT_FOUND and set count to 0.
  266. */
  267. unsigned long error = LsaNtStatusToWinError(status);
  268. if (error != ERROR_FILE_NOT_FOUND) {
  269. FreeSid(sid);
  270. LsaClose(policy);
  271. print_message(stderr, NSSM_MESSAGE_LSAENUMERATEACCOUNTRIGHTS_FAILED, username, error_string(error));
  272. return 4;
  273. }
  274. }
  275. for (unsigned long i = 0; i < count; i++) {
  276. if (rights[i].Length != lsa_right.Length) continue;
  277. if (_wcsnicmp(rights[i].Buffer, lsa_right.Buffer, lsa_right.MaximumLength)) continue;
  278. /* The SID has the right. */
  279. FreeSid(sid);
  280. LsaFreeMemory(rights);
  281. LsaClose(policy);
  282. return 0;
  283. }
  284. LsaFreeMemory(rights);
  285. /* Add the right. */
  286. status = LsaAddAccountRights(policy, sid, &lsa_right, 1);
  287. FreeSid(sid);
  288. LsaClose(policy);
  289. if (status != STATUS_SUCCESS) {
  290. print_message(stderr, NSSM_MESSAGE_LSAADDACCOUNTRIGHTS_FAILED, error_string(LsaNtStatusToWinError(status)));
  291. return 5;
  292. }
  293. print_message(stdout, NSSM_MESSAGE_GRANTED_LOGON_AS_SERVICE, username);
  294. return 0;
  295. }