| 1 |
/* |
| 2 |
* Copyright (c) 2015 Cryptonector LLC. |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* |
| 9 |
* 1. Redistributions of source code must retain the above copyright |
| 10 |
* notice, this list of conditions and the following disclaimer. |
| 11 |
* |
| 12 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 13 |
* notice, this list of conditions and the following disclaimer in the |
| 14 |
* documentation and/or other materials provided with the distribution. |
| 15 |
* |
| 16 |
* 3. The name Cryptonector LLC may not be used to endorse or promote |
| 17 |
* products derived from this software without specific prior written |
| 18 |
* permission. |
| 19 |
* |
| 20 |
* THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 21 |
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 22 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 23 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE |
| 24 |
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 |
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 |
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 27 |
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 28 |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
| 30 |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
*/ |
| 32 |
|
| 33 |
#ifdef HAVE_CONFIG_H |
| 34 |
#include <config.h> |
| 35 |
#endif |
| 36 |
|
| 37 |
#include <roken.h> |
| 38 |
#include <stdio.h> |
| 39 |
#include <stdlib.h> |
| 40 |
#include <string.h> |
| 41 |
#include <stdarg.h> |
| 42 |
#include <gssapi.h> |
| 43 |
#include <err.h> |
| 44 |
#include <getarg.h> |
| 45 |
|
| 46 |
static void |
| 47 |
print_gss_err(OM_uint32 stat, int status_type, gss_OID mech) |
| 48 |
{ |
| 49 |
gss_buffer_desc str; |
| 50 |
OM_uint32 maj; |
| 51 |
OM_uint32 min; |
| 52 |
OM_uint32 msg_ctx = 0; |
| 53 |
int first = 1; |
| 54 |
|
| 55 |
do { |
| 56 |
maj = gss_display_status(&min, stat, status_type, mech, &msg_ctx, |
| 57 |
&str); |
| 58 |
if (maj != GSS_S_COMPLETE) { |
| 59 |
fprintf(stderr, "Error displaying GSS %s error (%lu): %lu, %lu", |
| 60 |
status_type == GSS_C_GSS_CODE ? "major" : "minor", |
| 61 |
(unsigned long)stat, (unsigned long)maj, |
| 62 |
(unsigned long)min); |
| 63 |
return; |
| 64 |
} |
| 65 |
if (first) { |
| 66 |
fprintf(stderr, "GSS %s error: %.*s\n", |
| 67 |
status_type == GSS_C_GSS_CODE ? "major" : "minor", |
| 68 |
(int)str.length, (char *)str.value); |
| 69 |
first = 0; |
| 70 |
} else { |
| 71 |
fprintf(stderr, "\t%.*s\n", (int)str.length, (char *)str.value); |
| 72 |
} |
| 73 |
gss_release_buffer(&min, &str); |
| 74 |
} while (msg_ctx != 0); |
| 75 |
} |
| 76 |
|
| 77 |
static void |
| 78 |
print_gss_errs(OM_uint32 major, OM_uint32 minor, gss_OID mech) |
| 79 |
{ |
| 80 |
print_gss_err(major, GSS_C_GSS_CODE, GSS_C_NO_OID); |
| 81 |
print_gss_err(major, GSS_C_MECH_CODE, mech); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
gss_err(int exitval, OM_uint32 major, OM_uint32 minor, gss_OID mech, |
| 86 |
const char *fmt, ...) |
| 87 |
{ |
| 88 |
va_list args; |
| 89 |
|
| 90 |
va_start(args, fmt); |
| 91 |
vwarnx(fmt, args); |
| 92 |
va_end(args); |
| 93 |
print_gss_errs(major, minor, mech); |
| 94 |
exit(exitval); |
| 95 |
} |
| 96 |
|
| 97 |
static int version_flag = 0; |
| 98 |
static int help_flag = 0; |
| 99 |
|
| 100 |
static struct getargs args[] = { |
| 101 |
{"version", 0, arg_flag, &version_flag, "print version", NULL }, |
| 102 |
{"help", 0, arg_flag, &help_flag, NULL, NULL } |
| 103 |
}; |
| 104 |
|
| 105 |
static void |
| 106 |
usage(int ret) |
| 107 |
{ |
| 108 |
arg_printusage(args, sizeof(args)/sizeof(*args), |
| 109 |
NULL, "from_ccache to_ccache"); |
| 110 |
exit(ret); |
| 111 |
} |
| 112 |
|
| 113 |
int |
| 114 |
main(int argc, char **argv) |
| 115 |
{ |
| 116 |
OM_uint32 major, minor; |
| 117 |
gss_cred_id_t from_cred = GSS_C_NO_CREDENTIAL; |
| 118 |
gss_cred_id_t to_cred = GSS_C_NO_CREDENTIAL; |
| 119 |
gss_cred_id_t cred = GSS_C_NO_CREDENTIAL; |
| 120 |
char *from_env; |
| 121 |
char *to_env; |
| 122 |
int optidx = 0; |
| 123 |
|
| 124 |
setprogname(argv[0]); |
| 125 |
if (getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) |
| 126 |
usage(1); |
| 127 |
|
| 128 |
if (help_flag) |
| 129 |
usage (0); |
| 130 |
|
| 131 |
if (version_flag){ |
| 132 |
print_version(NULL); |
| 133 |
exit(0); |
| 134 |
} |
| 135 |
|
| 136 |
argc -= optidx; |
| 137 |
argv += optidx; |
| 138 |
|
| 139 |
if (argc < 2) |
| 140 |
errx(1, "required arguments missing"); |
| 141 |
if (argc > 2) |
| 142 |
errx(1, "too many arguments"); |
| 143 |
|
| 144 |
if (asprintf(&from_env, "KRB5CCNAME=%s", argv[0]) == -1 || from_env == NULL) |
| 145 |
err(1, "out of memory"); |
| 146 |
if (asprintf(&to_env, "KRB5CCNAME=%s", argv[1]) == -1 || to_env == NULL) |
| 147 |
err(1, "out of memory"); |
| 148 |
|
| 149 |
putenv(from_env); |
| 150 |
major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, |
| 151 |
GSS_KRB5_MECHANISM, GSS_C_INITIATE, GSS_C_INDEFINITE, |
| 152 |
GSS_C_INDEFINITE, &from_cred, NULL, NULL, NULL); |
| 153 |
if (major != GSS_S_COMPLETE) |
| 154 |
gss_err(1, major, minor, GSS_KRB5_MECHANISM, |
| 155 |
"failed to acquire creds from %s", argv[0]); |
| 156 |
|
| 157 |
putenv(to_env); |
| 158 |
major = gss_store_cred(&minor, from_cred, GSS_C_INITIATE, |
| 159 |
GSS_KRB5_MECHANISM, 1, 1, NULL, NULL); |
| 160 |
if (major != GSS_S_COMPLETE) |
| 161 |
gss_err(1, major, minor, GSS_KRB5_MECHANISM, |
| 162 |
"failed to store creds into %s", argv[1]); |
| 163 |
|
| 164 |
(void) gss_release_cred(&minor, &from_cred); |
| 165 |
(void) gss_release_cred(&minor, &to_cred); |
| 166 |
|
| 167 |
major = gss_add_cred(&minor, GSS_C_NO_CREDENTIAL, GSS_C_NO_NAME, |
| 168 |
GSS_KRB5_MECHANISM, GSS_C_INITIATE, GSS_C_INDEFINITE, |
| 169 |
GSS_C_INDEFINITE, &cred, NULL, NULL, NULL); |
| 170 |
if (major != GSS_S_COMPLETE) |
| 171 |
gss_err(1, major, minor, GSS_KRB5_MECHANISM, |
| 172 |
"failed to acquire creds from %s", argv[1]); |
| 173 |
(void) gss_release_cred(&minor, &cred); |
| 174 |
putenv("KRB5CCNAME"); |
| 175 |
free(from_env); |
| 176 |
free(to_env); |
| 177 |
|
| 178 |
return 0; |
| 179 |
} |