summaryrefslogtreecommitdiffstats
path: root/krb5-master-keyring-offsets.patch
blob: b54e84b80fac8e642ca234a7ba3e90f281c8efc8 (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
commit fb4817a32d0c369049e0868468dd2eb75487630d
Author: Simo Sorce <simo@redhat.com>
Date:   Thu Nov 14 17:23:59 2013 -0500

    Add support to store time offsets in cc_keyring
    
    The code follows the same model used for the memory ccache type.  Time
    offsets are stored in each credential cache in a special key just like
    the principal name.  Legacy session caches do not store timestamps as
    legacy code would fail when iterating over the new offset key.
    
    [ghudson@mit.edu: minor formatting changes; note legacy session
    exception in commit message]
    
    ticket: 7768 (new)
    target_version: 1.12
    tags: pullup

diff --git a/src/lib/krb5/ccache/cc_keyring.c b/src/lib/krb5/ccache/cc_keyring.c
index a07a0dc..2192fa5 100644
--- a/src/lib/krb5/ccache/cc_keyring.c
+++ b/src/lib/krb5/ccache/cc_keyring.c
@@ -189,6 +189,11 @@ debug_print(char *fmt, ...)
 #define KRCC_PERSISTENT_KEYRING_NAME "_krb"
 
 /*
+ * Name of the key holding time offsets for the individual cache
+ */
+#define KRCC_TIME_OFFSETS "__krb5_time_offsets__"
+
+/*
  * Keyring name prefix and length of random name part
  */
 #define KRCC_NAME_PREFIX "krb_ccache_"
@@ -217,6 +222,7 @@ typedef struct _krb5_krcc_cursor
     int     numkeys;
     int     currkey;
     key_serial_t princ_id;
+    key_serial_t offsets_id;
     key_serial_t *keys;
 } *krb5_krcc_cursor;
 
@@ -340,6 +346,12 @@ static krb5_error_code krb5_krcc_save_principal
 
 static krb5_error_code krb5_krcc_retrieve_principal
 (krb5_context context, krb5_ccache id, krb5_principal * princ);
+static krb5_error_code krb5_krcc_save_time_offsets
+(krb5_context context, krb5_ccache id, krb5_int32 time_offset,
+ krb5_int32 usec_offset);
+static krb5_error_code krb5_krcc_get_time_offsets
+(krb5_context context, krb5_ccache id, krb5_int32 *time_offset,
+ krb5_int32 *usec_offset);
 
 /* Routines to parse a key from a keyring into a cred structure */
 static krb5_error_code krb5_krcc_parse
@@ -410,6 +422,12 @@ krb5_krcc_parse_index(krb5_context context, krb5_int32 *version,
 static krb5_error_code
 krb5_krcc_unparse_index(krb5_context context, krb5_int32 version,
                         const char *primary, void **datapp, int *lenptr);
+static krb5_error_code
+krb5_krcc_parse_offsets(krb5_context context, krb5_int32 *time_offset,
+                        krb5_int32 *usec_offset, void *payload, int psize);
+static krb5_error_code
+krb5_krcc_unparse_offsets(krb5_context context, krb5_int32 time_offset,
+                          krb5_int32 usec_offset, void **datapp, int *lenptr);
 
 /* Note the following is a stub function for Linux */
 extern krb5_error_code krb5_change_cache(void);
@@ -835,6 +853,7 @@ krb5_krcc_initialize(krb5_context context, krb5_ccache id,
                      krb5_principal princ)
 {
     krb5_krcc_data *data = (krb5_krcc_data *)id->data;
+    krb5_os_context os_ctx = &context->os_context;
     krb5_error_code kret;
     const char *cache_name, *p;
 
@@ -863,6 +882,15 @@ krb5_krcc_initialize(krb5_context context, krb5_ccache id,
         (void)keyctl_link(data->cache_id, KEY_SPEC_SESSION_KEYRING);
 
     kret = krb5_krcc_save_principal(context, id, princ);
+
+    /* Save time offset if it is valid and this is not a legacy cache.  Legacy
+     * applications would fail to parse the new key in the cache keyring. */
+    if (!is_legacy_cache_name(data->name) &&
+        (os_ctx->os_flags & KRB5_OS_TOFFSET_VALID)) {
+        kret = krb5_krcc_save_time_offsets(context, id, os_ctx->time_offset,
+                                           os_ctx->usec_offset);
+    }
+
     if (kret == KRB5_OK)
         krb5_change_cache();
 
@@ -1039,6 +1067,7 @@ make_cache(key_serial_t collection_id, key_serial_t cache_id,
 static krb5_error_code KRB5_CALLCONV
 krb5_krcc_resolve(krb5_context context, krb5_ccache *id, const char *residual)
 {
+    krb5_os_context os_ctx = &context->os_context;
     krb5_error_code ret;
     key_serial_t collection_id, cache_id;
     char *anchor_name = NULL, *collection_name = NULL, *subsidiary_name = NULL;
@@ -1067,6 +1096,19 @@ krb5_krcc_resolve(krb5_context context, krb5_ccache *id, const char *residual)
 
     ret = make_cache(collection_id, cache_id, anchor_name, collection_name,
                      subsidiary_name, id);
+    if (ret)
+        goto cleanup;
+
+    /* Lookup time offsets if necessary. */
+    if ((context->library_options & KRB5_LIBOPT_SYNC_KDCTIME) &&
+        !(os_ctx->os_flags & KRB5_OS_TOFFSET_VALID)) {
+        if (krb5_krcc_get_time_offsets(context, *id,
+                                       &os_ctx->time_offset,
+                                       &os_ctx->usec_offset) == 0) {
+            os_ctx->os_flags &= ~KRB5_OS_TOFFSET_TIME;
+            os_ctx->os_flags |= KRB5_OS_TOFFSET_VALID;
+        }
+    }
 
 cleanup:
     free(anchor_name);
@@ -1122,6 +1164,8 @@ krb5_krcc_start_seq_get(krb5_context context, krb5_ccache id,
     }
 
     krcursor->princ_id = d->princ_id;
+    krcursor->offsets_id = keyctl_search(d->cache_id, KRCC_KEY_TYPE_USER,
+                                         KRCC_TIME_OFFSETS, 0);
     krcursor->numkeys = size / sizeof(key_serial_t);
     krcursor->keys = keys;
 
@@ -1174,8 +1218,10 @@ krb5_krcc_next_cred(krb5_context context, krb5_ccache id,
     if (krcursor->currkey >= krcursor->numkeys)
         return KRB5_CC_END;
 
-    /* If we're pointing at the entry with the principal, skip it */
-    if (krcursor->keys[krcursor->currkey] == krcursor->princ_id) {
+    /* If we're pointing at the entry with the principal, or at the key
+     * with the time offsets, skip it. */
+    while (krcursor->keys[krcursor->currkey] == krcursor->princ_id ||
+           krcursor->keys[krcursor->currkey] == krcursor->offsets_id) {
         krcursor->currkey++;
         /* Check if we have now reached the end */
         if (krcursor->currkey >= krcursor->numkeys)
@@ -1621,6 +1667,84 @@ errout:
     return kret;
 }
 
+static  krb5_error_code
+krb5_krcc_save_time_offsets(krb5_context context, krb5_ccache id,
+                            krb5_int32 time_offset, krb5_int32 usec_offset)
+{
+    krb5_krcc_data *d = (krb5_krcc_data *)id->data;
+    krb5_error_code kret;
+    key_serial_t newkey;
+    void *payload = NULL;
+    int psize;
+
+    k5_cc_mutex_assert_locked(context, &d->lock);
+
+    /* Prepare the payload. */
+    kret = krb5_krcc_unparse_offsets(context, time_offset, usec_offset,
+                                     &payload, &psize);
+    CHECK_N_GO(kret, errout);
+
+    /* Add new key into keyring. */
+    newkey = add_key(KRCC_KEY_TYPE_USER, KRCC_TIME_OFFSETS, payload, psize,
+                     d->cache_id);
+    if (newkey == -1) {
+        kret = errno;
+        DEBUG_PRINT(("Error adding time offsets key: %s\n", strerror(kret)));
+    } else {
+        kret = KRB5_OK;
+        krb5_krcc_update_change_time(d);
+    }
+
+errout:
+    free(payload);
+    return kret;
+}
+
+static krb5_error_code
+krb5_krcc_get_time_offsets(krb5_context context, krb5_ccache id,
+                           krb5_int32 *time_offset, krb5_int32 *usec_offset)
+{
+    krb5_krcc_data *d = (krb5_krcc_data *)id->data;
+    krb5_error_code kret;
+    key_serial_t key;
+    krb5_int32 t, u;
+    void *payload = NULL;
+    int psize;
+
+    k5_cc_mutex_lock(context, &d->lock);
+
+    if (!d->cache_id) {
+        kret = KRB5_FCC_NOFILE;
+        goto errout;
+    }
+
+    key = keyctl_search(d->cache_id, KRCC_KEY_TYPE_USER, KRCC_TIME_OFFSETS, 0);
+    if (key == -1) {
+        kret = ENOENT;
+        goto errout;
+    }
+
+    psize = keyctl_read_alloc(key, &payload);
+    if (psize == -1) {
+        DEBUG_PRINT(("Reading time offsets key %d: %s\n",
+                     key, strerror(errno)));
+        kret = KRB5_CC_IO;
+        goto errout;
+    }
+
+    kret = krb5_krcc_parse_offsets(context, &t, &u, payload, psize);
+    if (kret)
+        goto errout;
+
+    *time_offset = t;
+    *usec_offset = u;
+
+errout:
+    free(payload);
+    k5_cc_mutex_unlock(context, &d->lock);
+    return kret;
+}
+
 struct krcc_ptcursor_data {
     key_serial_t collection_id;
     char *anchor_name;
@@ -2656,6 +2780,83 @@ errout:
     return kret;
 }
 
+static krb5_error_code
+krb5_krcc_parse_offsets(krb5_context context, krb5_int32 *time_offset,
+                        krb5_int32 *usec_offset, void *payload, int psize)
+{
+    krb5_error_code kret;
+    krb5_krcc_bc bc;
+
+    bc.bpp = payload;
+    bc.endp = bc.bpp + psize;
+
+    kret = krb5_krcc_parse_int32(context, time_offset, &bc);
+    CHECK_OUT(kret);
+
+    kret = krb5_krcc_parse_int32(context, usec_offset, &bc);
+    CHECK_OUT(kret);
+
+    return KRB5_OK;
+}
+
+static krb5_error_code
+krb5_krcc_unparse_offsets_internal(krb5_context context,
+                                   krb5_int32 time_offset,
+                                   krb5_int32 usec_offset,
+                                   krb5_krcc_bc *bc)
+{
+    krb5_error_code kret;
+
+    kret = krb5_krcc_unparse_int32(context, time_offset, bc);
+    CHECK_OUT(kret);
+
+    kret = krb5_krcc_unparse_int32(context, usec_offset, bc);
+    CHECK_OUT(kret);
+
+    return KRB5_OK;
+}
+
+static krb5_error_code
+krb5_krcc_unparse_offsets(krb5_context context, krb5_int32 time_offset,
+                          krb5_int32 usec_offset, void **datapp, int *lenptr)
+{
+    krb5_error_code kret;
+    krb5_krcc_bc bc;
+    char *buf;
+
+    if (!datapp || !lenptr)
+        return EINVAL;
+
+    *datapp = NULL;
+    *lenptr = 0;
+
+    /* Do a dry run first to calculate the size. */
+    bc.bpp = bc.endp = NULL;
+    bc.size = 0;
+    kret = krb5_krcc_unparse_offsets_internal(context, time_offset,
+                                              usec_offset, &bc);
+    CHECK_OUT(kret);
+
+    buf = malloc(bc.size);
+    if (buf == NULL)
+        return ENOMEM;
+
+    bc.bpp = buf;
+    bc.endp = buf + bc.size;
+    kret = krb5_krcc_unparse_offsets_internal(context, time_offset,
+                                              usec_offset, &bc);
+    CHECK(kret);
+
+    /* Success! */
+    *datapp = buf;
+    *lenptr = bc.bpp - buf;
+    kret = KRB5_OK;
+
+errout:
+    if (kret)
+        free(buf);
+    return kret;
+}
 /*
  * Utility routine: called by krb5_krcc_* functions to keep
  * result of krb5_krcc_last_change_time up to date.