Merge 0.9->trunk
[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 datamanager = require "util.datamanager";
12 local dataform_new = require "util.dataforms".new;
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;
20
21 local compat = module:get_option_boolean("registration_compat", true);
22 local allow_registration = module:get_option_boolean("allow_registration", false);
23 local additional_fields = module:get_option("additional_registration_fields", {});
24
25 local field_map = {
26         username = { name = "username", type = "text-single", label = "Username", required = true };
27         password = { name = "password", type = "text-private", label = "Password", required = true };
28         nick = { name = "nick", type = "text-single", label = "Nickname" };
29         name = { name = "name", type = "text-single", label = "Full Name" };
30         first = { name = "first", type = "text-single", label = "Given Name" };
31         last = { name = "last", type = "text-single", label = "Family Name" };
32         email = { name = "email", type = "text-single", label = "Email" };
33         address = { name = "address", type = "text-single", label = "Street" };
34         city = { name = "city", type = "text-single", label = "City" };
35         state = { name = "state", type = "text-single", label = "State" };
36         zip = { name = "zip", type = "text-single", label = "Postal code" };
37         phone = { name = "phone", type = "text-single", label = "Telephone number" };
38         url = { name = "url", type = "text-single", label = "Webpage" };
39         date = { name = "date", type = "text-single", label = "Birth date" };
40 };
41
42 local registration_form = dataform_new{
43         title = "Creating a new account";
44         instructions = "Choose a username and password for use with this service.";
45
46         field_map.username;
47         field_map.password;
48 };
49
50 local registration_query = st.stanza("query", {xmlns = "jabber:iq:register"})
51         :tag("instructions"):text("Choose a username and password for use with this service."):up()
52         :tag("username"):up()
53         :tag("password"):up();
54
55 for _, field in ipairs(additional_fields) do
56         if type(field) == "table" then
57                 registration_form[#registration_form + 1] = field;
58         else
59                 if field:match("%+$") then
60                         field = field:sub(1, #field - 1);
61                         field_map[field].required = true;
62                 end
63
64                 registration_form[#registration_form + 1] = field_map[field];
65                 registration_query:tag(field):up();
66         end
67 end
68 registration_query:add_child(registration_form:form());
69
70 module:add_feature("jabber:iq:register");
71
72 local register_stream_feature = st.stanza("register", {xmlns="http://jabber.org/features/iq-register"}):up();
73 module:hook("stream-features", function(event)
74         local session, features = event.origin, event.features;
75
76         -- Advertise registration to unauthorized clients only.
77         if not(allow_registration) or session.type ~= "c2s_unauthed" then
78                 return
79         end
80
81         features:add_child(register_stream_feature);
82 end);
83
84 local function handle_registration_stanza(event)
85         local session, stanza = event.origin, event.stanza;
86
87         local query = stanza.tags[1];
88         if stanza.attr.type == "get" then
89                 local reply = st.reply(stanza);
90                 reply:tag("query", {xmlns = "jabber:iq:register"})
91                         :tag("registered"):up()
92                         :tag("username"):text(session.username):up()
93                         :tag("password"):up();
94                 session.send(reply);
95         else -- stanza.attr.type == "set"
96                 if query.tags[1] and query.tags[1].name == "remove" then
97                         local username, host = session.username, session.host;
98
99                         local old_session_close = session.close;
100                         session.close = function(session, ...)
101                                 session.send(st.reply(stanza));
102                                 return old_session_close(session, ...);
103                         end
104                         
105                         local ok, err = usermanager_delete_user(username, host);
106                         
107                         if not ok then
108                                 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err);
109                                 session.close = old_session_close;
110                                 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err));
111                                 return true;
112                         end
113                         
114                         module:log("info", "User removed their account: %s@%s", username, host);
115                         module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
116                 else
117                         local username = nodeprep(query:get_child("username"):get_text());
118                         local password = query:get_child("password"):get_text();
119                         if username and password then
120                                 if username == session.username then
121                                         if usermanager_set_password(username, password, session.host) then
122                                                 session.send(st.reply(stanza));
123                                         else
124                                                 -- TODO unable to write file, file may be locked, etc, what's the correct error?
125                                                 session.send(st.error_reply(stanza, "wait", "internal-server-error"));
126                                         end
127                                 else
128                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
129                                 end
130                         else
131                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
132                         end
133                 end
134         end
135         return true;
136 end
137
138 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
139 if compat then
140         module:hook("iq/host/jabber:iq:register:query", function (event)
141                 local session, stanza = event.origin, event.stanza;
142                 if session.type == "c2s" and jid_bare(stanza.attr.to) == session.host then
143                         return handle_registration_stanza(event);
144                 end
145         end);
146 end
147
148 local function parse_response(query)
149         local form = query:get_child("x", "jabber:x:data");
150         if form then
151                 return registration_form:data(form);
152         else
153                 local data = {};
154                 local errors = {};
155                 for _, field in ipairs(registration_form) do
156                         local name, required = field.name, field.required;
157                         if field_map[name] then
158                                 data[name] = query:get_child_text(name);
159                                 if (not data[name] or #data[name] == 0) and required then
160                                         errors[name] = "Required value missing";
161                                 end
162                         end
163                 end
164                 if next(errors) then
165                         return data, errors;
166                 end
167                 return data;
168         end
169 end
170
171 local recent_ips = {};
172 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
173 local whitelist_only = module:get_option("whitelist_registration_only");
174 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
175 local blacklisted_ips = module:get_option("registration_blacklist") or {};
176
177 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
178 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
179
180 module:hook("stanza/iq/jabber:iq:register:query", function(event)
181         local session, stanza = event.origin, event.stanza;
182
183         if not(allow_registration) or session.type ~= "c2s_unauthed" then
184                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
185         else
186                 local query = stanza.tags[1];
187                 if stanza.attr.type == "get" then
188                         local reply = st.reply(stanza);
189                         reply:add_child(registration_query);
190                         session.send(reply);
191                 elseif stanza.attr.type == "set" then
192                         if query.tags[1] and query.tags[1].name == "remove" then
193                                 session.send(st.error_reply(stanza, "auth", "registration-required"));
194                         else
195                                 local data, errors = parse_response(query);
196                                 if errors then
197                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
198                                 else
199                                         -- Check that the user is not blacklisted or registering too often
200                                         if not session.ip then
201                                                 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
202                                         elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
203                                                 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
204                                                 return true;
205                                         elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
206                                                 if not recent_ips[session.ip] then
207                                                         recent_ips[session.ip] = { time = os_time(), count = 1 };
208                                                 else
209                                                         local ip = recent_ips[session.ip];
210                                                         ip.count = ip.count + 1;
211                                                         
212                                                         if os_time() - ip.time < min_seconds_between_registrations then
213                                                                 ip.time = os_time();
214                                                                 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
215                                                                 return true;
216                                                         end
217                                                         ip.time = os_time();
218                                                 end
219                                         end
220                                         local username, password = nodeprep(data.username), data.password;
221                                         data.username, data.password = nil, nil;
222                                         local host = module.host;
223                                         if not username or username == "" then
224                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
225                                                 return true;
226                                         end
227                                         local user = { username = username , host = host, allowed = true }
228                                         module:fire_event("user-registering", user);
229                                         if not user.allowed then
230                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is forbidden."));
231                                         elseif usermanager_user_exists(username, host) then
232                                                 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
233                                         else
234                                                 -- TODO unable to write file, file may be locked, etc, what's the correct error?
235                                                 local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
236                                                 if usermanager_create_user(username, password, host) then
237                                                         if next(data) and not datamanager.store(username, host, "account_details", data) then
238                                                                 usermanager_delete_user(username, host);
239                                                                 session.send(error_reply);
240                                                                 return true;
241                                                         end
242                                                         session.send(st.reply(stanza)); -- user created!
243                                                         module:log("info", "User account created: %s@%s", username, host);
244                                                         module:fire_event("user-registered", {
245                                                                 username = username, host = host, source = "mod_register",
246                                                                 session = session });
247                                                 else
248                                                         session.send(error_reply);
249                                                 end
250                                         end
251                                 end
252                         end
253                 end
254         end
255         return true;
256 end);