mod_tls: :up() out of the starttls tag in stream:features
[prosody.git] / plugins / mod_register.lua
1 -- Prosody IM
2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 datamanager_store = require "util.datamanager".store;
16 local os_time = os.time;
17 local nodeprep = require "util.encodings".stringprep.nodeprep;
18
19 module:add_feature("jabber:iq:register");
20
21 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
22         if stanza.tags[1].name == "query" then
23                 local query = stanza.tags[1];
24                 if stanza.attr.type == "get" then
25                         local reply = st.reply(stanza);
26                         reply:tag("query", {xmlns = "jabber:iq:register"})
27                                 :tag("registered"):up()
28                                 :tag("username"):text(session.username):up()
29                                 :tag("password"):up();
30                         session.send(reply);
31                 elseif stanza.attr.type == "set" then
32                         if query.tags[1] and query.tags[1].name == "remove" then
33                                 -- TODO delete user auth data, send iq response, kick all user resources with a <not-authorized/>, delete all user data
34                                 local username, host = session.username, session.host;
35                                 --session.send(st.error_reply(stanza, "cancel", "not-allowed"));
36                                 --return;
37                                 usermanager_create_user(username, nil, host); -- Disable account
38                                 -- FIXME the disabling currently allows a different user to recreate the account
39                                 -- we should add an in-memory account block mode when we have threading
40                                 session.send(st.reply(stanza));
41                                 local roster = session.roster;
42                                 for _, session in pairs(hosts[host].sessions[username].sessions) do -- disconnect all resources
43                                         session:close({condition = "not-authorized", text = "Account deleted"});
44                                 end
45                                 -- TODO datamanager should be able to delete all user data itself
46                                 datamanager.store(username, host, "roster", nil);
47                                 datamanager.store(username, host, "vcard", nil);
48                                 datamanager.store(username, host, "private", nil);
49                                 datamanager.store(username, host, "offline", nil);
50                                 --local bare = username.."@"..host;
51                                 for jid, item in pairs(roster) do
52                                         if jid ~= "pending" then
53                                                 if item.subscription == "both" or item.subscription == "to" then
54                                                         -- TODO unsubscribe
55                                                 end
56                                                 if item.subscription == "both" or item.subscription == "from" then
57                                                         -- TODO unsubscribe
58                                                 end
59                                         end
60                                 end
61                                 datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
62                                 module:log("info", "User removed their account: %s@%s", username, host);
63                                 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
64                         else
65                                 local username = query:child_with_name("username");
66                                 local password = query:child_with_name("password");
67                                 if username and password then
68                                         -- FIXME shouldn't use table.concat
69                                         username = nodeprep(table.concat(username));
70                                         password = table.concat(password);
71                                         if username == session.username then
72                                                 if usermanager_create_user(username, password, session.host) then -- password change -- TODO is this the right way?
73                                                         session.send(st.reply(stanza));
74                                                 else
75                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
76                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
77                                                 end
78                                         else
79                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
80                                         end
81                                 else
82                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
83                                 end
84                         end
85                 end
86         else
87                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
88         end;
89 end);
90
91 local recent_ips = {};
92 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
93 local whitelist_only = module:get_option("whitelist_registration_only");
94 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
95 local blacklisted_ips = module:get_option("registration_blacklist") or {};
96
97 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
98 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
99
100 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
101         if module:get_option("allow_registration") == false then
102                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
103         elseif stanza.tags[1].name == "query" then
104                 local query = stanza.tags[1];
105                 if stanza.attr.type == "get" then
106                         local reply = st.reply(stanza);
107                         reply:tag("query", {xmlns = "jabber:iq:register"})
108                                 :tag("instructions"):text("Choose a username and password for use with this service."):up()
109                                 :tag("username"):up()
110                                 :tag("password"):up();
111                         session.send(reply);
112                 elseif stanza.attr.type == "set" then
113                         if query.tags[1] and query.tags[1].name == "remove" then
114                                 session.send(st.error_reply(stanza, "auth", "registration-required"));
115                         else
116                                 local username = query:child_with_name("username");
117                                 local password = query:child_with_name("password");
118                                 if username and password then
119                                         -- Check that the user is not blacklisted or registering too often
120                                         if blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
121                                                 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
122                                                 return;
123                                         elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
124                                                 if not recent_ips[session.ip] then
125                                                         recent_ips[session.ip] = { time = os_time(), count = 1 };
126                                                 else
127                                                         local ip = recent_ips[session.ip];
128                                                         ip.count = ip.count + 1;
129                                                         
130                                                         if os_time() - ip.time < min_seconds_between_registrations then
131                                                                 ip.time = os_time();
132                                                                 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
133                                                                 return;
134                                                         end
135                                                         ip.time = os_time();
136                                                 end
137                                         end
138                                         -- FIXME shouldn't use table.concat
139                                         username = nodeprep(table.concat(username));
140                                         password = table.concat(password);
141                                         local host = module.host;
142                                         if not username then
143                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
144                                         elseif usermanager_user_exists(username, host) then
145                                                 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
146                                         else
147                                                 if usermanager_create_user(username, password, host) then
148                                                         session.send(st.reply(stanza)); -- user created!
149                                                         module:log("info", "User account created: %s@%s", username, host);
150                                                         module:fire_event("user-registered", { 
151                                                                 username = username, host = host, source = "mod_register",
152                                                                 session = session });
153                                                 else
154                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
155                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk."));
156                                                 end
157                                         end
158                                 else
159                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
160                                 end
161                         end
162                 end
163         else
164                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
165         end;
166 end);
167