xref: /freebsd/crypto/krb5/src/lib/krb5/ccache/ccselect_hostname.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/krb5/ccache/ccselect_hostname.c - hostname ccselect module */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright (C) 2017 by Red Hat, Inc.
5*7f2fe78bSCy Schubert  * All rights reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Redistribution and use in source and binary forms, with or without
8*7f2fe78bSCy Schubert  * modification, are permitted provided that the following conditions
9*7f2fe78bSCy Schubert  * are met:
10*7f2fe78bSCy Schubert  *
11*7f2fe78bSCy Schubert  * * Redistributions of source code must retain the above copyright
12*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer.
13*7f2fe78bSCy Schubert  *
14*7f2fe78bSCy Schubert  * * Redistributions in binary form must reproduce the above copyright
15*7f2fe78bSCy Schubert  *   notice, this list of conditions and the following disclaimer in
16*7f2fe78bSCy Schubert  *   the documentation and/or other materials provided with the
17*7f2fe78bSCy Schubert  *   distribution.
18*7f2fe78bSCy Schubert  *
19*7f2fe78bSCy Schubert  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20*7f2fe78bSCy Schubert  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21*7f2fe78bSCy Schubert  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22*7f2fe78bSCy Schubert  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23*7f2fe78bSCy Schubert  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24*7f2fe78bSCy Schubert  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25*7f2fe78bSCy Schubert  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26*7f2fe78bSCy Schubert  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27*7f2fe78bSCy Schubert  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28*7f2fe78bSCy Schubert  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29*7f2fe78bSCy Schubert  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30*7f2fe78bSCy Schubert  * OF THE POSSIBILITY OF SUCH DAMAGE.
31*7f2fe78bSCy Schubert  */
32*7f2fe78bSCy Schubert 
33*7f2fe78bSCy Schubert #include "k5-int.h"
34*7f2fe78bSCy Schubert #include "cc-int.h"
35*7f2fe78bSCy Schubert #include <ctype.h>
36*7f2fe78bSCy Schubert #include <krb5/ccselect_plugin.h>
37*7f2fe78bSCy Schubert 
38*7f2fe78bSCy Schubert /* Swap a and b, using tmp as an intermediate. */
39*7f2fe78bSCy Schubert #define SWAP(a, b, tmp)                         \
40*7f2fe78bSCy Schubert     tmp = a;                                    \
41*7f2fe78bSCy Schubert     a = b;                                      \
42*7f2fe78bSCy Schubert     b = tmp;
43*7f2fe78bSCy Schubert 
44*7f2fe78bSCy Schubert static krb5_error_code
hostname_init(krb5_context context,krb5_ccselect_moddata * data_out,int * priority_out)45*7f2fe78bSCy Schubert hostname_init(krb5_context context, krb5_ccselect_moddata *data_out,
46*7f2fe78bSCy Schubert               int *priority_out)
47*7f2fe78bSCy Schubert {
48*7f2fe78bSCy Schubert     *data_out = NULL;
49*7f2fe78bSCy Schubert     *priority_out = KRB5_CCSELECT_PRIORITY_HEURISTIC;
50*7f2fe78bSCy Schubert     return 0;
51*7f2fe78bSCy Schubert }
52*7f2fe78bSCy Schubert 
53*7f2fe78bSCy Schubert static krb5_error_code
hostname_choose(krb5_context context,krb5_ccselect_moddata data,krb5_principal server,krb5_ccache * ccache_out,krb5_principal * princ_out)54*7f2fe78bSCy Schubert hostname_choose(krb5_context context, krb5_ccselect_moddata data,
55*7f2fe78bSCy Schubert                 krb5_principal server, krb5_ccache *ccache_out,
56*7f2fe78bSCy Schubert                 krb5_principal *princ_out)
57*7f2fe78bSCy Schubert {
58*7f2fe78bSCy Schubert     krb5_error_code ret;
59*7f2fe78bSCy Schubert     char *p, *host = NULL;
60*7f2fe78bSCy Schubert     size_t hostlen;
61*7f2fe78bSCy Schubert     krb5_cccol_cursor col_cursor;
62*7f2fe78bSCy Schubert     krb5_ccache ccache, tmp_ccache, best_ccache = NULL;
63*7f2fe78bSCy Schubert     krb5_principal princ, tmp_princ, best_princ = NULL;
64*7f2fe78bSCy Schubert     krb5_data domain;
65*7f2fe78bSCy Schubert 
66*7f2fe78bSCy Schubert     *ccache_out = NULL;
67*7f2fe78bSCy Schubert     *princ_out = NULL;
68*7f2fe78bSCy Schubert 
69*7f2fe78bSCy Schubert     if (server->type != KRB5_NT_SRV_HST || server->length < 2)
70*7f2fe78bSCy Schubert         return KRB5_PLUGIN_NO_HANDLE;
71*7f2fe78bSCy Schubert 
72*7f2fe78bSCy Schubert     /* Compute upper-case hostname. */
73*7f2fe78bSCy Schubert     hostlen = server->data[1].length;
74*7f2fe78bSCy Schubert     host = k5memdup0(server->data[1].data, hostlen, &ret);
75*7f2fe78bSCy Schubert     if (host == NULL)
76*7f2fe78bSCy Schubert         return ret;
77*7f2fe78bSCy Schubert     for (p = host; *p != '\0'; p++) {
78*7f2fe78bSCy Schubert         if (islower(*p))
79*7f2fe78bSCy Schubert             *p = toupper(*p);
80*7f2fe78bSCy Schubert     }
81*7f2fe78bSCy Schubert 
82*7f2fe78bSCy Schubert     /* Scan the collection for a cache with a client principal whose realm is
83*7f2fe78bSCy Schubert      * the longest tail of the server hostname. */
84*7f2fe78bSCy Schubert     ret = krb5_cccol_cursor_new(context, &col_cursor);
85*7f2fe78bSCy Schubert     if (ret)
86*7f2fe78bSCy Schubert         goto done;
87*7f2fe78bSCy Schubert 
88*7f2fe78bSCy Schubert     for (ret = krb5_cccol_cursor_next(context, col_cursor, &ccache);
89*7f2fe78bSCy Schubert          ret == 0 && ccache != NULL;
90*7f2fe78bSCy Schubert          ret = krb5_cccol_cursor_next(context, col_cursor, &ccache)) {
91*7f2fe78bSCy Schubert         ret = krb5_cc_get_principal(context, ccache, &princ);
92*7f2fe78bSCy Schubert         if (ret) {
93*7f2fe78bSCy Schubert             krb5_cc_close(context, ccache);
94*7f2fe78bSCy Schubert             break;
95*7f2fe78bSCy Schubert         }
96*7f2fe78bSCy Schubert 
97*7f2fe78bSCy Schubert         /* Check for a longer match than we have. */
98*7f2fe78bSCy Schubert         domain = make_data(host, hostlen);
99*7f2fe78bSCy Schubert         while (best_princ == NULL ||
100*7f2fe78bSCy Schubert                best_princ->realm.length < domain.length) {
101*7f2fe78bSCy Schubert             if (data_eq(princ->realm, domain)) {
102*7f2fe78bSCy Schubert                 SWAP(best_ccache, ccache, tmp_ccache);
103*7f2fe78bSCy Schubert                 SWAP(best_princ, princ, tmp_princ);
104*7f2fe78bSCy Schubert                 break;
105*7f2fe78bSCy Schubert             }
106*7f2fe78bSCy Schubert 
107*7f2fe78bSCy Schubert             /* Try the next parent domain. */
108*7f2fe78bSCy Schubert             p = memchr(domain.data, '.', domain.length);
109*7f2fe78bSCy Schubert             if (p == NULL)
110*7f2fe78bSCy Schubert                 break;
111*7f2fe78bSCy Schubert             domain = make_data(p + 1, hostlen - (p + 1 - host));
112*7f2fe78bSCy Schubert         }
113*7f2fe78bSCy Schubert 
114*7f2fe78bSCy Schubert         if (ccache != NULL)
115*7f2fe78bSCy Schubert             krb5_cc_close(context, ccache);
116*7f2fe78bSCy Schubert         krb5_free_principal(context, princ);
117*7f2fe78bSCy Schubert     }
118*7f2fe78bSCy Schubert 
119*7f2fe78bSCy Schubert     krb5_cccol_cursor_free(context, &col_cursor);
120*7f2fe78bSCy Schubert 
121*7f2fe78bSCy Schubert     if (best_ccache != NULL) {
122*7f2fe78bSCy Schubert         *ccache_out = best_ccache;
123*7f2fe78bSCy Schubert         *princ_out = best_princ;
124*7f2fe78bSCy Schubert     } else {
125*7f2fe78bSCy Schubert         ret = KRB5_PLUGIN_NO_HANDLE;
126*7f2fe78bSCy Schubert     }
127*7f2fe78bSCy Schubert 
128*7f2fe78bSCy Schubert done:
129*7f2fe78bSCy Schubert     free(host);
130*7f2fe78bSCy Schubert     return ret;
131*7f2fe78bSCy Schubert }
132*7f2fe78bSCy Schubert 
133*7f2fe78bSCy Schubert krb5_error_code
ccselect_hostname_initvt(krb5_context context,int maj_ver,int min_ver,krb5_plugin_vtable vtable)134*7f2fe78bSCy Schubert ccselect_hostname_initvt(krb5_context context, int maj_ver, int min_ver,
135*7f2fe78bSCy Schubert                          krb5_plugin_vtable vtable)
136*7f2fe78bSCy Schubert {
137*7f2fe78bSCy Schubert     krb5_ccselect_vtable vt;
138*7f2fe78bSCy Schubert 
139*7f2fe78bSCy Schubert     if (maj_ver != 1)
140*7f2fe78bSCy Schubert         return KRB5_PLUGIN_VER_NOTSUPP;
141*7f2fe78bSCy Schubert     vt = (krb5_ccselect_vtable)vtable;
142*7f2fe78bSCy Schubert     vt->name = "hostname";
143*7f2fe78bSCy Schubert     vt->init = hostname_init;
144*7f2fe78bSCy Schubert     vt->choose = hostname_choose;
145*7f2fe78bSCy Schubert     return 0;
146*7f2fe78bSCy Schubert }
147