Page Menu
Home
Phorge
Search
Configure Global Search
Log In
Files
F140712
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
View Options
diff --git a/debug_server.exs b/debug_server.exs
index 5541529..1de1e63 100644
--- a/debug_server.exs
+++ b/debug_server.exs
@@ -1,66 +1,72 @@
require Logger
Application.ensure_started(:cacophony)
Application.ensure_all_started(:ranch)
alias Cacophony.Message
defmodule DebugServer do
- def handle_message(%Message.BindRequest{} = request) do
+ def handle_message(%Message.BindRequest{} = request, state) do
+ bind_dn = request.dn != "" && request.dn || "cn=anonymous"
+ state = put_in(state, [:dn], bind_dn)
+
response = %Message.BindResponse{
id: request.id,
result_code: :success,
- matched_cn: "cn=kaniini"
+ matched_dn: state.dn
}
- {:ok, response}
+ {:ok, response, state}
end
- def handle_message(%Message.UnbindRequest{}), do: :noreply
+ def handle_message(%Message.UnbindRequest{}, _state), do: :noreply
+
+ def handle_message(%Message.WhoAmIRequest{} = request, %{dn: dn} = state) do
+ "cn=" <> authzid = dn
- def handle_message(%Message.WhoAmIRequest{} = request) do
response = %Message.WhoAmIResponse{
id: request.id,
+ matched_dn: dn,
result_code: :success,
- authzid: "u:kaniini@example.com"
+ authzid: "#{authzid}@example.com"
}
- {:ok, response}
+ {:ok, response, state}
end
def handle_message(_message), do: {:error, :badmatch}
end
defmodule DebugListener do
def start_link(_opts) do
:ranch.start_listener(make_ref(), :ranch_tcp, [{:port, 6389}], Cacophony.Server, [mod: DebugServer])
end
def child_spec(_) do
%{id: __MODULE__, start: {__MODULE__, :start_link, [[]]}}
end
end
defmodule DebugSupervisor do
use Supervisor
def start_link(opts) do
Supervisor.start_link(__MODULE__, :ok, opts)
end
@impl true
def init(:ok) do
children = [DebugListener]
Supervisor.init(children, strategy: :one_for_one)
end
end
{:ok, pid} = DebugSupervisor.start_link([])
ref = Process.monitor(pid)
Logger.info("Debugging server started on port 6389.")
receive do
{:DOWN, ^ref, _, _, _} -> Process.exit()
end
diff --git a/lib/cacophony/server.ex b/lib/cacophony/server.ex
index 339269b..f07c62d 100644
--- a/lib/cacophony/server.ex
+++ b/lib/cacophony/server.ex
@@ -1,64 +1,64 @@
defmodule Cacophony.Server do
@moduledoc """
A simple ranch protocol handler that services LDAP requests and dispatches
them to a handler module.
"""
@behaviour :ranch_protocol
require Logger
alias Cacophony.Message
def start_link(ref, socket, transport, mod: mod) do
pid = :proc_lib.spawn_link(__MODULE__, :init, [ref, socket, transport, mod])
{:ok, pid}
end
def init(ref, _, transport, mod) do
{:ok, socket} = :ranch.handshake(ref)
:ok = transport.setopts(socket, [{:active, true}])
Logger.debug("Opening connection: #{inspect(socket)}")
:gen_server.enter_loop(__MODULE__, [], %{
ref: ref,
socket: socket,
transport: transport,
mod: mod
})
end
def handle_info({:tcp, socket, data}, %{socket: socket, transport: transport, mod: mod} = state) do
{:ok, %{} = msg} = Message.decode(data)
- Logger.debug("Incoming message: #{inspect(msg)}")
+ Logger.debug("Incoming message: #{inspect(msg)} [state #{inspect(state)}]")
- case mod.handle_message(msg) do
- {:ok, reply} ->
+ case mod.handle_message(msg, state) do
+ {:ok, reply, new_state} ->
{:ok, bin} = Message.encode(reply)
transport.send(socket, bin)
- {:noreply, state}
+ {:noreply, new_state}
:noreply ->
{:noreply, state}
{:error, e} ->
Logger.error("While processing message #{inspect(msg)}, #{inspect(e)} occured.")
transport.close(socket)
{:stop, :normal, state}
end
end
def handle_info({:tcp_closed, socket}, %{socket: socket, transport: transport} = state) do
Logger.debug("Closing connection: #{inspect(socket)}")
transport.close(socket)
{:stop, :normal, state}
end
def handle_info({:tcp_error, socket}, %{socket: socket} = state) do
Logger.debug("Closing connection: #{inspect(socket)}")
{:stop, :normal, state}
end
end
File Metadata
Details
Attached
Mime Type
text/x-diff
Expires
Tue, Jan 21, 6:06 AM (1 d, 4 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
55666
Default Alt Text
(4 KB)
Attached To
Mode
R11 cacophony
Attached
Detach File
Event Timeline
Log In to Comment