summaryrefslogtreecommitdiffstats
path: root/src/plugins/hostrealm/test/main.c
blob: 3b36dce77deef422d567e0e927f5519f3fb2d42e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* plugins/hostrealm/test/main.c - test module for host-realm interface */
/*
 * Copyright (C) 2010,2013 by the Massachusetts Institute of Technology.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * * Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.
 *
 * * Redistributions in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * This file implements two hostrealm modules named "test1" and "test2".
 *
 * The first module returns multi-element lists.  For host_realm and
 * fallback_realm, it returns a list of the host's components in order.  For
 * default_realm, it returns a list containing "one" and "two".
 *
 * The second module tests error handling.  For host_realm and fallback_realm,
 * it returns a fatal error on hosts beginning with 'z', a list containing "a"
 * for hosts beginning with 'a', and passes control to later modules otherwise.
 * For default_realm, it returns a fatal error.
 */

#include <k5-int.h>
#include <krb5/hostrealm_plugin.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

static krb5_error_code
split_comps(krb5_context context, krb5_hostrealm_moddata data,
            const char *host, char ***realms_out)
{
    krb5_error_code ret;
    const char *p, *q;
    char *word = NULL, **list = NULL, **newptr;
    size_t count = 0;

    *realms_out = NULL;
    if (*host == '\0')
        return KRB5_PLUGIN_NO_HANDLE;
    p = host;
    while (TRUE) {
        q = strchr(p, '.');
        word = (q == NULL) ? strdup(p) : k5memdup0(p, q - p, &ret);
        if (word == NULL)
            goto oom;
        newptr = realloc(list, (count + 2) * sizeof(*list));
        if (newptr == NULL)
            goto oom;
        list = newptr;
        list[count++] = word;
        list[count] = NULL;
        word = NULL;
        if (q == NULL)
            break;
        p = q + 1;
    }
    *realms_out = list;
    return 0;

oom:
    krb5_free_host_realm(context, list);
    free(word);
    return ENOMEM;
}

static krb5_error_code
multi_defrealm(krb5_context context, krb5_hostrealm_moddata data,
               char ***realms_out)
{
    char **list = NULL, *one = NULL, *two = NULL;

    *realms_out = NULL;
    one = strdup("one");
    if (one == NULL)
        goto oom;
    two = strdup("two");
    if (two == NULL)
        goto oom;
    list = calloc(3, sizeof(*list));
    if (list == NULL)
        goto oom;
    list[0] = one;
    list[1] = two;
    list[2] = NULL;
    *realms_out = list;
    return 0;

oom:
    free(one);
    free(two);
    free(list);
    return ENOMEM;
}

static krb5_error_code
maybe_realm(krb5_context context, krb5_hostrealm_moddata data,
            const char *host, char ***realms_out)
{
    char **list, *a;

    *realms_out = NULL;
    if (*host == 'z')
        return KRB5_ERR_NO_SERVICE;
    if (*host != 'a')
        return KRB5_PLUGIN_NO_HANDLE;
    a = strdup("a");
    if (a == NULL)
        return ENOMEM;
    list = calloc(2, sizeof(*list));
    if (list == NULL) {
        free(a);
        return ENOMEM;
    }
    list[0] = a;
    list[1] = NULL;
    *realms_out = list;
    return 0;
}

static krb5_error_code
error(krb5_context context, krb5_hostrealm_moddata data, char ***realms_out)
{
    *realms_out = NULL;
    return KRB5_ERR_NO_SERVICE;
}

static void
free_realmlist(krb5_context context, krb5_hostrealm_moddata data,
               char **list)
{
    krb5_free_host_realm(context, list);
}

krb5_error_code
hostrealm_test1_initvt(krb5_context context, int maj_ver, int min_ver,
                       krb5_plugin_vtable vtable);
krb5_error_code
hostrealm_test2_initvt(krb5_context context, int maj_ver, int min_ver,
                       krb5_plugin_vtable vtable);

krb5_error_code
hostrealm_test1_initvt(krb5_context context, int maj_ver, int min_ver,
                       krb5_plugin_vtable vtable)
{
    krb5_hostrealm_vtable vt;

    if (maj_ver != 1)
        return KRB5_PLUGIN_VER_NOTSUPP;
    vt = (krb5_hostrealm_vtable)vtable;
    vt->name = "test1";
    vt->host_realm = split_comps;
    vt->fallback_realm = split_comps;
    vt->default_realm = multi_defrealm;
    vt->free_list = free_realmlist;
    return 0;
}

krb5_error_code
hostrealm_test2_initvt(krb5_context context, int maj_ver, int min_ver,
                       krb5_plugin_vtable vtable)
{
    krb5_hostrealm_vtable vt;

    if (maj_ver != 1)
        return KRB5_PLUGIN_VER_NOTSUPP;
    vt = (krb5_hostrealm_vtable)vtable;
    vt->name = "test2";
    vt->host_realm = maybe_realm;
    vt->default_realm = error;
    vt->free_list = free_realmlist;
    return 0;
}