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>
47 extern ServerOptions options;
48 extern u_char *session_id2;
49 extern int session_id2_len;
52 typedef struct ssh_gssapi_cred_cache {
57 } ssh_gssapi_cred_cache;
59 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
66 #include <gssapi_krb5.h>
67 #define krb5_get_err_text(context,code) error_message(code)
70 static krb5_context krb_context = NULL;
72 /* Initialise the krb5 library, so we can use it for those bits that
75 int ssh_gssapi_krb5_init() {
76 krb5_error_code problem;
78 if (krb_context !=NULL)
81 problem = krb5_init_context(&krb_context);
83 log("Cannot initialize krb5 context");
86 krb5_init_ets(krb_context);
91 /* Check if this user is OK to login. This only works with krb5 - other
92 * GSSAPI mechanisms will need their own.
93 * Returns true if the user is OK to log in, otherwise returns 0
97 ssh_gssapi_krb5_userok(char *name) {
101 if (ssh_gssapi_krb5_init() == 0)
104 if ((retval=krb5_parse_name(krb_context, gssapi_client_name.value,
106 log("krb5_parse_name(): %.100s",
107 krb5_get_err_text(krb_context,retval));
110 if (krb5_kuserok(krb_context, princ, name)) {
112 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
113 (char *)gssapi_client_name.value);
118 krb5_free_principal(krb_context, princ);
122 /* Make sure that this is called _after_ we've setuid to the user */
124 /* This writes out any forwarded credentials. Its specific to the Kerberos
127 * We assume that our caller has made sure that the user has selected
128 * delegated credentials, and that the client_creds structure is correctly
133 ssh_gssapi_krb5_storecreds() {
135 krb5_error_code problem;
136 krb5_principal princ;
138 static char name[40];
140 OM_uint32 maj_status,min_status;
143 if (gssapi_client_creds==NULL) {
144 debug("No credentials stored");
148 if (ssh_gssapi_krb5_init() == 0)
151 if (options.gss_use_session_ccache) {
152 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
154 if ((tmpfd = mkstemp(ccname))==-1) {
155 log("mkstemp(): %.100s", strerror(errno));
158 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
159 log("fchmod(): %.100s", strerror(errno));
164 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
165 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
167 log("open(): %.100s", strerror(errno));
173 snprintf(name, sizeof(name), "FILE:%s",ccname);
175 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
176 log("krb5_cc_default(): %.100s",
177 krb5_get_err_text(krb_context,problem));
181 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
183 log("krb5_parse_name(): %.100s",
184 krb5_get_err_text(krb_context,problem));
185 krb5_cc_destroy(krb_context,ccache);
189 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
190 log("krb5_cc_initialize(): %.100s",
191 krb5_get_err_text(krb_context,problem));
192 krb5_free_principal(krb_context,princ);
193 krb5_cc_destroy(krb_context,ccache);
197 krb5_free_principal(krb_context,princ);
200 if ((problem = krb5_cc_copy_cache(krb_context,
201 gssapi_client_creds->ccache,
203 log("krb5_cc_copy_cache(): %.100s",
204 krb5_get_err_text(krb_context,problem));
205 krb5_cc_destroy(krb_context,ccache);
209 if ((maj_status = gss_krb5_copy_ccache(&min_status,
212 log("gss_krb5_copy_ccache() failed");
213 ssh_gssapi_error(maj_status,min_status);
214 krb5_cc_destroy(krb_context,ccache);
219 krb5_cc_close(krb_context,ccache);
223 do_pam_putenv("KRB5CCNAME",name);
226 gssapi_cred_store.filename=strdup(ccname);
227 gssapi_cred_store.envvar="KRB5CCNAME";
228 gssapi_cred_store.envval=strdup(name);
236 #include <globus_gss_assist.h>
239 * Check if this user is OK to login under GSI. User has been authenticated
240 * as identity in global 'client_name.value' and is trying to log in as passed
241 * username in 'name'.
243 * Returns non-zero if user is authorized, 0 otherwise.
246 ssh_gssapi_gsi_userok(char *name)
250 /* This returns 0 on success */
251 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
254 debug("GSI user %s is%s authorized as target user %s",
255 (char *) gssapi_client_name.value,
256 (authorized ? "" : " not"),
263 * Handle setting up child environment for GSI.
265 * Make sure that this is called _after_ we've setuid to the user.
268 ssh_gssapi_gsi_storecreds()
270 OM_uint32 major_status;
271 OM_uint32 minor_status;
274 if (gssapi_client_creds != NULL)
276 char *creds_env = NULL;
279 * This is the current hack with the GSI gssapi library to
280 * export credentials to disk.
283 debug("Exporting delegated credentials");
285 minor_status = 0xdee0; /* Magic value */
287 gss_inquire_cred(&minor_status,
289 (gss_name_t *) &creds_env,
294 if ((major_status == GSS_S_COMPLETE) &&
295 (minor_status == 0xdee1) &&
301 * String is of the form:
302 * X509_USER_DELEG_PROXY=filename
303 * so we parse out the filename
304 * and then set X509_USER_PROXY
307 value = strchr(creds_env, '=');
314 do_pam_putenv("X509_USER_PROXY",value);
316 gssapi_cred_store.filename=NULL;
317 gssapi_cred_store.envvar="X509_USER_PROXY";
318 gssapi_cred_store.envval=strdup(value);
324 log("Failed to parse delegated credentials string '%s'",
330 log("Failed to export delegated credentials (error %ld)",
339 ssh_gssapi_cleanup_creds(void *ignored)
341 if (gssapi_cred_store.filename!=NULL) {
342 /* Unlink probably isn't sufficient */
343 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
344 unlink(gssapi_cred_store.filename);
349 ssh_gssapi_storecreds()
351 switch (gssapi_client_type) {
354 ssh_gssapi_krb5_storecreds();
359 ssh_gssapi_gsi_storecreds();
363 /* GSSAPI not used in this authentication */
364 debug("No GSSAPI credentials stored");
367 log("ssh_gssapi_do_child: Unknown mechanism");
371 if (options.gss_cleanup_creds) {
372 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
377 /* This allows GSSAPI methods to do things to the childs environment based
378 * on the passed authentication process and credentials.
380 * Question: If we didn't use userauth_external for some reason, should we
381 * still delegate credentials?
384 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
387 if (gssapi_cred_store.envvar!=NULL &&
388 gssapi_cred_store.envval!=NULL) {
390 debug("Setting %s to %s", gssapi_cred_store.envvar,
391 gssapi_cred_store.envval);
392 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
393 gssapi_cred_store.envval);
396 switch(gssapi_client_type) {
398 case GSS_KERBEROS: break;
404 debug("No GSSAPI credentials stored");
407 log("ssh_gssapi_do_child: Unknown mechanism");
412 ssh_gssapi_userok(char *user)
414 if (gssapi_client_name.length==0 ||
415 gssapi_client_name.value==NULL) {
416 debug("No suitable client data");
419 switch (gssapi_client_type) {
422 return(ssh_gssapi_krb5_userok(user));
423 break; /* Not reached */
427 return(ssh_gssapi_gsi_userok(user));
428 break; /* Not reached */
431 debug("Client not GSSAPI");
434 debug("Unknown client authentication type");
440 userauth_external(Authctxt *authctxt)
444 return(ssh_gssapi_userok(authctxt->user));
447 void input_gssapi_token(int type, int plen, void *ctxt);
448 void input_gssapi_exchange_complete(int type, int plen, void *ctxt);
450 /* We only support those mechanisms that we know about (ie ones that we know
451 * how to check local user kuserok and the like
454 userauth_gssapi(Authctxt *authctxt)
456 gss_OID_desc oid= {0,NULL};
459 gss_OID_set supported;
463 if (!authctxt->valid || authctxt->user == NULL)
465 mechs=packet_get_int();
467 debug("Mechanism negotiation is not supported");
471 ssh_gssapi_supported_oids(&supported);
475 oid.elements = packet_get_string(&oid.length);
476 gss_test_oid_set_member(&ms, &oid, supported, &present);
478 } while (mechs>0 && !present);
485 ctxt=xmalloc(sizeof(Gssctxt));
486 authctxt->methoddata=(void *)ctxt;
488 ssh_gssapi_build_ctx(ctxt);
489 ssh_gssapi_set_oid(ctxt,&oid);
491 if (ssh_gssapi_acquire_cred(ctxt))
494 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
496 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
497 packet_put_string(oid.elements,oid.length);
502 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
503 &input_gssapi_token);
504 authctxt->postponed = 1;
510 input_gssapi_token(int type, int plen, void *ctxt)
512 Authctxt *authctxt = ctxt;
514 gss_buffer_desc send_tok,recv_tok;
515 OM_uint32 maj_status, min_status;
517 if (authctxt == NULL || authctxt->methoddata == NULL)
518 fatal("No authentication or GSSAPI context");
520 gssctxt=authctxt->methoddata;
522 recv_tok.value=packet_get_string(&recv_tok.length);
524 maj_status=ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, NULL);
527 if (GSS_ERROR(maj_status)) {
528 /* Failure <sniff> */
529 authctxt->postponed = 0;
530 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
531 userauth_finish(authctxt, 0, "gssapi");
534 if (send_tok.length != 0) {
535 /* Send a packet back to the client */
536 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
537 packet_put_string(send_tok.value,send_tok.length);
540 gss_release_buffer(&min_status, &send_tok);
543 if (maj_status == GSS_S_COMPLETE) {
544 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
545 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
546 &input_gssapi_exchange_complete);
550 /* This is called when the client thinks we've completed authentication.
551 * It should only be enabled in the dispatch handler by the function above,
552 * which only enables it once the GSSAPI exchange is complete.
556 input_gssapi_exchange_complete(int type, int plen, void *ctxt)
558 Authctxt *authctxt = ctxt;
562 if (authctxt == NULL || authctxt->methoddata == NULL)
563 fatal("No authentication or GSSAPI context");
565 gssctxt=authctxt->methoddata;
567 /* This should never happen, but better safe than sorry. */
568 if (gssctxt->status != GSS_S_COMPLETE) {
569 packet_disconnect("Context negotiation is not complete");
572 if (ssh_gssapi_getclient(gssctxt,&gssapi_client_type,
574 &gssapi_client_creds)) {
575 fatal("Couldn't convert client name");
578 authenticated = ssh_gssapi_userok(authctxt->user);
580 authctxt->postponed = 0;
581 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
582 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
583 userauth_finish(authctxt, authenticated, "gssapi");