xref: /freebsd/crypto/heimdal/lib/krb5/verify_user.c (revision daf1cffce2e07931f27c6c6998652e90df6ba87e)
1 /*
2  * Copyright (c) 1997, 1999 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 RCSID("$Id: verify_user.c,v 1.11 1999/12/02 17:05:13 joda Exp $");
37 
38 static krb5_error_code
39 verify_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 
90 krb5_error_code
91 krb5_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 
105     ret = krb5_get_init_creds_password (context,
106 					&cred,
107 					principal,
108 					(char*)password,
109 					krb5_prompter_posix,
110 					NULL,
111 					0,
112 					NULL,
113 					&opt);
114 
115     if(ret)
116 	return ret;
117     return verify_common (context, principal, ccache, secure, service, cred);
118 }
119 
120 /*
121  * A variant of `krb5_verify_user'.  The realm of `principal' is
122  * ignored and all the local realms are tried.
123  */
124 
125 krb5_error_code
126 krb5_verify_user_lrealm(krb5_context context,
127 			krb5_principal principal,
128 			krb5_ccache ccache,
129 			const char *password,
130 			krb5_boolean secure,
131 			const char *service)
132 {
133     krb5_error_code ret;
134     krb5_get_init_creds_opt opt;
135     krb5_realm *realms, *r;
136     krb5_creds cred;
137 
138     krb5_get_init_creds_opt_init (&opt);
139 
140     ret = krb5_get_default_realms (context, &realms);
141     if (ret)
142 	return ret;
143     ret = KRB5_CONFIG_NODEFREALM;
144 
145     for (r = realms; *r != NULL && ret != 0; ++r) {
146 	char *tmp = strdup (*r);
147 
148 	if (tmp == NULL) {
149 	    krb5_free_host_realm (context, realms);
150 	    return ENOMEM;
151 	}
152 	free (*krb5_princ_realm (context, principal));
153 	krb5_princ_set_realm (context, principal, &tmp);
154 
155 	ret = krb5_get_init_creds_password (context,
156 					    &cred,
157 					    principal,
158 					    (char*)password,
159 					    krb5_prompter_posix,
160 					    NULL,
161 					    0,
162 					    NULL,
163 					    &opt);
164     }
165     krb5_free_host_realm (context, realms);
166     if(ret)
167 	return ret;
168 
169     return verify_common (context, principal, ccache, secure, service, cred);
170 }
171