Merge 0.7->trunk
[prosody.git] / plugins / mod_privacy.lua
1 -- Prosody IM
2 -- Copyright (C) 2009-2010 Matthew Wild
3 -- Copyright (C) 2009-2010 Waqas Hussain
4 -- Copyright (C) 2009 Thilo Cestonaro
5 -- 
6 -- This project is MIT/X11 licensed. Please see the
7 -- COPYING file in the source package for more information.
8 --
9
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;
19
20 function isListUsed(origin, name, privacy_lists)
21         local user = bare_sessions[origin.username.."@"..origin.host];
22         if user then
23                 for resource, session in pairs(user.sessions) do
24                         if resource ~= origin.resource then
25                                 if session.activePrivacyList == name then
26                                         return true;
27                                 elseif session.activePrivacyList == nil and privacy_lists.default == name then
28                                         return true;
29                                 end
30                         end
31                 end
32         end
33 end
34
35 function isAnotherSessionUsingDefaultList(origin)
36         local user = bare_sessions[origin.username.."@"..origin.host];
37         if user then
38                 for resource, session in pairs(user.sessions) do
39                         if resource ~= origin.resource and session.activePrivacyList == nil then
40                                 return true;
41                         end
42                 end
43         end
44 end
45
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>
50 </presence>
51 ]]--
52         local presence = st.presence({from=from, type="unavailable"});
53         presence:tag("status"):text("Logged out");
54
55         local node, host = jid_bare(to);
56         local bare = node .. "@" .. host;
57         
58         local user = bare_sessions[bare];
59         if user then
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);
64                 end
65         end
66 end
67
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."};
72                 end
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));
78         else
79                 return {"modify", "bad-request", "Neither default nor active list specifed to decline."};
80         end
81         return true;
82 end
83
84 function activateList(privacy_lists, origin, stanza, which, name)
85         local list = privacy_lists.lists[name];
86
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."};
90                 end
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));
96         else
97                 return {"modify", "bad-request", "Either not active or default given or unknown list name specified."};
98         end
99         return true;
100 end
101
102 function deleteList(privacy_lists, origin, stanza, name)
103         local list = privacy_lists.lists[name];
104
105         if list then
106                 if isListUsed(origin, name, privacy_lists) then
107                         return {"cancel", "conflict", "Another session is online and using the list which should be deleted."};
108                 end
109                 if privacy_lists.default == name then
110                         privacy_lists.default = nil;
111                 end
112                 if origin.activePrivacyList == name then
113                         origin.activePrivacyList = nil;
114                 end
115                 privacy_lists.lists[name] = nil;
116                 origin.send(st.reply(stanza));
117                 return true;
118         end
119         return {"modify", "bad-request", "Not existing list specifed to be deleted."};
120 end
121
122 function createOrReplaceList (privacy_lists, origin, stanza, name, entries, roster)
123         local bare_jid = origin.username.."@"..origin.host;
124         
125         if privacy_lists.lists == nil then
126                 privacy_lists.lists = {};
127         end
128
129         local list = {};
130         privacy_lists.lists[name] = list;
131
132         local orderCheck = {};
133         list.name = name;
134         list.items = {};
135
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."};
139                 end
140                 
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."};
143                 end
144                 
145                 local tmp = {};
146                 orderCheck[item.attr.order] = true;
147                 
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;
155                 tmp["iq"] = false;
156                 
157                 if #item.tags > 0 then
158                         for _,tag in ipairs(item.tags) do
159                                 tmp[tag.name] = true;
160                         end
161                 end
162                 
163                 if tmp.type == "group" then
164                         local found = false;
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
170                                                         found = true;
171                                                         break;
172                                                 end
173                                         end
174                                         if found == true then
175                                                 break;
176                                         end
177                                 end
178                         end
179                         if found == false then
180                                 return {"cancel", "item-not-found", "Specifed roster group not existing."};
181                         end
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."};
188                         end
189                 end
190                 
191                 if tmp.action ~= "deny" and tmp.action ~= "allow" then
192                         return {"cancel", "bad-request", "Action must be either deny or allow."};
193                 end
194                 list.items[#list.items + 1] = tmp;
195         end
196         
197         table.sort(list, function(a, b) return a.order < b.order; end);
198
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();
204                 iq:up();
205                 for resource, session in pairs(bare_sessions[bare_jid].sessions) do
206                         iq.attr.to = bare_jid.."/"..resource
207                         session.send(iq);
208                 end
209         else
210                 return {"cancel", "bad-request", "internal error."};
211         end
212         return true;
213 end
214
215 function getList(privacy_lists, origin, stanza, name)
216         local reply = st.reply(stanza);
217         reply:tag("query", {xmlns="jabber:iq:privacy"});
218
219         if name == nil then
220                 if privacy_lists.lists then
221                         if origin.ActivePrivacyList then
222                                 reply:tag("active", {name=origin.activePrivacyList}):up();
223                         end
224                         if privacy_lists.default then
225                                 reply:tag("default", {name=privacy_lists.default}):up();
226                         end
227                         for name,list in pairs(privacy_lists.lists) do
228                                 reply:tag("list", {name=name}):up();
229                         end
230                 end
231         else
232                 local list = privacy_lists.lists[name];
233                 if list then
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
241                                 reply:up();
242                         end
243                 else
244                         return {"cancel", "item-not-found", "Unknown list specified."};
245                 end
246         end
247         
248         origin.send(reply);
249         return true;
250 end
251
252 module:hook("iq/bare/jabber:iq:privacy:query", function(data)
253         local origin, stanza = data.origin, data.stanza;
254         
255         if stanza.attr.to == nil then -- only service requests to own bare JID
256                 local query = stanza.tags[1]; -- the query element
257                 local valid = false;
258                 local privacy_lists = datamanager.load(origin.username, origin.host, "privacy") or { lists = {} };
259
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;
265                                 lists[idx] = nil;
266                         end
267                 end
268
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);
277                                                 end
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);
283                                                 end
284                                         end
285                                 end
286                         end
287                 elseif stanza.attr.type == "get" then
288                         local name = nil;
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;
295                                         end
296                                 end
297                         end
298                         if listsToRetrieve == 0 or listsToRetrieve == 1 then
299                                 valid = getList(privacy_lists, origin, stanza, name);
300                         end
301                 end
302
303                 if valid ~= true then
304                         valid = valid or { "cancel", "bad-request", "Couldn't understand request" };
305                         if valid[1] == nil then
306                                 valid[1] = "cancel";
307                         end
308                         if valid[2] == nil then
309                                 valid[2] = "bad-request";
310                         end
311                         origin.send(st.error_reply(stanza, valid[1], valid[2], valid[3]));
312                 else
313                         datamanager.store(origin.username, origin.host, "privacy", privacy_lists);
314                 end
315                 return true;
316         end
317 end);
318
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;
325         
326         local is_to_user = bare_jid == jid_bare(to);
327         local is_from_user = bare_jid == jid_bare(from);
328         
329         module:log("debug", "stanza: %s, to: %s, from: %s", tostring(stanza.name), tostring(to), tostring(from));
330         
331         if privacy_lists.lists == nil or
332                 not (session.activePrivacyList or privacy_lists.default)
333         then
334                 return; -- Nothing to block, default is Allow all
335         end
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!
339         end
340         
341         local item;
342         local listname = session.activePrivacyList;
343         if listname == nil then
344                 listname = privacy_lists.default; -- no active list selected, use default list
345         end
346         local list = privacy_lists.lists[listname];
347         if not list then
348                 module:log("debug", "given privacy list not found. name: %s", listname);
349                 return;
350         end
351         for _,item in ipairs(list.items) do
352                 local apply = false;
353                 local block = false;
354                 if (
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)
360                 ) then
361                         apply = true;
362                 end
363                 if apply then
364                         local evilJid = {};
365                         apply = false;
366                         if is_to_user then
367                                 module:log("debug", "evil jid is (from): %s", from);
368                                 evilJid.node, evilJid.host, evilJid.resource = jid_split(from);
369                         else
370                                 module:log("debug", "evil jid is (to): %s", to);
371                                 evilJid.node, evilJid.host, evilJid.resource = jid_split(to);
372                         end
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
378                                 apply = true;
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
385                                                 apply = true;
386                                                 block = (item.action == "deny");
387                                                 break;
388                                         end
389                                 end
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
393                                         apply = true;
394                                         block = (item.action == "deny");
395                                 end
396                         elseif item.type == nil then
397                                 apply = true;
398                                 block = (item.action == "deny");
399                         end
400                 end
401                 if apply then
402                         if block then
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"));
408                                 end
409                                 return true; -- stanza blocked !
410                         else
411                                 module:log("debug", "stanza explicitly allowed!")
412                                 return;
413                         end
414                 end
415         end
416 end
417
418 function preCheckIncoming(e)
419         local session;
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
423                         return;
424                 end
425                 if resource == nil then
426                         local prio = 0;
427                         local session_;
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
431                                                 session = session_;
432                                                 prio = session_.priority;
433                                         end
434                                 end
435                         end
436                 else
437                         session = full_sessions[node.."@"..host.."/"..resource];
438                 end
439                 if session ~= nil then
440                         return checkIfNeedToBeBlocked(e, session);
441                 else
442                         module:log("debug", "preCheckIncoming: Couldn't get session for jid: %s@%s/%s", tostring(node), tostring(host), tostring(resource));
443                 end
444         end
445 end
446
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;
453                 end
454         end
455         return checkIfNeedToBeBlocked(e, session);
456 end
457
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);
467
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);