Browse Source

Strip quotes from directory name.

Windows directories aren't allowed to contain quotes so CreateProcess()
will fail if the AppDirectory is quoted.  Note that it succeeds even if
Application itself is quoted as the application plus parameters are
interpreted as a command line.
Iain Patterson 11 years ago
parent
commit
859c7ca832
4 changed files with 13 additions and 9 deletions
  1. 1 0
      nssm.h
  2. 4 4
      nssm.vcproj
  3. 7 4
      registry.cpp
  4. 1 1
      registry.h

+ 1 - 0
nssm.h

@@ -2,6 +2,7 @@
 #define NSSM_H
 
 #define _WIN32_WINNT 0x0500
+#include <shlwapi.h>
 #include <stdarg.h>
 #include <stdio.h>
 #include <windows.h>

+ 4 - 4
nssm.vcproj

@@ -76,7 +76,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"
+				AdditionalDependencies="shlwapi.lib"
 				LinkIncremental="2"
 				SuppressStartupBanner="true"
 				GenerateDebugInformation="true"
@@ -169,7 +169,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"
+				AdditionalDependencies="shlwapi.lib"
 				LinkIncremental="2"
 				SuppressStartupBanner="true"
 				GenerateDebugInformation="true"
@@ -261,7 +261,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"
+				AdditionalDependencies="shlwapi.lib"
 				LinkIncremental="1"
 				SuppressStartupBanner="true"
 				ProgramDatabaseFile=".\Release/nssm.pdb"
@@ -353,7 +353,7 @@
 			/>
 			<Tool
 				Name="VCLinkerTool"
-				OutputFile="$(OutDir)\$(ProjectName)-$(PlatformName).exe"
+				AdditionalDependencies="shlwapi.lib"
 				LinkIncremental="1"
 				SuppressStartupBanner="true"
 				ProgramDatabaseFile=".\Release/nssm.pdb"

+ 7 - 4
registry.cpp

@@ -102,7 +102,7 @@ int create_exit_action(char *service_name, const char *action_string) {
   return 0;
 }
 
-int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen) {
+int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen, bool sanitise) {
   unsigned char *buffer = (unsigned char *) HeapAlloc(GetProcessHeap(), 0, datalen);
   if (! buffer) {
     log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, value, "expand_parameter()", 0);
@@ -119,6 +119,9 @@ int expand_parameter(HKEY key, char *value, char *data, unsigned long datalen) {
     return 2;
   }
 
+  /* Paths aren't allowed to contain quotes. */
+  if (sanitise) PathUnquoteSpaces((LPSTR) buffer);
+
   ZeroMemory(data, datalen);
 
   /* Technically we shouldn't expand environment strings from REG_SZ values */
@@ -155,19 +158,19 @@ int get_parameters(char *service_name, char *exe, int exelen, char *flags, int f
   }
 
   /* Try to get executable file - MUST succeed */
-  if (expand_parameter(key, NSSM_REG_EXE, exe, exelen)) {
+  if (expand_parameter(key, NSSM_REG_EXE, exe, exelen, false)) {
     RegCloseKey(key);
     return 3;
   }
 
   /* Try to get flags - may fail and we don't care */
-  if (expand_parameter(key, NSSM_REG_FLAGS, flags, flagslen)) {
+  if (expand_parameter(key, NSSM_REG_FLAGS, flags, flagslen, false)) {
     log_event(EVENTLOG_WARNING_TYPE, NSSM_EVENT_NO_FLAGS, NSSM_REG_FLAGS, service_name, exe, 0);
     ZeroMemory(flags, flagslen);
   }
 
   /* Try to get startup directory - may fail and we fall back to a default */
-  if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen) || ! dir[0]) {
+  if (expand_parameter(key, NSSM_REG_DIR, dir, dirlen, true) || ! dir[0]) {
     /* Our buffers are defined to be long enough for this to be safe */
     size_t i;
     for (i = strlen(exe); i && exe[i] != '\\' && exe[i] != '/'; i--);

+ 1 - 1
registry.h

@@ -10,7 +10,7 @@
 int create_messages();
 int create_parameters(char *, char *, char *, char *);
 int create_exit_action(char *, const char *);
-int expand_parameter(HKEY, char *, char *, unsigned long);
+int expand_parameter(HKEY, char *, char *, unsigned long, bool);
 int get_parameters(char *, char *, int, char *, int, char *, int);
 int get_exit_action(char *, unsigned long *, unsigned char *, bool *);