test_sendto.c
1 /* 2 * Copyright (c) 2010 Kungliga Tekniska Högskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Portions Copyright (c) 2010 Apple Inc. 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 #include "krb5_locl.h" 36 #include <getarg.h> 37 38 /* 39 * 40 */ 41 42 static char *realm_string = NULL; 43 static char *uuid_string = NULL; 44 static char *signing_string = NULL; 45 static int use_large_flag = 0; 46 static int version_flag = 0; 47 static int help_flag = 0; 48 49 static struct getargs args[] = { 50 {"realm", 0, arg_string, &realm_string, 51 "realm", NULL }, 52 {"uuid", 0, arg_string, &uuid_string, 53 "uuid", NULL }, 54 {"signing-identity",0, arg_string, &signing_string, 55 "app-name", NULL }, 56 {"use-large", 0, arg_flag, &use_large_flag, 57 "use transport suitable for large packets", NULL }, 58 {"version", 0, arg_flag, &version_flag, 59 "print version", NULL }, 60 {"help", 0, arg_flag, &help_flag, 61 NULL, NULL } 62 }; 63 64 static void 65 usage (int ret) 66 { 67 arg_printusage (args, 68 sizeof(args)/sizeof(*args), 69 NULL, 70 ""); 71 exit (ret); 72 } 73 74 int 75 main(int argc, char **argv) 76 { 77 krb5_context context; 78 krb5_error_code ret; 79 int optidx = 0; 80 krb5_sendto_ctx ctx = NULL; 81 82 setprogname(argv[0]); 83 84 if(getarg(args, sizeof(args) / sizeof(args[0]), argc, argv, &optidx)) 85 usage(1); 86 87 if (help_flag) 88 usage (0); 89 90 if(version_flag){ 91 print_version(NULL); 92 exit(0); 93 } 94 95 ret = krb5_init_context (&context); 96 if (ret) 97 errx (1, "krb5_init_context failed: %d", ret); 98 99 100 if (realm_string == NULL) 101 errx(1, "missing realm"); 102 103 krb5_data send = { 104 .data = /* lha@WELLKNOWN:COM.APPLE.LKDC:localhost */ 105 "\x6a\x81\xb8\x30\x81\xb5\xa1\x03\x02\x01\x05\xa2\x03\x02\x01\x0a" 106 "\xa4\x81\xa8\x30\x81\xa5\xa0\x07\x03\x05\x00\x40\x01\x00\x00\xa1" 107 "\x10\x30\x0e\xa0\x03\x02\x01\x01\xa1\x07\x30\x05\x1b\x03\x6c\x68" 108 "\x61\xa2\x24\x1b\x22\x57\x45\x4c\x4c\x4b\x4e\x4f\x57\x4e\x3a\x43" 109 "\x4f\x4d\x2e\x41\x50\x50\x4c\x45\x2e\x4c\x4b\x44\x43\x3a\x6c\x6f" 110 "\x63\x61\x6c\x68\x6f\x73\x74\xa3\x37\x30\x35\xa0\x03\x02\x01\x01" 111 "\xa1\x2e\x30\x2c\x1b\x06\x6b\x72\x62\x74\x67\x74\x1b\x22\x57\x45" 112 "\x4c\x4c\x4b\x4e\x4f\x57\x4e\x3a\x43\x4f\x4d\x2e\x41\x50\x50\x4c" 113 "\x45\x2e\x4c\x4b\x44\x43\x3a\x6c\x6f\x63\x61\x6c\x68\x6f\x73\x74" 114 "\xa5\x11\x18\x0f\x32\x30\x31\x30\x30\x37\x33\x30\x31\x30\x33\x35" 115 "\x31\x38\x5a\xa7\x06\x02\x04\x3a\xcc\xb8\xba\xa8\x0e\x30\x0c\x02" 116 "\x01\x12\x02\x01\x11\x02\x01\x10\x02\x01\x17", 117 .length = 187 118 }, recv = { .data = NULL, .length = 0 }; 119 120 ret = krb5_sendto_ctx_alloc(context, &ctx); 121 if (ret) 122 krb5_err(context, 1, ret, "krb5_sendto_ctx_alloc"); 123 124 if (use_large_flag) 125 krb5_sendto_ctx_add_flags(ctx, KRB5_KRBHST_FLAGS_LARGE_MSG); 126 127 if (uuid_string || signing_string) { 128 krb5_uuid uuid; 129 if (uuid_string) 130 if (uuid_parse(uuid_string, uuid) != 0) 131 errx(1, "Failed to parse `%s` as a uuid", uuid_string); 132 ret = krb5_sendto_set_delegated_app(context, ctx, uuid_string ? uuid : NULL, 0, signing_string); 133 if (ret) 134 krb5_err(context, 1, ret, "krb5_sendto_set_delegated_app"); 135 } 136 137 ret = krb5_sendto_context(context, ctx, &send, realm_string, &recv); 138 if (ret) 139 krb5_err(context, 1, ret, "krb5_sendto_context"); 140 141 krb5_sendto_ctx_free(context, ctx); 142 143 krb5_free_context(context); 144 145 return 0; 146 }