sapling/tests/test-sshaskpass.py
Jun Wu 34c0e30789 sshaskpass: enable ssh to ask password if running with chg
Summary:
During chg pull or push over ssh, ssh is started by chgserver which does not
have a controlling tty. Therefore the ssh process won't be able to ask for
passwords interactively.

This is actually a hard issue because an unprivileged process without a ctty
cannot attach to a ctty of another process.

The discussion at upstream tends to make it clear it's part of limitations
of chg. Therefore if we decide to workaround it, it has to live outside core,
thus fb-hgext.

GUI ssh-askpass is actually a good and clean choice. See D3510178 and D3515604.
However, they are for OS X but not Linux.

This diff is a very hacky solution to make ssh-askpass works in terminal.
It starts a "tty server" providing tty I/O fds and set `SSH_ASKPASS` to use a
custom script talking to the "tty server".

Test Plan:
Run the new test. Start a sshd locally and try:
```
$ hg push ssh://root@localhost/tmp
pushing to ssh://root@localhost/tmp
root@localhost's password:
remote: Permission denied (publickey,password).
abort: no suitable response from remote hg!

$ chg push ssh://root@localhost/tmp
pushing to ssh://root@localhost/tmp
==== SSH Authenticating ====
root@localhost's password:
remote: Permission denied (publickey,password).
abort: no suitable response from remote hg!
```

Reviewers: #mercurial, ttung, mpm

Reviewed By: mpm

Subscribers: durham, mpm, mjpieters

Differential Revision: https://phabricator.intern.facebook.com/D3577509

Tasks: 12029680

Signature: t1:3577509:1469467700:cd93565bd47e535bb4cb41fcdaa39e45dddfae28
2016-07-17 20:35:34 +01:00

29 lines
782 B
Python

import os
import sys
# Make sure we use sshaskpass.py in this repo, unaffected by PYTHONPATH
sys.path.insert(0, os.path.join(os.path.dirname(__file__), '../hgext3rd'))
import sshaskpass
# stdin, stderr have to be tty to run test
pid, master = os.forkpty()
if pid:
# parent, test some I/O
os.write(master, '(input)\n')
with os.fdopen(master, 'r') as f:
sys.stdout.write('pty receives: %r' % f.read())
os.waitpid(pid, 0)
sys.exit(0)
# child, start a ttyserver and do some I/O
ttysrvpid, sockpath = sshaskpass._startttyserver()
try:
r, w = sshaskpass._receivefds(sockpath)
with os.fdopen(r) as f:
line = f.readline()
os.write(w, 'client receives: ' + line)
finally:
sshaskpass._killprocess(ttysrvpid)
os.unlink(sockpath)