/* * Copyright (c) 1997 - 2000, 2002 Kungliga Tekniska Högskolan * (Royal Institute of Technology, Stockholm, Sweden). * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * 3. Neither the name of the Institute nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $Source: /afs/pdc.kth.se/cluster/povel/src/kdeposit/RCS/kdeposit.c,v $ * */ #include "kdeposit.h" krb5_context context; const char *service = KF_SERVICE; int remote_forwardable = 1; static char *progname; char* tag = NULL; static char* usage = "kdeposit -t tag -s server [-g] [-h]\n\n" "-t identifying tag for remote side (typically job id)\n" "-g make forwarded tickets non-forwardable \n" "-h prints this help\n" "-s server to forward tickets to\n"; /* * forward creds to `hostname'/`service' over `sock' * return 0 iff OK */ static int forward_cred (int sock, const char *hostname, const char *service, char *message, size_t len) { krb5_auth_context auth_context; krb5_error_code status; krb5_principal server; krb5_data data; krb5_data data_send; krb5_ccache ccache; krb5_creds creds; krb5_kdc_flags flags; krb5_principal principal; status = krb5_auth_con_init (context, &auth_context); if (status) { krb5_warn (context, status, "krb5_auth_con_init"); return 1; } status = krb5_auth_con_setaddrs_from_fd (context, auth_context, &sock); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_auth_con_setaddr"); return 1; } status = krb5_sname_to_principal (context, hostname, service, KRB5_NT_SRV_HST, &server); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_sname_to_principal"); return 1; } status = krb5_sendauth (context, &auth_context, &sock, KF_VERSION_1, NULL, server, AP_OPTS_MUTUAL_REQUIRED | AP_OPTS_USE_SUBKEY, NULL, NULL, NULL, NULL, NULL, NULL); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn(context, status, "krb5_sendauth"); return 1; } data_send.data = (void *) tag; data_send.length = strlen(tag)+1; status = krb5_write_priv_message(context, auth_context, &sock, &data_send); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_write_message"); return 1; } memset (&creds, 0, sizeof(creds)); status = krb5_cc_default (context, &ccache); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_cc_default"); return 1; } status = krb5_cc_get_principal (context, ccache, &principal); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_cc_get_principal"); return 1; } creds.client = principal; status = krb5_make_principal (context, &creds.server, principal->realm, KRB5_TGS_NAME, principal->realm, NULL); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_make_principal"); return 1; } creds.times.endtime = 0; flags.i = 0; flags.b.forwarded = 1; flags.b.forwardable = remote_forwardable; status = krb5_get_forwarded_creds (context, auth_context, ccache, flags.i, hostname, &creds, &data); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_get_forwarded_creds"); return 1; } status = krb5_write_priv_message(context, auth_context, &sock, &data); if (status) { krb5_auth_con_free(context, auth_context); krb5_warn (context, status, "krb5_mk_priv"); return 1; } krb5_data_free (&data); status = krb5_read_priv_message(context, auth_context, &sock, &data); krb5_auth_con_free(context, auth_context); if (status) { krb5_warn (context, status, "krb5_mk_priv"); return 1; } if(data.length >= len) { krb5_warnx (context, "returned string is too long, truncating"); memcpy(message, data.data, len); message[len - 1] = '\0'; } else { memcpy(message, data.data, data.length); message[data.length] = '\0'; } krb5_data_free (&data); return(strcmp(message, "ok")); } int main(int argc, char **argv) { int port, status; int ret = 0; struct addrinfo *ai, *a; struct addrinfo hints; char* hostname = NULL; int error; char portstr[NI_MAXSERV]; int c; if (argc > 0) { progname = argv[0]; } status = krb5_init_context (&context); if (status) errx(1, "krb5_init_context failed: %d", status); remote_forwardable = krb5_config_get_bool (context, NULL, "libdefaults", "forwardable", NULL); while ((c = getopt (argc, argv, "ght:s:")) != -1) { switch (c) { case 'g': remote_forwardable = 0; break; case 'h': fprintf(stderr, "%s", usage); exit(0); break; case 't': if ((tag = strdup(optarg)) == NULL) { perror(progname); exit(1); } break; case 's': if ((hostname = strdup(optarg)) == NULL) { perror(progname); exit(1); } break; default: fprintf(stderr, "Unable to parse command line\n\n%s", usage); exit(1); break; } } if (tag == NULL) { fprintf(stderr, "Missing tag\n\n%s", usage); exit(1); } if (hostname == NULL) { fprintf(stderr, "Missing server to forward to\n\n%s", usage); exit(1); } port = krb5_getportbyname (context, KF_PORT_NAME, "tcp", KF_PORT_NUM); memset (&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; snprintf (portstr, sizeof(portstr), "%u", ntohs(port)); if ((error = getaddrinfo (hostname, portstr, &hints, &ai)) != 0) { fprintf(stderr, "getaddrinfo(%s): %s", hostname, gai_strerror(error)); exit (1); } for (a = ai; a != NULL; a = a->ai_next) { int s; s = socket (a->ai_family, a->ai_socktype, a->ai_protocol); if (s < 0) continue; if (connect (s, a->ai_addr, a->ai_addrlen) < 0) { warn ("connect(%s)", hostname); close (s); continue; } freeaddrinfo (ai); char message[128]; return forward_cred (s, hostname, service, message, sizeof(message)); } warnx ("failed to contact %s", hostname); freeaddrinfo (ai); return ret; }