Debian Codegraph @

netx/net/sourceforge/jnlp/security/appletextendedsecurity/impl/UnsignedAppletActionStorageImpl.java

raw
1 /* Copyright (C) 2013 Red Hat, Inc.
2
3 This file is part of IcedTea.
4
5 IcedTea is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 2.
8
9 IcedTea is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with IcedTea; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301 USA.
18
19 Linking this library statically or dynamically with other modules is
20 making a combined work based on this library. Thus, the terms and
21 conditions of the GNU General Public License cover the whole
22 combination.
23
24 As a special exception, the copyright holders of this library give you
25 permission to link this library with independent modules to produce an
26 executable, regardless of the license terms of these independent
27 modules, and to copy and distribute the resulting executable under
28 terms of your choice, provided that you also meet, for each linked
29 independent module, the terms and conditions of the license of that
30 module. An independent module is a module which is not derived from
31 or based on this library. If you modify this library, you may extend
32 this exception to your version of the library, but you are not
33 obligated to do so. If you do not wish to do so, delete this
34 exception statement from your version.
35 */
36 package net.sourceforge.jnlp.security.appletextendedsecurity.impl;
37
38 import java.io.BufferedWriter;
39 import java.io.File;
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.Collections;
43 import java.util.List;
44 import java.util.regex.PatternSyntaxException;
45 import net.sourceforge.jnlp.security.appletextendedsecurity.InvalidLineException;
46 import net.sourceforge.jnlp.security.appletextendedsecurity.UnsignedAppletActionEntry;
47 import net.sourceforge.jnlp.security.appletextendedsecurity.UnsignedAppletActionStorage;
48 import net.sourceforge.jnlp.security.dialogs.remember.ExecuteAppletAction;
49 import net.sourceforge.jnlp.security.dialogs.remember.RememberableDialog;
50 import net.sourceforge.jnlp.util.FileUtils;
51 import net.sourceforge.jnlp.util.lockingfile.LockingReaderWriter;
52 import net.sourceforge.jnlp.util.lockingfile.StorageIoException;
53 import net.sourceforge.jnlp.util.logging.OutputController;
54
55 public class UnsignedAppletActionStorageImpl extends LockingReaderWriter implements UnsignedAppletActionStorage {
56
57 protected List<UnsignedAppletActionEntry> items;
58 private String readVersion = null;
59 public static final String versionPreffix="#VERSION ";
60 public static final String BACKUP_SUFFIX = "-backup";
61 public static final int currentVersion = 2;
62 private int lineCounter = 0;
63 private boolean loadingDisabled = false;
64
65 public UnsignedAppletActionStorageImpl(File location) {
66 super(location);
67 }
68
69 @Override
70 public void writeContents() throws IOException {
71 super.writeContents();
72 }
73
74 @Override
75 public synchronized void writeContentsLocked() throws IOException {
76 super.writeContentsLocked();
77 }
78
79 @Override
80 protected void readContents() throws IOException {
81 if (items == null) {
82 items = new ArrayList<>();
83 } else {
84 items.clear();
85 }
86 super.readContents();
87 }
88
89 @Override
90 protected void readLine(String line) {
91 if (line.trim().length() != 0) {
92 lineCounter++;
93 if (line.startsWith(versionPreffix) && line.trim().split("\\s+").length > 1) {
94 if (readVersion == null) {
95 readVersion = line.trim();
96 actOnVersionLoad();
97 }
98 } else {
99 if (lineCounter>0 && readVersion == null){
100 actOnNoVersionLoad();
101 }
102 if (!loadingDisabled) {
103 this.items.add(UnsignedAppletActionEntry.createFromString(line));
104 }
105 }
106 }
107 }
108
109 @Override
110 public void writeContent(BufferedWriter bw) throws IOException {
111 lineCounter = 0;
112 readVersion = null;
113 bw.write(versionPreffix + currentVersion + " - note, do not edit or modify this line. It may cause removal of this file.");
114 bw.newLine();
115 for (UnsignedAppletActionEntry item : items) {
116 try{
117 item.write(bw);
118 bw.newLine();
119 }catch (InvalidLineException ex){
120 OutputController.getLogger().log(ex);
121 }
122 }
123 }
124
125 @Override
126 public void add(final UnsignedAppletActionEntry item) {
127 doLocked(new Runnable() {
128 @Override
129 public void run() {
130 try {
131 readContents();
132 items.add(item);
133 writeContents();
134 } catch (IOException ex) {
135 throw new StorageIoException(ex);
136 }
137 }
138 });
139 }
140
141 @Override
142 public void update(final UnsignedAppletActionEntry item) {
143 doLocked(new Runnable() {
144 @Override
145 public void run() {
146 try {
147 if (items == null) {
148 throw new StorageIoException("Storage is not initialised, can not update");
149 }
150 if (!items.contains(item)) {
151 throw new StorageIoException("Storage does not contain item you are updating. can not update");
152 }
153 writeContents();
154 } catch (IOException ex) {
155 throw new StorageIoException(ex);
156 }
157 }
158 });
159 }
160
161 @Override
162 public UnsignedAppletActionEntry getMatchingItem(String documentBase, String codeBase, List<String> archives, Class<? extends RememberableDialog> id) {
163 List<UnsignedAppletActionEntry> results = getMatchingItems(documentBase, codeBase, archives);
164 if (results == null || results.isEmpty()) {
165 return null;
166 }
167 //no comaprsion id provided
168 if (id == null){
169 return results.get(0);
170 }
171 // Chose the first result, unless we find a 'stronger' result
172 // Actions such as 'always accept' or 'always reject' are 'stronger' than
173 // the hints 'was accepted' or 'was rejected'.
174 for (UnsignedAppletActionEntry candidate : results) {
175 if (candidate.getAppletSecurityActions().getAction(id) == ExecuteAppletAction.ALWAYS
176 || candidate.getAppletSecurityActions().getAction(id) == ExecuteAppletAction.NEVER) {
177 //return first found strong
178 return candidate;
179 }
180 }
181 //no strong found, return first
182 return results.get(0);
183 }
184
185 public List<UnsignedAppletActionEntry> getMatchingItems(String documentBase, String codeBase, List<String> archives) {
186 List<UnsignedAppletActionEntry> result = new ArrayList<>();
187 lock();
188 try {
189 readContents();
190 if (items == null) {
191 return result;
192 }
193 for (UnsignedAppletActionEntry unsignedAppletActionEntry : items) {
194 if (isMatching(unsignedAppletActionEntry, documentBase, codeBase, archives)) {
195 result.add(unsignedAppletActionEntry);
196 }
197 }
198 } catch (IOException e) {
199 throw new StorageIoException(e);
200 } finally {
201 unlock();
202 }
203 return result;
204 }
205
206 private boolean isMatching(UnsignedAppletActionEntry unsignedAppletActionEntry, String documentBase, String codeBase, List<String> archives) {
207 try {
208 boolean result = true;
209 if (documentBase != null && !documentBase.trim().isEmpty()) {
210 result = result && documentBase.matches(unsignedAppletActionEntry.getDocumentBase().getRegEx());
211 }
212 if (codeBase != null && !codeBase.trim().isEmpty()) {
213 result = result && codeBase.matches(unsignedAppletActionEntry.getCodeBase().getRegEx());
214 }
215 if (archives != null) {
216 List<String> saved = unsignedAppletActionEntry.getArchives();
217 if (saved == null || saved.isEmpty()) {
218 return result;
219 }
220 result = result && compareArchives(archives, saved);
221 }
222 return result;
223 } catch (PatternSyntaxException ex) {
224 OutputController.getLogger().log(OutputController.Level.WARNING_ALL, ex);
225 return false;
226 }
227 }
228
229 @Override
230 public String toString() {
231 return getBackingFile() + " " + super.toString();
232 }
233
234 private boolean compareArchives(List<String> archives, List<String> saved) {
235 if (archives == null && saved !=null){
236 return false;
237 }
238 if (archives != null && saved ==null){
239 return false;
240 }
241 if (archives == null && saved ==null){
242 return true;
243 }
244 if (archives.size() != saved.size()) {
245 return false;
246 }
247 Collections.sort(archives);
248 Collections.sort(saved);
249 for (int i = 0; i < saved.size(); i++) {
250 String string1 = saved.get(i);
251 String string2 = archives.get(i);
252 //intentional reference compare
253 if (string1 == string2) {
254 continue;
255 }
256 if (string1 == null || string2 == null) {
257 return false;
258 }
259 if (string1.trim().equals(string2.trim())) {
260 continue;
261 }
262 return false;
263 }
264 return true;
265 }
266
267 @Override
268 public UnsignedAppletActionEntry getMatchingItemByDocumentBase(String documentBase, Class<? extends RememberableDialog> id) {
269 return getMatchingItem(documentBase, null, null, id);
270 }
271
272 @Override
273 public UnsignedAppletActionEntry getMatchingItemByCodeBase(String codeBase, Class<? extends RememberableDialog> id) {
274 return getMatchingItem(null, codeBase, null, id);
275 }
276
277 @Override
278 public UnsignedAppletActionEntry getMatchingItemByBases(String documentBase, String codeBase, Class<? extends RememberableDialog> id) {
279 return getMatchingItem(documentBase, codeBase, null, id);
280 }
281
282 private void actOnVersionLoad() {
283 String versionS = readVersion.split("\\s+")[1];
284 int version = 0;
285 try{
286 version = Integer.valueOf(versionS);
287 } catch (NumberFormatException e){
288 OutputController.getLogger().log(e);
289 }
290 if (version < 2){
291 OutputController.getLogger().log("Stoping laoding of vulnereable "+getBackingFile().getAbsolutePath()+". Will be replaced");
292 loadingDisabled = true;
293 backupOldFile(version, getBackingFile());
294 } else {
295 loadingDisabled = false;
296 }
297 }
298
299 private void actOnNoVersionLoad() {
300 readVersion=versionPreffix+"0";
301 actOnVersionLoad();
302 }
303
304 private void backupOldFile(int version, File backingFile) {
305 try {
306 File backup = new File(backingFile.getAbsolutePath() + "." + version + BACKUP_SUFFIX);
307 OutputController.getLogger().log(OutputController.Level.MESSAGE_ALL, "backuping " + getBackingFile().getAbsolutePath() + " as " + backup.getName());
308 String warning = "- !WARNING! this is automated copy of old " + backingFile.getName() + " which was removed/replaced. Before you blindly copy those items back, please note, that this file might be modified without your approval by evil attacker. It is advised to not return below lines, or verify them before returning";
309 String s = FileUtils.loadFileAsString(backingFile);
310 s.replaceFirst("\\s*", "");
311 if (s.startsWith(versionPreffix)) {
312 s = s.replaceFirst("\n", " " + warning + "\n");
313 } else {
314 s = readVersion + " " + warning + "\n" + s;
315 }
316 FileUtils.saveFile(s, backup);
317 } catch (Exception ex) {
318 OutputController.getLogger().log(OutputController.Level.WARNING_ALL, "Error during backuping: " + ex.getMessage());
319 OutputController.getLogger().log(ex);
320 }
321 }
322 }