summaryrefslogtreecommitdiff
path: root/src/tools/packager/main.cpp
blob: ce1b83d7dbf5544c190219a586f0a4d6f67714c9 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/****************************************************************************
**
** Copyright (C) 2017 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QCoreApplication>
#include <QCommandLineParser>
#include <QStringList>
#include <QDebug>

#include <stdio.h>

#include <QtAppManCommon/utilities.h>
#include "packager.h"

QT_USE_NAMESPACE_AM

enum Command {
    NoCommand,
    CreatePackage,
    DevSignPackage,
    DevVerifyPackage,
    StoreSignPackage,
    StoreVerifyPackage,
};

static struct {
    Command command;
    const char *name;
    const char *description;
} commandTable[] = {
    { CreatePackage,      "create-package",       "Create a new package." },
    { DevSignPackage,     "dev-sign-package",     "Add developer signature to package." },
    { DevVerifyPackage,   "dev-verify-package",   "Verify developer signature on package." },
    { StoreSignPackage,   "store-sign-package",   "Add store signature to package." },
    { StoreVerifyPackage, "store-verify-package", "Verify store signature on package." }
};

static Command command(QCommandLineParser &clp)
{
    if (!clp.positionalArguments().isEmpty()) {
        QByteArray cmd = clp.positionalArguments().at(0).toLatin1();

        for (uint i = 0; i < sizeof(commandTable) / sizeof(commandTable[0]); ++i) {
            if (cmd == commandTable[i].name) {
                clp.clearPositionalArguments();
                clp.addPositionalArgument(cmd, commandTable[i].description, cmd);
                return commandTable[i].command;
            }
        }
    }
    return NoCommand;
}

int main(int argc, char *argv[])
{
    ensureCorrectLocale();

    QCoreApplication::setApplicationName(qSL("ApplicationManager Packager"));
    QCoreApplication::setOrganizationName(qSL("Pelagicore AG"));
    QCoreApplication::setOrganizationDomain(qSL("pelagicore.com"));
    QCoreApplication::setApplicationVersion(qSL(AM_VERSION));

    QCoreApplication a(argc, argv);

    if (!checkCorrectLocale()) {
        fprintf(stderr, "ERROR: the packager needs a UTF-8 locale to work correctly:\n"
                        "       even automatically switching to C.UTF-8 or en_US.UTF-8 failed.\n");
        exit(2);
    }

    QString desc = qSL("\nPelagicore ApplicationManager packaging tool\n\nAvailable commands are:\n");
    uint longestName = 0;
    for (uint i = 0; i < sizeof(commandTable) / sizeof(commandTable[0]); ++i)
        longestName = qMax(longestName, qstrlen(commandTable[i].name));
    for (uint i = 0; i < sizeof(commandTable) / sizeof(commandTable[0]); ++i) {
        desc += qSL("  %1%2  %3\n")
                .arg(qL1S(commandTable[i].name),
                     QString(longestName - qstrlen(commandTable[i].name), qL1C(' ')),
                     qL1S(commandTable[i].description));
    }

    desc += qSL("\nMore information about each command can be obtained by running\n  application-packager <command> --help");

    QCommandLineParser clp;
    clp.setApplicationDescription(desc);

    clp.addHelpOption();
    clp.addVersionOption();

    clp.addPositionalArgument(qSL("command"), qSL("The command to execute."));

    if (!clp.parse(QCoreApplication::arguments())) {
        fprintf(stderr, "%s\n", qPrintable(clp.errorText()));
        exit(1);
    }

    Packager *p = nullptr;

    switch (command(clp)) {
    default:
    case NoCommand:
        if (clp.isSet(qSL("version")))
            clp.showVersion();
        if (clp.isSet(qSL("help")))
            clp.showHelp();
        clp.showHelp(1);
        break;

    case CreatePackage:
        clp.addPositionalArgument(qSL("package"),          qSL("The file name of the created package."));
        clp.addPositionalArgument(qSL("source-directory"), qSL("The package's content root directory."));
        clp.process(a);

        if (clp.positionalArguments().size() != 3)
            clp.showHelp(1);

        p = Packager::create(clp.positionalArguments().at(1),
                             clp.positionalArguments().at(2));
        break;

    case DevSignPackage:
        clp.addPositionalArgument(qSL("package"),        qSL("File name of the unsigned package (input)."));
        clp.addPositionalArgument(qSL("signed-package"), qSL("File name of the signed package (output)."));
        clp.addPositionalArgument(qSL("certificate"),    qSL("PKCS#12 certificate file."));
        clp.addPositionalArgument(qSL("password"),       qSL("Password for the PKCS#12 certificate."));
        clp.process(a);

        if (clp.positionalArguments().size() != 5)
            clp.showHelp(1);

        p = Packager::developerSign(clp.positionalArguments().at(1),
                                    clp.positionalArguments().at(2),
                                    clp.positionalArguments().at(3),
                                    clp.positionalArguments().at(4));
        break;

    case DevVerifyPackage:
        clp.addPositionalArgument(qSL("package"),      qSL("File name of the signed package (input)."));
        clp.addPositionalArgument(qSL("certificates"), qSL("The developer's CA certificate file(s)."), qSL("certificates..."));
        clp.process(a);

        if (clp.positionalArguments().size() < 3)
            clp.showHelp(1);

        p = Packager::developerVerify(clp.positionalArguments().at(1),
                                      clp.positionalArguments().mid(2));
        break;

    case StoreSignPackage:
        clp.addPositionalArgument(qSL("package"),        qSL("File name of the unsigned package (input)."));
        clp.addPositionalArgument(qSL("signed-package"), qSL("File name of the signed package (output)."));
        clp.addPositionalArgument(qSL("certificate"),    qSL("PKCS#12 certificate file."));
        clp.addPositionalArgument(qSL("password"),       qSL("Password for the PKCS#12 certificate."));
        clp.addPositionalArgument(qSL("hardware-id"),    qSL("Unique hardware id to which this package gets bound."));
        clp.process(a);

        if (clp.positionalArguments().size() != 6)
            clp.showHelp(1);

        p = Packager::storeSign(clp.positionalArguments().at(1),
                                clp.positionalArguments().at(2),
                                clp.positionalArguments().at(3),
                                clp.positionalArguments().at(4),
                                clp.positionalArguments().at(5));
        break;

    case StoreVerifyPackage:
        clp.addPositionalArgument(qSL("package"),      qSL("File name of the signed package (input)."));
        clp.addPositionalArgument(qSL("certificates"), qSL("Store CA certificate file(s)."), qSL("certificates..."));
        clp.addPositionalArgument(qSL("hardware-id"),  qSL("Unique hardware id to which this package was bound."));
        clp.process(a);

        if (clp.positionalArguments().size() < 4)
            clp.showHelp(1);

        p = Packager::storeVerify(clp.positionalArguments().at(1),
                                  clp.positionalArguments().mid(2, clp.positionalArguments().size() - 2),
                                  *--clp.positionalArguments().cend());
        break;
    }

    if (!p)
        return 2;
    try {
        p->execute();
        if (!p->output().isEmpty())
            fprintf(stdout, "%s\n", qPrintable(p->output()));
        return p->resultCode();
    } catch (const Exception &e) {
        fprintf(stderr, "ERROR: %s\n", qPrintable(e.errorString()));
        return 1;
    }
}