summaryrefslogtreecommitdiffstats
path: root/src/tests/cmocka/common_mock_krb5.c
blob: e253119f5a9e550a8d30fdd5dbdcf5cb69916ebf (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
/*
    Authors:
        Sumit Bose <sbose@redhat.com>
        Jakub Hrozek <jhrozek@redhat.com>

    Copyright (C) 2015 Red Hat

    SSSD tests: Tests keytab utilities

    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/>.
*/

#include "util/sss_krb5.h"
#include "tests/cmocka/common_mock.h"
#include "tests/cmocka/common_mock_krb5.h"

int mock_keytab(krb5_context kctx,
                const char *kt_path,
                krb5_keytab_entry *kt_keys,
                size_t nkeys)
{
    krb5_error_code kerr;
    krb5_keytab keytab;
    size_t n;

    kerr = krb5_kt_resolve(kctx, kt_path, &keytab);
    assert_int_equal(kerr, 0);

    for (n = 0; n < nkeys; n++) {
        kerr = krb5_kt_add_entry(kctx, keytab, &kt_keys[n]);
        assert_int_equal(kerr, 0);
    }

    kerr = krb5_kt_close(kctx, keytab);
    assert_int_equal(kerr, 0);

    return EOK;
}

void mock_krb5_keytab_entry(krb5_keytab_entry *kent,
                            krb5_principal principal,
                            krb5_timestamp timestamp,
                            krb5_kvno vno,
                            krb5_enctype enctype,
                            const char *key)
{
    memset(kent, 0, sizeof(krb5_keytab_entry));

    kent->magic = KV5M_KEYTAB_ENTRY;
    kent->principal = principal;
    kent->timestamp = timestamp;
    kent->vno = vno;
    kent->key.magic = KV5M_KEYBLOCK;
    kent->key.enctype = enctype;
    kent->key.length = strlen(key) - 1;
    kent->key.contents = (krb5_octet *) discard_const(key);
}

int mock_keytab_with_contents(TALLOC_CTX *mem_ctx,
                              const char *keytab_path,
                              const char *keytab_princ)
{
    krb5_context kctx;
    krb5_principal principal;
    krb5_error_code kerr;
    size_t nkeys = 2;
    krb5_keytab_entry keys[nkeys];
    char *keytab_file_name;

    kerr = krb5_init_context(&kctx);
    assert_int_equal(kerr, 0);

    keytab_file_name = talloc_asprintf(mem_ctx, "FILE:%s", keytab_path);
    assert_non_null(keytab_file_name);

    kerr = krb5_parse_name(kctx, keytab_princ, &principal);
    assert_int_equal(kerr, 0);

    memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));

    mock_krb5_keytab_entry(&keys[0], principal, 12345, 1, 1, "11");
    mock_krb5_keytab_entry(&keys[1], principal, 12345, 1, 2, "12");

    kerr = mock_keytab(kctx, keytab_file_name, keys, nkeys);
    assert_int_equal(kerr, 0);

    krb5_free_principal(kctx, principal);
    krb5_free_context(kctx);
    talloc_free(keytab_file_name);

    return 0;
}