nssm.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #include "nssm.h"
  2. extern unsigned long tls_index;
  3. extern bool is_admin;
  4. extern imports_t imports;
  5. /* Are two strings case-insensitively equivalent? */
  6. int str_equiv(const TCHAR *a, const TCHAR *b) {
  7. size_t len = _tcslen(a);
  8. if (_tcslen(b) != len) return 0;
  9. if (_tcsnicmp(a, b, len)) return 0;
  10. return 1;
  11. }
  12. /* Convert a string to a number. */
  13. int str_number(const TCHAR *string, unsigned long *number, TCHAR **bogus) {
  14. if (! string) return 1;
  15. *number = _tcstoul(string, bogus, 0);
  16. if (**bogus) return 2;
  17. return 0;
  18. }
  19. int str_number(const TCHAR *string, unsigned long *number) {
  20. TCHAR *bogus;
  21. return str_number(string, number, &bogus);
  22. }
  23. /* Remove basename of a path. */
  24. void strip_basename(TCHAR *buffer) {
  25. size_t len = _tcslen(buffer);
  26. size_t i;
  27. for (i = len; i && buffer[i] != _T('\\') && buffer[i] != _T('/'); i--);
  28. /* X:\ is OK. */
  29. if (i && buffer[i - 1] == _T(':')) i++;
  30. buffer[i] = _T('\0');
  31. }
  32. /* How to use me correctly */
  33. int usage(int ret) {
  34. if (GetConsoleWindow()) print_message(stderr, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
  35. else popup_message(0, MB_OK, NSSM_MESSAGE_USAGE, NSSM_VERSION, NSSM_CONFIGURATION, NSSM_DATE);
  36. return(ret);
  37. }
  38. void check_admin() {
  39. is_admin = false;
  40. /* Lifted from MSDN examples */
  41. PSID AdministratorsGroup;
  42. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  43. if (! AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup)) return;
  44. CheckTokenMembership(0, AdministratorsGroup, /*XXX*/(PBOOL) &is_admin);
  45. FreeSid(AdministratorsGroup);
  46. }
  47. /* See if we were launched from a console window. */
  48. static void check_console() {
  49. /* If we're running in a service context there will be no console window. */
  50. HWND console = GetConsoleWindow();
  51. if (! console) return;
  52. unsigned long pid;
  53. if (! GetWindowThreadProcessId(console, &pid)) return;
  54. /*
  55. If the process associated with the console window handle is the same as
  56. this process, we were not launched from an existing console. The user
  57. probably double-clicked our executable.
  58. */
  59. if (GetCurrentProcessId() != pid) return;
  60. /* We close our new console so that subsequent messages appear in a popup. */
  61. FreeConsole();
  62. }
  63. int _tmain(int argc, TCHAR **argv) {
  64. check_console();
  65. /* Remember if we are admin */
  66. check_admin();
  67. /* Elevate */
  68. if (argc > 1) {
  69. /*
  70. Valid commands are:
  71. start, stop, pause, continue, install, edit, get, set, reset, unset, remove
  72. */
  73. if (str_equiv(argv[1], _T("start"))) exit(control_service(0, argc - 2, argv + 2));
  74. if (str_equiv(argv[1], _T("stop"))) exit(control_service(SERVICE_CONTROL_STOP, argc - 2, argv + 2));
  75. if (str_equiv(argv[1], _T("pause"))) exit(control_service(SERVICE_CONTROL_PAUSE, argc - 2, argv + 2));
  76. if (str_equiv(argv[1], _T("continue"))) exit(control_service(SERVICE_CONTROL_CONTINUE, argc - 2, argv + 2));
  77. if (str_equiv(argv[1], _T("status"))) exit(control_service(SERVICE_CONTROL_INTERROGATE, argc - 2, argv + 2));
  78. if (str_equiv(argv[1], _T("install"))) {
  79. if (! is_admin) {
  80. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_INSTALL);
  81. exit(100);
  82. }
  83. exit(pre_install_service(argc - 2, argv + 2));
  84. }
  85. if (str_equiv(argv[1], _T("edit")) || str_equiv(argv[1], _T("get")) || str_equiv(argv[1], _T("set")) || str_equiv(argv[1], _T("reset")) || str_equiv(argv[1], _T("unset"))) {
  86. if (! is_admin) {
  87. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_EDIT);
  88. exit(100);
  89. }
  90. int ret = pre_edit_service(argc - 1, argv + 1);
  91. /* There might be a password here. */
  92. for (int i = 0; i < argc; i++) SecureZeroMemory(argv[i], _tcslen(argv[i]) * sizeof(TCHAR));
  93. exit(ret);
  94. }
  95. if (str_equiv(argv[1], _T("remove"))) {
  96. if (! is_admin) {
  97. print_message(stderr, NSSM_MESSAGE_NOT_ADMINISTRATOR_CANNOT_REMOVE);
  98. exit(100);
  99. }
  100. exit(pre_remove_service(argc - 2, argv + 2));
  101. }
  102. }
  103. /* Thread local storage for error message buffer */
  104. tls_index = TlsAlloc();
  105. /* Register messages */
  106. if (is_admin) create_messages();
  107. /*
  108. Optimisation for Windows 2000:
  109. When we're run from the command line the StartServiceCtrlDispatcher() call
  110. will time out after a few seconds on Windows 2000. On newer versions the
  111. call returns instantly. Check for stdin first and only try to call the
  112. function if there's no input stream found. Although it's possible that
  113. we're running with input redirected it's much more likely that we're
  114. actually running as a service.
  115. This will save time when running with no arguments from a command prompt.
  116. */
  117. if (_fileno(stdin) < 0) {
  118. /* Set up function pointers. */
  119. if (get_imports()) exit(111);
  120. /* Start service magic */
  121. SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
  122. if (! StartServiceCtrlDispatcher(table)) {
  123. unsigned long error = GetLastError();
  124. /* User probably ran nssm with no argument */
  125. if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
  126. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
  127. free_imports();
  128. exit(100);
  129. }
  130. }
  131. else exit(usage(1));
  132. /* And nothing more to do */
  133. exit(0);
  134. }