-
Notifications
You must be signed in to change notification settings - Fork 6
/
rabbit_auth_backend_ip_range.erl
133 lines (108 loc) · 4.28 KB
/
rabbit_auth_backend_ip_range.erl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
%% The contents of this file are subject to the Mozilla Public License
%% Version 1.1 (the "License"); you may not use this file except in
%% compliance with the License. You may obtain a copy of the License
%% at http://www.mozilla.org/MPL/
%%
%% Software distributed under the License is distributed on an "AS IS"
%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
%% the License for the specific language governing rights and
%% limitations under the License.
%%
%% Copyright (C) 2014 Petr Gotthard <petr.gotthard@centrum.cz>
%%
-module(rabbit_auth_backend_ip_range).
-behaviour(rabbit_authz_backend).
-include_lib("rabbit_common/include/rabbit.hrl").
-export([description/0]).
-export([user_login_authorization/1, user_login_authorization/2,
check_vhost_access/3, check_resource_access/4, check_topic_access/4,
state_can_expire/0]).
description() ->
[{name, <<"IP_Range">>},
{description, <<"IP based client authorization">>}].
user_login_authorization(_Username) ->
{ok, none}.
user_login_authorization(_Username, _AuthProps) ->
{ok, none}.
check_vhost_access(#auth_user{tags = Tags}, _VHostPath, AuthzData) ->
Address = extract_address(AuthzData),
% filter out applicable masks
case lists:filtermap(
fun({Tag, Masks}) ->
case lists:member(Tag, Tags) of
true -> {true, Masks};
false -> false
end
end, env(tag_masks)) of
[] -> check_masks(Address, env(default_masks));
Else -> check_masks(Address, lists:flatten(Else))
end.
check_masks(undefined, _Masks) -> true; % allow internal access
check_masks(unknown, _Masks) -> true; % allow internal access
check_masks(Address, Masks) ->
R = lists:foldl(
fun(StrMask, false) ->
{Mask, Bits} = compile_addrmask(StrMask),
Addr = address_to_binary(Address, Bits),
if
Addr == Mask -> true;
true -> false
end;
(_, true) -> true
end, false, Masks),
if
R == false ->
rabbit_log:warning("Address ~s not matching any of [ ~s]~n",
[inet_parse:ntoa(Address), format_masks(Masks)]),
false;
true ->
rabbit_log:debug("Address ~s matching [ ~s]~n",
[inet_parse:ntoa(Address), format_masks(Masks)]),
true
end.
check_resource_access(#auth_user{}, #resource{}, _Permission, _Context) -> true.
check_topic_access(#auth_user{}, #resource{}, _Permission, _Context) -> true.
state_can_expire() -> false.
%%--------------------------------------------------------------------
env(F) ->
{ok, V} = application:get_env(rabbitmq_auth_backend_ip_range, F),
V.
extract_address(undefined) ->
undefined;
extract_address(unknown) ->
unknown;
extract_address(#{peeraddr := Address}) ->
Address.
format_masks(Masks) ->
lists:flatten(lists:foldr(fun(Mask, Acc) ->
[io_lib:format("~s ", [Mask]) | Acc] end, [], Masks)).
compile_addrmask(AddrMask) ->
{ABin, ABits} =
case string:tokens(binary_to_list(AddrMask), "/\\") of
[Addr] ->
compile_address(Addr);
[Addr, Bits] ->
{Addr2, _} = compile_address(Addr),
{Addr2, list_to_integer(Bits)}
end,
{address_to_binary(ABin, ABits), ABits}.
compile_address(Addr) ->
case inet:parse_address(Addr) of
{ok, Address4} when size(Address4) == 4 -> {Address4, 32};
{ok, Address6} when size(Address6) == 8 -> {Address6, 128};
{error, _} -> throw({error, einval})
end.
address_to_binary({B1, B2, B3, B4}, 32) ->
<<B1:8, B2:8, B3:8, B4:8>>;
address_to_binary({B1, B2, B3, B4}, Bits) when Bits < 32 ->
<<Subset:Bits/bitstring, _Others/bitstring>> = <<B1:8, B2:8, B3:8, B4:8>>,
Subset;
address_to_binary({B1, B2, B3, B4}, Bits) when Bits > 32 ->
% matching the IPv4-mapped IPv6 address
<<Subset:Bits/bitstring, _Others/bitstring>> = <<0:80, 16#FFFF:16, B1:8, B2:8, B3:8, B4:8>>,
Subset;
address_to_binary({W1, W2, W3, W4, W5, W6, W7, W8}, Bits) ->
<<Subset:Bits/bitstring, _Others/bitstring>> = <<W1:16, W2:16, W3:16, W4:16, W5:16, W6:16, W7:16, W8:16>>,
Subset;
address_to_binary(_, 0) -> <<0>>.
%% end of file