Line data Source code
1 : /*
2 : Authors:
3 : Sumit Bose <sbose@redhat.com>
4 : Jakub Hrozek <jhrozek@redhat.com>
5 :
6 : Copyright (C) 2015 Red Hat
7 :
8 : SSSD tests: Tests keytab utilities
9 :
10 : This program is free software; you can redistribute it and/or modify
11 : it under the terms of the GNU General Public License as published by
12 : the Free Software Foundation; either version 3 of the License, or
13 : (at your option) any later version.
14 :
15 : This program is distributed in the hope that it will be useful,
16 : but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : GNU General Public License for more details.
19 :
20 : You should have received a copy of the GNU General Public License
21 : along with this program. If not, see <http://www.gnu.org/licenses/>.
22 : */
23 :
24 : #include "util/sss_krb5.h"
25 : #include "tests/cmocka/common_mock.h"
26 : #include "tests/cmocka/common_mock_krb5.h"
27 :
28 18 : int mock_keytab(krb5_context kctx,
29 : const char *kt_path,
30 : krb5_keytab_entry *kt_keys,
31 : size_t nkeys)
32 : {
33 : krb5_error_code kerr;
34 : krb5_keytab keytab;
35 : size_t n;
36 :
37 18 : kerr = krb5_kt_resolve(kctx, kt_path, &keytab);
38 18 : assert_int_equal(kerr, 0);
39 :
40 60 : for (n = 0; n < nkeys; n++) {
41 42 : kerr = krb5_kt_add_entry(kctx, keytab, &kt_keys[n]);
42 42 : assert_int_equal(kerr, 0);
43 : }
44 :
45 18 : kerr = krb5_kt_close(kctx, keytab);
46 18 : assert_int_equal(kerr, 0);
47 :
48 18 : return EOK;
49 : }
50 :
51 42 : void mock_krb5_keytab_entry(krb5_keytab_entry *kent,
52 : krb5_principal principal,
53 : krb5_timestamp timestamp,
54 : krb5_kvno vno,
55 : krb5_enctype enctype,
56 : const char *key)
57 : {
58 42 : memset(kent, 0, sizeof(krb5_keytab_entry));
59 :
60 42 : kent->magic = KV5M_KEYTAB_ENTRY;
61 42 : kent->principal = principal;
62 42 : kent->timestamp = timestamp;
63 42 : kent->vno = vno;
64 42 : kent->key.magic = KV5M_KEYBLOCK;
65 42 : kent->key.enctype = enctype;
66 42 : kent->key.length = strlen(key) - 1;
67 42 : kent->key.contents = (krb5_octet *) discard_const(key);
68 42 : }
69 :
70 15 : int mock_keytab_with_contents(TALLOC_CTX *mem_ctx,
71 : const char *keytab_path,
72 : const char *keytab_princ)
73 15 : {
74 : krb5_context kctx;
75 : krb5_principal principal;
76 : krb5_error_code kerr;
77 15 : size_t nkeys = 2;
78 15 : krb5_keytab_entry keys[nkeys];
79 : char *keytab_file_name;
80 :
81 15 : kerr = krb5_init_context(&kctx);
82 15 : assert_int_equal(kerr, 0);
83 :
84 15 : keytab_file_name = talloc_asprintf(mem_ctx, "FILE:%s", keytab_path);
85 15 : assert_non_null(keytab_file_name);
86 :
87 15 : kerr = krb5_parse_name(kctx, keytab_princ, &principal);
88 15 : assert_int_equal(kerr, 0);
89 :
90 15 : memset(&keys, nkeys, nkeys * sizeof(krb5_keytab_entry));
91 :
92 15 : mock_krb5_keytab_entry(&keys[0], principal, 12345, 1, 1, "11");
93 15 : mock_krb5_keytab_entry(&keys[1], principal, 12345, 1, 2, "12");
94 :
95 15 : kerr = mock_keytab(kctx, keytab_file_name, keys, nkeys);
96 15 : assert_int_equal(kerr, 0);
97 :
98 15 : krb5_free_principal(kctx, principal);
99 15 : krb5_free_context(kctx);
100 15 : talloc_free(keytab_file_name);
101 :
102 15 : return 0;
103 : }
|