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>
51 extern ServerOptions options;
52 extern u_char *session_id2;
53 extern int session_id2_len;
55 void userauth_reply(Authctxt *authctxt, int authenticated);
56 static void gssapi_unsetenv(const char *var);
58 typedef struct ssh_gssapi_cred_cache {
63 } ssh_gssapi_cred_cache;
65 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
66 unsigned char ssh1_key_digest[16]; /* used for ssh1 gssapi */
69 * Environment variables pointing to delegated credentials
71 static char *delegation_env[] = {
72 "X509_USER_PROXY", /* GSSAPI/SSLeay */
73 "KRB5CCNAME", /* Krb5 and possibly SSLeay */
82 #include <gssapi_krb5.h>
83 #define krb5_get_err_text(context,code) error_message(code)
86 static krb5_context krb_context = NULL;
88 /* Initialise the krb5 library, so we can use it for those bits that
91 int ssh_gssapi_krb5_init() {
92 krb5_error_code problem;
94 if (krb_context !=NULL)
97 problem = krb5_init_context(&krb_context);
99 log("Cannot initialize krb5 context");
102 krb5_init_ets(krb_context);
107 /* Check if this user is OK to login. This only works with krb5 - other
108 * GSSAPI mechanisms will need their own.
109 * Returns true if the user is OK to log in, otherwise returns 0
113 ssh_gssapi_krb5_userok(char *name) {
114 krb5_principal princ;
117 if (ssh_gssapi_krb5_init() == 0)
120 if ((retval=krb5_parse_name(krb_context, gssapi_client_name.value,
122 log("krb5_parse_name(): %.100s",
123 krb5_get_err_text(krb_context,retval));
126 if (krb5_kuserok(krb_context, princ, name)) {
128 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
129 (char *)gssapi_client_name.value);
134 krb5_free_principal(krb_context, princ);
138 /* Make sure that this is called _after_ we've setuid to the user */
140 /* This writes out any forwarded credentials. Its specific to the Kerberos
143 * We assume that our caller has made sure that the user has selected
144 * delegated credentials, and that the client_creds structure is correctly
149 ssh_gssapi_krb5_storecreds() {
151 krb5_error_code problem;
152 krb5_principal princ;
154 static char name[40];
156 OM_uint32 maj_status,min_status;
159 if (gssapi_client_creds==NULL) {
160 debug("No credentials stored");
164 if (ssh_gssapi_krb5_init() == 0)
167 if (options.gss_use_session_ccache) {
168 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
170 if ((tmpfd = mkstemp(ccname))==-1) {
171 log("mkstemp(): %.100s", strerror(errno));
174 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
175 log("fchmod(): %.100s", strerror(errno));
180 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
181 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
183 log("open(): %.100s", strerror(errno));
189 snprintf(name, sizeof(name), "FILE:%s",ccname);
191 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
192 log("krb5_cc_default(): %.100s",
193 krb5_get_err_text(krb_context,problem));
197 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
199 log("krb5_parse_name(): %.100s",
200 krb5_get_err_text(krb_context,problem));
201 krb5_cc_destroy(krb_context,ccache);
205 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
206 log("krb5_cc_initialize(): %.100s",
207 krb5_get_err_text(krb_context,problem));
208 krb5_free_principal(krb_context,princ);
209 krb5_cc_destroy(krb_context,ccache);
213 krb5_free_principal(krb_context,princ);
216 if ((problem = krb5_cc_copy_cache(krb_context,
217 gssapi_client_creds->ccache,
219 log("krb5_cc_copy_cache(): %.100s",
220 krb5_get_err_text(krb_context,problem));
221 krb5_cc_destroy(krb_context,ccache);
225 if ((maj_status = gss_krb5_copy_ccache(&min_status,
228 log("gss_krb5_copy_ccache() failed");
229 ssh_gssapi_error(maj_status,min_status);
230 krb5_cc_destroy(krb_context,ccache);
235 krb5_cc_close(krb_context,ccache);
239 do_pam_putenv("KRB5CCNAME",name);
242 gssapi_cred_store.filename=strdup(ccname);
243 gssapi_cred_store.envvar="KRB5CCNAME";
244 gssapi_cred_store.envval=strdup(name);
252 #include <globus_gss_assist.h>
255 * Check if this user is OK to login under GSI. User has been authenticated
256 * as identity in global 'client_name.value' and is trying to log in as passed
257 * username in 'name'.
259 * Returns non-zero if user is authorized, 0 otherwise.
262 ssh_gssapi_gsi_userok(char *name)
266 /* This returns 0 on success */
267 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
270 log("GSI user %s is%s authorized as target user %s",
271 (char *) gssapi_client_name.value, (authorized ? "" : " not"), name);
277 * Handle setting up child environment for GSI.
279 * Make sure that this is called _after_ we've setuid to the user.
282 ssh_gssapi_gsi_storecreds()
284 OM_uint32 major_status;
285 OM_uint32 minor_status;
288 if (gssapi_client_creds != NULL)
290 char *creds_env = NULL;
293 * This is the current hack with the GSI gssapi library to
294 * export credentials to disk.
297 debug("Exporting delegated credentials");
299 minor_status = 0xdee0; /* Magic value */
301 gss_inquire_cred(&minor_status,
303 (gss_name_t *) &creds_env,
308 if ((major_status == GSS_S_COMPLETE) &&
309 (minor_status == 0xdee1) &&
315 * String is of the form:
316 * X509_USER_DELEG_PROXY=filename
317 * so we parse out the filename
318 * and then set X509_USER_PROXY
321 value = strchr(creds_env, '=');
328 do_pam_putenv("X509_USER_PROXY",value);
330 gssapi_cred_store.filename=NULL;
331 gssapi_cred_store.envvar="X509_USER_PROXY";
332 gssapi_cred_store.envval=strdup(value);
338 log("Failed to parse delegated credentials string '%s'",
344 log("Failed to export delegated credentials (error %ld)",
353 ssh_gssapi_cleanup_creds(void *ignored)
355 if (gssapi_cred_store.filename!=NULL) {
356 /* Unlink probably isn't sufficient */
357 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
358 unlink(gssapi_cred_store.filename);
363 ssh_gssapi_storecreds()
365 switch (gssapi_client_type) {
368 ssh_gssapi_krb5_storecreds();
373 ssh_gssapi_gsi_storecreds();
377 /* GSSAPI not used in this authentication */
378 debug("No GSSAPI credentials stored");
381 log("ssh_gssapi_do_child: Unknown mechanism");
385 if (options.gss_cleanup_creds) {
386 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
391 /* This allows GSSAPI methods to do things to the childs environment based
392 * on the passed authentication process and credentials.
394 * Question: If we didn't use userauth_external for some reason, should we
395 * still delegate credentials?
398 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
401 if (gssapi_cred_store.envvar!=NULL &&
402 gssapi_cred_store.envval!=NULL) {
404 debug("Setting %s to %s", gssapi_cred_store.envvar,
405 gssapi_cred_store.envval);
406 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
407 gssapi_cred_store.envval);
410 switch(gssapi_client_type) {
412 case GSS_KERBEROS: break;
418 debug("No GSSAPI credentials stored");
421 log("ssh_gssapi_do_child: Unknown mechanism");
426 ssh_gssapi_userok(char *user)
428 if (gssapi_client_name.length==0 ||
429 gssapi_client_name.value==NULL) {
430 debug("No suitable client data");
433 switch (gssapi_client_type) {
436 return(ssh_gssapi_krb5_userok(user));
437 break; /* Not reached */
441 return(ssh_gssapi_gsi_userok(user));
442 break; /* Not reached */
445 debug("Client not GSSAPI");
448 debug("Unknown client authentication type");
454 userauth_external(Authctxt *authctxt)
458 return(ssh_gssapi_userok(authctxt->user));
461 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
462 void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
464 /* We only support those mechanisms that we know about (ie ones that we know
465 * how to check local user kuserok and the like
468 userauth_gssapi(Authctxt *authctxt)
470 gss_OID_desc oid= {0,NULL};
473 gss_OID_set supported;
478 if (!authctxt->valid || authctxt->user == NULL)
481 if (datafellows & SSH_OLD_GSSAPI) {
482 debug("Early drafts of GSSAPI userauth not supported");
486 mechs=packet_get_int();
488 debug("Mechanism negotiation is not supported");
492 ssh_gssapi_supported_oids(&supported);
496 oid.elements = packet_get_string(&len);
498 gss_test_oid_set_member(&ms, &oid, supported, &present);
500 } while (mechs>0 && !present);
507 ctxt=xmalloc(sizeof(Gssctxt));
508 authctxt->methoddata=(void *)ctxt;
510 ssh_gssapi_build_ctx(ctxt);
511 ssh_gssapi_set_oid(ctxt,&oid);
513 if (ssh_gssapi_acquire_cred(ctxt))
516 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
519 packet_start(SSH_SMSG_AUTH_GSSAPI_RESPONSE);
521 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
522 packet_put_string(oid.elements,oid.length);
528 dispatch_set(SSH_MSG_AUTH_GSSAPI_TOKEN,
529 &input_gssapi_token);
531 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
532 &input_gssapi_token);
533 authctxt->postponed = 1;
539 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
541 Authctxt *authctxt = ctxt;
543 gss_buffer_desc send_tok,recv_tok;
544 OM_uint32 maj_status, min_status;
546 if (authctxt == NULL || authctxt->methoddata == NULL)
547 fatal("No authentication or GSSAPI context");
549 gssctxt=authctxt->methoddata;
551 recv_tok.value=packet_get_string(&recv_tok.length);
553 maj_status=ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, NULL);
556 if (GSS_ERROR(maj_status)) {
557 /* Failure <sniff> */
558 ssh_gssapi_send_error(maj_status,min_status);
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;
605 if(globus_gss_assist_gridmap(gssapi_client_name.value,
606 &gridmapped_name) == 0) {
607 user = gridmapped_name;
608 debug("I gridmapped and got %s", user);
610 if (pw && allowed_user(pw)) {
611 authctxt->user = user;
612 authctxt->pw = pwcopy(pw);
619 if (authctxt == NULL || authctxt->methoddata == NULL)
620 fatal("No authentication or GSSAPI context");
622 gssctxt=authctxt->methoddata;
624 /* This should never happen, but better safe than sorry. */
625 if (gssctxt->status != GSS_S_COMPLETE) {
626 packet_disconnect("Context negotiation is not complete");
629 if (ssh_gssapi_getclient(gssctxt,&gssapi_client_type,
631 &gssapi_client_creds)) {
632 fatal("Couldn't convert client name");
635 authenticated = ssh_gssapi_userok(authctxt->user);
637 /* ssh1 needs to exchange the hash of the keys */
641 OM_uint32 maj_status, min_status;
642 gss_buffer_desc gssbuf,msg_tok;
645 gssbuf.value=ssh1_key_digest;
646 gssbuf.length=sizeof(ssh1_key_digest);
647 if ((maj_status=gss_wrap(&min_status,
654 ssh_gssapi_error(maj_status,min_status);
655 fatal("Couldn't wrap keys");
657 packet_start(SSH_SMSG_AUTH_GSSAPI_HASH);
658 packet_put_string((char *)msg_tok.value,msg_tok.length);
661 gss_release_buffer(&min_status,&msg_tok);
663 packet_start(SSH_MSG_AUTH_GSSAPI_ABORT);
669 authctxt->postponed = 0;
670 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
671 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
672 userauth_finish(authctxt, authenticated, "gssapi");
676 * Clean our environment on startup. This means removing any environment
677 * strings that might inadvertantly been in root's environment and
678 * could cause serious security problems if we think we set them.
681 ssh_gssapi_clean_env(void)
687 for (envstr_index = 0;
688 (envstr = delegation_env[envstr_index]) != NULL;
691 if (getenv(envstr)) {
692 debug("Clearing environment variable %s", envstr);
693 gssapi_unsetenv(envstr);
699 * Wrapper around unsetenv.
702 gssapi_unsetenv(const char *var)
707 #else /* !HAVE_UNSETENV */
708 extern char **environ;
709 char **p1 = environ; /* New array list */
710 char **p2 = environ; /* Current array list */
711 int len = strlen(var);
714 * Walk through current environ array (p2) copying each pointer
715 * to new environ array (p1) unless the pointer is to the item
716 * we want to delete. Copy happens in place.
719 if ((strncmp(*p2, var, len) == 0) &&
720 ((*p2)[len] == '=')) {
722 * *p2 points at item to be deleted, just skip over it
727 * *p2 points at item we want to save, so copy it
735 /* And make sure new array is NULL terminated */
737 #endif /* HAVE_UNSETENV */