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 = util_Jid.split;
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 {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
102 function deleteList(privacy_lists, origin, stanza, name)
103 local list = privacy_lists.lists[name];
106 if isListUsed(origin, name, privacy_lists) then
107 return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
109 if privacy_lists.default == name then
110 privacy_lists.default = nil;
112 if origin.activePrivacyList == name then
113 origin.activePrivacyList = nil;
115 privacy_lists.lists[name] = nil;
116 origin.send(st.reply(stanza));
119 return {"modify", "bad-request", "Not existing list specifed to be deleted."};
122 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
123 local bare_jid = origin.username.."@"..origin.host;
125 if privacy_lists.lists == nil then
126 privacy_lists.lists = {};
130 privacy_lists.lists[name] = list;
132 local orderCheck = {};
136 for _,item in ipairs(entries) do
137 if to_number(item.attr.order) == nil or to_number(item.attr.order) < 0 or orderCheck[item.attr.order] ~= nil then
138 return {"modify", "bad-request", "Order attribute not valid."};
141 if item.attr.type ~= nil and item.attr.type ~= "jid" and item.attr.type ~= "subscription" and item.attr.type ~= "group" then
142 return {"modify", "bad-request", "Type attribute not valid."};
146 orderCheck[item.attr.order] = true;
148 tmp["type"] = item.attr.type;
149 tmp["value"] = item.attr.value;
150 tmp["action"] = item.attr.action;
151 tmp["order"] = to_number(item.attr.order);
152 tmp["presence-in"] = false;
153 tmp["presence-out"] = false;
154 tmp["message"] = false;
157 if #item.tags > 0 then
158 for _,tag in ipairs(item.tags) do
159 tmp[tag.name] = true;
163 if tmp.type == "group" then
165 local roster = load_roster(origin.username, origin.host);
166 for jid,item in pairs(roster) do
167 if item.groups ~= nil then
168 for group in pairs(item.groups) do
169 if group == tmp.value then
174 if found == true then
179 if found == false then
180 return {"cancel", "item-not-found", "Specifed roster group not existing."};
182 elseif tmp.type == "subscription" then
183 if tmp.value ~= "both" and
184 tmp.value ~= "to" and
185 tmp.value ~= "from" and
186 tmp.value ~= "none" then
187 return {"cancel", "bad-request", "Subscription value must be both, to, from or none."};
191 if tmp.action ~= "deny" and tmp.action ~= "allow" then
192 return {"cancel", "bad-request", "Action must be either deny or allow."};
194 list.items[#list.items + 1] = tmp;
197 table.sort(list, function(a, b) return a.order < b.order; end);
199 origin.send(st.reply(stanza));
200 if bare_sessions[bare_jid] ~= nil then
201 local iq = st.iq ( { type = "set", id="push1" } );
202 iq:tag ("query", { xmlns = "jabber:iq:privacy" } );
203 iq:tag ("list", { name = list.name } ):up();
205 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
206 iq.attr.to = bare_jid.."/"..resource
210 return {"cancel", "bad-request", "internal error."};
215 function getList(privacy_lists, origin, stanza, name)
216 local reply = st.reply(stanza);
217 reply:tag("query", {xmlns="jabber:iq:privacy"});
220 if privacy_lists.lists then
221 if origin.ActivePrivacyList then
222 reply:tag("active", {name=origin.activePrivacyList}):up();
224 if privacy_lists.default then
225 reply:tag("default", {name=privacy_lists.default}):up();
227 for name,list in pairs(privacy_lists.lists) do
228 reply:tag("list", {name=name}):up();
232 local list = privacy_lists.lists[name];
234 reply = reply:tag("list", {name=list.name});
235 for _,item in ipairs(list.items) do
236 reply:tag("item", {type=item.type, value=item.value, action=item.action, order=item.order});
237 if item["message"] then reply:tag("message"):up(); end
238 if item["iq"] then reply:tag("iq"):up(); end
239 if item["presence-in"] then reply:tag("presence-in"):up(); end
240 if item["presence-out"] then reply:tag("presence-out"):up(); end
244 return {"cancel", "item-not-found", "Unknown list specified."};
252 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
253 local origin, stanza = data.origin, data.stanza;
255 if stanza.attr.to == nil then -- only service requests to own bare JID
256 local query = stanza.tags[1]; -- the query element
258 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or { lists = {} };
260 if privacy_lists.lists[1] then -- Code to migrate from old privacy lists format, remove in 0.8
261 module:log("info", "Upgrading format of stored privacy lists for %s@%s", origin.username, origin.host);
262 local lists = privacy_lists.lists;
263 for idx, list in ipairs(lists) do
264 lists[list.name] = list;
269 if stanza.attr.type == "set" then
270 if #query.tags == 1 then -- the <query/> element MUST NOT include more than one child element
271 for _,tag in ipairs(query.tags) do
272 if tag.name == "active" or tag.name == "default" then
273 if tag.attr.name == nil then -- Client declines the use of active / default list
274 valid = declineList(privacy_lists, origin, stanza, tag.name);
275 else -- Client requests change of active / default list
276 valid = activateList(privacy_lists, origin, stanza, tag.name, tag.attr.name);
278 elseif tag.name == "list" and tag.attr.name then -- Client adds / edits a privacy list
279 if #tag.tags == 0 then -- Client removes a privacy list
280 valid = deleteList(privacy_lists, origin, stanza, tag.attr.name);
281 else -- Client edits a privacy list
282 valid = createOrReplaceList(privacy_lists, origin, stanza, tag.attr.name, tag.tags);
287 elseif stanza.attr.type == "get" then
289 local listsToRetrieve = 0;
290 if #query.tags >= 1 then
291 for _,tag in ipairs(query.tags) do
292 if tag.name == "list" then -- Client requests a privacy list from server
293 name = tag.attr.name;
294 listsToRetrieve = listsToRetrieve + 1;
298 if listsToRetrieve == 0 or listsToRetrieve == 1 then
299 valid = getList(privacy_lists, origin, stanza, name);
303 if valid ~= true then
304 valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
305 if valid[1] == nil then
308 if valid[2] == nil then
309 valid[2] = "bad-request";
311 origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
313 datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
319 function checkIfNeedToBeBlocked(e, session)
320 local origin, stanza = e.origin, e.stanza;
321 local privacy_lists = datamanager.load(session.username, session.host, "privacy") or {};
322 local bare_jid = session.username.."@"..session.host;
323 local to = stanza.attr.to;
324 local from = stanza.attr.from;
326 local is_to_user = bare_jid == jid_bare(to);
327 local is_from_user = bare_jid == jid_bare(from);
329 module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
331 if privacy_lists.lists == nil or
332 not (session.activePrivacyList or privacy_lists.default)
334 return; -- Nothing to block, default is Allow all
336 if is_from_user and is_to_user then
337 module:log("debug", "Not blocking communications between user's resources");
338 return; -- from one of a user's resource to another => HANDS OFF!
342 local listname = session.activePrivacyList;
343 if listname == nil then
344 listname = privacy_lists.default; -- no active list selected, use default list
346 local list = privacy_lists.lists[listname];
348 module:log("debug", "given privacy list not found. name: %s", listname);
351 for _,item in ipairs(list.items) do
355 (stanza.name == "message" and item.message) or
356 (stanza.name == "iq" and item.iq) or
357 (stanza.name == "presence" and is_to_user and item["presence-in"]) or
358 (stanza.name == "presence" and is_from_user and item["presence-out"]) or
359 (item.message == false and item.iq == false and item["presence-in"] == false and item["presence-out"] == false)
367 module:log("debug", "evil jid is (from): %s", from);
368 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
370 module:log("debug", "evil jid is (to): %s", to);
371 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
373 if item.type == "jid" and
374 (evilJid.node and evilJid.host and evilJid.resource and item.value == evilJid.node.."@"..evilJid.host.."/"..evilJid.resource) or
375 (evilJid.node and evilJid.host and item.value == evilJid.node.."@"..evilJid.host) or
376 (evilJid.host and evilJid.resource and item.value == evilJid.host.."/"..evilJid.resource) or
377 (evilJid.host and item.value == evilJid.host) then
379 block = (item.action == "deny");
380 elseif item.type == "group" then
381 local roster = load_roster(session.username, session.host);
382 local groups = roster[evilJid.node .. "@" .. evilJid.host].groups;
383 for group in pairs(groups) do
384 if group == item.value then
386 block = (item.action == "deny");
390 elseif item.type == "subscription" and evilJid.node ~= nil and evilJid.host ~= nil then -- we need a valid bare evil jid
391 local roster = load_roster(session.username, session.host);
392 if roster[evilJid.node .. "@" .. evilJid.host].subscription == item.value then
394 block = (item.action == "deny");
396 elseif item.type == nil then
398 block = (item.action == "deny");
403 module:log("debug", "stanza blocked: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
404 if stanza.name == "message" then
405 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
406 elseif stanza.name == "iq" and (stanza.attr.type == "get" or stanza.attr.type == "set") then
407 origin.send(st.error_reply(stanza, "cancel", "service-unavailable"));
409 return true; -- stanza blocked !
411 module:log("debug", "stanza explicitly allowed!")
418 function preCheckIncoming(e)
420 if e.stanza.attr.to ~= nil then
421 local node, host, resource = jid_split(e.stanza.attr.to);
422 if node == nil or host == nil then
425 if resource == nil then
428 if bare_sessions[node.."@"..host] ~= nil then
429 for resource, session_ in pairs(bare_sessions[node.."@"..host].sessions) do
430 if session_.priority ~= nil and session_.priority > prio then
432 prio = session_.priority;
437 session = full_sessions[node.."@"..host.."/"..resource];
439 if session ~= nil then
440 return checkIfNeedToBeBlocked(e, session);
442 module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
447 function preCheckOutgoing(e)
448 local session = e.origin;
449 if e.stanza.attr.from == nil then
450 e.stanza.attr.from = session.username .. "@" .. session.host;
451 if session.resource ~= nil then
452 e.stanza.attr.from = e.stanza.attr.from .. "/" .. session.resource;
455 return checkIfNeedToBeBlocked(e, session);
458 module:hook("pre-message/full", preCheckOutgoing, 500);
459 module:hook("pre-message/bare", preCheckOutgoing, 500);
460 module:hook("pre-message/host", preCheckOutgoing, 500);
461 module:hook("pre-iq/full", preCheckOutgoing, 500);
462 module:hook("pre-iq/bare", preCheckOutgoing, 500);
463 module:hook("pre-iq/host", preCheckOutgoing, 500);
464 module:hook("pre-presence/full", preCheckOutgoing, 500);
465 module:hook("pre-presence/bare", preCheckOutgoing, 500);
466 module:hook("pre-presence/host", preCheckOutgoing, 500);
468 module:hook("message/full", preCheckIncoming, 500);
469 module:hook("message/bare", preCheckIncoming, 500);
470 module:hook("message/host", preCheckIncoming, 500);
471 module:hook("iq/full", preCheckIncoming, 500);
472 module:hook("iq/bare", preCheckIncoming, 500);
473 module:hook("iq/host", preCheckIncoming, 500);
474 module:hook("presence/full", preCheckIncoming, 500);
475 module:hook("presence/bare", preCheckIncoming, 500);
476 module:hook("presence/host", preCheckIncoming, 500);