我正在编写一个运行各种shell命令的PHP程序.有时它需要调用su,并且按照设计,我希望它提示提升特权密码.在PHP中使用passthru()可以正常工作.
我选择只为我的程序编写功能测试,因为它依赖于ssh,su和其他shell命令.因此,我想在PHPUnit中运行真实的东西,看看它是否像我期望的那样工作.
由于这需要SSH服务器和用户帐户配置,我已将测试设置为在Docker中运行.这种方法看起来很好 – 图像构建,运行时调用PHPUnit,然后退出.我希望有一种方法可以通过Docker将结果返回给调用系统,例如Travis CI.
我已选择Alpine Linux作为我的基本Docker镜像,但我遇到终端分配问题.我原本以为PHPUnit正在阻碍(参见这个问题的原始版本)但我现在已经缩小到SSH,无论是由PHP运行,还是仅在控制台运行.
这很好用:
su -c whoami
但是,这不是:
ssh localhost -t 'su -c whoami'
我明白了:
su: must be suid to work properly
Connection to localhost closed.
请注意,ssh设置为无密码访问localhost(用于测试目的),因此我期望输入的唯一密码是su.
ssh的手册表明-f对于要求密码的位置很有帮助(交换机“在命令执行之前请求ssh转到后台”)所以我试试这个:
ssh localhost -t -f 'su -c whoami'
我得到:
Pseudo-terminal will not be allocated because stdin is not a terminal.
4275760bde94:~$su: must be suid to work properly
ssh localhost -tt -f 'su -c whoami'
双t仍然会出现关于SUID的投诉,但仍然无效.
但是,如果我在我的Ubuntu开发机上执行此操作(也配置了PPK访问自己),它可以正常工作:
$ssh localhost -t 'su -c whoami'
Password:
root
Connection to localhost closed.
这使它看起来像Alpine或BusyBox的OpenSSH有问题.我该如何进一步深入研究?
一个解决方案只是交换到另一个基础发行版,Ubuntu肯定会工作正常,但这将大大增加我的Docker图像大小(目前总共68M非常好).所以,如果可以的话,我想坚持使用Alpine.
不太可能是Docker
我确实想知道Docker是否可能阻止创建或连接终端,但很快就打了折扣,因为我可以使用docker exec -it container_name sh获得交互式shell.此外,我可以在Docker shell中的两个单独的命令中执行ssh然后su,就好了.
Bash没有帮助
我注意到Bash在Alpine中可用,但这也没有帮助,这让我感到惊讶:
/ $apk add bash
bash-4.3$bash
bash-4.3$su nonpriv
bash-4.3$ssh localhost whoami
nonpriv
bash-4.3$ssh localhost 'su -c whoami'
su: must be suid to work properly
bash-4.3$ssh localhost 'su -s /bin/bash -c whoami'
su: must be suid to work properly
bash-4.3$ssh -t localhost 'su -s /bin/bash -c whoami'
su: must be suid to work properly
Connection to localhost closed.
bash-4.3$ssh -tt localhost 'su -s /bin/bash -c whoami'
su: must be suid to work properly
Connection to localhost closed.
bash-4.3$ssh -tf localhost 'su -s /bin/bash -c whoami'
Pseudo-terminal will not be allocated because stdin is not a terminal.
bash-4.3$su: must be suid to work properly
bash-4.3$ssh -ttf localhost 'su -s /bin/bash -c whoami'
bash-4.3$su: must be suid to work properly
Connection to localhost closed.
尝试shell插值
我发现这几乎有用:
/ $ssh -t localhost "$( su -c whoami )"
sh: Password:: not found
sh: root: not found
Connection to localhost closed.
这使用标准的Alpine sh shell,并使用上面链接中的“$()”构造,我并不完全理解.它输出密码:,这是su中的提示,然后等待密码.输入密码后,运行whoami,打印root.
所以看起来它正在运行命令,但我不确定它是否立即运行whoami然后将其传递给ssh(不是我想要的)或者它是否正在获取一个远程shell到localhost然后这样做(这个是我的意图).
在任何情况下,它都试图运行标准输出行,就好像它们本身就是命令一样.如何才能通过SSH打印输出?