2017-09-06 09:14:41 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 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 <cstdlib>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "Add.h"
|
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
|
2018-10-28 14:55:00 -04:00
|
|
|
#include "cli/TextStream.h"
|
2017-09-06 09:14:41 -04:00
|
|
|
#include "cli/Utils.h"
|
|
|
|
#include "core/Database.h"
|
|
|
|
#include "core/Entry.h"
|
|
|
|
#include "core/Group.h"
|
|
|
|
#include "core/PasswordGenerator.h"
|
|
|
|
|
|
|
|
Add::Add()
|
|
|
|
{
|
2018-02-05 19:17:36 -05:00
|
|
|
name = QString("add");
|
|
|
|
description = QObject::tr("Add a new entry to a database.");
|
2017-09-06 09:14:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Add::~Add()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-05 19:17:36 -05:00
|
|
|
int Add::execute(const QStringList& arguments)
|
2017-09-06 09:14:41 -04:00
|
|
|
{
|
2018-10-28 14:55:00 -04:00
|
|
|
TextStream inputTextStream(Utils::STDIN, QIODevice::ReadOnly);
|
|
|
|
TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
|
|
|
|
TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
|
2017-09-06 09:14:41 -04:00
|
|
|
|
|
|
|
QCommandLineParser parser;
|
2018-09-29 13:00:47 -04:00
|
|
|
parser.setApplicationDescription(description);
|
2017-09-06 09:14:41 -04:00
|
|
|
parser.addPositionalArgument("database", QObject::tr("Path of the database."));
|
2018-11-28 11:24:12 -05:00
|
|
|
parser.addOption(Command::QuietOption);
|
2018-11-28 16:15:15 -05:00
|
|
|
parser.addOption(Command::KeyFileOption);
|
2017-09-06 09:14:41 -04:00
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QCommandLineOption username(QStringList() << "u"
|
|
|
|
<< "username",
|
2017-09-06 09:14:41 -04:00
|
|
|
QObject::tr("Username for the entry."),
|
|
|
|
QObject::tr("username"));
|
|
|
|
parser.addOption(username);
|
|
|
|
|
|
|
|
QCommandLineOption url(QStringList() << "url", QObject::tr("URL for the entry."), QObject::tr("URL"));
|
|
|
|
parser.addOption(url);
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QCommandLineOption prompt(QStringList() << "p"
|
|
|
|
<< "password-prompt",
|
2017-09-06 09:14:41 -04:00
|
|
|
QObject::tr("Prompt for the entry's password."));
|
|
|
|
parser.addOption(prompt);
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QCommandLineOption generate(QStringList() << "g"
|
|
|
|
<< "generate",
|
2017-09-06 09:14:41 -04:00
|
|
|
QObject::tr("Generate a password for the entry."));
|
|
|
|
parser.addOption(generate);
|
|
|
|
|
2018-10-31 23:27:38 -04:00
|
|
|
QCommandLineOption length(QStringList() << "l"
|
|
|
|
<< "password-length",
|
2017-09-06 09:14:41 -04:00
|
|
|
QObject::tr("Length for the generated password."),
|
|
|
|
QObject::tr("length"));
|
|
|
|
parser.addOption(length);
|
|
|
|
|
|
|
|
parser.addPositionalArgument("entry", QObject::tr("Path of the entry to add."));
|
2018-09-29 13:00:47 -04:00
|
|
|
|
|
|
|
parser.addHelpOption();
|
2017-09-06 09:14:41 -04:00
|
|
|
parser.process(arguments);
|
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
|
|
|
if (args.size() != 2) {
|
|
|
|
outputTextStream << parser.helpText().replace("keepassxc-cli", "keepassxc-cli add");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-09-29 13:00:47 -04:00
|
|
|
const QString& databasePath = args.at(0);
|
|
|
|
const QString& entryPath = args.at(1);
|
2017-09-06 09:14:41 -04:00
|
|
|
|
2018-12-11 10:49:51 -05:00
|
|
|
auto db = Utils::unlockDatabase(databasePath,
|
|
|
|
parser.value(Command::KeyFileOption),
|
|
|
|
parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT,
|
|
|
|
Utils::STDERR);
|
2018-09-29 13:00:47 -04:00
|
|
|
if (!db) {
|
2017-09-06 09:14:41 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validating the password length here, before we actually create
|
|
|
|
// the entry.
|
|
|
|
QString passwordLength = parser.value(length);
|
|
|
|
if (!passwordLength.isEmpty() && !passwordLength.toInt()) {
|
2018-09-29 13:00:47 -04:00
|
|
|
errorTextStream << QObject::tr("Invalid value for password length %1.").arg(passwordLength) << endl;
|
2017-09-06 09:14:41 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry* entry = db->rootGroup()->addEntryWithPath(entryPath);
|
|
|
|
if (!entry) {
|
2018-09-29 13:00:47 -04:00
|
|
|
errorTextStream << QObject::tr("Could not create entry with path %1.").arg(entryPath) << endl;
|
2017-09-06 09:14:41 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parser.value("username").isEmpty()) {
|
|
|
|
entry->setUsername(parser.value("username"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!parser.value("url").isEmpty()) {
|
|
|
|
entry->setUrl(parser.value("url"));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parser.isSet(prompt)) {
|
2018-11-28 11:24:12 -05:00
|
|
|
if (!parser.isSet(Command::QuietOption)) {
|
|
|
|
outputTextStream << QObject::tr("Enter password for new entry: ") << flush;
|
|
|
|
}
|
|
|
|
QString password = Utils::getPassword(parser.isSet(Command::QuietOption) ? Utils::DEVNULL : Utils::STDOUT);
|
2017-09-06 09:14:41 -04:00
|
|
|
entry->setPassword(password);
|
|
|
|
} else if (parser.isSet(generate)) {
|
|
|
|
PasswordGenerator passwordGenerator;
|
|
|
|
|
|
|
|
if (passwordLength.isEmpty()) {
|
|
|
|
passwordGenerator.setLength(PasswordGenerator::DefaultLength);
|
|
|
|
} else {
|
2018-11-22 05:47:31 -05:00
|
|
|
passwordGenerator.setLength(passwordLength.toInt());
|
2017-09-06 09:14:41 -04:00
|
|
|
}
|
|
|
|
|
2018-01-22 07:47:20 -05:00
|
|
|
passwordGenerator.setCharClasses(PasswordGenerator::DefaultCharset);
|
2018-02-05 19:17:36 -05:00
|
|
|
passwordGenerator.setFlags(PasswordGenerator::DefaultFlags);
|
2017-09-06 09:14:41 -04:00
|
|
|
QString password = passwordGenerator.generatePassword();
|
|
|
|
entry->setPassword(password);
|
|
|
|
}
|
|
|
|
|
2018-11-22 05:47:31 -05:00
|
|
|
QString errorMessage;
|
|
|
|
if (!db->save(databasePath, &errorMessage, true, false)) {
|
2018-09-29 13:00:47 -04:00
|
|
|
errorTextStream << QObject::tr("Writing the database failed %1.").arg(errorMessage) << endl;
|
2017-09-06 09:14:41 -04:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-11-28 11:24:12 -05:00
|
|
|
if (!parser.isSet(Command::QuietOption)) {
|
|
|
|
outputTextStream << QObject::tr("Successfully added entry %1.").arg(entry->title()) << endl;
|
|
|
|
}
|
2017-09-06 09:14:41 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|