summaryrefslogtreecommitdiffstats
path: root/src/lib/gssapi/generic/disp_major_status.c
blob: 848af8fa5526d0948e1b696a21e0e734df55c087 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
/*
 * Copyright 1993 by OpenVision Technologies, Inc.
 *
 * Permission to use, copy, modify, distribute, and sell this software
 * and its documentation for any purpose is hereby granted without fee,
 * provided that the above copyright notice appears in all copies and
 * that both that copyright notice and this permission notice appear in
 * supporting documentation, and that the name of OpenVision not be used
 * in advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission. OpenVision makes no
 * representations about the suitability of this software for any
 * purpose.  It is provided "as is" without express or implied warranty.
 *
 * OPENVISION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL OPENVISION BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
 * USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
 * OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "gssapiP_generic.h"
#include <string.h>
#include <stdio.h>

/*
 * $Id$
 */

/* This code has knowledge of the min and max errors of each type
   within the gssapi major status */

#define GSS_ERROR_STR(value, array, select, min, max, num)              \
    (((select(value) < (min)) || (select(value) > (max))) ? NULL :      \
     _((array)[num(value)]))

/**/

static const char * const calling_error_string[] = {
    NULL,
    N_("A required input parameter could not be read"),
    N_("A required input parameter could not be written"),
    N_("A parameter was malformed"),
};

static const char * const calling_error = N_("calling error");

#define GSS_CALLING_ERROR_STR(x)                                        \
    GSS_ERROR_STR((x), calling_error_string, GSS_CALLING_ERROR,         \
                  GSS_S_CALL_INACCESSIBLE_READ, GSS_S_CALL_BAD_STRUCTURE, \
                  GSS_CALLING_ERROR_FIELD)

/**/

static const char * const routine_error_string[] = {
    NULL,
    N_("An unsupported mechanism was requested"),
    N_("An invalid name was supplied"),
    N_("A supplied name was of an unsupported type"),
    N_("Incorrect channel bindings were supplied"),
    N_("An invalid status code was supplied"),
    N_("A token had an invalid signature"),
    N_("No credentials were supplied"),
    N_("No context has been established"),
    N_("A token was invalid"),
    N_("A credential was invalid"),
    N_("The referenced credentials have expired"),
    N_("The context has expired"),
    N_("Miscellaneous failure"),
    N_("The quality-of-protection requested could not be provided"),
    N_("The operation is forbidden by the local security policy"),
    N_("The operation or option is not available"),
};

static const char * const routine_error = N_("routine error");

#define GSS_ROUTINE_ERROR_STR(x)                                \
    GSS_ERROR_STR((x), routine_error_string, GSS_ROUTINE_ERROR, \
                  GSS_S_BAD_MECH, GSS_S_FAILURE,                \
                  GSS_ROUTINE_ERROR_FIELD)

/**/

/* this becomes overly gross after about 4 strings */

static const char * const sinfo_string[] = {
    N_("The routine must be called again to complete its function"),
    N_("The token was a duplicate of an earlier token"),
    N_("The token's validity period has expired"),
    N_("A later token has already been processed"),
};

static const char * const sinfo_code = N_("supplementary info code");

#define LSBGET(x) ((((x)^((x)-1))+1)>>1)
#define LSBMASK(n) ((1<<(n))^((1<<(n))-1))

#define GSS_SINFO_STR(x)                                                \
    ((((1<<(x)) < GSS_S_CONTINUE_NEEDED) || ((1<<(x)) > GSS_S_UNSEQ_TOKEN)) ? \
     /**/NULL:sinfo_string[(x)])

/**/

static const char * const no_error = N_("No error");
static const char * const unknown_error = N_("Unknown %s (field = %d)");

/**/

static int
display_unknown(const char *kind, OM_uint32 value, gss_buffer_t buffer)
{
    char *str;

    if (asprintf(&str, _(unknown_error), kind, value) < 0)
        return(0);

    buffer->length = strlen(str);
    buffer->value = str;

    return(1);
}

/* code should be set to the calling error field */

static OM_uint32
display_calling(OM_uint32 *minor_status, OM_uint32 code,
                gss_buffer_t status_string)
{
    const char *str;

    if ((str = GSS_CALLING_ERROR_STR(code))) {
        if (! g_make_string_buffer(str, status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    } else {
        if (! display_unknown(_(calling_error), GSS_CALLING_ERROR_FIELD(code),
                              status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    }
    *minor_status = 0;
    return(GSS_S_COMPLETE);
}

/* code should be set to the routine error field */

static OM_uint32
display_routine(OM_uint32 *minor_status, OM_uint32 code,
                gss_buffer_t status_string)
{
    const char *str;

    if ((str = GSS_ROUTINE_ERROR_STR(code))) {
        if (! g_make_string_buffer(str, status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    } else {
        if (! display_unknown(_(routine_error), GSS_ROUTINE_ERROR_FIELD(code),
                              status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    }
    *minor_status = 0;
    return(GSS_S_COMPLETE);
}

/* code should be set to the bit offset (log_2) of a supplementary info bit */

static OM_uint32
display_bit(OM_uint32 *minor_status, OM_uint32 code,
            gss_buffer_t status_string)
{
    const char *str;

    if ((str = GSS_SINFO_STR(code))) {
        if (! g_make_string_buffer(str, status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    } else {
        if (! display_unknown(_(sinfo_code), 1<<code, status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
    }
    *minor_status = 0;
    return(GSS_S_COMPLETE);
}

/**/

/* return error messages, for routine errors, call error, and status,
   in that order.
   message_context == 0 : print the routine error
   message_context == 1 : print the calling error
   message_context > 2  : print supplementary info bit (message_context-2)
*/

OM_uint32
g_display_major_status(OM_uint32 *minor_status, OM_uint32 status_value,
                       OM_uint32 *message_context, gss_buffer_t status_string)
{
    OM_uint32 ret, tmp;
    int bit;

    /*** deal with no error at all specially */

    if (status_value == 0) {
        if (! g_make_string_buffer(no_error, status_string)) {
            *minor_status = ENOMEM;
            return(GSS_S_FAILURE);
        }
        *message_context = 0;
        *minor_status = 0;
        return(GSS_S_COMPLETE);
    }

    /*** do routine error */

    if (*message_context == 0) {
        if ((tmp = GSS_ROUTINE_ERROR(status_value))) {
            status_value -= tmp;
            if ((ret = display_routine(minor_status, tmp, status_string)))
                return(ret);
            *minor_status = 0;
            if (status_value) {
                (*message_context)++;
                return(GSS_S_COMPLETE);
            } else {
                *message_context = 0;
                return(GSS_S_COMPLETE);
            }
        } else {
            (*message_context)++;
        }
    } else {
        status_value -= GSS_ROUTINE_ERROR(status_value);
    }

    /*** do calling error */

    if (*message_context == 1) {
        if ((tmp = GSS_CALLING_ERROR(status_value))) {
            status_value -= tmp;
            if ((ret = display_calling(minor_status, tmp, status_string)))
                return(ret);
            *minor_status = 0;
            if (status_value) {
                (*message_context)++;
                return(GSS_S_COMPLETE);
            } else {
                *message_context = 0;
                return(GSS_S_COMPLETE);
            }
        } else {
            (*message_context)++;
        }
    } else {
        status_value -= GSS_CALLING_ERROR(status_value);
    }

    /*** do sinfo bits (*message_context == 2 + number of bits done) */

    tmp = GSS_SUPPLEMENTARY_INFO_FIELD(status_value);
    /* mask off the bits which have been done */
    if (*message_context > 2) {
        tmp &= ~LSBMASK(*message_context-3);
        status_value &= ~LSBMASK(*message_context-3);
    }

    if (!tmp) {
        /* bogon input - there should be something left */
        *minor_status = (OM_uint32) G_BAD_MSG_CTX;
        return(GSS_S_FAILURE);
    }

    /* compute the bit offset */
    /*SUPPRESS 570*/
    for (bit=0; (((OM_uint32) 1)<<bit) != LSBGET(tmp); bit++) ;

    /* print it */
    if ((ret = display_bit(minor_status, bit, status_string)))
        return(ret);

    /* compute the new status_value/message_context */
    status_value -= ((OM_uint32) 1)<<bit;

    if (status_value) {
        *message_context = bit+3;
        return(GSS_S_COMPLETE);
    } else {
        *message_context = 0;
        return(GSS_S_COMPLETE);
    }
}