plugins/mod_register_ibr.lua
changeset 8739 a071c203a1a0
parent 8586 9918f324a0be
child 8740 6d71845bf56f
equal deleted inserted replaced
8738:856a40ec4a0a 8739:a071c203a1a0
   113 	local log = session.log or module._log;
   113 	local log = session.log or module._log;
   114 
   114 
   115 	if session.type ~= "c2s_unauthed" then
   115 	if session.type ~= "c2s_unauthed" then
   116 		log("debug", "Attempted registration when disabled or already authenticated");
   116 		log("debug", "Attempted registration when disabled or already authenticated");
   117 		session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   117 		session.send(st.error_reply(stanza, "cancel", "service-unavailable"));
   118 	elseif require_encryption and not session.secure then
   118 		return true;
       
   119 	end
       
   120 
       
   121 	if require_encryption and not session.secure then
   119 		session.send(st.error_reply(stanza, "modify", "policy-violation", "Encryption is required"));
   122 		session.send(st.error_reply(stanza, "modify", "policy-violation", "Encryption is required"));
       
   123 		return true;
       
   124 	end
       
   125 
       
   126 	local query = stanza.tags[1];
       
   127 	if stanza.attr.type == "get" then
       
   128 		local reply = st.reply(stanza);
       
   129 		reply:add_child(registration_query);
       
   130 		session.send(reply);
       
   131 		return true;
       
   132 	end
       
   133 
       
   134 	-- stanza.attr.type == "set"
       
   135 	if query.tags[1] and query.tags[1].name == "remove" then
       
   136 		session.send(st.error_reply(stanza, "auth", "registration-required"));
       
   137 		return true;
       
   138 	end
       
   139 
       
   140 	local data, errors = parse_response(query);
       
   141 	if errors then
       
   142 		log("debug", "Error parsing registration form:");
       
   143 		for field, err in pairs(errors) do
       
   144 			log("debug", "Field %q: %s", field, err);
       
   145 		end
       
   146 		session.send(st.error_reply(stanza, "modify", "not-acceptable"));
       
   147 		return true;
       
   148 	end
       
   149 
       
   150 	local username, password = nodeprep(data.username), data.password;
       
   151 	data.username, data.password = nil, nil;
       
   152 	local host = module.host;
       
   153 	if not username or username == "" then
       
   154 		log("debug", "The requested username is invalid.");
       
   155 		session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
       
   156 		return true;
       
   157 	end
       
   158 
       
   159 	local user = { username = username , host = host, additional = data, ip = session.ip, session = session, allowed = true }
       
   160 	module:fire_event("user-registering", user);
       
   161 	if not user.allowed then
       
   162 		log("debug", "Registration disallowed by module: %s", user.reason or "no reason given");
       
   163 		session.send(st.error_reply(stanza, "modify", "not-acceptable", user.reason));
       
   164 		return true;
       
   165 	end
       
   166 
       
   167 	if usermanager_user_exists(username, host) then
       
   168 		log("debug", "Attempt to register with existing username");
       
   169 		session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
       
   170 		return true;
       
   171 	end
       
   172 
       
   173 	-- TODO unable to write file, file may be locked, etc, what's the correct error?
       
   174 	local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
       
   175 	if usermanager_create_user(username, password, host) then
       
   176 		data.registered = os.time();
       
   177 		if not account_details:set(username, data) then
       
   178 			log("debug", "Could not store extra details");
       
   179 			usermanager_delete_user(username, host);
       
   180 			session.send(error_reply);
       
   181 			return true;
       
   182 		end
       
   183 		session.send(st.reply(stanza)); -- user created!
       
   184 		log("info", "User account created: %s@%s", username, host);
       
   185 		module:fire_event("user-registered", {
       
   186 			username = username, host = host, source = "mod_register",
       
   187 			session = session });
   120 	else
   188 	else
   121 		local query = stanza.tags[1];
   189 		log("debug", "Could not create user");
   122 		if stanza.attr.type == "get" then
   190 		session.send(error_reply);
   123 			local reply = st.reply(stanza);
       
   124 			reply:add_child(registration_query);
       
   125 			session.send(reply);
       
   126 		elseif stanza.attr.type == "set" then
       
   127 			if query.tags[1] and query.tags[1].name == "remove" then
       
   128 				session.send(st.error_reply(stanza, "auth", "registration-required"));
       
   129 			else
       
   130 				local data, errors = parse_response(query);
       
   131 				if errors then
       
   132 					log("debug", "Error parsing registration form:");
       
   133 					for field, err in pairs(errors) do
       
   134 						log("debug", "Field %q: %s", field, err);
       
   135 					end
       
   136 					session.send(st.error_reply(stanza, "modify", "not-acceptable"));
       
   137 				else
       
   138 					local username, password = nodeprep(data.username), data.password;
       
   139 					data.username, data.password = nil, nil;
       
   140 					local host = module.host;
       
   141 					if not username or username == "" then
       
   142 						log("debug", "The requested username is invalid.");
       
   143 						session.send(st.error_reply(stanza, "modify", "not-acceptable", "The requested username is invalid."));
       
   144 						return true;
       
   145 					end
       
   146 					local user = { username = username , host = host, additional = data, ip = session.ip, session = session, allowed = true }
       
   147 					module:fire_event("user-registering", user);
       
   148 					if not user.allowed then
       
   149 						log("debug", "Registration disallowed by module: %s", user.reason or "no reason given");
       
   150 						session.send(st.error_reply(stanza, "modify", "not-acceptable", user.reason));
       
   151 					elseif usermanager_user_exists(username, host) then
       
   152 						log("debug", "Attempt to register with existing username");
       
   153 						session.send(st.error_reply(stanza, "cancel", "conflict", "The requested username already exists."));
       
   154 					else
       
   155 						-- TODO unable to write file, file may be locked, etc, what's the correct error?
       
   156 						local error_reply = st.error_reply(stanza, "wait", "internal-server-error", "Failed to write data to disk.");
       
   157 						if usermanager_create_user(username, password, host) then
       
   158 							data.registered = os.time();
       
   159 							if not account_details:set(username, data) then
       
   160 								log("debug", "Could not store extra details");
       
   161 								usermanager_delete_user(username, host);
       
   162 								session.send(error_reply);
       
   163 								return true;
       
   164 							end
       
   165 							session.send(st.reply(stanza)); -- user created!
       
   166 							log("info", "User account created: %s@%s", username, host);
       
   167 							module:fire_event("user-registered", {
       
   168 								username = username, host = host, source = "mod_register",
       
   169 								session = session });
       
   170 						else
       
   171 							log("debug", "Could not create user");
       
   172 							session.send(error_reply);
       
   173 						end
       
   174 					end
       
   175 				end
       
   176 			end
       
   177 		end
       
   178 	end
   191 	end
   179 	return true;
   192 	return true;
   180 end);
   193 end);