1*1c9681d1Schristos /* $NetBSD: misc.c,v 1.2 2017/01/28 21:31:44 christos Exp $ */
2f59d82ffSelric
3f59d82ffSelric /*
4f59d82ffSelric * Copyright (c) 1997 - 2001 Kungliga Tekniska Högskolan
5f59d82ffSelric * (Royal Institute of Technology, Stockholm, Sweden).
6f59d82ffSelric * All rights reserved.
7f59d82ffSelric *
8f59d82ffSelric * Redistribution and use in source and binary forms, with or without
9f59d82ffSelric * modification, are permitted provided that the following conditions
10f59d82ffSelric * are met:
11f59d82ffSelric *
12f59d82ffSelric * 1. Redistributions of source code must retain the above copyright
13f59d82ffSelric * notice, this list of conditions and the following disclaimer.
14f59d82ffSelric *
15f59d82ffSelric * 2. Redistributions in binary form must reproduce the above copyright
16f59d82ffSelric * notice, this list of conditions and the following disclaimer in the
17f59d82ffSelric * documentation and/or other materials provided with the distribution.
18f59d82ffSelric *
19f59d82ffSelric * 3. Neither the name of the Institute nor the names of its contributors
20f59d82ffSelric * may be used to endorse or promote products derived from this software
21f59d82ffSelric * without specific prior written permission.
22f59d82ffSelric *
23f59d82ffSelric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24f59d82ffSelric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f59d82ffSelric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f59d82ffSelric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27f59d82ffSelric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f59d82ffSelric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f59d82ffSelric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f59d82ffSelric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f59d82ffSelric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f59d82ffSelric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f59d82ffSelric * SUCH DAMAGE.
34f59d82ffSelric */
35f59d82ffSelric
36f59d82ffSelric #include "kdc_locl.h"
37f59d82ffSelric
38e0895134Schristos static int
name_type_ok(krb5_context context,krb5_kdc_configuration * config,krb5_const_principal principal)39e0895134Schristos name_type_ok(krb5_context context,
40e0895134Schristos krb5_kdc_configuration *config,
41e0895134Schristos krb5_const_principal principal)
42e0895134Schristos {
43e0895134Schristos int nt = krb5_principal_get_type(context, principal);
44e0895134Schristos
45e0895134Schristos if (!krb5_principal_is_krbtgt(context, principal))
46e0895134Schristos return 1;
47e0895134Schristos if (nt == KRB5_NT_SRV_INST || nt == KRB5_NT_UNKNOWN)
48e0895134Schristos return 1;
49e0895134Schristos if (config->strict_nametypes == 0)
50e0895134Schristos return 1;
51e0895134Schristos return 0;
52e0895134Schristos }
53e0895134Schristos
54f59d82ffSelric struct timeval _kdc_now;
55f59d82ffSelric
56f59d82ffSelric krb5_error_code
_kdc_db_fetch(krb5_context context,krb5_kdc_configuration * config,krb5_const_principal principal,unsigned flags,krb5uint32 * kvno_ptr,HDB ** db,hdb_entry_ex ** h)57f59d82ffSelric _kdc_db_fetch(krb5_context context,
58f59d82ffSelric krb5_kdc_configuration *config,
59f59d82ffSelric krb5_const_principal principal,
60f59d82ffSelric unsigned flags,
61603f2576Spettai krb5uint32 *kvno_ptr,
62f59d82ffSelric HDB **db,
63f59d82ffSelric hdb_entry_ex **h)
64f59d82ffSelric {
65e0895134Schristos hdb_entry_ex *ent = NULL;
66f59d82ffSelric krb5_error_code ret = HDB_ERR_NOENTRY;
67f59d82ffSelric int i;
68f59d82ffSelric unsigned kvno = 0;
69e0895134Schristos krb5_principal enterprise_principal = NULL;
70e0895134Schristos krb5_const_principal princ;
71f59d82ffSelric
72e0895134Schristos *h = NULL;
73e0895134Schristos
74e0895134Schristos if (!name_type_ok(context, config, principal))
75e0895134Schristos goto out2;
76e0895134Schristos
77e0895134Schristos if (kvno_ptr != NULL && *kvno_ptr != 0) {
78f59d82ffSelric kvno = *kvno_ptr;
79f59d82ffSelric flags |= HDB_F_KVNO_SPECIFIED;
80e0895134Schristos } else {
81e0895134Schristos flags |= HDB_F_ALL_KVNOS;
82f59d82ffSelric }
83f59d82ffSelric
84f59d82ffSelric ent = calloc(1, sizeof (*ent));
85e0895134Schristos if (ent == NULL)
86e0895134Schristos return krb5_enomem(context);
87f59d82ffSelric
88e0895134Schristos if (principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
89f59d82ffSelric if (principal->name.name_string.len != 1) {
90f59d82ffSelric ret = KRB5_PARSE_MALFORMED;
91f59d82ffSelric krb5_set_error_message(context, ret,
92f59d82ffSelric "malformed request: "
93f59d82ffSelric "enterprise name with %d name components",
94f59d82ffSelric principal->name.name_string.len);
95e0895134Schristos goto out;
96f59d82ffSelric }
97f59d82ffSelric ret = krb5_parse_name(context, principal->name.name_string.val[0],
98f59d82ffSelric &enterprise_principal);
99e0895134Schristos if (ret)
100e0895134Schristos goto out;
101f59d82ffSelric }
102f59d82ffSelric
103e0895134Schristos for (i = 0; i < config->num_db; i++) {
104f59d82ffSelric ret = config->db[i]->hdb_open(context, config->db[i], O_RDONLY, 0);
105f59d82ffSelric if (ret) {
106f59d82ffSelric const char *msg = krb5_get_error_message(context, ret);
107f59d82ffSelric kdc_log(context, config, 0, "Failed to open database: %s", msg);
108f59d82ffSelric krb5_free_error_message(context, msg);
109f59d82ffSelric continue;
110f59d82ffSelric }
111f59d82ffSelric
112e0895134Schristos princ = principal;
113e0895134Schristos if (!(config->db[i]->hdb_capability_flags & HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL) && enterprise_principal)
114e0895134Schristos princ = enterprise_principal;
115e0895134Schristos
116f59d82ffSelric ret = config->db[i]->hdb_fetch_kvno(context,
117f59d82ffSelric config->db[i],
118e0895134Schristos princ,
119f59d82ffSelric flags | HDB_F_DECRYPT,
120f59d82ffSelric kvno,
121f59d82ffSelric ent);
122f59d82ffSelric config->db[i]->hdb_close(context, config->db[i]);
123e0895134Schristos
124e0895134Schristos switch (ret) {
125e0895134Schristos case HDB_ERR_WRONG_REALM:
126e0895134Schristos /*
127e0895134Schristos * the ent->entry.principal just contains hints for the client
128e0895134Schristos * to retry. This is important for enterprise principal routing
129e0895134Schristos * between trusts.
130e0895134Schristos */
131e0895134Schristos /* fall through */
132e0895134Schristos case 0:
133f59d82ffSelric if (db)
134f59d82ffSelric *db = config->db[i];
135f59d82ffSelric *h = ent;
136e0895134Schristos ent = NULL;
137e0895134Schristos goto out;
138e0895134Schristos
139e0895134Schristos case HDB_ERR_NOENTRY:
140e0895134Schristos /* Check the other databases */
141e0895134Schristos continue;
142e0895134Schristos
143e0895134Schristos default:
144e0895134Schristos /*
145e0895134Schristos * This is really important, because errors like
146e0895134Schristos * HDB_ERR_NOT_FOUND_HERE (used to indicate to Samba that
147e0895134Schristos * the RODC on which this code is running does not have
148e0895134Schristos * the key we need, and so a proxy to the KDC is required)
149e0895134Schristos * have specific meaning, and need to be propogated up.
150e0895134Schristos */
151e0895134Schristos goto out;
152f59d82ffSelric }
153f59d82ffSelric }
154e0895134Schristos
155e0895134Schristos out2:
156e0895134Schristos if (ret == HDB_ERR_NOENTRY) {
157e0895134Schristos krb5_set_error_message(context, ret, "no such entry found in hdb");
158e0895134Schristos }
159e0895134Schristos out:
160e0895134Schristos krb5_free_principal(context, enterprise_principal);
161f59d82ffSelric free(ent);
162f59d82ffSelric return ret;
163f59d82ffSelric }
164f59d82ffSelric
165f59d82ffSelric void
_kdc_free_ent(krb5_context context,hdb_entry_ex * ent)166f59d82ffSelric _kdc_free_ent(krb5_context context, hdb_entry_ex *ent)
167f59d82ffSelric {
168f59d82ffSelric hdb_free_entry (context, ent);
169f59d82ffSelric free (ent);
170f59d82ffSelric }
171f59d82ffSelric
172f59d82ffSelric /*
173f59d82ffSelric * Use the order list of preferred encryption types and sort the
174f59d82ffSelric * available keys and return the most preferred key.
175f59d82ffSelric */
176f59d82ffSelric
177f59d82ffSelric krb5_error_code
_kdc_get_preferred_key(krb5_context context,krb5_kdc_configuration * config,hdb_entry_ex * h,const char * name,krb5_enctype * enctype,Key ** key)178f59d82ffSelric _kdc_get_preferred_key(krb5_context context,
179f59d82ffSelric krb5_kdc_configuration *config,
180f59d82ffSelric hdb_entry_ex *h,
181f59d82ffSelric const char *name,
182f59d82ffSelric krb5_enctype *enctype,
183f59d82ffSelric Key **key)
184f59d82ffSelric {
185f59d82ffSelric krb5_error_code ret;
186f59d82ffSelric int i;
187f59d82ffSelric
188603f2576Spettai if (config->use_strongest_server_key) {
189603f2576Spettai const krb5_enctype *p = krb5_kerberos_enctypes(context);
190f59d82ffSelric
191603f2576Spettai for (i = 0; p[i] != (krb5_enctype)ETYPE_NULL; i++) {
192603f2576Spettai if (krb5_enctype_valid(context, p[i]) != 0 &&
193603f2576Spettai !_kdc_is_weak_exception(h->entry.principal, p[i]))
194f59d82ffSelric continue;
195e0895134Schristos ret = hdb_enctype2key(context, &h->entry, NULL, p[i], key);
196603f2576Spettai if (ret != 0)
197603f2576Spettai continue;
198603f2576Spettai if (enctype != NULL)
199f59d82ffSelric *enctype = p[i];
200f59d82ffSelric return 0;
201f59d82ffSelric }
202603f2576Spettai } else {
203603f2576Spettai *key = NULL;
204603f2576Spettai
205603f2576Spettai for (i = 0; i < h->entry.keys.len; i++) {
206603f2576Spettai if (krb5_enctype_valid(context, h->entry.keys.val[i].key.keytype) != 0 &&
207603f2576Spettai !_kdc_is_weak_exception(h->entry.principal, h->entry.keys.val[i].key.keytype))
208603f2576Spettai continue;
209e0895134Schristos ret = hdb_enctype2key(context, &h->entry, NULL,
210603f2576Spettai h->entry.keys.val[i].key.keytype, key);
211603f2576Spettai if (ret != 0)
212603f2576Spettai continue;
213603f2576Spettai if (enctype != NULL)
214603f2576Spettai *enctype = (*key)->key.keytype;
215603f2576Spettai return 0;
216603f2576Spettai }
217f59d82ffSelric }
218f59d82ffSelric
219f59d82ffSelric krb5_set_error_message(context, EINVAL,
220f59d82ffSelric "No valid kerberos key found for %s", name);
221603f2576Spettai return EINVAL; /* XXX */
222f59d82ffSelric }
223f59d82ffSelric
224