2 * Copyright (c) 2001,2002 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.
37 #include <openssl/evp.h>
48 #include "monitor_wrap.h"
52 extern ServerOptions options;
53 extern u_char *session_id2;
54 extern int session_id2_len;
56 typedef struct ssh_gssapi_cred_cache {
61 } ssh_gssapi_cred_cache;
63 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
66 * Environment variables pointing to delegated credentials
68 static char *delegation_env[] = {
69 "X509_USER_PROXY", /* GSSAPI/SSLeay */
70 "KRB5CCNAME", /* Krb5 and possibly SSLeay */
74 static void gssapi_unsetenv(const char *var);
81 #include <gssapi_krb5.h>
82 #define krb5_get_err_text(context,code) error_message(code)
85 static krb5_context krb_context = NULL;
87 /* Initialise the krb5 library, so we can use it for those bits that
90 int ssh_gssapi_krb5_init() {
91 krb5_error_code problem;
93 if (krb_context !=NULL)
96 problem = krb5_init_context(&krb_context);
98 log("Cannot initialize krb5 context");
101 krb5_init_ets(krb_context);
106 /* Check if this user is OK to login. This only works with krb5 - other
107 * GSSAPI mechanisms will need their own.
108 * Returns true if the user is OK to log in, otherwise returns 0
112 ssh_gssapi_krb5_userok(char *name) {
113 krb5_principal princ;
116 if (ssh_gssapi_krb5_init() == 0)
119 if ((retval=krb5_parse_name(krb_context, gssapi_client_name.value,
121 log("krb5_parse_name(): %.100s",
122 krb5_get_err_text(krb_context,retval));
125 if (krb5_kuserok(krb_context, princ, name)) {
127 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
128 (char *)gssapi_client_name.value);
133 krb5_free_principal(krb_context, princ);
138 ssh_gssapi_krb5_localname(char **user)
140 krb5_principal princ;
142 if (krb5_parse_name(krb_context, gssapi_client_name.value, &princ)) {
145 *user = (char *)xmalloc(256);
146 if (krb5_aname_to_localname(krb_context, princ, 256, *user)) {
154 #ifndef HAVE_GSSAPI_EXT
155 /* Make sure that this is called _after_ we've setuid to the user */
157 /* This writes out any forwarded credentials. Its specific to the Kerberos
160 * We assume that our caller has made sure that the user has selected
161 * delegated credentials, and that the client_creds structure is correctly
166 ssh_gssapi_krb5_storecreds() {
168 krb5_error_code problem;
169 krb5_principal princ;
171 static char name[40];
173 OM_uint32 maj_status,min_status;
176 if (gssapi_client_creds==NULL) {
177 debug("No credentials stored");
178 return GSS_S_NO_CRED;
181 if (ssh_gssapi_krb5_init() == 0)
182 return GSS_S_FAILURE;
184 if (options.gss_use_session_ccache) {
185 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
187 if ((tmpfd = mkstemp(ccname))==-1) {
188 log("mkstemp(): %.100s", strerror(errno));
189 return GSS_S_FAILURE;
191 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
192 log("fchmod(): %.100s", strerror(errno));
194 return GSS_S_FAILURE;
197 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
198 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
200 log("open(): %.100s", strerror(errno));
201 return GSS_S_FAILURE;
206 snprintf(name, sizeof(name), "FILE:%s",ccname);
208 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
209 log("krb5_cc_default(): %.100s",
210 krb5_get_err_text(krb_context,problem));
211 return GSS_S_FAILURE;
214 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
216 log("krb5_parse_name(): %.100s",
217 krb5_get_err_text(krb_context,problem));
218 krb5_cc_destroy(krb_context,ccache);
219 return GSS_S_FAILURE;
222 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
223 log("krb5_cc_initialize(): %.100s",
224 krb5_get_err_text(krb_context,problem));
225 krb5_free_principal(krb_context,princ);
226 krb5_cc_destroy(krb_context,ccache);
227 return GSS_S_FAILURE;
230 krb5_free_principal(krb_context,princ);
232 if ((maj_status = gss_krb5_copy_ccache(&min_status,
235 log("gss_krb5_copy_ccache() failed");
236 ssh_gssapi_error(maj_status,min_status);
237 krb5_cc_destroy(krb_context,ccache);
238 return GSS_S_FAILURE;
241 krb5_cc_close(krb_context,ccache);
245 do_pam_putenv("KRB5CCNAME",name);
248 gssapi_cred_store.filename=strdup(ccname);
249 gssapi_cred_store.envvar="KRB5CCNAME";
250 gssapi_cred_store.envval=strdup(name);
252 return GSS_S_COMPLETE;
254 #endif /* HAVE_GSSAPI_EXT */
259 #include <globus_gss_assist.h>
262 * Check if this user is OK to login under GSI. User has been authenticated
263 * as identity in global 'client_name.value' and is trying to log in as passed
264 * username in 'name'.
266 * Returns non-zero if user is authorized, 0 otherwise.
269 ssh_gssapi_gsi_userok(char *name)
273 /* This returns 0 on success */
274 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
277 log("GSI user %s is%s authorized as target user %s",
278 (char *) gssapi_client_name.value, (authorized ? "" : " not"), name);
284 * Return the local username associated with the GSI credentials.
287 ssh_gssapi_gsi_localname(char **user)
289 return(globus_gss_assist_gridmap(gssapi_client_name.value, user) == 0);
292 #ifndef HAVE_GSSAPI_EXT
294 * Handle setting up child environment for GSI.
296 * Make sure that this is called _after_ we've setuid to the user.
299 ssh_gssapi_gsi_storecreds()
301 OM_uint32 major_status;
302 OM_uint32 minor_status;
304 if (gssapi_client_creds != NULL)
306 char *creds_env = NULL;
309 * This is the current hack with the GSI gssapi library to
310 * export credentials to disk.
313 debug("Exporting delegated credentials");
315 minor_status = 0xdee0; /* Magic value */
317 gss_inquire_cred(&minor_status,
319 (gss_name_t *) &creds_env,
324 if ((major_status == GSS_S_COMPLETE) &&
325 (minor_status == 0xdee1) &&
331 * String is of the form:
332 * X509_USER_DELEG_PROXY=filename
333 * so we parse out the filename
334 * and then set X509_USER_PROXY
337 value = strchr(creds_env, '=');
344 do_pam_putenv("X509_USER_PROXY",value);
346 gssapi_cred_store.filename=strdup(value);
347 gssapi_cred_store.envvar="X509_USER_PROXY";
348 gssapi_cred_store.envval=strdup(value);
350 return GSS_S_COMPLETE;
354 log("Failed to parse delegated credentials string '%s'",
360 log("Failed to export delegated credentials (error %ld)",
366 #endif /* HAVE_GSSAPI_EXT */
371 ssh_gssapi_cleanup_creds(void *ignored)
373 /* OM_uint32 min_stat; */
375 if (gssapi_cred_store.filename!=NULL) {
376 /* Unlink probably isn't sufficient */
377 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
378 unlink(gssapi_cred_store.filename);
381 if (gssapi_client_creds != GSS_C_NO_CREDENTIAL)
382 gss_release_cred(&min_stat, &gssapi_client_creds);
386 #ifndef HAVE_GSSAPI_EXT
388 gss_export_cred(OM_uint32 * minor_status,
389 const gss_cred_id_t cred_handle,
390 const gss_OID desired_mech,
391 OM_uint32 option_req,
392 gss_buffer_t export_buffer)
394 OM_uint32 maj_stat = GSS_S_FAILURE;
396 switch (gssapi_client_type) {
399 maj_stat = ssh_gssapi_krb5_storecreds();
404 maj_stat = ssh_gssapi_gsi_storecreds();
408 /* GSSAPI not used in this authentication */
409 debug("No GSSAPI credentials stored");
412 log("ssh_gssapi_do_child: Unknown mechanism");
416 if (GSS_ERROR(maj_stat)) {
417 *minor_status = GSS_S_FAILURE;
422 #endif /* HAVE_GSSAPI_EXT */
425 ssh_gssapi_storecreds()
427 OM_uint32 maj_stat, min_stat;
428 gss_buffer_desc export_cred = GSS_C_EMPTY_BUFFER;
431 if (gssapi_client_creds == GSS_C_NO_CREDENTIAL)
434 maj_stat = gss_export_cred(&min_stat, gssapi_client_creds,
435 GSS_C_NO_OID, 1, &export_cred);
436 if (GSS_ERROR(maj_stat)) {
437 ssh_gssapi_error(maj_stat, min_stat);
441 p = strchr((char *) export_cred.value, '=');
443 log("Failed to parse exported credentials string '%.100s'",
444 (char *)export_cred.value);
445 gss_release_buffer(&min_stat, &export_cred);
449 gssapi_cred_store.envvar = strdup((char *)export_cred.value);
450 gssapi_cred_store.envval = strdup(p);
451 gss_release_buffer(&min_stat, &export_cred);
453 if (options.gss_cleanup_creds) {
454 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
458 /* This allows GSSAPI methods to do things to the childs environment based
459 * on the passed authentication process and credentials.
461 * Question: If we didn't use userauth_external for some reason, should we
462 * still delegate credentials?
465 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
468 if (gssapi_cred_store.envvar!=NULL &&
469 gssapi_cred_store.envval!=NULL) {
471 debug("Setting %s to %s", gssapi_cred_store.envvar,
472 gssapi_cred_store.envval);
473 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
474 gssapi_cred_store.envval);
477 switch(gssapi_client_type) {
479 case GSS_KERBEROS: break;
485 debug("No GSSAPI credentials stored");
488 log("ssh_gssapi_do_child: Unknown mechanism");
493 ssh_gssapi_userok(char *user)
495 if (gssapi_client_name.length==0 ||
496 gssapi_client_name.value==NULL) {
497 debug("No suitable client data");
500 switch (gssapi_client_type) {
503 return(ssh_gssapi_krb5_userok(user));
504 break; /* Not reached */
508 return(ssh_gssapi_gsi_userok(user));
509 break; /* Not reached */
512 debug("Client not GSSAPI");
515 debug("Unknown client authentication type");
521 ssh_gssapi_localname(char **user)
524 if (gssapi_client_name.length==0 ||
525 gssapi_client_name.value==NULL) {
526 debug("No suitable client data");
529 switch (gssapi_client_type) {
532 return(ssh_gssapi_krb5_localname(user));
533 break; /* Not reached */
537 return(ssh_gssapi_gsi_localname(user));
538 break; /* Not reached */
541 debug("Client not GSSAPI");
544 debug("Unknown client authentication type");
550 * Clean our environment on startup. This means removing any environment
551 * strings that might inadvertantly been in root's environment and
552 * could cause serious security problems if we think we set them.
555 ssh_gssapi_clean_env(void)
561 for (envstr_index = 0;
562 (envstr = delegation_env[envstr_index]) != NULL;
565 if (getenv(envstr)) {
566 debug("Clearing environment variable %s", envstr);
567 gssapi_unsetenv(envstr);
573 * Wrapper around unsetenv.
576 gssapi_unsetenv(const char *var)
581 #else /* !HAVE_UNSETENV */
582 extern char **environ;
583 char **p1 = environ; /* New array list */
584 char **p2 = environ; /* Current array list */
585 int len = strlen(var);
588 * Walk through current environ array (p2) copying each pointer
589 * to new environ array (p1) unless the pointer is to the item
590 * we want to delete. Copy happens in place.
593 if ((strncmp(*p2, var, len) == 0) &&
594 ((*p2)[len] == '=')) {
596 * *p2 points at item to be deleted, just skip over it
601 * *p2 points at item we want to save, so copy it
609 /* And make sure new array is NULL terminated */
611 #endif /* HAVE_UNSETENV */