1 /* 2 * Copyright (c) 2005 Kungliga Tekniska H�gskolan 3 * (Royal Institute of Technology, Stockholm, Sweden). 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * 3. Neither the name of KTH nor the names of its contributors may be 18 * used to endorse or promote products derived from this software without 19 * specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY KTH AND ITS CONTRIBUTORS ``AS IS'' AND ANY 22 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KTH OR ITS CONTRIBUTORS BE 25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 30 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 31 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 32 33 #include "krb5_locl.h" 34 #include <err.h> 35 36 RCSID("$Id: test_addr.c 15036 2005-04-30 15:19:58Z lha $"); 37 38 static void 39 print_addr(krb5_context context, const char *addr) 40 { 41 krb5_addresses addresses; 42 krb5_error_code ret; 43 char buf[38]; 44 char buf2[1000]; 45 size_t len; 46 int i; 47 48 ret = krb5_parse_address(context, addr, &addresses); 49 if (ret) 50 krb5_err(context, 1, ret, "krb5_parse_address"); 51 52 if (addresses.len < 1) 53 krb5_err(context, 1, ret, "too few addresses"); 54 55 for (i = 0; i < addresses.len; i++) { 56 krb5_print_address(&addresses.val[i], buf, sizeof(buf), &len); 57 #if 0 58 printf("addr %d: %s (%d/%d)\n", i, buf, (int)len, (int)strlen(buf)); 59 #endif 60 if (strlen(buf) > sizeof(buf)) 61 abort(); 62 krb5_print_address(&addresses.val[i], buf2, sizeof(buf2), &len); 63 #if 0 64 printf("addr %d: %s (%d/%d)\n", i, buf2, (int)len, (int)strlen(buf2)); 65 #endif 66 if (strlen(buf2) > sizeof(buf2)) 67 abort(); 68 69 } 70 krb5_free_addresses(context, &addresses); 71 72 } 73 74 static void 75 truncated_addr(krb5_context context, const char *addr, 76 size_t truncate_len, size_t outlen) 77 { 78 krb5_addresses addresses; 79 krb5_error_code ret; 80 char *buf; 81 size_t len; 82 83 buf = ecalloc(1, outlen + 1); 84 85 ret = krb5_parse_address(context, addr, &addresses); 86 if (ret) 87 krb5_err(context, 1, ret, "krb5_parse_address"); 88 89 if (addresses.len != 1) 90 krb5_err(context, 1, ret, "addresses should be one"); 91 92 krb5_print_address(&addresses.val[0], buf, truncate_len, &len); 93 94 #if 0 95 printf("addr %s (%d/%d)\n", buf, (int)len, (int)strlen(buf)); 96 #endif 97 98 if (truncate_len > strlen(buf) + 1) 99 abort(); 100 if (outlen != len) 101 abort(); 102 103 krb5_print_address(&addresses.val[0], buf, outlen + 1, &len); 104 105 #if 0 106 printf("addr %s (%d/%d)\n", buf, (int)len, (int)strlen(buf)); 107 #endif 108 109 if (len != outlen) 110 abort(); 111 if (strlen(buf) != len) 112 abort(); 113 114 krb5_free_addresses(context, &addresses); 115 free(buf); 116 } 117 118 static void 119 check_truncation(krb5_context context, const char *addr) 120 { 121 int i, len = strlen(addr); 122 123 for (i = 0; i < len; i++) 124 truncated_addr(context, addr, i, len); 125 } 126 127 static void 128 match_addr(krb5_context context, const char *range_addr, 129 const char *one_addr, int match) 130 { 131 krb5_addresses range, one; 132 krb5_error_code ret; 133 134 ret = krb5_parse_address(context, range_addr, &range); 135 if (ret) 136 krb5_err(context, 1, ret, "krb5_parse_address"); 137 138 if (range.len != 1) 139 krb5_err(context, 1, ret, "wrong num of addresses"); 140 141 ret = krb5_parse_address(context, one_addr, &one); 142 if (ret) 143 krb5_err(context, 1, ret, "krb5_parse_address"); 144 145 if (one.len != 1) 146 krb5_err(context, 1, ret, "wrong num of addresses"); 147 148 if (krb5_address_order(context, &range.val[0], &one.val[0]) == 0) { 149 if (!match) 150 krb5_errx(context, 1, "match when one shouldn't be"); 151 } else { 152 if (match) 153 krb5_errx(context, 1, "no match when one should be"); 154 } 155 156 krb5_free_addresses(context, &range); 157 krb5_free_addresses(context, &one); 158 } 159 160 int 161 main(int argc, char **argv) 162 { 163 krb5_context context; 164 krb5_error_code ret; 165 166 setprogname(argv[0]); 167 168 ret = krb5_init_context(&context); 169 if (ret) 170 errx (1, "krb5_init_context failed: %d", ret); 171 172 print_addr(context, "RANGE:127.0.0.0/8"); 173 print_addr(context, "RANGE:127.0.0.0/24"); 174 print_addr(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255"); 175 print_addr(context, "RANGE:130.237.237.4/29"); 176 #ifdef HAVE_IPV6 177 print_addr(context, "RANGE:fe80::209:6bff:fea0:e522/64"); 178 print_addr(context, "RANGE:IPv6:fe80::209:6bff:fea0:e522/64"); 179 print_addr(context, "RANGE:IPv6:fe80::-IPv6:fe80::ffff:ffff:ffff:ffff"); 180 print_addr(context, "RANGE:fe80::-fe80::ffff:ffff:ffff:ffff"); 181 #endif 182 183 check_truncation(context, "IPv4:127.0.0.0"); 184 check_truncation(context, "RANGE:IPv4:127.0.0.0-IPv4:127.0.0.255"); 185 #ifdef HAVE_IPV6 186 check_truncation(context, "IPv6:::1"); 187 check_truncation(context, "IPv6:fe80::ffff:ffff:ffff:ffff"); 188 #endif 189 190 match_addr(context, "RANGE:127.0.0.0/8", "inet:127.0.0.0", 1); 191 match_addr(context, "RANGE:127.0.0.0/8", "inet:127.255.255.255", 1); 192 match_addr(context, "RANGE:127.0.0.0/8", "inet:128.0.0.0", 0); 193 194 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.7", 0); 195 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.8", 1); 196 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.15", 1); 197 match_addr(context, "RANGE:130.237.237.8/29", "inet:130.237.237.16", 0); 198 199 krb5_free_context(context); 200 201 return 0; 202 } 203