2 -- Copyright (C) 2008 Matthew Wild
3 -- Copyright (C) 2008 Waqas Hussain
5 -- This program is free software; you can redistribute it and/or
6 -- modify it under the terms of the GNU General Public License
7 -- as published by the Free Software Foundation; either version 2
8 -- of the License, or (at your option) any later version.
10 -- This program is distributed in the hope that it will be useful,
11 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
12 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 -- GNU General Public License for more details.
15 -- You should have received a copy of the GNU General Public License
16 -- along with this program; if not, write to the Free Software
17 -- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 local st = require "util.stanza";
23 local usermanager_user_exists = require "core.usermanager".user_exists;
24 local usermanager_create_user = require "core.usermanager".create_user;
25 local datamanager_store = require "util.datamanager".store;
26 local os_time = os.time;
28 module:add_feature("jabber:iq:register");
30 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
31 if stanza.tags[1].name == "query" then
32 local query = stanza.tags[1];
33 if stanza.attr.type == "get" then
34 local reply = st.reply(stanza);
35 reply:tag("query", {xmlns = "jabber:iq:register"})
36 :tag("registered"):up()
37 :tag("username"):text(session.username):up()
38 :tag("password"):up();
40 elseif stanza.attr.type == "set" then
41 if query.tags[1] and query.tags[1].name == "remove" then
42 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
43 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
45 usermanager_create_user(session.username, nil, session.host); -- Disable account
46 -- FIXME the disabling currently allows a different user to recreate the account
47 -- we should add an in-memory account block mode when we have threading
48 session.send(st.reply(stanza));
49 local roster = session.roster;
50 for _, session in pairs(hosts[session.host].sessions[session.username].sessions) do -- disconnect all resources
51 session:disconnect({condition = "not-authorized", text = "Account deleted"});
53 -- TODO datamanager should be able to delete all user data itself
54 datamanager.store(session.username, session.host, "roster", nil);
55 datamanager.store(session.username, session.host, "vcard", nil);
56 datamanager.store(session.username, session.host, "private", nil);
57 datamanager.store(session.username, session.host, "offline", nil);
58 local bare = session.username.."@"..session.host;
59 for jid, item in pairs(roster) do
60 if jid ~= "pending" then
61 if item.subscription == "both" or item.subscription == "to" then
64 if item.subscription == "both" or item.subscription == "from" then
69 datamanager.store(session.username, session.host, "accounts", nil); -- delete accounts datastore at the end
71 local username = query:child_with_name("username");
72 local password = query:child_with_name("password");
73 if username and password then
74 -- FIXME shouldn't use table.concat
75 username = table.concat(username);
76 password = table.concat(password);
77 if username == session.username then
78 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
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"));
93 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
97 local recent_ips = {};
98 local min_seconds_between_registrations = config.get(module.host, "core", "min_seconds_between_registrations");
99 local whitelisted_ips = config.get(module.host, "core", "registration_whitelist") or { "127.0.0.1" };
100 local blacklisted_ips = config.get(module.host, "core", "registration_blacklist") or {};
102 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
103 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
105 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
106 if config.get(module.host, "core", "allow_registration") == false then
107 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
108 elseif stanza.tags[1].name == "query" then
109 local query = stanza.tags[1];
110 if stanza.attr.type == "get" then
111 local reply = st.reply(stanza);
112 reply:tag("query", {xmlns = "jabber:iq:register"})
113 :tag("instructions"):text("Choose a username and password for use with this service."):up()
114 :tag("username"):up()
115 :tag("password"):up();
117 elseif stanza.attr.type == "set" then
118 if query.tags[1] and query.tags[1].name == "remove" then
119 session.send(st.error_reply(stanza, "auth", "registration-required"));
121 local username = query:child_with_name("username");
122 local password = query:child_with_name("password");
123 if username and password then
124 -- Check that the user is not blacklisted or registering too often
125 if blacklisted_ips[session.ip] then
126 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
128 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
129 if not recent_ips[session.ip] then
130 recent_ips[session.ip] = { time = os_time(), count = 1 };
133 local ip = recent_ips[session.ip];
134 ip.count = ip.count + 1;
136 if os_time() - ip.time < min_seconds_between_registrations then
138 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
144 -- FIXME shouldn't use table.concat
145 username = table.concat(username);
146 password = table.concat(password);
147 if usermanager_user_exists(username, session.host) then
148 session.send(st.error_reply(stanza, "cancel", "conflict"));
150 if usermanager_create_user(username, password, session.host) then
151 session.send(st.reply(stanza)); -- user created!
153 -- TODO unable to write file, file may be locked, etc, what's the correct error?
154 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
158 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
163 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));