summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/generic/util_errmap.c
blob: 5a863b6c08f613c62a1a1c9051706af4a71b01f0 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * Copyright 2007, 2008 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 *
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  Furthermore if you modify this software you must label
 * your software as modified software and not distribute it in such a
 * fashion that it might be confused with the original M.I.T. software.
 * M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 *
 */

#include "gssapiP_generic.h"
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif

/* The mapping table is 0-based, but let's export codes that are
   1-based, keeping 0 for errors or unknown errors.

   The elements in the mapping table currently have separate copies of
   each OID stored.  This is a bit wasteful, but we are assuming the
   table isn't likely to grow very large.  */

struct mecherror {
    gss_OID_desc mech;
    OM_uint32 code;
};

static inline int
cmp_OM_uint32(OM_uint32 m1, OM_uint32 m2)
{
    if (m1 < m2)
        return -1;
    else if (m1 > m2)
        return 1;
    else
        return 0;
}

static inline int
mecherror_cmp(struct mecherror m1, struct mecherror m2)
{
    if (m1.code < m2.code)
        return -1;
    if (m1.code > m2.code)
        return 1;
    if (m1.mech.length < m2.mech.length)
        return -1;
    if (m1.mech.length > m2.mech.length)
        return 1;
    if (m1.mech.length == 0)
        return 0;
    return memcmp(m1.mech.elements, m2.mech.elements, m1.mech.length);
}

static void
print_OM_uint32 (OM_uint32 value, FILE *f)
{
    fprintf(f, "%lu", (unsigned long) value);
}

static inline int
mecherror_copy(struct mecherror *dest, struct mecherror src)
{
    *dest = src;
    dest->mech.elements = malloc(src.mech.length);
    if (dest->mech.elements == NULL) {
        if (src.mech.length)
            return ENOMEM;
        else
            return 0;
    }
    memcpy(dest->mech.elements, src.mech.elements, src.mech.length);
    return 0;
}

static void
mecherror_print(struct mecherror value, FILE *f)
{
    OM_uint32 minor;
    gss_buffer_desc str;
    static const struct {
        const char *oidstr, *name;
    } mechnames[] = {
        { "{ 1 2 840 113554 1 2 2 }", "krb5-new" },
        { "{ 1 3 5 1 5 2 }", "krb5-old" },
        { "{ 1 2 840 48018 1 2 2 }", "krb5-microsoft" },
        { "{ 1 3 6 1 5 5 2 }", "spnego" },
    };
    unsigned int i;

    fprintf(f, "%lu@", (unsigned long) value.code);

    if (value.mech.length == 0) {
        fprintf(f, "(com_err)");
        return;
    }
    fprintf(f, "%p=", value.mech.elements);
    if (generic_gss_oid_to_str(&minor, &value.mech, &str)) {
        fprintf(f, "(error in conversion)");
        return;
    }
    /* Note: generic_gss_oid_to_str returns a null-terminated string.  */
    for (i = 0; i < sizeof(mechnames)/sizeof(mechnames[0]); i++) {
        if (!strcmp(str.value, mechnames[i].oidstr) && mechnames[i].name != 0) {
            fprintf(f, "%s", mechnames[i].name);
            break;
        }
    }
    if (i == sizeof(mechnames)/sizeof(mechnames[0]))
        fprintf(f, "%s", (char *) str.value);
    generic_gss_release_buffer(&minor, &str);
}

#include "errmap.h"
#include "krb5.h"               /* for KRB5KRB_AP_WRONG_PRINC */

static mecherrmap m;
static k5_mutex_t mutex = K5_MUTEX_PARTIAL_INITIALIZER;
static OM_uint32 next_fake = 100000;

int gssint_mecherrmap_init(void)
{
    int err;

    err = mecherrmap_init(&m);
    if (err)
        return err;
    err = k5_mutex_finish_init(&mutex);
    if (err) {
        mecherrmap_destroy(&m);
        return err;
    }

    return 0;
}

/* Currently the enumeration template doesn't handle freeing
   element storage when destroying the collection.  */
static int free_one(OM_uint32 i, struct mecherror value, void *p)
{
    if (value.mech.length && value.mech.elements)
        free(value.mech.elements);
    return 0;
}

void gssint_mecherrmap_destroy(void)
{
    mecherrmap_foreach(&m, free_one, NULL);
    mecherrmap_destroy(&m);
    k5_mutex_destroy(&mutex);
}

OM_uint32 gssint_mecherrmap_map(OM_uint32 minor, const gss_OID_desc * oid)
{
    const struct mecherror *mep;
    struct mecherror me, me_copy;
    const OM_uint32 *p;
    int err;
    OM_uint32 new_status;

#ifdef DEBUG
    FILE *f;
    f = fopen("/dev/pts/9", "w+");
    if (f == NULL)
        f = stderr;
#endif

    me.code = minor;
    me.mech = *oid;
    err = k5_mutex_lock(&mutex);
    if (err) {
#ifdef DEBUG
        if (f != stderr) fclose(f);
#endif
        return 0;
    }

    /* Is this status+oid already mapped?  */
    p = mecherrmap_findright(&m, me);
    if (p != NULL) {
        k5_mutex_unlock(&mutex);
#ifdef DEBUG
        fprintf(f, "%s: found ", __func__);
        mecherror_print(me, f);
        fprintf(f, " in map as %lu\n", (unsigned long) *p);
        if (f != stderr) fclose(f);
#endif
        return *p;
    }
    /* Is this status code already mapped to something else
       mech-specific?  */
    mep = mecherrmap_findleft(&m, minor);
    if (mep == NULL) {
        /* Map it to itself plus this mech-oid.  */
        new_status = minor;
    } else {
        /* Already assigned.  Pick a fake new value and map it.  */
        /* There's a theoretical infinite loop risk here, if we fill
           in 2**32 values.  Also, returning 0 has a special
           meaning.  */
        do {
            next_fake++;
            new_status = next_fake;
            if (new_status == 0)
                /* ??? */;
        } while (mecherrmap_findleft(&m, new_status) != NULL);
    }
    err = mecherror_copy(&me_copy, me);
    if (err) {
        k5_mutex_unlock(&mutex);
        return err;
    }
    err = mecherrmap_add(&m, new_status, me_copy);
    k5_mutex_unlock(&mutex);
    if (err) {
        if (me_copy.mech.length)
            free(me_copy.mech.elements);
    }
#ifdef DEBUG
    fprintf(f, "%s: mapping ", __func__);
    mecherror_print(me, f);
    fprintf(f, " to %lu: err=%d\nnew map: ", (unsigned long) new_status, err);
    mecherrmap_printmap(&m, f);
    fprintf(f, "\n");
    if (f != stderr) fclose(f);
#endif
    if (err)
        return 0;
    else
        return new_status;
}

static gss_OID_desc no_oid = { 0, 0 };
OM_uint32 gssint_mecherrmap_map_errcode(OM_uint32 errcode)
{
    return gssint_mecherrmap_map(errcode, &no_oid);
}

int gssint_mecherrmap_get(OM_uint32 minor, gss_OID mech_oid,
                          OM_uint32 *mech_minor)
{
    const struct mecherror *p;
    int err;

    if (minor == 0) {
        return EINVAL;
    }
    err = k5_mutex_lock(&mutex);
    if (err)
        return err;
    p = mecherrmap_findleft(&m, minor);
    k5_mutex_unlock(&mutex);
    if (!p) {
        return EINVAL;
    }
    *mech_oid = p->mech;
    *mech_minor = p->code;
    return 0;
}