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)
21 if privacy_lists.lists then
22 for i=1,#privacy_lists.lists do
23 if privacy_lists.lists[i].name == name then
30 function isListUsed(origin, name, privacy_lists)
31 local user = bare_sessions[origin.username.."@"..origin.host];
33 for resource, session in pairs(user.sessions) do
34 if resource ~= origin.resource then
35 if session.activePrivacyList == name then
37 elseif session.activePrivacyList == nil and privacy_lists.default == name then
45 function isAnotherSessionUsingDefaultList(origin)
46 local user = bare_sessions[origin.username.."@"..origin.host];
48 for resource, session in pairs(user.sessions) do
49 if resource ~= origin.resource and session.activePrivacyList == nil then
56 function sendUnavailable(origin, to, from)
57 --[[ example unavailable presence stanza
58 <presence from="node@host/resource" type="unavailable" to="node@host" >
59 <status>Logged out</status>
62 local presence = st.presence({from=from, type="unavailable"});
63 presence:tag("status"):text("Logged out");
65 local node, host = jid_bare(to);
66 local bare = node .. "@" .. host;
68 local user = bare_sessions[bare];
70 for resource, session in pairs(user.sessions) do
71 presence.attr.to = session.full_jid;
72 module:log("debug", "send unavailable to: %s; from: %s", tostring(presence.attr.to), tostring(presence.attr.from));
73 origin.send(presence);
78 function sendNeededUnavailablePersences(origin, listnameOrItem) -- TODO implement it correctly!
79 if type(listnameOrItem) == "string" then
80 local listname = listnameOrItem;
81 for _,list in ipairs(privacy_lists.lists) do
82 if list.name == listname then
83 for _,item in ipairs(list.items) do
84 sendNeededUnavailablePersences(origin, item);
88 elseif type(listnameOrItem) == "table" then
89 module:log("debug", "got an item, check whether to send unavailable presence stanza or not");
90 local item = listnameOrItem;
92 if item["presence-out"] == true then
93 if item.type == "jid" then
94 sendUnavailable(origin, item.value, origin.full_jid);
95 elseif item.type == "group" then
96 elseif item.type == "subscription" then
97 elseif item.type == nil then
99 elseif item["presence-in"] == true then
100 if item.type == "jid" then
101 sendUnavailable(origin, origin.full_jid, item.value);
102 elseif item.type == "group" then
103 elseif item.type == "subscription" then
104 elseif item.type == nil then
108 module:log("debug", "got unknown type: %s", type(listnameOrItem));
112 function declineList(privacy_lists, origin, stanza, which)
113 if which == "default" then
114 if isAnotherSessionUsingDefaultList(origin) then
115 return { "cancel", "conflict", "Another session is online and using the default list."};
117 privacy_lists.default = nil;
118 origin.send(st.reply(stanza));
119 elseif which == "active" then
120 origin.activePrivacyList = nil;
121 origin.send(st.reply(stanza));
123 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
128 function activateList(privacy_lists, origin, stanza, which, name)
129 local idx = findNamedList(privacy_lists, name);
131 if privacy_lists.default == nil then
132 privacy_lists.default = "";
134 if origin.activePrivacyList == nil then
135 origin.activePrivacyList = "";
138 if which == "default" and idx ~= nil then
139 if isAnotherSessionUsingDefaultList(origin) then
140 return {"cancel", "conflict", "Another session is online and using the default list."};
142 privacy_lists.default = name;
143 origin.send(st.reply(stanza));
145 if origin.activePrivacyList == nil then
146 sendNeededUnavailablePersences(origin, name);
149 elseif which == "active" and idx ~= nil then
150 origin.activePrivacyList = name;
151 origin.send(st.reply(stanza));
152 -- sendNeededUnavailablePersences(origin, name);
154 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
159 function deleteList(privacy_lists, origin, stanza, name)
160 local idx = findNamedList(privacy_lists, name);
163 if isListUsed(origin, name, privacy_lists) then
164 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
166 if privacy_lists.default == name then
167 privacy_lists.default = "";
169 if origin.activePrivacyList == name then
170 origin.activePrivacyList = "";
172 table.remove(privacy_lists.lists, idx);
173 origin.send(st.reply(stanza));
176 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
179 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
180 local idx = findNamedList(privacy_lists, name);
181 local bare_jid = origin.username.."@"..origin.host;
183 if privacy_lists.lists == nil then
184 privacy_lists.lists = {};
188 idx = #privacy_lists.lists + 1;
191 local orderCheck = {};
196 for _,item in ipairs(entries) do
197 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
198 return {"modify", "bad-request", "Order attribute not valid."};
201 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
202 return {"modify", "bad-request", "Type attribute not valid."};
206 orderCheck[item.attr.order] = true;
208 tmp["type"] = item.attr.type;
209 tmp["value"] = item.attr.value;
210 tmp["action"] = item.attr.action;
211 tmp["order"] = to_number(item.attr.order);
212 tmp["presence-in"] = false;
213 tmp["presence-out"] = false;
214 tmp["message"] = false;
217 if #item.tags > 0 then
218 for _,tag in ipairs(item.tags) do
219 tmp[tag.name] = true;
223 if tmp.type == "group" then
225 local roster = load_roster(origin.username, origin.host);
226 for jid,item in pairs(roster) do
227 if item.groups ~= nil then
228 for group in pairs(item.groups) do
229 if group == tmp.value then
234 if found == true then
239 if found == false then
240 return {"cancel", "item-not-found", "Specifed roster group not existing."};
242 elseif tmp.type == "subscription" then
243 if tmp.value ~= "both" and
244 tmp.value ~= "to" and
245 tmp.value ~= "from" and
246 tmp.value ~= "none" then
247 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
251 if tmp.action ~= "deny" and tmp.action ~= "allow" then
252 return {"cancel", "bad-request", "Action must be either deny or allow."};
256 if (privacy_lists.default == name and origin.activePrivacyList == nil) or origin.activePrivacyList == name then
257 module:log("debug", "calling sendNeededUnavailablePresences!");
258 -- item is valid and list is active, so send needed unavailable stanzas
259 sendNeededUnavailablePersences(origin, tmp);
262 list.items[#list.items + 1] = tmp;
265 table.sort(list, function(a, b) return a.order < b.order; end);
267 privacy_lists.lists[idx] = list;
268 origin.send(st.reply(stanza));
269 if bare_sessions[bare_jid] ~= nil then
270 local iq = st.iq ( { type = "set", id="push1" } );
271 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
272 iq:tag ("list", { name = list.name } ):up();
274 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
275 iq.attr.to = bare_jid.."/"..resource
279 return {"cancel", "bad-request", "internal error."};
284 function getList(privacy_lists, origin, stanza, name)
285 local reply = st.reply(stanza);
286 reply:tag("query", {xmlns="jabber:iq:privacy"});
289 reply:tag("active", {name=origin.activePrivacyList or ""}):up();
290 reply:tag("default", {name=privacy_lists.default or ""}):up();
291 if privacy_lists.lists then
292 for _,list in ipairs(privacy_lists.lists) do
293 reply:tag("list", {name=list.name}):up();
297 local idx = findNamedList(privacy_lists, name);
299 local list = privacy_lists.lists[idx];
300 reply = reply:tag("list", {name=list.name});
301 for _,item in ipairs(list.items) do
302 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
303 if item["message"] then reply:tag("message"):up(); end
304 if item["iq"] then reply:tag("iq"):up(); end
305 if item["presence-in"] then reply:tag("presence-in"):up(); end
306 if item["presence-out"] then reply:tag("presence-out"):up(); end
310 return {"cancel", "item-not-found", "Unknown list specified."};
318 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
319 local origin, stanza = data.origin, data.stanza;
321 if stanza.attr.to == nil then -- only service requests to own bare JID
322 local query = stanza.tags[1]; -- the query element
324 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or {};
326 if stanza.attr.type == "set" then
327 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
328 for _,tag in ipairs(query.tags) do
329 if tag.name == "active" or tag.name == "default" then
330 if tag.attr.name == nil then -- Client declines the use of active / default list
331 valid = declineList(privacy_lists, origin, stanza, tag.name);
332 else -- Client requests change of active / default list
333 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
335 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
336 if #tag.tags == 0 then -- Client removes a privacy list
337 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
338 else -- Client edits a privacy list
339 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
344 elseif stanza.attr.type == "get" then
346 local listsToRetrieve = 0;
347 if #query.tags >= 1 then
348 for _,tag in ipairs(query.tags) do
349 if tag.name == "list" then -- Client requests a privacy list from server
350 name = tag.attr.name;
351 listsToRetrieve = listsToRetrieve + 1;
355 if listsToRetrieve == 0 or listsToRetrieve == 1 then
356 valid = getList(privacy_lists, origin, stanza, name);
360 if valid ~= true then
361 if valid[0] == nil then
364 if valid[1] == nil then
365 valid[1] = "bad-request";
367 origin.send(st.error_reply(stanza, valid[0], valid[1], valid[2]));
369 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
375 function checkIfNeedToBeBlocked(e, session)
376 local origin, stanza = e.origin, e.stanza;
377 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
378 local bare_jid = session.username.."@"..session.host;
379 local to = stanza.attr.to;
380 local from = stanza.attr.from;
382 local to_user = bare_jid == jid_bare(to);
383 local from_user = bare_jid == jid_bare(from);
385 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
387 if privacy_lists.lists == nil or
388 (session.activePrivacyList == nil or session.activePrivacyList == "") and
389 (privacy_lists.default == nil or privacy_lists.default == "")
391 return; -- Nothing to block, default is Allow all
393 if from_user and to_user then
394 module:log("debug", "Not blocking communications between user's resources");
395 return; -- from one of a user's resource to another => HANDS OFF!
401 local listname = session.activePrivacyList;
402 if listname == nil or listname == "" then
403 listname = privacy_lists.default; -- no active list selected, use default list
405 idx = findNamedList(privacy_lists, listname);
407 module:log("debug", "given privacy listname not found. name: %s", listname);
410 list = privacy_lists.lists[idx];
412 module:log("debug", "privacy list index wrong. index: %d", idx);
415 for _,item in ipairs(list.items) do
419 (stanza.name == "message" and item.message) or
420 (stanza.name == "iq" and item.iq) or
421 (stanza.name == "presence" and to_user and item["presence-in"]) or
422 (stanza.name == "presence" and from_user and item["presence-out"]) or
423 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
431 module:log("debug", "evil jid is (from): %s", from);
432 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
434 module:log("debug", "evil jid is (to): %s", to);
435 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
437 if item.type == "jid" and
438 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
439 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
440 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
441 (evilJid.host and item.value == evilJid.host) then
443 block = (item.action == "deny");
444 elseif item.type == "group" then
445 local roster = load_roster(session.username, session.host);
446 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
447 for group in pairs(groups) do
448 if group == item.value then
450 block = (item.action == "deny");
454 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
455 local roster = load_roster(session.username, session.host);
456 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
458 block = (item.action == "deny");
460 elseif item.type == nil then
462 block = (item.action == "deny");
467 module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
468 if stanza.name == "message" then
469 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
470 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
471 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
473 return true; -- stanza blocked !
475 module:log("debug", "stanza explicitly allowed!")
482 function preCheckIncoming(e)
484 if e.stanza.attr.to ~= nil then
485 local node, host, resource = jid_split(e.stanza.attr.to);
486 if node == nil or host == nil then
489 if resource == nil then
492 if bare_sessions[node.."@"..host] ~= nil then
493 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
494 if session_.priority ~= nil and session_.priority > prio then
496 prio = session_.priority;
501 session = full_sessions[node.."@"..host.."/"..resource];
503 if session ~= nil then
504 return checkIfNeedToBeBlocked(e, session);
506 module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
511 function preCheckOutgoing(e)
512 local session = e.origin;
513 if e.stanza.attr.from == nil then
514 e.stanza.attr.from = session.username .. "@" .. session.host;
515 if session.resource ~= nil then
516 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
519 return checkIfNeedToBeBlocked(e, session);
522 module:hook("pre-message/full", preCheckOutgoing, 500);
523 module:hook("pre-message/bare", preCheckOutgoing, 500);
524 module:hook("pre-message/host", preCheckOutgoing, 500);
525 module:hook("pre-iq/full", preCheckOutgoing, 500);
526 module:hook("pre-iq/bare", preCheckOutgoing, 500);
527 module:hook("pre-iq/host", preCheckOutgoing, 500);
528 module:hook("pre-presence/full", preCheckOutgoing, 500);
529 module:hook("pre-presence/bare", preCheckOutgoing, 500);
530 module:hook("pre-presence/host", preCheckOutgoing, 500);
532 module:hook("message/full", preCheckIncoming, 500);
533 module:hook("message/bare", preCheckIncoming, 500);
534 module:hook("message/host", preCheckIncoming, 500);
535 module:hook("iq/full", preCheckIncoming, 500);
536 module:hook("iq/bare", preCheckIncoming, 500);
537 module:hook("iq/host", preCheckIncoming, 500);
538 module:hook("presence/full", preCheckIncoming, 500);
539 module:hook("presence/bare", preCheckIncoming, 500);
540 module:hook("presence/host", preCheckIncoming, 500);