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>
49 extern ServerOptions options;
50 extern u_char *session_id2;
51 extern int session_id2_len;
54 typedef struct ssh_gssapi_cred_cache {
59 } ssh_gssapi_cred_cache;
61 static struct ssh_gssapi_cred_cache gssapi_cred_store = {NULL,NULL,NULL};
68 #include <gssapi_krb5.h>
69 #define krb5_get_err_text(context,code) error_message(code)
72 static krb5_context krb_context = NULL;
74 /* Initialise the krb5 library, so we can use it for those bits that
77 int ssh_gssapi_krb5_init() {
78 krb5_error_code problem;
80 if (krb_context !=NULL)
83 problem = krb5_init_context(&krb_context);
85 log("Cannot initialize krb5 context");
88 krb5_init_ets(krb_context);
93 /* Check if this user is OK to login. This only works with krb5 - other
94 * GSSAPI mechanisms will need their own.
95 * Returns true if the user is OK to log in, otherwise returns 0
99 ssh_gssapi_krb5_userok(char *name) {
100 krb5_principal princ;
103 if (ssh_gssapi_krb5_init() == 0)
106 if ((retval=krb5_parse_name(krb_context, gssapi_client_name.value,
108 log("krb5_parse_name(): %.100s",
109 krb5_get_err_text(krb_context,retval));
112 if (krb5_kuserok(krb_context, princ, name)) {
114 log("Authorized to %s, krb5 principal %s (krb5_kuserok)",name,
115 (char *)gssapi_client_name.value);
120 krb5_free_principal(krb_context, princ);
124 /* Make sure that this is called _after_ we've setuid to the user */
126 /* This writes out any forwarded credentials. Its specific to the Kerberos
129 * We assume that our caller has made sure that the user has selected
130 * delegated credentials, and that the client_creds structure is correctly
135 ssh_gssapi_krb5_storecreds() {
137 krb5_error_code problem;
138 krb5_principal princ;
140 static char name[40];
142 OM_uint32 maj_status,min_status;
145 if (gssapi_client_creds==NULL) {
146 debug("No credentials stored");
150 if (ssh_gssapi_krb5_init() == 0)
153 if (options.gss_use_session_ccache) {
154 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d_XXXXXX",geteuid());
156 if ((tmpfd = mkstemp(ccname))==-1) {
157 log("mkstemp(): %.100s", strerror(errno));
160 if (fchmod(tmpfd, S_IRUSR | S_IWUSR) == -1) {
161 log("fchmod(): %.100s", strerror(errno));
166 snprintf(ccname,sizeof(ccname),"/tmp/krb5cc_%d",geteuid());
167 tmpfd = open(ccname, O_TRUNC | O_CREAT, S_IRUSR | S_IWUSR);
169 log("open(): %.100s", strerror(errno));
175 snprintf(name, sizeof(name), "FILE:%s",ccname);
177 if ((problem = krb5_cc_resolve(krb_context, name, &ccache))) {
178 log("krb5_cc_default(): %.100s",
179 krb5_get_err_text(krb_context,problem));
183 if ((problem = krb5_parse_name(krb_context, gssapi_client_name.value,
185 log("krb5_parse_name(): %.100s",
186 krb5_get_err_text(krb_context,problem));
187 krb5_cc_destroy(krb_context,ccache);
191 if ((problem = krb5_cc_initialize(krb_context, ccache, princ))) {
192 log("krb5_cc_initialize(): %.100s",
193 krb5_get_err_text(krb_context,problem));
194 krb5_free_principal(krb_context,princ);
195 krb5_cc_destroy(krb_context,ccache);
199 krb5_free_principal(krb_context,princ);
202 if ((problem = krb5_cc_copy_cache(krb_context,
203 gssapi_client_creds->ccache,
205 log("krb5_cc_copy_cache(): %.100s",
206 krb5_get_err_text(krb_context,problem));
207 krb5_cc_destroy(krb_context,ccache);
211 if ((maj_status = gss_krb5_copy_ccache(&min_status,
214 log("gss_krb5_copy_ccache() failed");
215 ssh_gssapi_error(maj_status,min_status);
216 krb5_cc_destroy(krb_context,ccache);
221 krb5_cc_close(krb_context,ccache);
225 do_pam_putenv("KRB5CCNAME",name);
228 gssapi_cred_store.filename=strdup(ccname);
229 gssapi_cred_store.envvar="KRB5CCNAME";
230 gssapi_cred_store.envval=strdup(name);
238 #include <globus_gss_assist.h>
241 * Check if this user is OK to login under GSI. User has been authenticated
242 * as identity in global 'client_name.value' and is trying to log in as passed
243 * username in 'name'.
245 * Returns non-zero if user is authorized, 0 otherwise.
248 ssh_gssapi_gsi_userok(char *name)
252 /* This returns 0 on success */
253 authorized = (globus_gss_assist_userok(gssapi_client_name.value,
256 debug("GSI user %s is%s authorized as target user %s",
257 (char *) gssapi_client_name.value,
258 (authorized ? "" : " not"),
265 * Handle setting up child environment for GSI.
267 * Make sure that this is called _after_ we've setuid to the user.
270 ssh_gssapi_gsi_storecreds()
272 OM_uint32 major_status;
273 OM_uint32 minor_status;
276 if (gssapi_client_creds != NULL)
278 char *creds_env = NULL;
281 * This is the current hack with the GSI gssapi library to
282 * export credentials to disk.
285 debug("Exporting delegated credentials");
287 minor_status = 0xdee0; /* Magic value */
289 gss_inquire_cred(&minor_status,
291 (gss_name_t *) &creds_env,
296 if ((major_status == GSS_S_COMPLETE) &&
297 (minor_status == 0xdee1) &&
303 * String is of the form:
304 * X509_USER_DELEG_PROXY=filename
305 * so we parse out the filename
306 * and then set X509_USER_PROXY
309 value = strchr(creds_env, '=');
316 do_pam_putenv("X509_USER_PROXY",value);
318 gssapi_cred_store.filename=NULL;
319 gssapi_cred_store.envvar="X509_USER_PROXY";
320 gssapi_cred_store.envval=strdup(value);
326 log("Failed to parse delegated credentials string '%s'",
332 log("Failed to export delegated credentials (error %ld)",
341 ssh_gssapi_cleanup_creds(void *ignored)
343 if (gssapi_cred_store.filename!=NULL) {
344 /* Unlink probably isn't sufficient */
345 debug("removing gssapi cred file\"%s\"",gssapi_cred_store.filename);
346 unlink(gssapi_cred_store.filename);
351 ssh_gssapi_storecreds()
353 switch (gssapi_client_type) {
356 ssh_gssapi_krb5_storecreds();
361 ssh_gssapi_gsi_storecreds();
365 /* GSSAPI not used in this authentication */
366 debug("No GSSAPI credentials stored");
369 log("ssh_gssapi_do_child: Unknown mechanism");
373 if (options.gss_cleanup_creds) {
374 fatal_add_cleanup(ssh_gssapi_cleanup_creds, NULL);
379 /* This allows GSSAPI methods to do things to the childs environment based
380 * on the passed authentication process and credentials.
382 * Question: If we didn't use userauth_external for some reason, should we
383 * still delegate credentials?
386 ssh_gssapi_do_child(char ***envp, u_int *envsizep)
389 if (gssapi_cred_store.envvar!=NULL &&
390 gssapi_cred_store.envval!=NULL) {
392 debug("Setting %s to %s", gssapi_cred_store.envvar,
393 gssapi_cred_store.envval);
394 child_set_env(envp, envsizep, gssapi_cred_store.envvar,
395 gssapi_cred_store.envval);
398 switch(gssapi_client_type) {
400 case GSS_KERBEROS: break;
406 debug("No GSSAPI credentials stored");
409 log("ssh_gssapi_do_child: Unknown mechanism");
414 ssh_gssapi_userok(char *user)
416 if (gssapi_client_name.length==0 ||
417 gssapi_client_name.value==NULL) {
418 debug("No suitable client data");
421 switch (gssapi_client_type) {
424 return(ssh_gssapi_krb5_userok(user));
425 break; /* Not reached */
429 return(ssh_gssapi_gsi_userok(user));
430 break; /* Not reached */
433 debug("Client not GSSAPI");
436 debug("Unknown client authentication type");
442 userauth_external(Authctxt *authctxt)
446 return(ssh_gssapi_userok(authctxt->user));
449 void input_gssapi_token(int type, u_int32_t plen, void *ctxt);
450 void input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt);
452 /* We only support those mechanisms that we know about (ie ones that we know
453 * how to check local user kuserok and the like
456 userauth_gssapi(Authctxt *authctxt)
458 gss_OID_desc oid= {0,NULL};
461 gss_OID_set supported;
466 if (!authctxt->valid || authctxt->user == NULL)
469 if (datafellows & SSH_OLD_GSSAPI) {
470 debug("Early drafts of GSSAPI userauth not supported");
474 mechs=packet_get_int();
476 debug("Mechanism negotiation is not supported");
480 ssh_gssapi_supported_oids(&supported);
484 oid.elements = packet_get_string(&len);
486 gss_test_oid_set_member(&ms, &oid, supported, &present);
488 } while (mechs>0 && !present);
495 ctxt=xmalloc(sizeof(Gssctxt));
496 authctxt->methoddata=(void *)ctxt;
498 ssh_gssapi_build_ctx(ctxt);
499 ssh_gssapi_set_oid(ctxt,&oid);
501 if (ssh_gssapi_acquire_cred(ctxt))
504 /* Send SSH_MSG_USERAUTH_GSSAPI_RESPONSE */
506 packet_start(SSH2_MSG_USERAUTH_GSSAPI_RESPONSE);
507 packet_put_string(oid.elements,oid.length);
512 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,
513 &input_gssapi_token);
514 authctxt->postponed = 1;
520 input_gssapi_token(int type, u_int32_t plen, void *ctxt)
522 Authctxt *authctxt = ctxt;
524 gss_buffer_desc send_tok,recv_tok;
525 OM_uint32 maj_status, min_status;
527 if (authctxt == NULL || authctxt->methoddata == NULL)
528 fatal("No authentication or GSSAPI context");
530 gssctxt=authctxt->methoddata;
532 recv_tok.value=packet_get_string(&recv_tok.length);
534 maj_status=ssh_gssapi_accept_ctx(gssctxt, &recv_tok, &send_tok, NULL);
537 if (GSS_ERROR(maj_status)) {
538 /* Failure <sniff> */
539 authctxt->postponed = 0;
540 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
541 userauth_finish(authctxt, 0, "gssapi");
544 if (send_tok.length != 0) {
545 /* Send a packet back to the client */
546 packet_start(SSH2_MSG_USERAUTH_GSSAPI_TOKEN);
547 packet_put_string(send_tok.value,send_tok.length);
550 gss_release_buffer(&min_status, &send_tok);
553 if (maj_status == GSS_S_COMPLETE) {
554 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN,NULL);
555 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE,
556 &input_gssapi_exchange_complete);
560 /* This is called when the client thinks we've completed authentication.
561 * It should only be enabled in the dispatch handler by the function above,
562 * which only enables it once the GSSAPI exchange is complete.
566 input_gssapi_exchange_complete(int type, u_int32_t plen, void *ctxt)
568 Authctxt *authctxt = ctxt;
572 if (authctxt == NULL || authctxt->methoddata == NULL)
573 fatal("No authentication or GSSAPI context");
575 gssctxt=authctxt->methoddata;
577 /* This should never happen, but better safe than sorry. */
578 if (gssctxt->status != GSS_S_COMPLETE) {
579 packet_disconnect("Context negotiation is not complete");
582 if (ssh_gssapi_getclient(gssctxt,&gssapi_client_type,
584 &gssapi_client_creds)) {
585 fatal("Couldn't convert client name");
588 authenticated = ssh_gssapi_userok(authctxt->user);
590 authctxt->postponed = 0;
591 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_TOKEN, NULL);
592 dispatch_set(SSH2_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE, NULL);
593 userauth_finish(authctxt, authenticated, "gssapi");