summaryrefslogtreecommitdiffstats
path: root/proxy/src/client/gpm_acquire_cred.c
blob: c0b16cb3d403e64cea426d07545d170284cdb8f5 (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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/* Copyright (C) 2011 the GSS-PROXY contributors, see COPYING for license */

#include "gssapi_gpm.h"

static int gpmint_cred_to_actual_mechs(gssx_cred *c, gss_OID_set *a)
{
    gssx_cred_element *e;
    gss_OID_set m = GSS_C_NO_OID_SET;
    int i;


    if (c->elements.elements_len) {

        m = malloc(sizeof(gss_OID_set_desc));
        if (!m) {
            return ENOMEM;
        }
        m->elements = calloc(c->elements.elements_len,
                             sizeof(gss_OID_desc));
        if (!m->elements) {
            free(m);
            return ENOMEM;
        }

        for (i = 0; i < c->elements.elements_len; i++) {
            e = &c->elements.elements_val[i];

            m->elements[i].elements = gp_memdup(e->mech.octet_string_val,
                                                e->mech.octet_string_len);
            if (!m->elements[i].elements) {
                while (i > 0) {
                    i--;
                    free(m->elements[i].elements);
                }
                free(m->elements);
                free(m);
                return ENOMEM;
            }
            m->elements[i].length = e->mech.octet_string_len;
        }
    }

    *a = m;
    return 0;
}

OM_uint32 gpm_acquire_cred(OM_uint32 *minor_status,
                           gssx_name *desired_name,
                           OM_uint32 time_req,
                           const gss_OID_set desired_mechs,
                           gss_cred_usage_t cred_usage,
                           gssx_cred **output_cred_handle,
                           gss_OID_set *actual_mechs,
                           OM_uint32 *time_rec)
{
    union gp_rpc_arg uarg;
    union gp_rpc_res ures;
    gssx_arg_acquire_cred *arg = &uarg.acquire_cred;
    gssx_res_acquire_cred *res = &ures.acquire_cred;
    uint32_t ret_min;
    uint32_t ret_maj;
    int ret = 0;

    memset(&uarg, 0, sizeof(union gp_rpc_arg));
    memset(&ures, 0, sizeof(union gp_rpc_res));

    if (output_cred_handle == NULL) {
        ret_maj = GSS_S_FAILURE;
        ret_min = EINVAL;
        goto done;
    }

    /* ignore call_ctx for now */

    arg->desired_name = desired_name;

    if (desired_mechs) {
        ret = gp_conv_oid_set_to_gssx(desired_mechs, &arg->desired_mechs);
        if (ret) {
            ret_maj = GSS_S_FAILURE;
            ret_min = ret;
            goto done;
        }
    }
    arg->time_req = time_req;
    arg->cred_usage = gp_conv_cred_usage_to_gssx(cred_usage);

    /* execute proxy request */
    ret = gpm_make_call(GSSX_ACQUIRE_CRED, &uarg, &ures);
    if (ret) {
        ret_maj = GSS_S_FAILURE;
        ret_min = ret;
        goto done;
    }

    if (res->status.major_status) {
        gpm_save_status(&res->status);
        ret_min = res->status.minor_status;
        ret_maj = res->status.major_status;
        goto done;
    }

    if (actual_mechs) {
        ret = gpmint_cred_to_actual_mechs(res->output_cred_handle,
                                          actual_mechs);
        if (ret) {
            ret_maj = GSS_S_FAILURE;
            ret_min = ret;
            goto done;
        }
    }

    if (time_rec) {
        gssx_cred_element *e;
        uint32_t t = 0;

        if (res->output_cred_handle->elements.elements_len) {
            e = &res->output_cred_handle->elements.elements_val[0];
            if (e->initiator_time_rec < e->acceptor_time_rec) {
                t = e->initiator_time_rec;
            } else {
                t = e->acceptor_time_rec;
            }
        }

        *time_rec = t;
    }

    /* we steal the cred handler here */
    *output_cred_handle = res->output_cred_handle;
    res->output_cred_handle = NULL;
    ret_maj = GSS_S_COMPLETE;
    ret_min = 0;

done:
    /* desired_name is passed in, don't let gpm_free_xdrs free it */
    arg->desired_name = NULL;
    gpm_free_xdrs(GSSX_ACQUIRE_CRED, &uarg, &ures);
    *minor_status = ret_min;
    return ret_maj;
}

OM_uint32 gpm_add_cred(OM_uint32 *minor_status,
                       gssx_cred *input_cred_handle,
                       gssx_name *desired_name,
                       const gss_OID desired_mech,
                       gss_cred_usage_t cred_usage,
                       OM_uint32 initiator_time_req,
                       OM_uint32 acceptor_time_req,
                       gssx_cred **output_cred_handle,
                       gss_OID_set *actual_mechs,
                       OM_uint32 *initiator_time_rec,
                       OM_uint32 *acceptor_time_rec)
{
    union gp_rpc_arg uarg;
    union gp_rpc_res ures;
    gssx_arg_acquire_cred *arg = &uarg.acquire_cred;
    gssx_res_acquire_cred *res = &ures.acquire_cred;
    gss_OID_set_desc mechs;
    uint32_t ret_min;
    uint32_t ret_maj;
    int ret = 0;

    memset(&uarg, 0, sizeof(union gp_rpc_arg));
    memset(&ures, 0, sizeof(union gp_rpc_res));

    /* ignore call_ctx for now */

    if (input_cred_handle) {
        arg->input_cred_handle = input_cred_handle;
    }
    if (output_cred_handle != NULL) {
        arg->add_cred_to_input_handle = true;
    }

    arg->desired_name = desired_name;

    if (desired_mech != GSS_C_NO_OID) {
        mechs.count = 1;
        mechs.elements = desired_mech;
        ret = gp_conv_oid_set_to_gssx(&mechs, &arg->desired_mechs);
        if (ret) {
            ret_maj = GSS_S_FAILURE;
            ret_min = ret;
            goto done;
        }
    }
    arg->cred_usage = gp_conv_cred_usage_to_gssx(cred_usage);
    arg->initiator_time_req = initiator_time_req;
    arg->acceptor_time_req = acceptor_time_req;

    /* execute proxy request */
    ret = gpm_make_call(GSSX_ACQUIRE_CRED, &uarg, &ures);
    if (ret) {
        ret_maj = GSS_S_FAILURE;
        ret_min = ret;
        goto done;
    }

    if (res->status.major_status) {
        gpm_save_status(&res->status);
        ret_min = res->status.minor_status;
        ret_maj = res->status.major_status;
        goto done;
    }

    if (actual_mechs) {
        ret = gpmint_cred_to_actual_mechs(res->output_cred_handle,
                                          actual_mechs);
        if (ret) {
            ret_maj = GSS_S_FAILURE;
            ret_min = ret;
            goto done;
        }
    }

    if (res->output_cred_handle->elements.elements_len) {
        gssx_cred_element *e;
        e = &res->output_cred_handle->elements.elements_val[0];
        if (initiator_time_rec) {
            *initiator_time_rec = e->initiator_time_rec;
        }
        if (acceptor_time_rec) {
            *acceptor_time_rec = e->initiator_time_rec;
        }
    } else {
        if (initiator_time_rec) {
            *initiator_time_rec = 0;
        }
        if (acceptor_time_rec) {
            *acceptor_time_rec = 0;
        }
    }

    if (output_cred_handle) {
        /* we steal the cred handler here */
        *output_cred_handle = res->output_cred_handle;
        res->output_cred_handle = NULL;
    }

    ret_maj = GSS_S_COMPLETE;
    ret_min = 0;

done:
    gpm_free_xdrs(GSSX_ACQUIRE_CRED, &uarg, &ures);
    *minor_status = ret_min;
    return ret_maj;
}

OM_uint32 gpm_inquire_cred(OM_uint32 *minor_status,
                           gssx_cred *cred,
                           gssx_name **name,
                           OM_uint32 *lifetime,
                           gss_cred_usage_t *cred_usage,
                           gss_OID_set *mechanisms)
{
    gss_OID_set mechs = GSS_C_NO_OID_SET;
    gssx_name *dname = NULL;
    gssx_cred_element *e;
    gss_OID_desc tmp_oid;
    uint32_t ret_min = 0;
    uint32_t ret_maj = GSS_S_COMPLETE;
    uint32_t life;
    int cu;
    int i;

    if (!cred) {
        *minor_status = 0;
        return GSS_S_CALL_INACCESSIBLE_READ;
    }
    if (cred->elements.elements_len == 0) {
        *minor_status = 0;
        return GSS_S_FAILURE;
    }

    if (name) {
        ret_min = gp_copy_gssx_name_alloc(&cred->desired_name, &dname);
        if (ret_min != 0) {
            return GSS_S_FAILURE;
        }
    }

    if (mechanisms) {
        ret_maj = gss_create_empty_oid_set(&ret_min, &mechs);
        if (ret_maj) {
            goto done;
        }
    }

    life = GSS_C_INDEFINITE;
    cu = -1;

    for (i = 0; i < cred->elements.elements_len; i++) {

        e = &cred->elements.elements_val[i];

        switch (e->cred_usage) {
        case GSSX_C_INITIATE:
            if (e->initiator_time_rec != 0 &&
                e->initiator_time_rec < life) {
                life = e->initiator_time_rec;
            }
            switch (cu) {
            case GSS_C_BOTH:
                break;
            case GSS_C_ACCEPT:
                cu = GSS_C_BOTH;
                break;
            default:
                cu = GSS_C_INITIATE;
            }
            break;
        case GSSX_C_ACCEPT:
            if (e->acceptor_time_rec != 0 &&
                e->acceptor_time_rec < life) {
                life = e->acceptor_time_rec;
            }
            switch (cu) {
            case GSS_C_BOTH:
                break;
            case GSS_C_INITIATE:
                cu = GSS_C_BOTH;
                break;
            default:
                cu = GSS_C_ACCEPT;
            }
            break;
        case GSSX_C_BOTH:
            if (e->initiator_time_rec != 0 &&
                e->initiator_time_rec < life) {
                life = e->initiator_time_rec;
            }
            if (e->acceptor_time_rec != 0 &&
                e->acceptor_time_rec < life) {
                life = e->acceptor_time_rec;
            }
            cu = GSS_C_BOTH;
            break;
        }

        if (mechanisms) {
            gp_conv_gssx_to_oid(&e->mech, &tmp_oid);
            ret_maj = gss_add_oid_set_member(&ret_min, &tmp_oid, &mechs);
            if (ret_maj) {
                goto done;
            }
        }
    }

    if (lifetime) {
        *lifetime = life;
    }

    if (cred_usage) {
        *cred_usage = cu;
    }

done:
    *minor_status = ret_min;
    if (ret_maj == GSS_S_COMPLETE) {
        if (name) {
            *name = dname;
        }
        if (mechanisms) {
            *mechanisms = mechs;
        }
    } else {
        (void)gpm_release_name(&ret_min, &dname);
        (void)gss_release_oid_set(&ret_min, &mechs);
    }
    return ret_maj;
}

OM_uint32 gpm_inquire_cred_by_mech(OM_uint32 *minor_status,
                                   gssx_cred *cred,
                                   gss_OID mech_type,
                                   gssx_name **name,
                                   OM_uint32 *initiator_lifetime,
                                   OM_uint32 *acceptor_lifetime,
                                   gss_cred_usage_t *cred_usage)
{
    gssx_name *dname = NULL;
    gssx_cred_element *e;
    gss_OID_desc tmp_oid;
    uint32_t ret_min = 0;
    uint32_t ret_maj = GSS_S_COMPLETE;
    int i;

    if (!cred) {
        *minor_status = 0;
        return GSS_S_CALL_INACCESSIBLE_READ;
    }
    if (cred->elements.elements_len == 0) {
        *minor_status = 0;
        return GSS_S_FAILURE;
    }

    for (i = 0; i < cred->elements.elements_len; i++) {

        e = &cred->elements.elements_val[i];
        gp_conv_gssx_to_oid(&e->mech, &tmp_oid);
        if (!gss_oid_equal(&tmp_oid, mech_type)) {
            continue;
        }

        switch (e->cred_usage) {
        case GSSX_C_INITIATE:
            if (initiator_lifetime) {
                *initiator_lifetime = e->initiator_time_rec;
            }
            if (cred_usage) {
                *cred_usage = GSS_C_INITIATE;
            }
            break;
        case GSSX_C_ACCEPT:
            if (acceptor_lifetime) {
                *acceptor_lifetime = e->acceptor_time_rec;
            }
            if (cred_usage) {
                *cred_usage = GSS_C_ACCEPT;
            }
            break;
        case GSSX_C_BOTH:
            if (initiator_lifetime) {
                *initiator_lifetime = e->initiator_time_rec;
            }
            if (acceptor_lifetime) {
                *acceptor_lifetime = e->acceptor_time_rec;
            }
            if (cred_usage) {
                *cred_usage = GSS_C_BOTH;
            }
            break;
        }
        if (name) {
            ret_min = gp_copy_gssx_name_alloc(&e->MN, &dname);
            if (ret_min != 0) {
                ret_maj = GSS_S_FAILURE;
                goto done;
            }
            *name = dname;
        }
        goto done;
    }

    if (i >= cred->elements.elements_len) {
        ret_maj = GSS_S_FAILURE;
    }

done:
    *minor_status = ret_min;
    if (ret_maj != GSS_S_COMPLETE) {
        (void)gpm_release_name(&ret_min, &dname);
    }
    return ret_maj;
}