diff options
author | Simponic <loganhunt@simponic.xyz> | 2022-12-27 23:50:22 -0700 |
---|---|---|
committer | Simponic <loganhunt@simponic.xyz> | 2022-12-27 23:50:22 -0700 |
commit | 10bc34245e8e1e3ba63fb0720d3bcfb1119db921 (patch) | |
tree | 0158f37dcda36f36327e45c4ce7543e77c26c8a0 /lib/chessh/ssh | |
parent | f7c2ccbe26dc808e4a7eae9a378e6c382220961a (diff) | |
download | chessh-10bc34245e8e1e3ba63fb0720d3bcfb1119db921.tar.gz chessh-10bc34245e8e1e3ba63fb0720d3bcfb1119db921.zip |
Initial erlang stuff
Diffstat (limited to 'lib/chessh/ssh')
-rw-r--r-- | lib/chessh/ssh/daemon.ex | 60 | ||||
-rw-r--r-- | lib/chessh/ssh/server.ex | 0 | ||||
-rw-r--r-- | lib/chessh/ssh/server_key.ex | 11 |
3 files changed, 71 insertions, 0 deletions
diff --git a/lib/chessh/ssh/daemon.ex b/lib/chessh/ssh/daemon.ex new file mode 100644 index 0000000..acb6bea --- /dev/null +++ b/lib/chessh/ssh/daemon.ex @@ -0,0 +1,60 @@ +defmodule Chessh.SSH.Daemon do + use GenServer + + def start_link(_) do + GenServer.start_link(__MODULE__, %{ + pid: nil + }) + end + + def init(state) do + GenServer.cast(self(), :start) + {:ok, state} + end + + def pwd_authenticate(username, password, _address, attempts) do + if Chessh.Auth.PasswordAuthenticator.authenticate(username, password) do + true + else + newAttempts = + case attempts do + :undefined -> 0 + _ -> attempts + end + + if Application.fetch_env!(:chessh, :max_password_attempts) <= newAttempts do + :disconnect + else + {false, newAttempts + 1} + end + end + end + + def handle_cast(:start, state) do + port = Application.fetch_env!(:chessh, :port) + key_dir = String.to_charlist(Application.fetch_env!(:chessh, :key_dir)) + max_sessions = Application.fetch_env!(:chessh, :max_sessions) + + case :ssh.daemon( + port, + system_dir: key_dir, + pwdfun: &pwd_authenticate/4, + key_cb: Chessh.SSH.ServerKey, + id_string: :random, + subsystems: [], + parallel_login: true, + max_sessions: max_sessions + ) do + {:ok, pid} -> + Process.link(pid) + {:noreply, %{state | pid: pid}, :hibernate} + + {:error, err} -> + raise inspect(err) + end + + {:noreply, state} + end + + def handle_info(_, state), do: {:noreply, state} +end diff --git a/lib/chessh/ssh/server.ex b/lib/chessh/ssh/server.ex deleted file mode 100644 index e69de29..0000000 --- a/lib/chessh/ssh/server.ex +++ /dev/null diff --git a/lib/chessh/ssh/server_key.ex b/lib/chessh/ssh/server_key.ex new file mode 100644 index 0000000..1096e09 --- /dev/null +++ b/lib/chessh/ssh/server_key.ex @@ -0,0 +1,11 @@ +defmodule Chessh.SSH.ServerKey do + @behaviour :ssh_server_key_api + + def is_auth_key(key, username, _daemon_options) do + Chessh.Auth.KeyAuthenticator.authenticate(username, key) + end + + def host_key(algorithm, daemon_options) do + :ssh_file.host_key(algorithm, daemon_options) + end +end |