xref: /freebsd/crypto/krb5/src/lib/kdb/keytab.c (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1*7f2fe78bSCy Schubert /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2*7f2fe78bSCy Schubert /* lib/kdb/keytab.c */
3*7f2fe78bSCy Schubert /*
4*7f2fe78bSCy Schubert  * Copyright 1995 by the Massachusetts Institute of Technology.
5*7f2fe78bSCy Schubert  * All Rights Reserved.
6*7f2fe78bSCy Schubert  *
7*7f2fe78bSCy Schubert  * Export of this software from the United States of America may
8*7f2fe78bSCy Schubert  *   require a specific license from the United States Government.
9*7f2fe78bSCy Schubert  *   It is the responsibility of any person or organization contemplating
10*7f2fe78bSCy Schubert  *   export to obtain such a license before exporting.
11*7f2fe78bSCy Schubert  *
12*7f2fe78bSCy Schubert  * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13*7f2fe78bSCy Schubert  * distribute this software and its documentation for any purpose and
14*7f2fe78bSCy Schubert  * without fee is hereby granted, provided that the above copyright
15*7f2fe78bSCy Schubert  * notice appear in all copies and that both that copyright notice and
16*7f2fe78bSCy Schubert  * this permission notice appear in supporting documentation, and that
17*7f2fe78bSCy Schubert  * the name of M.I.T. not be used in advertising or publicity pertaining
18*7f2fe78bSCy Schubert  * to distribution of the software without specific, written prior
19*7f2fe78bSCy Schubert  * permission.  Furthermore if you modify this software you must label
20*7f2fe78bSCy Schubert  * your software as modified software and not distribute it in such a
21*7f2fe78bSCy Schubert  * fashion that it might be confused with the original M.I.T. software.
22*7f2fe78bSCy Schubert  * M.I.T. makes no representations about the suitability of
23*7f2fe78bSCy Schubert  * this software for any purpose.  It is provided "as is" without express
24*7f2fe78bSCy Schubert  * or implied warranty.
25*7f2fe78bSCy Schubert  */
26*7f2fe78bSCy Schubert 
27*7f2fe78bSCy Schubert #include <string.h>
28*7f2fe78bSCy Schubert 
29*7f2fe78bSCy Schubert #include "k5-int.h"
30*7f2fe78bSCy Schubert #include "kdb_kt.h"
31*7f2fe78bSCy Schubert 
32*7f2fe78bSCy Schubert static int
33*7f2fe78bSCy Schubert is_xrealm_tgt(krb5_context, krb5_const_principal);
34*7f2fe78bSCy Schubert 
35*7f2fe78bSCy Schubert krb5_error_code krb5_ktkdb_close (krb5_context, krb5_keytab);
36*7f2fe78bSCy Schubert 
37*7f2fe78bSCy Schubert krb5_error_code krb5_ktkdb_get_entry (krb5_context, krb5_keytab, krb5_const_principal,
38*7f2fe78bSCy Schubert                                       krb5_kvno, krb5_enctype, krb5_keytab_entry *);
39*7f2fe78bSCy Schubert 
40*7f2fe78bSCy Schubert static krb5_error_code
krb5_ktkdb_get_name(krb5_context context,krb5_keytab keytab,char * name,unsigned int namelen)41*7f2fe78bSCy Schubert krb5_ktkdb_get_name(krb5_context context, krb5_keytab keytab,
42*7f2fe78bSCy Schubert                     char *name, unsigned int namelen)
43*7f2fe78bSCy Schubert {
44*7f2fe78bSCy Schubert     if (strlcpy(name, "KDB:", namelen) >= namelen)
45*7f2fe78bSCy Schubert         return KRB5_KT_NAME_TOOLONG;
46*7f2fe78bSCy Schubert     return 0;
47*7f2fe78bSCy Schubert }
48*7f2fe78bSCy Schubert 
49*7f2fe78bSCy Schubert krb5_kt_ops krb5_kt_kdb_ops = {
50*7f2fe78bSCy Schubert     0,
51*7f2fe78bSCy Schubert     "KDB",      /* Prefix -- this string should not appear anywhere else! */
52*7f2fe78bSCy Schubert     krb5_ktkdb_resolve,         /* resolve */
53*7f2fe78bSCy Schubert     krb5_ktkdb_get_name,        /* get_name */
54*7f2fe78bSCy Schubert     krb5_ktkdb_close,           /* close */
55*7f2fe78bSCy Schubert     krb5_ktkdb_get_entry,       /* get */
56*7f2fe78bSCy Schubert     NULL,                       /* start_seq_get */
57*7f2fe78bSCy Schubert     NULL,                       /* get_next */
58*7f2fe78bSCy Schubert     NULL,                       /* end_get */
59*7f2fe78bSCy Schubert     NULL,                       /* add (extended) */
60*7f2fe78bSCy Schubert     NULL,                       /* remove (extended) */
61*7f2fe78bSCy Schubert };
62*7f2fe78bSCy Schubert 
63*7f2fe78bSCy Schubert typedef struct krb5_ktkdb_data {
64*7f2fe78bSCy Schubert     char * name;
65*7f2fe78bSCy Schubert } krb5_ktkdb_data;
66*7f2fe78bSCy Schubert 
67*7f2fe78bSCy Schubert krb5_error_code
krb5_db_register_keytab(krb5_context context)68*7f2fe78bSCy Schubert krb5_db_register_keytab(krb5_context context)
69*7f2fe78bSCy Schubert {
70*7f2fe78bSCy Schubert     return krb5_kt_register(context, &krb5_kt_kdb_ops);
71*7f2fe78bSCy Schubert }
72*7f2fe78bSCy Schubert 
73*7f2fe78bSCy Schubert krb5_error_code
krb5_ktkdb_resolve(context,name,id)74*7f2fe78bSCy Schubert krb5_ktkdb_resolve(context, name, id)
75*7f2fe78bSCy Schubert     krb5_context          context;
76*7f2fe78bSCy Schubert     const char          * name;
77*7f2fe78bSCy Schubert     krb5_keytab         * id;
78*7f2fe78bSCy Schubert {
79*7f2fe78bSCy Schubert     if ((*id = (krb5_keytab) malloc(sizeof(**id))) == NULL)
80*7f2fe78bSCy Schubert         return(ENOMEM);
81*7f2fe78bSCy Schubert     (*id)->ops = &krb5_kt_kdb_ops;
82*7f2fe78bSCy Schubert     (*id)->magic = KV5M_KEYTAB;
83*7f2fe78bSCy Schubert     return(0);
84*7f2fe78bSCy Schubert }
85*7f2fe78bSCy Schubert 
86*7f2fe78bSCy Schubert krb5_error_code
krb5_ktkdb_close(context,kt)87*7f2fe78bSCy Schubert krb5_ktkdb_close(context, kt)
88*7f2fe78bSCy Schubert     krb5_context context;
89*7f2fe78bSCy Schubert     krb5_keytab kt;
90*7f2fe78bSCy Schubert {
91*7f2fe78bSCy Schubert     /*
92*7f2fe78bSCy Schubert      * This routine is responsible for freeing all memory allocated
93*7f2fe78bSCy Schubert      * for this keytab.  There are no system resources that need
94*7f2fe78bSCy Schubert      * to be freed nor are there any open files.
95*7f2fe78bSCy Schubert      *
96*7f2fe78bSCy Schubert      * This routine should undo anything done by krb5_ktkdb_resolve().
97*7f2fe78bSCy Schubert      */
98*7f2fe78bSCy Schubert 
99*7f2fe78bSCy Schubert     kt->ops = NULL;
100*7f2fe78bSCy Schubert     free(kt);
101*7f2fe78bSCy Schubert 
102*7f2fe78bSCy Schubert     return 0;
103*7f2fe78bSCy Schubert }
104*7f2fe78bSCy Schubert 
105*7f2fe78bSCy Schubert static krb5_context ktkdb_ctx = NULL;
106*7f2fe78bSCy Schubert 
107*7f2fe78bSCy Schubert /*
108*7f2fe78bSCy Schubert  * Set a different context for use with ktkdb_get_entry().  This is
109*7f2fe78bSCy Schubert  * primarily useful for kadmind, where the gssapi library context,
110*7f2fe78bSCy Schubert  * which will be used for the keytab, will necessarily have a
111*7f2fe78bSCy Schubert  * different context than that used by the kadm5 library to access the
112*7f2fe78bSCy Schubert  * database for its own purposes.
113*7f2fe78bSCy Schubert  */
114*7f2fe78bSCy Schubert krb5_error_code
krb5_ktkdb_set_context(krb5_context ctx)115*7f2fe78bSCy Schubert krb5_ktkdb_set_context(krb5_context ctx)
116*7f2fe78bSCy Schubert {
117*7f2fe78bSCy Schubert     ktkdb_ctx = ctx;
118*7f2fe78bSCy Schubert     return 0;
119*7f2fe78bSCy Schubert }
120*7f2fe78bSCy Schubert 
121*7f2fe78bSCy Schubert krb5_error_code
krb5_ktkdb_get_entry(in_context,id,principal,kvno,enctype,entry)122*7f2fe78bSCy Schubert krb5_ktkdb_get_entry(in_context, id, principal, kvno, enctype, entry)
123*7f2fe78bSCy Schubert     krb5_context          in_context;
124*7f2fe78bSCy Schubert     krb5_keytab           id;
125*7f2fe78bSCy Schubert     krb5_const_principal  principal;
126*7f2fe78bSCy Schubert     krb5_kvno             kvno;
127*7f2fe78bSCy Schubert     krb5_enctype          enctype;
128*7f2fe78bSCy Schubert     krb5_keytab_entry   * entry;
129*7f2fe78bSCy Schubert {
130*7f2fe78bSCy Schubert     krb5_context          context;
131*7f2fe78bSCy Schubert     krb5_error_code       kerror = 0;
132*7f2fe78bSCy Schubert     krb5_key_data       * key_data;
133*7f2fe78bSCy Schubert     krb5_db_entry       * db_entry;
134*7f2fe78bSCy Schubert     int xrealm_tgt;
135*7f2fe78bSCy Schubert     krb5_boolean similar;
136*7f2fe78bSCy Schubert 
137*7f2fe78bSCy Schubert     if (ktkdb_ctx)
138*7f2fe78bSCy Schubert         context = ktkdb_ctx;
139*7f2fe78bSCy Schubert     else
140*7f2fe78bSCy Schubert         context = in_context;
141*7f2fe78bSCy Schubert 
142*7f2fe78bSCy Schubert     xrealm_tgt = is_xrealm_tgt(context, principal);
143*7f2fe78bSCy Schubert 
144*7f2fe78bSCy Schubert     /* Check whether database is inited. Open is commented */
145*7f2fe78bSCy Schubert     if ((kerror = krb5_db_inited(context)))
146*7f2fe78bSCy Schubert         return(kerror);
147*7f2fe78bSCy Schubert 
148*7f2fe78bSCy Schubert     /* get_principal */
149*7f2fe78bSCy Schubert     kerror = krb5_db_get_principal(context, principal, 0, &db_entry);
150*7f2fe78bSCy Schubert     if (kerror == KRB5_KDB_NOENTRY)
151*7f2fe78bSCy Schubert         return(KRB5_KT_NOTFOUND);
152*7f2fe78bSCy Schubert     if (kerror)
153*7f2fe78bSCy Schubert         return(kerror);
154*7f2fe78bSCy Schubert 
155*7f2fe78bSCy Schubert     if (db_entry->attributes & KRB5_KDB_DISALLOW_SVR
156*7f2fe78bSCy Schubert         || db_entry->attributes & KRB5_KDB_DISALLOW_ALL_TIX) {
157*7f2fe78bSCy Schubert         kerror = KRB5_KT_NOTFOUND;
158*7f2fe78bSCy Schubert         goto error;
159*7f2fe78bSCy Schubert     }
160*7f2fe78bSCy Schubert 
161*7f2fe78bSCy Schubert     /* match key */
162*7f2fe78bSCy Schubert     /* For cross realm tgts, we match whatever enctype is provided;
163*7f2fe78bSCy Schubert      * for other principals, we only match the first enctype that is
164*7f2fe78bSCy Schubert      * found.  Since the TGS and AS code do the same thing, then we
165*7f2fe78bSCy Schubert      * will only successfully decrypt  tickets we have issued.*/
166*7f2fe78bSCy Schubert     kerror = krb5_dbe_find_enctype(context, db_entry,
167*7f2fe78bSCy Schubert                                    xrealm_tgt?enctype:-1,
168*7f2fe78bSCy Schubert                                    -1, kvno, &key_data);
169*7f2fe78bSCy Schubert     if (kerror == KRB5_KDB_NO_MATCHING_KEY)
170*7f2fe78bSCy Schubert         kerror = KRB5_KT_KVNONOTFOUND;
171*7f2fe78bSCy Schubert     if (kerror)
172*7f2fe78bSCy Schubert         goto error;
173*7f2fe78bSCy Schubert 
174*7f2fe78bSCy Schubert 
175*7f2fe78bSCy Schubert     kerror = krb5_dbe_decrypt_key_data(context, NULL, key_data,
176*7f2fe78bSCy Schubert                                        &entry->key, NULL);
177*7f2fe78bSCy Schubert     if (kerror)
178*7f2fe78bSCy Schubert         goto error;
179*7f2fe78bSCy Schubert 
180*7f2fe78bSCy Schubert     if (enctype > 0) {
181*7f2fe78bSCy Schubert         kerror = krb5_c_enctype_compare(context, enctype,
182*7f2fe78bSCy Schubert                                         entry->key.enctype, &similar);
183*7f2fe78bSCy Schubert         if (kerror)
184*7f2fe78bSCy Schubert             goto error;
185*7f2fe78bSCy Schubert 
186*7f2fe78bSCy Schubert         if (!similar) {
187*7f2fe78bSCy Schubert             kerror = KRB5_KDB_NO_PERMITTED_KEY;
188*7f2fe78bSCy Schubert             goto error;
189*7f2fe78bSCy Schubert         }
190*7f2fe78bSCy Schubert     }
191*7f2fe78bSCy Schubert     /*
192*7f2fe78bSCy Schubert      * Coerce the enctype of the output keyblock in case we got an
193*7f2fe78bSCy Schubert      * inexact match on the enctype.
194*7f2fe78bSCy Schubert      */
195*7f2fe78bSCy Schubert     entry->key.enctype = enctype;
196*7f2fe78bSCy Schubert 
197*7f2fe78bSCy Schubert     kerror = krb5_copy_principal(context, principal, &entry->principal);
198*7f2fe78bSCy Schubert     if (kerror)
199*7f2fe78bSCy Schubert         goto error;
200*7f2fe78bSCy Schubert 
201*7f2fe78bSCy Schubert     /* Close database */
202*7f2fe78bSCy Schubert error:
203*7f2fe78bSCy Schubert     krb5_db_free_principal(context, db_entry);
204*7f2fe78bSCy Schubert     /*    krb5_db_close_database(context); */
205*7f2fe78bSCy Schubert     return(kerror);
206*7f2fe78bSCy Schubert }
207*7f2fe78bSCy Schubert 
208*7f2fe78bSCy Schubert /*
209*7f2fe78bSCy Schubert  * is_xrealm_tgt: Returns true if the principal is a cross-realm  TGT
210*7f2fe78bSCy Schubert  * principal-- a principal with first component  krbtgt and second
211*7f2fe78bSCy Schubert  * component not equal to realm.
212*7f2fe78bSCy Schubert  */
213*7f2fe78bSCy Schubert static int
is_xrealm_tgt(krb5_context context,krb5_const_principal princ)214*7f2fe78bSCy Schubert is_xrealm_tgt(krb5_context context, krb5_const_principal princ)
215*7f2fe78bSCy Schubert {
216*7f2fe78bSCy Schubert     krb5_data *dat;
217*7f2fe78bSCy Schubert     if (krb5_princ_size(context, princ) != 2)
218*7f2fe78bSCy Schubert         return 0;
219*7f2fe78bSCy Schubert     dat = krb5_princ_component(context, princ, 0);
220*7f2fe78bSCy Schubert     if (strncmp("krbtgt", dat->data, dat->length) != 0)
221*7f2fe78bSCy Schubert         return 0;
222*7f2fe78bSCy Schubert     dat = krb5_princ_component(context, princ, 1);
223*7f2fe78bSCy Schubert     if (dat->length != princ->realm.length)
224*7f2fe78bSCy Schubert         return 1;
225*7f2fe78bSCy Schubert     if (strncmp(dat->data, princ->realm.data, dat->length) == 0)
226*7f2fe78bSCy Schubert         return 0;
227*7f2fe78bSCy Schubert     return 1;
228*7f2fe78bSCy Schubert 
229*7f2fe78bSCy Schubert }
230