1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/ccache/ccselect_realm.c - realm ccselect module */
3 /*
4 * Copyright (C) 2011 by the Massachusetts Institute of Technology.
5 * All rights reserved.
6 *
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
11 *
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
25 */
26
27 #include "k5-int.h"
28 #include "cc-int.h"
29 #include <krb5/ccselect_plugin.h>
30
31 static krb5_error_code
realm_init(krb5_context context,krb5_ccselect_moddata * data_out,int * priority_out)32 realm_init(krb5_context context, krb5_ccselect_moddata *data_out,
33 int *priority_out)
34 {
35 *data_out = NULL;
36 *priority_out = KRB5_CCSELECT_PRIORITY_HEURISTIC;
37 return 0;
38 }
39
40 static krb5_error_code
realm_choose(krb5_context context,krb5_ccselect_moddata data,krb5_principal server,krb5_ccache * cache_out,krb5_principal * princ_out)41 realm_choose(krb5_context context, krb5_ccselect_moddata data,
42 krb5_principal server, krb5_ccache *cache_out,
43 krb5_principal *princ_out)
44 {
45 krb5_error_code ret;
46 krb5_cccol_cursor cursor;
47 krb5_ccache cache;
48 krb5_principal princ;
49
50 *cache_out = NULL;
51 *princ_out = NULL;
52
53 if (krb5_is_referral_realm(&server->realm))
54 return KRB5_PLUGIN_NO_HANDLE;
55
56 /* Scan the collection for a cache with a client principal in the same
57 * realm as the server principal. */
58 ret = krb5_cccol_cursor_new(context, &cursor);
59 if (ret)
60 return ret;
61 while ((ret = krb5_cccol_cursor_next(context, cursor, &cache)) == 0 &&
62 cache != NULL) {
63 ret = krb5_cc_get_principal(context, cache, &princ);
64 if (ret == 0) {
65 if (data_eq(princ->realm, server->realm))
66 break;
67 krb5_free_principal(context, princ);
68 }
69 krb5_cc_close(context, cache);
70 }
71 krb5_cccol_cursor_free(context, &cursor);
72 if (ret)
73 return ret;
74
75 if (cache == NULL)
76 return KRB5_PLUGIN_NO_HANDLE;
77 *cache_out = cache;
78 *princ_out = princ;
79 return 0;
80 }
81
82 krb5_error_code
ccselect_realm_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)83 ccselect_realm_initvt(krb5_context context, int maj_ver, int min_ver,
84 krb5_plugin_vtable vtable)
85 {
86 krb5_ccselect_vtable vt;
87
88 if (maj_ver != 1)
89 return KRB5_PLUGIN_VER_NOTSUPP;
90 vt = (krb5_ccselect_vtable)vtable;
91 vt->name = "realm";
92 vt->init = realm_init;
93 vt->choose = realm_choose;
94 return 0;
95 }
96