1 /* $NetBSD: verify_user.c,v 1.1.1.2 2014/04/24 12:45:51 pettai Exp $ */
4 * Copyright (c) 1997-2004 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 static krb5_error_code
39 verify_common (krb5_context context
,
40 krb5_principal principal
,
48 krb5_principal server
;
49 krb5_verify_init_creds_opt vopt
;
52 ret
= krb5_sname_to_principal (context
, NULL
, service
, KRB5_NT_SRV_HST
,
57 krb5_verify_init_creds_opt_init(&vopt
);
58 krb5_verify_init_creds_opt_set_ap_req_nofail(&vopt
, secure
);
60 ret
= krb5_verify_init_creds(context
,
66 krb5_free_principal(context
, server
);
70 ret
= krb5_cc_default (context
, &id
);
74 ret
= krb5_cc_initialize(context
, id
, principal
);
76 ret
= krb5_cc_store_cred(context
, id
, &cred
);
79 krb5_cc_close(context
, id
);
81 krb5_free_cred_contents(context
, &cred
);
86 * Verify user `principal' with `password'.
88 * If `secure', also verify against local service key for `service'.
90 * As a side effect, fresh tickets are obtained and stored in `ccache'.
93 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
94 krb5_verify_opt_init(krb5_verify_opt
*opt
)
96 memset(opt
, 0, sizeof(*opt
));
98 opt
->service
= "host";
101 KRB5_LIB_FUNCTION
int KRB5_LIB_CALL
102 krb5_verify_opt_alloc(krb5_context context
, krb5_verify_opt
**opt
)
104 *opt
= calloc(1, sizeof(**opt
));
105 if ((*opt
) == NULL
) {
106 krb5_set_error_message(context
, ENOMEM
,
107 N_("malloc: out of memory", ""));
110 krb5_verify_opt_init(*opt
);
114 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
115 krb5_verify_opt_free(krb5_verify_opt
*opt
)
120 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
121 krb5_verify_opt_set_ccache(krb5_verify_opt
*opt
, krb5_ccache ccache
)
123 opt
->ccache
= ccache
;
126 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
127 krb5_verify_opt_set_keytab(krb5_verify_opt
*opt
, krb5_keytab keytab
)
129 opt
->keytab
= keytab
;
132 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
133 krb5_verify_opt_set_secure(krb5_verify_opt
*opt
, krb5_boolean secure
)
135 opt
->secure
= secure
;
138 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
139 krb5_verify_opt_set_service(krb5_verify_opt
*opt
, const char *service
)
141 opt
->service
= service
;
144 KRB5_LIB_FUNCTION
void KRB5_LIB_CALL
145 krb5_verify_opt_set_flags(krb5_verify_opt
*opt
, unsigned int 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
)
158 krb5_get_init_creds_opt
*opt
;
161 ret
= krb5_get_init_creds_opt_alloc (context
, &opt
);
164 krb5_get_init_creds_opt_set_default_flags(context
, NULL
,
165 krb5_principal_get_realm(context
, principal
),
167 ret
= krb5_get_init_creds_password (context
,
176 krb5_get_init_creds_opt_free(context
, opt
);
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
);
186 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
187 krb5_verify_user_opt(krb5_context context
,
188 krb5_principal principal
,
189 const char *password
,
190 krb5_verify_opt
*opt
)
194 if(opt
&& (opt
->flags
& KRB5_VERIFY_LREALMS
)) {
195 krb5_realm
*realms
, *r
;
196 ret
= krb5_get_default_realms (context
, &realms
);
199 ret
= KRB5_CONFIG_NODEFREALM
;
201 for (r
= realms
; *r
!= NULL
&& ret
!= 0; ++r
) {
202 ret
= krb5_principal_set_realm(context
, principal
, *r
);
204 krb5_free_host_realm (context
, realms
);
208 ret
= verify_user_opt_int(context
, principal
, password
, opt
);
210 krb5_free_host_realm (context
, realms
);
214 ret
= verify_user_opt_int(context
, principal
, password
, opt
);
218 /* compat function that calls above */
220 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
221 krb5_verify_user(krb5_context context
,
222 krb5_principal principal
,
224 const char *password
,
230 krb5_verify_opt_init(&opt
);
232 krb5_verify_opt_set_ccache(&opt
, ccache
);
233 krb5_verify_opt_set_secure(&opt
, secure
);
234 krb5_verify_opt_set_service(&opt
, service
);
236 return krb5_verify_user_opt(context
, principal
, password
, &opt
);
240 * A variant of `krb5_verify_user'. The realm of `principal' is
241 * ignored and all the local realms are tried.
244 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
245 krb5_verify_user_lrealm(krb5_context context
,
246 krb5_principal principal
,
248 const char *password
,
254 krb5_verify_opt_init(&opt
);
256 krb5_verify_opt_set_ccache(&opt
, ccache
);
257 krb5_verify_opt_set_secure(&opt
, secure
);
258 krb5_verify_opt_set_service(&opt
, service
);
259 krb5_verify_opt_set_flags(&opt
, KRB5_VERIFY_LREALMS
);
261 return krb5_verify_user_opt(context
, principal
, password
, &opt
);