Comparison

plugins/mod_register.lua @ 709:8bb83563cb21

Automated merge with http://waqas.ath.cx:8000/
author Matthew Wild <mwild1@gmail.com>
date Tue, 13 Jan 2009 15:29:00 +0000
parent 691:406b070b5d3e
child 758:b1885732e979
comparison
equal deleted inserted replaced
708:b72d408f5f15 709:8bb83563cb21
21 21
22 local st = require "util.stanza"; 22 local st = require "util.stanza";
23 local usermanager_user_exists = require "core.usermanager".user_exists; 23 local usermanager_user_exists = require "core.usermanager".user_exists;
24 local usermanager_create_user = require "core.usermanager".create_user; 24 local usermanager_create_user = require "core.usermanager".create_user;
25 local datamanager_store = require "util.datamanager".store; 25 local datamanager_store = require "util.datamanager".store;
26 local os_time = os.time;
26 27
27 module:add_feature("jabber:iq:register"); 28 module:add_feature("jabber:iq:register");
28 29
29 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza) 30 module:add_iq_handler("c2s", "jabber:iq:register", function (session, stanza)
30 if stanza.tags[1].name == "query" then 31 if stanza.tags[1].name == "query" then
91 else 92 else
92 session.send(st.error_reply(stanza, "cancel", "service-unavailable")); 93 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
93 end; 94 end;
94 end); 95 end);
95 96
97 local recent_ips = {};
98 local min_seconds_between_registrations = config.get(module.host, "core", "min_seconds_between_registrations");
99 local whitelist_only = config.get(module.host, "core", "whitelist_registration_only");
100 local whitelisted_ips = config.get(module.host, "core", "registration_whitelist") or { "127.0.0.1" };
101 local blacklisted_ips = config.get(module.host, "core", "registration_blacklist") or {};
102
103 for _, ip in ipairs(whitelisted_ips) do whitelisted_ips[ip] = true; end
104 for _, ip in ipairs(blacklisted_ips) do blacklisted_ips[ip] = true; end
105
96 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza) 106 module:add_iq_handler("c2s_unauthed", "jabber:iq:register", function (session, stanza)
97 if config.get(module.host, "core", "allow_registration") == false then 107 if config.get(module.host, "core", "allow_registration") == false then
98 session.send(st.error_reply(stanza, "cancel", "service-unavailable")); 108 session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
99 elseif stanza.tags[1].name == "query" then 109 elseif stanza.tags[1].name == "query" then
100 local query = stanza.tags[1]; 110 local query = stanza.tags[1];
110 session.send(st.error_reply(stanza, "auth", "registration-required")); 120 session.send(st.error_reply(stanza, "auth", "registration-required"));
111 else 121 else
112 local username = query:child_with_name("username"); 122 local username = query:child_with_name("username");
113 local password = query:child_with_name("password"); 123 local password = query:child_with_name("password");
114 if username and password then 124 if username and password then
125 -- Check that the user is not blacklisted or registering too often
126 if blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then
127 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
128 return;
129 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then
130 if not recent_ips[session.ip] then
131 recent_ips[session.ip] = { time = os_time(), count = 1 };
132 else
133
134 local ip = recent_ips[session.ip];
135 ip.count = ip.count + 1;
136
137 if os_time() - ip.time < min_seconds_between_registrations then
138 ip.time = os_time();
139 session.send(st.error_reply(stanza, "cancel", "not-acceptable"));
140 return;
141 end
142 ip.time = os_time();
143 end
144 end
115 -- FIXME shouldn't use table.concat 145 -- FIXME shouldn't use table.concat
116 username = table.concat(username); 146 username = table.concat(username);
117 password = table.concat(password); 147 password = table.concat(password);
118 if usermanager_user_exists(username, session.host) then 148 if usermanager_user_exists(username, session.host) then
119 session.send(st.error_reply(stanza, "cancel", "conflict")); 149 session.send(st.error_reply(stanza, "cancel", "conflict"));