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