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 int ssh_gssapi_krb5_init();
48 static int ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name);
49 static int ssh_gssapi_krb5_localname(ssh_gssapi_client *client, char **user);
50 static void ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client);
52 static krb5_context krb_context = NULL;
54 /* We've been using a wrongly encoded mechanism ID for yonks */
56 ssh_gssapi_mech gssapi_kerberos_mech_old = {
57 "Se3H81ismmOC3OE+FwYCiQ==",
59 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"},
60 &ssh_gssapi_krb5_init,
61 &ssh_gssapi_krb5_userok,
62 &ssh_gssapi_krb5_localname,
63 &ssh_gssapi_krb5_storecreds
66 ssh_gssapi_mech gssapi_kerberos_mech = {
67 "toWM5Slw5Ew8Mqkay+al2g==",
69 {9, "\x2A\x86\x48\x86\xF7\x12\x01\x02\x02"},
71 &ssh_gssapi_krb5_userok,
72 &ssh_gssapi_krb5_localname,
73 &ssh_gssapi_krb5_storecreds
76 /* Initialise the krb5 library, so we can use it for those bits that
80 ssh_gssapi_krb5_init() {
81 krb5_error_code problem;
83 if (krb_context !=NULL)
86 problem = krb5_init_context(&krb_context);
88 log("Cannot initialize krb5 context");
91 krb5_init_ets(krb_context);
96 /* Check if this user is OK to login. This only works with krb5 - other
97 * GSSAPI mechanisms will need their own.
98 * Returns true if the user is OK to log in, otherwise returns 0
102 ssh_gssapi_krb5_userok(ssh_gssapi_client *client, char *name) {
103 krb5_principal princ;
106 if (ssh_gssapi_krb5_init() == 0)
109 if ((retval=krb5_parse_name(krb_context, client->name.value,
111 log("krb5_parse_name(): %.100s",
112 krb5_get_err_text(krb_context,retval));
115 if (krb5_kuserok(krb_context, princ, name)) {
117 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
118 (char *)client->name.value);
123 krb5_free_principal(krb_context, princ);
127 /* Retrieve the local username associated with a set of Kerberos
128 * credentials. Hopefully we can use this for the 'empty' username
129 * logins discussed in the draft */
131 ssh_gssapi_krb5_localname(ssh_gssapi_client *client, char **user) {
132 krb5_principal princ;
135 if (ssh_gssapi_krb5_init() == 0)
138 if ((retval=krb5_parse_name(krb_context, client->name.value,
140 log("krb5_parse_name(): %.100s",
141 krb5_get_err_text(krb_context,retval));
145 /* We've got to return a malloc'd string */
146 *user = (char *)xmalloc(256);
147 if (krb5_aname_to_localname(krb_context, princ, 256, *user)) {
156 /* Make sure that this is called _after_ we've setuid to the user */
158 /* This writes out any forwarded credentials. Its specific to the Kerberos
161 * We assume that our caller has made sure that the user has selected
162 * delegated credentials, and that the client_creds structure is correctly
167 ssh_gssapi_krb5_storecreds(ssh_gssapi_client *client) {
169 krb5_error_code problem;
170 krb5_principal princ;
172 static char name[40];
174 OM_uint32 maj_status,min_status;
175 gss_cred_id_t krb5_cred_handle;
177 if (client->creds==NULL) {
178 debug("No credentials stored");
182 if (ssh_gssapi_krb5_init() == 0)
185 if (options.gss_use_session_ccache) {
186 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
188 if ((tmpfd = mkstemp(ccname))==-1) {
189 log("mkstemp(): %.100s", strerror(errno));
192 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
193 log("fchmod(): %.100s", strerror(errno));
198 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
199 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
201 log("open(): %.100s", strerror(errno));
207 snprintf(name, sizeof(name), "FILE:%s",ccname);
209 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
210 log("krb5_cc_default(): %.100s",
211 krb5_get_err_text(krb_context,problem));
215 if ((problem = krb5_parse_name(krb_context, client->name.value,
217 log("krb5_parse_name(): %.100s",
218 krb5_get_err_text(krb_context,problem));
219 krb5_cc_destroy(krb_context,ccache);
223 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
224 log("krb5_cc_initialize(): %.100s",
225 krb5_get_err_text(krb_context,problem));
226 krb5_free_principal(krb_context,princ);
227 krb5_cc_destroy(krb_context,ccache);
231 krb5_free_principal(krb_context,princ);
235 __gss_get_mechanism_cred(client->creds,
236 &(gssapi_kerberos_mech.oid));
238 krb5_cred_handle = client->creds;
241 if ((maj_status = gss_krb5_copy_ccache(&min_status,
244 log("gss_krb5_copy_ccache() failed");
245 krb5_cc_destroy(krb_context,ccache);
249 krb5_cc_close(krb_context,ccache);
252 do_pam_putenv("KRB5CCNAME",name);
255 client->store.filename=strdup(ccname);
256 client->store.envvar="KRB5CCNAME";
257 client->store.envval=strdup(name);