2018-07-07 17:58:32 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* Copyright 2017 Florian Schmaus, 2018 Paul Schaub.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-07-08 19:36:44 +02:00
|
|
|
package org.jivesoftware.smackx.ox.store.filebased;
|
2018-07-07 17:58:32 +02:00
|
|
|
|
2018-07-08 16:25:56 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.BufferedWriter;
|
2018-07-07 17:58:32 +02:00
|
|
|
import java.io.File;
|
2018-07-08 16:25:56 +02:00
|
|
|
import java.io.FileNotFoundException;
|
2018-07-07 17:58:32 +02:00
|
|
|
import java.io.IOException;
|
2018-07-08 16:25:56 +02:00
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.util.logging.Level;
|
|
|
|
import java.util.logging.Logger;
|
2018-07-07 17:58:32 +02:00
|
|
|
|
2018-07-08 19:36:44 +02:00
|
|
|
import org.jivesoftware.smackx.ox.store.abstr.AbstractOpenPgpTrustStore;
|
2018-07-08 16:25:56 +02:00
|
|
|
import org.jivesoftware.smackx.ox.util.Util;
|
2018-07-07 17:58:32 +02:00
|
|
|
|
|
|
|
import org.jxmpp.jid.BareJid;
|
2018-07-08 19:36:44 +02:00
|
|
|
import org.pgpainless.pgpainless.key.OpenPgpV4Fingerprint;
|
2018-07-07 17:58:32 +02:00
|
|
|
|
2018-07-08 16:25:56 +02:00
|
|
|
public class FileBasedOpenPgpTrustStore extends AbstractOpenPgpTrustStore {
|
|
|
|
|
|
|
|
private static final Logger LOGGER = Logger.getLogger(FileBasedOpenPgpTrustStore.class.getName());
|
2018-07-07 17:58:32 +02:00
|
|
|
|
|
|
|
private final File basePath;
|
|
|
|
|
|
|
|
public static String TRUST_RECORD(OpenPgpV4Fingerprint fingerprint) {
|
|
|
|
return fingerprint.toString() + ".trust";
|
|
|
|
}
|
|
|
|
|
|
|
|
public FileBasedOpenPgpTrustStore(File basePath) {
|
|
|
|
this.basePath = basePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-08 16:25:56 +02:00
|
|
|
protected Trust readTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint) throws IOException {
|
2018-07-07 17:58:32 +02:00
|
|
|
File file = getTrustPath(owner, fingerprint);
|
2018-07-08 16:25:56 +02:00
|
|
|
BufferedReader reader = null;
|
2018-07-07 17:58:32 +02:00
|
|
|
try {
|
2018-07-08 16:25:56 +02:00
|
|
|
reader = Files.newBufferedReader(file.toPath(), Util.UTF8);
|
2018-07-07 17:58:32 +02:00
|
|
|
|
2018-07-08 16:25:56 +02:00
|
|
|
Trust trust = null;
|
|
|
|
String line; int lineNr = 0;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
lineNr++;
|
|
|
|
try {
|
|
|
|
trust = Trust.valueOf(line);
|
|
|
|
break;
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
LOGGER.log(Level.WARNING, "Skipping invalid trust record in line " + lineNr + " of file " +
|
|
|
|
file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
reader.close();
|
|
|
|
return trust != null ? trust : Trust.undecided;
|
2018-07-07 17:58:32 +02:00
|
|
|
} catch (IOException e) {
|
2018-07-08 16:25:56 +02:00
|
|
|
if (reader != null) {
|
|
|
|
try {
|
|
|
|
reader.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
// Don't care
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (e instanceof FileNotFoundException) {
|
|
|
|
return Trust.undecided;
|
|
|
|
}
|
|
|
|
throw e;
|
2018-07-07 17:58:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2018-07-08 16:25:56 +02:00
|
|
|
protected void writeTrust(BareJid owner, OpenPgpV4Fingerprint fingerprint, Trust trust) throws IOException {
|
|
|
|
File file = getTrustPath(owner, fingerprint);
|
2018-07-07 17:58:32 +02:00
|
|
|
|
2018-07-08 16:25:56 +02:00
|
|
|
if (!file.exists()) {
|
|
|
|
if (!file.createNewFile()) {
|
|
|
|
throw new IOException("Cannot create file " + file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (file.isDirectory()) {
|
|
|
|
throw new IOException("File " + file.getAbsolutePath() + " is a directory.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BufferedWriter writer = null;
|
|
|
|
try {
|
|
|
|
writer = Files.newBufferedWriter(file.toPath(), Util.UTF8);
|
|
|
|
writer.write(trust.toString());
|
|
|
|
writer.flush();
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
if (writer != null) {
|
|
|
|
try {
|
|
|
|
writer.close();
|
|
|
|
} catch (IOException ignored) {
|
|
|
|
// Don't care
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw e;
|
|
|
|
}
|
2018-07-07 17:58:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private File getTrustPath(BareJid owner, OpenPgpV4Fingerprint fingerprint) {
|
|
|
|
return new File(FileBasedOpenPgpStore.getContactsPath(basePath, owner), TRUST_RECORD(fingerprint));
|
|
|
|
}
|
|
|
|
}
|