1 /* $NetBSD: test_cred.c,v 1.1.1.2 2014/04/24 12:45:29 pettai Exp $ */ 2 3 /* 4 * Copyright (c) 2003-2004 Kungliga Tekniska Högskolan 5 * (Royal Institute of Technology, Stockholm, Sweden). 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * 3. Neither the name of KTH nor the names of its contributors may be 20 * used to endorse or promote products derived from this software without 21 * specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 24 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 26 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 27 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 28 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 29 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 30 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 31 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 32 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 33 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 34 */ 35 36 #ifdef HAVE_CONFIG_H 37 #include <config.h> 38 #endif 39 40 #include <krb5/roken.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <stdarg.h> 45 #include <gssapi/gssapi.h> 46 #include <gssapi/gssapi_krb5.h> 47 #include <gssapi/gssapi_spnego.h> 48 #include <err.h> 49 #include <krb5/getarg.h> 50 51 static void 52 gss_print_errors (int min_stat) 53 { 54 OM_uint32 new_stat; 55 OM_uint32 msg_ctx = 0; 56 gss_buffer_desc status_string; 57 OM_uint32 ret; 58 59 do { 60 ret = gss_display_status (&new_stat, 61 min_stat, 62 GSS_C_MECH_CODE, 63 GSS_C_NO_OID, 64 &msg_ctx, 65 &status_string); 66 if (!GSS_ERROR(ret)) { 67 fprintf (stderr, "%.*s\n", (int)status_string.length, 68 (char *)status_string.value); 69 gss_release_buffer (&new_stat, &status_string); 70 } 71 } while (!GSS_ERROR(ret) && msg_ctx != 0); 72 } 73 74 static void 75 gss_err(int exitval, int status, const char *fmt, ...) 76 { 77 va_list args; 78 79 va_start(args, fmt); 80 vwarnx (fmt, args); 81 gss_print_errors (status); 82 va_end(args); 83 exit (exitval); 84 } 85 86 static void 87 acquire_release_loop(gss_name_t name, int counter, gss_cred_usage_t usage) 88 { 89 OM_uint32 maj_stat, min_stat; 90 gss_cred_id_t cred; 91 int i; 92 93 for (i = 0; i < counter; i++) { 94 maj_stat = gss_acquire_cred(&min_stat, name, 95 GSS_C_INDEFINITE, 96 GSS_C_NO_OID_SET, 97 usage, 98 &cred, 99 NULL, 100 NULL); 101 if (maj_stat != GSS_S_COMPLETE) 102 gss_err(1, min_stat, "aquire %d %d != GSS_S_COMPLETE", 103 i, (int)maj_stat); 104 105 maj_stat = gss_release_cred(&min_stat, &cred); 106 if (maj_stat != GSS_S_COMPLETE) 107 gss_err(1, min_stat, "release %d %d != GSS_S_COMPLETE", 108 i, (int)maj_stat); 109 } 110 } 111 112 113 static void 114 acquire_add_release_add(gss_name_t name, gss_cred_usage_t usage) 115 { 116 OM_uint32 maj_stat, min_stat; 117 gss_cred_id_t cred, cred2, cred3; 118 119 maj_stat = gss_acquire_cred(&min_stat, name, 120 GSS_C_INDEFINITE, 121 GSS_C_NO_OID_SET, 122 usage, 123 &cred, 124 NULL, 125 NULL); 126 if (maj_stat != GSS_S_COMPLETE) 127 gss_err(1, min_stat, "aquire %d != GSS_S_COMPLETE", (int)maj_stat); 128 129 maj_stat = gss_add_cred(&min_stat, 130 cred, 131 GSS_C_NO_NAME, 132 GSS_KRB5_MECHANISM, 133 usage, 134 GSS_C_INDEFINITE, 135 GSS_C_INDEFINITE, 136 &cred2, 137 NULL, 138 NULL, 139 NULL); 140 141 if (maj_stat != GSS_S_COMPLETE) 142 gss_err(1, min_stat, "add_cred %d != GSS_S_COMPLETE", (int)maj_stat); 143 144 maj_stat = gss_release_cred(&min_stat, &cred); 145 if (maj_stat != GSS_S_COMPLETE) 146 gss_err(1, min_stat, "release %d != GSS_S_COMPLETE", (int)maj_stat); 147 148 maj_stat = gss_add_cred(&min_stat, 149 cred2, 150 GSS_C_NO_NAME, 151 GSS_KRB5_MECHANISM, 152 GSS_C_BOTH, 153 GSS_C_INDEFINITE, 154 GSS_C_INDEFINITE, 155 &cred3, 156 NULL, 157 NULL, 158 NULL); 159 160 maj_stat = gss_release_cred(&min_stat, &cred2); 161 if (maj_stat != GSS_S_COMPLETE) 162 gss_err(1, min_stat, "release 2 %d != GSS_S_COMPLETE", (int)maj_stat); 163 164 maj_stat = gss_release_cred(&min_stat, &cred3); 165 if (maj_stat != GSS_S_COMPLETE) 166 gss_err(1, min_stat, "release 2 %d != GSS_S_COMPLETE", (int)maj_stat); 167 } 168 169 static int version_flag = 0; 170 static int help_flag = 0; 171 172 static struct getargs args[] = { 173 {"version", 0, arg_flag, &version_flag, "print version", NULL }, 174 {"help", 0, arg_flag, &help_flag, NULL, NULL } 175 }; 176 177 static void 178 usage (int ret) 179 { 180 arg_printusage (args, sizeof(args)/sizeof(*args), 181 NULL, "service@host"); 182 exit (ret); 183 } 184 185 186 int 187 main(int argc, char **argv) 188 { 189 struct gss_buffer_desc_struct name_buffer; 190 OM_uint32 maj_stat, min_stat; 191 gss_name_t name; 192 int optidx = 0; 193 194 setprogname(argv[0]); 195 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 196 usage(1); 197 198 if (help_flag) 199 usage (0); 200 201 if(version_flag){ 202 print_version(NULL); 203 exit(0); 204 } 205 206 argc -= optidx; 207 argv += optidx; 208 209 if (argc < 1) 210 errx(1, "argc < 1"); 211 212 name_buffer.value = argv[0]; 213 name_buffer.length = strlen(argv[0]); 214 215 maj_stat = gss_import_name(&min_stat, &name_buffer, 216 GSS_C_NT_HOSTBASED_SERVICE, 217 &name); 218 if (maj_stat != GSS_S_COMPLETE) 219 errx(1, "import name error"); 220 221 acquire_release_loop(name, 100, GSS_C_ACCEPT); 222 acquire_release_loop(name, 100, GSS_C_INITIATE); 223 acquire_release_loop(name, 100, GSS_C_BOTH); 224 225 acquire_add_release_add(name, GSS_C_ACCEPT); 226 acquire_add_release_add(name, GSS_C_INITIATE); 227 acquire_add_release_add(name, GSS_C_BOTH); 228 229 gss_release_name(&min_stat, &name); 230 231 return 0; 232 } 233