<div dir="ltr"><div><div><div></div>I created a pull request for the ssh application: <a href="https://github.com/erlang/otp/pull/1331">https://github.com/erlang/otp/pull/1331</a><br></div>It includes a test case and a fix for opening a file by a relative path.<br><br></div>Karolis<br><div><div><div><br></div></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 1, 2017 at 4:06 PM, Karolis Petrauskas <span dir="ltr"><<a href="mailto:k.petrauskas@gmail.com" target="_blank">k.petrauskas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>In ssh_sftpd.erl (ssh application, version 4.4) there is condition for determining absolute file path when opening a file:<br><br>            AbsPath = case Root of<br>                          "" -><br>                              Path;<br>                          _ -><br>                              relate_file_name(Path, State0)  <br>                      end,<br><br></div><div>There is no such condition when listing directory or resolving real path.<br>Root = "" by default, if root_dir is not specified by the user.<br><br></div><div>Is this difference in resolving file names intended? <br></div><span class="HOEnZb"><font color="#888888"><div><br></div>Karolis<br><div><br></div></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Feb 1, 2017 at 1:40 PM, Karolis Petrauskas <span dir="ltr"><<a href="mailto:k.petrauskas@gmail.com" target="_blank">k.petrauskas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Hi,<br><br></div><div>I made a smaller test program to reproduce the problem (shown bellow).<br>It reproduces the problem only partially, because it can't find the files in<br>the Erlang sftp server by relative file names all the time. In my previous<br>experiments I the misbehaviour was unstable.<br><br><div style="margin-left:40px">-module(test_sftp).<br>-export([test/1]).<br><br>make_files(DirName) -><br>    lists:foreach(<br>        fun (FileName) -> ok = file:write_file(filename:join(<wbr>DirName, FileName), [FileName, " contents"]) end,<br>        ["some.txt", "other.txt", "none.txt", "yetanother.txt", "any.txt"]<br>    ),<br>    ok.<br><br>start_server(Port, Cwd) -><br>    {ok, Pid} = ssh:daemon(Port, [<br>        {system_dir, "test_sftp_data/sys"},<br>        {user_dir,   "test_sftp_data/usr"},<br>        {user_passwords, [{"test_sftp", "test_sftp"}]},<br>        {subsystems, [ssh_sftpd:subsystem_spec([{cw<wbr>d, Cwd}])]}<br>    ]),<br>    {ok, Pid}.<br><br>check_client(Port, User, Pass, DirName) -><br>    {ok, ChannelPid, ConnectionRef} = ssh_sftp:start_channel("localh<wbr>ost", Port, [{user, User}, {password, Pass}]),<br>    {ok, FileNames} = ssh_sftp:list_dir(ChannelPid, DirName),<br>    Result = lists:map(fun (FileName) -><br>        {DirName, FileName, ssh_sftp:read_file(ChannelPid, filename:join(DirName, FileName))}<br>    end, FileNames),<br>    ok = ssh_sftp:stop_channel(ChannelP<wbr>id),<br>    ok = ssh:close(ConnectionRef),<br>    Result.<br><br>test(Pass) -><br>    ok = make_files("/home/karolis/temp<wbr>/test_sftp"),<br>    {ok, Pid} = start_server(8022, "/home/karolis"),<br>    lists:foreach(fun (N) -><br>        io:format("--------------- Test n=~p ---------------------~n", [N]),<br>        io:format("Erlang-ABS: ~100p~n",  [check_client(8022, "test_sftp", "test_sftp", "/home/karolis/temp/test_sftp"<wbr>)]),<br>        io:format("Erlang-REL: ~100p~n",  [check_client(8022, "test_sftp", "test_sftp", "temp/test_sftp")]),<br>        io:format("OpenSSH-ABS: ~100p~n", [check_client(22,   "karolis",   Pass,        "/home/karolis/temp/test_sftp<wbr>")]),<br>        io:format("OpenSSH-REL: ~100p~n", [check_client(22,   "karolis",   Pass,        "temp/test_sftp")])<br>    end, lists:seq(1, 10)),<br>    ok = ssh:stop_daemon(Pid),<br>    ok.<br></div><br><br></div>All the iterations printed the same results (only the output of the first iteration is shown bellow):<br><div><br><div style="margin-left:40px">--------------- Test n=1 ---------------------<br>Erlang-ABS: [{"/home/karolis/temp/test_sft<wbr>p","none.txt",{ok,<<"none.txt contents">>}},<br>             {"/home/karolis/temp/test_sft<wbr>p","any.txt",{ok,<<"any.txt contents">>}},<br>             {"/home/karolis/temp/test_sft<wbr>p","other.txt",{ok,<<"other.<wbr>txt contents">>}},<br>             {"/home/karolis/temp/test_sft<wbr>p","yetanother.txt",{ok,<<"yet<wbr>another.txt contents">>}},<br>             {"/home/karolis/temp/test_sft<wbr>p","some.txt",{ok,<<"some.txt contents">>}}]<br><b>Erlang-REL: [{"temp/test_sftp","none.txt",<wbr>{error,no_such_file}},<br>             {"temp/test_sftp","any.txt",{<wbr>error,no_such_file}},<br>             {"temp/test_sftp","other.txt"<wbr>,{error,no_such_file}},<br>             {"temp/test_sftp","yetanother<wbr>.txt",{error,no_such_file}},<br>             {"temp/test_sftp","some.txt",<wbr>{error,no_such_file}}]</b><br>OpenSSH-ABS: [{"/home/karolis/temp/test_sft<wbr>p","..",{error,failure}},<br>              {"/home/karolis/temp/test_sftp<wbr>","none.txt",{ok,<<"none.txt contents">>}},<br>              {"/home/karolis/temp/test_sftp<wbr>","any.txt",{ok,<<"any.txt contents">>}},<br>              {"/home/karolis/temp/test_sftp<wbr>","other.txt",{ok,<<"other.txt contents">>}},<br>              {"/home/karolis/temp/test_sftp<wbr>","yetanother.txt",{ok,<<"yeta<wbr>nother.txt contents">>}},<br>              {"/home/karolis/temp/test_sftp<wbr>","some.txt",{ok,<<"some.txt contents">>}},<br>              {"/home/karolis/temp/test_sftp<wbr>",".",{error,failure}}]<br>OpenSSH-REL: [{"temp/test_sftp","..",{error<wbr>,failure}},<br>              {"temp/test_sftp","none.txt",{<wbr>ok,<<"none.txt contents">>}},<br>              {"temp/test_sftp","any.txt",{o<wbr>k,<<"any.txt contents">>}},<br>              {"temp/test_sftp","other.txt",<wbr>{ok,<<"other.txt contents">>}},<br>              {"temp/test_sftp","yetanother.<wbr>txt",{ok,<<"yetanother.txt contents">>}},<br>              {"temp/test_sftp","some.txt",{<wbr>ok,<<"some.txt contents">>}},<br>              {"temp/test_sftp",".",{error,f<wbr>ailure}}]<br></div><br><br></div><div>I repeated the same test with the home dir (relative path = "."), and the results were the same.<br>So it seems that the Erlang's sftp server can list directories, but can't open the files  by relative paths.<span class="m_-4139892465532325511HOEnZb"><font color="#888888"><br></font></span></div><span class="m_-4139892465532325511HOEnZb"><font color="#888888"><div><br></div><div>Karolis<br></div></font></span></div><div class="m_-4139892465532325511HOEnZb"><div class="m_-4139892465532325511h5"><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Jan 30, 2017 at 10:19 PM, Karolis Petrauskas <span dir="ltr"><<a href="mailto:k.petrauskas@gmail.com" target="_blank">k.petrauskas@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">I'm using "Erlang/OTP 19 [erts-8.2]", debian package from<br>
erlang-solutions, version 1:19.2-1.<br>
<br>
I'll check that with erlang client and sshd server. I was playing with<br>
ssh_sftpd server from a command line (sftp), and noticed no problems<br>
except the cwd problem when combined with root directory restriction.<br>
<span class="m_-4139892465532325511m_-1317666687722841017HOEnZb"><font color="#888888"><br>
Karolis<br>
</font></span><div class="m_-4139892465532325511m_-1317666687722841017HOEnZb"><div class="m_-4139892465532325511m_-1317666687722841017h5"><br>
On Mon, Jan 30, 2017 at 8:01 PM, bengt <<a href="mailto:cean.ebengt@gmail.com" target="_blank">cean.ebengt@gmail.com</a>> wrote:<br>
> Greetings,<br>
><br>
> Can you try with sshd, instead of ssh_sftpd ? As a way of halving the potential error sources. Also, which Erlang version are you on?<br>
><br>
><br>
> bengt<br>
><br>
>> On 30 Jan 2017, at 13:55, Karolis Petrauskas <<a href="mailto:k.petrauskas@gmail.com" target="_blank">k.petrauskas@gmail.com</a>> wrote:<br>
>><br>
>> Hello,<br>
>><br>
>> I'm trying to use erlang's sftp server and client. In the example<br>
>> bellow, i use erlang for both: the server and the client. For some<br>
>> reason, i can't open some files.<br>
>><br>
>>    43> ssh_sftp:list_dir(Ch, ".").<br>
>>    {ok,["other.txt","some.txt"]}<br>
>><br>
>>    44> ssh_sftp:read_file(Ch, "some.txt").<br>
>>    {ok,<<"some.txt contents">>}<br>
>><br>
>>    45> ssh_sftp:read_file(Ch, "other.txt").<br>
>>    {error,no_such_file}<br>
>><br>
>> I was able to read both files with absolute file names specified, but<br>
>> in my case I should not depend on the absolute paths. I could use the<br>
>> absolute file names when opening files, if I could get the current<br>
>> working directory from the server, but I cant find any function for<br>
>> that.<br>
>><br>
>> The server behaviour is also a unclear to me. I can't set the cwd, if<br>
>> the root option is used when starting the daemon. I have tried various<br>
>> combinations of root/cwd (cwd is relative to root, or absolute on the<br>
>> target file system), the sftp client always logins with / as its<br>
>> current working directory.<br>
>><br>
>> In my case, I use the sftpd only for testing the sftp client in CT.<br>
>> I'm working on Erlang/OTP 19 [erts-8.2].<br>
>><br>
>> Would be thankful for any pointers.<br>
>><br>
>> Karolis<br>
>> ______________________________<wbr>_________________<br>
>> erlang-questions mailing list<br>
>> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
>> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
><br>
> ______________________________<wbr>_________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/list<wbr>info/erlang-questions</a><br>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>
</div></div></blockquote></div><br></div>