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.
36 #include <openssl/evp.h>
48 extern ServerOptions options;
49 extern u_char *session_id2;
50 extern int session_id2_len;
53 typedef struct ssh_gssapi_cred_cache {
58 } ssh_gssapi_cred_cache;
60 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
67 #include <gssapi_krb5.h>
68 #define krb5_get_err_text(context,code) error_message(code)
71 static krb5_context krb_context = NULL;
73 /* Initialise the krb5 library, so we can use it for those bits that
76 int ssh_gssapi_krb5_init() {
77 krb5_error_code problem;
79 if (krb_context !=NULL)
82 problem = krb5_init_context(&krb_context);
84 log("Cannot initialize krb5 context");
87 krb5_init_ets(krb_context);
92 /* Check if this user is OK to login. This only works with krb5 - other
93 * GSSAPI mechanisms will need their own.
94 * Returns true if the user is OK to log in, otherwise returns 0
98 ssh_gssapi_krb5_userok(char *name) {
102 if (ssh_gssapi_krb5_init() == 0)
105 if ((retval=krb5_parse_name(krb_context, gssapi_client_name.value,
107 log("krb5_parse_name(): %.100s",
108 krb5_get_err_text(krb_context,retval));
111 if (krb5_kuserok(krb_context, princ, name)) {
113 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
114 (char *)gssapi_client_name.value);
119 krb5_free_principal(krb_context, princ);
123 /* Make sure that this is called _after_ we've setuid to the user */
125 /* This writes out any forwarded credentials. Its specific to the Kerberos
128 * We assume that our caller has made sure that the user has selected
129 * delegated credentials, and that the client_creds structure is correctly
134 ssh_gssapi_krb5_storecreds() {
136 krb5_error_code problem;
137 krb5_principal princ;
139 static char name[40];
141 OM_uint32 maj_status,min_status;
144 if (gssapi_client_creds==NULL) {
145 debug("No credentials stored");
149 if (ssh_gssapi_krb5_init() == 0)
152 if (options.gss_use_session_ccache) {
153 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
155 if ((tmpfd = mkstemp(ccname))==-1) {
156 log("mkstemp(): %.100s", strerror(errno));
159 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
160 log("fchmod(): %.100s", strerror(errno));
165 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
166 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
168 log("open(): %.100s", strerror(errno));
174 snprintf(name, sizeof(name), "FILE:%s",ccname);
176 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
177 log("krb5_cc_default(): %.100s",
178 krb5_get_err_text(krb_context,problem));
182 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
184 log("krb5_parse_name(): %.100s",
185 krb5_get_err_text(krb_context,problem));
186 krb5_cc_destroy(krb_context,ccache);
190 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
191 log("krb5_cc_initialize(): %.100s",
192 krb5_get_err_text(krb_context,problem));
193 krb5_free_principal(krb_context,princ);
194 krb5_cc_destroy(krb_context,ccache);
198 krb5_free_principal(krb_context,princ);
201 if ((problem = krb5_cc_copy_cache(krb_context,
202 gssapi_client_creds->ccache,
204 log("krb5_cc_copy_cache(): %.100s",
205 krb5_get_err_text(krb_context,problem));
206 krb5_cc_destroy(krb_context,ccache);
210 if ((maj_status = gss_krb5_copy_ccache(&min_status,
213 log("gss_krb5_copy_ccache() failed");
214 ssh_gssapi_error(maj_status,min_status);
215 krb5_cc_destroy(krb_context,ccache);
220 krb5_cc_close(krb_context,ccache);
224 do_pam_putenv("KRB5CCNAME",name);
227 gssapi_cred_store.filename=strdup(ccname);
228 gssapi_cred_store.envvar="KRB5CCNAME";
229 gssapi_cred_store.envval=strdup(name);
237 #include <globus_gss_assist.h>
240 * Check if this user is OK to login under GSI. User has been authenticated
241 * as identity in global 'client_name.value' and is trying to log in as passed
242 * username in 'name'.
244 * Returns non-zero if user is authorized, 0 otherwise.
247 ssh_gssapi_gsi_userok(char *name)
251 /* This returns 0 on success */
252 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
255 debug("GSI user %s is%s authorized as target user %s",
256 (char *) gssapi_client_name.value,
257 (authorized ? "" : " not"),
264 * Handle setting up child environment for GSI.
266 * Make sure that this is called _after_ we've setuid to the user.
269 ssh_gssapi_gsi_storecreds()
271 OM_uint32 major_status;
272 OM_uint32 minor_status;
275 if (gssapi_client_creds != NULL)
277 char *creds_env = NULL;
280 * This is the current hack with the GSI gssapi library to
281 * export credentials to disk.
284 debug("Exporting delegated credentials");
286 minor_status = 0xdee0; /* Magic value */
288 gss_inquire_cred(&minor_status,
290 (gss_name_t *) &creds_env,
295 if ((major_status == GSS_S_COMPLETE) &&
296 (minor_status == 0xdee1) &&
302 * String is of the form:
303 * X509_USER_DELEG_PROXY=filename
304 * so we parse out the filename
305 * and then set X509_USER_PROXY
308 value = strchr(creds_env, '=');
315 do_pam_putenv("X509_USER_PROXY",value);
317 gssapi_cred_store.filename=NULL;
318 gssapi_cred_store.envvar="X509_USER_PROXY";
319 gssapi_cred_store.envval=strdup(value);
325 log("Failed to parse delegated credentials string '%s'",
331 log("Failed to export delegated credentials (error %ld)",
340 ssh_gssapi_cleanup_creds(void *ignored)
342 if (gssapi_cred_store.filename!=NULL) {
343 /* Unlink probably isn't sufficient */
344 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
345 unlink(gssapi_cred_store.filename);
350 ssh_gssapi_storecreds()
352 switch (gssapi_client_type) {
355 ssh_gssapi_krb5_storecreds();
360 ssh_gssapi_gsi_storecreds();
364 /* GSSAPI not used in this authentication */
365 debug("No GSSAPI credentials stored");
368 log("ssh_gssapi_do_child: Unknown mechanism");
372 if (options.gss_cleanup_creds) {
373 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
378 /* This allows GSSAPI methods to do things to the childs environment based
379 * on the passed authentication process and credentials.
381 * Question: If we didn't use userauth_external for some reason, should we
382 * still delegate credentials?
385 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
388 if (gssapi_cred_store.envvar!=NULL &&
389 gssapi_cred_store.envval!=NULL) {
391 debug("Setting %s to %s", gssapi_cred_store.envvar,
392 gssapi_cred_store.envval);
393 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
394 gssapi_cred_store.envval);
397 switch(gssapi_client_type) {
399 case GSS_KERBEROS: break;
405 debug("No GSSAPI credentials stored");
408 log("ssh_gssapi_do_child: Unknown mechanism");
413 ssh_gssapi_userok(char *user)
415 if (gssapi_client_name.length==0 ||
416 gssapi_client_name.value==NULL) {
417 debug("No suitable client data");
420 switch (gssapi_client_type) {
423 return(ssh_gssapi_krb5_userok(user));
424 break; /* Not reached */
428 return(ssh_gssapi_gsi_userok(user));
429 break; /* Not reached */
432 debug("Client not GSSAPI");
435 debug("Unknown client authentication type");
441 userauth_external(Authctxt *authctxt)
445 return(ssh_gssapi_userok(authctxt->user));
448 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
449 void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
451 /* We only support those mechanisms that we know about (ie ones that we know
452 * how to check local user kuserok and the like
455 userauth_gssapi(Authctxt *authctxt)
457 gss_OID_desc oid= {0,NULL};
460 gss_OID_set supported;
465 if (!authctxt->valid || authctxt->user == NULL)
467 mechs=packet_get_int();
469 debug("Mechanism negotiation is not supported");
473 ssh_gssapi_supported_oids(&supported);
477 oid.elements = packet_get_string(&len);
479 gss_test_oid_set_member(&ms, &oid, supported, &present);
481 } while (mechs>0 && !present);
488 ctxt=xmalloc(sizeof(Gssctxt));
489 authctxt->methoddata=(void *)ctxt;
491 ssh_gssapi_build_ctx(ctxt);
492 ssh_gssapi_set_oid(ctxt,&oid);
494 if (ssh_gssapi_acquire_cred(ctxt))
497 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
499 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
500 packet_put_string(oid.elements,oid.length);
505 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
506 &input_gssapi_token);
507 authctxt->postponed = 1;
513 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
515 Authctxt *authctxt = ctxt;
517 gss_buffer_desc send_tok,recv_tok;
518 OM_uint32 maj_status, min_status;
520 if (authctxt == NULL || authctxt->methoddata == NULL)
521 fatal("No authentication or GSSAPI context");
523 gssctxt=authctxt->methoddata;
525 recv_tok.value=packet_get_string(&recv_tok.length);
527 maj_status=ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, NULL);
530 if (GSS_ERROR(maj_status)) {
531 /* Failure <sniff> */
532 authctxt->postponed = 0;
533 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
534 userauth_finish(authctxt, 0, "gssapi");
537 if (send_tok.length != 0) {
538 /* Send a packet back to the client */
539 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
540 packet_put_string(send_tok.value,send_tok.length);
543 gss_release_buffer(&min_status, &send_tok);
546 if (maj_status == GSS_S_COMPLETE) {
547 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
548 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
549 &input_gssapi_exchange_complete);
553 /* This is called when the client thinks we've completed authentication.
554 * It should only be enabled in the dispatch handler by the function above,
555 * which only enables it once the GSSAPI exchange is complete.
559 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
561 Authctxt *authctxt = ctxt;
565 if (authctxt == NULL || authctxt->methoddata == NULL)
566 fatal("No authentication or GSSAPI context");
568 gssctxt=authctxt->methoddata;
570 /* This should never happen, but better safe than sorry. */
571 if (gssctxt->status != GSS_S_COMPLETE) {
572 packet_disconnect("Context negotiation is not complete");
575 if (ssh_gssapi_getclient(gssctxt,&gssapi_client_type,
577 &gssapi_client_creds)) {
578 fatal("Couldn't convert client name");
581 authenticated = ssh_gssapi_userok(authctxt->user);
583 authctxt->postponed = 0;
584 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
585 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
586 userauth_finish(authctxt, authenticated, "gssapi");