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.
30 #include <sys/types.h>
45 extern ServerOptions options;
47 #include <globus_gss_assist.h>
49 static int ssh_gssapi_gsi_userok(ssh_gssapi_client *client, char *name);
50 static int ssh_gssapi_gsi_localname(ssh_gssapi_client *client, char **user);
51 static void ssh_gssapi_gsi_storecreds(ssh_gssapi_client *client);
52 static int ssh_gssapi_gsi_updatecreds(ssh_gssapi_ccache *store,
53 ssh_gssapi_client *client);
55 ssh_gssapi_mech gssapi_gsi_mech = {
56 "dZuIebMjgUqaxvbF7hDbAw==",
58 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"},
60 &ssh_gssapi_gsi_userok,
61 &ssh_gssapi_gsi_localname,
62 &ssh_gssapi_gsi_storecreds,
63 &ssh_gssapi_gsi_updatecreds
67 * Check if this user is OK to login under GSI. User has been authenticated
68 * as identity in global 'client_name.value' and is trying to log in as passed
71 * Returns non-zero if user is authorized, 0 otherwise.
74 ssh_gssapi_gsi_userok(ssh_gssapi_client *client, char *name)
78 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
82 #ifdef GLOBUS_GSI_GSS_ASSIST_MODULE
83 if (globus_module_activate(GLOBUS_GSI_GSS_ASSIST_MODULE) != 0) {
88 /* use new globus_gss_assist_map_and_authorize() interface if available */
89 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
90 debug("calling globus_gss_assist_map_and_authorize()");
92 (res = globus_gss_assist_map_and_authorize(client->context, "ssh",
94 debug("%s", globus_error_print_chain(globus_error_get(res)));
95 } else if (lname && lname[0] && strcmp(name, lname) != 0) {
96 debug("GSI user maps to %s, not %s", lname, name);
101 debug("calling globus_gss_assist_userok()");
102 if (GLOBUS_SUCCESS !=
103 (res = (globus_gss_assist_userok(client->displayname.value,
105 debug("%s", globus_error_print_chain(globus_error_get(res)));
111 logit("GSI user %s is%s authorized as target user %s",
112 (char *) client->displayname.value, (authorized ? "" : " not"), name);
118 * Return the local username associated with the GSI credentials.
121 ssh_gssapi_gsi_localname(ssh_gssapi_client *client, char **user)
124 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
125 char lname[256] = "";
128 #ifdef GLOBUS_GSI_GSS_ASSIST_MODULE
129 if (globus_module_activate(GLOBUS_GSI_GSS_ASSIST_MODULE) != 0) {
134 /* use new globus_gss_assist_map_and_authorize() interface if available */
135 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
136 debug("calling globus_gss_assist_map_and_authorize()");
137 if (GLOBUS_SUCCESS !=
138 (res = globus_gss_assist_map_and_authorize(client->context, "ssh",
139 NULL, lname, 256))) {
140 debug("%s", globus_error_print_chain(globus_error_get(res)));
141 logit("failed to map GSI user %s", (char *)client->displayname.value);
144 *user = strdup(lname);
146 debug("calling globus_gss_assist_gridmap()");
147 if (GLOBUS_SUCCESS !=
148 (res = globus_gss_assist_gridmap(client->displayname.value, user))) {
149 debug("%s", globus_error_print_chain(globus_error_get(res)));
150 logit("failed to map GSI user %s", (char *)client->displayname.value);
155 logit("GSI user %s mapped to target user %s",
156 (char *) client->displayname.value, *user);
162 * Export GSI credentials to disk.
165 ssh_gssapi_gsi_storecreds(ssh_gssapi_client *client)
167 OM_uint32 major_status;
168 OM_uint32 minor_status;
169 gss_buffer_desc export_cred = GSS_C_EMPTY_BUFFER;
172 if (!client || !client->creds) {
176 major_status = gss_export_cred(&minor_status,
181 if (GSS_ERROR(major_status) && major_status != GSS_S_UNAVAILABLE) {
183 ssh_gssapi_build_ctx(&ctx);
184 ctx->major = major_status;
185 ctx->minor = minor_status;
186 ssh_gssapi_set_oid(ctx, &gssapi_gsi_mech.oid);
187 ssh_gssapi_error(ctx);
188 ssh_gssapi_delete_ctx(&ctx);
192 p = strchr((char *) export_cred.value, '=');
194 logit("Failed to parse exported credentials string '%.100s'",
195 (char *)export_cred.value);
196 gss_release_buffer(&minor_status, &export_cred);
200 if (strcmp((char *)export_cred.value,"X509_USER_DELEG_PROXY") == 0) {
201 client->store.envvar = strdup("X509_USER_PROXY");
203 client->store.envvar = strdup((char *)export_cred.value);
205 if (access(p, R_OK) == 0) {
206 if (client->store.filename) {
207 if (rename(p, client->store.filename) < 0) {
208 logit("Failed to rename %s to %s: %s", p,
209 client->store.filename, strerror(errno));
210 xfree(client->store.filename);
211 client->store.filename = strdup(p);
213 p = client->store.filename;
216 client->store.filename = strdup(p);
219 client->store.envval = strdup(p);
222 do_pam_putenv(client->store.envvar, client->store.envval);
224 gss_release_buffer(&minor_status, &export_cred);
228 * Export updated GSI credentials to disk.
231 ssh_gssapi_gsi_updatecreds(ssh_gssapi_ccache *store,ssh_gssapi_client *client)
233 ssh_gssapi_gsi_storecreds(client);