1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/**
* $RCSfile$
* $Revision: $
* $Date: $
*
* Copyright (C) 2005-2008 Jive Software. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jivesoftware.openfire.privacy;
import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.XMPPPacketReader;
import org.jivesoftware.openfire.XMPPServer;
import org.jivesoftware.openfire.net.MXParser;
import org.jivesoftware.openfire.roster.Roster;
import org.jivesoftware.openfire.user.UserNotFoundException;
import org.jivesoftware.util.cache.CacheSizes;
import org.jivesoftware.util.cache.Cacheable;
import org.jivesoftware.util.cache.CannotCalculateSizeException;
import org.jivesoftware.util.cache.ExternalizableUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;
import org.xmpp.packet.JID;
import org.xmpp.packet.Packet;
/**
* A privacy list contains a set of rules that define if communication with the list owner
* is allowed or denied. Users may have zero, one or more privacy lists. When a list is the
* default list then that list is going to be used by default for all user sessions or analyze,
* when user is offline, if communication may proceed (e.g. define if a message should be stored
* offline). A user may configure is he wants to have a default list or not. When no default list
* is defined then communication will not be blocked. However, users may define an active list
* for a particular session. Active lists override default list (if there is one) and will be used
* only for the duration of the session.
*
* @author Gaston Dombiak
*/
public class PrivacyList implements Cacheable, Externalizable {
private static final Logger Log = LoggerFactory.getLogger(PrivacyList.class);
/**
* Reuse the same factory for all the connections.
*/
private static XmlPullParserFactory factory = null;
private static ThreadLocal<XMPPPacketReader> localParser = null;
static {
try {
factory = XmlPullParserFactory.newInstance(MXParser.class.getName(), null);
factory.setNamespaceAware(true);
}
catch (XmlPullParserException e) {
Log.error("Error creating a parser factory", e);
}
// Create xmpp parser to keep in each thread
localParser = new ThreadLocal<XMPPPacketReader>() {
@Override
protected XMPPPacketReader initialValue() {
XMPPPacketReader parser = new XMPPPacketReader();
factory.setNamespaceAware(true);
parser.setXPPFactory(factory);
return parser;
}
};
}
private JID userJID;
private String name;
private boolean isDefault;
private List<PrivacyItem> items = new ArrayList<PrivacyItem>();
/**
* Constructor added for Externalizable. Do not use this constructor.
*/
public PrivacyList() {
}
public PrivacyList(String username, String name, boolean isDefault, Element listElement) {
this.userJID = XMPPServer.getInstance().createJID(username, null, true);
this.name = name;
this.isDefault = isDefault;
// Set the new list items
updateList(listElement);
}
/**
* Returns the JID of the user that owns this privacy list.
*
* @return the JID of the user that owns this privacy list.
*/
public JID getUserJID() {
return userJID;
}
/**
* Returns the name that uniquely identifies this list among the users lists.
*
* @return the name that uniquely identifies this list among the users lists.
*/
public String getName() {
return name;
}
/**
* Returns true if this privacy list is the default list to apply for the user. Default
* privacy lists can be overriden per session by setting an active privacy list.
*
* @return true if this privacy list is the default list to apply for the user.
*/
public boolean isDefault() {
return isDefault;
}
/**
* Sets if this privacy list is the default list to apply for the user. Default
* privacy lists can be overriden per session by setting an active privacy list.
*
* @param isDefault true if this privacy list is the default list to apply for the user.
*/
public void setDefaultList(boolean isDefault) {
this.isDefault = isDefault;
// Trigger event that this list has been modified
PrivacyListManager.getInstance().dispatchModifiedEvent(this);
}
/**
* Returns true if the specified packet must be blocked based on this privacy list rules.
* Rules are going to be analyzed based on their order (in ascending order). When a rule
* is matched then communication will be blocked or allowed based on that rule. No more
* further analysis is going to be made.
*
* @param packet the packet to analyze if it must be blocked.
* @return true if the specified packet must be blocked based on this privacy list rules.
*/
public boolean shouldBlockPacket(Packet packet) {
if (packet.getFrom() == null) {
// Sender is the server so it's not denied
return false;
}
// Iterate over the rules and check each rule condition
Roster roster = getRoster();
for (PrivacyItem item : items) {
if (item.matchesCondition(packet, roster, userJID)) {
if (item.isAllow()) {
return false;
}
if (Log.isDebugEnabled()) {
Log.debug("PrivacyList: Packet was blocked: " + packet);
}
return true;
}
}
// If no rule blocked the communication then allow the packet to flow
return false;
}
/**
* Returns an Element with the privacy list XML representation.
*
* @return an Element with the privacy list XML representation.
*/
public Element asElement() {
//Element listElement = DocumentFactory.getInstance().createDocument().addElement("list");
Element listElement = DocumentFactory.getInstance().createDocument()
.addElement("list", "jabber:iq:privacy");
listElement.addAttribute("name", getName());
// Add the list items to the result
for (PrivacyItem item : items) {
listElement.add(item.asElement());
}
return listElement;
}
/**
* Sets the new list items based on the specified Element. The Element must contain
* a list of item elements.
*
* @param listElement the element containing a list of items.
*/
public void updateList(Element listElement) {
updateList(listElement, true);
}
/**
* Sets the new list items based on the specified Element. The Element must contain
* a list of item elements.
*
* @param listElement the element containing a list of items.
* @param notify true if a provicy list modified event will be triggered.
*/
private void updateList(Element listElement, boolean notify) {
// Reset the list of items of this list
items = new ArrayList<PrivacyItem>();
List<Element> itemsElements = listElement.elements("item");
for (Element itemElement : itemsElements) {
PrivacyItem newItem = new PrivacyItem(itemElement);
items.add(newItem);
// If the user's roster is required to evaluation whether a packet must be blocked
// then ensure that the roster is available
if (newItem.isRosterRequired()) {
Roster roster = getRoster();
if (roster == null) {
Log.warn("Privacy item removed since roster of user was not found: " + userJID.getNode());
items.remove(newItem);
}
}
}
// Sort items collections
Collections.sort(items);
if (notify) {
// Trigger event that this list has been modified
PrivacyListManager.getInstance().dispatchModifiedEvent(this);
}
}
private Roster getRoster() {
try {
return XMPPServer.getInstance().getRosterManager().getRoster(userJID.getNode());
} catch (UserNotFoundException e) {
Log.warn("Roster not found for user: " + userJID);
}
return null;
}
public int getCachedSize() throws CannotCalculateSizeException {
// Approximate the size of the object in bytes by calculating the size
// of each field.
int size = 0;
size += CacheSizes.sizeOfObject(); // overhead of object
size += CacheSizes.sizeOfString(userJID.toString()); // userJID
size += CacheSizes.sizeOfString(name); // name
size += CacheSizes.sizeOfBoolean(); // isDefault
size += CacheSizes.sizeOfCollection(items); // items of the list
return size;
}
@Override
public int hashCode() {
return name.hashCode();
}
@Override
public boolean equals(Object object) {
if (this == object) {
return true;
}
if (object != null && object instanceof PrivacyList) {
return name.equals(((PrivacyList)object).getName());
}
else {
return false;
}
}
public void writeExternal(ObjectOutput out) throws IOException {
ExternalizableUtil.getInstance().writeSerializable(out, userJID);
ExternalizableUtil.getInstance().writeSafeUTF(out, name);
ExternalizableUtil.getInstance().writeBoolean(out, isDefault);
ExternalizableUtil.getInstance().writeSafeUTF(out, asElement().asXML());
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
userJID = (JID) ExternalizableUtil.getInstance().readSerializable(in);
name = ExternalizableUtil.getInstance().readSafeUTF(in);
isDefault = ExternalizableUtil.getInstance().readBoolean(in);
String xml = ExternalizableUtil.getInstance().readSafeUTF(in);
try {
Element element = localParser.get().read(new StringReader(xml)).getRootElement();
updateList(element, false);
} catch (Exception e) {
Log.error("Error while parsing Privacy Property", e);
}
}
}