2018-01-19 15:48:40 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2018 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>
|
|
|
|
|
2018-01-31 00:50:02 +01:00
|
|
|
#include "Generate.h"
|
2018-09-29 19:00:47 +02:00
|
|
|
#include "cli/Utils.h"
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
#include <QCommandLineParser>
|
|
|
|
#include <QTextStream>
|
|
|
|
|
|
|
|
#include "core/PasswordGenerator.h"
|
|
|
|
|
2018-01-31 00:50:02 +01:00
|
|
|
Generate::Generate()
|
2018-01-19 15:48:40 +01:00
|
|
|
{
|
2018-02-06 01:17:36 +01:00
|
|
|
name = QString("generate");
|
|
|
|
description = QObject::tr("Generate a new random password.");
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 00:50:02 +01:00
|
|
|
Generate::~Generate()
|
2018-01-19 15:48:40 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2018-02-06 01:17:36 +01:00
|
|
|
int Generate::execute(const QStringList& arguments)
|
2018-01-19 15:48:40 +01:00
|
|
|
{
|
2018-09-29 19:00:47 +02:00
|
|
|
QTextStream in(Utils::STDIN, QIODevice::ReadOnly);
|
|
|
|
QTextStream out(Utils::STDOUT, QIODevice::WriteOnly);
|
|
|
|
out.setCodec("UTF-8"); // force UTF-8 to prevent ??? characters in extended-ASCII passwords
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
QCommandLineParser parser;
|
2018-09-29 19:00:47 +02:00
|
|
|
parser.setApplicationDescription(description);
|
|
|
|
QCommandLineOption len(QStringList() << "L" << "length",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Length of the generated password"),
|
2018-03-31 16:01:30 -04:00
|
|
|
QObject::tr("length"));
|
2018-01-21 02:32:46 +01:00
|
|
|
parser.addOption(len);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption lower(QStringList() << "l" << "lower",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Use lowercase characters"));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(lower);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption upper(QStringList() << "u" << "upper",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Use uppercase characters"));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(upper);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption numeric(QStringList() << "n" << "numeric",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Use numbers."));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(numeric);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption special(QStringList() << "s" << "special",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Use special characters"));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(special);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption extended(QStringList() << "e" << "extended",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Use extended ASCII"));
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.addOption(extended);
|
2018-09-29 19:00:47 +02:00
|
|
|
QCommandLineOption exclude(QStringList() << "x" << "exclude",
|
2018-06-11 05:37:09 +03:00
|
|
|
QObject::tr("Exclude character set"),
|
|
|
|
QObject::tr("chars"));
|
|
|
|
parser.addOption(exclude);
|
|
|
|
QCommandLineOption exclude_similar(QStringList() << "exclude-similar",
|
|
|
|
QObject::tr("Exclude similar looking characters"));
|
|
|
|
parser.addOption(exclude_similar);
|
|
|
|
QCommandLineOption every_group(QStringList() << "every-group",
|
|
|
|
QObject::tr("Include characters from every selected group"));
|
|
|
|
parser.addOption(every_group);
|
2018-09-29 19:00:47 +02:00
|
|
|
parser.addHelpOption();
|
2018-01-19 15:48:40 +01:00
|
|
|
parser.process(arguments);
|
|
|
|
|
|
|
|
const QStringList args = parser.positionalArguments();
|
2018-02-06 01:17:36 +01:00
|
|
|
if (!args.isEmpty()) {
|
2018-09-29 19:00:47 +02:00
|
|
|
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
|
2018-01-19 15:48:40 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PasswordGenerator passwordGenerator;
|
|
|
|
|
2018-01-21 02:32:46 +01:00
|
|
|
if (parser.value(len).isEmpty()) {
|
|
|
|
passwordGenerator.setLength(PasswordGenerator::DefaultLength);
|
|
|
|
} else {
|
|
|
|
int length = parser.value(len).toInt();
|
2018-09-29 19:00:47 +02:00
|
|
|
passwordGenerator.setLength(static_cast<size_t>(length));
|
2018-01-21 02:32:46 +01:00
|
|
|
}
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
PasswordGenerator::CharClasses classes = 0x0;
|
|
|
|
|
|
|
|
if (parser.isSet(lower)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
classes |= PasswordGenerator::LowerLetters;
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
if (parser.isSet(upper)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
classes |= PasswordGenerator::UpperLetters;
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
if (parser.isSet(numeric)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
classes |= PasswordGenerator::Numbers;
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
if (parser.isSet(special)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
classes |= PasswordGenerator::SpecialCharacters;
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
if (parser.isSet(extended)) {
|
2018-03-31 16:01:30 -04:00
|
|
|
classes |= PasswordGenerator::EASCII;
|
2018-01-19 15:48:40 +01:00
|
|
|
}
|
|
|
|
|
2018-06-11 05:37:09 +03:00
|
|
|
PasswordGenerator::GeneratorFlags flags = 0x0;
|
|
|
|
|
|
|
|
if (parser.isSet(exclude_similar)) {
|
|
|
|
flags |= PasswordGenerator::ExcludeLookAlike;
|
|
|
|
}
|
|
|
|
if (parser.isSet(every_group)) {
|
|
|
|
flags |= PasswordGenerator::CharFromEveryGroup;
|
|
|
|
}
|
|
|
|
|
2018-01-22 13:47:20 +01:00
|
|
|
passwordGenerator.setCharClasses(classes);
|
2018-06-11 05:37:09 +03:00
|
|
|
passwordGenerator.setFlags(flags);
|
|
|
|
passwordGenerator.setExcludedChars(parser.value(exclude));
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
if (!passwordGenerator.isValid()) {
|
2018-09-29 19:00:47 +02:00
|
|
|
out << parser.helpText().replace("keepassxc-cli", "keepassxc-cli generate");
|
2018-01-19 15:48:40 +01:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString password = passwordGenerator.generatePassword();
|
2018-09-29 19:00:47 +02:00
|
|
|
out << password << endl;
|
2018-01-19 15:48:40 +01:00
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|