2 * Copyright (c) 2001 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>
50 extern ServerOptions options;
51 extern u_char *session_id2;
52 extern int session_id2_len;
54 void userauth_reply(Authctxt *authctxt, int authenticated);
55 static void gssapi_unsetenv(const char *var);
57 typedef struct ssh_gssapi_cred_cache {
62 } ssh_gssapi_cred_cache;
64 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
65 unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
68 * Environment variables pointing to delegated credentials
70 static char *delegation_env[] = {
71 "X509_USER_PROXY", /* GSSAPI/SSLeay */
72 "KRB5CCNAME", /* Krb5 and possibly SSLeay */
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);
137 /* Make sure that this is called _after_ we've setuid to the user */
139 /* This writes out any forwarded credentials. Its specific to the Kerberos
142 * We assume that our caller has made sure that the user has selected
143 * delegated credentials, and that the client_creds structure is correctly
148 ssh_gssapi_krb5_storecreds() {
150 krb5_error_code problem;
151 krb5_principal princ;
153 static char name[40];
155 OM_uint32 maj_status,min_status;
158 if (gssapi_client_creds==NULL) {
159 debug("No credentials stored");
163 if (ssh_gssapi_krb5_init() == 0)
166 if (options.gss_use_session_ccache) {
167 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
169 if ((tmpfd = mkstemp(ccname))==-1) {
170 log("mkstemp(): %.100s", strerror(errno));
173 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
174 log("fchmod(): %.100s", strerror(errno));
179 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
180 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
182 log("open(): %.100s", strerror(errno));
188 snprintf(name, sizeof(name), "FILE:%s",ccname);
190 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
191 log("krb5_cc_default(): %.100s",
192 krb5_get_err_text(krb_context,problem));
196 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
198 log("krb5_parse_name(): %.100s",
199 krb5_get_err_text(krb_context,problem));
200 krb5_cc_destroy(krb_context,ccache);
204 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
205 log("krb5_cc_initialize(): %.100s",
206 krb5_get_err_text(krb_context,problem));
207 krb5_free_principal(krb_context,princ);
208 krb5_cc_destroy(krb_context,ccache);
212 krb5_free_principal(krb_context,princ);
215 if ((problem = krb5_cc_copy_cache(krb_context,
216 gssapi_client_creds->ccache,
218 log("krb5_cc_copy_cache(): %.100s",
219 krb5_get_err_text(krb_context,problem));
220 krb5_cc_destroy(krb_context,ccache);
224 if ((maj_status = gss_krb5_copy_ccache(&min_status,
227 log("gss_krb5_copy_ccache() failed");
228 ssh_gssapi_error(maj_status,min_status);
229 krb5_cc_destroy(krb_context,ccache);
234 krb5_cc_close(krb_context,ccache);
238 do_pam_putenv("KRB5CCNAME",name);
241 gssapi_cred_store.filename=strdup(ccname);
242 gssapi_cred_store.envvar="KRB5CCNAME";
243 gssapi_cred_store.envval=strdup(name);
251 #include <globus_gss_assist.h>
254 * Check if this user is OK to login under GSI. User has been authenticated
255 * as identity in global 'client_name.value' and is trying to log in as passed
256 * username in 'name'.
258 * Returns non-zero if user is authorized, 0 otherwise.
261 ssh_gssapi_gsi_userok(char *name)
265 /* This returns 0 on success */
266 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
269 debug("GSI user %s is%s authorized as target user %s",
270 (char *) gssapi_client_name.value,
271 (authorized ? "" : " not"),
278 * Handle setting up child environment for GSI.
280 * Make sure that this is called _after_ we've setuid to the user.
283 ssh_gssapi_gsi_storecreds()
285 OM_uint32 major_status;
286 OM_uint32 minor_status;
289 if (gssapi_client_creds != NULL)
291 char *creds_env = NULL;
294 * This is the current hack with the GSI gssapi library to
295 * export credentials to disk.
298 debug("Exporting delegated credentials");
300 minor_status = 0xdee0; /* Magic value */
302 gss_inquire_cred(&minor_status,
304 (gss_name_t *) &creds_env,
309 if ((major_status == GSS_S_COMPLETE) &&
310 (minor_status == 0xdee1) &&
316 * String is of the form:
317 * X509_USER_DELEG_PROXY=filename
318 * so we parse out the filename
319 * and then set X509_USER_PROXY
322 value = strchr(creds_env, '=');
329 do_pam_putenv("X509_USER_PROXY",value);
331 gssapi_cred_store.filename=NULL;
332 gssapi_cred_store.envvar="X509_USER_PROXY";
333 gssapi_cred_store.envval=strdup(value);
339 log("Failed to parse delegated credentials string '%s'",
345 log("Failed to export delegated credentials (error %ld)",
354 ssh_gssapi_cleanup_creds(void *ignored)
356 if (gssapi_cred_store.filename!=NULL) {
357 /* Unlink probably isn't sufficient */
358 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
359 unlink(gssapi_cred_store.filename);
364 ssh_gssapi_storecreds()
366 switch (gssapi_client_type) {
369 ssh_gssapi_krb5_storecreds();
374 ssh_gssapi_gsi_storecreds();
378 /* GSSAPI not used in this authentication */
379 debug("No GSSAPI credentials stored");
382 log("ssh_gssapi_do_child: Unknown mechanism");
386 if (options.gss_cleanup_creds) {
387 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
392 /* This allows GSSAPI methods to do things to the childs environment based
393 * on the passed authentication process and credentials.
395 * Question: If we didn't use userauth_external for some reason, should we
396 * still delegate credentials?
399 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
402 if (gssapi_cred_store.envvar!=NULL &&
403 gssapi_cred_store.envval!=NULL) {
405 debug("Setting %s to %s", gssapi_cred_store.envvar,
406 gssapi_cred_store.envval);
407 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
408 gssapi_cred_store.envval);
411 switch(gssapi_client_type) {
413 case GSS_KERBEROS: break;
419 debug("No GSSAPI credentials stored");
422 log("ssh_gssapi_do_child: Unknown mechanism");
427 ssh_gssapi_userok(char *user)
429 if (gssapi_client_name.length==0 ||
430 gssapi_client_name.value==NULL) {
431 debug("No suitable client data");
434 switch (gssapi_client_type) {
437 return(ssh_gssapi_krb5_userok(user));
438 break; /* Not reached */
442 return(ssh_gssapi_gsi_userok(user));
443 break; /* Not reached */
446 debug("Client not GSSAPI");
449 debug("Unknown client authentication type");
455 userauth_external(Authctxt *authctxt)
459 return(ssh_gssapi_userok(authctxt->user));
462 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
463 void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
465 /* We only support those mechanisms that we know about (ie ones that we know
466 * how to check local user kuserok and the like
469 userauth_gssapi(Authctxt *authctxt)
471 gss_OID_desc oid= {0,NULL};
474 gss_OID_set supported;
479 if (!authctxt->valid || authctxt->user == NULL)
482 if (datafellows & SSH_OLD_GSSAPI) {
483 debug("Early drafts of GSSAPI userauth not supported");
487 mechs=packet_get_int();
489 debug("Mechanism negotiation is not supported");
493 ssh_gssapi_supported_oids(&supported);
497 oid.elements = packet_get_string(&len);
499 gss_test_oid_set_member(&ms, &oid, supported, &present);
501 } while (mechs>0 && !present);
508 ctxt=xmalloc(sizeof(Gssctxt));
509 authctxt->methoddata=(void *)ctxt;
511 ssh_gssapi_build_ctx(ctxt);
512 ssh_gssapi_set_oid(ctxt,&oid);
514 if (ssh_gssapi_acquire_cred(ctxt))
517 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
520 packet_start(SSH_SMSG_AUTH_GSSAPI_RESPONSE);
522 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
523 packet_put_string(oid.elements,oid.length);
529 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN,
530 &input_gssapi_token);
532 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
533 &input_gssapi_token);
534 authctxt->postponed = 1;
540 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
542 Authctxt *authctxt = ctxt;
544 gss_buffer_desc send_tok,recv_tok;
545 OM_uint32 maj_status, min_status;
547 if (authctxt == NULL || authctxt->methoddata == NULL)
548 fatal("No authentication or GSSAPI context");
550 gssctxt=authctxt->methoddata;
552 recv_tok.value=packet_get_string(&recv_tok.length);
554 maj_status=ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, NULL);
557 if (GSS_ERROR(maj_status)) {
558 /* Failure <sniff> */
559 authctxt->postponed = 0;
560 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
561 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
562 userauth_finish(authctxt, 0, "gssapi");
565 if (send_tok.length != 0) {
566 /* Send a packet back to the client */
568 packet_start(SSH_MSG_AUTH_GSSAPI_TOKEN);
570 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
571 packet_put_string(send_tok.value,send_tok.length);
574 gss_release_buffer(&min_status, &send_tok);
577 if (maj_status == GSS_S_COMPLETE) {
578 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN, NULL);
579 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
580 /* ssh1 does not have an extra message here */
582 input_gssapi_exchange_complete(0, 0, ctxt);
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
585 &input_gssapi_exchange_complete);
589 /* This is called when the client thinks we've completed authentication.
590 * It should only be enabled in the dispatch handler by the function above,
591 * which only enables it once the GSSAPI exchange is complete.
595 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
597 Authctxt *authctxt = ctxt;
601 if(strcmp(authctxt->user,"") == 0) {
603 char *gridmapped_name = NULL;
604 struct passwd *pw = NULL;
606 if(globus_gss_assist_gridmap(gssapi_client_name.value,
607 &gridmapped_name) == 0) {
608 user = gridmapped_name;
609 debug("I gridmapped and got %s", user);
611 if (pw && allowed_user(pw)) {
612 authctxt->user = user;
613 authctxt->pw = pwcopy(pw);
620 if (authctxt == NULL || authctxt->methoddata == NULL)
621 fatal("No authentication or GSSAPI context");
623 gssctxt=authctxt->methoddata;
625 /* This should never happen, but better safe than sorry. */
626 if (gssctxt->status != GSS_S_COMPLETE) {
627 packet_disconnect("Context negotiation is not complete");
630 if (ssh_gssapi_getclient(gssctxt,&gssapi_client_type,
632 &gssapi_client_creds)) {
633 fatal("Couldn't convert client name");
636 authenticated = ssh_gssapi_userok(authctxt->user);
638 /* ssh1 needs to exchange the hash of the keys */
642 OM_uint32 maj_status, min_status;
643 gss_buffer_desc gssbuf,msg_tok;
646 gssbuf.value=ssh1_key_digest;
647 gssbuf.length=sizeof(ssh1_key_digest);
648 if ((maj_status=gss_wrap(&min_status,
655 ssh_gssapi_error(maj_status,min_status);
656 fatal("Couldn't wrap keys");
658 packet_start(SSH_SMSG_AUTH_GSSAPI_HASH);
659 packet_put_string((char *)msg_tok.value,msg_tok.length);
662 gss_release_buffer(&min_status,&msg_tok);
664 packet_start(SSH_MSG_AUTH_GSSAPI_ABORT);
670 authctxt->postponed = 0;
671 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
672 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
673 userauth_finish(authctxt, authenticated, "gssapi");
677 * Clean our environment on startup. This means removing any environment
678 * strings that might inadvertantly been in root's environment and
679 * could cause serious security problems if we think we set them.
682 ssh_gssapi_clean_env(void)
688 for (envstr_index = 0;
689 (envstr = delegation_env[envstr_index]) != NULL;
692 if (getenv(envstr)) {
693 debug("Clearing environment variable %s", envstr);
694 gssapi_unsetenv(envstr);
700 * Wrapper around unsetenv.
703 gssapi_unsetenv(const char *var)
708 #else /* !HAVE_UNSETENV */
709 extern char **environ;
710 char **p1 = environ; /* New array list */
711 char **p2 = environ; /* Current array list */
712 int len = strlen(var);
715 * Walk through current environ array (p2) copying each pointer
716 * to new environ array (p1) unless the pointer is to the item
717 * we want to delete. Copy happens in place.
720 if ((strncmp(*p2, var, len) == 0) &&
721 ((*p2)[len] == '=')) {
723 * *p2 points at item to be deleted, just skip over it
728 * *p2 points at item we want to save, so copy it
736 /* And make sure new array is NULL terminated */
738 #endif /* HAVE_UNSETENV */