aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb5/ccache/cccursor.c
blob: f0ae4a832be73a8cbbb309266a28898e923898fe (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
/*
 * lib/krb5/ccache/cccursor.c
 *
 * Copyright 2006, 2007 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 * cursor for sequential traversal of ccaches
 */

#include "k5-int.h"

#include <assert.h>

#define CCCURSOR_CONTEXT 1
#define CCCURSOR_ENV 2
#define CCCURSOR_OS 3
#define CCCURSOR_PERTYPE 4

#define NFULLNAMES 3

/* Prefix and residual parts of a full ccache name. */
struct cc_fullname {
    char *pfx;
    char *res;
};

struct _krb5_cccol_cursor {
    int pos;
    krb5_cc_typecursor typecursor;
    const krb5_cc_ops *ops;
    krb5_cc_ptcursor ptcursor;
    int cur_fullname;
    struct cc_fullname fullnames[NFULLNAMES]; /* previously seen ccaches */
};
/* typedef of krb5_cccol_cursor is in krb5.h */

static int cccol_already(krb5_context, krb5_cccol_cursor, krb5_ccache *);

static int cccol_cmpname(const char *, const char *, struct cc_fullname *);

static krb5_error_code
cccol_do_resolve(krb5_context, krb5_cccol_cursor, const char *, krb5_ccache *);

static krb5_error_code
cccol_pertype_next(krb5_context, krb5_cccol_cursor, krb5_ccache *);

krb5_error_code KRB5_CALLCONV
krb5_cccol_cursor_new(
    krb5_context context,
    krb5_cccol_cursor *cursor)
{
    krb5_error_code ret = 0;
    krb5_cccol_cursor n = NULL;
    int i;

    *cursor = NULL;
    n = malloc(sizeof(*n));
    if (n == NULL)
	return ENOMEM;

    n->pos = CCCURSOR_CONTEXT;
    n->typecursor = NULL;
    n->ptcursor = NULL;
    n->ops = NULL;

    for (i = 0; i < NFULLNAMES; i++) {
	n->fullnames[i].pfx = n->fullnames[i].res = NULL;
    }
    n->cur_fullname = 0;
    ret = krb5int_cc_typecursor_new(context, &n->typecursor);
    if (ret)
	goto errout;

    do {
	/* Find first backend with ptcursor functionality. */
	ret = krb5int_cc_typecursor_next(context, n->typecursor, &n->ops);
	if (ret || n->ops == NULL)
	    goto errout;
    } while (n->ops->ptcursor_new == NULL);

    ret = n->ops->ptcursor_new(context, &n->ptcursor);
    if (ret)
	goto errout;

errout:
    if (ret) {
	krb5_cccol_cursor_free(context, &n);
    }
    *cursor = n;
    return ret;
}

krb5_error_code KRB5_CALLCONV
krb5_cccol_cursor_next(
    krb5_context context,
    krb5_cccol_cursor cursor,
    krb5_ccache *ccache)
{
    krb5_error_code ret = 0;
    char *name;
    krb5_os_context os_ctx = NULL;

    *ccache = NULL;
    os_ctx = &context->os_context;

    switch (cursor->pos) {
    case CCCURSOR_CONTEXT:
	name = os_ctx->default_ccname;
	if (name != NULL) {
	    cursor->pos = CCCURSOR_ENV;
	    ret = cccol_do_resolve(context, cursor, name, ccache);
	    if (ret)
		goto errout;
	    if (*ccache != NULL)
		break;
	}
	/* fall through */
    case CCCURSOR_ENV:
	name = getenv(KRB5_ENV_CCNAME);
	if (name != NULL) {
	    cursor->pos = CCCURSOR_OS;
	    ret = cccol_do_resolve(context, cursor, name, ccache);
	    if (ret)
		goto errout;
	    if (*ccache != NULL)
		break;
	}
	/* fall through */
    case CCCURSOR_OS:
	ret = krb5int_cc_os_default_name(context, &name);
	if (ret) goto errout;
	if (name != NULL) {
	    cursor->pos = CCCURSOR_PERTYPE;
	    ret = cccol_do_resolve(context, cursor, name, ccache);
	    free(name);
	    if (ret)
		goto errout;
	    if (*ccache != NULL)
		break;
	}
	/* fall through */
    case CCCURSOR_PERTYPE:
	cursor->pos = CCCURSOR_PERTYPE;
	do {
	    ret = cccol_pertype_next(context, cursor, ccache);
	    if (ret)
		goto errout;
	} while (cccol_already(context, cursor, ccache));
	break;
    }
errout:
    return ret;
}

krb5_error_code KRB5_CALLCONV
krb5_cccol_cursor_free(
    krb5_context context,
    krb5_cccol_cursor *cursor)
{
    krb5_cccol_cursor c = *cursor;
    int i;

    if (c == NULL)
	return 0;

    for (i = 0; i < NFULLNAMES; i++) {
	if (c->fullnames[i].pfx != NULL)
	    free(c->fullnames[i].pfx);
	if (c->fullnames[i].res != NULL)
	    free(c->fullnames[i].res);
    }
    if (c->ptcursor != NULL)
	c->ops->ptcursor_free(context, &c->ptcursor);
    if (c->typecursor != NULL)
	krb5int_cc_typecursor_free(context, &c->typecursor);
    free(c);

    *cursor = NULL;
    return 0;
}

/*
 * Determine if a ccache from a per-type cursor was already one of the
 * higher-priority defaults.
 */
static int
cccol_already(
    krb5_context context,
    krb5_cccol_cursor c,
    krb5_ccache *ccache)
{
    const char *name = NULL, *prefix = NULL;
    int i;

    if (*ccache == NULL)
	return 0;
    name = krb5_cc_get_name(context, *ccache);
    if (name == NULL)
	return 0;
    prefix = krb5_cc_get_type(context, *ccache);

    assert(c->cur_fullname < NFULLNAMES);
    for (i = 0; i < c->cur_fullname; i++) {
	if (cccol_cmpname(prefix, name, &c->fullnames[i])) {
	    krb5_cc_close(context, *ccache);
	    *ccache = NULL;
	    return 1;
	}
    }
    return 0;
}

/*
 * Compare {prefix, name} against a cc_fullname.
 */
static int
cccol_cmpname(
    const char *prefix,
    const char *name,
    struct cc_fullname *fullname)
{
    if (fullname->pfx == NULL || fullname->res == NULL)
	return 0;
    if (strcmp(prefix, fullname->pfx))
	return 0;
    if (strcmp(name, fullname->res))
	return 0;

    return 1;
}

/*
 * Resolve one of the high-precedence ccaches, and cache its full name
 * {prefix, residual} for exclusion when doing per-type ccache
 * iteration.  Also check to see if we've already seen the ccache
 * name we're given.
 */
static krb5_error_code
cccol_do_resolve(
    krb5_context context,
    krb5_cccol_cursor cursor,
    const char *name,
    krb5_ccache *ccache)
{
    krb5_error_code ret = 0;
    struct cc_fullname *fullname;

    assert(cursor->cur_fullname < NFULLNAMES);
    ret = krb5_cc_resolve(context, name, ccache);
    if (ret)
	return ret;

    if (cccol_already(context, cursor, ccache))
	return 0;

    fullname = &cursor->fullnames[cursor->cur_fullname];
    fullname->pfx = strdup(krb5_cc_get_type(context, *ccache));
    fullname->res = strdup(krb5_cc_get_name(context, *ccache));
    cursor->cur_fullname++;
    return ret;
}

/*
 * Find next ccache in current backend, iterating through backends if
 * ccache list of the current backend is exhausted.
 */
static krb5_error_code
cccol_pertype_next(
    krb5_context context,
    krb5_cccol_cursor cursor,
    krb5_ccache *ccache)
{
    krb5_error_code ret = 0;

    *ccache = NULL;

    /* Are we out of backends? */
    if (cursor->ops == NULL)
	return 0;
    /*
     * Loop in case there are multiple backends with empty ccache
     * lists.
     */
    while (*ccache == NULL) {
	ret = cursor->ops->ptcursor_next(context, cursor->ptcursor, ccache);
	if (ret)
	    goto errout;
	if (*ccache != NULL)
	    return 0;

	ret = cursor->ops->ptcursor_free(context, &cursor->ptcursor);
	if (ret)
	    goto errout;

	do {
	    /* Find first backend with ptcursor functionality. */
	    ret = krb5int_cc_typecursor_next(context, cursor->typecursor,
					     &cursor->ops);
	    if (ret)
		goto errout;
	    if (cursor->ops == NULL)
		return 0;
	} while (cursor->ops->ptcursor_new == NULL);

	ret = cursor->ops->ptcursor_new(context, &cursor->ptcursor);
	if (ret)
	    goto errout;
    }
errout:
    return ret;
}