emqttd_acl_internal.erl 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2013-2018 EMQ Enterprise, Inc. (http://emqtt.io)
  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(emqttd_acl_internal).
  17. -behaviour(emqttd_acl_mod).
  18. -author("Feng Lee <feng@emqtt.io>").
  19. -include("emqttd.hrl").
  20. -include("emqttd_cli.hrl").
  21. -export([all_rules/0]).
  22. %% ACL callbacks
  23. -export([init/1, check_acl/2, reload_acl/1, description/0]).
  24. -define(ACL_RULE_TAB, mqtt_acl_rule).
  25. -record(state, {config}).
  26. %%--------------------------------------------------------------------
  27. %% API
  28. %%--------------------------------------------------------------------
  29. %% @doc Read all rules
  30. -spec(all_rules() -> list(emqttd_access_rule:rule())).
  31. all_rules() ->
  32. case ets:lookup(?ACL_RULE_TAB, all_rules) of
  33. [] -> [];
  34. [{_, Rules}] -> Rules
  35. end.
  36. %%--------------------------------------------------------------------
  37. %% ACL callbacks
  38. %%--------------------------------------------------------------------
  39. %% @doc Init internal ACL
  40. -spec(init([File :: string()]) -> {ok, State :: any()}).
  41. init([File]) ->
  42. ets:new(?ACL_RULE_TAB, [set, public, named_table, {read_concurrency, true}]),
  43. State = #state{config = File},
  44. true = load_rules_from_file(State),
  45. {ok, State}.
  46. load_rules_from_file(#state{config = AclFile}) ->
  47. {ok, Terms} = file:consult(AclFile),
  48. Rules = [emqttd_access_rule:compile(Term) || Term <- Terms],
  49. lists:foreach(fun(PubSub) ->
  50. ets:insert(?ACL_RULE_TAB, {PubSub,
  51. lists:filter(fun(Rule) -> filter(PubSub, Rule) end, Rules)})
  52. end, [publish, subscribe]),
  53. ets:insert(?ACL_RULE_TAB, {all_rules, Terms}).
  54. filter(_PubSub, {allow, all}) ->
  55. true;
  56. filter(_PubSub, {deny, all}) ->
  57. true;
  58. filter(publish, {_AllowDeny, _Who, publish, _Topics}) ->
  59. true;
  60. filter(_PubSub, {_AllowDeny, _Who, pubsub, _Topics}) ->
  61. true;
  62. filter(subscribe, {_AllowDeny, _Who, subscribe, _Topics}) ->
  63. true;
  64. filter(_PubSub, {_AllowDeny, _Who, _, _Topics}) ->
  65. false.
  66. %% @doc Check ACL
  67. -spec(check_acl({Client, PubSub, Topic}, State) -> allow | deny | ignore when
  68. Client :: mqtt_client(),
  69. PubSub :: pubsub(),
  70. Topic :: binary(),
  71. State :: #state{}).
  72. check_acl(_Who, #state{config = undefined}) ->
  73. allow;
  74. check_acl({Client, PubSub, Topic}, #state{}) ->
  75. case match(Client, Topic, lookup(PubSub)) of
  76. {matched, allow} -> allow;
  77. {matched, deny} -> deny;
  78. nomatch -> ignore
  79. end.
  80. lookup(PubSub) ->
  81. case ets:lookup(?ACL_RULE_TAB, PubSub) of
  82. [] -> [];
  83. [{PubSub, Rules}] -> Rules
  84. end.
  85. match(_Client, _Topic, []) ->
  86. nomatch;
  87. match(Client, Topic, [Rule|Rules]) ->
  88. case emqttd_access_rule:match(Client, Topic, Rule) of
  89. nomatch -> match(Client, Topic, Rules);
  90. {matched, AllowDeny} -> {matched, AllowDeny}
  91. end.
  92. %% @doc Reload ACL
  93. -spec(reload_acl(State :: #state{}) -> ok | {error, Reason :: any()}).
  94. reload_acl(#state{config = undefined}) ->
  95. ok;
  96. reload_acl(State) ->
  97. case catch load_rules_from_file(State) of
  98. {'EXIT', Error} -> {error, Error};
  99. true -> ?PRINT("~s~n", ["reload acl_internal successfully"]), ok
  100. end.
  101. %% @doc ACL Module Description
  102. -spec(description() -> string()).
  103. description() ->
  104. "Internal ACL with etc/acl.conf".