mod_register: Switch to using util.throttle for limiting registrations per ip per...
[prosody.git] / plugins / mod_register.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2010 Matthew Wild
3 -- Copyright (C) 2008-2010 Waqas Hussain
4 --
5 -- This project is MIT/X11 licensed. Please see the
6 -- COPYING file in the source package for more information.
7 --
8
9
10 local st = require "util.stanza";
11 local dataform_new = require "util.dataforms".new;
12 local usermanager_user_exists = require "core.usermanager".user_exists;
13 local usermanager_create_user = require "core.usermanager".create_user;
14 local usermanager_set_password = require "core.usermanager".set_password;
15 local usermanager_delete_user = require "core.usermanager".delete_user;
16 local nodeprep = require "util.encodings".stringprep.nodeprep;
17 local jid_bare = require "util.jid".bare;
18 local create_throttle = require "util.throttle".create;
19
20 local compat = module:get_option_boolean("registration_compat", true);
21 local allow_registration = module:get_option_boolean("allow_registration", false);
22 local additional_fields = module:get_option("additional_registration_fields", {});
23
24 local account_details = module:open_store("account_details");
25
26 local field_map = {
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" };
41 };
42
43 local registration_form = dataform_new{
44         title = "Creating a new account";
45         instructions = "Choose a username and password for use with this service.";
46
47         field_map.username;
48         field_map.password;
49 };
50
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()
53         :tag("username"):up()
54         :tag("password"):up();
55
56 for _, field in ipairs(additional_fields) do
57         if type(field) == "table" then
58                 registration_form[#registration_form + 1] = field;
59         else
60                 if field:match("%+$") then
61                         field = field:sub(1, #field - 1);
62                         field_map[field].required = true;
63                 end
64
65                 registration_form[#registration_form + 1] = field_map[field];
66                 registration_query:tag(field):up();
67         end
68 end
69 registration_query:add_child(registration_form:form());
70
71 module:add_feature("jabber:iq:register");
72
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;
76
77         -- Advertise registration to unauthorized clients only.
78         if not(allow_registration) or session.type ~= "c2s_unauthed" then
79                 return
80         end
81
82         features:add_child(register_stream_feature);
83 end);
84
85 local function handle_registration_stanza(event)
86         local session, stanza = event.origin, event.stanza;
87         local log = session.log or module._log;
88
89         local query = stanza.tags[1];
90         if stanza.attr.type == "get" then
91                 local reply = st.reply(stanza);
92                 reply:tag("query", {xmlns = "jabber:iq:register"})
93                         :tag("registered"):up()
94                         :tag("username"):text(session.username):up()
95                         :tag("password"):up();
96                 session.send(reply);
97         else -- stanza.attr.type == "set"
98                 if query.tags[1] and query.tags[1].name == "remove" then
99                         local username, host = session.username, session.host;
100
101                         -- This one weird trick sends a reply to this stanza before the user is deleted
102                         local old_session_close = session.close;
103                         session.close = function(session, ...)
104                                 session.send(st.reply(stanza));
105                                 return old_session_close(session, ...);
106                         end
107
108                         local ok, err = usermanager_delete_user(username, host);
109
110                         if not ok then
111                                 log("debug", "Removing user account %s@%s failed: %s", username, host, err);
112                                 session.close = old_session_close;
113                                 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
114                                 return true;
115                         end
116
117                         log("info", "User removed their account: %s@%s", username, host);
118                         module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
119                 else
120                         local username = nodeprep(query:get_child_text("username"));
121                         local password = query:get_child_text("password");
122                         if username and password then
123                                 if username == session.username then
124                                         if usermanager_set_password(username, password, session.host) then
125                                                 session.send(st.reply(stanza));
126                                         else
127                                                 -- TODO unable to write file, file may be locked, etc, what's the correct error?
128                                                 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
129                                         end
130                                 else
131                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
132                                 end
133                         else
134                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
135                         end
136                 end
137         end
138         return true;
139 end
140
141 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
142 if compat then
143         module:hook("iq/host/jabber:iq:register:query", function (event)
144                 local session, stanza = event.origin, event.stanza;
145                 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
146                         return handle_registration_stanza(event);
147                 end
148         end);
149 end
150
151 local function parse_response(query)
152         local form = query:get_child("x", "jabber:x:data");
153         if form then
154                 return registration_form:data(form);
155         else
156                 local data = {};
157                 local errors = {};
158                 for _, field in ipairs(registration_form) do
159                         local name, required = field.name, field.required;
160                         if field_map[name] then
161                                 data[name] = query:get_child_text(name);
162                                 if (not data[name] or #data[name] == 0) and required then
163                                         errors[name] = "Required value missing";
164                                 end
165                         end
166                 end
167                 if next(errors) then
168                         return data, errors;
169                 end
170                 return data;
171         end
172 end
173
174 local recent_ips = {};
175 local min_seconds_between_registrations = module:get_option_number("min_seconds_between_registrations");
176 local whitelist_only = module:get_option_boolean("whitelist_registration_only");
177 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items;
178 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items;
179
180 local throttle_max = module:get_option_number("registration_throttle_max", min_seconds_between_registrations and 1);
181 local throttle_period = module:get_option_number("registration_throttle_period", min_seconds_between_registrations);
182
183 local function check_throttle(ip)
184         if not throttle_max then return true end
185         local throttle = recent_ips[ip];
186         if not throttle then
187                 throttle = create_throttle(throttle_max, throttle_period);
188                 recent_ips[ip] = throttle;
189         end
190         return throttle:poll(1);
191 end
192
193 module:hook("stanza/iq/jabber:iq:register:query", function(event)
194         local session, stanza = event.origin, event.stanza;
195         local log = session.log or module._log;
196
197         if not(allow_registration) or session.type ~= "c2s_unauthed" then
198                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
199         else
200                 local query = stanza.tags[1];
201                 if stanza.attr.type == "get" then
202                         local reply = st.reply(stanza);
203                         reply:add_child(registration_query);
204                         session.send(reply);
205                 elseif stanza.attr.type == "set" then
206                         if query.tags[1] and query.tags[1].name == "remove" then
207                                 session.send(st.error_reply(stanza, "auth", "registration-required"));
208                         else
209                                 local data, errors = parse_response(query);
210                                 if errors then
211                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
212                                 else
213                                         -- Check that the user is not blacklisted or registering too often
214                                         if not session.ip then
215                                                 log("debug", "User's IP not known; can't apply blacklist/whitelist");
216                                         elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
217                                                 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
218                                                 return true;
219                                         elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
220                                                 if check_throttle(session.ip) then
221                                                         session.send(st.error_reply(stanza, "wait", "not-acceptable"));
222                                                         return true;
223                                                 end
224                                         end
225                                         local username, password = nodeprep(data.username), data.password;
226                                         data.username, data.password = nil, nil;
227                                         local host = module.host;
228                                         if not username or username == "" then
229                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
230                                                 return true;
231                                         end
232                                         local user = { username = username , host = host, allowed = true }
233                                         module:fire_event("user-registering", user);
234                                         if not user.allowed then
235                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is forbidden."));
236                                         elseif usermanager_user_exists(username, host) then
237                                                 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
238                                         else
239                                                 -- TODO unable to write file, file may be locked, etc, what's the correct error?
240                                                 local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
241                                                 if usermanager_create_user(username, password, host) then
242                                                         if next(data) and not account_details:set(username, data) then
243                                                                 usermanager_delete_user(username, host);
244                                                                 session.send(error_reply);
245                                                                 return true;
246                                                         end
247                                                         session.send(st.reply(stanza)); -- user created!
248                                                         log("info", "User account created: %s@%s", username, host);
249                                                         module:fire_event("user-registered", {
250                                                                 username = username, host = host, source = "mod_register",
251                                                                 session = session });
252                                                 else
253                                                         session.send(error_reply);
254                                                 end
255                                         end
256                                 end
257                         end
258                 end
259         end
260         return true;
261 end);