Pass sudo password prompt to stdout of spawned process in Node.js

孤街醉人 提交于 2019-12-24 17:56:08

问题


I have built a GUI with Node.js that allows a user to run custom binaries, designed for CLI. These binaries are spawned with child_process module. A user is able to write to stdin and read from stdout of those child processes.

Yet, when a binary uses sudo command, a password prompt bypass stdout and is headed for (pseudo-)terminal device, therefore I can't catch it programmatically and ask user for a password with a fancy GUI notification. I am aware, that -S argument allows sudo to expect the password from the standard input, yet I have no control over scripts that are run by a user.

I have found out that the stream object that represents stdin or stdout of a process may have isTTY property that indicates that this process is run in TTY context. I have tried to pass streams created with TTY module to stdio argument of child_process.spawn method without any luck.

I tried to use pty.js project (https://github.com/chjj/pty.js/) that actually solves the problem, but for some reasons I can't rely on 3rd party libraries. I try to figure out the way pty.js redirects sudo prompts to stdin, and it seems they use custom C code to emulate PTY (https://github.com/chjj/pty.js/blob/master/src/unix/pty.cc). I am concerned with a comment in one of source files, that states that ‘<...> with vanilla node, it's impossible to spawn a child process that has the stdin and stdout fd's be isatty <...>’. Is that true?

Right now I use a workaround that requires user to elevate privileges to my GUI process so it can spawn children that may use sudo, but I'm not satisfied with this hack. According to manual (http://linux.die.net/man/8/sudo), there is a way to specify helper program to handle password prompt by setting SUDO_ASKPASS environment variable, but I wasn't able to find an example of such an application.

So I'd like to know if there is a common practice to overcome my problem or should I give up and just keep on with getting root privileges preliminary.

来源:https://stackoverflow.com/questions/36756398/pass-sudo-password-prompt-to-stdout-of-spawned-process-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!