Software / code / prosody
Comparison
plugins/mod_register.lua @ 7017:ff734a602886
mod_register: Use session log instance to ease indentification
| author | Kim Alvefur <zash@zash.se> |
|---|---|
| date | Mon, 21 Dec 2015 14:41:38 +0100 |
| parent | 5776:bd0ff8ae98a8 |
| child | 7018:5c3d4254d415 |
comparison
equal
deleted
inserted
replaced
| 7016:e0a0af42b09f | 7017:ff734a602886 |
|---|---|
| 82 features:add_child(register_stream_feature); | 82 features:add_child(register_stream_feature); |
| 83 end); | 83 end); |
| 84 | 84 |
| 85 local function handle_registration_stanza(event) | 85 local function handle_registration_stanza(event) |
| 86 local session, stanza = event.origin, event.stanza; | 86 local session, stanza = event.origin, event.stanza; |
| 87 local log = session.log or module._log; | |
| 87 | 88 |
| 88 local query = stanza.tags[1]; | 89 local query = stanza.tags[1]; |
| 89 if stanza.attr.type == "get" then | 90 if stanza.attr.type == "get" then |
| 90 local reply = st.reply(stanza); | 91 local reply = st.reply(stanza); |
| 91 reply:tag("query", {xmlns = "jabber:iq:register"}) | 92 reply:tag("query", {xmlns = "jabber:iq:register"}) |
| 104 end | 105 end |
| 105 | 106 |
| 106 local ok, err = usermanager_delete_user(username, host); | 107 local ok, err = usermanager_delete_user(username, host); |
| 107 | 108 |
| 108 if not ok then | 109 if not ok then |
| 109 module:log("debug", "Removing user account %s@%s failed: %s", username, host, err); | 110 log("debug", "Removing user account %s@%s failed: %s", username, host, err); |
| 110 session.close = old_session_close; | 111 session.close = old_session_close; |
| 111 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err)); | 112 session.send(st.error_reply(stanza, "cancel", "service-unavailable", err)); |
| 112 return true; | 113 return true; |
| 113 end | 114 end |
| 114 | 115 |
| 115 module:log("info", "User removed their account: %s@%s", username, host); | 116 log("info", "User removed their account: %s@%s", username, host); |
| 116 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session }); | 117 module:fire_event("user-deregistered", { username = username, host = host, source = "mod_register", session = session }); |
| 117 else | 118 else |
| 118 local username = nodeprep(query:get_child_text("username")); | 119 local username = nodeprep(query:get_child_text("username")); |
| 119 local password = query:get_child_text("password"); | 120 local password = query:get_child_text("password"); |
| 120 if username and password then | 121 if username and password then |
| 175 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items; | 176 local whitelisted_ips = module:get_option_set("registration_whitelist", { "127.0.0.1" })._items; |
| 176 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items; | 177 local blacklisted_ips = module:get_option_set("registration_blacklist", {})._items; |
| 177 | 178 |
| 178 module:hook("stanza/iq/jabber:iq:register:query", function(event) | 179 module:hook("stanza/iq/jabber:iq:register:query", function(event) |
| 179 local session, stanza = event.origin, event.stanza; | 180 local session, stanza = event.origin, event.stanza; |
| 181 local log = session.log or module._log; | |
| 180 | 182 |
| 181 if not(allow_registration) or session.type ~= "c2s_unauthed" then | 183 if not(allow_registration) or session.type ~= "c2s_unauthed" then |
| 182 session.send(st.error_reply(stanza, "cancel", "service-unavailable")); | 184 session.send(st.error_reply(stanza, "cancel", "service-unavailable")); |
| 183 else | 185 else |
| 184 local query = stanza.tags[1]; | 186 local query = stanza.tags[1]; |
| 194 if errors then | 196 if errors then |
| 195 session.send(st.error_reply(stanza, "modify", "not-acceptable")); | 197 session.send(st.error_reply(stanza, "modify", "not-acceptable")); |
| 196 else | 198 else |
| 197 -- Check that the user is not blacklisted or registering too often | 199 -- Check that the user is not blacklisted or registering too often |
| 198 if not session.ip then | 200 if not session.ip then |
| 199 module:log("debug", "User's IP not known; can't apply blacklist/whitelist"); | 201 log("debug", "User's IP not known; can't apply blacklist/whitelist"); |
| 200 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then | 202 elseif blacklisted_ips[session.ip] or (whitelist_only and not whitelisted_ips[session.ip]) then |
| 201 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account.")); | 203 session.send(st.error_reply(stanza, "cancel", "not-acceptable", "You are not allowed to register an account.")); |
| 202 return true; | 204 return true; |
| 203 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then | 205 elseif min_seconds_between_registrations and not whitelisted_ips[session.ip] then |
| 204 if not recent_ips[session.ip] then | 206 if not recent_ips[session.ip] then |
| 236 usermanager_delete_user(username, host); | 238 usermanager_delete_user(username, host); |
| 237 session.send(error_reply); | 239 session.send(error_reply); |
| 238 return true; | 240 return true; |
| 239 end | 241 end |
| 240 session.send(st.reply(stanza)); -- user created! | 242 session.send(st.reply(stanza)); -- user created! |
| 241 module:log("info", "User account created: %s@%s", username, host); | 243 log("info", "User account created: %s@%s", username, host); |
| 242 module:fire_event("user-registered", { | 244 module:fire_event("user-registered", { |
| 243 username = username, host = host, source = "mod_register", | 245 username = username, host = host, source = "mod_register", |
| 244 session = session }); | 246 session = session }); |
| 245 else | 247 else |
| 246 session.send(error_reply); | 248 session.send(error_reply); |