2 -- Copyright (C) 2008-2009 Matthew Wild
3 -- Copyright (C) 2008-2009 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 = util_Jid.split;
17 local load_roster = require "core.rostermanager".load_roster;
18 local to_number = tonumber;
20 function findNamedList (privacy_lists, name)
22 if privacy_lists.lists == nil then
26 for i=1, #privacy_lists.lists do
27 if privacy_lists.lists[i].name == name then
35 function isListUsed(origin, name, privacy_lists)
36 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
37 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
38 if resource ~= origin.resource then
39 if session.activePrivacyList == name then
41 elseif session.activePrivacyList == nil and privacy_lists.default == name then
50 function isAnotherSessionUsingDefaultList(origin)
52 if bare_sessions[origin.username.."@"..origin.host].sessions ~= nil then
53 for resource, session in pairs(bare_sessions[origin.username.."@"..origin.host].sessions) do
54 if resource ~= origin.resource and session.activePrivacyList == nil then
63 function sendUnavailable(origin, to, from)
64 --[[ example unavailable presence stanza
65 <presence from="node@host/resource" type="unavailable" to="node@host" >
66 <status>Logged out</status>
69 local presence = st.presence({from=from, type="unavailable"})
70 presence:tag("status"):text("Logged out");
72 local node, host = jid_bare(to);
73 local bare = node .. "@" .. host;
75 if bare_sessions[bare].sessions ~= nil then
76 for resource, session in pairs(bare_sessions[bare].sessions) do
77 presence.attr.to = session.full_jid;
78 module:log("debug", "send unavailable to: %s; from: %s", tostring(presence.attr.to), tostring(presence.attr.from));
79 origin.send(presence);
84 function sendNeededUnavailablePersences(origin, listnameOrItem) -- TODO implement it correctly!
85 if type(listnameOrItem) == "string" then
86 local listname = listnameOrItem;
87 for _,list in ipairs(privacy_lists.lists) do
88 if list.name == listname then
89 for _,item in ipairs(list.items) do
90 sendNeededUnavailablePersences(origin, item);
94 elseif type(listnameOrItem) == "table" then
95 module:log("debug", "got an item, check wether to send unavailable presence stanza or not");
96 local item = listnameOrItem;
97 local serialize = require "util.serialization".serialize;
100 if item["presence-out"] == true then
101 if item.type == "jid" then
102 sendUnavailable(origin, item.value, origin.full_jid);
103 elseif item.type == "group" then
104 elseif item.type == "subscription" then
105 elseif item.type == nil then
107 elseif item["presence-in"] == true then
108 if item.type == "jid" then
109 sendUnavailable(origin, origin.full_jid, item.value);
110 elseif item.type == "group" then
111 elseif item.type == "subscription" then
112 elseif item.type == nil then
116 module:log("debug", "got unknown type: %s", type(listnameOrItem));
120 function declineList (privacy_lists, origin, stanza, which)
121 if which == "default" then
122 if isAnotherSessionUsingDefaultList(origin) then
123 return { "cancel", "conflict", "Another session is online and using the default list."};
125 privacy_lists.default = nil;
126 origin.send(st.reply(stanza));
127 elseif which == "active" then
128 origin.activePrivacyList = nil;
129 origin.send(st.reply(stanza));
131 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
136 function activateList (privacy_lists, origin, stanza, which, name)
137 local idx = findNamedList(privacy_lists, name);
139 if privacy_lists.default == nil then
140 privacy_lists.default = "";
142 if origin.activePrivacyList == nil then
143 origin.activePrivacyList = "";
146 if which == "default" and idx ~= nil then
147 if isAnotherSessionUsingDefaultList(origin) then
148 return {"cancel", "conflict", "Another session is online and using the default list."};
150 privacy_lists.default = name;
151 origin.send(st.reply(stanza));
153 if origin.activePrivacyList == nil then
154 sendNeededUnavailablePersences(origin, name);
157 elseif which == "active" and idx ~= nil then
158 origin.activePrivacyList = name;
159 origin.send(st.reply(stanza));
160 -- sendNeededUnavailablePersences(origin, name);
162 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
167 function deleteList (privacy_lists, origin, stanza, name)
168 local idx = findNamedList(privacy_lists, name);
171 if isListUsed(origin, name, privacy_lists) then
172 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
174 if privacy_lists.default == name then
175 privacy_lists.default = "";
177 if origin.activePrivacyList == name then
178 origin.activePrivacyList = "";
180 table.remove(privacy_lists.lists, idx);
181 origin.send(st.reply(stanza));
184 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
187 local function sortByOrder(a, b)
188 if a.order < b.order then
194 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
195 local idx = findNamedList(privacy_lists, name);
196 local bare_jid = origin.username.."@"..origin.host;
198 if privacy_lists.lists == nil then
199 privacy_lists.lists = {};
203 idx = #privacy_lists.lists + 1;
206 local orderCheck = {};
211 for _,item in ipairs(entries) do
212 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
213 return {"modify", "bad-request", "Order attribute not valid."};
216 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
217 return {"modify", "bad-request", "Type attribute not valid."};
221 orderCheck[item.attr.order] = true;
223 tmp["type"] = item.attr.type;
224 tmp["value"] = item.attr.value;
225 tmp["action"] = item.attr.action;
226 tmp["order"] = to_number(item.attr.order);
227 tmp["presence-in"] = false;
228 tmp["presence-out"] = false;
229 tmp["message"] = false;
232 if #item.tags > 0 then
233 for _,tag in ipairs(item.tags) do
234 tmp[tag.name] = true;
238 if tmp.type == "group" then
240 local roster = load_roster(origin.username, origin.host);
241 for jid,item in pairs(roster) do
242 if item.groups ~= nil then
243 for group in pairs(item.groups) do
244 if group == tmp.value then
249 if found == true then
254 if found == false then
255 return {"cancel", "item-not-found", "Specifed roster group not existing."};
257 elseif tmp.type == "subscription" then
258 if tmp.value ~= "both" and
259 tmp.value ~= "to" and
260 tmp.value ~= "from" and
261 tmp.value ~= "none" then
262 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
266 if tmp.action ~= "deny" and tmp.action ~= "allow" then
267 return {"cancel", "bad-request", "Action must be either deny or allow."};
271 if (privacy_lists.default == name and origin.activePrivacyList == nil) or origin.activePrivacyList == name then
272 module:log("debug", "calling sendNeededUnavailablePresences!");
273 -- item is valid and list is active, so send needed unavailable stanzas
274 sendNeededUnavailablePersences(origin, tmp);
277 list.items[#list.items + 1] = tmp;
280 table.sort(list, sortByOrder);
282 privacy_lists.lists[idx] = list;
283 origin.send(st.reply(stanza));
284 if bare_sessions[bare_jid] ~= nil then
285 local iq = st.iq ( { type = "set", id="push1" } );
286 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
287 iq:tag ("list", { name = list.name } ):up();
289 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
290 iq.attr.to = bare_jid.."/"..resource
294 return {"cancel", "bad-request", "internal error."};
299 function getList(privacy_lists, origin, stanza, name)
300 local reply = st.reply(stanza);
301 reply:tag("query", {xmlns="jabber:iq:privacy"});
304 reply:tag("active", {name=origin.activePrivacyList or ""}):up();
305 reply:tag("default", {name=privacy_lists.default or ""}):up();
306 if privacy_lists.lists then
307 for _,list in ipairs(privacy_lists.lists) do
308 reply:tag("list", {name=list.name}):up();
312 local idx = findNamedList(privacy_lists, name);
314 local list = privacy_lists.lists[idx];
315 reply = reply:tag("list", {name=list.name});
316 for _,item in ipairs(list.items) do
317 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
318 if item["message"] then reply:tag("message"):up(); end
319 if item["iq"] then reply:tag("iq"):up(); end
320 if item["presence-in"] then reply:tag("presence-in"):up(); end
321 if item["presence-out"] then reply:tag("presence-out"):up(); end
325 return {"cancel", "item-not-found", "Unknown list specified."};
333 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
334 local origin, stanza = data.origin, data.stanza;
336 if stanza.attr.to == nil then -- only service requests to own bare JID
337 local query = stanza.tags[1]; -- the query element
339 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
341 if stanza.attr.type == "set" then
342 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
343 for _,tag in ipairs(query.tags) do
344 if tag.name == "active" or tag.name == "default" then
345 if tag.attr.name == nil then -- Client declines the use of active / default list
346 valid = declineList(privacy_lists, origin, stanza, tag.name);
347 else -- Client requests change of active / default list
348 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
350 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
351 if #tag.tags == 0 then -- Client removes a privacy list
352 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
353 else -- Client edits a privacy list
354 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
359 elseif stanza.attr.type == "get" then
361 local listsToRetrieve = 0;
362 if #query.tags >= 1 then
363 for _,tag in ipairs(query.tags) do
364 if tag.name == "list" then -- Client requests a privacy list from server
365 name = tag.attr.name;
366 listsToRetrieve = listsToRetrieve + 1;
370 if listsToRetrieve == 0 or listsToRetrieve == 1 then
371 valid = getList(privacy_lists, origin, stanza, name);
375 if valid ~= true then
376 if valid[0] == nil then
379 if valid[1] == nil then
380 valid[1] = "bad-request";
382 origin.send(st.error_reply(stanza, valid[0], valid[1], valid[2]));
384 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
391 function checkIfNeedToBeBlocked(e, session)
392 local origin, stanza = e.origin, e.stanza;
393 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
394 local bare_jid = session.username.."@"..session.host;
396 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
398 if stanza.attr.to ~= nil and stanza.attr.from ~= nil then
399 if privacy_lists.lists == nil or
400 (session.activePrivacyList == nil or session.activePrivacyList == "") and
401 (privacy_lists.default == nil or privacy_lists.default == "")
403 return; -- Nothing to block, default is Allow all
405 if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then
406 module:log("debug", "Never block communications from one of a user's resources to another.");
407 return; -- from one of a user's resource to another => HANDS OFF!
413 local listname = session.activePrivacyList;
414 if listname == nil or listname == "" then
415 listname = privacy_lists.default; -- no active list selected, use default list
417 idx = findNamedList(privacy_lists, listname);
419 module:log("error", "given privacy listname not found. name: %s", listname);
422 list = privacy_lists.lists[idx];
424 module:log("info", "privacy list index wrong. index: %d", idx);
427 for _,item in ipairs(list.items) do
431 (stanza.name == "message" and item.message) or
432 (stanza.name == "iq" and item.iq) or
433 (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
434 (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
435 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-in"] == false)
442 if jid_bare(stanza.attr.to) == bare_jid then
443 module:log("debug", "evil jid is (from): %s", stanza.attr.from);
444 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
446 module:log("debug", "evil jid is (to): %s", stanza.attr.to);
447 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
449 if item.type == "jid" and
450 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
451 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
452 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
453 (evilJid.host and item.value == evilJid.host) then
455 block = (item.action == "deny");
456 elseif item.type == "group" then
457 local roster = load_roster(session.username, session.host);
458 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
459 for group in pairs(groups) do
460 if group == item.value then
462 block = (item.action == "deny");
466 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
467 local roster = load_roster(session.username, session.host);
468 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
470 block = (item.action == "deny");
472 elseif item.type == nil then
474 block = (item.action == "deny");
479 module:log("info", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
480 if stanza.name == "message" then
481 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
482 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
483 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
485 return true; -- stanza blocked !
487 module:log("info", "stanza explicit allowed!")
496 function preCheckIncoming(e)
498 if e.stanza.attr.to ~= nil then
499 local node, host, resource = jid_split(e.stanza.attr.to);
500 if node == nil or host == nil then
503 if resource == nil then
506 if bare_sessions[node.."@"..host] ~= nil then
507 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
508 if session_.priority ~= nil and session_.priority > prio then
510 prio = session_.priority;
515 session = full_sessions[node.."@"..host.."/"..resource];
517 if session ~= nil then
518 return checkIfNeedToBeBlocked(e, session);
520 module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource))
526 function preCheckOutgoing(e)
527 local session = e.origin;
528 if e.stanza.attr.from == nil then
529 e.stanza.attr.from = session.username .. "@" .. session.host;
530 if session.resource ~= nil then
531 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
534 return checkIfNeedToBeBlocked(e, session);
538 module:hook("pre-message/full", preCheckOutgoing, 500);
539 module:hook("pre-message/bare", preCheckOutgoing, 500);
540 module:hook("pre-message/host", preCheckOutgoing, 500);
541 module:hook("pre-iq/full", preCheckOutgoing, 500);
542 module:hook("pre-iq/bare", preCheckOutgoing, 500);
543 module:hook("pre-iq/host", preCheckOutgoing, 500);
544 module:hook("pre-presence/full", preCheckOutgoing, 500);
545 module:hook("pre-presence/bare", preCheckOutgoing, 500);
546 module:hook("pre-presence/host", preCheckOutgoing, 500);
548 module:hook("message/full", preCheckIncoming, 500);
549 module:hook("message/bare", preCheckIncoming, 500);
550 module:hook("message/host", preCheckIncoming, 500);
551 module:hook("iq/full", preCheckIncoming, 500);
552 module:hook("iq/bare", preCheckIncoming, 500);
553 module:hook("iq/host", preCheckIncoming, 500);
554 module:hook("presence/full", preCheckIncoming, 500);
555 module:hook("presence/bare", preCheckIncoming, 500);
556 module:hook("presence/host", preCheckIncoming, 500);
558 module:log("info", "mod_privacy loaded ...");