aboutsummaryrefslogtreecommitdiff
path: root/priv/policy.erl
diff options
context:
space:
mode:
Diffstat (limited to 'priv/policy.erl')
-rw-r--r--priv/policy.erl39
1 files changed, 39 insertions, 0 deletions
diff --git a/priv/policy.erl b/priv/policy.erl
new file mode 100644
index 0000000..bca21da
--- /dev/null
+++ b/priv/policy.erl
@@ -0,0 +1,39 @@
+%%%-------------------------------------------------------------------
+%%% @copyright (C) 2016, Guido Günther
+%%% @doc
+%%%
+%%% @end
+%%%-------------------------------------------------------------------
+-module(policy).
+
+-export([add_host/3,
+ del_host/2
+ ]).
+
+% -> data to return
+add_host(Hostname, Class, Realm) ->
+ Dn = host2dn(Hostname, Realm),
+ Attrs = class2attr(Hostname, Class, Realm),
+ ok = ldapsp_ldap:add(Dn, Attrs),
+ [{dn, list_to_binary(Dn)},
+ {randompassword, <<"UNSET">>}].
+
+% -> true, false
+del_host(Hostname, Realm) ->
+ Dn = host2dn(Hostname, Realm),
+ del_result(ldapsp_ldap:delete(Dn)).
+
+%% Private functions
+host2dn(Host, Realm) ->
+ Base = string:join([ "dc=" ++ C || C <- string:tokens(Realm, ".")], ", "),
+ "cn=" ++ hd(string:tokens(Host, ".")) ++ ", " ++ Base.
+
+class2attr(Host, _Class, Realm) ->
+ [{"objectclass", ["top", "groupOfUniqueNames"]},
+ {"cn", [Host]},
+ {"uniqueMember", [host2dn(Host, Realm)]}].
+
+del_result({error,noSuchObject}) -> true;
+del_result(ok) -> true;
+del_result(_) -> false.
+