No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / krb5 / verify_user.c
blob4a1300d8fa51ed566d7ec0dac60347659d298e60
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 __RCSID("$Heimdal: verify_user.c 19078 2006-11-20 18:12:41Z lha $"
37 "$NetBSD$");
39 static krb5_error_code
40 verify_common (krb5_context context,
41 krb5_principal principal,
42 krb5_ccache ccache,
43 krb5_keytab keytab,
44 krb5_boolean secure,
45 const char *service,
46 krb5_creds cred)
48 krb5_error_code ret;
49 krb5_principal server;
50 krb5_verify_init_creds_opt vopt;
51 krb5_ccache id;
53 ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST,
54 &server);
55 if(ret)
56 return ret;
58 krb5_verify_init_creds_opt_init(&vopt);
59 krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure);
61 ret = krb5_verify_init_creds(context,
62 &cred,
63 server,
64 keytab,
65 NULL,
66 &vopt);
67 krb5_free_principal(context, server);
68 if(ret)
69 return ret;
70 if(ccache == NULL)
71 ret = krb5_cc_default (context, &id);
72 else
73 id = ccache;
74 if(ret == 0){
75 ret = krb5_cc_initialize(context, id, principal);
76 if(ret == 0){
77 ret = krb5_cc_store_cred(context, id, &cred);
79 if(ccache == NULL)
80 krb5_cc_close(context, id);
82 krb5_free_cred_contents(context, &cred);
83 return ret;
87 * Verify user `principal' with `password'.
89 * If `secure', also verify against local service key for `service'.
91 * As a side effect, fresh tickets are obtained and stored in `ccache'.
94 void KRB5_LIB_FUNCTION
95 krb5_verify_opt_init(krb5_verify_opt *opt)
97 memset(opt, 0, sizeof(*opt));
98 opt->secure = TRUE;
99 opt->service = "host";
102 int KRB5_LIB_FUNCTION
103 krb5_verify_opt_alloc(krb5_context context, krb5_verify_opt **opt)
105 *opt = calloc(1, sizeof(**opt));
106 if ((*opt) == NULL) {
107 krb5_set_error_string(context, "malloc: out of memory");
108 return ENOMEM;
110 krb5_verify_opt_init(*opt);
111 return 0;
114 void KRB5_LIB_FUNCTION
115 krb5_verify_opt_free(krb5_verify_opt *opt)
117 free(opt);
120 void KRB5_LIB_FUNCTION
121 krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache)
123 opt->ccache = ccache;
126 void KRB5_LIB_FUNCTION
127 krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab)
129 opt->keytab = keytab;
132 void KRB5_LIB_FUNCTION
133 krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure)
135 opt->secure = secure;
138 void KRB5_LIB_FUNCTION
139 krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service)
141 opt->service = service;
144 void KRB5_LIB_FUNCTION
145 krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags)
147 opt->flags |= flags;
150 static krb5_error_code
151 verify_user_opt_int(krb5_context context,
152 krb5_principal principal,
153 const char *password,
154 krb5_verify_opt *vopt)
157 krb5_error_code ret;
158 krb5_get_init_creds_opt *opt;
159 krb5_creds cred;
161 ret = krb5_get_init_creds_opt_alloc (context, &opt);
162 if (ret)
163 return ret;
164 krb5_get_init_creds_opt_set_default_flags(context, NULL,
165 krb5_principal_get_realm(context, principal),
166 opt);
167 ret = krb5_get_init_creds_password (context,
168 &cred,
169 principal,
170 password,
171 krb5_prompter_posix,
172 NULL,
174 NULL,
175 opt);
176 krb5_get_init_creds_opt_free(context, opt);
177 if(ret)
178 return ret;
179 #define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D))
180 return verify_common (context, principal, OPT(ccache, NULL),
181 OPT(keytab, NULL), vopt ? vopt->secure : TRUE,
182 OPT(service, "host"), cred);
183 #undef OPT
186 krb5_error_code KRB5_LIB_FUNCTION
187 krb5_verify_user_opt(krb5_context context,
188 krb5_principal principal,
189 const char *password,
190 krb5_verify_opt *opt)
192 krb5_error_code ret;
194 if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) {
195 krb5_realm *realms, *r;
196 ret = krb5_get_default_realms (context, &realms);
197 if (ret)
198 return ret;
199 ret = KRB5_CONFIG_NODEFREALM;
201 for (r = realms; *r != NULL && ret != 0; ++r) {
202 char *tmp = strdup (*r);
204 if (tmp == NULL) {
205 krb5_free_host_realm (context, realms);
206 krb5_set_error_string (context, "malloc: out of memory");
207 return ENOMEM;
209 free (*krb5_princ_realm (context, principal));
210 krb5_princ_set_realm (context, principal, &tmp);
212 ret = verify_user_opt_int(context, principal, password, opt);
214 krb5_free_host_realm (context, realms);
215 if(ret)
216 return ret;
217 } else
218 ret = verify_user_opt_int(context, principal, password, opt);
219 return ret;
222 /* compat function that calls above */
224 krb5_error_code KRB5_LIB_FUNCTION
225 krb5_verify_user(krb5_context context,
226 krb5_principal principal,
227 krb5_ccache ccache,
228 const char *password,
229 krb5_boolean secure,
230 const char *service)
232 krb5_verify_opt opt;
234 krb5_verify_opt_init(&opt);
236 krb5_verify_opt_set_ccache(&opt, ccache);
237 krb5_verify_opt_set_secure(&opt, secure);
238 krb5_verify_opt_set_service(&opt, service);
240 return krb5_verify_user_opt(context, principal, password, &opt);
244 * A variant of `krb5_verify_user'. The realm of `principal' is
245 * ignored and all the local realms are tried.
248 krb5_error_code KRB5_LIB_FUNCTION
249 krb5_verify_user_lrealm(krb5_context context,
250 krb5_principal principal,
251 krb5_ccache ccache,
252 const char *password,
253 krb5_boolean secure,
254 const char *service)
256 krb5_verify_opt opt;
258 krb5_verify_opt_init(&opt);
260 krb5_verify_opt_set_ccache(&opt, ccache);
261 krb5_verify_opt_set_secure(&opt, secure);
262 krb5_verify_opt_set_service(&opt, service);
263 krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS);
265 return krb5_verify_user_opt(context, principal, password, &opt);