mod_register: Change the default for 'allow_registration' from true to false, most...
[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 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;
20
21 local compat = module:get_option_boolean("registration_compat", true);
22 local allow_registration = module:get_option_boolean("allow_registration", false);
23
24 module:add_feature("jabber:iq:register");
25
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;
29
30         -- Advertise registration to unauthorized clients only.
31         if not(allow_registration) or session.type ~= "c2s_unauthed" then
32                 return
33         end
34
35         features:add_child(register_stream_feature);
36 end);
37
38 local function handle_registration_stanza(event)
39         local session, stanza = event.origin, event.stanza;
40
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();
48                 session.send(reply);
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;
53                         
54                         local ok, err = usermanager_delete_user(username, host);
55                         
56                         if not ok then
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));
59                                 return true;
60                         end
61                         
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"});
66                         end
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}));
76                                         end
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}));
79                                         end
80                                 end
81                         end
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 });
86                 else
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));
93                                         else
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"));
96                                         end
97                                 else
98                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
99                                 end
100                         else
101                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
102                         end
103                 end
104         end
105         return true;
106 end
107
108 module:hook("iq/self/jabber:iq:register:query", handle_registration_stanza);
109 if compat then
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);
114                 end
115         end);
116 end
117
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 {};
123
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
126
127 module:hook("stanza/iq/jabber:iq:register:query", function(event)
128         local session, stanza = event.origin, event.stanza;
129
130         if not(allow_registration) or session.type ~= "c2s_unauthed" then
131                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
132         else
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();
140                         session.send(reply);
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"));
144                         else
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."));
153                                                 return true;
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 };
157                                                 else
158                                                         local ip = recent_ips[session.ip];
159                                                         ip.count = ip.count + 1;
160                                                         
161                                                         if os_time() - ip.time < min_seconds_between_registrations then
162                                                                 ip.time = os_time();
163                                                                 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
164                                                                 return true;
165                                                         end
166                                                         ip.time = os_time();
167                                                 end
168                                         end
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."));
177                                         else
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 });
184                                                 else
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."));
187                                                 end
188                                         end
189                                 else
190                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
191                                 end
192                         end
193                 end
194         end
195         return true;
196 end);