Skip to content

Commit 8164545

Browse files
committed
Initial import
0 parents  commit 8164545

10 files changed

+217
-0
lines changed

.gitignore

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.rebar3
2+
rebar3
3+
_*
4+
.eunit
5+
*.o
6+
*.beam
7+
*.plt
8+
.erlang.cookie
9+
ebin
10+
log
11+
erl_crash.dump
12+
.rebar
13+
_rel
14+
_deps
15+
_plugins
16+
_tdeps
17+
logs

LICENSE

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Copyright (c) 2017 Ricardo Lanziano
2+
3+
Permission is hereby granted, free of charge, to any person obtaining
4+
a copy of this software and associated documentation files (the
5+
"Software"), to deal in the Software without restriction, including
6+
without limitation the rights to use, copy, modify, merge, publish,
7+
distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to
9+
the following conditions:
10+
11+
The above copyright notice and this permission notice shall be
12+
included in all copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
servire
2+
=======
3+
4+
Serve the current directory over SFTP.
5+
6+
Quick and dirty escript for sharing files and directories over SFTP
7+
without having to add users to your system. It might improve in the
8+
future.
9+
10+
You need to have Erlang and `rebar3` installed in order to compile and
11+
run the escript.
12+
13+
## Build
14+
```sh
15+
$ cd servire
16+
$ rebar3 escriptize
17+
$ cp _build/default/bin/servire $YOUR_LOCAL_PATH/bin
18+
```
19+
20+
## Usage
21+
```sh
22+
$ cd /tmp
23+
$ servire 9999
24+
Serving /tmp on port 9999
25+
```
26+
27+
## License
28+
29+
Copyright (c) 2017 Ricardo Lanziano
30+
31+
This work is free. You can redistribute it and/or modify it under the
32+
terms of the MIT License. See the [LICENSE](LICENSE) file for more
33+
details.

rebar.config

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{erl_opts, [debug_info]}.
2+
{lfe_first_files, []}.
3+
4+
{deps, [lfe]}.
5+
6+
{plugins, [{'lfe-compile', "0.8.0-rc3", {pkg, rebar3_lfe_compile}}]}.
7+
8+
{provider_hooks, [
9+
{pre, [{compile, {lfe, compile}}]}
10+
]}.
11+
12+
{profiles, [
13+
{test, [
14+
{eunit_compile_opts, [
15+
{src_dirs, ["test", "src"]}
16+
]},
17+
{ct_compile_opts, [
18+
{src_dirs, ["test", "src"]}
19+
]},
20+
{deps, [ltest]}]},
21+
22+
{doc, [
23+
{plugins, [
24+
{lodox, {git, "https://github.com/lfe-rebar3/lodox.git", {tag, "0.16.2"}}}
25+
]}
26+
]}
27+
]}.

rebar.lock

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{"1.1.0",
2+
[{<<"lfe">>,{pkg,<<"lfe">>,<<"1.2.0">>},0}]}.
3+
[
4+
{pkg_hash,[
5+
{<<"lfe">>, <<"257708859C0A6949F174CECEE6F08BB5D6F08C0F97EC7B75C07072014723ECBC">>}]}
6+
].

src/servire-keys.lfe

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#|
2+
@doc
3+
servire SSH key callback API
4+
@end
5+
|#
6+
7+
(defmodule servire-keys
8+
(behaviour ssh_server_key_api)
9+
(export (host_key 2)
10+
(is_auth_key 3)))
11+
12+
;;; API
13+
14+
(defun host_key (_type _opts)
15+
(let* [(pem-file (++ (servire-utils:get-priv-dir) "/sshd.pem"))
16+
(`#(ok ,pem) (file:read_file pem-file))
17+
(`[,rsa] (public_key:pem_decode pem))]
18+
`#(ok ,(public_key:pem_entry_decode rsa))))
19+
20+
(defun is_auth_key (_key _user _ssh_opts)
21+
'true)

src/servire-utils.lfe

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#|
2+
@doc
3+
servire misc utils
4+
@end
5+
|#
6+
7+
(defmodule servire-utils
8+
(export all))
9+
10+
;; Misc utils
11+
12+
(defun get-priv-dir ()
13+
(++ (os:getenv "HOME") "/.servire"))

src/servire.app.src

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{application, 'servire',
2+
[{description, "Serve files via SFTP"},
3+
{vsn, "0.0.1"},
4+
{registered, []},
5+
{applications,
6+
[kernel,
7+
stdlib,
8+
crypto,
9+
public_key,
10+
ssh
11+
]},
12+
{env, []},
13+
{modules, []},
14+
15+
{contributors, ["Ricardo Lanziano"]},
16+
{licenses, ["MIT"]},
17+
{links, [{"GitHub", "https://github.com/arpunk/servire"}]}
18+
]}.

src/servire.lfe

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#|
2+
@doc
3+
servire public API
4+
@end
5+
|#
6+
7+
(defmodule servire
8+
(export (main 1)))
9+
10+
;;; API
11+
12+
(defun main
13+
([`(,port)]
14+
(application:ensure_all_started 'ssh)
15+
(run (list_to_integer port)))
16+
([_] (usage)))
17+
18+
(defun run (port)
19+
(serve port)
20+
(timer:sleep 'infinity))
21+
22+
(defun serve (port)
23+
(let* ([`#(ok ,root) (file:get_cwd)]
24+
[opts `(#(cwd ,root) #(root ,root))]
25+
[subsystem (ssh_sftpd:subsystem_spec opts)]
26+
[args (default-opts subsystem)]
27+
[`#(ok ,daemon-ref) (ssh:daemon port args)])
28+
(io:format "Serving ~s on port ~p~n" `(,root ,port))
29+
`#(ok ,daemon-ref)))
30+
31+
(defun usage ()
32+
(io:format "usage: servire port~n"))
33+
34+
;;; Internal functions
35+
36+
(defun default-opts (subsystem)
37+
`(#(key_cb servire-keys)
38+
#(subsystems (,subsystem))
39+
#(parallel_login true)
40+
#(shell ,(lambda (user _password) (spawn (lambda () (welcome user)))))
41+
#(max_sessions 10)
42+
#(id_string ,"servire")
43+
#(system_dir ,(servire-utils:get-priv-dir))
44+
#(pwdfun ,(lambda (_u _p) 'true))))
45+
46+
(defun pwd-fun (_user _pass _peer _state)
47+
'true)
48+
49+
(defun welcome (user)
50+
(io:format "Welcome ~s, you may now use sftp~n" `(,user)))

test/unit-servire-tests.lfe

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(defmodule unit-servire-tests
2+
(behaviour ltest-unit)
3+
(export all)
4+
(import
5+
(from ltest
6+
(check-failed-assert 2)
7+
(check-wrong-assert-exception 2))))
8+
9+
(include-lib "ltest/include/ltest-macros.lfe")
10+
11+
(deftest servire-hello-world
12+
(is 'true))

0 commit comments

Comments
 (0)