Use anon realm for anonymous PKINIT
[heimdal.git] / lib / krb5 / verify_user.c
blob663196b29b15a210aa2e826d651a134658c3207c
1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
34 #include "krb5_locl.h"
36 static krb5_error_code
37 verify_common (krb5_context context,
38 krb5_principal principal,
39 krb5_ccache ccache,
40 krb5_keytab keytab,
41 krb5_boolean secure,
42 const char *service,
43 krb5_creds cred)
45 krb5_error_code ret;
46 krb5_principal server;
47 krb5_verify_init_creds_opt vopt;
48 krb5_ccache id;
50 ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST,
51 &server);
52 if(ret)
53 return ret;
55 krb5_verify_init_creds_opt_init(&vopt);
56 krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure);
58 ret = krb5_verify_init_creds(context,
59 &cred,
60 server,
61 keytab,
62 NULL,
63 &vopt);
64 krb5_free_principal(context, server);
65 if(ret)
66 return ret;
67 if(ccache == NULL)
68 ret = krb5_cc_default (context, &id);
69 else
70 id = ccache;
71 if(ret == 0){
72 ret = krb5_cc_initialize(context, id, principal);
73 if(ret == 0){
74 ret = krb5_cc_store_cred(context, id, &cred);
76 if(ccache == NULL)
77 krb5_cc_close(context, id);
79 krb5_free_cred_contents(context, &cred);
80 return ret;
84 * Verify user `principal' with `password'.
86 * If `secure', also verify against local service key for `service'.
88 * As a side effect, fresh tickets are obtained and stored in `ccache'.
91 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
92 krb5_verify_opt_init(krb5_verify_opt *opt)
94 memset(opt, 0, sizeof(*opt));
95 opt->secure = TRUE;
96 opt->service = "host";
99 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
100 krb5_verify_opt_alloc(krb5_context context, krb5_verify_opt **opt)
102 *opt = calloc(1, sizeof(**opt));
103 if ((*opt) == NULL)
104 return krb5_enomem(context);
105 krb5_verify_opt_init(*opt);
106 return 0;
109 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
110 krb5_verify_opt_free(krb5_verify_opt *opt)
112 free(opt);
115 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
116 krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache)
118 opt->ccache = ccache;
121 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
122 krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab)
124 opt->keytab = keytab;
127 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
128 krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure)
130 opt->secure = secure;
133 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
134 krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service)
136 opt->service = service;
139 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
140 krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags)
142 opt->flags |= flags;
145 static krb5_error_code
146 verify_user_opt_int(krb5_context context,
147 krb5_principal principal,
148 const char *password,
149 krb5_verify_opt *vopt)
152 krb5_error_code ret;
153 krb5_get_init_creds_opt *opt;
154 krb5_creds cred;
156 ret = krb5_get_init_creds_opt_alloc (context, &opt);
157 if (ret)
158 return ret;
159 krb5_get_init_creds_opt_set_default_flags(context, NULL,
160 krb5_principal_get_realm(context, principal),
161 opt);
162 ret = krb5_get_init_creds_password (context,
163 &cred,
164 principal,
165 password,
166 krb5_prompter_posix,
167 NULL,
169 NULL,
170 opt);
171 krb5_get_init_creds_opt_free(context, opt);
172 if(ret)
173 return ret;
174 #define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D))
175 return verify_common (context, principal, OPT(ccache, NULL),
176 OPT(keytab, NULL), vopt ? vopt->secure : TRUE,
177 OPT(service, "host"), cred);
178 #undef OPT
181 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
182 krb5_verify_user_opt(krb5_context context,
183 krb5_principal principal,
184 const char *password,
185 krb5_verify_opt *opt)
187 krb5_error_code ret;
189 if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) {
190 krb5_realm *realms, *r;
191 ret = krb5_get_default_realms (context, &realms);
192 if (ret)
193 return ret;
194 ret = KRB5_CONFIG_NODEFREALM;
196 for (r = realms; *r != NULL && ret != 0; ++r) {
197 ret = krb5_principal_set_realm(context, principal, *r);
198 if (ret) {
199 krb5_free_host_realm (context, realms);
200 return ret;
203 ret = verify_user_opt_int(context, principal, password, opt);
205 krb5_free_host_realm (context, realms);
206 if(ret)
207 return ret;
208 } else
209 ret = verify_user_opt_int(context, principal, password, opt);
210 return ret;
213 /* compat function that calls above */
215 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
216 krb5_verify_user(krb5_context context,
217 krb5_principal principal,
218 krb5_ccache ccache,
219 const char *password,
220 krb5_boolean secure,
221 const char *service)
223 krb5_verify_opt opt;
225 krb5_verify_opt_init(&opt);
227 krb5_verify_opt_set_ccache(&opt, ccache);
228 krb5_verify_opt_set_secure(&opt, secure);
229 krb5_verify_opt_set_service(&opt, service);
231 return krb5_verify_user_opt(context, principal, password, &opt);
235 * A variant of `krb5_verify_user'. The realm of `principal' is
236 * ignored and all the local realms are tried.
239 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
240 krb5_verify_user_lrealm(krb5_context context,
241 krb5_principal principal,
242 krb5_ccache ccache,
243 const char *password,
244 krb5_boolean secure,
245 const char *service)
247 krb5_verify_opt opt;
249 krb5_verify_opt_init(&opt);
251 krb5_verify_opt_set_ccache(&opt, ccache);
252 krb5_verify_opt_set_secure(&opt, secure);
253 krb5_verify_opt_set_service(&opt, service);
254 krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS);
256 return krb5_verify_user_opt(context, principal, password, &opt);