comparison plugins/mod_c2s.lua @ 10563:e8db377a2983

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Tue, 24 Dec 2019 00:39:45 +0100
parents 09697a673015
children fa2a89132dfb
comparison
equal deleted inserted replaced
10562:670afc079f68 10563:e8db377a2983
54 --- Stream events handlers 54 --- Stream events handlers
55 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'}; 55 local stream_xmlns_attr = {xmlns='urn:ietf:params:xml:ns:xmpp-streams'};
56 56
57 function stream_callbacks.streamopened(session, attr) 57 function stream_callbacks.streamopened(session, attr)
58 local send = session.send; 58 local send = session.send;
59 if not attr.to then
60 session:close{ condition = "improper-addressing",
61 text = "A 'to' attribute is required on stream headers" };
62 return;
63 end
59 local host = nameprep(attr.to); 64 local host = nameprep(attr.to);
60 if not host then 65 if not host then
61 session:close{ condition = "improper-addressing", 66 session:close{ condition = "improper-addressing",
62 text = "A valid 'to' attribute is required on stream headers" }; 67 text = "A valid 'to' attribute is required on stream headers" };
63 return; 68 return;
95 local info = sock:info(); 100 local info = sock:info();
96 (session.log or log)("info", "Stream encrypted (%s with %s)", info.protocol, info.cipher); 101 (session.log or log)("info", "Stream encrypted (%s with %s)", info.protocol, info.cipher);
97 session.compressed = info.compression; 102 session.compressed = info.compression;
98 else 103 else
99 (session.log or log)("info", "Stream encrypted"); 104 (session.log or log)("info", "Stream encrypted");
100 session.compressed = sock.compression and sock:compression(); --COMPAT mw/luasec-hg
101 end 105 end
102 end 106 end
103 107
104 local features = st.stanza("stream:features"); 108 local features = st.stanza("stream:features");
105 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features }); 109 hosts[session.host].events.fire_event("stream-features", { origin = session, features = features });
106 if features.tags[1] or session.full_jid then 110 if features.tags[1] or session.full_jid then
107 send(features); 111 send(features);
108 else 112 else
109 (session.log or log)("warn", "No stream features to offer"); 113 if session.secure then
114 -- Normally STARTTLS would be offered
115 (session.log or log)("warn", "No stream features to offer on secure session. Check authentication settings.");
116 else
117 -- Here SASL should be offered
118 (session.log or log)("warn", "No stream features to offer on insecure session. Check encryption and security settings.");
119 end
110 session:close{ condition = "undefined-condition", text = "No stream features to proceed with" }; 120 session:close{ condition = "undefined-condition", text = "No stream features to proceed with" };
111 end 121 end
112 end 122 end
113 123
114 function stream_callbacks.streamclosed(session) 124 function stream_callbacks.streamclosed(session)
119 function stream_callbacks.error(session, error, data) 129 function stream_callbacks.error(session, error, data)
120 if error == "no-stream" then 130 if error == "no-stream" then
121 session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}"))); 131 session.log("debug", "Invalid opening stream header (%s)", (data:gsub("^([^\1]+)\1", "{%1}")));
122 session:close("invalid-namespace"); 132 session:close("invalid-namespace");
123 elseif error == "parse-error" then 133 elseif error == "parse-error" then
124 (session.log or log)("debug", "Client XML parse error: %s", tostring(data)); 134 (session.log or log)("debug", "Client XML parse error: %s", data);
125 session:close("not-well-formed"); 135 session:close("not-well-formed");
126 elseif error == "stream-error" then 136 elseif error == "stream-error" then
127 local condition, text = "undefined-condition"; 137 local condition, text = "undefined-condition";
128 for child in data:childtags(nil, xmlns_xmpp_streams) do 138 for child in data:childtags(nil, xmlns_xmpp_streams) do
129 if child.name ~= "text" then 139 if child.name ~= "text" then
249 259
250 -- Check if TLS compression is used 260 -- Check if TLS compression is used
251 local sock = conn:socket(); 261 local sock = conn:socket();
252 if sock.info then 262 if sock.info then
253 session.compressed = sock:info"compression"; 263 session.compressed = sock:info"compression";
254 elseif sock.compression then
255 session.compressed = sock:compression(); --COMPAT mw/luasec-hg
256 end 264 end
257 end 265 end
258 266
259 if opt_keepalives then 267 if opt_keepalives then
260 conn:setoption("keepalive", opt_keepalives); 268 conn:setoption("keepalive", opt_keepalives);
281 if data then 289 if data then
282 data = filter("bytes/in", data); 290 data = filter("bytes/in", data);
283 if data then 291 if data then
284 local ok, err = stream:feed(data); 292 local ok, err = stream:feed(data);
285 if not ok then 293 if not ok then
286 log("debug", "Received invalid XML (%s) %d bytes: %s", tostring(err), #data, data:sub(1, 300):gsub("[\r\n]+", " "):gsub("[%z\1-\31]", "_")); 294 log("debug", "Received invalid XML (%s) %d bytes: %q", err, #data, data:sub(1, 300));
287 session:close("not-well-formed"); 295 session:close("not-well-formed");
288 end 296 end
289 end 297 end
290 end 298 end
291 end 299 end
325 if session then 333 if session then
326 return (hosts[session.host] or prosody).events.fire_event("c2s-read-timeout", { session = session }); 334 return (hosts[session.host] or prosody).events.fire_event("c2s-read-timeout", { session = session });
327 end 335 end
328 end 336 end
329 337
338 function listener.ondrain(conn)
339 local session = sessions[conn];
340 if session then
341 return (hosts[session.host] or prosody).events.fire_event("c2s-ondrain", { session = session });
342 end
343 end
344
330 local function keepalive(event) 345 local function keepalive(event)
331 local session = event.session; 346 local session = event.session;
332 if not session.notopen then 347 if not session.notopen then
333 return event.session.send(' '); 348 return event.session.send(' ');
334 end 349 end
357 name = "c2s"; 372 name = "c2s";
358 listener = listener; 373 listener = listener;
359 default_port = 5222; 374 default_port = 5222;
360 encryption = "starttls"; 375 encryption = "starttls";
361 multiplex = { 376 multiplex = {
377 protocol = "xmpp-client";
362 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>"; 378 pattern = "^<.*:stream.*%sxmlns%s*=%s*(['\"])jabber:client%1.*>";
363 }; 379 };
364 }); 380 });
365 381
366 module:provides("net", { 382 module:provides("net", {