account.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. /* Build the virtual account name. */
  198. TCHAR *virtual_account(const TCHAR *service_name) {
  199. size_t len = _tcslen(NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN) + _tcslen(service_name) + 2;
  200. TCHAR *name = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  201. if (! name) {
  202. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("name"), _T("virtual_account"));
  203. return 0;
  204. }
  205. _sntprintf_s(name, len, _TRUNCATE, _T("%s\\%s"), NSSM_VIRTUAL_SERVICE_ACCOUNT_DOMAIN, service_name);
  206. return name;
  207. }
  208. /* Does the username represent a virtual account for the service? */
  209. int is_virtual_account(const TCHAR *service_name, const TCHAR *username) {
  210. if (! imports.IsWellKnownSid) return 0;
  211. if (! service_name) return 0;
  212. if (! username) return 0;
  213. TCHAR *canon = virtual_account(service_name);
  214. int ret = str_equiv(canon, username);
  215. HeapFree(GetProcessHeap(), 0, canon);
  216. return ret;
  217. }
  218. /*
  219. Get well-known alias for LocalSystem and friends.
  220. Returns a pointer to a static string. DO NOT try to free it.
  221. */
  222. const TCHAR *well_known_sid(SID *sid) {
  223. if (! imports.IsWellKnownSid) return 0;
  224. if (imports.IsWellKnownSid(sid, WinLocalSystemSid)) return NSSM_LOCALSYSTEM_ACCOUNT;
  225. if (imports.IsWellKnownSid(sid, WinLocalServiceSid)) return NSSM_LOCALSERVICE_ACCOUNT;
  226. if (imports.IsWellKnownSid(sid, WinNetworkServiceSid)) return NSSM_NETWORKSERVICE_ACCOUNT;
  227. return 0;
  228. }
  229. const TCHAR *well_known_username(const TCHAR *username) {
  230. if (! username) return NSSM_LOCALSYSTEM_ACCOUNT;
  231. if (str_equiv(username, NSSM_LOCALSYSTEM_ACCOUNT)) return NSSM_LOCALSYSTEM_ACCOUNT;
  232. SID *sid;
  233. if (username_sid(username, &sid)) return 0;
  234. const TCHAR *well_known = well_known_sid(sid);
  235. FreeSid(sid);
  236. return well_known;
  237. }
  238. int grant_logon_as_service(const TCHAR *username) {
  239. if (! username) return 0;
  240. /* Open Policy object. */
  241. LSA_OBJECT_ATTRIBUTES attributes;
  242. ZeroMemory(&attributes, sizeof(attributes));
  243. LSA_HANDLE policy;
  244. NTSTATUS status;
  245. if (open_lsa_policy(&policy)) return 1;
  246. /* Look up SID for the account. */
  247. SID *sid;
  248. if (username_sid(username, &sid, &policy)) {
  249. LsaClose(policy);
  250. return 2;
  251. }
  252. /*
  253. Shouldn't happen because it should have been checked before callling this function.
  254. */
  255. if (well_known_sid(sid)) {
  256. LsaClose(policy);
  257. return 3;
  258. }
  259. /* Check if the SID has the "Log on as a service" right. */
  260. LSA_UNICODE_STRING lsa_right;
  261. lsa_right.Buffer = NSSM_LOGON_AS_SERVICE_RIGHT;
  262. lsa_right.Length = (unsigned short) wcslen(lsa_right.Buffer) * sizeof(wchar_t);
  263. lsa_right.MaximumLength = lsa_right.Length + sizeof(wchar_t);
  264. LSA_UNICODE_STRING *rights;
  265. unsigned long count = ~0;
  266. status = LsaEnumerateAccountRights(policy, sid, &rights, &count);
  267. if (status != STATUS_SUCCESS) {
  268. /*
  269. If the account has no rights set LsaEnumerateAccountRights() will return
  270. STATUS_OBJECT_NAME_NOT_FOUND and set count to 0.
  271. */
  272. unsigned long error = LsaNtStatusToWinError(status);
  273. if (error != ERROR_FILE_NOT_FOUND) {
  274. FreeSid(sid);
  275. LsaClose(policy);
  276. print_message(stderr, NSSM_MESSAGE_LSAENUMERATEACCOUNTRIGHTS_FAILED, username, error_string(error));
  277. return 4;
  278. }
  279. }
  280. for (unsigned long i = 0; i < count; i++) {
  281. if (rights[i].Length != lsa_right.Length) continue;
  282. if (_wcsnicmp(rights[i].Buffer, lsa_right.Buffer, lsa_right.MaximumLength)) continue;
  283. /* The SID has the right. */
  284. FreeSid(sid);
  285. LsaFreeMemory(rights);
  286. LsaClose(policy);
  287. return 0;
  288. }
  289. LsaFreeMemory(rights);
  290. /* Add the right. */
  291. status = LsaAddAccountRights(policy, sid, &lsa_right, 1);
  292. FreeSid(sid);
  293. LsaClose(policy);
  294. if (status != STATUS_SUCCESS) {
  295. print_message(stderr, NSSM_MESSAGE_LSAADDACCOUNTRIGHTS_FAILED, error_string(LsaNtStatusToWinError(status)));
  296. return 5;
  297. }
  298. print_message(stdout, NSSM_MESSAGE_GRANTED_LOGON_AS_SERVICE, username);
  299. return 0;
  300. }