Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sudo: a password is required #50

Closed
miguelmota opened this issue May 8, 2017 · 4 comments
Closed

sudo: a password is required #50

miguelmota opened this issue May 8, 2017 · 4 comments

Comments

@miguelmota
Copy link

Running example code doesn't work

index.js

var Sudoer = require('electron-sudo').default;

var options = {name: 'electron sudo application'};
var sudoer = new Sudoer(options);

sudoer.spawn(
  'echo', ['$PARAM'], {env: {PARAM: 'VALUE'}}
)
.then(cp => {
  cp.on('close', () => {
    console.log(cp.output.stdout.toString())
    console.log(cp.output.stderr.toString())
  });
})

output

$ node index.js

sudo: a password is required

Would appreciate it if someone can point me in the right direction.

Thanks!

@davydany
Copy link

davydany commented May 9, 2017

You're probably on a Mac, and your ticket sounds like its related to this: #39 Basically Mac's tty-tickets feature is causing an issue with this library.

electron-sudo is great, but the devs haven't fixed this yet 😢

I've instead started to use the workaround with sudo-prompt. https://www.npmjs.com/package/sudo-prompt

This is what the sudo-prompt documentation says:

On systems where the user has opted to have tty-tickets enabled, each call to exec() will result in a separate password prompt. Where tty-tickets are disabled, subsequent calls to exec() (but not concurrent calls) will not require a password prompt, so long as the user's sudo timestamp file remains valid.

You should never rely on sudo-prompt to execute your calls in order. If you need to enforce ordering of calls, then you should explicitly order your calls in your application. Where your commands are short-lived, you should queue your calls to exec() to make sure your user is not overloaded with password prompts.

If electron-sudo's devs fix this bug, I'd gladly switch back to electron-sudo since sudo-prompt is still a workaround and doesn't actually fix the issue. sudo-prompt will throw multiple windows asking for the user for each command you want sudo access for. You can get around this by using && but it makes your code VERY VERY messy!

@automation-stack
Copy link
Owner

Duplicated #39

@boid-com
Copy link

boid-com commented Mar 9, 2018

I also would like a fix for this one. Will switch to sudo-prompt until then.

@miguelmota
Copy link
Author

miguelmota commented Mar 9, 2018

If you require sudo prompt on mac then this is all you really need:

var cp = require('child_process')
var isRoot = (process.getuid && process.getuid() === 0)

if (!isRoot) {
  var cmd = `echo hello`
  var prompt = `/usr/bin/osascript -e 'do shell script "bash -c \\\"${cmd}\\\"" with administrator privileges'`
  cp.exec(prompt)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants