emqx_access_rule.erl 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_access_rule).
  17. %% APIs
  18. -export([ match/3
  19. , compile/1
  20. ]).
  21. -export_type([rule/0]).
  22. -type(acl_result() :: allow | deny).
  23. -type(who() :: all | binary() |
  24. {client, binary()} |
  25. {user, binary()} |
  26. {ipaddr, esockd_cidr:cidr_string()}).
  27. -type(access() :: subscribe | publish | pubsub).
  28. -type(rule() :: {acl_result(), all} |
  29. {acl_result(), who(), access(), list(emqx_topic:topic())}).
  30. -define(ALLOW_DENY(A), ((A =:= allow) orelse (A =:= deny))).
  31. -define(PUBSUB(A), ((A =:= subscribe) orelse (A =:= publish) orelse (A =:= pubsub))).
  32. %% @doc Compile Access Rule.
  33. compile({A, all}) when ?ALLOW_DENY(A) ->
  34. {A, all};
  35. compile({A, Who, Access, Topic}) when ?ALLOW_DENY(A), ?PUBSUB(Access), is_binary(Topic) ->
  36. {A, compile(who, Who), Access, [compile(topic, Topic)]};
  37. compile({A, Who, Access, TopicFilters}) when ?ALLOW_DENY(A), ?PUBSUB(Access) ->
  38. {A, compile(who, Who), Access, [compile(topic, Topic) || Topic <- TopicFilters]}.
  39. compile(who, all) ->
  40. all;
  41. compile(who, {ipaddr, CIDR}) ->
  42. {ipaddr, esockd_cidr:parse(CIDR, true)};
  43. compile(who, {client, all}) ->
  44. {client, all};
  45. compile(who, {client, ClientId}) ->
  46. {client, bin(ClientId)};
  47. compile(who, {user, all}) ->
  48. {user, all};
  49. compile(who, {user, Username}) ->
  50. {user, bin(Username)};
  51. compile(who, {'and', Conds}) when is_list(Conds) ->
  52. {'and', [compile(who, Cond) || Cond <- Conds]};
  53. compile(who, {'or', Conds}) when is_list(Conds) ->
  54. {'or', [compile(who, Cond) || Cond <- Conds]};
  55. compile(topic, {eq, Topic}) ->
  56. {eq, emqx_topic:words(bin(Topic))};
  57. compile(topic, Topic) ->
  58. Words = emqx_topic:words(bin(Topic)),
  59. case pattern(Words) of
  60. true -> {pattern, Words};
  61. false -> Words
  62. end.
  63. pattern(Words) ->
  64. lists:member(<<"%u">>, Words) orelse lists:member(<<"%c">>, Words).
  65. bin(L) when is_list(L) ->
  66. list_to_binary(L);
  67. bin(B) when is_binary(B) ->
  68. B.
  69. %% @doc Match access rule
  70. -spec(match(emqx_types:clientinfo(), emqx_types:topic(), rule())
  71. -> {matched, allow} | {matched, deny} | nomatch).
  72. match(_ClientInfo, _Topic, {AllowDeny, all}) when ?ALLOW_DENY(AllowDeny) ->
  73. {matched, AllowDeny};
  74. match(ClientInfo, Topic, {AllowDeny, Who, _PubSub, TopicFilters})
  75. when ?ALLOW_DENY(AllowDeny) ->
  76. case match_who(ClientInfo, Who)
  77. andalso match_topics(ClientInfo, Topic, TopicFilters) of
  78. true -> {matched, AllowDeny};
  79. false -> nomatch
  80. end.
  81. match_who(_ClientInfo, all) ->
  82. true;
  83. match_who(_ClientInfo, {user, all}) ->
  84. true;
  85. match_who(_ClientInfo, {client, all}) ->
  86. true;
  87. match_who(#{clientid := ClientId}, {client, ClientId}) ->
  88. true;
  89. match_who(#{username := Username}, {user, Username}) ->
  90. true;
  91. match_who(#{peerhost := undefined}, {ipaddr, _Tup}) ->
  92. false;
  93. match_who(#{peerhost := IP}, {ipaddr, CIDR}) ->
  94. esockd_cidr:match(IP, CIDR);
  95. match_who(ClientInfo, {'and', Conds}) when is_list(Conds) ->
  96. lists:foldl(fun(Who, Allow) ->
  97. match_who(ClientInfo, Who) andalso Allow
  98. end, true, Conds);
  99. match_who(ClientInfo, {'or', Conds}) when is_list(Conds) ->
  100. lists:foldl(fun(Who, Allow) ->
  101. match_who(ClientInfo, Who) orelse Allow
  102. end, false, Conds);
  103. match_who(_ClientInfo, _Who) ->
  104. false.
  105. match_topics(_ClientInfo, _Topic, []) ->
  106. false;
  107. match_topics(ClientInfo, Topic, [{pattern, PatternFilter}|Filters]) ->
  108. TopicFilter = feed_var(ClientInfo, PatternFilter),
  109. match_topic(emqx_topic:words(Topic), TopicFilter)
  110. orelse match_topics(ClientInfo, Topic, Filters);
  111. match_topics(ClientInfo, Topic, [TopicFilter|Filters]) ->
  112. match_topic(emqx_topic:words(Topic), TopicFilter)
  113. orelse match_topics(ClientInfo, Topic, Filters).
  114. match_topic(Topic, {eq, TopicFilter}) ->
  115. Topic == TopicFilter;
  116. match_topic(Topic, TopicFilter) ->
  117. emqx_topic:match(Topic, TopicFilter).
  118. feed_var(ClientInfo, Pattern) ->
  119. feed_var(ClientInfo, Pattern, []).
  120. feed_var(_ClientInfo, [], Acc) ->
  121. lists:reverse(Acc);
  122. feed_var(ClientInfo = #{clientid := undefined}, [<<"%c">>|Words], Acc) ->
  123. feed_var(ClientInfo, Words, [<<"%c">>|Acc]);
  124. feed_var(ClientInfo = #{clientid := ClientId}, [<<"%c">>|Words], Acc) ->
  125. feed_var(ClientInfo, Words, [ClientId |Acc]);
  126. feed_var(ClientInfo = #{username := undefined}, [<<"%u">>|Words], Acc) ->
  127. feed_var(ClientInfo, Words, [<<"%u">>|Acc]);
  128. feed_var(ClientInfo = #{username := Username}, [<<"%u">>|Words], Acc) ->
  129. feed_var(ClientInfo, Words, [Username|Acc]);
  130. feed_var(ClientInfo, [W|Words], Acc) ->
  131. feed_var(ClientInfo, Words, [W|Acc]).