Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DatabaseEdit.cpp « cli « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: f5ca4ef2eb366573911446c501b672264d70a68d (plain)
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 *  Copyright (C) 2022 KeePassXC Team <team@keepassxc.org>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "DatabaseEdit.h"

#include "Utils.h"
#include "cli/DatabaseCreate.h"
#include "keys/ChallengeResponseKey.h"
#include "keys/FileKey.h"
#include "keys/PasswordKey.h"

#include <QCommandLineParser>
#include <QFileInfo>

const QCommandLineOption DatabaseEdit::UnsetPasswordOption =
    QCommandLineOption(QStringList() << "unset-password", QObject::tr("Unset the password for the database."));
const QCommandLineOption DatabaseEdit::UnsetKeyFileOption =
    QCommandLineOption(QStringList() << "unset-key-file", QObject::tr("Unset the key file for the database."));

DatabaseEdit::DatabaseEdit()
{
    name = QString("db-edit");
    description = QObject::tr("Edit a database.");
    options.append(DatabaseCreate::SetKeyFileOption);
    options.append(DatabaseCreate::SetPasswordOption);
    options.append(DatabaseEdit::UnsetKeyFileOption);
    options.append(DatabaseEdit::UnsetPasswordOption);
}

int DatabaseEdit::executeWithDatabase(QSharedPointer<Database> database, QSharedPointer<QCommandLineParser> parser)
{
    auto& out = Utils::STDOUT;
    auto& err = Utils::STDERR;

    const QStringList args = parser->positionalArguments();
    bool databaseWasChanged = false;

    if (parser->isSet(DatabaseCreate::SetPasswordOption) && parser->isSet(DatabaseEdit::UnsetPasswordOption)) {
        err << QObject::tr("Cannot use %1 and %2 at the same time.")
                   .arg(DatabaseCreate::SetPasswordOption.names().at(0))
                   .arg(DatabaseEdit::UnsetPasswordOption.names().at(0))
            << endl;
        return EXIT_FAILURE;
    }

    if (parser->isSet(DatabaseCreate::SetKeyFileOption) && parser->isSet(DatabaseEdit::UnsetKeyFileOption)) {
        err << QObject::tr("Cannot use %1 and %2 at the same time.")
                   .arg(DatabaseCreate::SetKeyFileOption.names().at(0))
                   .arg(DatabaseEdit::UnsetKeyFileOption.names().at(0))
            << endl;
        return EXIT_FAILURE;
    }

    bool hasKeyChange =
        (parser->isSet(DatabaseCreate::SetPasswordOption) || parser->isSet(DatabaseCreate::SetKeyFileOption)
         || parser->isSet(DatabaseEdit::UnsetPasswordOption) || parser->isSet(DatabaseEdit::UnsetKeyFileOption));

    if (hasKeyChange) {
        auto newDatabaseKey = getNewDatabaseKey(database,
                                                parser->isSet(DatabaseCreate::SetPasswordOption),
                                                parser->isSet(DatabaseEdit::UnsetPasswordOption),
                                                parser->value(DatabaseCreate::SetKeyFileOption),
                                                parser->isSet(DatabaseEdit::UnsetKeyFileOption));
        if (newDatabaseKey.isNull()) {
            err << QObject::tr("Could not change the database key.") << endl;
            return EXIT_FAILURE;
        }
        database->setKey(newDatabaseKey);
        databaseWasChanged = true;
    }

    if (!databaseWasChanged) {
        out << QObject::tr("Database was not modified.") << endl;
        return EXIT_SUCCESS;
    }

    QString errorMessage;
    if (!database->save(Database::Atomic, {}, &errorMessage)) {
        err << QObject::tr("Writing the database failed: %1").arg(errorMessage) << endl;
        return EXIT_FAILURE;
    }

    out << QObject::tr("Successfully edited the database.") << endl;
    return EXIT_SUCCESS;
}

QSharedPointer<CompositeKey> DatabaseEdit::getNewDatabaseKey(QSharedPointer<Database> database,
                                                             bool updatePassword,
                                                             bool removePassword,
                                                             QString newFileKeyPath,
                                                             bool removeKeyFile)
{
    auto& err = Utils::STDERR;
    auto newDatabaseKey = QSharedPointer<CompositeKey>::create();
    bool updateKeyFile = !newFileKeyPath.isEmpty();

    auto currentPasswordKey = database->key()->getKey(PasswordKey::UUID);
    auto currentFileKey = database->key()->getKey(FileKey::UUID);
    auto currentChallengeResponseKey = database->key()->getChallengeResponseKey(ChallengeResponseKey::UUID);

    if (removePassword && currentPasswordKey.isNull()) {
        err << QObject::tr("Cannot remove password: The database does not have a password.") << endl;
        return {};
    }

    if (removeKeyFile && currentFileKey.isNull()) {
        err << QObject::tr("Cannot remove file key: The database does not have a file key.") << endl;
        return {};
    }

    if (updatePassword) {
        QSharedPointer<PasswordKey> newPasswordKey = Utils::getConfirmedPassword();
        if (newPasswordKey.isNull()) {
            err << QObject::tr("Failed to set database password.") << endl;
            return {};
        }
        newDatabaseKey->addKey(newPasswordKey);
    } else if (!removePassword && !currentPasswordKey.isNull()) {
        newDatabaseKey->addKey(currentPasswordKey);
    }

    if (updateKeyFile) {
        QSharedPointer<FileKey> newFileKey = QSharedPointer<FileKey>::create();
        QString errorMessage;
        if (!Utils::loadFileKey(newFileKeyPath, newFileKey)) {
            err << QObject::tr("Loading the new key file failed: %1").arg(errorMessage) << endl;
            return {};
        }
        newDatabaseKey->addKey(newFileKey);
    } else if (!removeKeyFile && !currentFileKey.isNull()) {
        newDatabaseKey->addKey(currentFileKey);
    }

    // This is a sanity check to make sure that this function is not used if
    // new key types are introduced. Otherwise, those key types would be
    // silently removed from the database.
    for (const QSharedPointer<Key>& key : database->key()->keys()) {
        if (key->uuid() != PasswordKey::UUID && key->uuid() != FileKey::UUID) {
            err << QObject::tr("Found unexpected Key type %1").arg(key->uuid().toString()) << endl;
            return {};
        }
    }
    for (const QSharedPointer<ChallengeResponseKey>& key : database->key()->challengeResponseKeys()) {
        if (key->uuid() != ChallengeResponseKey::UUID) {
            err << QObject::tr("Found unexpected Key type %1").arg(key->uuid().toString()) << endl;
            return {};
        }
    }

    if (!currentChallengeResponseKey.isNull()) {
        newDatabaseKey->addChallengeResponseKey(currentChallengeResponseKey);
    }

    if (newDatabaseKey->keys().isEmpty() && newDatabaseKey->challengeResponseKeys().isEmpty()) {
        err << QObject::tr("Cannot remove all the keys from a database.") << endl;
        return {};
    }

    return newDatabaseKey;
}