xref: /minix3/crypto/external/bsd/heimdal/dist/lib/krb5/verify_user.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /*	$NetBSD: verify_user.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $	*/
2ebfedea0SLionel Sambuc 
3ebfedea0SLionel Sambuc /*
4ebfedea0SLionel Sambuc  * Copyright (c) 1997-2004 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 static krb5_error_code
verify_common(krb5_context context,krb5_principal principal,krb5_ccache ccache,krb5_keytab keytab,krb5_boolean secure,const char * service,krb5_creds cred)39ebfedea0SLionel Sambuc verify_common (krb5_context context,
40ebfedea0SLionel Sambuc 	       krb5_principal principal,
41ebfedea0SLionel Sambuc 	       krb5_ccache ccache,
42ebfedea0SLionel Sambuc 	       krb5_keytab keytab,
43ebfedea0SLionel Sambuc 	       krb5_boolean secure,
44ebfedea0SLionel Sambuc 	       const char *service,
45ebfedea0SLionel Sambuc 	       krb5_creds cred)
46ebfedea0SLionel Sambuc {
47ebfedea0SLionel Sambuc     krb5_error_code ret;
48ebfedea0SLionel Sambuc     krb5_principal server;
49ebfedea0SLionel Sambuc     krb5_verify_init_creds_opt vopt;
50ebfedea0SLionel Sambuc     krb5_ccache id;
51ebfedea0SLionel Sambuc 
52ebfedea0SLionel Sambuc     ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST,
53ebfedea0SLionel Sambuc 				   &server);
54ebfedea0SLionel Sambuc     if(ret)
55ebfedea0SLionel Sambuc 	return ret;
56ebfedea0SLionel Sambuc 
57ebfedea0SLionel Sambuc     krb5_verify_init_creds_opt_init(&vopt);
58ebfedea0SLionel Sambuc     krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure);
59ebfedea0SLionel Sambuc 
60ebfedea0SLionel Sambuc     ret = krb5_verify_init_creds(context,
61ebfedea0SLionel Sambuc 				 &cred,
62ebfedea0SLionel Sambuc 				 server,
63ebfedea0SLionel Sambuc 				 keytab,
64ebfedea0SLionel Sambuc 				 NULL,
65ebfedea0SLionel Sambuc 				 &vopt);
66ebfedea0SLionel Sambuc     krb5_free_principal(context, server);
67ebfedea0SLionel Sambuc     if(ret)
68ebfedea0SLionel Sambuc 	return ret;
69ebfedea0SLionel Sambuc     if(ccache == NULL)
70ebfedea0SLionel Sambuc 	ret = krb5_cc_default (context, &id);
71ebfedea0SLionel Sambuc     else
72ebfedea0SLionel Sambuc 	id = ccache;
73ebfedea0SLionel Sambuc     if(ret == 0){
74ebfedea0SLionel Sambuc 	ret = krb5_cc_initialize(context, id, principal);
75ebfedea0SLionel Sambuc 	if(ret == 0){
76ebfedea0SLionel Sambuc 	    ret = krb5_cc_store_cred(context, id, &cred);
77ebfedea0SLionel Sambuc 	}
78ebfedea0SLionel Sambuc 	if(ccache == NULL)
79ebfedea0SLionel Sambuc 	    krb5_cc_close(context, id);
80ebfedea0SLionel Sambuc     }
81ebfedea0SLionel Sambuc     krb5_free_cred_contents(context, &cred);
82ebfedea0SLionel Sambuc     return ret;
83ebfedea0SLionel Sambuc }
84ebfedea0SLionel Sambuc 
85ebfedea0SLionel Sambuc /*
86ebfedea0SLionel Sambuc  * Verify user `principal' with `password'.
87ebfedea0SLionel Sambuc  *
88ebfedea0SLionel Sambuc  * If `secure', also verify against local service key for `service'.
89ebfedea0SLionel Sambuc  *
90ebfedea0SLionel Sambuc  * As a side effect, fresh tickets are obtained and stored in `ccache'.
91ebfedea0SLionel Sambuc  */
92ebfedea0SLionel Sambuc 
93ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_init(krb5_verify_opt * opt)94ebfedea0SLionel Sambuc krb5_verify_opt_init(krb5_verify_opt *opt)
95ebfedea0SLionel Sambuc {
96ebfedea0SLionel Sambuc     memset(opt, 0, sizeof(*opt));
97ebfedea0SLionel Sambuc     opt->secure = TRUE;
98ebfedea0SLionel Sambuc     opt->service = "host";
99ebfedea0SLionel Sambuc }
100ebfedea0SLionel Sambuc 
101ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION int KRB5_LIB_CALL
krb5_verify_opt_alloc(krb5_context context,krb5_verify_opt ** opt)102ebfedea0SLionel Sambuc krb5_verify_opt_alloc(krb5_context context, krb5_verify_opt **opt)
103ebfedea0SLionel Sambuc {
104ebfedea0SLionel Sambuc     *opt = calloc(1, sizeof(**opt));
105ebfedea0SLionel Sambuc     if ((*opt) == NULL) {
106ebfedea0SLionel Sambuc 	krb5_set_error_message(context, ENOMEM,
107ebfedea0SLionel Sambuc 			       N_("malloc: out of memory", ""));
108ebfedea0SLionel Sambuc 	return ENOMEM;
109ebfedea0SLionel Sambuc     }
110ebfedea0SLionel Sambuc     krb5_verify_opt_init(*opt);
111ebfedea0SLionel Sambuc     return 0;
112ebfedea0SLionel Sambuc }
113ebfedea0SLionel Sambuc 
114ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_free(krb5_verify_opt * opt)115ebfedea0SLionel Sambuc krb5_verify_opt_free(krb5_verify_opt *opt)
116ebfedea0SLionel Sambuc {
117ebfedea0SLionel Sambuc     free(opt);
118ebfedea0SLionel Sambuc }
119ebfedea0SLionel Sambuc 
120ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_ccache(krb5_verify_opt * opt,krb5_ccache ccache)121ebfedea0SLionel Sambuc krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache)
122ebfedea0SLionel Sambuc {
123ebfedea0SLionel Sambuc     opt->ccache = ccache;
124ebfedea0SLionel Sambuc }
125ebfedea0SLionel Sambuc 
126ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_keytab(krb5_verify_opt * opt,krb5_keytab keytab)127ebfedea0SLionel Sambuc krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab)
128ebfedea0SLionel Sambuc {
129ebfedea0SLionel Sambuc     opt->keytab = keytab;
130ebfedea0SLionel Sambuc }
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_secure(krb5_verify_opt * opt,krb5_boolean secure)133ebfedea0SLionel Sambuc krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure)
134ebfedea0SLionel Sambuc {
135ebfedea0SLionel Sambuc     opt->secure = secure;
136ebfedea0SLionel Sambuc }
137ebfedea0SLionel Sambuc 
138ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_service(krb5_verify_opt * opt,const char * service)139ebfedea0SLionel Sambuc krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service)
140ebfedea0SLionel Sambuc {
141ebfedea0SLionel Sambuc     opt->service = service;
142ebfedea0SLionel Sambuc }
143ebfedea0SLionel Sambuc 
144ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_flags(krb5_verify_opt * opt,unsigned int flags)145ebfedea0SLionel Sambuc krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags)
146ebfedea0SLionel Sambuc {
147ebfedea0SLionel Sambuc     opt->flags |= flags;
148ebfedea0SLionel Sambuc }
149ebfedea0SLionel Sambuc 
150ebfedea0SLionel Sambuc static krb5_error_code
verify_user_opt_int(krb5_context context,krb5_principal principal,const char * password,krb5_verify_opt * vopt)151ebfedea0SLionel Sambuc verify_user_opt_int(krb5_context context,
152ebfedea0SLionel Sambuc 		    krb5_principal principal,
153ebfedea0SLionel Sambuc 		    const char *password,
154ebfedea0SLionel Sambuc 		    krb5_verify_opt *vopt)
155ebfedea0SLionel Sambuc 
156ebfedea0SLionel Sambuc {
157ebfedea0SLionel Sambuc     krb5_error_code ret;
158ebfedea0SLionel Sambuc     krb5_get_init_creds_opt *opt;
159ebfedea0SLionel Sambuc     krb5_creds cred;
160ebfedea0SLionel Sambuc 
161ebfedea0SLionel Sambuc     ret = krb5_get_init_creds_opt_alloc (context, &opt);
162ebfedea0SLionel Sambuc     if (ret)
163ebfedea0SLionel Sambuc 	return ret;
164ebfedea0SLionel Sambuc     krb5_get_init_creds_opt_set_default_flags(context, NULL,
165ebfedea0SLionel Sambuc 					      krb5_principal_get_realm(context, principal),
166ebfedea0SLionel Sambuc 					      opt);
167ebfedea0SLionel Sambuc     ret = krb5_get_init_creds_password (context,
168ebfedea0SLionel Sambuc 					&cred,
169ebfedea0SLionel Sambuc 					principal,
170ebfedea0SLionel Sambuc 					password,
171ebfedea0SLionel Sambuc 					krb5_prompter_posix,
172ebfedea0SLionel Sambuc 					NULL,
173ebfedea0SLionel Sambuc 					0,
174ebfedea0SLionel Sambuc 					NULL,
175ebfedea0SLionel Sambuc 					opt);
176ebfedea0SLionel Sambuc     krb5_get_init_creds_opt_free(context, opt);
177ebfedea0SLionel Sambuc     if(ret)
178ebfedea0SLionel Sambuc 	return ret;
179ebfedea0SLionel Sambuc #define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D))
180ebfedea0SLionel Sambuc     return verify_common (context, principal, OPT(ccache, NULL),
181ebfedea0SLionel Sambuc 			  OPT(keytab, NULL), vopt ? vopt->secure : TRUE,
182ebfedea0SLionel Sambuc 			  OPT(service, "host"), cred);
183ebfedea0SLionel Sambuc #undef OPT
184ebfedea0SLionel Sambuc }
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verify_user_opt(krb5_context context,krb5_principal principal,const char * password,krb5_verify_opt * opt)187ebfedea0SLionel Sambuc krb5_verify_user_opt(krb5_context context,
188ebfedea0SLionel Sambuc 		     krb5_principal principal,
189ebfedea0SLionel Sambuc 		     const char *password,
190ebfedea0SLionel Sambuc 		     krb5_verify_opt *opt)
191ebfedea0SLionel Sambuc {
192ebfedea0SLionel Sambuc     krb5_error_code ret;
193ebfedea0SLionel Sambuc 
194ebfedea0SLionel Sambuc     if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) {
195ebfedea0SLionel Sambuc 	krb5_realm *realms, *r;
196ebfedea0SLionel Sambuc 	ret = krb5_get_default_realms (context, &realms);
197ebfedea0SLionel Sambuc 	if (ret)
198ebfedea0SLionel Sambuc 	    return ret;
199ebfedea0SLionel Sambuc 	ret = KRB5_CONFIG_NODEFREALM;
200ebfedea0SLionel Sambuc 
201ebfedea0SLionel Sambuc 	for (r = realms; *r != NULL && ret != 0; ++r) {
202ebfedea0SLionel Sambuc 	    ret = krb5_principal_set_realm(context, principal, *r);
203ebfedea0SLionel Sambuc 	    if (ret) {
204ebfedea0SLionel Sambuc 		krb5_free_host_realm (context, realms);
205ebfedea0SLionel Sambuc 		return ret;
206ebfedea0SLionel Sambuc 	    }
207ebfedea0SLionel Sambuc 
208ebfedea0SLionel Sambuc 	    ret = verify_user_opt_int(context, principal, password, opt);
209ebfedea0SLionel Sambuc 	}
210ebfedea0SLionel Sambuc 	krb5_free_host_realm (context, realms);
211ebfedea0SLionel Sambuc 	if(ret)
212ebfedea0SLionel Sambuc 	    return ret;
213ebfedea0SLionel Sambuc     } else
214ebfedea0SLionel Sambuc 	ret = verify_user_opt_int(context, principal, password, opt);
215ebfedea0SLionel Sambuc     return ret;
216ebfedea0SLionel Sambuc }
217ebfedea0SLionel Sambuc 
218ebfedea0SLionel Sambuc /* compat function that calls above */
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verify_user(krb5_context context,krb5_principal principal,krb5_ccache ccache,const char * password,krb5_boolean secure,const char * service)221ebfedea0SLionel Sambuc krb5_verify_user(krb5_context context,
222ebfedea0SLionel Sambuc 		 krb5_principal principal,
223ebfedea0SLionel Sambuc 		 krb5_ccache ccache,
224ebfedea0SLionel Sambuc 		 const char *password,
225ebfedea0SLionel Sambuc 		 krb5_boolean secure,
226ebfedea0SLionel Sambuc 		 const char *service)
227ebfedea0SLionel Sambuc {
228ebfedea0SLionel Sambuc     krb5_verify_opt opt;
229ebfedea0SLionel Sambuc 
230ebfedea0SLionel Sambuc     krb5_verify_opt_init(&opt);
231ebfedea0SLionel Sambuc 
232ebfedea0SLionel Sambuc     krb5_verify_opt_set_ccache(&opt, ccache);
233ebfedea0SLionel Sambuc     krb5_verify_opt_set_secure(&opt, secure);
234ebfedea0SLionel Sambuc     krb5_verify_opt_set_service(&opt, service);
235ebfedea0SLionel Sambuc 
236ebfedea0SLionel Sambuc     return krb5_verify_user_opt(context, principal, password, &opt);
237ebfedea0SLionel Sambuc }
238ebfedea0SLionel Sambuc 
239ebfedea0SLionel Sambuc /*
240ebfedea0SLionel Sambuc  * A variant of `krb5_verify_user'.  The realm of `principal' is
241ebfedea0SLionel Sambuc  * ignored and all the local realms are tried.
242ebfedea0SLionel Sambuc  */
243ebfedea0SLionel Sambuc 
244ebfedea0SLionel Sambuc KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_verify_user_lrealm(krb5_context context,krb5_principal principal,krb5_ccache ccache,const char * password,krb5_boolean secure,const char * service)245ebfedea0SLionel Sambuc krb5_verify_user_lrealm(krb5_context context,
246ebfedea0SLionel Sambuc 			krb5_principal principal,
247ebfedea0SLionel Sambuc 			krb5_ccache ccache,
248ebfedea0SLionel Sambuc 			const char *password,
249ebfedea0SLionel Sambuc 			krb5_boolean secure,
250ebfedea0SLionel Sambuc 			const char *service)
251ebfedea0SLionel Sambuc {
252ebfedea0SLionel Sambuc     krb5_verify_opt opt;
253ebfedea0SLionel Sambuc 
254ebfedea0SLionel Sambuc     krb5_verify_opt_init(&opt);
255ebfedea0SLionel Sambuc 
256ebfedea0SLionel Sambuc     krb5_verify_opt_set_ccache(&opt, ccache);
257ebfedea0SLionel Sambuc     krb5_verify_opt_set_secure(&opt, secure);
258ebfedea0SLionel Sambuc     krb5_verify_opt_set_service(&opt, service);
259ebfedea0SLionel Sambuc     krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS);
260ebfedea0SLionel Sambuc 
261ebfedea0SLionel Sambuc     return krb5_verify_user_opt(context, principal, password, &opt);
262ebfedea0SLionel Sambuc }
263