/*********************************************************************************************************************************** Repository Get Command ***********************************************************************************************************************************/ #include #include #include "command/repo/common.h" #include "command/repo/get.h" #include "common/crypto/cipherBlock.h" #include "common/debug.h" #include "common/format/cipherBlockFormat.h" #include "common/io/fdWrite.h" #include "common/io/io.h" #include "common/log.h" #include "common/memContext.h" #include "common/regExp.h" #include "config/config.h" #include "storage/helper.h" #include "info/info.h" #include "info/infoArchive.h" #include "info/infoBackup.h" /*********************************************************************************************************************************** Write source file to destination IO ***********************************************************************************************************************************/ static int storageGetProcess(IoWrite *const destination) { FUNCTION_LOG_BEGIN(logLevelDebug); FUNCTION_LOG_PARAM(IO_READ, destination); FUNCTION_LOG_END(); // Get source file if (strLstSize(cfgCommandParam()) != 1) THROW(ParamRequiredError, "source file required"); const String *file = strLstGet(cfgCommandParam(), 0); // Assume the file is missing int result = 1; MEM_CONTEXT_TEMP_BEGIN() { // Is path valid for repo? file = repoPathIsValid(file); // Create new file read IoRead *const source = storageReadIo( storageNewReadP(storageRepo(), file, .ignoreMissing = cfgOptionBool(cfgOptIgnoreMissing))); // Is the file an info file, i.e. one that has a header in front of its content? bool fileIsInfo = false; // Is the file missing? bool missing = false; // Add decryption if needed if (!cfgOptionBool(cfgOptRaw)) { const CipherType repoCipherType = cfgOptionStrId(cfgOptRepoCipherType); // Check for a missing file before finding the passphrase when missing files are ignored if (repoCipherType != cipherTypeNone && cfgOptionBool(cfgOptIgnoreMissing)) missing = !storageExistsP(storageRepo(), file); if (repoCipherType != cipherTypeNone && !missing) { // Determine the passphrase using the following pattern: // // REPO / (main passphrase) // / archive / (main passphrase) // / archive / stanza / (archive passphrase) // / backup / (main passphrase) // / backup / stanza / (manifest passphrase) // / backup / stanza / set / (backup passphrase) // / backup / stanza / backup.history / (manifest passphrase) // // Format >= 6 stores a manifest passphrase for each backup set by label. // // Nothing should be stored at the top level of the repo except the backup/archive paths. The backup/archive paths // should contain only stanza paths. // ----------------------------------------------------------------------------------------------------------------- const CipherSpec *cipherSpec = NULL; const CipherSpecMap *cipherSpecMap = NULL; const StringList *const filePathSplitLst = strLstNewSplit(file, FSLASH_STR); // At a minimum the path must contain archive/backup, a stanza, and a file if (strLstSize(filePathSplitLst) > 2) { const String *const stanza = strLstGet(filePathSplitLst, 1); // If stanza option is specified then it must match the given file path if (cfgOptionStrNull(cfgOptStanza) != NULL && !strEq(stanza, cfgOptionStr(cfgOptStanza))) { THROW_FMT( OptionInvalidValueError, "stanza name '%s' given in option doesn't match the given path", strZ(cfgOptionDisplay(cfgOptStanza))); } // Archive path if (strEq(strLstGet(filePathSplitLst, 0), STORAGE_PATH_ARCHIVE_STR)) { // Find the archive keys. WAL stores the id of the key it is encrypted with in the header. if (!strEndsWithZ(file, INFO_ARCHIVE_FILE) && !strEndsWithZ(file, INFO_ARCHIVE_FILE INFO_COPY_EXT)) { const InfoArchive *const info = infoArchiveLoadFile( storageRepo(), strNewFmt(STORAGE_PATH_ARCHIVE "/%s/%s", strZ(stanza), INFO_ARCHIVE_FILE), cfgCipherSpecMain()); cipherSpecMap = infoArchiveCipherSpecMap(info); } // Else the file is the archive info, which the repo passphrase opens else { cipherSpec = cfgCipherSpecMain(); fileIsInfo = true; } } // Backup path if (strEq(strLstGet(filePathSplitLst, 0), STORAGE_PATH_BACKUP_STR)) { cipherSpec = cfgCipherSpecMain(); if (!strEndsWithZ(file, INFO_BACKUP_FILE) && !strEndsWithZ(file, INFO_BACKUP_FILE INFO_COPY_EXT)) { // Find the manifest passphrase by the label of the backup const InfoBackup *const info = infoBackupLoadFile( storageRepo(), strNewFmt(STORAGE_PATH_BACKUP "/%s/%s", strZ(stanza), INFO_BACKUP_FILE), cfgCipherSpecMain()); const String *backupLabel = strLstGet(filePathSplitLst, 2); const bool history = strEq(backupLabel, STRDEF(BACKUP_PATH_HISTORY)); // Get the label from the name of the history manifest if (history) { backupLabel = strBase(file); } // Else get the label from the destination of the latest link else if (strEq(backupLabel, STRDEF(BACKUP_LINK_LATEST))) { const StorageInfo latestInfo = storageInfoP( storageRepo(), strNewFmt(STORAGE_PATH_BACKUP "/%s/" BACKUP_LINK_LATEST, strZ(stanza)), .level = storageInfoLevelDetail, .ignoreMissing = true); if (latestInfo.type == storageTypeLink) backupLabel = latestInfo.linkDestination; } const CipherSpec *const cipherSpecManifest = regExpMatchOne( backupRegExpP(.full = true, .differential = true, .incremental = true, .noAnchorEnd = true), backupLabel) ? infoBackupCipherSpecManifest(info, backupLabel) : infoBackupCipherSpec(info); // Find the backup passphrase if not a manifest if (!history && !strEndsWithZ(file, BACKUP_MANIFEST_FILE) && !strEndsWithZ(file, BACKUP_MANIFEST_FILE INFO_COPY_EXT)) { const Manifest *const manifest = manifestLoadFile( storageRepo(), strNewFmt( STORAGE_PATH_BACKUP "/%s/%s/%s", strZ(stanza), strZ(strLstGet(filePathSplitLst, 2)), BACKUP_MANIFEST_FILE), cipherSpecManifest); cipherSpec = manifestCipherSpec(manifest); } // Else use the manifest passphrase else cipherSpec = cipherSpecManifest; } // Else the file is the backup info, which the repo passphrase opens else fileIsInfo = true; } } // Error when unable to determine cipher passphrase if (cipherSpec == NULL && cipherSpecMap == NULL) THROW_FMT(OptionInvalidValueError, "unable to determine cipher passphrase for '%s'", strZ(file)); // Add the decryption filter. An info file is read through the filter that reads its header and WAL through the // filter that selects the key by the id in the header. if (fileIsInfo) cipherBlockFormatFilterGroupReadAdd(ioReadFilterGroup(source), cipherSpec); else if (cipherSpecMap != NULL) cipherBlockFormatFilterGroupReadAddMap(ioReadFilterGroup(source), cipherSpecMap); else { ASSERT(cipherSpecType(cipherSpec) != cipherTypeNone); cipherBlockFilterGroupAdd(ioReadFilterGroup(source), cipherModeDecrypt, cipherSpec); } } } // Open source if (!missing && ioReadOpen(source)) { // Open the destination file now that we know the source exists and is readable ioWriteOpen(destination); // Copy data from source to destination ioCopyP(source, destination); // Close the source and destination ioReadClose(source); ioWriteClose(destination); // Source file exists result = 0; } } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN(INT, result); } /**********************************************************************************************************************************/ FN_EXTERN int cmdStorageGet(void) { FUNCTION_LOG_VOID(logLevelDebug); // Assume the file is missing int result = 1; MEM_CONTEXT_TEMP_BEGIN() { TRY_BEGIN() { result = storageGetProcess(ioFdWriteNew(STRDEF("stdout"), STDOUT_FILENO, cfgOptionUInt64(cfgOptIoTimeout))); } // Ignore write errors because it's possible (even likely) that this output is being piped to something like head which will // exit when it gets what it needs and leave us writing to a broken pipe. It would be better to just ignore the broken pipe // error but currently we don't store system error codes. CATCH(FileWriteError) { } TRY_END(); } MEM_CONTEXT_TEMP_END(); FUNCTION_LOG_RETURN(INT, result); }