summaryrefslogtreecommitdiffstats
path: root/daemons/ipa-slapi-plugins/libotp/libotp.c
blob: 3fb298035e86dca7b42b62a328410b94332ea4c6 (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
/** BEGIN COPYRIGHT BLOCK
 * 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 3 of the License, or
 * (at your option) any later version.
 *
 * 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/>.
 *
 * Additional permission under GPLv3 section 7:
 *
 * In the following paragraph, "GPL" means the GNU General Public
 * License, version 3 or any later version, and "Non-GPL Code" means
 * code that is governed neither by the GPL nor a license
 * compatible with the GPL.
 *
 * You may link the code of this Program with Non-GPL Code and convey
 * linked combinations including the two, provided that such Non-GPL
 * Code only links to the code of this Program through those well
 * defined interfaces identified in the file named EXCEPTION found in
 * the source code files (the "Approved Interfaces"). The files of
 * Non-GPL Code may instantiate templates or use macros or inline
 * functions from the Approved Interfaces without causing the resulting
 * work to be covered by the GPL. Only the copyright holders of this
 * Program may make changes or additions to the list of Approved
 * Interfaces.
 *
 * Authors:
 * Nathaniel McCallum <npmccallum@redhat.com>
 *
 * Copyright (C) 2013 Red Hat, Inc.
 * All rights reserved.
 * END COPYRIGHT BLOCK **/

#include "libotp.h"
#include "librfc.h"

#include <time.h>
#include <errno.h>

#define TOKEN(s) "ipaToken" s
#define O(s) TOKEN("OTP" s)
#define T(s) TOKEN("TOTP" s)

#define IPA_OTP_DEFAULT_TOKEN_STEP 30
#define IPA_OTP_OBJCLS_FILTER "(objectClass=ipaTokenTOTP)"


enum otptoken_type {
    OTPTOKEN_NONE = 0,
    OTPTOKEN_TOTP,
};

struct otptoken {
    Slapi_ComponentId *plugin_id;
    Slapi_DN *sdn;
    struct hotp_token token;
    enum otptoken_type type;
    struct {
        unsigned int step;
        int offset;
    } totp;
};

static const char *get_basedn(Slapi_DN *dn)
{
    Slapi_DN *suffix = NULL;
    void *node = NULL;

    for (suffix = slapi_get_first_suffix(&node, 0);
         suffix != NULL;
         suffix = slapi_get_next_suffix(&node, 0)) {
        if (slapi_sdn_issuffix(dn, suffix))
            return (char *) slapi_sdn_get_dn(suffix);
    }

    return NULL;
}

static inline bool is_algo_valid(const char *algo)
{
    static const char *valid_algos[] = { "sha1", "sha256", "sha384",
                                         "sha512", NULL };
    int i, ret;

    for (i = 0; valid_algos[i]; i++) {
        ret = strcasecmp(algo, valid_algos[i]);
        if (ret == 0)
            return true;
    }

    return false;
}

static const struct berval *entry_attr_get_berval(const Slapi_Entry* e,
                                                  const char *type)
{
    Slapi_Attr* attr = NULL;
    Slapi_Value *v;
    int ret;

    ret = slapi_entry_attr_find(e, type, &attr);
    if (ret != 0 || attr == NULL)
        return NULL;

    ret = slapi_attr_first_value(attr, &v);
    if (ret < 0)
        return NULL;

    return slapi_value_get_berval(v);
}

static bool validate(struct otptoken *token, time_t now, ssize_t step,
                     uint32_t first, const uint32_t *second)
{
    uint32_t tmp;

    switch (token->type) {
    case OTPTOKEN_TOTP:
        step = (now + token->totp.offset) / token->totp.step + step;
        break;
    default:
        return false;
    }

    if (!hotp(&token->token, step, &tmp))
        return false;

    if (first != tmp)
        return false;

    if (second == NULL)
        return true;

    if (!hotp(&token->token, step + 1, &tmp))
        return false;

    return *second == tmp;
}

static bool writeback(struct otptoken *token, ssize_t step, bool sync)
{
    Slapi_Value *svals[] = { NULL, NULL };
    Slapi_PBlock *pb = NULL;
    Slapi_Mods *mods = NULL;
    bool success = false;
    const char *attr;
    int value;
    int ret;

    switch (token->type) {
    case OTPTOKEN_TOTP:
        if (!sync)
            return true;
        attr = T("clockOffset");
        value = token->totp.offset + step * token->totp.step;
        break;
    default:
        return false;
    }

    /* Create the value. */
    svals[0] = slapi_value_new();
    if (slapi_value_set_int(svals[0], value) != 0)
        goto error;

    /* Create the mods. */
    mods = slapi_mods_new();
    slapi_mods_add_mod_values(mods, LDAP_MOD_REPLACE, attr, svals);

    /* Perform the modification. */
    pb = slapi_pblock_new();
    slapi_modify_internal_set_pb(pb, slapi_sdn_get_dn(token->sdn),
                                 slapi_mods_get_ldapmods_byref(mods),
                                 NULL, NULL, token->plugin_id, 0);
    if (slapi_modify_internal_pb(pb) != 0)
        goto error;
    if (slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &ret) != 0)
        goto error;
    if (ret != LDAP_SUCCESS)
        goto error;

    /* Save our modifications to the object. */
    switch (token->type) {
    case OTPTOKEN_TOTP:
        token->totp.offset = value;
        break;
    default:
        break;
    }

    success = true;

error:
    slapi_pblock_destroy(pb);
    slapi_mods_free(&mods);
    return success;
}

static void otptoken_free(struct otptoken *token)
{
    if (token == NULL)
        return;

    slapi_sdn_free(&token->sdn);
    free(token->token.key.bytes);
    slapi_ch_free_string(&token->token.algo);
    free(token);
}

void otptoken_free_array(struct otptoken **tokens)
{
    if (tokens == NULL)
        return;

    for (size_t i = 0; tokens[i] != NULL; i++)
        otptoken_free(tokens[i]);

    free(tokens);
}

static struct otptoken *otptoken_new(Slapi_ComponentId *id, Slapi_Entry *entry)
{
    const struct berval *tmp;
    struct otptoken *token;
    char **vals;

    token = calloc(1, sizeof(struct otptoken));
    if (token == NULL)
        return NULL;
    token->plugin_id = id;

    /* Get the token type. */
    vals = slapi_entry_attr_get_charray(entry, "objectClass");
    if (vals == NULL)
        goto error;
    token->type = OTPTOKEN_NONE;
    for (int i = 0; vals[i] != NULL; i++) {
        if (strcasecmp(vals[i], "ipaTokenTOTP") == 0)
            token->type = OTPTOKEN_TOTP;
    }
    slapi_ch_array_free(vals);
    if (token->type == OTPTOKEN_NONE)
        goto error;

    /* Get SDN. */
    token->sdn = slapi_sdn_dup(slapi_entry_get_sdn(entry));
    if (token->sdn == NULL)
        goto error;

    /* Get key. */
    tmp = entry_attr_get_berval(entry, O("key"));
    if (tmp == NULL)
        goto error;
    token->token.key.len = tmp->bv_len;
    token->token.key.bytes = malloc(token->token.key.len);
    if (token->token.key.bytes == NULL)
        goto error;
    memcpy(token->token.key.bytes, tmp->bv_val, token->token.key.len);

    /* Get length. */
    token->token.digits = slapi_entry_attr_get_int(entry, O("digits"));
    if (token->token.digits != 6 && token->token.digits != 8)
        goto error;

    /* Get algorithm. */
    token->token.algo = slapi_entry_attr_get_charptr(entry, O("algorithm"));
    if (token->token.algo == NULL)
        token->token.algo = slapi_ch_strdup("sha1");
    if (!is_algo_valid(token->token.algo))
        goto error;

    switch (token->type) {
    case OTPTOKEN_TOTP:
        /* Get offset. */
        token->totp.offset = slapi_entry_attr_get_int(entry, T("clockOffset"));

        /* Get step. */
        token->totp.step = slapi_entry_attr_get_uint(entry, T("timeStep"));
        if (token->totp.step == 0)
            token->totp.step = IPA_OTP_DEFAULT_TOKEN_STEP;
        break;
    default:
        break;
    }

    return token;

error:
    otptoken_free(token);
    return NULL;
}

static struct otptoken **find(Slapi_ComponentId *id, const char *user_dn,
                              const char *token_dn, const char *intfilter,
                              const char *extfilter)
{
    struct otptoken **tokens = NULL;
    Slapi_Entry **entries = NULL;
    Slapi_PBlock *pb = NULL;
    Slapi_DN *sdn = NULL;
    char *filter = NULL;
    size_t count = 0;
    int result = -1;

    if (intfilter == NULL)
        intfilter = "";

    if (extfilter == NULL)
        extfilter = "";

    /* Create the filter. */
    if (user_dn == NULL) {
        filter = "(&" IPA_OTP_OBJCLS_FILTER "%s%s)";
        filter = slapi_filter_sprintf(filter, intfilter, extfilter);
    } else {
        filter = "(&" IPA_OTP_OBJCLS_FILTER "(ipatokenOwner=%s%s)%s%s)";
        filter = slapi_filter_sprintf(filter, ESC_AND_NORM_NEXT_VAL,
                                      user_dn, intfilter, extfilter);
    }

    /* Create the search. */
    pb = slapi_pblock_new();
    if (token_dn != NULL) {
        /* Find only the token specified. */
        slapi_search_internal_set_pb(pb, token_dn, LDAP_SCOPE_BASE, filter,
                                     NULL, 0, NULL, NULL, id, 0);
    } else {
        sdn = slapi_sdn_new_dn_byval(user_dn);
        if (sdn == NULL)
            goto error;

        /* Find all user tokens. */
        slapi_search_internal_set_pb(pb, get_basedn(sdn),
                                     LDAP_SCOPE_SUBTREE, filter, NULL,
                                     0, NULL, NULL, id, 0);
    }
    slapi_search_internal_pb(pb);
    slapi_ch_free_string(&filter);

    /* Get the results. */
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_RESULT, &result);
    if (result != LDAP_SUCCESS)
        goto error;
    slapi_pblock_get(pb, SLAPI_PLUGIN_INTOP_SEARCH_ENTRIES, &entries);
    if (entries == NULL)
        goto error;

    /* TODO: Can I get the count another way? */
    for (count = 0; entries[count] != NULL; count++)
        continue;

    /* Create the array. */
    tokens = calloc(count + 1, sizeof(*tokens));
    if (tokens == NULL)
        goto error;
    for (count = 0; entries[count] != NULL; count++) {
        tokens[count] = otptoken_new(id, entries[count]);
        if (tokens[count] == NULL) {
            otptoken_free_array(tokens);
            tokens = NULL;
            goto error;
        }
    }

error:
    if (sdn != NULL)
        slapi_sdn_free(&sdn);
    slapi_pblock_destroy(pb);
    return tokens;
}

struct otptoken **otptoken_find(Slapi_ComponentId *id, const char *user_dn,
                                const char *token_dn, bool active,
                                const char *filter)
{
    static const char template[] =
    "(|(ipatokenNotBefore<=%04d%02d%02d%02d%02d%02dZ)(!(ipatokenNotBefore=*)))"
    "(|(ipatokenNotAfter>=%04d%02d%02d%02d%02d%02dZ)(!(ipatokenNotAfter=*)))"
    "(|(ipatokenDisabled=FALSE)(!(ipatokenDisabled=*)))";
    char actfilt[sizeof(template)];
    struct tm tm;
    time_t now;

    if (!active)
        return find(id, user_dn, token_dn, NULL, filter);

    /* Get the current time. */
    if (time(&now) == (time_t) -1)
        return NULL;
    if (gmtime_r(&now, &tm) == NULL)
        return NULL;

    /* Get the current time string. */
    if (snprintf(actfilt, sizeof(actfilt), template,
                 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
                 tm.tm_hour, tm.tm_min, tm.tm_sec,
                 tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
                 tm.tm_hour, tm.tm_min, tm.tm_sec) < 0)
        return NULL;

    return find(id, user_dn, token_dn, actfilt, filter);
}

int otptoken_get_digits(struct otptoken *token)
{
    return token == NULL ? 0 : token->token.digits;
}

const Slapi_DN *otptoken_get_sdn(struct otptoken *token)
{
    return token->sdn;
}

bool otptoken_validate(struct otptoken *token, size_t steps, uint32_t code)
{
    time_t now = 0;

    if (token == NULL)
        return false;

    /* We only need the local time for time-based tokens. */
    if (token->type == OTPTOKEN_TOTP && time(&now) == (time_t) -1)
        return false;

    for (int i = 0; i <= steps; i++) {
        /* Validate the positive step. */
        if (validate(token, now, i, code, NULL))
            return writeback(token, i + 1, false);

        if (i == 0)
            continue;

        /* Validate the negative step. */
        if (validate(token, now, 0 - i, code, NULL))
            return writeback(token, 0 - i + 1, false);
    }

    return false;
}

bool otptoken_validate_string(struct otptoken *token, size_t steps,
                              const char *code, ssize_t len, bool tail)
{
    uint32_t otp;

    if (token == NULL || code == NULL)
        return false;

    if (len < 0)
        len = strlen(code);

    if (len < token->token.digits)
        return false;

    if (tail)
        code = &code[len - token->token.digits];
    len = token->token.digits;

    /*
     *  Convert code string to decimal.
     *
     *  NOTE: We can't use atol() or strtoul() because:
     *  1. We may have leading zeros (atol() fails here).
     *  2. Neither support limiting conversion by length.
     */
    otp = 0;
    for (ssize_t i = 0; i < len; i++) {
        if (code[i] < '0' || code[i] > '9')
            return false;
        otp *= 10;
        otp += code[i] - '0';
    }

    return otptoken_validate(token, steps, otp);
}

bool otptoken_sync(struct otptoken * const *tokens, size_t steps,
                   uint32_t first_code, uint32_t second_code)
{
    time_t now = 0;

    if (tokens == NULL)
        return false;

    if (time(&now) == (time_t) -1)
        return false;

    for (int i = 0; i <= steps; i++) {
        for (int j = 0; tokens[j] != NULL; j++) {
            /* Validate the positive step. */
            if (validate(tokens[j], now, i, first_code, &second_code))
                return writeback(tokens[j], i + 2, true);

            if (i == 0)
                continue;

            /* Validate the negative step. */
            if (validate(tokens[j], now, 0 - i, first_code, &second_code))
                return writeback(tokens[j], 0 - i + 2, true);
        }
    }

    return false;
}