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 dataform_new = require "util.dataforms".new;
14 local usermanager_user_exists = require "core.usermanager".user_exists;
15 local usermanager_create_user = require "core.usermanager".create_user;
16 local usermanager_set_password = require "core.usermanager".set_password;
17 local usermanager_delete_user = require "core.usermanager".delete_user;
18 local os_time = os.time;
19 local nodeprep = require "util.encodings".stringprep.nodeprep;
20 local jid_bare = require "util.jid".bare;
22 local compat = module:get_option_boolean("registration_compat", true);
23 local allow_registration = module:get_option_boolean("allow_registration", false);
24 local additional_fields = module:get_option("additional_registration_fields", {});
27 username = { name = "username", type = "text-single", label = "Username", required = true };
28 password = { name = "password", type = "text-private", label = "Password", required = true };
29 nick = { name = "nick", type = "text-single", label = "Nickname" };
30 name = { name = "name", type = "text-single", label = "Full Name" };
31 first = { name = "first", type = "text-single", label = "Given Name" };
32 last = { name = "last", type = "text-single", label = "Family Name" };
33 email = { name = "email", type = "text-single", label = "Email" };
34 address = { name = "address", type = "text-single", label = "Street" };
35 city = { name = "city", type = "text-single", label = "City" };
36 state = { name = "state", type = "text-single", label = "State" };
37 zip = { name = "zip", type = "text-single", label = "Postal code" };
38 phone = { name = "phone", type = "text-single", label = "Telephone number" };
39 url = { name = "url", type = "text-single", label = "Webpage" };
40 date = { name = "date", type = "text-single", label = "Birth date" };
43 local registration_form = dataform_new{
44 title = "Creating a new account";
45 instructions = "Choose a username and password for use with this service.";
51 local registration_query = st.stanza("query", {xmlns = "jabber:iq:register"})
52 :tag("instructions"):text("Choose a username and password for use with this service."):up()
54 :tag("password"):up();
56 for _, field in ipairs(additional_fields) do
57 if type(field) == "table" then
58 registration_form[#registration_form + 1] = field;
60 if field:match("%+$") then
61 field = field:sub(1, #field - 1);
62 field_map[field].required = true;
65 registration_form[#registration_form + 1] = field_map[field];
66 registration_query:tag(field):up();
69 registration_query:add_child(registration_form:form());
71 module:add_feature("jabber:iq:register");
73 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up();
74 module:hook("stream-features", function(event)
75 local session, features = event.origin, event.features;
77 -- Advertise registration to unauthorized clients only.
78 if not(allow_registration) or session.type ~= "c2s_unauthed" then
82 features:add_child(register_stream_feature);
85 local function handle_registration_stanza(event)
86 local session, stanza = event.origin, event.stanza;
88 local query = stanza.tags[1];
89 if stanza.attr.type == "get" then
90 local reply = st.reply(stanza);
91 reply:tag("query", {xmlns = "jabber:iq:register"})
92 :tag("registered"):up()
93 :tag("username"):text(session.username):up()
94 :tag("password"):up();
96 else -- stanza.attr.type == "set"
97 if query.tags[1] and query.tags[1].name == "remove" then
98 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
99 local username, host = session.username, session.host;
101 local ok, err = usermanager_delete_user(username, host);
104 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
105 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
109 session.send(st.reply(stanza));
110 local roster = session.roster;
111 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
112 session:close({condition = "not-authorized", text = "Account deleted"});
114 -- TODO datamanager should be able to delete all user data itself
115 datamanager.store(username, host, "vcard", nil);
116 datamanager.store(username, host, "private", nil);
117 datamanager.store(username, host, "account_details", nil);
118 datamanager.list_store(username, host, "offline", nil);
119 local bare = username.."@"..host;
120 for jid, item in pairs(roster) do
121 if jid and jid ~= "pending" then
122 if item.subscription == "both" or item.subscription == "from" or (roster.pending and roster.pending[jid]) then
123 core_post_stanza(hosts[host], st.presence({type="unsubscribed", from=bare, to=jid}));
125 if item.subscription == "both" or item.subscription == "to" or item.ask then
126 core_post_stanza(hosts[host], st.presence({type="unsubscribe", from=bare, to=jid}));
130 datamanager.store(username, host, "roster", nil);
131 datamanager.store(username, host, "privacy", nil);
132 module:log("info", "User removed their account: %s@%s", username, host);
133 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
135 local username = nodeprep(query:get_child("username"):get_text());
136 local password = query:get_child("password"):get_text();
137 if username and password then
138 if username == session.username then
139 if usermanager_set_password(username, password, session.host) then
140 session.send(st.reply(stanza));
142 -- TODO unable to write file, file may be locked, etc, what's the correct error?
143 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
146 session.send(st.error_reply(stanza, "modify", "bad-request"));
149 session.send(st.error_reply(stanza, "modify", "bad-request"));
156 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
158 module:hook("iq/host/jabber:iq:register:query", function (event)
159 local session, stanza = event.origin, event.stanza;
160 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
161 return handle_registration_stanza(event);
166 local function parse_response(query)
167 local form = query:get_child("x", "jabber:x:data");
169 return registration_form:data(form);
173 for _, field in ipairs(registration_form) do
174 local name, required = field.name, field.required;
175 if field_map[name] then
176 data[name] = query:get_child_text(name);
177 if (not data[name] or #data[name] == 0) and required then
178 errors[name] = "Required value missing";
189 local recent_ips = {};
190 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
191 local whitelist_only = module:get_option("whitelist_registration_only");
192 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
193 local blacklisted_ips = module:get_option("registration_blacklist") or {};
195 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
196 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
198 module:hook("stanza/iq/jabber:iq:register:query", function(event)
199 local session, stanza = event.origin, event.stanza;
201 if not(allow_registration) or session.type ~= "c2s_unauthed" then
202 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
204 local query = stanza.tags[1];
205 if stanza.attr.type == "get" then
206 local reply = st.reply(stanza);
207 reply:add_child(registration_query);
209 elseif stanza.attr.type == "set" then
210 if query.tags[1] and query.tags[1].name == "remove" then
211 session.send(st.error_reply(stanza, "auth", "registration-required"));
213 local data, errors = parse_response(query);
215 session.send(st.error_reply(stanza, "modify", "not-acceptable"));
217 -- Check that the user is not blacklisted or registering too often
218 if not session.ip then
219 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
220 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
221 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
223 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
224 if not recent_ips[session.ip] then
225 recent_ips[session.ip] = { time = os_time(), count = 1 };
227 local ip = recent_ips[session.ip];
228 ip.count = ip.count + 1;
230 if os_time() - ip.time < min_seconds_between_registrations then
232 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
238 local username, password = nodeprep(data.username), data.password;
239 data.username, data.password = nil, nil;
240 local host = module.host;
241 if not username or username == "" then
242 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
243 elseif usermanager_user_exists(username, host) then
244 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
246 -- TODO unable to write file, file may be locked, etc, what's the correct error?
247 local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
248 if usermanager_create_user(username, password, host) then
249 if next(data) and not datamanager.store(username, host, "account_details", data) then
250 usermanager_delete_user(username, host);
251 session.send(error_reply);
254 session.send(st.reply(stanza)); -- user created!
255 module:log("info", "User account created: %s@%s", username, host);
256 module:fire_event("user-registered", {
257 username = username, host = host, source = "mod_register",
258 session = session });
260 session.send(error_reply);