Problem to download binary file using phpseclib SCP - php

I have a a small PHP script that uses phpseclib to download files from remote server.
The script is like below:
include('Net/SCP.php');
echo var_dump($ssh->exec('whoami')); // debug to test the ssh connection. returns "myuser"
$scp = new Net_SCP($ssh);
try{
$remotePath = '/home/user/test.txt';
$localPath = '/tmp/myfile';
if (!$scp->get($remotePath, $localPath)) {
throw new Exception("Problems to get file");
}
} catch (Exception $e) {
echo "\n\n" . var_dump($e->getMessage()) . "\n\n";die;
}
There are some other questions here in SO that uses very similar snippets.
It works like a charm for many files, but it fails for some binary files ($remotePath = '/home/user/test.p12';, for instance).
Are there any know limitation to download binary files using phpseclib (I didn't find anything in their issues on github)? If not, what I'm doing wrong? Am I forgetting some options or anything?
As a side note scp myuser#serverip:/home/user/test.p12 /tmp/teste.p12 works fine in command line.
Following the comments, I must indicate that my script just fails. The statment $scp->get($remotePath, $localPath returns false for all binary files that I tried. Thats all i have for now.
As far as I know, phpseclib does not have any detailed log on these fails.
My application log (nginx) does not show anything special. Access log on my remote server (centOS. for these tests I have the control over it, but its not the real scenario) I got something like below:
Jul 27 15:22:58 localhost sshd[14101]: Accepted password for myuser from myip port 51740 ssh2
Jul 27 15:22:58 localhost sshd[14101]: pam_unix(sshd:session): session opened for user myuser by (uid=0)
Jul 27 15:22:58 localhost sshd[14103]: Received disconnect from myip port 51740:11:
Jul 27 15:22:58 localhost sshd[14103]: Disconnected from user myuser myip port 51740
PHP version: 7.3 (the script is also used in servers with older versions)
Local server: Debian 10
Remote server: CentOs 8
The problematic file that fires the problem is a certificate p12 file.

I found the problem and it was much more simple than I tought. It was just a permission problem (for any good reason, I put all my test files in a directory without read permition).
I decided to leave this answer here, because I think that this is not clear on phpseclib documentation, but the 'Net/SCP.php' only works with files with read permission, so, before download make sure that the file are readable or execute something like chmod o+r filename.
The snippet in the question works fine with binary files.

Related

is_readable() returns false while require_once returns true

I get false whenever I use the function is_readable on a file shared by my host on a VM, the expected result is true.
I am doing the setup of a local development environment for an existing projet. I don't want to use another function because it's a complex legacy system and I am afraid I would hide other potential problems I would stumble upon later in development.
The VM is set up using vagrant and virtualbox. The OS is a Windows Server 2008 machine with Zend Server with PHP 5.3 hosting code shared on the host, which is a Mac.
The shared folder is created as follow:
vmConfig.vm.synced_folder "/path/to/shared/folder/cms", '/cms', mount_options: ["dmode=775,fmode=664,type=smb"], owner: 'wcmadmin', group: 'wcmadmin'
A piece of code is trying to see if a file is readable. is_readable returns false. I run the script via command line with both users wcmadmin and Administrator and I get the same results.
function smarty_core_assemble_plugin_filepath($params, &$smarty)
{
[...]
// try relative to cwd (or absolute)
if (is_readable($_plugin_filepath)) {
$_return = $_plugin_filepath;
break;
}
[...]
I did a test script to dig further:
echo 'is_readable: ';
var_export(is_readable('C:\cms\path\to\file\file.php'));
echo "\n";
echo 'require_once: ';
var_export(require_once('C:\cms\path\to\file\file.php'));
And I have the following results:
is_readable: false
require_once: true
Using file_get_contents on the file returns the content correctly.
Using cygwin, the permissions on the file are as follow:
$ ls -al C:\cms\path\to\file\file.php
-rw-r--r-- 1 wcmadmin None 1498 Apr 18 07:22 C:\cms\path\to\file\file.php
Files path have been changed for the purpose of this question. While they may have some discrepancies, they resolve correctly during real tests.
It's a bug in virtual box and is still happening as of version 5.0.18.
See:
https://www.virtualbox.org/ticket/11675

Copy network file from a Linux host to my Linux server using PHP

I am trying to retrieve a network file from an OpenSUSE 13.1 Host to my OpenSUSE 13.2 Webserver, but I don't seem to have any success.
First I check if target directory is a directory:
$path = "\\\\192.168.xxx.xxx\\public";
if(is_dir($path)){ // returns FALSE every time.
return true;
}
In this case the function is_dir() returns FALSE, although the folder exists and has 777 permisions, the IP is correct, and the computer is turned ON.
I have tried all the combinations of formatting the network path, including $path = "smb://192.168.xxx.xxx/public"; witch returns an error
Unable to find the wrapper "smb" - did you forget to enable it when
you configured PHP?
Could someone tell me what I am missing?
Would it work the same through a OpenVPN with my Centos 6 webserver and an Ubuntu 14 host?
PHP has no built-in Samba support on the Linux platform. You will need to mount the remote share (requires superuser-equivalent access) and access its contents like a local filesystem.

Secure LDAP works with PHP CLI but not through apache

I am trying to authenticate over secure LDAP against my Debian server from another Debian box.
The following code works from the command line (prints "Success"):
if($ldap = ldap_connect('ldaps://myserver.tld'))
{
ldap_set_option($ldap, LDAP_OPT_PROTOCOL_VERSION, 3);
$bind = ldap_bind($ldap,'cn=admin,dc=myserver,dc=tld','mypassword');
print ldap_error($ldap);
}
...pulling the same file via Apache/mod_php prints "Can't contact LDAP server"
There is another similar question here on stackoverflow, but its answers did not work for me. I have already tried using the same php.ini for apache that was used for the cli, but the problem stayed the same.
We just had this problem happen. It turned out that all we had to do was restart Apache.
I have no idea what the cause was (I don't administer the server) but it was most likely security updates.
I was finally able to resolve my own issue and I do want to share my solution with you:
I am using a self-signed certificate for my LDAP-server, so it was neccessary to create my own ca-certificate and import it to the client's certificate store to establish trust.
While I ran the above script from the cli as user root, apache runs as www-data.
My selfmade ca-certifictate's permissions were set to 400, its user to root, thus only root could read it.
This prevented php on apache (running as www-data) from estabishing a connection to my LDAP-server. As soon as I had changed the ca-certificate's permissions to 444, everything worked like a charm...

Renaming and Moving Files on a Mapped Network Drive using PHP

I am generating a small script that manipulates a file name, renames the file, and moves it to a directory within it's current directory.
The problem (I think) is that I am running a local PHP Script from my computer and trying to have it do the manipulation on a network drive (R:\ in this case)
I decided, since I'm using Windows 7, that I'll be using the rename() function. Here is the snippet of code..
if (!empty($_REQUEST['uploaded_file']))
rename("R:\\".$fileName, "R:\\Movies\\".$newFileName);
Note: $filename and $newFileName are pulled earlier in the script
The resulting error is:
Warning: rename(R:\movie.title.2011.avi,R:\Movies\Movie Title [2011].avi):
The system cannot find the path specified. (code: 3)
The code use to work for a while, but for whatever reason, all of the sudden, it doesn't want to work anymore.
I'm not sure if using the rename scheme is the best approach for getting this done.. My Synology DS1010+ didn't like me using SFTP to connect and rename too many times.. It eventually started to refuse connections, so I needed to do this differently..
Environment Information:
Operating System:
Windows 7 Ultimate x64
PHP Version: 5.4.4
FileZilla Connection Attempt:
Status: Connecting to 192.168.5.25:21...
Status: Connection established, waiting for welcome message...
Response: 220 Diniz-Stora FTP server ready.
Command: USER admin
Response: 331 Password required for admin.
Command: PASS **********
Response: 230 User admin logged in.
Status: Connected
Status: Retrieving directory listing...
Command: PWD
Response: 257 "/" is current directory.
Command: TYPE I
Response: 200 Type set to I.
Command: PASV
Response: 227 Entering Passive Mode (24,63,165,211,217,0)
Command: LIST

netbeans php remote server upload files

I'm a student, new to netBean php remote server. I was trying to upload php files to remote server through netbeans (on run). I'm sure everything in run configuration, ftp information is correct, I have rights to upload file to that folder.
And this error occur:
Message prompt:
"Cannot logout from server *name. Reason 250 OK. Current directory: *dir name"
Logs:
Log in output window:
220---------- Welcome to Pure-FTPd [privsep] ----------
220-You are user number 17 of 500 allowed.
220-Local time is now 04:13. Server port: 21.
220-This is a private system - No anonymous login
220 You will be disconnected after 3 minutes of inactivity.
USER a4514022
331 User a4514022 OK. Password required
PASS ******
230-OK. Current restricted directory is /
230-362 files used (3%) - authorized: 10000 files
230 2476 Kbytes used (0%) - authorized: 1536000 Kb
TYPE I
200 TYPE is now 8-bit binary
CWD /public_html
250 OK. Current directory is /public_html
CWD /public_html
250 OK. Current directory is /public_html
Summary
Failed:
file test.php Cannot upload file test.php (unknown reason).
Runtime: 408 ms, transfered: 0 file(s), 0 KB
I don't get what happen. How to fix it?
I use netbean 6.9.1, windows 7 and java ver 7 (build 1.7.0_05) platform 1.7 Those numbers, I don't know which one is the version. I just put all those there. It seems I'm the rare one get this problem...
I Know you asked this and probably your problem has solved
but I write this for the another ones;
first check the Proxy.
Tools>Option>Proxy Settings
and then set on the "no proxy" .
It will probably be solved.
I had to select less secure option e.g. Encryption: Pure FTP option to get it working for GoDaddy setup.
This answer comes even later. I also had the error and ended up here in search.
My mistake was that I had the file I wanted to download in editing with another reader (outside Netbeans). In other words, the downloaded file could not be saved to local filesystem, because the opened file prevented that.

Categories