2022-05-29 21:17:03 +02:00
|
|
|
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package sop.cli.picocli.commands;
|
|
|
|
|
|
|
|
import sop.exception.SOPGPException;
|
2022-06-06 20:06:14 +02:00
|
|
|
import sop.util.UTCUtil;
|
|
|
|
import sop.util.UTF8Util;
|
2022-05-29 21:17:03 +02:00
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.ByteArrayOutputStream;
|
2022-05-29 21:17:03 +02:00
|
|
|
import java.io.File;
|
2022-06-06 20:06:14 +02:00
|
|
|
import java.io.FileInputStream;
|
2022-11-11 15:56:33 +01:00
|
|
|
import java.io.FileNotFoundException;
|
2022-06-06 20:06:14 +02:00
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.OutputStream;
|
2022-05-29 21:17:03 +02:00
|
|
|
import java.util.Collection;
|
2022-06-06 20:06:14 +02:00
|
|
|
import java.util.Date;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.ResourceBundle;
|
2022-11-11 15:56:33 +01:00
|
|
|
import java.util.regex.Pattern;
|
2022-05-29 21:17:03 +02:00
|
|
|
|
|
|
|
public abstract class AbstractSopCmd implements Runnable {
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
public interface EnvironmentVariableResolver {
|
|
|
|
/**
|
|
|
|
* Resolve the value of the given environment variable.
|
|
|
|
* Return null if the variable is not present.
|
|
|
|
*
|
|
|
|
* @param name name of the variable
|
|
|
|
* @return variable value or null
|
|
|
|
*/
|
|
|
|
String resolveEnvironmentVariable(String name);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static final String PRFX_ENV = "@ENV:";
|
|
|
|
public static final String PRFX_FD = "@FD:";
|
|
|
|
public static final Date BEGINNING_OF_TIME = new Date(0);
|
|
|
|
public static final Date END_OF_TIME = new Date(8640000000000000L);
|
|
|
|
|
2022-11-14 13:54:08 +01:00
|
|
|
public static final Pattern PATTERN_FD = Pattern.compile("^\\d{1,20}$");
|
2022-11-11 15:56:33 +01:00
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
protected final ResourceBundle messages;
|
|
|
|
protected EnvironmentVariableResolver envResolver = System::getenv;
|
|
|
|
|
|
|
|
public AbstractSopCmd() {
|
|
|
|
this(Locale.getDefault());
|
|
|
|
}
|
|
|
|
|
|
|
|
public AbstractSopCmd(@Nonnull Locale locale) {
|
2022-08-04 12:15:53 +02:00
|
|
|
messages = ResourceBundle.getBundle("msg_sop", locale);
|
2022-06-06 20:06:14 +02:00
|
|
|
}
|
2022-05-29 21:17:03 +02:00
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
void throwIfOutputExists(String output) {
|
|
|
|
if (output == null) {
|
2022-05-29 21:17:03 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
File outputFile = new File(output);
|
2022-05-29 21:17:03 +02:00
|
|
|
if (outputFile.exists()) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.output_file_already_exists", outputFile.getAbsolutePath());
|
|
|
|
throw new SOPGPException.OutputExists(errorMsg);
|
2022-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
public String getMsg(String key) {
|
|
|
|
return messages.getString(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMsg(String key, String arg1) {
|
|
|
|
return String.format(messages.getString(key), arg1);
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMsg(String key, String arg1, String arg2) {
|
|
|
|
return String.format(messages.getString(key), arg1, arg2);
|
|
|
|
}
|
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
void throwIfMissingArg(Object arg, String argName) {
|
|
|
|
if (arg == null) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.usage.argument_required", argName);
|
|
|
|
throw new SOPGPException.MissingArg(errorMsg);
|
2022-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void throwIfEmptyParameters(Collection<?> arg, String parmName) {
|
|
|
|
if (arg.isEmpty()) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.usage.parameter_required", parmName);
|
|
|
|
throw new SOPGPException.MissingArg(errorMsg);
|
2022-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
<T> T throwIfUnsupportedSubcommand(T subcommand, String subcommandName) {
|
|
|
|
if (subcommand == null) {
|
2022-06-06 20:06:14 +02:00
|
|
|
String errorMsg = getMsg("sop.error.feature_support.subcommand_not_supported", subcommandName);
|
|
|
|
throw new SOPGPException.UnsupportedSubcommand(errorMsg);
|
2022-05-29 21:17:03 +02:00
|
|
|
}
|
|
|
|
return subcommand;
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
void setEnvironmentVariableResolver(EnvironmentVariableResolver envResolver) {
|
|
|
|
if (envResolver == null) {
|
|
|
|
throw new NullPointerException("Variable envResolver cannot be null.");
|
|
|
|
}
|
|
|
|
this.envResolver = envResolver;
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputStream getInput(String indirectInput) throws IOException {
|
|
|
|
if (indirectInput == null) {
|
|
|
|
throw new IllegalArgumentException("Input cannot not be null.");
|
|
|
|
}
|
|
|
|
|
|
|
|
String trimmed = indirectInput.trim();
|
|
|
|
if (trimmed.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Input cannot be blank.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (trimmed.startsWith(PRFX_ENV)) {
|
|
|
|
if (new File(trimmed).exists()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.ambiguous_filename", trimmed);
|
|
|
|
throw new SOPGPException.AmbiguousInput(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
String envName = trimmed.substring(PRFX_ENV.length());
|
|
|
|
String envValue = envResolver.resolveEnvironmentVariable(envName);
|
|
|
|
if (envValue == null) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.environment_variable_not_set", envName);
|
|
|
|
throw new IllegalArgumentException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (envValue.trim().isEmpty()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.environment_variable_empty", envName);
|
|
|
|
throw new IllegalArgumentException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new ByteArrayInputStream(envValue.getBytes("UTF8"));
|
|
|
|
|
|
|
|
} else if (trimmed.startsWith(PRFX_FD)) {
|
|
|
|
|
|
|
|
if (new File(trimmed).exists()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.ambiguous_filename", trimmed);
|
|
|
|
throw new SOPGPException.AmbiguousInput(errorMsg);
|
|
|
|
}
|
|
|
|
|
2022-11-11 15:56:33 +01:00
|
|
|
File fdFile = fileDescriptorFromString(trimmed);
|
|
|
|
try {
|
|
|
|
FileInputStream fileIn = new FileInputStream(fdFile);
|
|
|
|
return fileIn;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.file_descriptor_not_found", fdFile.getAbsolutePath());
|
|
|
|
throw new IOException(errorMsg, e);
|
|
|
|
}
|
2022-06-06 20:06:14 +02:00
|
|
|
} else {
|
|
|
|
File file = new File(trimmed);
|
|
|
|
if (!file.exists()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.input_file_does_not_exist", file.getAbsolutePath());
|
|
|
|
throw new SOPGPException.MissingInput(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.isFile()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.input_not_a_file", file.getAbsolutePath());
|
|
|
|
throw new SOPGPException.MissingInput(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new FileInputStream(file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public OutputStream getOutput(String indirectOutput) throws IOException {
|
|
|
|
if (indirectOutput == null) {
|
|
|
|
throw new IllegalArgumentException("Output cannot be null.");
|
|
|
|
}
|
|
|
|
|
|
|
|
String trimmed = indirectOutput.trim();
|
|
|
|
if (trimmed.isEmpty()) {
|
|
|
|
throw new IllegalArgumentException("Output cannot be blank.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ENV not allowed for output
|
|
|
|
if (trimmed.startsWith(PRFX_ENV)) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.illegal_use_of_env_designator");
|
|
|
|
throw new SOPGPException.UnsupportedSpecialPrefix(errorMsg);
|
|
|
|
}
|
|
|
|
|
2022-11-11 15:56:33 +01:00
|
|
|
// File Descriptor
|
2022-06-06 20:06:14 +02:00
|
|
|
if (trimmed.startsWith(PRFX_FD)) {
|
2022-11-11 15:56:33 +01:00
|
|
|
File fdFile = fileDescriptorFromString(trimmed);
|
|
|
|
try {
|
|
|
|
FileOutputStream fout = new FileOutputStream(fdFile);
|
|
|
|
return fout;
|
|
|
|
} catch (FileNotFoundException e) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.file_descriptor_not_found", fdFile.getAbsolutePath());
|
|
|
|
throw new IOException(errorMsg, e);
|
|
|
|
}
|
2022-06-06 20:06:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
File file = new File(trimmed);
|
|
|
|
if (file.exists()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.output_file_already_exists", file.getAbsolutePath());
|
|
|
|
throw new SOPGPException.OutputExists(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!file.createNewFile()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.output_file_cannot_be_created", file.getAbsolutePath());
|
|
|
|
throw new IOException(errorMsg);
|
|
|
|
}
|
|
|
|
|
|
|
|
return new FileOutputStream(file);
|
|
|
|
}
|
2022-11-11 15:56:33 +01:00
|
|
|
|
|
|
|
public File fileDescriptorFromString(String fdString) {
|
|
|
|
File fdDir = new File("/dev/fd/");
|
|
|
|
if (!fdDir.exists()) {
|
|
|
|
String errorMsg = getMsg("sop.error.indirect_data_type.designator_fd_not_supported");
|
|
|
|
throw new SOPGPException.UnsupportedSpecialPrefix(errorMsg);
|
|
|
|
}
|
|
|
|
String fdNumber = fdString.substring(PRFX_FD.length());
|
|
|
|
if (!PATTERN_FD.matcher(fdNumber).matches()) {
|
2022-11-14 13:54:08 +01:00
|
|
|
throw new IllegalArgumentException("File descriptor must be a positive number.");
|
2022-11-11 15:56:33 +01:00
|
|
|
}
|
|
|
|
File descriptor = new File(fdDir, fdNumber);
|
|
|
|
return descriptor;
|
|
|
|
}
|
|
|
|
|
2022-06-06 20:06:14 +02:00
|
|
|
public static String stringFromInputStream(InputStream inputStream) throws IOException {
|
|
|
|
try {
|
|
|
|
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
|
|
|
|
byte[] buf = new byte[4096]; int read;
|
|
|
|
while ((read = inputStream.read(buf)) != -1) {
|
|
|
|
byteOut.write(buf, 0, read);
|
|
|
|
}
|
|
|
|
// TODO: For decrypt operations we MUST accept non-UTF8 passwords
|
|
|
|
return UTF8Util.decodeUTF8(byteOut.toByteArray());
|
|
|
|
} finally {
|
|
|
|
inputStream.close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date parseNotAfter(String notAfter) {
|
|
|
|
Date date = notAfter.equals("now") ? new Date() : notAfter.equals("-") ? END_OF_TIME : UTCUtil.parseUTCDate(notAfter);
|
|
|
|
if (date == null) {
|
|
|
|
String errorMsg = getMsg("sop.error.input.malformed_not_after");
|
|
|
|
throw new IllegalArgumentException(errorMsg);
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
|
|
|
public Date parseNotBefore(String notBefore) {
|
|
|
|
Date date = notBefore.equals("now") ? new Date() : notBefore.equals("-") ? BEGINNING_OF_TIME : UTCUtil.parseUTCDate(notBefore);
|
|
|
|
if (date == null) {
|
|
|
|
String errorMsg = getMsg("sop.error.input.malformed_not_before");
|
|
|
|
throw new IllegalArgumentException(errorMsg);
|
|
|
|
}
|
|
|
|
return date;
|
|
|
|
}
|
|
|
|
|
2022-05-29 21:17:03 +02:00
|
|
|
}
|