29 local jid_split = require "util.jid".split; |
30 local jid_split = require "util.jid".split; |
30 local print = print; |
31 local print = print; |
31 |
32 |
32 function core_process_stanza(origin, stanza) |
33 function core_process_stanza(origin, stanza) |
33 log("debug", "Received["..origin.type.."]: "..tostring(stanza)) |
34 log("debug", "Received["..origin.type.."]: "..tostring(stanza)) |
|
35 |
34 -- TODO verify validity of stanza (as well as JID validity) |
36 -- TODO verify validity of stanza (as well as JID validity) |
35 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
37 if stanza.name == "iq" and not(#stanza.tags == 1 and stanza.tags[1].attr.xmlns) then |
36 if stanza.attr.type == "set" or stanza.attr.type == "get" then |
38 if stanza.attr.type == "set" or stanza.attr.type == "get" then |
37 error("Invalid IQ"); |
39 error("Invalid IQ"); |
38 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then |
40 elseif #stanza.tags > 1 and not(stanza.attr.type == "error" or stanza.attr.type == "result") then |
44 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" |
46 and not(stanza.name == "iq" and stanza.tags[1].name == "bind" |
45 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then |
47 and stanza.tags[1].attr.xmlns == "urn:ietf:params:xml:ns:xmpp-bind") then |
46 error("Client MUST bind resource after auth"); |
48 error("Client MUST bind resource after auth"); |
47 end |
49 end |
48 |
50 |
49 local to = stanza.attr.to; |
|
50 -- TODO also, stazas should be returned to their original state before the function ends |
51 -- TODO also, stazas should be returned to their original state before the function ends |
51 if origin.type == "c2s" then |
52 if origin.type == "c2s" then |
52 stanza.attr.from = origin.full_jid; -- quick fix to prevent impersonation (FIXME this would be incorrect when the origin is not c2s) |
53 stanza.attr.from = origin.full_jid; |
53 end |
54 end |
54 |
55 local to = stanza.attr.to; |
|
56 local node, host, resource = jid_split(to); |
|
57 local to_bare = node and (node.."@"..host) or host; -- bare JID |
|
58 local from = stanza.attr.from; |
|
59 local from_node, from_host, from_resource = jid_split(from); |
|
60 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID |
|
61 |
|
62 if origin.type == "s2sin" then |
|
63 if origin.host ~= from_host then -- remote server trying to impersonate some other server? |
|
64 return; -- FIXME what should we do here? does this work with subdomains? |
|
65 end |
|
66 end |
|
67 if to and not(hosts[to]) and not(hosts[to_bare]) and (not(hosts[host]) or hosts[host].type ~= "local") then -- not for us? |
|
68 return; -- FIXME what should we do here? |
|
69 end |
|
70 |
|
71 -- FIXME do stanzas not of jabber:client get handled by components? |
55 if not to then |
72 if not to then |
56 core_handle_stanza(origin, stanza); |
73 core_handle_stanza(origin, stanza); |
|
74 elseif hosts[to] and hosts[to].type == "local" then -- directed at a local server |
|
75 core_handle_stanza(origin, stanza); |
|
76 elseif hosts[to_bare] and hosts[to_bare].type == "component" then -- hack to allow components to handle node@server |
|
77 component_handle_stanza(origin, stanza); |
|
78 elseif hosts[to] and hosts[to].type == "component" then -- hack to allow components to handle node@server/resource and server/resource |
|
79 component_handle_stanza(origin, stanza); |
|
80 elseif hosts[host].type == "component" then -- directed at a component |
|
81 component_handle_stanza(origin, stanza); |
57 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then |
82 elseif origin.type == "c2s" and stanza.name == "presence" and stanza.attr.type ~= nil and stanza.attr.type ~= "unavailable" then |
58 local node, host = jid_split(stanza.attr.to); |
|
59 local to_bare = node and (node.."@"..host) or host; -- bare JID |
|
60 local from_node, from_host = jid_split(stanza.attr.from); |
|
61 local from_bare = from_node and (from_node.."@"..from_host) or from_host; -- bare JID |
|
62 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); |
83 handle_outbound_presence_subscriptions_and_probes(origin, stanza, from_bare, to_bare); |
63 elseif hosts[to] and hosts[to].type == "local" then |
84 elseif stanza.name == "iq" and not resource then -- directed at bare JID |
64 core_handle_stanza(origin, stanza); |
|
65 elseif stanza.name == "iq" and not select(3, jid_split(to)) then |
|
66 core_handle_stanza(origin, stanza); |
85 core_handle_stanza(origin, stanza); |
67 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then |
86 elseif stanza.attr.xmlns and stanza.attr.xmlns ~= "jabber:client" and stanza.attr.xmlns ~= "jabber:server" then |
68 modules_handle_stanza(origin, stanza); |
87 modules_handle_stanza(origin, stanza); |
69 elseif origin.type == "c2s" or origin.type == "s2sin" then |
88 elseif origin.type == "c2s" or origin.type == "s2sin" then |
70 core_route_stanza(origin, stanza); |
89 core_route_stanza(origin, stanza); |
|
90 else |
|
91 log("warn", "stanza not processed"); |
71 end |
92 end |
72 end |
93 end |
73 |
94 |
74 -- This function handles stanzas which are not routed any further, |
95 -- This function handles stanzas which are not routed any further, |
75 -- that is, they are handled by this server |
96 -- that is, they are handled by this server |