nssm.cpp 5.3 KB

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