1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 /* plugins/hostrealm/test/main.c - test module for host-realm interface */ 3 /* 4 * Copyright (C) 2010,2013 by the Massachusetts Institute of Technology. 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 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 14 * * Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the 17 * distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 22 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 23 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 30 * OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 /* 34 * This file implements two hostrealm modules named "test1" and "test2". 35 * 36 * The first module returns multi-element lists. For host_realm and 37 * fallback_realm, it returns a list of the host's components in order. For 38 * default_realm, it returns a list containing "one" and "two". 39 * 40 * The second module tests error handling. For host_realm and fallback_realm, 41 * it returns a fatal error on hosts beginning with 'z', a list containing "a" 42 * for hosts beginning with 'a', and passes control to later modules otherwise. 43 * For default_realm, it returns a fatal error. 44 */ 45 46 #include <k5-int.h> 47 #include <krb5/hostrealm_plugin.h> 48 #include <sys/types.h> 49 #include <sys/stat.h> 50 #include <fcntl.h> 51 #include <unistd.h> 52 53 static krb5_error_code 54 split_comps(krb5_context context, krb5_hostrealm_moddata data, 55 const char *host, char ***realms_out) 56 { 57 krb5_error_code ret; 58 const char *p, *q; 59 char *word = NULL, **list = NULL, **newptr; 60 size_t count = 0; 61 62 *realms_out = NULL; 63 if (*host == '\0') 64 return KRB5_PLUGIN_NO_HANDLE; 65 p = host; 66 while (TRUE) { 67 q = strchr(p, '.'); 68 word = (q == NULL) ? strdup(p) : k5memdup0(p, q - p, &ret); 69 if (word == NULL) 70 goto oom; 71 newptr = realloc(list, (count + 2) * sizeof(*list)); 72 if (newptr == NULL) 73 goto oom; 74 list = newptr; 75 list[count++] = word; 76 list[count] = NULL; 77 word = NULL; 78 if (q == NULL) 79 break; 80 p = q + 1; 81 } 82 *realms_out = list; 83 return 0; 84 85 oom: 86 krb5_free_host_realm(context, list); 87 free(word); 88 return ENOMEM; 89 } 90 91 static krb5_error_code 92 multi_defrealm(krb5_context context, krb5_hostrealm_moddata data, 93 char ***realms_out) 94 { 95 char **list = NULL, *one = NULL, *two = NULL; 96 97 *realms_out = NULL; 98 one = strdup("one"); 99 if (one == NULL) 100 goto oom; 101 two = strdup("two"); 102 if (two == NULL) 103 goto oom; 104 list = calloc(3, sizeof(*list)); 105 if (list == NULL) 106 goto oom; 107 list[0] = one; 108 list[1] = two; 109 list[2] = NULL; 110 *realms_out = list; 111 return 0; 112 113 oom: 114 free(one); 115 free(two); 116 free(list); 117 return ENOMEM; 118 } 119 120 static krb5_error_code 121 maybe_realm(krb5_context context, krb5_hostrealm_moddata data, 122 const char *host, char ***realms_out) 123 { 124 char **list, *a; 125 126 *realms_out = NULL; 127 if (*host == 'z') 128 return KRB5_ERR_NO_SERVICE; 129 if (*host != 'a') 130 return KRB5_PLUGIN_NO_HANDLE; 131 a = strdup("a"); 132 if (a == NULL) 133 return ENOMEM; 134 list = calloc(2, sizeof(*list)); 135 if (list == NULL) { 136 free(a); 137 return ENOMEM; 138 } 139 list[0] = a; 140 list[1] = NULL; 141 *realms_out = list; 142 return 0; 143 } 144 145 static krb5_error_code 146 error(krb5_context context, krb5_hostrealm_moddata data, char ***realms_out) 147 { 148 *realms_out = NULL; 149 return KRB5_ERR_NO_SERVICE; 150 } 151 152 static void 153 free_realmlist(krb5_context context, krb5_hostrealm_moddata data, 154 char **list) 155 { 156 krb5_free_host_realm(context, list); 157 } 158 159 krb5_error_code 160 hostrealm_test1_initvt(krb5_context context, int maj_ver, int min_ver, 161 krb5_plugin_vtable vtable); 162 krb5_error_code 163 hostrealm_test2_initvt(krb5_context context, int maj_ver, int min_ver, 164 krb5_plugin_vtable vtable); 165 166 krb5_error_code 167 hostrealm_test1_initvt(krb5_context context, int maj_ver, int min_ver, 168 krb5_plugin_vtable vtable) 169 { 170 krb5_hostrealm_vtable vt; 171 172 if (maj_ver != 1) 173 return KRB5_PLUGIN_VER_NOTSUPP; 174 vt = (krb5_hostrealm_vtable)vtable; 175 vt->name = "test1"; 176 vt->host_realm = split_comps; 177 vt->fallback_realm = split_comps; 178 vt->default_realm = multi_defrealm; 179 vt->free_list = free_realmlist; 180 return 0; 181 } 182 183 krb5_error_code 184 hostrealm_test2_initvt(krb5_context context, int maj_ver, int min_ver, 185 krb5_plugin_vtable vtable) 186 { 187 krb5_hostrealm_vtable vt; 188 189 if (maj_ver != 1) 190 return KRB5_PLUGIN_VER_NOTSUPP; 191 vt = (krb5_hostrealm_vtable)vtable; 192 vt->name = "test2"; 193 vt->host_realm = maybe_realm; 194 vt->default_realm = error; 195 vt->free_list = free_realmlist; 196 return 0; 197 } 198