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