Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
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
/***********************************************************************************************************************************
Stanza Rekey Command
***********************************************************************************************************************************/
#include <build.h>
 
#include <time.h>
 
#include "command/control/common.h"
#include "command/stanza/common.h"
#include "command/stanza/rekey.h"
#include "common/debug.h"
#include "common/format/format.h"
#include "common/log.h"
#include "common/memContext.h"
#include "config/config.h"
#include "info/helper.h"
#include "storage/helper.h"
 
/**********************************************************************************************************************************/
FN_EXTERN void
cmdStanzaRekey(void)
{
    FUNCTION_LOG_VOID(logLevelDebug);
 
    // Verify that a stop was not issued before proceeding
    lockStopTest();
 
    MEM_CONTEXT_TEMP_BEGIN()
    {
        unsigned int repoIdxMin = 0;
        unsigned int repoIdxMax = cfgOptionGroupIdxTotal(cfgOptGrpRepo) - 1;
        unsigned int repoTotal = 0;
 
        // If the repo was specified then rekey only that repo
        if (cfgOptionTest(cfgOptRepo))
        {
            repoIdxMin = cfgOptionGroupIdxDefault(cfgOptGrpRepo);
            repoIdxMax = repoIdxMin;
        }
 
        for (unsigned int repoIdx = repoIdxMin; repoIdx <= repoIdxMax; repoIdx++)
        {
            // Get the repo storage in case it is remote and encryption settings need to be pulled down
            storageRepoIdx(repoIdx);
 
            const CipherType cipherType = cfgOptionIdxStrId(cfgOptRepoCipherType, repoIdx);
 
            // Skip a repo that is not encrypted
            if (cipherType == cipherTypeNone)
                continue;
 
            repoTotal++;
 
            const bool rekey = cfgOptionIdxTest(cfgOptRepoCipherPassOld, repoIdx);
 
            LOG_INFO_FMT(
                CFGCMD_STANZA_REKEY " for stanza '%s' on %s", strZ(cfgOptionDisplay(cfgOptStanza)),
                cfgOptionGroupName(cfgOptGrpRepo, repoIdx));
 
            // Error when the new passphrase is the old passphrase
            if (rekey && strEq(cfgOptionIdxStr(cfgOptRepoCipherPass, repoIdx), cfgOptionIdxStr(cfgOptRepoCipherPassOld, repoIdx)))
            {
                THROW_FMT(
                    OptionInvalidValueError, "option '%s' must not be the same as option '%s'",
                    cfgOptionIdxName(cfgOptRepoCipherPass, repoIdx), cfgOptionIdxName(cfgOptRepoCipherPassOld, repoIdx));
            }
 
            // Load the info files (errors if missing)
            InfoArchive *const infoArchive = infoArchiveRepoIdx(repoIdx);
            InfoBackup *const infoBackup = infoBackupRepoIdxP(repoIdx);
 
            // Format of the repository
            const unsigned int format = infoArchiveFormat(infoArchive);
 
            // Warn when there is nothing to do. Format < 6 has a single archive key that cannot be rotated.
            if (!rekey && format < REPOSITORY_FORMAT_6)
            {
                LOG_WARN("archive key rotation requires repository format " STRINGIFY(REPOSITORY_FORMAT_6) " or higher");
                continue;
            }
 
            // Check the new passphrase
            if (rekey)
                stanzaCipherPassCheck(repoIdx, format, "rekey a stanza");
 
            // Rotate the archive key
            if (format >= REPOSITORY_FORMAT_6)
            {
                infoArchiveCipherRotate(infoArchive, cipherSpecGen(cipherType, format), time(NULL));
 
                LOG_INFO_FMT("rotate archive key to id %s", strZ(cipherSpecMapIdCurrent(infoArchiveCipherSpecMap(infoArchive))));
            }
 
            // Save the info files with the passphrase
            infoArchiveCipherPriorClear(infoArchive);
            infoArchiveRepoIdxSave(repoIdx);
 
            if (rekey)
            {
                infoBackupCipherPriorClear(infoBackup);
                infoBackupRepoIdxSave(repoIdx);
 
                LOG_INFO("rekey info files with the new passphrase");
            }
        }
 
        // Error when no repo is encrypted
        if (repoTotal == 0)
            THROW(RepoInvalidError, CFGCMD_STANZA_REKEY " requires an encrypted repository");
    }
    MEM_CONTEXT_TEMP_END();
 
    FUNCTION_LOG_RETURN_VOID();
}