verify_user.c revision 72445
1/*
2 * Copyright (c) 1997-2001 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
36RCSID("$Id: verify_user.c,v 1.12 2001/01/04 17:40:00 joda Exp $");
37
38static krb5_error_code
39verify_common (krb5_context context,
40	       krb5_principal principal,
41	       krb5_ccache ccache,
42	       krb5_boolean secure,
43	       const char *service,
44	       krb5_creds cred)
45{
46    krb5_error_code ret;
47    krb5_principal server;
48    krb5_verify_init_creds_opt vopt;
49    krb5_ccache id;
50
51    ret = krb5_sname_to_principal (context, NULL, service, KRB5_NT_SRV_HST,
52				   &server);
53    if(ret) 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				 NULL,
62				 NULL,
63				 &vopt);
64    krb5_free_principal(context, server);
65    if(ret) return ret;
66    if(ccache == NULL)
67	ret = krb5_cc_default (context, &id);
68    else
69	id = ccache;
70    if(ret == 0){
71	ret = krb5_cc_initialize(context, id, principal);
72	if(ret == 0){
73	    ret = krb5_cc_store_cred(context, id, &cred);
74	}
75	if(ccache == NULL)
76	    krb5_cc_close(context, id);
77    }
78    krb5_free_creds_contents(context, &cred);
79    return ret;
80}
81
82/*
83 * Verify user `principal' with `password'.
84 *
85 * If `secure', also verify against local service key for `service'.
86 *
87 * As a side effect, fresh tickets are obtained and stored in `ccache'.
88 */
89
90krb5_error_code
91krb5_verify_user(krb5_context context,
92		 krb5_principal principal,
93		 krb5_ccache ccache,
94		 const char *password,
95		 krb5_boolean secure,
96		 const char *service)
97{
98
99    krb5_error_code ret;
100    krb5_get_init_creds_opt opt;
101    krb5_creds cred;
102
103    krb5_get_init_creds_opt_init (&opt);
104    krb5_get_init_creds_opt_set_default_flags(context, NULL,
105					      *krb5_princ_realm(context, principal),
106					      &opt);
107
108    ret = krb5_get_init_creds_password (context,
109					&cred,
110					principal,
111					(char*)password,
112					krb5_prompter_posix,
113					NULL,
114					0,
115					NULL,
116					&opt);
117
118    if(ret)
119	return ret;
120    return verify_common (context, principal, ccache, secure, service, cred);
121}
122
123/*
124 * A variant of `krb5_verify_user'.  The realm of `principal' is
125 * ignored and all the local realms are tried.
126 */
127
128krb5_error_code
129krb5_verify_user_lrealm(krb5_context context,
130			krb5_principal principal,
131			krb5_ccache ccache,
132			const char *password,
133			krb5_boolean secure,
134			const char *service)
135{
136    krb5_error_code ret;
137    krb5_get_init_creds_opt opt;
138    krb5_realm *realms, *r;
139    krb5_creds cred;
140
141    krb5_get_init_creds_opt_init (&opt);
142
143    ret = krb5_get_default_realms (context, &realms);
144    if (ret)
145	return ret;
146    ret = KRB5_CONFIG_NODEFREALM;
147
148    for (r = realms; *r != NULL && ret != 0; ++r) {
149	char *tmp = strdup (*r);
150
151	if (tmp == NULL) {
152	    krb5_free_host_realm (context, realms);
153	    return ENOMEM;
154	}
155	free (*krb5_princ_realm (context, principal));
156	krb5_princ_set_realm (context, principal, &tmp);
157
158	krb5_get_init_creds_opt_set_default_flags(context, NULL,
159						  *krb5_princ_realm(context, principal),
160						  &opt);
161	ret = krb5_get_init_creds_password (context,
162					    &cred,
163					    principal,
164					    (char*)password,
165					    krb5_prompter_posix,
166					    NULL,
167					    0,
168					    NULL,
169					    &opt);
170    }
171    krb5_free_host_realm (context, realms);
172    if(ret)
173	return ret;
174
175    return verify_common (context, principal, ccache, secure, service, cred);
176}
177