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