Use well-known account aliases.
[nssm.git] / settings.cpp
index 907524d..172eeba 100644 (file)
@@ -530,31 +530,25 @@ int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *n
   bool localsystem = false;
   TCHAR *username = NSSM_LOCALSYSTEM_ACCOUNT;
   TCHAR *password = 0;
-  TCHAR *canon = 0;
   if (additional) {
     username = (TCHAR *) additional;
     if (value && value->string) password = value->string;
   }
   else if (value && value->string) username = value->string;
 
-  if (requires_password(username)) {
-    if (! password) {
-      /* We need a password if the account requires it. */
-      print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
-      return -1;
-    }
+  const TCHAR *well_known = well_known_username(username);
+  size_t passwordsize = 0;
+  if (well_known) {
+    if (str_equiv(well_known, NSSM_LOCALSYSTEM_ACCOUNT)) localsystem = true;
+    username = (TCHAR *) well_known;
+    password = _T("");
   }
-  else {
-    password = 0;
-    if (is_localsystem(username)) {
-      localsystem = true;
-      username = NSSM_LOCALSYSTEM_ACCOUNT;
-    }
-    else {
-      canon = canonical_username(username);
-      if (canon) username = canon;
-    }
+  else if (! password) {
+    /* We need a password if the account requires it. */
+    print_message(stderr, NSSM_MESSAGE_MISSING_PASSWORD, name);
+    return -1;
   }
+  else passwordsize = _tcslen(password) * sizeof(TCHAR);
 
   /*
     ChangeServiceConfig() will fail to set the username if the service is set
@@ -564,8 +558,7 @@ int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *n
   if (! localsystem) {
     QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle);
     if (! qsc) {
-      if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
-      if (canon) LocalFree(canon);
+      if (passwordsize) SecureZeroMemory(password, passwordsize);
       return -1;
     }
 
@@ -573,24 +566,22 @@ int native_set_objectname(const TCHAR *service_name, void *param, const TCHAR *n
     HeapFree(GetProcessHeap(), 0, qsc);
   }
 
-  if (password) {
+  if (! well_known) {
     if (grant_logon_as_service(username)) {
-      if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
-      if (canon) LocalFree(canon);
+      if (passwordsize) SecureZeroMemory(password, passwordsize);
       print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username);
       return -1;
     }
   }
 
   if (! ChangeServiceConfig(service_handle, type, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 0, 0, 0, 0, username, password, 0)) {
-    if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
-    if (canon) LocalFree(canon);
+    if (passwordsize) SecureZeroMemory(password, passwordsize);
     print_message(stderr, NSSM_MESSAGE_CHANGESERVICECONFIG_FAILED, error_string(GetLastError()));
     return -1;
   }
-  if (password) SecureZeroMemory(password, _tcslen(password) * sizeof(TCHAR));
 
-  if (canon) LocalFree(canon);
+  if (passwordsize) SecureZeroMemory(password, passwordsize);
+
   if (localsystem) return 0;
 
   return 1;