2 -- Copyright (C) 2009-2010 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain
4 -- Copyright (C) 2009 Thilo Cestonaro
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
10 local prosody = prosody;
11 local st = require "util.stanza";
12 local datamanager = require "util.datamanager";
13 local bare_sessions, full_sessions = bare_sessions, full_sessions;
14 local util_Jid = require "util.jid";
15 local jid_bare = util_Jid.bare;
16 local jid_split, jid_join = util_Jid.split, util_Jid.join;
17 local load_roster = require "core.rostermanager".load_roster;
18 local to_number = tonumber;
20 function isListUsed(origin, name, privacy_lists)
21 local user = bare_sessions[origin.username.."@"..origin.host];
23 for resource, session in pairs(user.sessions) do
24 if resource ~= origin.resource then
25 if session.activePrivacyList == name then
27 elseif session.activePrivacyList == nil and privacy_lists.default == name then
35 function isAnotherSessionUsingDefaultList(origin)
36 local user = bare_sessions[origin.username.."@"..origin.host];
38 for resource, session in pairs(user.sessions) do
39 if resource ~= origin.resource and session.activePrivacyList == nil then
46 function sendUnavailable(origin, to, from)
47 --[[ example unavailable presence stanza
48 <presence from="node@host/resource" type="unavailable" to="node@host" >
49 <status>Logged out</status>
52 local presence = st.presence({from=from, type="unavailable"});
53 presence:tag("status"):text("Logged out");
55 local node, host = jid_bare(to);
56 local bare = node .. "@" .. host;
58 local user = bare_sessions[bare];
60 for resource, session in pairs(user.sessions) do
61 presence.attr.to = session.full_jid;
62 module:log("debug", "send unavailable to: %s; from: %s", tostring(presence.attr.to), tostring(presence.attr.from));
63 origin.send(presence);
68 function declineList(privacy_lists, origin, stanza, which)
69 if which == "default" then
70 if isAnotherSessionUsingDefaultList(origin) then
71 return { "cancel", "conflict", "Another session is online and using the default list."};
73 privacy_lists.default = nil;
74 origin.send(st.reply(stanza));
75 elseif which == "active" then
76 origin.activePrivacyList = nil;
77 origin.send(st.reply(stanza));
79 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
84 function activateList(privacy_lists, origin, stanza, which, name)
85 local list = privacy_lists.lists[name];
87 if which == "default" and list then
88 if isAnotherSessionUsingDefaultList(origin) then
89 return {"cancel", "conflict", "Another session is online and using the default list."};
91 privacy_lists.default = name;
92 origin.send(st.reply(stanza));
93 elseif which == "active" and list then
94 origin.activePrivacyList = name;
95 origin.send(st.reply(stanza));
97 return {"cancel", "item-not-found", "No such list: "..name};
99 return {"modify", "bad-request", "No list chosen to be active or default."};
104 function deleteList(privacy_lists, origin, stanza, name)
105 local list = privacy_lists.lists[name];
108 if isListUsed(origin, name, privacy_lists) then
109 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
111 if privacy_lists.default == name then
112 privacy_lists.default = nil;
114 if origin.activePrivacyList == name then
115 origin.activePrivacyList = nil;
117 privacy_lists.lists[name] = nil;
118 origin.send(st.reply(stanza));
121 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
124 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
125 local bare_jid = origin.username.."@"..origin.host;
127 if privacy_lists.lists == nil then
128 privacy_lists.lists = {};
132 privacy_lists.lists[name] = list;
134 local orderCheck = {};
138 for _,item in ipairs(entries) do
139 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
140 return {"modify", "bad-request", "Order attribute not valid."};
143 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
144 return {"modify", "bad-request", "Type attribute not valid."};
148 orderCheck[item.attr.order] = true;
150 tmp["type"] = item.attr.type;
151 tmp["value"] = item.attr.value;
152 tmp["action"] = item.attr.action;
153 tmp["order"] = to_number(item.attr.order);
154 tmp["presence-in"] = false;
155 tmp["presence-out"] = false;
156 tmp["message"] = false;
159 if #item.tags > 0 then
160 for _,tag in ipairs(item.tags) do
161 tmp[tag.name] = true;
165 if tmp.type == "subscription" then
166 if tmp.value ~= "both" and
167 tmp.value ~= "to" and
168 tmp.value ~= "from" and
169 tmp.value ~= "none" then
170 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
174 if tmp.action ~= "deny" and tmp.action ~= "allow" then
175 return {"cancel", "bad-request", "Action must be either deny or allow."};
177 list.items[#list.items + 1] = tmp;
180 table.sort(list, function(a, b) return a.order < b.order; end);
182 origin.send(st.reply(stanza));
183 if bare_sessions[bare_jid] ~= nil then
184 local iq = st.iq ( { type = "set", id="push1" } );
185 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
186 iq:tag ("list", { name = list.name } ):up();
188 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
189 iq.attr.to = bare_jid.."/"..resource
193 return {"cancel", "bad-request", "internal error."};
198 function getList(privacy_lists, origin, stanza, name)
199 local reply = st.reply(stanza);
200 reply:tag("query", {xmlns="jabber:iq:privacy"});
203 if privacy_lists.lists then
204 if origin.ActivePrivacyList then
205 reply:tag("active", {name=origin.activePrivacyList}):up();
207 if privacy_lists.default then
208 reply:tag("default", {name=privacy_lists.default}):up();
210 for name,list in pairs(privacy_lists.lists) do
211 reply:tag("list", {name=name}):up();
215 local list = privacy_lists.lists[name];
217 reply = reply:tag("list", {name=list.name});
218 for _,item in ipairs(list.items) do
219 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
220 if item["message"] then reply:tag("message"):up(); end
221 if item["iq"] then reply:tag("iq"):up(); end
222 if item["presence-in"] then reply:tag("presence-in"):up(); end
223 if item["presence-out"] then reply:tag("presence-out"):up(); end
227 return {"cancel", "item-not-found", "Unknown list specified."};
235 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
236 local origin, stanza = data.origin, data.stanza;
238 if stanza.attr.to == nil then -- only service requests to own bare JID
239 local query = stanza.tags[1]; -- the query element
241 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or { lists = {} };
243 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
244 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
245 local lists = privacy_lists.lists;
246 for idx, list in ipairs(lists) do
247 lists[list.name] = list;
252 if stanza.attr.type == "set" then
253 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
254 for _,tag in ipairs(query.tags) do
255 if tag.name == "active" or tag.name == "default" then
256 if tag.attr.name == nil then -- Client declines the use of active / default list
257 valid = declineList(privacy_lists, origin, stanza, tag.name);
258 else -- Client requests change of active / default list
259 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
261 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
262 if #tag.tags == 0 then -- Client removes a privacy list
263 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
264 else -- Client edits a privacy list
265 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
270 elseif stanza.attr.type == "get" then
272 local listsToRetrieve = 0;
273 if #query.tags >= 1 then
274 for _,tag in ipairs(query.tags) do
275 if tag.name == "list" then -- Client requests a privacy list from server
276 name = tag.attr.name;
277 listsToRetrieve = listsToRetrieve + 1;
281 if listsToRetrieve == 0 or listsToRetrieve == 1 then
282 valid = getList(privacy_lists, origin, stanza, name);
286 if valid ~= true then
287 valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
288 if valid[1] == nil then
291 if valid[2] == nil then
292 valid[2] = "bad-request";
294 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
296 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
302 function checkIfNeedToBeBlocked(e, session)
303 local origin, stanza = e.origin, e.stanza;
304 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
305 local bare_jid = session.username.."@"..session.host;
306 local to = stanza.attr.to or bare_jid;
307 local from = stanza.attr.from;
309 local is_to_user = bare_jid == jid_bare(to);
310 local is_from_user = bare_jid == jid_bare(from);
312 --module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
314 if privacy_lists.lists == nil or
315 not (session.activePrivacyList or privacy_lists.default)
317 return; -- Nothing to block, default is Allow all
319 if is_from_user and is_to_user then
320 --module:log("debug", "Not blocking communications between user's resources");
321 return; -- from one of a user's resource to another => HANDS OFF!
325 local listname = session.activePrivacyList;
326 if listname == nil then
327 listname = privacy_lists.default; -- no active list selected, use default list
329 local list = privacy_lists.lists[listname];
330 if not list then -- should never happen
331 module:log("warn", "given privacy list not found. name: %s for user %s", listname, bare_jid);
334 for _,item in ipairs(list.items) do
338 (stanza.name == "message" and item.message) or
339 (stanza.name == "iq" and item.iq) or
340 (stanza.name == "presence" and is_to_user and item["presence-in"]) or
341 (stanza.name == "presence" and is_from_user and item["presence-out"]) or
342 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
350 --module:log("debug", "evil jid is (from): %s", from);
351 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
353 --module:log("debug", "evil jid is (to): %s", to);
354 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
356 if item.type == "jid" and
357 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
358 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
359 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
360 (evilJid.host and item.value == evilJid.host) then
362 block = (item.action == "deny");
363 elseif item.type == "group" then
364 local roster = load_roster(session.username, session.host);
365 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
367 local groups = roster_entry.groups;
368 for group in pairs(groups) do
369 if group == item.value then
371 block = (item.action == "deny");
376 elseif item.type == "subscription" then -- we need a valid bare evil jid
377 local roster = load_roster(session.username, session.host);
378 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
379 if (not(roster_entry) and item.value == "none")
380 or (roster_entry and roster_entry.subscription == item.value) then
382 block = (item.action == "deny");
384 elseif item.type == nil then
386 block = (item.action == "deny");
391 module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
392 if stanza.name == "message" then
393 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
394 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
395 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
397 return true; -- stanza blocked !
399 --module:log("debug", "stanza explicitly allowed!")
406 function preCheckIncoming(e)
408 if e.stanza.attr.to ~= nil then
409 local node, host, resource = jid_split(e.stanza.attr.to);
410 if node == nil or host == nil then
413 if resource == nil then
416 if bare_sessions[node.."@"..host] ~= nil then
417 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
418 if session_.priority ~= nil and session_.priority > prio then
420 prio = session_.priority;
425 session = full_sessions[node.."@"..host.."/"..resource];
427 if session ~= nil then
428 return checkIfNeedToBeBlocked(e, session);
430 --module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
435 function preCheckOutgoing(e)
436 local session = e.origin;
437 if e.stanza.attr.from == nil then
438 e.stanza.attr.from = session.username .. "@" .. session.host;
439 if session.resource ~= nil then
440 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
443 return checkIfNeedToBeBlocked(e, session);
446 module:hook("pre-message/full", preCheckOutgoing, 500);
447 module:hook("pre-message/bare", preCheckOutgoing, 500);
448 module:hook("pre-message/host", preCheckOutgoing, 500);
449 module:hook("pre-iq/full", preCheckOutgoing, 500);
450 module:hook("pre-iq/bare", preCheckOutgoing, 500);
451 module:hook("pre-iq/host", preCheckOutgoing, 500);
452 module:hook("pre-presence/full", preCheckOutgoing, 500);
453 module:hook("pre-presence/bare", preCheckOutgoing, 500);
454 module:hook("pre-presence/host", preCheckOutgoing, 500);
456 module:hook("message/full", preCheckIncoming, 500);
457 module:hook("message/bare", preCheckIncoming, 500);
458 module:hook("message/host", preCheckIncoming, 500);
459 module:hook("iq/full", preCheckIncoming, 500);
460 module:hook("iq/bare", preCheckIncoming, 500);
461 module:hook("iq/host", preCheckIncoming, 500);
462 module:hook("presence/full", preCheckIncoming, 500);
463 module:hook("presence/bare", preCheckIncoming, 500);
464 module:hook("presence/host", preCheckIncoming, 500);