summaryrefslogtreecommitdiffstats
path: root/src/rnp/rnpcfg.cpp
blob: e1c35a30aeb2ff97d00d419d26150782eac597bd (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
/*
 * Copyright (c) 2017-2020 [Ribose Inc](https://www.ribose.com).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <limits.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#else
#include "uniwin.h"
#endif
#include <sys/stat.h>
#include <time.h>
#include <errno.h>
#include <stdexcept>
#include <inttypes.h>

#include "config.h"
#include "rnpcfg.h"
#include "defaults.h"
#include "utils.h"
#include "time-utils.h"
#include <rnp/rnp.h>

// must be placed after include "utils.h"
#ifndef RNP_USE_STD_REGEX
#include <regex.h>
#else
#include <regex>
#endif

typedef enum rnp_cfg_val_type_t {
    RNP_CFG_VAL_NULL = 0,
    RNP_CFG_VAL_INT = 1,
    RNP_CFG_VAL_BOOL = 2,
    RNP_CFG_VAL_STRING = 3,
    RNP_CFG_VAL_LIST = 4
} rnp_cfg_val_type_t;

class rnp_cfg_val {
    rnp_cfg_val_type_t type_;

  public:
    rnp_cfg_val(rnp_cfg_val_type_t t) : type_(t){};
    rnp_cfg_val_type_t
    type() const
    {
        return type_;
    };

    virtual ~rnp_cfg_val(){};
};

class rnp_cfg_int_val : public rnp_cfg_val {
    int val_;

  public:
    rnp_cfg_int_val(int val) : rnp_cfg_val(RNP_CFG_VAL_INT), val_(val){};
    int
    val() const
    {
        return val_;
    };
};

class rnp_cfg_bool_val : public rnp_cfg_val {
    bool val_;

  public:
    rnp_cfg_bool_val(bool val) : rnp_cfg_val(RNP_CFG_VAL_BOOL), val_(val){};
    bool
    val() const
    {
        return val_;
    };
};

class rnp_cfg_str_val : public rnp_cfg_val {
    std::string val_;

  public:
    rnp_cfg_str_val(const std::string &val) : rnp_cfg_val(RNP_CFG_VAL_STRING), val_(val){};
    const std::string &
    val() const
    {
        return val_;
    };
};

class rnp_cfg_list_val : public rnp_cfg_val {
    std::vector<std::string> val_;

  public:
    rnp_cfg_list_val() : rnp_cfg_val(RNP_CFG_VAL_LIST), val_(){};
    std::vector<std::string> &
    val()
    {
        return val_;
    };
    const std::vector<std::string> &
    val() const
    {
        return val_;
    };
};

void
rnp_cfg::load_defaults()
{
    set_bool(CFG_OVERWRITE, false);
    set_str(CFG_OUTFILE, "");
    set_str(CFG_ZALG, DEFAULT_Z_ALG);
    set_int(CFG_ZLEVEL, DEFAULT_Z_LEVEL);
    set_str(CFG_CIPHER, DEFAULT_SYMM_ALG);
    set_int(CFG_NUMTRIES, MAX_PASSWORD_ATTEMPTS);
    set_int(CFG_S2K_MSEC, DEFAULT_S2K_MSEC);
}

void
rnp_cfg::set_str(const std::string &key, const std::string &val)
{
    unset(key);
    vals_[key] = new rnp_cfg_str_val(val);
}

void
rnp_cfg::set_str(const std::string &key, const char *val)
{
    unset(key);
    vals_[key] = new rnp_cfg_str_val(val);
}

void
rnp_cfg::set_int(const std::string &key, int val)
{
    unset(key);
    vals_[key] = new rnp_cfg_int_val(val);
}

void
rnp_cfg::set_bool(const std::string &key, bool val)
{
    unset(key);
    vals_[key] = new rnp_cfg_bool_val(val);
}

void
rnp_cfg::unset(const std::string &key)
{
    if (!vals_.count(key)) {
        return;
    }
    delete vals_[key];
    vals_.erase(key);
}

void
rnp_cfg::add_str(const std::string &key, const std::string &val)
{
    if (!vals_.count(key)) {
        vals_[key] = new rnp_cfg_list_val();
    }
    if (vals_[key]->type() != RNP_CFG_VAL_LIST) {
        RNP_LOG("expected list val for \"%s\"", key.c_str());
        throw std::invalid_argument("type");
    }
    (dynamic_cast<rnp_cfg_list_val &>(*vals_[key])).val().push_back(val);
}

bool
rnp_cfg::has(const std::string &key) const
{
    return vals_.count(key);
}

const std::string &
rnp_cfg::get_str(const std::string &key) const
{
    if (!has(key) || (vals_.at(key)->type() != RNP_CFG_VAL_STRING)) {
        return empty_str_;
    }
    return (dynamic_cast<const rnp_cfg_str_val &>(*vals_.at(key))).val();
}

const char *
rnp_cfg::get_cstr(const std::string &key) const
{
    if (!has(key) || (vals_.at(key)->type() != RNP_CFG_VAL_STRING)) {
        return NULL;
    }
    return (dynamic_cast<const rnp_cfg_str_val &>(*vals_.at(key))).val().c_str();
}

int
rnp_cfg::get_int(const std::string &key, int def) const
{
    if (!has(key)) {
        return def;
    }
    const rnp_cfg_val *val = vals_.at(key);
    switch (val->type()) {
    case RNP_CFG_VAL_INT:
        return (dynamic_cast<const rnp_cfg_int_val &>(*val)).val();
    case RNP_CFG_VAL_BOOL:
        return (dynamic_cast<const rnp_cfg_bool_val &>(*val)).val();
    case RNP_CFG_VAL_STRING:
        return atoi((dynamic_cast<const rnp_cfg_str_val &>(*val)).val().c_str());
    default:
        return def;
    }
}

bool
rnp_cfg::get_bool(const std::string &key) const
{
    if (!has(key)) {
        return false;
    }
    const rnp_cfg_val *val = vals_.at(key);
    switch (val->type()) {
    case RNP_CFG_VAL_INT:
        return (dynamic_cast<const rnp_cfg_int_val &>(*val)).val();
    case RNP_CFG_VAL_BOOL:
        return (dynamic_cast<const rnp_cfg_bool_val &>(*val)).val();
    case RNP_CFG_VAL_STRING: {
        const std::string &str = (dynamic_cast<const rnp_cfg_str_val &>(*val)).val();
        return !strcasecmp(str.c_str(), "true") || (atoi(str.c_str()) > 0);
    }
    default:
        return false;
    }
}

size_t
rnp_cfg::get_count(const std::string &key) const
{
    if (!has(key) || (vals_.at(key)->type() != RNP_CFG_VAL_LIST)) {
        return 0;
    }
    return (dynamic_cast<const rnp_cfg_list_val &>(*vals_.at(key))).val().size();
}

const std::string &
rnp_cfg::get_str(const std::string &key, size_t idx) const
{
    if (get_count(key) <= idx) {
        RNP_LOG("idx is out of bounds for \"%s\"", key.c_str());
        throw std::invalid_argument("idx");
    }
    return (dynamic_cast<const rnp_cfg_list_val &>(*vals_.at(key))).val().at(idx);
}

std::vector<std::string>
rnp_cfg::get_list(const std::string &key) const
{
    if (!has(key)) {
        /* it's okay to return empty list */
        return std::vector<std::string>();
    }
    if (vals_.at(key)->type() != RNP_CFG_VAL_LIST) {
        RNP_LOG("no list at the key \"%s\"", key.c_str());
        throw std::invalid_argument("key");
    }
    return (dynamic_cast<const rnp_cfg_list_val &>(*vals_.at(key))).val();
}

int
rnp_cfg::get_pswdtries() const
{
    const std::string &numtries = get_str(CFG_NUMTRIES);
    int                num = atoi(numtries.c_str());
    if (numtries.empty() || (num <= 0)) {
        return MAX_PASSWORD_ATTEMPTS;
    } else if (numtries == "unlimited") {
        return INFINITE_ATTEMPTS;
    }
    return num;
}

const std::string
rnp_cfg::get_hashalg() const
{
    const std::string hash_alg = get_str(CFG_HASH);
    if (!hash_alg.empty()) {
        return hash_alg;
    }
    return DEFAULT_HASH_ALG;
}

bool
rnp_cfg::get_expiration(const std::string &key, uint32_t &seconds) const
{
    if (!has(key)) {
        return false;
    }
    const std::string &val = get_str(key);
    uint64_t           delta;
    uint64_t           t;
    if (parse_date(val, t)) {
        uint64_t now = time();
        if (t > now) {
            delta = t - now;
            if (delta > UINT32_MAX) {
                RNP_LOG("Expiration time exceeds 32-bit value");
                return false;
            }
            seconds = delta;
            return true;
        }
        return false;
    }
    const char *reg = "^([0-9]+)([hdwmy]?)$";
#ifndef RNP_USE_STD_REGEX
    static regex_t r;
    static int     compiled;
    regmatch_t     matches[3];

    if (!compiled) {
        compiled = 1;
        if (regcomp(&r, reg, REG_EXTENDED | REG_ICASE)) {
            RNP_LOG("failed to compile regexp");
            return false;
        }
    }
    if (regexec(&r, val.c_str(), ARRAY_SIZE(matches), matches, 0)) {
        return false;
    }
    auto delta_str = &val.c_str()[matches[1].rm_so];
    char mult = val.c_str()[matches[2].rm_so];
#else
    static std::regex re(reg, std::regex_constants::extended | std::regex_constants::icase);
    std::smatch       result;

    if (!std::regex_search(val, result, re)) {
        return false;
    }
    std::string delta_stdstr = result[1].str();
    const char *delta_str = delta_stdstr.c_str();
    char        mult = result[2].str()[0];
#endif
    errno = 0;
    delta = strtoul(delta_str, NULL, 10);
    if (errno || delta > UINT_MAX) {
        RNP_LOG("Invalid expiration '%s'.", delta_str);
        return false;
    }
    switch (std::tolower(mult)) {
    case 'h':
        delta *= 60 * 60;
        break;
    case 'd':
        delta *= 60 * 60 * 24;
        break;
    case 'w':
        delta *= 60 * 60 * 24 * 7;
        break;
    case 'm':
        delta *= 60 * 60 * 24 * 31;
        break;
    case 'y':
        delta *= 60 * 60 * 24 * 365;
        break;
    }
    if (delta > UINT32_MAX) {
        RNP_LOG("Expiration value exceed 32 bit.");
        return false;
    }
    seconds = delta;
    return true;
}

bool
rnp_cfg::extract_timestamp(const std::string &st, uint64_t &t) const
{
    if (st.empty()) {
        return false;
    }
    if (parse_date(st, t)) {
        return true;
    }
    /* Check if string is UNIX timestamp */
    for (auto c : st) {
        if (!isdigit(c)) {
            return false;
        }
    }
    t = std::stoll(st);
    return true;
}

uint64_t
rnp_cfg::get_sig_creation() const
{
    uint64_t t = 0;
    if (extract_timestamp(get_str(CFG_CREATION), t)) {
        return t;
    }
    return time();
}

uint64_t
rnp_cfg::time() const
{
    uint64_t t = 0;
    if (extract_timestamp(get_str(CFG_CURTIME), t)) {
        return t;
    }
    return ::time(NULL);
}

void
rnp_cfg::copy(const rnp_cfg &src)
{
    for (const auto &it : src.vals_) {
        if (has(it.first)) {
            unset(it.first);
        }
        rnp_cfg_val *val = NULL;
        switch (it.second->type()) {
        case RNP_CFG_VAL_INT:
            val = new rnp_cfg_int_val(dynamic_cast<const rnp_cfg_int_val &>(*it.second));
            break;
        case RNP_CFG_VAL_BOOL:
            val = new rnp_cfg_bool_val(dynamic_cast<const rnp_cfg_bool_val &>(*it.second));
            break;
        case RNP_CFG_VAL_STRING:
            val = new rnp_cfg_str_val(dynamic_cast<const rnp_cfg_str_val &>(*it.second));
            break;
        case RNP_CFG_VAL_LIST:
            val = new rnp_cfg_list_val(dynamic_cast<const rnp_cfg_list_val &>(*it.second));
            break;
        default:
            continue;
        }
        vals_[it.first] = val;
    }
}

void
rnp_cfg::clear()
{
    for (const auto &it : vals_) {
        delete it.second;
    }
    vals_.clear();
}

rnp_cfg::~rnp_cfg()
{
    clear();
}

/**
 * @brief Get number of days in month.
 *
 * @param year number of year, i.e. 2021
 * @param month number of month, 1..12
 * @return number of days (28..31) or 0 if month is wrong.
 */
static int
days_in_month(int year, int month)
{
    switch (month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        return 31;
    case 4:
    case 6:
    case 9:
    case 11:
        return 30;
    case 2: {
        bool leap_year = !(year % 400) || (!(year % 4) && (year % 100));
        return leap_year ? 29 : 28;
    }
    default:
        return 0;
    }
}

bool
rnp_cfg::parse_date(const std::string &s, uint64_t &t) const
{
    /* fill time zone information */
    struct tm tm;
    rnp_localtime(::time(NULL), tm);
    tm.tm_hour = 0;
    tm.tm_min = 0;
    tm.tm_sec = 0;
    const char *reg = "^([0-9]{4})[-/\\.]([0-9]{2})[-/\\.]([0-9]{2})$";
#ifndef RNP_USE_STD_REGEX
    static regex_t r;
    static int     compiled;

    if (!compiled) {
        compiled = 1;
        if (regcomp(&r, reg, REG_EXTENDED)) {
            RNP_LOG("failed to compile regexp");
            return false;
        }
    }
    regmatch_t matches[4];
    if (regexec(&r, s.c_str(), ARRAY_SIZE(matches), matches, 0)) {
        return false;
    }
    int year = strtol(&s[matches[1].rm_so], NULL, 10);
    int mon = strtol(&s[matches[2].rm_so], NULL, 10);
    int mday = strtol(&s[matches[3].rm_so], NULL, 10);
#else
    static std::regex re(reg, std::regex_constants::extended);
    std::smatch       result;

    if (!std::regex_search(s, result, re)) {
        return false;
    }
    int year = std::stoi(result[1].str());
    int mon = std::stoi(result[2].str());
    int mday = std::stoi(result[3].str());
#endif
    if (year < 1970 || mon < 1 || mon > 12 || !mday || (mday > days_in_month(year, mon))) {
        RNP_LOG("invalid date: %s.", s.c_str());
        return false;
    }
    tm.tm_year = year - 1900;
    tm.tm_mon = mon - 1;
    tm.tm_mday = mday;
    /* line below is required to correctly handle DST changes */
    tm.tm_isdst = -1;

    struct tm check_tm = tm;
    time_t    built_time = rnp_mktime(&tm);
    time_t    check_time = mktime(&check_tm);
    if (built_time != check_time) {
        /* If date is beyond of yk2038 and we have 32-bit signed time_t, we need to reduce
         * timestamp */
        RNP_LOG("Warning: date %s is beyond of 32-bit time_t, so timestamp was reduced to "
                "maximum supported value.",
                s.c_str());
    }
    t = built_time;
    return true;
}