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 wether 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;
380 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
382 if stanza.attr.to ~= nil and stanza.attr.from ~= nil then
383 if privacy_lists.lists == nil or
384 (session.activePrivacyList == nil or session.activePrivacyList == "") and
385 (privacy_lists.default == nil or privacy_lists.default == "")
387 return; -- Nothing to block, default is Allow all
389 if jid_bare(stanza.attr.from) == bare_jid and jid_bare(stanza.attr.to) == bare_jid then
390 module:log("debug", "Never block communications from one of a user's resources to another.");
391 return; -- from one of a user's resource to another => HANDS OFF!
397 local listname = session.activePrivacyList;
398 if listname == nil or listname == "" then
399 listname = privacy_lists.default; -- no active list selected, use default list
401 idx = findNamedList(privacy_lists, listname);
403 module:log("error", "given privacy listname not found. name: %s", listname);
406 list = privacy_lists.lists[idx];
408 module:log("info", "privacy list index wrong. index: %d", idx);
411 for _,item in ipairs(list.items) do
415 (stanza.name == "message" and item.message) or
416 (stanza.name == "iq" and item.iq) or
417 (stanza.name == "presence" and jid_bare(stanza.attr.to) == bare_jid and item["presence-in"]) or
418 (stanza.name == "presence" and jid_bare(stanza.attr.from) == bare_jid and item["presence-out"]) or
419 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
426 if jid_bare(stanza.attr.to) == bare_jid then
427 module:log("debug", "evil jid is (from): %s", stanza.attr.from);
428 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.from);
430 module:log("debug", "evil jid is (to): %s", stanza.attr.to);
431 evilJid.node, evilJid.host, evilJid.resource = jid_split(stanza.attr.to);
433 if item.type == "jid" and
434 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
435 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
436 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
437 (evilJid.host and item.value == evilJid.host) then
439 block = (item.action == "deny");
440 elseif item.type == "group" then
441 local roster = load_roster(session.username, session.host);
442 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
443 for group in pairs(groups) do
444 if group == item.value then
446 block = (item.action == "deny");
450 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
451 local roster = load_roster(session.username, session.host);
452 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
454 block = (item.action == "deny");
456 elseif item.type == nil then
458 block = (item.action == "deny");
463 module:log("info", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(stanza.attr.to), tostring(stanza.attr.from));
464 if stanza.name == "message" then
465 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
466 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
467 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
469 return true; -- stanza blocked !
471 module:log("info", "stanza explicit allowed!")
479 function preCheckIncoming(e)
481 if e.stanza.attr.to ~= nil then
482 local node, host, resource = jid_split(e.stanza.attr.to);
483 if node == nil or host == nil then
486 if resource == nil then
489 if bare_sessions[node.."@"..host] ~= nil then
490 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
491 if session_.priority ~= nil and session_.priority > prio then
493 prio = session_.priority;
498 session = full_sessions[node.."@"..host.."/"..resource];
500 if session ~= nil then
501 return checkIfNeedToBeBlocked(e, session);
503 module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
508 function preCheckOutgoing(e)
509 local session = e.origin;
510 if e.stanza.attr.from == nil then
511 e.stanza.attr.from = session.username .. "@" .. session.host;
512 if session.resource ~= nil then
513 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
516 return checkIfNeedToBeBlocked(e, session);
519 module:hook("pre-message/full", preCheckOutgoing, 500);
520 module:hook("pre-message/bare", preCheckOutgoing, 500);
521 module:hook("pre-message/host", preCheckOutgoing, 500);
522 module:hook("pre-iq/full", preCheckOutgoing, 500);
523 module:hook("pre-iq/bare", preCheckOutgoing, 500);
524 module:hook("pre-iq/host", preCheckOutgoing, 500);
525 module:hook("pre-presence/full", preCheckOutgoing, 500);
526 module:hook("pre-presence/bare", preCheckOutgoing, 500);
527 module:hook("pre-presence/host", preCheckOutgoing, 500);
529 module:hook("message/full", preCheckIncoming, 500);
530 module:hook("message/bare", preCheckIncoming, 500);
531 module:hook("message/host", preCheckIncoming, 500);
532 module:hook("iq/full", preCheckIncoming, 500);
533 module:hook("iq/bare", preCheckIncoming, 500);
534 module:hook("iq/host", preCheckIncoming, 500);
535 module:hook("presence/full", preCheckIncoming, 500);
536 module:hook("presence/bare", preCheckIncoming, 500);
537 module:hook("presence/host", preCheckIncoming, 500);