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);
53 ssh_gssapi_mech gssapi_gsi_mech = {
54 "dZuIebMjgUqaxvbF7hDbAw==",
56 {9, "\x2B\x06\x01\x04\x01\x9B\x50\x01\x01"},
58 &ssh_gssapi_gsi_userok,
59 &ssh_gssapi_gsi_localname,
60 &ssh_gssapi_gsi_storecreds,
65 * Check if this user is OK to login under GSI. User has been authenticated
66 * as identity in global 'client_name.value' and is trying to log in as passed
69 * Returns non-zero if user is authorized, 0 otherwise.
72 ssh_gssapi_gsi_userok(ssh_gssapi_client *client, char *name)
76 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
80 #ifdef GLOBUS_GSI_GSS_ASSIST_MODULE
81 if (globus_module_activate(GLOBUS_GSI_GSS_ASSIST_MODULE) != 0) {
86 /* use new globus_gss_assist_map_and_authorize() interface if available */
87 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
88 debug("calling globus_gss_assist_map_and_authorize()");
90 (res = globus_gss_assist_map_and_authorize(client->context, "ssh",
92 debug("%s", globus_error_print_chain(globus_error_get(res)));
93 } else if (lname && lname[0] && strcmp(name, lname) != 0) {
94 debug("GSI user maps to %s, not %s", lname, name);
99 debug("calling globus_gss_assist_userok()");
100 if (GLOBUS_SUCCESS !=
101 (res = (globus_gss_assist_userok(client->displayname.value,
103 debug("%s", globus_error_print_chain(globus_error_get(res)));
109 logit("GSI user %s is%s authorized as target user %s",
110 (char *) client->displayname.value, (authorized ? "" : " not"), name);
116 * Return the local username associated with the GSI credentials.
119 ssh_gssapi_gsi_localname(ssh_gssapi_client *client, char **user)
122 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
123 char lname[256] = "";
126 #ifdef GLOBUS_GSI_GSS_ASSIST_MODULE
127 if (globus_module_activate(GLOBUS_GSI_GSS_ASSIST_MODULE) != 0) {
132 /* use new globus_gss_assist_map_and_authorize() interface if available */
133 #ifdef HAVE_GLOBUS_GSS_ASSIST_MAP_AND_AUTHORIZE
134 debug("calling globus_gss_assist_map_and_authorize()");
135 if (GLOBUS_SUCCESS !=
136 (res = globus_gss_assist_map_and_authorize(client->context, "ssh",
137 NULL, lname, 256))) {
138 debug("%s", globus_error_print_chain(globus_error_get(res)));
139 logit("failed to map GSI user %s", (char *)client->displayname.value);
142 *user = strdup(lname);
144 debug("calling globus_gss_assist_gridmap()");
145 if (GLOBUS_SUCCESS !=
146 (res = globus_gss_assist_gridmap(client->displayname.value, user))) {
147 debug("%s", globus_error_print_chain(globus_error_get(res)));
148 logit("failed to map GSI user %s", (char *)client->displayname.value);
153 logit("GSI user %s mapped to target user %s",
154 (char *) client->displayname.value, *user);
160 * Export GSI credentials to disk.
163 ssh_gssapi_gsi_storecreds(ssh_gssapi_client *client)
165 OM_uint32 major_status;
166 OM_uint32 minor_status;
167 gss_buffer_desc export_cred = GSS_C_EMPTY_BUFFER;
170 if (!client || !client->creds) {
174 major_status = gss_export_cred(&minor_status,
179 if (GSS_ERROR(major_status) && major_status != GSS_S_UNAVAILABLE) {
181 ssh_gssapi_build_ctx(&ctx);
182 ctx->major = major_status;
183 ctx->minor = minor_status;
184 ssh_gssapi_set_oid(ctx, &gssapi_gsi_mech.oid);
185 ssh_gssapi_error(ctx);
186 ssh_gssapi_delete_ctx(&ctx);
190 p = strchr((char *) export_cred.value, '=');
192 logit("Failed to parse exported credentials string '%.100s'",
193 (char *)export_cred.value);
194 gss_release_buffer(&minor_status, &export_cred);
198 if (strcmp((char *)export_cred.value,"X509_USER_DELEG_PROXY") == 0) {
199 client->store.envvar = strdup("X509_USER_PROXY");
201 client->store.envvar = strdup((char *)export_cred.value);
203 if (access(p, R_OK) == 0) {
204 if (client->store.filename) {
205 if (rename(p, client->store.filename) < 0) {
206 logit("Failed to rename %s to %s: %s", p,
207 client->store.filename, strerror(errno));
208 xfree(client->store.filename);
209 client->store.filename = strdup(p);
211 p = client->store.filename;
214 client->store.filename = strdup(p);
217 client->store.envval = strdup(p);
220 do_pam_putenv(client->store.envvar, client->store.envval);
222 gss_release_buffer(&minor_status, &export_cred);