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