I'm trying to add users to the server running my node application. Right now I am using:
exec("useradd -mp {password-hash}")
with a hash that gets created by
exec("mkpasswd -m des {password}")
This leaves the password visible in the process list, and that should be avoided at all costs.
That's why I am wondering if there is a module on npm or an easier way to add a linux user to the system programmatically (this is necessary because the app I am building creates a user account on the system when a user registers on the web). Can this be done without evoking system commands like I am currently doing? I have found neither npm module nor any information of using node to interact with ubuntu/unix user management.
Solution
Using Mustafa's hints from the top answer, I implemented a version of the mkpasswd
command using spawn
. In CoffeeScript, it would look like this:
{spawn} = require 'child_process'
child = spawn 'mkpasswd', ['-m','des','--stdin']
child.stdout.on 'data', (data)->
console.log 'password hash arrived: ', data.toString()
child.stdin.write 'password'
Make sure you add the proper error handling event handlers to child.stderr
and child.stdout
while debugging your code.