2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 config = require "core.configmanager";
13 local datamanager = require "util.datamanager";
14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_create_user = require "core.usermanager".create_user;
16 local datamanager_store = require "util.datamanager".store;
17 local os_time = os.time;
18 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 module:add_feature("jabber:iq:register");
22 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
23 if stanza.tags[1].name == "query" then
24 local query = stanza.tags[1];
25 if stanza.attr.type == "get" then
26 local reply = st.reply(stanza);
27 reply:tag("query", {xmlns = "jabber:iq:register"})
28 :tag("registered"):up()
29 :tag("username"):text(session.username):up()
30 :tag("password"):up();
32 elseif stanza.attr.type == "set" then
33 if query.tags[1] and query.tags[1].name == "remove" then
34 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
35 local username, host = session.username, session.host;
36 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
38 usermanager_create_user(username, nil, host); -- Disable account
39 -- FIXME the disabling currently allows a different user to recreate the account
40 -- we should add an in-memory account block mode when we have threading
41 session.send(st.reply(stanza));
42 local roster = session.roster;
43 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
44 session:close({condition = "not-authorized", text = "Account deleted"});
46 -- TODO datamanager should be able to delete all user data itself
47 datamanager.store(username, host, "roster", nil);
48 datamanager.store(username, host, "vcard", nil);
49 datamanager.store(username, host, "private", nil);
50 datamanager.store(username, host, "offline", nil);
51 --local bare = username.."@"..host;
52 for jid, item in pairs(roster) do
53 if jid ~= "pending" then
54 if item.subscription == "both" or item.subscription == "to" then
57 if item.subscription == "both" or item.subscription == "from" then
62 datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
63 module:log("info", "User removed their account: %s@%s", username, host);
64 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register" });
66 local username = query:child_with_name("username");
67 local password = query:child_with_name("password");
68 if username and password then
69 -- FIXME shouldn't use table.concat
70 username = nodeprep(table.concat(username));
71 password = table.concat(password);
72 if username == session.username then
73 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
74 session.send(st.reply(stanza));
76 -- TODO unable to write file, file may be locked, etc, what's the correct error?
77 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
80 session.send(st.error_reply(stanza, "modify", "bad-request"));
83 session.send(st.error_reply(stanza, "modify", "bad-request"));
88 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
92 local recent_ips = {};
93 local min_seconds_between_registrations = config.get(module.host, "core", "min_seconds_between_registrations");
94 local whitelist_only = config.get(module.host, "core", "whitelist_registration_only");
95 local whitelisted_ips = config.get(module.host, "core", "registration_whitelist") or { "127.0.0.1" };
96 local blacklisted_ips = config.get(module.host, "core", "registration_blacklist") or {};
98 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
99 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
101 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
102 if config.get(module.host, "core", "allow_registration") == false then
103 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
104 elseif stanza.tags[1].name == "query" then
105 local query = stanza.tags[1];
106 if stanza.attr.type == "get" then
107 local reply = st.reply(stanza);
108 reply:tag("query", {xmlns = "jabber:iq:register"})
109 :tag("instructions"):text("Choose a username and password for use with this service."):up()
110 :tag("username"):up()
111 :tag("password"):up();
113 elseif stanza.attr.type == "set" then
114 if query.tags[1] and query.tags[1].name == "remove" then
115 session.send(st.error_reply(stanza, "auth", "registration-required"));
117 local username = query:child_with_name("username");
118 local password = query:child_with_name("password");
119 if username and password then
120 -- Check that the user is not blacklisted or registering too often
121 if blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
122 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
124 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
125 if not recent_ips[session.ip] then
126 recent_ips[session.ip] = { time = os_time(), count = 1 };
129 local ip = recent_ips[session.ip];
130 ip.count = ip.count + 1;
132 if os_time() - ip.time < min_seconds_between_registrations then
134 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
140 -- FIXME shouldn't use table.concat
141 username = nodeprep(table.concat(username));
142 password = table.concat(password);
143 if usermanager_user_exists(username, session.host) then
144 session.send(st.error_reply(stanza, "cancel", "conflict"));
146 if usermanager_create_user(username, password, session.host) then
147 session.send(st.reply(stanza)); -- user created!
148 module:log("info", "User account created: %s@%s", username, session.host);
149 module:fire_event("user-registered", {
150 username = username, host = session.host, source = "mod_register" });
152 -- TODO unable to write file, file may be locked, etc, what's the correct error?
153 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
157 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
162 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));