account.cpp 12 KB

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