aboutsummaryrefslogtreecommitdiffstats
path: root/include/licenser.h
blob: b14d844269ebc0179e187530bdef5a7be2ed4b25 (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
// Copyright (C) 2022 The Qt Company Ltd.
//

#pragma once

#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <ctime>
#include <cassert>
#include <unistd.h>

#include "commonsetup.h"
#include "httpclient.h"
#include "tcpserver.h"
#include "utils.h"
#include "../hmac_sha256/hmac_sha256.h"
#include "jsonhandler.h"

#define WORKING_DIR             "/opt/licd"
#define DAEMON_SETTINGS_FILE    WORKING_DIR"/licd.ini"
#define LICENSE_FILE_BASE       WORKING_DIR"/lic"
#define SHA256_HASH_SIZE        32

enum class RequestReply {
    bad_request      = -1,
    license_granted  = 0,
    license_rejected = 1,
    bad_connection   = 2
};

enum class RequestType {
    no_request              = 0,
    normal_License_renewal  = 1,
    long_term_request       = 2,
    init_user_settings      = 3
};

// Struct to store incoming request info for easier handling
struct RequestInfo {
    RequestType type = RequestType::no_request;
    std::string operation = "";
    std::string appName = "";
    std::string appVersion = "";
    std::string userId = "";
    std::string licenseId = "";
    std::string email = "";
    std::string licenseFile = "";
    std::string userHome = "";
    std::string payload = "";
    std::string serverAddr = "";
};

class Licenser  {
public:
    explicit Licenser(uint16_t tcpPort=0);
    ~Licenser() {}
    int listen();
    int sendLicensingRequest(std::string &reply, const RequestInfo &request);
    int sendLongTermRequest(std::string &reply, const RequestInfo &request);
    void sendResponseToCLient(uint16_t clientId, std::string response);

private:
    HttpClient *m_http;
    TcpServer *m_server;
    uint64_t m_mocInterval;
    std::string m_infoString;

    int parseIncoming(const std::string &incoming, RequestInfo &request);
    int buildRequestJson(RequestInfo &request);
    int parseAndSaveJsonReply(std::string &reply, const RequestInfo &request);
    bool checkLicenseExpiryDate(const std::string &licenseFile);
    bool isMocRenewalDue(const std::string &licenseFile);
    void doHmacHashSha256(const std::string &payload, const std::string &secret, std::string &authKey);
    int initDaemonSettings();


    // Default settings for the daemon. Will be overridden by content of the settings file,
    // see 'qtlicenser.conf'
    std::map<std::string, std::string> settings{
        {"server_addr", "10.0.128.123:8080"},
        {"server_secret", "9llVmlSjW7RW7bcdDeiQ0JyfMRDlWcEQviC8q7BS"},
        {"reservation_access_point", "/api/v2/reservations"},
        {"long-term_access_point", "/api/v2/reservation/long-term"},
        {"moc_renewal_interval", "24"},
        {"license_file_base", LICENSE_FILE_BASE},
        {"hw_id", "c31aa37762ef3bb84e09eeea0bc9bccb6b96a81b2657b1a356ec51709975a5ea"},
        {"tcp_listening_port", "60000"}
    };

    std::map<RequestReply, std::string> replyString{
        {RequestReply::bad_request, "ERROR Bad request"},
        {RequestReply::license_granted, "License acquired."},
        {RequestReply::license_rejected, "No valid license acquired"},
        {RequestReply::bad_connection, "No connection to server. Try again later."}
    };

};