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.
11 local st = require "util.stanza";
12 local usermanager_user_exists = require "core.usermanager".user_exists;
13 local usermanager_create_user = require "core.usermanager".create_user;
14 local datamanager_store = require "util.datamanager".store;
15 local os_time = os.time;
16 local nodeprep = require "util.encodings".stringprep.nodeprep;
18 module:add_feature("jabber:iq:register");
20 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
21 if stanza.tags[1].name == "query" then
22 local query = stanza.tags[1];
23 if stanza.attr.type == "get" then
24 local reply = st.reply(stanza);
25 reply:tag("query", {xmlns = "jabber:iq:register"})
26 :tag("registered"):up()
27 :tag("username"):text(session.username):up()
28 :tag("password"):up();
30 elseif stanza.attr.type == "set" then
31 if query.tags[1] and query.tags[1].name == "remove" then
32 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
33 local username, host = session.username, session.host;
34 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
36 usermanager_create_user(username, nil, host); -- Disable account
37 -- FIXME the disabling currently allows a different user to recreate the account
38 -- we should add an in-memory account block mode when we have threading
39 session.send(st.reply(stanza));
40 local roster = session.roster;
41 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
42 session:close({condition = "not-authorized", text = "Account deleted"});
44 -- TODO datamanager should be able to delete all user data itself
45 datamanager.store(username, host, "roster", nil);
46 datamanager.store(username, host, "vcard", nil);
47 datamanager.store(username, host, "private", nil);
48 datamanager.store(username, host, "offline", nil);
49 --local bare = username.."@"..host;
50 for jid, item in pairs(roster) do
51 if jid ~= "pending" then
52 if item.subscription == "both" or item.subscription == "to" then
55 if item.subscription == "both" or item.subscription == "from" then
60 datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
62 local username = query:child_with_name("username");
63 local password = query:child_with_name("password");
64 if username and password then
65 -- FIXME shouldn't use table.concat
66 username = nodeprep(table.concat(username));
67 password = table.concat(password);
68 if username == session.username then
69 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
70 session.send(st.reply(stanza));
72 -- TODO unable to write file, file may be locked, etc, what's the correct error?
73 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
76 session.send(st.error_reply(stanza, "modify", "bad-request"));
79 session.send(st.error_reply(stanza, "modify", "bad-request"));
84 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
88 local recent_ips = {};
89 local min_seconds_between_registrations = config.get(module.host, "core", "min_seconds_between_registrations");
90 local whitelist_only = config.get(module.host, "core", "whitelist_registration_only");
91 local whitelisted_ips = config.get(module.host, "core", "registration_whitelist") or { "127.0.0.1" };
92 local blacklisted_ips = config.get(module.host, "core", "registration_blacklist") or {};
94 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
95 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
97 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
98 if config.get(module.host, "core", "allow_registration") == false then
99 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
100 elseif stanza.tags[1].name == "query" then
101 local query = stanza.tags[1];
102 if stanza.attr.type == "get" then
103 local reply = st.reply(stanza);
104 reply:tag("query", {xmlns = "jabber:iq:register"})
105 :tag("instructions"):text("Choose a username and password for use with this service."):up()
106 :tag("username"):up()
107 :tag("password"):up();
109 elseif stanza.attr.type == "set" then
110 if query.tags[1] and query.tags[1].name == "remove" then
111 session.send(st.error_reply(stanza, "auth", "registration-required"));
113 local username = query:child_with_name("username");
114 local password = query:child_with_name("password");
115 if username and password then
116 -- Check that the user is not blacklisted or registering too often
117 if blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
118 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
120 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
121 if not recent_ips[session.ip] then
122 recent_ips[session.ip] = { time = os_time(), count = 1 };
125 local ip = recent_ips[session.ip];
126 ip.count = ip.count + 1;
128 if os_time() - ip.time < min_seconds_between_registrations then
130 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
136 -- FIXME shouldn't use table.concat
137 username = nodeprep(table.concat(username));
138 password = table.concat(password);
139 if usermanager_user_exists(username, session.host) then
140 session.send(st.error_reply(stanza, "cancel", "conflict"));
142 if usermanager_create_user(username, password, session.host) then
143 session.send(st.reply(stanza)); -- user created!
145 -- TODO unable to write file, file may be locked, etc, what's the correct error?
146 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
150 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
155 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));