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 module:add_feature("jabber:iq:privacy");
12 local st = require "util.stanza";
13 local datamanager = require "util.datamanager";
14 local bare_sessions, full_sessions = prosody.bare_sessions, prosody.full_sessions;
15 local util_Jid = require "util.jid";
16 local jid_bare = util_Jid.bare;
17 local jid_split, jid_join = util_Jid.split, util_Jid.join;
18 local load_roster = require "core.rostermanager".load_roster;
19 local to_number = tonumber;
21 function isListUsed(origin, name, privacy_lists)
22 local user = bare_sessions[origin.username.."@"..origin.host];
24 for resource, session in pairs(user.sessions) do
25 if resource ~= origin.resource then
26 if session.activePrivacyList == name then
28 elseif session.activePrivacyList == nil and privacy_lists.default == name then
36 function isAnotherSessionUsingDefaultList(origin)
37 local user = bare_sessions[origin.username.."@"..origin.host];
39 for resource, session in pairs(user.sessions) do
40 if resource ~= origin.resource and session.activePrivacyList == nil then
47 function declineList(privacy_lists, origin, stanza, which)
48 if which == "default" then
49 if isAnotherSessionUsingDefaultList(origin) then
50 return { "cancel", "conflict", "Another session is online and using the default list."};
52 privacy_lists.default = nil;
53 origin.send(st.reply(stanza));
54 elseif which == "active" then
55 origin.activePrivacyList = nil;
56 origin.send(st.reply(stanza));
58 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
63 function activateList(privacy_lists, origin, stanza, which, name)
64 local list = privacy_lists.lists[name];
66 if which == "default" and list then
67 if isAnotherSessionUsingDefaultList(origin) then
68 return {"cancel", "conflict", "Another session is online and using the default list."};
70 privacy_lists.default = name;
71 origin.send(st.reply(stanza));
72 elseif which == "active" and list then
73 origin.activePrivacyList = name;
74 origin.send(st.reply(stanza));
76 return {"cancel", "item-not-found", "No such list: "..name};
78 return {"modify", "bad-request", "No list chosen to be active or default."};
83 function deleteList(privacy_lists, origin, stanza, name)
84 local list = privacy_lists.lists[name];
87 if isListUsed(origin, name, privacy_lists) then
88 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
90 if privacy_lists.default == name then
91 privacy_lists.default = nil;
93 if origin.activePrivacyList == name then
94 origin.activePrivacyList = nil;
96 privacy_lists.lists[name] = nil;
97 origin.send(st.reply(stanza));
100 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
103 function createOrReplaceList (privacy_lists, origin, stanza, name, entries)
104 local bare_jid = origin.username.."@"..origin.host;
106 if privacy_lists.lists == nil then
107 privacy_lists.lists = {};
111 privacy_lists.lists[name] = list;
113 local orderCheck = {};
117 for _,item in ipairs(entries) do
118 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
119 return {"modify", "bad-request", "Order attribute not valid."};
122 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
123 return {"modify", "bad-request", "Type attribute not valid."};
127 orderCheck[item.attr.order] = true;
129 tmp["type"] = item.attr.type;
130 tmp["value"] = item.attr.value;
131 tmp["action"] = item.attr.action;
132 tmp["order"] = to_number(item.attr.order);
133 tmp["presence-in"] = false;
134 tmp["presence-out"] = false;
135 tmp["message"] = false;
138 if #item.tags > 0 then
139 for _,tag in ipairs(item.tags) do
140 tmp[tag.name] = true;
144 if tmp.type == "subscription" then
145 if tmp.value ~= "both" and
146 tmp.value ~= "to" and
147 tmp.value ~= "from" and
148 tmp.value ~= "none" then
149 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
153 if tmp.action ~= "deny" and tmp.action ~= "allow" then
154 return {"cancel", "bad-request", "Action must be either deny or allow."};
156 list.items[#list.items + 1] = tmp;
159 table.sort(list, function(a, b) return a.order < b.order; end);
161 origin.send(st.reply(stanza));
162 if bare_sessions[bare_jid] ~= nil then
163 local iq = st.iq ( { type = "set", id="push1" } );
164 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
165 iq:tag ("list", { name = list.name } ):up();
167 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
168 iq.attr.to = bare_jid.."/"..resource
172 return {"cancel", "bad-request", "internal error."};
177 function getList(privacy_lists, origin, stanza, name)
178 local reply = st.reply(stanza);
179 reply:tag("query", {xmlns="jabber:iq:privacy"});
182 if privacy_lists.lists then
183 if origin.activePrivacyList then
184 reply:tag("active", {name=origin.activePrivacyList}):up();
186 if privacy_lists.default then
187 reply:tag("default", {name=privacy_lists.default}):up();
189 for name,list in pairs(privacy_lists.lists) do
190 reply:tag("list", {name=name}):up();
194 local list = privacy_lists.lists[name];
196 reply = reply:tag("list", {name=list.name});
197 for _,item in ipairs(list.items) do
198 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
199 if item["message"] then reply:tag("message"):up(); end
200 if item["iq"] then reply:tag("iq"):up(); end
201 if item["presence-in"] then reply:tag("presence-in"):up(); end
202 if item["presence-out"] then reply:tag("presence-out"):up(); end
206 return {"cancel", "item-not-found", "Unknown list specified."};
214 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
215 local origin, stanza = data.origin, data.stanza;
217 if stanza.attr.to == nil then -- only service requests to own bare JID
218 local query = stanza.tags[1]; -- the query element
220 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or { lists = {} };
222 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
223 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
224 local lists = privacy_lists.lists;
225 for idx, list in ipairs(lists) do
226 lists[list.name] = list;
231 if stanza.attr.type == "set" then
232 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
233 for _,tag in ipairs(query.tags) do
234 if tag.name == "active" or tag.name == "default" then
235 if tag.attr.name == nil then -- Client declines the use of active / default list
236 valid = declineList(privacy_lists, origin, stanza, tag.name);
237 else -- Client requests change of active / default list
238 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
240 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
241 if #tag.tags == 0 then -- Client removes a privacy list
242 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
243 else -- Client edits a privacy list
244 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
249 elseif stanza.attr.type == "get" then
251 local listsToRetrieve = 0;
252 if #query.tags >= 1 then
253 for _,tag in ipairs(query.tags) do
254 if tag.name == "list" then -- Client requests a privacy list from server
255 name = tag.attr.name;
256 listsToRetrieve = listsToRetrieve + 1;
260 if listsToRetrieve == 0 or listsToRetrieve == 1 then
261 valid = getList(privacy_lists, origin, stanza, name);
265 if valid ~= true then
266 valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
267 if valid[1] == nil then
270 if valid[2] == nil then
271 valid[2] = "bad-request";
273 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
275 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
281 function checkIfNeedToBeBlocked(e, session)
282 local origin, stanza = e.origin, e.stanza;
283 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
284 local bare_jid = session.username.."@"..session.host;
285 local to = stanza.attr.to or bare_jid;
286 local from = stanza.attr.from;
288 local is_to_user = bare_jid == jid_bare(to);
289 local is_from_user = bare_jid == jid_bare(from);
291 --module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
293 if privacy_lists.lists == nil or
294 not (session.activePrivacyList or privacy_lists.default)
296 return; -- Nothing to block, default is Allow all
298 if is_from_user and is_to_user then
299 --module:log("debug", "Not blocking communications between user's resources");
300 return; -- from one of a user's resource to another => HANDS OFF!
303 local listname = session.activePrivacyList;
304 if listname == nil then
305 listname = privacy_lists.default; -- no active list selected, use default list
307 local list = privacy_lists.lists[listname];
308 if not list then -- should never happen
309 module:log("warn", "given privacy list not found. name: %s for user %s", listname, bare_jid);
312 for _,item in ipairs(list.items) do
316 (stanza.name == "message" and item.message) or
317 (stanza.name == "iq" and item.iq) or
318 (stanza.name == "presence" and is_to_user and item["presence-in"]) or
319 (stanza.name == "presence" and is_from_user and item["presence-out"]) or
320 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
328 --module:log("debug", "evil jid is (from): %s", from);
329 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
331 --module:log("debug", "evil jid is (to): %s", to);
332 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
334 if item.type == "jid" and
335 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
336 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
337 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
338 (evilJid.host and item.value == evilJid.host) then
340 block = (item.action == "deny");
341 elseif item.type == "group" then
342 local roster = load_roster(session.username, session.host);
343 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
345 local groups = roster_entry.groups;
346 for group in pairs(groups) do
347 if group == item.value then
349 block = (item.action == "deny");
354 elseif item.type == "subscription" then -- we need a valid bare evil jid
355 local roster = load_roster(session.username, session.host);
356 local roster_entry = roster[jid_join(evilJid.node, evilJid.host)];
357 if (not(roster_entry) and item.value == "none")
358 or (roster_entry and roster_entry.subscription == item.value) then
360 block = (item.action == "deny");
362 elseif item.type == nil then
364 block = (item.action == "deny");
369 -- drop and not bounce groupchat messages, otherwise users will get kicked
370 if stanza.attr.type == "groupchat" then
373 module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
374 if stanza.name == "message" then
375 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
376 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
377 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
379 return true; -- stanza blocked !
381 --module:log("debug", "stanza explicitly allowed!")
388 function preCheckIncoming(e)
390 if e.stanza.attr.to ~= nil then
391 local node, host, resource = jid_split(e.stanza.attr.to);
392 if node == nil or host == nil then
395 if resource == nil then
397 if bare_sessions[node.."@"..host] ~= nil then
398 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
399 if session_.priority ~= nil and session_.priority > prio then
401 prio = session_.priority;
406 session = full_sessions[node.."@"..host.."/"..resource];
408 if session ~= nil then
409 return checkIfNeedToBeBlocked(e, session);
411 --module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
416 function preCheckOutgoing(e)
417 local session = e.origin;
418 if e.stanza.attr.from == nil then
419 e.stanza.attr.from = session.username .. "@" .. session.host;
420 if session.resource ~= nil then
421 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
424 if session.username then -- FIXME do properly
425 return checkIfNeedToBeBlocked(e, session);
429 module:hook("pre-message/full", preCheckOutgoing, 500);
430 module:hook("pre-message/bare", preCheckOutgoing, 500);
431 module:hook("pre-message/host", preCheckOutgoing, 500);
432 module:hook("pre-iq/full", preCheckOutgoing, 500);
433 module:hook("pre-iq/bare", preCheckOutgoing, 500);
434 module:hook("pre-iq/host", preCheckOutgoing, 500);
435 module:hook("pre-presence/full", preCheckOutgoing, 500);
436 module:hook("pre-presence/bare", preCheckOutgoing, 500);
437 module:hook("pre-presence/host", preCheckOutgoing, 500);
439 module:hook("message/full", preCheckIncoming, 500);
440 module:hook("message/bare", preCheckIncoming, 500);
441 module:hook("message/host", preCheckIncoming, 500);
442 module:hook("iq/full", preCheckIncoming, 500);
443 module:hook("iq/bare", preCheckIncoming, 500);
444 module:hook("iq/host", preCheckIncoming, 500);
445 module:hook("presence/full", preCheckIncoming, 500);
446 module:hook("presence/bare", preCheckIncoming, 500);
447 module:hook("presence/host", preCheckIncoming, 500);