-1

I was wondering how openssh gets the password when login, cause I got stuck in automating entering passwords to the similar tools in linux which requires getting password from tty like ssh.

Tried to understand sshpass and found that sshpass forks a child process with the same pid then enters the password under the child process.

Don't know if my guess was right that ssh needs to check the right pid since I cannot stdin to the current tty using another process to enter the ssh password.

pynexj
  • 15,152
  • 5
  • 24
  • 45
  • 1
    It doub that anyone can fork a process with the same pid on any Unix. – ceving Mar 26 '19 at 09:02
  • This explains how to turn echo off in a terminal to read a password: https://stackoverflow.com/questions/5633472/how-do-i-turn-off-echo-in-a-terminal – ceving Mar 26 '19 at 09:04

1 Answers1

1

For security reasons, many programs requires a password interactively from users. Quite many programs uses the following kind of check before reading a password from stdin:

if (isatty(STDIN_FILENO) == 0)
{
    exit(EXIT_FAILURE); 
}

So the program allows password only from a terminal. That way it try to prevent non-interactive password entering.

sshpass is just a tool for:

fooling ssh into thinking it is getting the password from an interactive user. [from man page of sshpass]

For fooling ssh, sshpass creates and open a pseudo terminal, and gives that for stdin of ssh. fork() is needed because sshpass must write a password to ssh via the pseudo terminal.

This way stdin of ssh process is a terminal, and isatty test will be passed.

SKi
  • 7,243
  • 21
  • 49
  • Thanks for your answer, I think my expression is not very accurate and clear about the question. And [this one](https://stackoverflow.com/questions/55415122/what-causes-read-in-c-does-not-read-content-that-being-input-by-other-proc) is the exact problem I got, plz take a glance. – Yuri Sharp Mar 29 '19 at 10:21