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