2 * Copyright (c) 2001-2003 Simon Wilkinson. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
16 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
17 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
18 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
19 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
20 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
21 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
22 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 extern ServerOptions options;
43 #include <gssapi_krb5.h>
44 #define krb5_get_err_text(context,code) error_message(code)
47 static krb5_context krb_context = NULL;
49 /* Initialise the krb5 library, so we can use it for those bits that
53 ssh_gssapi_krb5_init() {
54 krb5_error_code problem;
56 if (krb_context !=NULL)
59 problem = krb5_init_context(&krb_context);
61 log("Cannot initialize krb5 context");
64 krb5_init_ets(krb_context);
69 /* Check if this user is OK to login. This only works with krb5 - other
70 * GSSAPI mechanisms will need their own.
71 * Returns true if the user is OK to log in, otherwise returns 0
75 ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name) {
79 if (ssh_gssapi_krb5_init() == 0)
82 if ((retval=krb5_parse_name(krb_context, client->name.value,
84 log("krb5_parse_name(): %.100s",
85 krb5_get_err_text(krb_context,retval));
88 if (krb5_kuserok(krb_context, princ, name)) {
90 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
91 (char *)client->name.value);
96 krb5_free_principal(krb_context, princ);
100 /* Retrieve the local username associated with a set of Kerberos
101 * credentials. Hopefully we can use this for the 'empty' username
102 * logins discussed in the draft */
104 ssh_gssapi_krb5_localname(ssh_gssapi_client *client, char **user) {
105 krb5_principal princ;
108 if (ssh_gssapi_krb5_init() == 0)
111 if ((retval=krb5_parse_name(krb_context, client->name.value,
113 log("krb5_parse_name(): %.100s",
114 krb5_get_err_text(krb_context,retval));
118 /* We've got to return a malloc'd string */
119 *user = (char *)xmalloc(256);
120 if (krb5_aname_to_localname(krb_context, princ, 256, *user)) {
129 /* Make sure that this is called _after_ we've setuid to the user */
131 /* This writes out any forwarded credentials. Its specific to the Kerberos
134 * We assume that our caller has made sure that the user has selected
135 * delegated credentials, and that the client_creds structure is correctly
140 ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client) {
142 krb5_error_code problem;
143 krb5_principal princ;
145 static char name[40];
147 OM_uint32 maj_status,min_status;
149 if (client->creds==NULL) {
150 debug("No credentials stored");
154 if (ssh_gssapi_krb5_init() == 0)
157 if (options.gss_use_session_ccache) {
158 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
160 if ((tmpfd = mkstemp(ccname))==-1) {
161 log("mkstemp(): %.100s", strerror(errno));
164 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
165 log("fchmod(): %.100s", strerror(errno));
170 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
171 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
173 log("open(): %.100s", strerror(errno));
179 snprintf(name, sizeof(name), "FILE:%s",ccname);
181 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
182 log("krb5_cc_default(): %.100s",
183 krb5_get_err_text(krb_context,problem));
187 if ((problem = krb5_parse_name(krb_context, client->name.value,
189 log("krb5_parse_name(): %.100s",
190 krb5_get_err_text(krb_context,problem));
191 krb5_cc_destroy(krb_context,ccache);
195 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
196 log("krb5_cc_initialize(): %.100s",
197 krb5_get_err_text(krb_context,problem));
198 krb5_free_principal(krb_context,princ);
199 krb5_cc_destroy(krb_context,ccache);
203 krb5_free_principal(krb_context,princ);
206 if ((problem = krb5_cc_copy_cache(krb_context,
207 client->creds->ccache,
209 log("krb5_cc_copy_cache(): %.100s",
210 krb5_get_err_text(krb_context,problem));
211 krb5_cc_destroy(krb_context,ccache);
215 if ((maj_status = gss_krb5_copy_ccache(&min_status,
218 log("gss_krb5_copy_ccache() failed");
219 krb5_cc_destroy(krb_context,ccache);
224 krb5_cc_close(krb_context,ccache);
227 do_pam_putenv("KRB5CCNAME",name);
230 client->store.filename=strdup(ccname);
231 client->store.envvar="KRB5CCNAME";
232 client->store.envval=strdup(name);
237 /* We've been using a wrongly encoded mechanism ID for yonks */
239 ssh_gssapi_mech gssapi_kerberos_mech_old = {
240 "Se3H81ismmOC3OE+FwYCiQ==",
242 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"},
243 &ssh_gssapi_krb5_init,
244 &ssh_gssapi_krb5_userok,
245 &ssh_gssapi_krb5_localname,
246 &ssh_gssapi_krb5_storecreds
249 ssh_gssapi_mech gssapi_kerberos_mech = {
250 "toWM5Slw5Ew8Mqkay+al2g==",
252 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"},
254 &ssh_gssapi_krb5_userok,
255 &ssh_gssapi_krb5_localname,
256 &ssh_gssapi_krb5_storecreds