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

Generate.cpp « cli « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc4add242f980aa455b5fbb822596e51edad97c0 (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
/*
 *  Copyright (C) 2019 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 "Generate.h"

#include "cli/TextStream.h"
#include "cli/Utils.h"

const QCommandLineOption Generate::PasswordLengthOption =
    QCommandLineOption(QStringList() << "L"
                                     << "length",
                       QObject::tr("Length of the generated password"),
                       QObject::tr("length"));

const QCommandLineOption Generate::LowerCaseOption = QCommandLineOption(QStringList() << "l"
                                                                                      << "lower",
                                                                        QObject::tr("Use lowercase characters"));

const QCommandLineOption Generate::UpperCaseOption = QCommandLineOption(QStringList() << "U"
                                                                                      << "upper",
                                                                        QObject::tr("Use uppercase characters"));

const QCommandLineOption Generate::NumbersOption = QCommandLineOption(QStringList() << "n"
                                                                                    << "numeric",
                                                                      QObject::tr("Use numbers"));

const QCommandLineOption Generate::SpecialCharsOption = QCommandLineOption(QStringList() << "s"
                                                                                         << "special",
                                                                           QObject::tr("Use special characters"));

const QCommandLineOption Generate::ExtendedAsciiOption = QCommandLineOption(QStringList() << "e"
                                                                                          << "extended",
                                                                            QObject::tr("Use extended ASCII"));

const QCommandLineOption Generate::ExcludeCharsOption = QCommandLineOption(QStringList() << "x"
                                                                                         << "exclude",
                                                                           QObject::tr("Exclude character set"),
                                                                           QObject::tr("chars"));

const QCommandLineOption Generate::ExcludeSimilarCharsOption =
    QCommandLineOption(QStringList() << "exclude-similar", QObject::tr("Exclude similar looking characters"));

const QCommandLineOption Generate::IncludeEveryGroupOption =
    QCommandLineOption(QStringList() << "every-group", QObject::tr("Include characters from every selected group"));
Generate::Generate()
{
    name = QString("generate");
    description = QObject::tr("Generate a new random password.");
    options.append(Generate::PasswordLengthOption);
    options.append(Generate::LowerCaseOption);
    options.append(Generate::UpperCaseOption);
    options.append(Generate::NumbersOption);
    options.append(Generate::SpecialCharsOption);
    options.append(Generate::ExtendedAsciiOption);
    options.append(Generate::ExcludeCharsOption);
    options.append(Generate::ExcludeSimilarCharsOption);
    options.append(Generate::IncludeEveryGroupOption);
}

/**
 * Creates a password generator instance using the command line options
 * of the parser object.
 */
QSharedPointer<PasswordGenerator> Generate::createGenerator(QSharedPointer<QCommandLineParser> parser)
{
    TextStream errorTextStream(Utils::STDERR, QIODevice::WriteOnly);
    QSharedPointer<PasswordGenerator> passwordGenerator = QSharedPointer<PasswordGenerator>(new PasswordGenerator());
    QString passwordLength = parser->value(Generate::PasswordLengthOption);
    if (passwordLength.isEmpty()) {
        passwordGenerator->setLength(PasswordGenerator::DefaultLength);
    } else if (passwordLength.toInt() <= 0) {
        errorTextStream << QObject::tr("Invalid password length %1").arg(passwordLength) << endl;
        return QSharedPointer<PasswordGenerator>(nullptr);
    } else {
        passwordGenerator->setLength(passwordLength.toInt());
    }

    PasswordGenerator::CharClasses classes = 0x0;

    if (parser->isSet(Generate::LowerCaseOption)) {
        classes |= PasswordGenerator::LowerLetters;
    }
    if (parser->isSet(Generate::UpperCaseOption)) {
        classes |= PasswordGenerator::UpperLetters;
    }
    if (parser->isSet(Generate::NumbersOption)) {
        classes |= PasswordGenerator::Numbers;
    }
    if (parser->isSet(Generate::SpecialCharsOption)) {
        classes |= PasswordGenerator::SpecialCharacters;
    }
    if (parser->isSet(Generate::ExtendedAsciiOption)) {
        classes |= PasswordGenerator::EASCII;
    }

    PasswordGenerator::GeneratorFlags flags = 0x0;

    if (parser->isSet(Generate::ExcludeSimilarCharsOption)) {
        flags |= PasswordGenerator::ExcludeLookAlike;
    }
    if (parser->isSet(Generate::IncludeEveryGroupOption)) {
        flags |= PasswordGenerator::CharFromEveryGroup;
    }

    // The default charset will be used if no explicit class
    // option was set.
    passwordGenerator->setCharClasses(classes);
    passwordGenerator->setFlags(flags);
    passwordGenerator->setExcludedChars(parser->value(Generate::ExcludeCharsOption));

    if (!passwordGenerator->isValid()) {
        errorTextStream << QObject::tr("Invalid password generator after applying all options") << endl;
        return QSharedPointer<PasswordGenerator>(nullptr);
    }

    return passwordGenerator;
}

int Generate::execute(const QStringList& arguments)
{
    QSharedPointer<QCommandLineParser> parser = getCommandLineParser(arguments);
    if (parser.isNull()) {
        return EXIT_FAILURE;
    }

    QSharedPointer<PasswordGenerator> passwordGenerator = Generate::createGenerator(parser);
    if (passwordGenerator.isNull()) {
        return EXIT_FAILURE;
    }

    TextStream outputTextStream(Utils::STDOUT, QIODevice::WriteOnly);
    QString password = passwordGenerator->generatePassword();
    outputTextStream << password << endl;

    return EXIT_SUCCESS;
}