settings.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. #include "nssm.h"
  2. /* XXX: (value && value->string) is probably bogus because value is probably never null */
  3. /* Affinity. */
  4. #define NSSM_AFFINITY_ALL _T("All")
  5. extern const TCHAR *exit_action_strings[];
  6. extern const TCHAR *startup_strings[];
  7. extern const TCHAR *priority_strings[];
  8. /* Does the parameter refer to the default value of the setting? */
  9. static inline int is_default(const TCHAR *value) {
  10. return (str_equiv(value, _T("default")) || str_equiv(value, _T("*")) || ! value[0]);
  11. }
  12. static int value_from_string(const TCHAR *name, value_t *value, const TCHAR *string) {
  13. size_t len = _tcslen(string);
  14. if (! len++) {
  15. value->string = 0;
  16. return 0;
  17. }
  18. value->string = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, len * sizeof(TCHAR));
  19. if (! value->string) {
  20. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  21. return -1;
  22. }
  23. if (_sntprintf_s(value->string, len, _TRUNCATE, _T("%s"), string) < 0) {
  24. HeapFree(GetProcessHeap(), 0, value->string);
  25. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, name, _T("value_from_string()"));
  26. return -1;
  27. }
  28. return 1;
  29. }
  30. /* Functions to manage NSSM-specific settings in the registry. */
  31. static int setting_set_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  32. HKEY key = (HKEY) param;
  33. if (! key) return -1;
  34. unsigned long number;
  35. long error;
  36. /* Resetting to default? */
  37. if (! value || ! value->string) {
  38. error = RegDeleteValue(key, name);
  39. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  40. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  41. return -1;
  42. }
  43. if (str_number(value->string, &number)) return -1;
  44. if (default_value && number == (unsigned long) default_value) {
  45. error = RegDeleteValue(key, name);
  46. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  47. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  48. return -1;
  49. }
  50. if (set_number(key, (TCHAR *) name, number)) return -1;
  51. return 1;
  52. }
  53. static int setting_get_number(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  54. HKEY key = (HKEY) param;
  55. return get_number(key, (TCHAR *) name, &value->numeric, false);
  56. }
  57. static int setting_set_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  58. HKEY key = (HKEY) param;
  59. if (! key) return -1;
  60. long error;
  61. /* Resetting to default? */
  62. if (! value || ! value->string) {
  63. if (default_value) value->string = (TCHAR *) default_value;
  64. else {
  65. error = RegDeleteValue(key, name);
  66. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  67. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  68. return -1;
  69. }
  70. }
  71. if (default_value && _tcslen((TCHAR *) default_value) && str_equiv(value->string, (TCHAR *) default_value)) {
  72. error = RegDeleteValue(key, name);
  73. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  74. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  75. return -1;
  76. }
  77. if (set_expand_string(key, (TCHAR *) name, value->string)) return -1;
  78. return 1;
  79. }
  80. static int setting_get_string(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  81. HKEY key = (HKEY) param;
  82. TCHAR buffer[VALUE_LENGTH];
  83. if (get_string(key, (TCHAR *) name, (TCHAR *) buffer, (unsigned long) sizeof(buffer), false, false, false)) return -1;
  84. return value_from_string(name, value, buffer);
  85. }
  86. static int setting_set_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  87. unsigned long exitcode;
  88. TCHAR *code;
  89. TCHAR action_string[ACTION_LEN];
  90. if (additional) {
  91. /* Default action? */
  92. if (is_default(additional)) code = 0;
  93. else {
  94. if (str_number(additional, &exitcode)) return -1;
  95. code = (TCHAR *) additional;
  96. }
  97. }
  98. HKEY key = open_registry(service_name, name, KEY_WRITE);
  99. if (! key) return -1;
  100. long error;
  101. int ret = 1;
  102. /* Resetting to default? */
  103. if (value && value->string) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), value->string);
  104. else {
  105. if (code) {
  106. /* Delete explicit action. */
  107. error = RegDeleteValue(key, code);
  108. RegCloseKey(key);
  109. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  110. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, code, service_name, error_string(error));
  111. return -1;
  112. }
  113. else {
  114. /* Explicitly keep the default action. */
  115. if (default_value) _sntprintf_s(action_string, _countof(action_string), _TRUNCATE, _T("%s"), (TCHAR *) default_value);
  116. ret = 0;
  117. }
  118. }
  119. /* Validate the string. */
  120. for (int i = 0; exit_action_strings[i]; i++) {
  121. if (! _tcsnicmp((const TCHAR *) action_string, exit_action_strings[i], ACTION_LEN)) {
  122. if (default_value && str_equiv(action_string, (TCHAR *) default_value)) ret = 0;
  123. if (RegSetValueEx(key, code, 0, REG_SZ, (const unsigned char *) exit_action_strings[i], (unsigned long) (_tcslen(action_string) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  124. print_message(stderr, NSSM_MESSAGE_SETVALUE_FAILED, code, service_name, error_string(GetLastError()));
  125. RegCloseKey(key);
  126. return -1;
  127. }
  128. RegCloseKey(key);
  129. return ret;
  130. }
  131. }
  132. print_message(stderr, NSSM_MESSAGE_INVALID_EXIT_ACTION, action_string);
  133. for (int i = 0; exit_action_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), exit_action_strings[i]);
  134. return -1;
  135. }
  136. static int setting_get_exit_action(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  137. unsigned long exitcode = 0;
  138. unsigned long *code = 0;
  139. if (additional) {
  140. if (! is_default(additional)) {
  141. if (str_number(additional, &exitcode)) return -1;
  142. code = &exitcode;
  143. }
  144. }
  145. TCHAR action_string[ACTION_LEN];
  146. bool default_action;
  147. if (get_exit_action(service_name, code, action_string, &default_action)) return -1;
  148. value_from_string(name, value, action_string);
  149. if (default_action && ! _tcsnicmp((const TCHAR *) action_string, (TCHAR *) default_value, ACTION_LEN)) return 0;
  150. return 1;
  151. }
  152. static int setting_set_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  153. HKEY key = (HKEY) param;
  154. if (! key) return -1;
  155. long error;
  156. __int64 mask;
  157. __int64 system_affinity = 0LL;
  158. if (value && value->string) {
  159. DWORD_PTR affinity;
  160. if (! GetProcessAffinityMask(GetCurrentProcess(), &affinity, (DWORD_PTR *) &system_affinity)) system_affinity = ~0;
  161. if (is_default(value->string) || str_equiv(value->string, NSSM_AFFINITY_ALL)) mask = 0LL;
  162. else if (affinity_string_to_mask(value->string, &mask)) {
  163. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, value->string, num_cpus() - 1);
  164. return -1;
  165. }
  166. }
  167. else mask = 0LL;
  168. if (! mask) {
  169. error = RegDeleteValue(key, name);
  170. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  171. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  172. return -1;
  173. }
  174. /* Canonicalise. */
  175. TCHAR *canon = 0;
  176. if (affinity_mask_to_string(mask, &canon)) canon = value->string;
  177. __int64 effective_affinity = mask & system_affinity;
  178. if (effective_affinity != mask) {
  179. /* Requested CPUs did not intersect with available CPUs? */
  180. if (! effective_affinity) mask = effective_affinity = system_affinity;
  181. TCHAR *system = 0;
  182. if (! affinity_mask_to_string(system_affinity, &system)) {
  183. TCHAR *effective = 0;
  184. if (! affinity_mask_to_string(effective_affinity, &effective)) {
  185. print_message(stderr, NSSM_MESSAGE_EFFECTIVE_AFFINITY_MASK, value->string, system, effective);
  186. HeapFree(GetProcessHeap(), 0, effective);
  187. }
  188. HeapFree(GetProcessHeap(), 0, system);
  189. }
  190. }
  191. if (RegSetValueEx(key, name, 0, REG_SZ, (const unsigned char *) canon, (unsigned long) (_tcslen(canon) + 1) * sizeof(TCHAR)) != ERROR_SUCCESS) {
  192. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  193. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, name, error_string(GetLastError()), 0);
  194. return -1;
  195. }
  196. if (canon != value->string) HeapFree(GetProcessHeap(), 0, canon);
  197. return 1;
  198. }
  199. static int setting_get_affinity(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  200. HKEY key = (HKEY) param;
  201. if (! key) return -1;
  202. unsigned long type;
  203. TCHAR *buffer = 0;
  204. unsigned long buflen = 0;
  205. int ret = RegQueryValueEx(key, name, 0, &type, 0, &buflen);
  206. if (ret == ERROR_FILE_NOT_FOUND) {
  207. if (value_from_string(name, value, NSSM_AFFINITY_ALL) == 1) return 0;
  208. return -1;
  209. }
  210. if (ret != ERROR_SUCCESS) return -1;
  211. if (type != REG_SZ) return -1;
  212. buffer = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, buflen);
  213. if (! buffer) {
  214. print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("affinity"), _T("setting_get_affinity"));
  215. return -1;
  216. }
  217. if (get_string(key, (TCHAR *) name, buffer, buflen, false, false, true)) {
  218. HeapFree(GetProcessHeap(), 0, buffer);
  219. return -1;
  220. }
  221. __int64 affinity;
  222. if (affinity_string_to_mask(buffer, &affinity)) {
  223. print_message(stderr, NSSM_MESSAGE_BOGUS_AFFINITY_MASK, buffer, num_cpus() - 1);
  224. HeapFree(GetProcessHeap(), 0, buffer);
  225. return -1;
  226. }
  227. HeapFree(GetProcessHeap(), 0, buffer);
  228. /* Canonicalise. */
  229. if (affinity_mask_to_string(affinity, &buffer)) {
  230. if (buffer) HeapFree(GetProcessHeap(), 0, buffer);
  231. return -1;
  232. }
  233. ret = value_from_string(name, value, buffer);
  234. HeapFree(GetProcessHeap(), 0, buffer);
  235. return ret;
  236. }
  237. static int setting_set_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  238. HKEY key = (HKEY) param;
  239. if (! param) return -1;
  240. if (! value || ! value->string || ! value->string[0]) {
  241. long error = RegDeleteValue(key, name);
  242. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  243. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  244. return -1;
  245. }
  246. unsigned long envlen = (unsigned long) _tcslen(value->string) + 1;
  247. TCHAR *unformatted = 0;
  248. unsigned long newlen;
  249. if (unformat_environment(value->string, envlen, &unformatted, &newlen)) return -1;
  250. if (test_environment(unformatted)) {
  251. HeapFree(GetProcessHeap(), 0, unformatted);
  252. print_message(stderr, NSSM_GUI_INVALID_ENVIRONMENT);
  253. return -1;
  254. }
  255. if (RegSetValueEx(key, name, 0, REG_MULTI_SZ, (const unsigned char *) unformatted, (unsigned long) newlen * sizeof(TCHAR)) != ERROR_SUCCESS) {
  256. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  257. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SETVALUE_FAILED, NSSM_REG_ENV, error_string(GetLastError()), 0);
  258. return -1;
  259. }
  260. if (newlen) HeapFree(GetProcessHeap(), 0, unformatted);
  261. return 1;
  262. }
  263. static int setting_get_environment(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  264. HKEY key = (HKEY) param;
  265. if (! param) return -1;
  266. TCHAR *env = 0;
  267. unsigned long envlen;
  268. if (get_environment((TCHAR *) service_name, key, (TCHAR *) name, &env, &envlen)) return -1;
  269. if (! envlen) return 0;
  270. TCHAR *formatted;
  271. unsigned long newlen;
  272. if (format_environment(env, envlen, &formatted, &newlen)) return -1;
  273. int ret;
  274. if (additional) {
  275. /* Find named environment variable. */
  276. TCHAR *s;
  277. size_t len = _tcslen(additional);
  278. for (s = env; *s; s++) {
  279. /* Look for <additional>=<string> NULL NULL */
  280. if (! _tcsnicmp(s, additional, len) && s[len] == _T('=')) {
  281. /* Strip <key>= */
  282. s += len + 1;
  283. ret = value_from_string(name, value, s);
  284. HeapFree(GetProcessHeap(), 0, env);
  285. return ret;
  286. }
  287. /* Skip this string. */
  288. for ( ; *s; s++);
  289. }
  290. HeapFree(GetProcessHeap(), 0, env);
  291. return 0;
  292. }
  293. HeapFree(GetProcessHeap(), 0, env);
  294. ret = value_from_string(name, value, formatted);
  295. if (newlen) HeapFree(GetProcessHeap(), 0, formatted);
  296. return ret;
  297. }
  298. static int setting_set_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  299. HKEY key = (HKEY) param;
  300. if (! param) return -1;
  301. TCHAR *priority_string;
  302. int i;
  303. long error;
  304. if (value && value->string) priority_string = value->string;
  305. else if (default_value) priority_string = (TCHAR *) default_value;
  306. else {
  307. error = RegDeleteValue(key, name);
  308. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  309. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  310. return -1;
  311. }
  312. for (i = 0; priority_strings[i]; i++) {
  313. if (! str_equiv(priority_strings[i], priority_string)) continue;
  314. if (default_value && str_equiv(priority_string, (TCHAR *) default_value)) {
  315. error = RegDeleteValue(key, name);
  316. if (error == ERROR_SUCCESS || error == ERROR_FILE_NOT_FOUND) return 0;
  317. print_message(stderr, NSSM_MESSAGE_REGDELETEVALUE_FAILED, name, service_name, error_string(error));
  318. return -1;
  319. }
  320. if (set_number(key, (TCHAR *) name, priority_index_to_constant(i))) return -1;
  321. return 1;
  322. }
  323. print_message(stderr, NSSM_MESSAGE_INVALID_PRIORITY, priority_string);
  324. for (i = 0; priority_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), priority_strings[i]);
  325. return -1;
  326. }
  327. static int setting_get_priority(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  328. HKEY key = (HKEY) param;
  329. if (! param) return -1;
  330. unsigned long constant;
  331. switch (get_number(key, (TCHAR *) name, &constant, false)) {
  332. case 0: return value_from_string(name, value, (const TCHAR *) default_value);
  333. case -1: return -1;
  334. }
  335. return value_from_string(name, value, priority_strings[priority_constant_to_index(constant)]);
  336. }
  337. /* Functions to manage native service settings. */
  338. int native_set_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  339. SC_HANDLE service_handle = (SC_HANDLE) param;
  340. if (! service_handle) return -1;
  341. TCHAR *description = 0;
  342. if (value) description = value->string;
  343. if (set_service_description(service_name, service_handle, description)) return -1;
  344. if (description && description[0]) return 1;
  345. return 0;
  346. }
  347. int native_get_description(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  348. SC_HANDLE service_handle = (SC_HANDLE) param;
  349. if (! service_handle) return -1;
  350. TCHAR buffer[VALUE_LENGTH];
  351. if (get_service_description(service_name, service_handle, _countof(buffer), buffer)) return -1;
  352. if (buffer[0]) return value_from_string(name, value, buffer);
  353. value->string = 0;
  354. return 0;
  355. }
  356. int native_set_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  357. SC_HANDLE service_handle = (SC_HANDLE) param;
  358. if (! service_handle) return -1;
  359. TCHAR *displayname = 0;
  360. if (value && value->string) displayname = value->string;
  361. else displayname = (TCHAR *) service_name;
  362. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, displayname)) {
  363. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  364. return -1;
  365. }
  366. /*
  367. If the display name and service name differ only in case,
  368. ChangeServiceConfig() will return success but the display name will be
  369. set to the service name, NOT the value passed to the function.
  370. This appears to be a quirk of Windows rather than a bug here.
  371. */
  372. if (displayname != service_name && ! str_equiv(displayname, service_name)) return 1;
  373. return 0;
  374. }
  375. int native_get_displayname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  376. SC_HANDLE service_handle = (SC_HANDLE) param;
  377. if (! service_handle) return -1;
  378. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  379. if (! qsc) return -1;
  380. int ret = value_from_string(name, value, qsc->lpDisplayName);
  381. HeapFree(GetProcessHeap(), 0, qsc);
  382. return ret;
  383. }
  384. int native_set_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  385. SC_HANDLE service_handle = (SC_HANDLE) param;
  386. if (! service_handle) return -1;
  387. /* It makes no sense to try to reset the image path. */
  388. if (! value || ! value->string) {
  389. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  390. return -1;
  391. }
  392. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, value->string, 0, 0, 0, 0, 0, 0)) {
  393. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  394. return -1;
  395. }
  396. return 1;
  397. }
  398. int native_get_imagepath(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  399. SC_HANDLE service_handle = (SC_HANDLE) param;
  400. if (! service_handle) return -1;
  401. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  402. if (! qsc) return -1;
  403. int ret = value_from_string(name, value, qsc->lpBinaryPathName);
  404. HeapFree(GetProcessHeap(), 0, qsc);
  405. return ret;
  406. }
  407. int native_set_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  408. print_message(stderr, NSSM_MESSAGE_CANNOT_RENAME_SERVICE);
  409. return -1;
  410. }
  411. int native_get_name(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  412. return value_from_string(name, value, service_name);
  413. }
  414. int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  415. SC_HANDLE service_handle = (SC_HANDLE) param;
  416. if (! service_handle) return -1;
  417. /*
  418. Logical syntax is: nssm set <service> ObjectName <username> <password>
  419. That means the username is actually passed in the additional parameter.
  420. */
  421. bool localsystem = true;
  422. TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT;
  423. TCHAR *password = 0;
  424. if (additional) {
  425. if (! str_equiv(additional, NSSM_LOCALSYSTEM_ACCOUNT)) {
  426. localsystem = false;
  427. username = (TCHAR *) additional;
  428. if (value && value->string) password = value->string;
  429. else {
  430. /* We need a password if the account is not LOCALSYSTEM. */
  431. print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
  432. return -1;
  433. }
  434. }
  435. }
  436. /*
  437. ChangeServiceConfig() will fail to set the username if the service is set
  438. to interact with the desktop.
  439. */
  440. unsigned long type = SERVICE_NO_CHANGE;
  441. if (! localsystem) {
  442. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  443. if (! qsc) {
  444. if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
  445. return -1;
  446. }
  447. type = qsc->dwServiceType & ~SERVICE_INTERACTIVE_PROCESS;
  448. HeapFree(GetProcessHeap(), 0, qsc);
  449. }
  450. if (grant_logon_as_service(username)) {
  451. if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
  452. print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
  453. return -1;
  454. }
  455. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) {
  456. if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
  457. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  458. return -1;
  459. }
  460. if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
  461. if (localsystem) return 0;
  462. return 1;
  463. }
  464. int native_get_objectname(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  465. SC_HANDLE service_handle = (SC_HANDLE) param;
  466. if (! service_handle) return -1;
  467. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  468. if (! qsc) return -1;
  469. int ret = value_from_string(name, value, qsc->lpServiceStartName);
  470. HeapFree(GetProcessHeap(), 0, qsc);
  471. return ret;
  472. }
  473. int native_set_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  474. SC_HANDLE service_handle = (SC_HANDLE) param;
  475. if (! service_handle) return -1;
  476. /* It makes no sense to try to reset the startup type. */
  477. if (! value || ! value->string) {
  478. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  479. return -1;
  480. }
  481. /* Map NSSM_STARTUP_* constant to Windows SERVICE_*_START constant. */
  482. int service_startup = -1;
  483. int i;
  484. for (i = 0; startup_strings[i]; i++) {
  485. if (str_equiv(value->string, startup_strings[i])) {
  486. service_startup = i;
  487. break;
  488. }
  489. }
  490. if (service_startup < 0) {
  491. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_STARTUP, value->string);
  492. for (i = 0; startup_strings[i]; i++) _ftprintf(stderr, _T("%s\n"), startup_strings[i]);
  493. return -1;
  494. }
  495. unsigned long startup;
  496. switch (service_startup) {
  497. case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break;
  498. case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break;
  499. default: startup = SERVICE_AUTO_START;
  500. }
  501. if (! ChangeServiceConfig(service_handle, SERVICE_NO_CHANGE, startup, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  502. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  503. return -1;
  504. }
  505. SERVICE_DELAYED_AUTO_START_INFO delayed;
  506. ZeroMemory(&delayed, sizeof(delayed));
  507. if (service_startup == NSSM_STARTUP_DELAYED) delayed.fDelayedAutostart = 1;
  508. else delayed.fDelayedAutostart = 0;
  509. if (! ChangeServiceConfig2(service_handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) {
  510. unsigned long error = GetLastError();
  511. /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */
  512. if (error != ERROR_INVALID_LEVEL) {
  513. log_event(EVENTLOG_ERROR_TYPE, NSSM_MESSAGE_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service_name, error_string(error), 0);
  514. }
  515. }
  516. return 1;
  517. }
  518. int native_get_startup(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  519. SC_HANDLE service_handle = (SC_HANDLE) param;
  520. if (! service_handle) return -1;
  521. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  522. if (! qsc) return -1;
  523. unsigned long startup;
  524. int ret = get_service_startup(service_name, service_handle, qsc, &startup);
  525. HeapFree(GetProcessHeap(), 0, qsc);
  526. if (ret) return -1;
  527. unsigned long i;
  528. for (i = 0; startup_strings[i]; i++);
  529. if (startup >= i) return -1;
  530. return value_from_string(name, value, startup_strings[startup]);
  531. }
  532. int native_set_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  533. SC_HANDLE service_handle = (SC_HANDLE) param;
  534. if (! service_handle) return -1;
  535. /* It makes no sense to try to reset the service type. */
  536. if (! value || ! value->string) {
  537. print_message(stderr, NSSM_MESSAGE_NO_DEFAULT_VALUE, name);
  538. return -1;
  539. }
  540. /*
  541. We can only manage services of type SERVICE_WIN32_OWN_PROCESS
  542. and SERVICE_INTERACTIVE_PROCESS.
  543. */
  544. unsigned long type = SERVICE_WIN32_OWN_PROCESS;
  545. if (str_equiv(value->string, NSSM_INTERACTIVE_PROCESS)) type |= SERVICE_INTERACTIVE_PROCESS;
  546. else if (! str_equiv(value->string, NSSM_WIN32_OWN_PROCESS)) {
  547. print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE_TYPE, value->string);
  548. _ftprintf(stderr, _T("%s\n"), NSSM_WIN32_OWN_PROCESS);
  549. _ftprintf(stderr, _T("%s\n"), NSSM_INTERACTIVE_PROCESS);
  550. return -1;
  551. }
  552. /*
  553. ChangeServiceConfig() will fail if the service runs under an account
  554. other than LOCALSYSTEM and we try to make it interactive.
  555. */
  556. if (type & SERVICE_INTERACTIVE_PROCESS) {
  557. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  558. if (! qsc) return -1;
  559. if (! str_equiv(qsc->lpServiceStartName, NSSM_LOCALSYSTEM_ACCOUNT)) {
  560. HeapFree(GetProcessHeap(), 0, qsc);
  561. print_message(stderr, NSSM_MESSAGE_INTERACTIVE_NOT_LOCALSYSTEM, value->string, service_name, NSSM_LOCALSYSTEM_ACCOUNT);
  562. return -1;
  563. }
  564. HeapFree(GetProcessHeap(), 0, qsc);
  565. }
  566. if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0)) {
  567. print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
  568. return -1;
  569. }
  570. return 1;
  571. }
  572. int native_get_type(const TCHAR *service_name, void *param, const TCHAR *name, void *default_value, value_t *value, const TCHAR *additional) {
  573. SC_HANDLE service_handle = (SC_HANDLE) param;
  574. if (! service_handle) return -1;
  575. QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
  576. if (! qsc) return -1;
  577. value->numeric = qsc->dwServiceType;
  578. HeapFree(GetProcessHeap(), 0, qsc);
  579. const TCHAR *string;
  580. switch (value->numeric) {
  581. case SERVICE_KERNEL_DRIVER: string = NSSM_KERNEL_DRIVER; break;
  582. case SERVICE_FILE_SYSTEM_DRIVER: string = NSSM_FILE_SYSTEM_DRIVER; break;
  583. case SERVICE_WIN32_OWN_PROCESS: string = NSSM_WIN32_OWN_PROCESS; break;
  584. case SERVICE_WIN32_SHARE_PROCESS: string = NSSM_WIN32_SHARE_PROCESS; break;
  585. case SERVICE_WIN32_OWN_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_INTERACTIVE_PROCESS; break;
  586. case SERVICE_WIN32_SHARE_PROCESS|SERVICE_INTERACTIVE_PROCESS: string = NSSM_SHARE_INTERACTIVE_PROCESS; break;
  587. default: string = NSSM_UNKNOWN;
  588. }
  589. return value_from_string(name, value, string);
  590. }
  591. int set_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  592. if (! key) return -1;
  593. int ret;
  594. if (setting->set) ret = setting->set(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  595. else ret = -1;
  596. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  597. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  598. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  599. return ret;
  600. }
  601. int set_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  602. if (! service_handle) return -1;
  603. int ret;
  604. if (setting->set) ret = setting->set(service_name, service_handle, setting->name, setting->default_value, value, additional);
  605. else ret = -1;
  606. if (! ret) print_message(stdout, NSSM_MESSAGE_RESET_SETTING, setting->name, service_name);
  607. else if (ret > 0) print_message(stdout, NSSM_MESSAGE_SET_SETTING, setting->name, service_name);
  608. else print_message(stderr, NSSM_MESSAGE_SET_SETTING_FAILED, setting->name, service_name);
  609. return ret;
  610. }
  611. /*
  612. Returns: 1 if the value was retrieved.
  613. 0 if the default value was retrieved.
  614. -1 on error.
  615. */
  616. int get_setting(const TCHAR *service_name, HKEY key, settings_t *setting, value_t *value, const TCHAR *additional) {
  617. if (! key) return -1;
  618. int ret;
  619. switch (setting->type) {
  620. case REG_EXPAND_SZ:
  621. case REG_MULTI_SZ:
  622. case REG_SZ:
  623. value->string = (TCHAR *) setting->default_value;
  624. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  625. else ret = -1;
  626. break;
  627. case REG_DWORD:
  628. value->numeric = (unsigned long) setting->default_value;
  629. if (setting->get) ret = setting->get(service_name, (void *) key, setting->name, setting->default_value, value, additional);
  630. else ret = -1;
  631. break;
  632. default:
  633. ret = -1;
  634. break;
  635. }
  636. if (ret < 0) print_message(stderr, NSSM_MESSAGE_GET_SETTING_FAILED, setting->name, service_name);
  637. return ret;
  638. }
  639. int get_setting(const TCHAR *service_name, SC_HANDLE service_handle, settings_t *setting, value_t *value, const TCHAR *additional) {
  640. if (! service_handle) return -1;
  641. return setting->get(service_name, service_handle, setting->name, 0, value, additional);
  642. }
  643. settings_t settings[] = {
  644. { NSSM_REG_EXE, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  645. { NSSM_REG_FLAGS, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  646. { NSSM_REG_DIR, REG_EXPAND_SZ, (void *) _T(""), false, 0, setting_set_string, setting_get_string },
  647. { NSSM_REG_EXIT, REG_SZ, (void *) exit_action_strings[NSSM_EXIT_RESTART], false, ADDITIONAL_MANDATORY, setting_set_exit_action, setting_get_exit_action },
  648. { NSSM_REG_AFFINITY, REG_SZ, 0, false, 0, setting_set_affinity, setting_get_affinity },
  649. { NSSM_REG_ENV, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
  650. { NSSM_REG_ENV_EXTRA, REG_MULTI_SZ, NULL, false, ADDITIONAL_CRLF, setting_set_environment, setting_get_environment },
  651. { NSSM_REG_NO_CONSOLE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  652. { NSSM_REG_PRIORITY, REG_SZ, (void *) priority_strings[NSSM_NORMAL_PRIORITY], false, 0, setting_set_priority, setting_get_priority },
  653. { NSSM_REG_RESTART_DELAY, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  654. { NSSM_REG_STDIN, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  655. { NSSM_REG_STDIN NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDIN_SHARING, false, 0, setting_set_number, setting_get_number },
  656. { NSSM_REG_STDIN NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDIN_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  657. { NSSM_REG_STDIN NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDIN_FLAGS, false, 0, setting_set_number, setting_get_number },
  658. { NSSM_REG_STDOUT, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  659. { NSSM_REG_STDOUT NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDOUT_SHARING, false, 0, setting_set_number, setting_get_number },
  660. { NSSM_REG_STDOUT NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDOUT_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  661. { NSSM_REG_STDOUT NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDOUT_FLAGS, false, 0, setting_set_number, setting_get_number },
  662. { NSSM_REG_STDERR, REG_EXPAND_SZ, NULL, false, 0, setting_set_string, setting_get_string },
  663. { NSSM_REG_STDERR NSSM_REG_STDIO_SHARING, REG_DWORD, (void *) NSSM_STDERR_SHARING, false, 0, setting_set_number, setting_get_number },
  664. { NSSM_REG_STDERR NSSM_REG_STDIO_DISPOSITION, REG_DWORD, (void *) NSSM_STDERR_DISPOSITION, false, 0, setting_set_number, setting_get_number },
  665. { NSSM_REG_STDERR NSSM_REG_STDIO_FLAGS, REG_DWORD, (void *) NSSM_STDERR_FLAGS, false, 0, setting_set_number, setting_get_number },
  666. { NSSM_REG_STOP_METHOD_SKIP, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  667. { NSSM_REG_KILL_CONSOLE_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_CONSOLE_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  668. { NSSM_REG_KILL_WINDOW_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_WINDOW_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  669. { NSSM_REG_KILL_THREADS_GRACE_PERIOD, REG_DWORD, (void *) NSSM_KILL_THREADS_GRACE_PERIOD, false, 0, setting_set_number, setting_get_number },
  670. { NSSM_REG_THROTTLE, REG_DWORD, (void *) NSSM_RESET_THROTTLE_RESTART, false, 0, setting_set_number, setting_get_number },
  671. { NSSM_REG_ROTATE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  672. { NSSM_REG_ROTATE_ONLINE, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  673. { NSSM_REG_ROTATE_SECONDS, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  674. { NSSM_REG_ROTATE_BYTES_LOW, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  675. { NSSM_REG_ROTATE_BYTES_HIGH, REG_DWORD, 0, false, 0, setting_set_number, setting_get_number },
  676. { NSSM_NATIVE_DESCRIPTION, REG_SZ, _T(""), true, 0, native_set_description, native_get_description },
  677. { NSSM_NATIVE_DISPLAYNAME, REG_SZ, NULL, true, 0, native_set_displayname, native_get_displayname },
  678. { NSSM_NATIVE_IMAGEPATH, REG_EXPAND_SZ, NULL, true, 0, native_set_imagepath, native_get_imagepath },
  679. { NSSM_NATIVE_OBJECTNAME, REG_SZ, NSSM_LOCALSYSTEM_ACCOUNT, true, ADDITIONAL_SETTING, native_set_objectname, native_get_objectname },
  680. { NSSM_NATIVE_NAME, REG_SZ, NULL, true, 0, native_set_name, native_get_name },
  681. { NSSM_NATIVE_STARTUP, REG_SZ, NULL, true, 0, native_set_startup, native_get_startup },
  682. { NSSM_NATIVE_TYPE, REG_SZ, NULL, true, 0, native_set_type, native_get_type },
  683. { NULL, NULL, NULL, NULL, NULL }
  684. };