xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/krb5/verify_init.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: verify_init.c,v 1.2 2017/01/28 21:31:49 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 1997 - 2008 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include "krb5_locl.h"
37ca1c9b0cSelric 
38ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_init_creds_opt_init(krb5_verify_init_creds_opt * options)39ca1c9b0cSelric krb5_verify_init_creds_opt_init(krb5_verify_init_creds_opt *options)
40ca1c9b0cSelric {
41ca1c9b0cSelric     memset (options, 0, sizeof(*options));
42ca1c9b0cSelric }
43ca1c9b0cSelric 
44ca1c9b0cSelric KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_init_creds_opt_set_ap_req_nofail(krb5_verify_init_creds_opt * options,int ap_req_nofail)45ca1c9b0cSelric krb5_verify_init_creds_opt_set_ap_req_nofail(krb5_verify_init_creds_opt *options,
46ca1c9b0cSelric 					     int ap_req_nofail)
47ca1c9b0cSelric {
48ca1c9b0cSelric     options->flags |= KRB5_VERIFY_INIT_CREDS_OPT_AP_REQ_NOFAIL;
49ca1c9b0cSelric     options->ap_req_nofail = ap_req_nofail;
50ca1c9b0cSelric }
51ca1c9b0cSelric 
52ca1c9b0cSelric /*
53ca1c9b0cSelric  *
54ca1c9b0cSelric  */
55ca1c9b0cSelric 
56ca1c9b0cSelric static krb5_boolean
fail_verify_is_ok(krb5_context context,krb5_verify_init_creds_opt * options)57ca1c9b0cSelric fail_verify_is_ok (krb5_context context,
58ca1c9b0cSelric 		   krb5_verify_init_creds_opt *options)
59ca1c9b0cSelric {
60b9d004c6Schristos 
61b9d004c6Schristos     if (options && (options->flags & KRB5_VERIFY_INIT_CREDS_OPT_AP_REQ_NOFAIL)
62ca1c9b0cSelric 	&& options->ap_req_nofail != 0)
63b9d004c6Schristos 	return FALSE;
64b9d004c6Schristos 
65b9d004c6Schristos     if (krb5_config_get_bool(context,
66ca1c9b0cSelric 			     NULL,
67ca1c9b0cSelric 			     "libdefaults",
68ca1c9b0cSelric 			     "verify_ap_req_nofail",
69ca1c9b0cSelric 			     NULL))
70ca1c9b0cSelric 	return FALSE;
71b9d004c6Schristos 
72ca1c9b0cSelric     return TRUE;
73ca1c9b0cSelric }
74ca1c9b0cSelric 
75ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verify_init_creds(krb5_context context,krb5_creds * creds,krb5_principal ap_req_server,krb5_keytab ap_req_keytab,krb5_ccache * ccache,krb5_verify_init_creds_opt * options)76ca1c9b0cSelric krb5_verify_init_creds(krb5_context context,
77ca1c9b0cSelric 		       krb5_creds *creds,
78ca1c9b0cSelric 		       krb5_principal ap_req_server,
79ca1c9b0cSelric 		       krb5_keytab ap_req_keytab,
80ca1c9b0cSelric 		       krb5_ccache *ccache,
81ca1c9b0cSelric 		       krb5_verify_init_creds_opt *options)
82ca1c9b0cSelric {
83ca1c9b0cSelric     krb5_error_code ret;
84ca1c9b0cSelric     krb5_data req;
85ca1c9b0cSelric     krb5_ccache local_ccache = NULL;
86ca1c9b0cSelric     krb5_creds *new_creds = NULL;
87ca1c9b0cSelric     krb5_auth_context auth_context = NULL;
88ca1c9b0cSelric     krb5_principal server = NULL;
89ca1c9b0cSelric     krb5_keytab keytab = NULL;
90ca1c9b0cSelric 
91ca1c9b0cSelric     krb5_data_zero (&req);
92ca1c9b0cSelric 
93ca1c9b0cSelric     if (ap_req_server == NULL) {
94ca1c9b0cSelric 	char local_hostname[MAXHOSTNAMELEN];
95ca1c9b0cSelric 
96ca1c9b0cSelric 	if (gethostname (local_hostname, sizeof(local_hostname)) < 0) {
97ca1c9b0cSelric 	    ret = errno;
98ca1c9b0cSelric 	    krb5_set_error_message (context, ret, "gethostname: %s",
99ca1c9b0cSelric 				    strerror(ret));
100ca1c9b0cSelric 	    return ret;
101ca1c9b0cSelric 	}
102ca1c9b0cSelric 
103ca1c9b0cSelric 	ret = krb5_sname_to_principal (context,
104ca1c9b0cSelric 				       local_hostname,
105ca1c9b0cSelric 				       "host",
106ca1c9b0cSelric 				       KRB5_NT_SRV_HST,
107ca1c9b0cSelric 				       &server);
108ca1c9b0cSelric 	if (ret)
109ca1c9b0cSelric 	    goto cleanup;
110ca1c9b0cSelric     } else
111ca1c9b0cSelric 	server = ap_req_server;
112ca1c9b0cSelric 
113ca1c9b0cSelric     if (ap_req_keytab == NULL) {
114ca1c9b0cSelric 	ret = krb5_kt_default (context, &keytab);
115ca1c9b0cSelric 	if (ret)
116ca1c9b0cSelric 	    goto cleanup;
117ca1c9b0cSelric     } else
118ca1c9b0cSelric 	keytab = ap_req_keytab;
119ca1c9b0cSelric 
120ca1c9b0cSelric     if (ccache && *ccache)
121ca1c9b0cSelric 	local_ccache = *ccache;
122ca1c9b0cSelric     else {
123ca1c9b0cSelric 	ret = krb5_cc_new_unique(context, krb5_cc_type_memory,
124ca1c9b0cSelric 				 NULL, &local_ccache);
125ca1c9b0cSelric 	if (ret)
126ca1c9b0cSelric 	    goto cleanup;
127ca1c9b0cSelric 	ret = krb5_cc_initialize (context,
128ca1c9b0cSelric 				  local_ccache,
129ca1c9b0cSelric 				  creds->client);
130ca1c9b0cSelric 	if (ret)
131ca1c9b0cSelric 	    goto cleanup;
132ca1c9b0cSelric 	ret = krb5_cc_store_cred (context,
133ca1c9b0cSelric 				  local_ccache,
134ca1c9b0cSelric 				  creds);
135ca1c9b0cSelric 	if (ret)
136ca1c9b0cSelric 	    goto cleanup;
137ca1c9b0cSelric     }
138ca1c9b0cSelric 
139ca1c9b0cSelric     if (!krb5_principal_compare (context, server, creds->server)) {
140ca1c9b0cSelric 	krb5_creds match_cred;
141ca1c9b0cSelric 
142ca1c9b0cSelric 	memset (&match_cred, 0, sizeof(match_cred));
143ca1c9b0cSelric 
144ca1c9b0cSelric 	match_cred.client = creds->client;
145ca1c9b0cSelric 	match_cred.server = server;
146ca1c9b0cSelric 
147ca1c9b0cSelric 	ret = krb5_get_credentials (context,
148ca1c9b0cSelric 				    0,
149ca1c9b0cSelric 				    local_ccache,
150ca1c9b0cSelric 				    &match_cred,
151ca1c9b0cSelric 				    &new_creds);
152ca1c9b0cSelric 	if (ret) {
153ca1c9b0cSelric 	    if (fail_verify_is_ok (context, options))
154ca1c9b0cSelric 		ret = 0;
155ca1c9b0cSelric 	    goto cleanup;
156ca1c9b0cSelric 	}
157ca1c9b0cSelric 	creds = new_creds;
158ca1c9b0cSelric     }
159ca1c9b0cSelric 
160ca1c9b0cSelric     ret = krb5_mk_req_extended (context,
161ca1c9b0cSelric 				&auth_context,
162ca1c9b0cSelric 				0,
163ca1c9b0cSelric 				NULL,
164ca1c9b0cSelric 				creds,
165ca1c9b0cSelric 				&req);
166ca1c9b0cSelric 
167ca1c9b0cSelric     krb5_auth_con_free (context, auth_context);
168ca1c9b0cSelric     auth_context = NULL;
169ca1c9b0cSelric 
170ca1c9b0cSelric     if (ret)
171ca1c9b0cSelric 	goto cleanup;
172ca1c9b0cSelric 
173ca1c9b0cSelric     ret = krb5_rd_req (context,
174ca1c9b0cSelric 		       &auth_context,
175ca1c9b0cSelric 		       &req,
176ca1c9b0cSelric 		       server,
177ca1c9b0cSelric 		       keytab,
178ca1c9b0cSelric 		       0,
179ca1c9b0cSelric 		       NULL);
180ca1c9b0cSelric 
181ca1c9b0cSelric     if (ret == KRB5_KT_NOTFOUND && fail_verify_is_ok (context, options))
182ca1c9b0cSelric 	ret = 0;
183ca1c9b0cSelric cleanup:
184ca1c9b0cSelric     if (auth_context)
185ca1c9b0cSelric 	krb5_auth_con_free (context, auth_context);
186ca1c9b0cSelric     krb5_data_free (&req);
187ca1c9b0cSelric     if (new_creds != NULL)
188ca1c9b0cSelric 	krb5_free_creds (context, new_creds);
189ca1c9b0cSelric     if (ap_req_server == NULL && server)
190ca1c9b0cSelric 	krb5_free_principal (context, server);
191ca1c9b0cSelric     if (ap_req_keytab == NULL && keytab)
192ca1c9b0cSelric 	krb5_kt_close (context, keytab);
193ca1c9b0cSelric     if (local_ccache != NULL
194ca1c9b0cSelric 	&&
195ca1c9b0cSelric 	(ccache == NULL
196ca1c9b0cSelric 	 || (ret != 0 && *ccache == NULL)))
197ca1c9b0cSelric 	krb5_cc_destroy (context, local_ccache);
198ca1c9b0cSelric 
199ca1c9b0cSelric     if (ret == 0 && ccache != NULL && *ccache == NULL)
200ca1c9b0cSelric 	*ccache = local_ccache;
201ca1c9b0cSelric 
202ca1c9b0cSelric     return ret;
203ca1c9b0cSelric }
204ca1c9b0cSelric 
205ca1c9b0cSelric /**
206ca1c9b0cSelric  * Validate the newly fetch credential, see also krb5_verify_init_creds().
207ca1c9b0cSelric  *
208ca1c9b0cSelric  * @param context a Kerberos 5 context
209ca1c9b0cSelric  * @param creds the credentials to verify
210ca1c9b0cSelric  * @param client the client name to match up
211ca1c9b0cSelric  * @param ccache the credential cache to use
212ca1c9b0cSelric  * @param service a service name to use, used with
213ca1c9b0cSelric  *        krb5_sname_to_principal() to build a hostname to use to
214ca1c9b0cSelric  *        verify.
215ca1c9b0cSelric  *
216ca1c9b0cSelric  * @ingroup krb5_ccache
217ca1c9b0cSelric  */
218ca1c9b0cSelric 
219ca1c9b0cSelric KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_get_validated_creds(krb5_context context,krb5_creds * creds,krb5_principal client,krb5_ccache ccache,char * service)220ca1c9b0cSelric krb5_get_validated_creds(krb5_context context,
221ca1c9b0cSelric 			 krb5_creds *creds,
222ca1c9b0cSelric 			 krb5_principal client,
223ca1c9b0cSelric 			 krb5_ccache ccache,
224ca1c9b0cSelric 			 char *service)
225ca1c9b0cSelric {
226ca1c9b0cSelric     krb5_verify_init_creds_opt vopt;
227ca1c9b0cSelric     krb5_principal server;
228ca1c9b0cSelric     krb5_error_code ret;
229ca1c9b0cSelric 
230ca1c9b0cSelric     if (krb5_principal_compare(context, creds->client, client) != TRUE) {
231ca1c9b0cSelric 	krb5_set_error_message(context, KRB5_PRINC_NOMATCH,
232ca1c9b0cSelric 			       N_("Validation credentials and client "
233ca1c9b0cSelric 				  "doesn't match", ""));
234ca1c9b0cSelric 	return KRB5_PRINC_NOMATCH;
235ca1c9b0cSelric     }
236ca1c9b0cSelric 
237ca1c9b0cSelric     ret = krb5_sname_to_principal (context, NULL, service,
238ca1c9b0cSelric 				   KRB5_NT_SRV_HST, &server);
239ca1c9b0cSelric     if(ret)
240ca1c9b0cSelric 	return ret;
241ca1c9b0cSelric 
242ca1c9b0cSelric     krb5_verify_init_creds_opt_init(&vopt);
243ca1c9b0cSelric 
244ca1c9b0cSelric     ret = krb5_verify_init_creds(context, creds, server, NULL, NULL, &vopt);
245ca1c9b0cSelric     krb5_free_principal(context, server);
246ca1c9b0cSelric 
247ca1c9b0cSelric     return ret;
248ca1c9b0cSelric }
249