I've spent a couple of days wrestling with this problem. I suppose it's time to ask for help.
I need to be able to send and receive files to and from a remote server (mediaTemple (gs), specifically), over SSH. I would like to minimize, if not eliminate entirely, the need to use passwords. I would prefer to authenticate wherever possible using RSA keys.
I am able to get a little bit further using passwords, but I'd like to stick to my original plan of using public key authentication a while longer before I begin making concessions.
I have the luxury of testing my code on two different "clients": a Windows box (using copSSH, which is a cygwin-based openSSH implementation) and a Linux box. The abovementioned test machines attempt to go out and connect to the remote mediaTemple (gs) server.
The Windows box is a home PC, and I am attempting this with Apache running with root permissions (to eliminate problems in testing). The Linux box is another mediaTemple box (a (dv), specifically), which is on the same LAN as the "server", and Apache is running as "nobody", most likely.
Both test machines are running only slightly disparate versions of the relevant software, and the results, thus far, have been identical. Given the test scenario described above, the problem appears not to be with the firewall, or anything else related to the network topology. I'm not convinced that my problems are platform-specific, either.
Here is the code, in its simplest form:
$con = ssh2_connect('remote.host.com', '22', array('hostkey', 'ssh-rsa'));
ssh2_auth_pubkey_file($con, $user, $pubKeyFile, $privKeyFile);
This code fails with:
Warning: ssh2_auth_pubkey_file() [function.ssh2-auth-pubkey-file]: Authentication failed for joe.user using public key in [...] on line...
On both client machines, Apache has read-access to the key files defined in $pubKeyFile and $privKeyFile. On the Linux client, the directory containing the key files is in a directory that is several levels deep with owner root:apache, and the public and private key files within the directory have owner apache:apache and 700 permissions.
In other words, Apache is able to read the key files just fine, and the 700 permissions ensure that SSH-related executables (such as scp) do not complain about unsecured permissions on the key files. I can echo the key contents to the browser, so Apache can obviously read the key contents.
I have logged into the target server from each of the test clients, using key file authentication, so it's working/enabled, and the client machines have been added to the known_hosts file on the server.
If I use a password, instead of key authentication, like this
$con = ssh2_connect('remote.host.com', '22');
ssh2_auth_password($con, 'joe.user', 'some-password');
I am able to establish a connection and issue commands to the remote server.
Must the key files be located in a specific place on the client (such as in a "real" user's home directory -- difficult, given that Apache doesn't have a home directory)? Or should PHP be able to use key files in any location on the filesystem, as long as Apache can read the files and the the permissions are otherwise set as restrictively as possible?
This person had the same problem, and as is the case with most of these SSH/PHP questions, the thread dead-ends:
ssh_auth_pubkey_file fails:
http://forums.devnetwork.net/viewtopic.php?f=30&t=87758
Thanks for any help. Once I solve the public key authentication problem, I'll describe the issues that I encounter with scp, etc. once authenticated.