coreybutler / node-windows

Windows support for Node.JS scripts (daemons, eventlog, UAC, etc).
Other
2.83k stars 356 forks source link

Issue with password when running as another user #260

Closed GregMydlarz closed 4 years ago

GregMydlarz commented 4 years ago

I'm running a file watcher app as a windows service (W10) with the following code used to install the service:

var Service = require('node-windows').Service;
const config = require('./SHR_modules/config');

// Create a new service object
var svc = new Service({
  name:'SmartHR',
  description: 'Smart HR file watcher',
  script: require('path').join(__dirname,'watcher.js'),
    workingDirectory: __dirname
});

// Listen for the "install" event, which indicates the
// process is available as a service.
svc.on('install',function(){
  svc.start();
  console.log('installed as user: ' +  svc.logOnAs.account)
});

svc.on('uninstall',function(){
    console.log('Uninstall complete.');
    console.log('The service exists: ',svc.exists);
  });

svc.logOnAs.domain = config.sqlServerLogin.domain;
svc.logOnAs.account = config.sqlServerLogin.user;
svc.logOnAs.password = config.sqlServerLogin.password;

svc.install();

//svc.uninstall();

Running the code as an administrator and the service does install properly, but it's stopped, so when I try to start it, the message is that the service can't start for the wrong password. Username and domain are correct. If I copy/paste the password into the Services Manager from my config.js, the service starts up and runs from now on. Why the password (whatever it is) is not passed by that line:

svc.logOnAs.password = config.sqlServerLogin.password; correctly?

GregMydlarz commented 4 years ago

The XML generator function in the winsw.js is missing one line for the service account. Original code:

    if (config.logOnAs) {
      xml.push({
        serviceaccount: [
          {domain: config.logOnAs.domain || 'NT AUTHORITY'},
          {user: config.logOnAs.account || 'LocalSystem'},
          {password: config.logOnAs.password || ''},
        ]
      });
    }

Working code:

    if (config.logOnAs) {
      xml.push({
        serviceaccount: [
          {domain: config.logOnAs.domain || 'NT AUTHORITY'},
          {user: config.logOnAs.account || 'LocalSystem'},
          {password: config.logOnAs.password || ''},
          {allowservicelogon: 'true'}
        ]
      });
    }

Want me to create a pull request for that?

coreybutler commented 4 years ago

Closing thanks to PR #262.