aboutsummaryrefslogtreecommitdiffstats
path: root/qtlicensetool/qtlicensetool.cpp
blob: 75f0343f19051e3be5a7eddb9f49bf8182860a77 (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
/* Copyright (C) 2022 The Qt Company Ltd.
 *
 * SPDX-License-Identifier: GPL-3.0-only WITH Qt-GPL-exception-1.0
*/

#include "qtlicensetool.h"

int main(int argc, char *argv[])
{
    // Fetch the settings from .ini file
    LicdSetup setup(e_set_type_licensetool, "");
    if (setup.initSettings() == e_file_not_present) {
        errorAndExit("Not able to find the settings");
    }
    if (argc < 2) {
        errorAndExit("No arguments given");
    }
    std::string action = argv[1];
    // Get rid of commands which don't require further actions
    if (action == "--version" || action == "-v") {
        showVersion();
        return 0;
    } else if (action == "--help" || action == "-h") {
        helpAndExit();
    }

    // Then those requiring connection
    if (action == "--longterm" || action == "-L") {
        // Override setup with cmd-line args:
        overrideSetup(&setup, argc, argv);
        doLongtermRequest(&setup);
    } else if (action == "--serverversion" || action == "-S") {
        askStatus(SERVER_VERSION_CMD, &setup);
    } else if (action == "--daemonversion" || action == "-D") {
        askStatus(DAEMON_VERSION_CMD, &setup);
    } else if (action == "--reservation" || action == "-r") {
        askStatus(RESERVATION_QUERY_CMD, &setup);
    } else {
        errorAndExit("Invalid parameters");
    }
}

void showVersion()
{
    std::cout << "qtlicensetool v" << QTLICENSETOOL_VERSION
                << " - CLI tool for Qt Licensing\n";
    return;
}

int askStatus(const std::string &statusRequest, LicdSetup *setup)
{
    std::string req = statusRequest + " -a ";
    req += QTLICENSETOOL_APP_NAME;
    req += " -v ";
    req += QTLICENSETOOL_VERSION;
    std::string daemonAddr = setup->get("licd_addr");
    int daemonPort = utils::strToInt(setup->get("licd_port"));
    if (daemonPort < 0) {
        errorAndExit("Invalid daemon port - check settings/arguments");
    }

    // Connect and send/receive the request
    TcpClient tcp(daemonAddr, daemonPort);
    std::string reply;
    int result = tcp.sendAndReceive(req, reply);
    if (result != e_tcp_success) {
        std::cout << tcp.errorString(result) << std::endl;
        return e_tcp_error_conn;
    }
    std::cout << reply << std::endl;
    return 0;
}

void overrideSetup(LicdSetup *setup, int argc, char *argv[])
{
    // Pick up the cmd-line switches: Start looping args from index 1,
    // as first one is the command itself
    bool longterm = false;
    for (int i = 1; i < argc; i++) {
        std::string sw = utils::trimStr(argv[i]);
        std::string val = "";
        if (sw == "-L" || sw == "--longterm") {
            longterm = true;
            continue;
        }
        if (longterm) {
            // Longterm operation (add|remove) has to come right after the -L switch, not later
            if (sw == OP_ADD_RESERVATION || sw == OP_REMOVE_RESERVATION) {
                setup->set("operation", sw);
                longterm = false;
                continue;
            }
            else {
                errorAndExit("Invalid longterm operation");
            }
        }

        // Check if given switch has a value
        if (sw[0] == '-' && argc > i+1) {
            val = utils::trimStr(argv[i+1]);
            if (val[0] == '-') { // do not accept another switch as a value
                val = "";
            } else {
                i++;
            }
        }
        if (val.empty()) {
            std::string error = "No value for argument " + sw;
            errorAndExit(error);
        }
        // Pick up values
        if (sw == "-i") {
            setup->set("license_id", val);
        } else if (sw == "-u") {
            setup->set("user_id", val);
        } else if (sw == "-d") {
            size_t colPos = val.find(':');
            if (colPos == std::string::npos) {
                errorAndExit("Invalid service address: Must be in form <url>:<port>");
            }
            setup->set("licd_addr", val.substr(0, colPos));
            setup->set("licd_port", val.substr(colPos+1));
        } else if (sw == "-l") {
            size_t colPos = val.find(':');
            if (colPos == std::string::npos) {
                errorAndExit("Invalid license server address: Must be in form <url>:<port>");
            }
            setup->set("license_server_addr", val.substr(0, colPos));
            setup->set("license_server_port", val.substr(colPos + 1));

        } else {
            std::string error = "Invalid argument:" + sw;
            errorAndExit(error);
        }
    }
}

int doLongtermRequest(LicdSetup *setup)
{
    std::string operation = setup->get("operation");
    if (operation != OP_ADD_RESERVATION && operation != OP_REMOVE_RESERVATION) {
        std::string error = "Invalid operation \"" + operation;
        error += "\"";
            errorAndExit(error);
    }
    uint16_t daemonPort = utils::strToInt(setup->get("licd_port"));
    if (daemonPort < 0) {
        errorAndExit("Invalid daemon port - check settings/arguments");
    }

    // Ready to build the request
    std::stringstream request;
    request << LONGTERM_REQUEST_CMD << " " << setup->get("operation")
            << " -u " << setup->get("user_id")
            << " -i " << setup->get("license_id")
            << " -e " << setup->get("user_email")
            << " -a " << QTLICENSETOOL_APP_NAME
            << " -v " << QTLICENSETOOL_VERSION;
    if (!setup->get("license_server_addr").empty()) {
        // Add optional license server URL if specified
        request << " -l " << setup->get("license_server_addr")
                << ":" << setup->get("license_server_port");
    }

    // Connect and send/receive the request
    TcpClient tcp(setup->get("licd_addr"), daemonPort);
    std::string reply;
    int result = tcp.sendAndReceive(request.str(), reply);
    if (result != e_tcp_success) {
        std::cout << tcp.errorString(result) << std::endl;
        exit(EXIT_FAILURE);
    }
    if (reply.find("No") != std::string::npos
            || reply.find("ERROR") != std::string::npos
            || reply.find("expired") != std::string::npos) {
        errorAndExit(reply);
    }
    std::cout << reply << std::endl;
    exit(EXIT_SUCCESS);
}

void errorAndExit(const std::string &reason)
{
    if (!reason.empty()) {
        std::cout << "ERROR! " << reason << std::endl;
    }
    exit(EXIT_FAILURE);
}

void helpAndExit()
{
    std::stringstream ss;
    std::cout << "Usage:\n"
        << "     qtlicensetool <Action> [params]\n"
        << "   Where 'Action' is:\n"
        << "     -h or --help                : Prints out the tool's help text\n"
        << "     -v or --version             : Prints out qtlicensetool version\n"
        << "     -S or --serverversion       : Prints out License Server version\n"
        << "     -D or --daemonversion       : Prints out the License Service (daemon) version\n"
        << "     -r or --reservation         : Prints out the list of active reservations\n"
        << "     -L or --longterm            : Longterm reservation (add or remove)\n"
        << "   For longterm usage ('-L' or '--longterm'), you must tell which operation to do:\n"
        << "     add|remove  <--- Either one needs to be there\n"
        << "   Optionat parameters which override [default| section settings:\n"
        << "     -d = Full license daemon address and port separated by ':'\n"
        << "     -u = Your Qt license username\n"
        << "     -i = Your Qt license ID number (must be an int)\n"
        << "     -l = License server address and port  separated by ':'\n"
        << "   Example:\n"
        << "     > qtlicensetool --longterm add          : Make a long-term license reservation\n"
        << "     > qtlicensetool -L add -i 462412        : Same as above, but using different license ID\n"
        << "     > qtlicensetool --daemonversion         : Prints out a daemon version number\n";
    exit(EXIT_SUCCESS);
}