|
@@ -12,16 +12,34 @@
|
|
-type connect_error() ::
|
|
-type connect_error() ::
|
|
invalid_authorization_specification
|
|
invalid_authorization_specification
|
|
| invalid_password
|
|
| invalid_password
|
|
- | {unsupported_auth_method, integer()}
|
|
|
|
|
|
+ | {unsupported_auth_method,
|
|
|
|
+ kerberosV5 | crypt | scm | gss | sspi | {unknown, integer()} | {sasl, [binary()]}}
|
|
|
|
+ | {sasl_server_final, any()}
|
|
| epgsql:query_error().
|
|
| epgsql:query_error().
|
|
|
|
|
|
-include("epgsql.hrl").
|
|
-include("epgsql.hrl").
|
|
-include("protocol.hrl").
|
|
-include("protocol.hrl").
|
|
|
|
|
|
|
|
+-type auth_fun() :: fun((init | binary(), _, _) ->
|
|
|
|
+ {send, byte(), iodata(), any()}
|
|
|
|
+ | ok
|
|
|
|
+ | {error, any()}
|
|
|
|
+ | unknown).
|
|
|
|
+
|
|
-record(connect,
|
|
-record(connect,
|
|
{opts :: list(),
|
|
{opts :: list(),
|
|
- auth_method,
|
|
|
|
- stage = connect :: connect | auth | initialization}).
|
|
|
|
|
|
+ auth_fun :: auth_fun() | undefined,
|
|
|
|
+ auth_state :: any() | undefined,
|
|
|
|
+ auth_send :: {integer(), iodata()} | undefined,
|
|
|
|
+ stage = connect :: connect | maybe_auth | auth | initialization}).
|
|
|
|
+
|
|
|
|
+-define(SCRAM_AUTH_METHOD, <<"SCRAM-SHA-256">>).
|
|
|
|
+-define(AUTH_OK, 0).
|
|
|
|
+-define(AUTH_CLEARTEXT, 3).
|
|
|
|
+-define(AUTH_MD5, 5).
|
|
|
|
+-define(AUTH_SASL, 10).
|
|
|
|
+-define(AUTH_SASL_CONTINUE, 11).
|
|
|
|
+-define(AUTH_SASL_FINAL, 12).
|
|
|
|
|
|
init({Host, Username, Password, Opts}) ->
|
|
init({Host, Username, Password, Opts}) ->
|
|
Opts1 = [{host, Host},
|
|
Opts1 = [{host, Host},
|
|
@@ -73,21 +91,13 @@ execute(PgSock, #connect{opts = Opts, stage = connect} = State) ->
|
|
undefined -> PgSock2;
|
|
undefined -> PgSock2;
|
|
Async -> epgsql_sock:set_attr(async, Async, PgSock2)
|
|
Async -> epgsql_sock:set_attr(async, Async, PgSock2)
|
|
end,
|
|
end,
|
|
- {ok, PgSock3, State#connect{stage = auth}};
|
|
|
|
|
|
+ {ok, PgSock3, State#connect{stage = maybe_auth}};
|
|
{error, Reason} = Error ->
|
|
{error, Reason} = Error ->
|
|
{stop, Reason, Error, PgSock}
|
|
{stop, Reason, Error, PgSock}
|
|
end;
|
|
end;
|
|
-execute(PgSock, #connect{stage = auth, auth_method = cleartext, opts = Opts} = St) ->
|
|
|
|
- Password = get_val(password, Opts),
|
|
|
|
- epgsql_sock:send(PgSock, ?PASSWORD, [Password, 0]),
|
|
|
|
- {ok, PgSock, St};
|
|
|
|
-execute(PgSock, #connect{stage = auth, auth_method = {md5, Salt}, opts = Opts} = St) ->
|
|
|
|
- User = get_val(username, Opts),
|
|
|
|
- Password = get_val(password, Opts),
|
|
|
|
- Digest1 = hex(erlang:md5([Password, User])),
|
|
|
|
- Str = ["md5", hex(erlang:md5([Digest1, Salt])), 0],
|
|
|
|
- epgsql_sock:send(PgSock, ?PASSWORD, Str),
|
|
|
|
- {ok, PgSock, St}.
|
|
|
|
|
|
+execute(PgSock, #connect{stage = auth, auth_send = {PacketId, Data}} = St) ->
|
|
|
|
+ epgsql_sock:send(PgSock, PacketId, Data),
|
|
|
|
+ {ok, PgSock, St#connect{auth_send = undefined}}.
|
|
|
|
|
|
|
|
|
|
maybe_ssl(S, false, _, PgSock) ->
|
|
maybe_ssl(S, false, _, PgSock) ->
|
|
@@ -114,30 +124,98 @@ maybe_ssl(S, Flag, Opts, PgSock) ->
|
|
end
|
|
end
|
|
end.
|
|
end.
|
|
|
|
|
|
-%% --- Auth ---
|
|
|
|
-
|
|
|
|
-%% AuthenticationOk
|
|
|
|
-handle_message(?AUTHENTICATION_REQUEST, <<0:?int32>>, Sock, State) ->
|
|
|
|
- {noaction, Sock, State#connect{stage = initialization}};
|
|
|
|
|
|
+%% Auth sub-protocol
|
|
|
|
+
|
|
|
|
+auth_init(<<?AUTH_CLEARTEXT:?int32>>, Sock, St) ->
|
|
|
|
+ auth_init(fun auth_cleartext/3, undefined, Sock, St);
|
|
|
|
+auth_init(<<?AUTH_MD5:?int32, Salt:4/binary>>, Sock, St) ->
|
|
|
|
+ auth_init(fun auth_md5/3, Salt, Sock, St);
|
|
|
|
+auth_init(<<?AUTH_SASL:?int32, MethodsB/binary>>, Sock, St) ->
|
|
|
|
+ Methods = epgsql_wire:decode_strings(MethodsB),
|
|
|
|
+ case lists:member(?SCRAM_AUTH_METHOD, Methods) of
|
|
|
|
+ true ->
|
|
|
|
+ auth_init(fun auth_scram/3, undefined, Sock, St);
|
|
|
|
+ false ->
|
|
|
|
+ {stop, normal, {error, {unsupported_auth_method,
|
|
|
|
+ {sasl, lists:delete(<<>>, Methods)}}}}
|
|
|
|
+ end;
|
|
|
|
+auth_init(<<M:?int32, _/binary>>, Sock, _St) ->
|
|
|
|
+ Method = case M of
|
|
|
|
+ 2 -> kerberosV5;
|
|
|
|
+ 4 -> crypt;
|
|
|
|
+ 6 -> scm;
|
|
|
|
+ 7 -> gss;
|
|
|
|
+ 8 -> sspi;
|
|
|
|
+ _ -> {unknown, M}
|
|
|
|
+ end,
|
|
|
|
+ {stop, normal, {error, {unsupported_auth_method, Method}}, Sock}.
|
|
|
|
+
|
|
|
|
+auth_init(Fun, InitState, PgSock, St) ->
|
|
|
|
+ auth_handle(init, PgSock, St#connect{auth_fun = Fun, auth_state = InitState,
|
|
|
|
+ stage = auth}).
|
|
|
|
+
|
|
|
|
+auth_handle(Data, PgSock, #connect{auth_fun = Fun, auth_state = AuthSt} = St) ->
|
|
|
|
+ case Fun(Data, AuthSt, St) of
|
|
|
|
+ {send, SendPacketId, SendData, AuthSt1} ->
|
|
|
|
+ {requeue, PgSock, St#connect{auth_state = AuthSt1,
|
|
|
|
+ auth_send = {SendPacketId, SendData}}};
|
|
|
|
+ ok -> {noaction, PgSock, St};
|
|
|
|
+ {error, Reason} ->
|
|
|
|
+ {stop, normal, {error, Reason}};
|
|
|
|
+ unknown -> unknown
|
|
|
|
+ end.
|
|
|
|
|
|
%% AuthenticationCleartextPassword
|
|
%% AuthenticationCleartextPassword
|
|
-handle_message(?AUTHENTICATION_REQUEST, <<3:?int32>>, Sock, St) ->
|
|
|
|
- {requeue, Sock, St#connect{stage = auth, auth_method = cleartext}};
|
|
|
|
|
|
+auth_cleartext(init, _AuthState, #connect{opts = Opts}) ->
|
|
|
|
+ Password = get_val(password, Opts),
|
|
|
|
+ {send, ?PASSWORD, [Password, 0], undefined};
|
|
|
|
+auth_cleartext(_, _, _) -> unknown.
|
|
|
|
|
|
%% AuthenticationMD5Password
|
|
%% AuthenticationMD5Password
|
|
-handle_message(?AUTHENTICATION_REQUEST, <<5:?int32, Salt:4/binary>>, Sock, St) ->
|
|
|
|
- {requeue, Sock, St#connect{stage = auth, auth_method = {md5, Salt}}};
|
|
|
|
|
|
+auth_md5(init, Salt, #connect{opts = Opts}) ->
|
|
|
|
+ User = get_val(username, Opts),
|
|
|
|
+ Password = get_val(password, Opts),
|
|
|
|
+ Digest1 = hex(erlang:md5([Password, User])),
|
|
|
|
+ Str = ["md5", hex(erlang:md5([Digest1, Salt])), 0],
|
|
|
|
+ {send, ?PASSWORD, Str, undefined};
|
|
|
|
+auth_md5(_, _, _) -> unknown.
|
|
|
|
|
|
-handle_message(?AUTHENTICATION_REQUEST, <<M:?int32, _/binary>>, Sock, _State) ->
|
|
|
|
- Method = case M of
|
|
|
|
- 2 -> kerberosV5;
|
|
|
|
- 4 -> crypt;
|
|
|
|
- 6 -> scm;
|
|
|
|
- 7 -> gss;
|
|
|
|
- 8 -> sspi;
|
|
|
|
- _ -> unknown
|
|
|
|
- end,
|
|
|
|
- {stop, normal, {error, {unsupported_auth_method, Method}}, Sock};
|
|
|
|
|
|
+%% AuthenticationSASL
|
|
|
|
+auth_scram(init, undefined, #connect{opts = Opts}) ->
|
|
|
|
+ User = get_val(username, Opts),
|
|
|
|
+ Nonce = epgsql_scram:get_nonce(16),
|
|
|
|
+ ClientFirst = epgsql_scram:get_client_first(User, Nonce),
|
|
|
|
+ SaslInitialResponse = [?SCRAM_AUTH_METHOD, 0, <<(iolist_size(ClientFirst)):?int32>>, ClientFirst],
|
|
|
|
+ {send, ?SASL_ANY_RESPONSE, SaslInitialResponse, {auth_request, Nonce}};
|
|
|
|
+auth_scram(<<?AUTH_SASL_CONTINUE:?int32, ServerFirst/binary>>, {auth_request, Nonce}, #connect{opts = Opts}) ->
|
|
|
|
+ User = get_val(username, Opts),
|
|
|
|
+ Password = get_val(password, Opts),
|
|
|
|
+ ServerFirstParts = epgsql_scram:parse_server_first(ServerFirst, Nonce),
|
|
|
|
+ {ClientFinalMessage, ServerProof} = epgsql_scram:get_client_final(ServerFirstParts, Nonce, User, Password),
|
|
|
|
+ {send, ?SASL_ANY_RESPONSE, ClientFinalMessage, {server_final, ServerProof}};
|
|
|
|
+auth_scram(<<?AUTH_SASL_FINAL:?int32, ServerFinalMsg/binary>>, {server_final, ServerProof}, _Conn) ->
|
|
|
|
+ case epgsql_scram:parse_server_final(ServerFinalMsg) of
|
|
|
|
+ {ok, ServerProof} -> ok;
|
|
|
|
+ Other -> {error, {sasl_server_final, Other}}
|
|
|
|
+ end;
|
|
|
|
+auth_scram(_, _, _) ->
|
|
|
|
+ unknown.
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+%% --- Auth ---
|
|
|
|
+
|
|
|
|
+%% AuthenticationOk
|
|
|
|
+handle_message(?AUTHENTICATION_REQUEST, <<?AUTH_OK:?int32>>, Sock, State) ->
|
|
|
|
+ {noaction, Sock, State#connect{stage = initialization,
|
|
|
|
+ auth_fun = undefined,
|
|
|
|
+ auth_state = undefned,
|
|
|
|
+ auth_send = undefined}};
|
|
|
|
+
|
|
|
|
+handle_message(?AUTHENTICATION_REQUEST, Message, Sock, #connect{stage = Stage} = St) when Stage =/= auth ->
|
|
|
|
+ auth_init(Message, Sock, St);
|
|
|
|
+
|
|
|
|
+handle_message(?AUTHENTICATION_REQUEST, Packet, Sock, #connect{stage = auth} = St) ->
|
|
|
|
+ auth_handle(Packet, Sock, St);
|
|
|
|
|
|
%% --- Initialization ---
|
|
%% --- Initialization ---
|
|
|
|
|
|
@@ -153,7 +231,8 @@ handle_message(?READY_FOR_QUERY, _, Sock, _State) ->
|
|
|
|
|
|
|
|
|
|
%% ErrorResponse
|
|
%% ErrorResponse
|
|
-handle_message(?ERROR, Err, Sock, #connect{stage = auth} = _State) ->
|
|
|
|
|
|
+handle_message(?ERROR, Err, Sock, #connect{stage = Stage} = _State) when Stage == auth;
|
|
|
|
+ Stage == maybe_auth ->
|
|
Why = case Err#error.code of
|
|
Why = case Err#error.code of
|
|
<<"28000">> -> invalid_authorization_specification;
|
|
<<"28000">> -> invalid_authorization_specification;
|
|
<<"28P01">> -> invalid_password;
|
|
<<"28P01">> -> invalid_password;
|