1 /*
2 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
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.
16 *
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.
20 *
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.
32 */
33
34 #include "krb5_locl.h"
35
36 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)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)
44 {
45 krb5_error_code ret;
46 krb5_principal server;
47 krb5_verify_init_creds_opt vopt;
48 krb5_ccache id;
49
50 ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST,
51 &server);
52 if(ret)
53 return ret;
54
55 krb5_verify_init_creds_opt_init(&vopt);
56 krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt, secure);
57
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);
75 }
76 if(ccache == NULL)
77 krb5_cc_close(context, id);
78 }
79 krb5_free_cred_contents(context, &cred);
80 return ret;
81 }
82
83 /*
84 * Verify user `principal' with `password'.
85 *
86 * If `secure', also verify against local service key for `service'.
87 *
88 * As a side effect, fresh tickets are obtained and stored in `ccache'.
89 */
90
91 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_init(krb5_verify_opt * opt)92 krb5_verify_opt_init(krb5_verify_opt *opt)
93 {
94 memset(opt, 0, sizeof(*opt));
95 opt->secure = TRUE;
96 opt->service = "host";
97 }
98
99 KRB5_LIB_FUNCTION int KRB5_LIB_CALL
krb5_verify_opt_alloc(krb5_context context,krb5_verify_opt ** opt)100 krb5_verify_opt_alloc(krb5_context context, krb5_verify_opt **opt)
101 {
102 *opt = calloc(1, sizeof(**opt));
103 if ((*opt) == NULL) {
104 krb5_set_error_message(context, ENOMEM,
105 N_("malloc: out of memory", ""));
106 return ENOMEM;
107 }
108 krb5_verify_opt_init(*opt);
109 return 0;
110 }
111
112 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_free(krb5_verify_opt * opt)113 krb5_verify_opt_free(krb5_verify_opt *opt)
114 {
115 free(opt);
116 }
117
118 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_ccache(krb5_verify_opt * opt,krb5_ccache ccache)119 krb5_verify_opt_set_ccache(krb5_verify_opt *opt, krb5_ccache ccache)
120 {
121 opt->ccache = ccache;
122 }
123
124 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_keytab(krb5_verify_opt * opt,krb5_keytab keytab)125 krb5_verify_opt_set_keytab(krb5_verify_opt *opt, krb5_keytab keytab)
126 {
127 opt->keytab = keytab;
128 }
129
130 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_secure(krb5_verify_opt * opt,krb5_boolean secure)131 krb5_verify_opt_set_secure(krb5_verify_opt *opt, krb5_boolean secure)
132 {
133 opt->secure = secure;
134 }
135
136 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_service(krb5_verify_opt * opt,const char * service)137 krb5_verify_opt_set_service(krb5_verify_opt *opt, const char *service)
138 {
139 opt->service = service;
140 }
141
142 KRB5_LIB_FUNCTION void KRB5_LIB_CALL
krb5_verify_opt_set_flags(krb5_verify_opt * opt,unsigned int flags)143 krb5_verify_opt_set_flags(krb5_verify_opt *opt, unsigned int flags)
144 {
145 opt->flags |= flags;
146 }
147
148 static krb5_error_code
verify_user_opt_int(krb5_context context,krb5_principal principal,const char * password,krb5_verify_opt * vopt)149 verify_user_opt_int(krb5_context context,
150 krb5_principal principal,
151 const char *password,
152 krb5_verify_opt *vopt)
153
154 {
155 krb5_error_code ret;
156 krb5_get_init_creds_opt *opt;
157 krb5_creds cred;
158
159 ret = krb5_get_init_creds_opt_alloc (context, &opt);
160 if (ret)
161 return ret;
162 krb5_get_init_creds_opt_set_default_flags(context, NULL,
163 krb5_principal_get_realm(context, principal),
164 opt);
165 ret = krb5_get_init_creds_password (context,
166 &cred,
167 principal,
168 password,
169 krb5_prompter_posix,
170 NULL,
171 0,
172 NULL,
173 opt);
174 krb5_get_init_creds_opt_free(context, opt);
175 if(ret)
176 return ret;
177 #define OPT(V, D) ((vopt && (vopt->V)) ? (vopt->V) : (D))
178 return verify_common (context, principal, OPT(ccache, NULL),
179 OPT(keytab, NULL), vopt ? vopt->secure : TRUE,
180 OPT(service, "host"), cred);
181 #undef OPT
182 }
183
184 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)185 krb5_verify_user_opt(krb5_context context,
186 krb5_principal principal,
187 const char *password,
188 krb5_verify_opt *opt)
189 {
190 krb5_error_code ret;
191
192 if(opt && (opt->flags & KRB5_VERIFY_LREALMS)) {
193 krb5_realm *realms, *r;
194 ret = krb5_get_default_realms (context, &realms);
195 if (ret)
196 return ret;
197 ret = KRB5_CONFIG_NODEFREALM;
198
199 for (r = realms; *r != NULL && ret != 0; ++r) {
200 ret = krb5_principal_set_realm(context, principal, *r);
201 if (ret) {
202 krb5_free_host_realm (context, realms);
203 return ret;
204 }
205
206 ret = verify_user_opt_int(context, principal, password, opt);
207 }
208 krb5_free_host_realm (context, realms);
209 if(ret)
210 return ret;
211 } else
212 ret = verify_user_opt_int(context, principal, password, opt);
213 return ret;
214 }
215
216 /* compat function that calls above */
217
218 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)219 krb5_verify_user(krb5_context context,
220 krb5_principal principal,
221 krb5_ccache ccache,
222 const char *password,
223 krb5_boolean secure,
224 const char *service)
225 {
226 krb5_verify_opt opt;
227
228 krb5_verify_opt_init(&opt);
229
230 krb5_verify_opt_set_ccache(&opt, ccache);
231 krb5_verify_opt_set_secure(&opt, secure);
232 krb5_verify_opt_set_service(&opt, service);
233
234 return krb5_verify_user_opt(context, principal, password, &opt);
235 }
236
237 /*
238 * A variant of `krb5_verify_user'. The realm of `principal' is
239 * ignored and all the local realms are tried.
240 */
241
242 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)243 krb5_verify_user_lrealm(krb5_context context,
244 krb5_principal principal,
245 krb5_ccache ccache,
246 const char *password,
247 krb5_boolean secure,
248 const char *service)
249 {
250 krb5_verify_opt opt;
251
252 krb5_verify_opt_init(&opt);
253
254 krb5_verify_opt_set_ccache(&opt, ccache);
255 krb5_verify_opt_set_secure(&opt, secure);
256 krb5_verify_opt_set_service(&opt, service);
257 krb5_verify_opt_set_flags(&opt, KRB5_VERIFY_LREALMS);
258
259 return krb5_verify_user_opt(context, principal, password, &opt);
260 }
261