mod_tls: Remove extraneous flag to starttls() for s2sout connecections
[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 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_set_password(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, "vcard", nil);
47                                 datamanager.store(username, host, "private", nil);
48                                 datamanager.list_store(username, host, "offline", nil);
49                                 local bare = username.."@"..host;
50                                 for jid, item in pairs(roster) do
51                                         if jid and jid ~= "pending" then
52                                                 if item.subscription == "both" or item.subscription == "from" or (roster.pending and roster.pending[jid]) then
53                                                         core_post_stanza(hosts[host], st.presence({type="unsubscribed", from=bare, to=jid}));
54                                                 end
55                                                 if item.subscription == "both" or item.subscription == "to" or item.ask then
56                                                         core_post_stanza(hosts[host], st.presence({type="unsubscribe", from=bare, to=jid}));
57                                                 end
58                                         end
59                                 end
60                                 datamanager.store(username, host, "roster", nil);
61                                 datamanager.store(username, host, "privacy", nil);
62                                 datamanager.store(username, host, "accounts", nil); -- delete accounts datastore at the end
63                                 module:log("info", "User removed their account: %s@%s", username, host);
64                                 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session });
65                         else
66                                 local username = query:child_with_name("username");
67                                 local password = query:child_with_name("password");
68                                 if username and password then
69                                         -- FIXME shouldn't use table.concat
70                                         username = nodeprep(table.concat(username));
71                                         password = table.concat(password);
72                                         if username == session.username then
73                                                 if usermanager_set_password(username, password, session.host) then
74                                                         session.send(st.reply(stanza));
75                                                 else
76                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
77                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error"));
78                                                 end
79                                         else
80                                                 session.send(st.error_reply(stanza, "modify", "bad-request"));
81                                         end
82                                 else
83                                         session.send(st.error_reply(stanza, "modify", "bad-request"));
84                                 end
85                         end
86                 end
87         else
88                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
89         end;
90 end);
91
92 local recent_ips = {};
93 local min_seconds_between_registrations = module:get_option("min_seconds_between_registrations");
94 local whitelist_only = module:get_option("whitelist_registration_only");
95 local whitelisted_ips = module:get_option("registration_whitelist") or { "127.0.0.1" };
96 local blacklisted_ips = module:get_option("registration_blacklist") or {};
97
98 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
99 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
100
101 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
102         if module:get_option("allow_registration") == false then
103                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
104         elseif stanza.tags[1].name == "query" then
105                 local query = stanza.tags[1];
106                 if stanza.attr.type == "get" then
107                         local reply = st.reply(stanza);
108                         reply:tag("query", {xmlns = "jabber:iq:register"})
109                                 :tag("instructions"):text("Choose a username and password for use with this service."):up()
110                                 :tag("username"):up()
111                                 :tag("password"):up();
112                         session.send(reply);
113                 elseif stanza.attr.type == "set" then
114                         if query.tags[1] and query.tags[1].name == "remove" then
115                                 session.send(st.error_reply(stanza, "auth", "registration-required"));
116                         else
117                                 local username = query:child_with_name("username");
118                                 local password = query:child_with_name("password");
119                                 if username and password then
120                                         -- Check that the user is not blacklisted or registering too often
121                                         if not session.ip then
122                                                 module:log("debug", "User's IP not known; can't apply blacklist/whitelist");
123                                         elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
124                                                 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account."));
125                                                 return;
126                                         elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
127                                                 if not recent_ips[session.ip] then
128                                                         recent_ips[session.ip] = { time = os_time(), count = 1 };
129                                                 else
130                                                         local ip = recent_ips[session.ip];
131                                                         ip.count = ip.count + 1;
132                                                         
133                                                         if os_time() - ip.time < min_seconds_between_registrations then
134                                                                 ip.time = os_time();
135                                                                 session.send(st.error_reply(stanza, "wait", "not-acceptable"));
136                                                                 return;
137                                                         end
138                                                         ip.time = os_time();
139                                                 end
140                                         end
141                                         -- FIXME shouldn't use table.concat
142                                         username = nodeprep(table.concat(username));
143                                         password = table.concat(password);
144                                         local host = module.host;
145                                         if not username or username == "" then
146                                                 session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
147                                         elseif usermanager_user_exists(username, host) then
148                                                 session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
149                                         else
150                                                 if usermanager_create_user(username, password, host) then
151                                                         session.send(st.reply(stanza)); -- user created!
152                                                         module:log("info", "User account created: %s@%s", username, host);
153                                                         module:fire_event("user-registered", { 
154                                                                 username = username, host = host, source = "mod_register",
155                                                                 session = session });
156                                                 else
157                                                         -- TODO unable to write file, file may be locked, etc, what's the correct error?
158                                                         session.send(st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk."));
159                                                 end
160                                         end
161                                 else
162                                         session.send(st.error_reply(stanza, "modify", "not-acceptable"));
163                                 end
164                         end
165                 end
166         else
167                 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
168         end;
169 end);
170