2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
10 local hosts = _G.hosts;
11 local st = require "util.stanza";
12 local datamanager = require "util.datamanager";
13 local usermanager_user_exists = require "core.usermanager".user_exists;
14 local usermanager_create_user = require "core.usermanager".create_user;
15 local usermanager_set_password = require "core.usermanager".set_password;
16 local usermanager_delete_user = require "core.usermanager".delete_user;
17 local os_time = os.time;
18 local nodeprep = require "util.encodings".stringprep.nodeprep;
19 local jid_bare = require "util.jid".bare;
21 local compat = module:get_option_boolean("registration_compat", true);
23 module:add_feature("jabber:iq:register");
25 local function handle_registration_stanza(event)
26 local session, stanza = event.origin, event.stanza;
28 local query = stanza.tags[1];
29 if stanza.attr.type == "get" then
30 local reply = st.reply(stanza);
31 reply:tag("query", {xmlns = "jabber:iq:register"})
32 :tag("registered"):up()
33 :tag("username"):text(session.username):up()
34 :tag("password"):up();
36 else -- stanza.attr.type == "set"
37 if query.tags[1] and query.tags[1].name == "remove" then
38 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
39 local username, host = session.username, session.host;
41 local ok, err = usermanager_delete_user(username, host);
44 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
45 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
49 session.send(st.reply(stanza));
50 local roster = session.roster;
51 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
52 session:close({condition = "not-authorized", text = "Account deleted"});
54 -- TODO datamanager should be able to delete all user data itself
55 datamanager.store(username, host, "vcard", nil);
56 datamanager.store(username, host, "private", nil);
57 datamanager.list_store(username, host, "offline", nil);
58 local bare = username.."@"..host;
59 for jid, item in pairs(roster) do
60 if jid and jid ~= "pending" then
61 if item.subscription == "both" or item.subscription == "from" or (roster.pending and roster.pending[jid]) then
62 core_post_stanza(hosts[host], st.presence({type="unsubscribed", from=bare, to=jid}));
64 if item.subscription == "both" or item.subscription == "to" or item.ask then
65 core_post_stanza(hosts[host], st.presence({type="unsubscribe", from=bare, to=jid}));
69 datamanager.store(username, host, "roster", nil);
70 datamanager.store(username, host, "privacy", nil);
71 module:log("info", "User removed their account: %s@%s", username, host);
72 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
74 local username = nodeprep(query:get_child("username"):get_text());
75 local password = query:get_child("password"):get_text();
76 if username and password then
77 if username == session.username then
78 if usermanager_set_password(username, password, session.host) then
79 session.send(st.reply(stanza));
81 -- TODO unable to write file, file may be locked, etc, what's the correct error?
82 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
85 session.send(st.error_reply(stanza, "modify", "bad-request"));
88 session.send(st.error_reply(stanza, "modify", "bad-request"));
95 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
97 module:hook("iq/host/jabber:iq:register:query", function (event)
98 local session, stanza = event.origin, event.stanza;
99 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
100 return handle_registration_stanza(event);
105 local recent_ips = {};
106 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
107 local whitelist_only = module:get_option("whitelist_registration_only");
108 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
109 local blacklisted_ips = module:get_option("registration_blacklist") or {};
111 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
112 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
114 module:hook("stanza/iq/jabber:iq:register:query", function(event)
115 local session, stanza = event.origin, event.stanza;
117 if module:get_option("allow_registration") == false or session.type ~= "c2s_unauthed" then
118 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
120 local query = stanza.tags[1];
121 if stanza.attr.type == "get" then
122 local reply = st.reply(stanza);
123 reply:tag("query", {xmlns = "jabber:iq:register"})
124 :tag("instructions"):text("Choose a username and password for use with this service."):up()
125 :tag("username"):up()
126 :tag("password"):up();
128 elseif stanza.attr.type == "set" then
129 if query.tags[1] and query.tags[1].name == "remove" then
130 session.send(st.error_reply(stanza, "auth", "registration-required"));
132 local username = query:child_with_name("username");
133 local password = query:child_with_name("password");
134 if username and password then
135 -- Check that the user is not blacklisted or registering too often
136 if not session.ip then
137 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
138 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
139 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
141 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
142 if not recent_ips[session.ip] then
143 recent_ips[session.ip] = { time = os_time(), count = 1 };
145 local ip = recent_ips[session.ip];
146 ip.count = ip.count + 1;
148 if os_time() - ip.time < min_seconds_between_registrations then
150 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
156 -- FIXME shouldn't use table.concat
157 username = nodeprep(table.concat(username));
158 password = table.concat(password);
159 local host = module.host;
160 if not username or username == "" then
161 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
162 elseif usermanager_user_exists(username, host) then
163 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
165 if usermanager_create_user(username, password, host) then
166 session.send(st.reply(stanza)); -- user created!
167 module:log("info", "User account created: %s@%s", username, host);
168 module:fire_event("user-registered", {
169 username = username, host = host, source = "mod_register",
170 session = session });
172 -- TODO unable to write file, file may be locked, etc, what's the correct error?
173 session.send(st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk."));
177 session.send(st.error_reply(stanza, "modify", "not-acceptable"));