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);
22 local allow_registration = module:get_option_boolean("allow_registration", false);
24 module:add_feature("jabber:iq:register");
26 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up();
27 module:hook("stream-features", function(event)
28 local session, features = event.origin, event.features;
30 -- Advertise registration to unauthorized clients only.
31 if not(allow_registration) or session.type ~= "c2s_unauthed" then
35 features:add_child(register_stream_feature);
38 local function handle_registration_stanza(event)
39 local session, stanza = event.origin, event.stanza;
41 local query = stanza.tags[1];
42 if stanza.attr.type == "get" then
43 local reply = st.reply(stanza);
44 reply:tag("query", {xmlns = "jabber:iq:register"})
45 :tag("registered"):up()
46 :tag("username"):text(session.username):up()
47 :tag("password"):up();
49 else -- stanza.attr.type == "set"
50 if query.tags[1] and query.tags[1].name == "remove" then
51 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
52 local username, host = session.username, session.host;
54 local ok, err = usermanager_delete_user(username, host);
57 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
58 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
62 session.send(st.reply(stanza));
63 local roster = session.roster;
64 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
65 session:close({condition = "not-authorized", text = "Account deleted"});
67 -- TODO datamanager should be able to delete all user data itself
68 datamanager.store(username, host, "vcard", nil);
69 datamanager.store(username, host, "private", nil);
70 datamanager.list_store(username, host, "offline", nil);
71 local bare = username.."@"..host;
72 for jid, item in pairs(roster) do
73 if jid and jid ~= "pending" then
74 if item.subscription == "both" or item.subscription == "from" or (roster.pending and roster.pending[jid]) then
75 core_post_stanza(hosts[host], st.presence({type="unsubscribed", from=bare, to=jid}));
77 if item.subscription == "both" or item.subscription == "to" or item.ask then
78 core_post_stanza(hosts[host], st.presence({type="unsubscribe", from=bare, to=jid}));
82 datamanager.store(username, host, "roster", nil);
83 datamanager.store(username, host, "privacy", nil);
84 module:log("info", "User removed their account: %s@%s", username, host);
85 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
87 local username = nodeprep(query:get_child("username"):get_text());
88 local password = query:get_child("password"):get_text();
89 if username and password then
90 if username == session.username then
91 if usermanager_set_password(username, password, session.host) then
92 session.send(st.reply(stanza));
94 -- TODO unable to write file, file may be locked, etc, what's the correct error?
95 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
98 session.send(st.error_reply(stanza, "modify", "bad-request"));
101 session.send(st.error_reply(stanza, "modify", "bad-request"));
108 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
110 module:hook("iq/host/jabber:iq:register:query", function (event)
111 local session, stanza = event.origin, event.stanza;
112 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
113 return handle_registration_stanza(event);
118 local recent_ips = {};
119 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
120 local whitelist_only = module:get_option("whitelist_registration_only");
121 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
122 local blacklisted_ips = module:get_option("registration_blacklist") or {};
124 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
125 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
127 module:hook("stanza/iq/jabber:iq:register:query", function(event)
128 local session, stanza = event.origin, event.stanza;
130 if not(allow_registration) or session.type ~= "c2s_unauthed" then
131 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
133 local query = stanza.tags[1];
134 if stanza.attr.type == "get" then
135 local reply = st.reply(stanza);
136 reply:tag("query", {xmlns = "jabber:iq:register"})
137 :tag("instructions"):text("Choose a username and password for use with this service."):up()
138 :tag("username"):up()
139 :tag("password"):up();
141 elseif stanza.attr.type == "set" then
142 if query.tags[1] and query.tags[1].name == "remove" then
143 session.send(st.error_reply(stanza, "auth", "registration-required"));
145 local username = query:child_with_name("username");
146 local password = query:child_with_name("password");
147 if username and password then
148 -- Check that the user is not blacklisted or registering too often
149 if not session.ip then
150 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
151 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
152 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
154 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
155 if not recent_ips[session.ip] then
156 recent_ips[session.ip] = { time = os_time(), count = 1 };
158 local ip = recent_ips[session.ip];
159 ip.count = ip.count + 1;
161 if os_time() - ip.time < min_seconds_between_registrations then
163 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
169 -- FIXME shouldn't use table.concat
170 username = nodeprep(table.concat(username));
171 password = table.concat(password);
172 local host = module.host;
173 if not username or username == "" then
174 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
175 elseif usermanager_user_exists(username, host) then
176 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
178 if usermanager_create_user(username, password, host) then
179 session.send(st.reply(stanza)); -- user created!
180 module:log("info", "User account created: %s@%s", username, host);
181 module:fire_event("user-registered", {
182 username = username, host = host, source = "mod_register",
183 session = session });
185 -- TODO unable to write file, file may be locked, etc, what's the correct error?
186 session.send(st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk."));
190 session.send(st.error_reply(stanza, "modify", "not-acceptable"));