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