nssm.cpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #include "nssm.h"
  2. extern unsigned long tls_index;
  3. /* String function */
  4. int str_equiv(const char *a, const char *b) {
  5. int i;
  6. for (i = 0; ; i++) {
  7. if (tolower(b[i]) != tolower(a[i])) return 0;
  8. if (! a[i]) return 1;
  9. }
  10. }
  11. /* How to use me correctly */
  12. int usage(int ret) {
  13. fprintf(stderr, "NSSM: The non-sucking service manager\n");
  14. fprintf(stderr, "Version %s, %s\n", NSSM_VERSION, NSSM_DATE);
  15. fprintf(stderr, "Usage: nssm <option> [args]\n\n");
  16. fprintf(stderr, "To show service installation GUI:\n\n");
  17. fprintf(stderr, " nssm install [<servicename>]\n\n");
  18. fprintf(stderr, "To install a service without confirmation:\n\n");
  19. fprintf(stderr, " nssm install <servicename> <app> [<args>]\n\n");
  20. fprintf(stderr, "To show service removal GUI:\n\n");
  21. fprintf(stderr, " nssm remove [<servicename>]\n\n");
  22. fprintf(stderr, "To remove a service without confirmation:\n\n");
  23. fprintf(stderr, " nssm remove <servicename> confirm\n");
  24. return(ret);
  25. }
  26. int check_admin(char *action) {
  27. /* Lifted from MSDN examples */
  28. PSID AdministratorsGroup;
  29. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
  30. BOOL ok = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup);
  31. if (ok) {
  32. if (! CheckTokenMembership(0, AdministratorsGroup, &ok)) ok = 0;
  33. FreeSid(AdministratorsGroup);
  34. if (ok) return 0;
  35. fprintf(stderr, "Administator access is needed to %s a service.\n", action);
  36. return 1;
  37. }
  38. /* Can't tell if we are admin or not; later operations may fail */
  39. return 0;
  40. }
  41. int main(int argc, char **argv) {
  42. /* Elevate */
  43. if (argc > 1) {
  44. if (str_equiv(argv[1], "install") || str_equiv(argv[1], "remove")) {
  45. if (check_admin(argv[1])) exit(100);
  46. }
  47. /* Valid commands are install or remove */
  48. if (str_equiv(argv[1], "install")) {
  49. exit(pre_install_service(argc - 2, argv + 2));
  50. }
  51. if (str_equiv(argv[1], "remove")) {
  52. exit(pre_remove_service(argc - 2, argv + 2));
  53. }
  54. }
  55. /* Thread local storage for error message buffer */
  56. tls_index = TlsAlloc();
  57. /* Register messages */
  58. create_messages();
  59. /* Start service magic */
  60. SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } };
  61. if (! StartServiceCtrlDispatcher(table)) {
  62. unsigned long error = GetLastError();
  63. /* User probably ran nssm with no argument */
  64. if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1));
  65. log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0);
  66. exit(100);
  67. }
  68. /* And nothing more to do */
  69. exit(0);
  70. }