summaryrefslogtreecommitdiffstats
path: root/proxy/src/gp_rpc_acquire_cred.c
blob: e9deabf0f816310c22fea307961a2a101419389a (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
/* Copyright (C) 2011 the GSS-PROXY contributors, see COPYING for license */

#include "gp_rpc_process.h"
#include <gssapi/gssapi_krb5.h>

int gp_acquire_cred(struct gp_call_ctx *gpcall,
                    union gp_rpc_arg *arg,
                    union gp_rpc_res *res)
{
    struct gssx_arg_acquire_cred *aca;
    struct gssx_res_acquire_cred *acr;
    uint32_t ret_maj;
    uint32_t ret_min;
    gss_cred_id_t in_cred = GSS_C_NO_CREDENTIAL;
    gss_OID_set desired_mechs = GSS_C_NO_OID_SET;
    gss_OID_set use_mechs = GSS_C_NO_OID_SET;
    gss_OID desired_mech = GSS_C_NO_OID;
    gss_cred_usage_t cred_usage;
    gss_cred_id_t out_cred = GSS_C_NO_CREDENTIAL;
    gss_cred_id_t *add_out_cred = NULL;
    int ret;
    int i;

    aca = &arg->acquire_cred;
    acr = &res->acquire_cred;

    GPRPCDEBUG(gssx_arg_acquire_cred, aca);

    if (aca->input_cred_handle) {
        ret_maj = gp_import_gssx_cred(&ret_min, gpcall,
                                      aca->input_cred_handle, &in_cred);
        if (ret_maj) {
            goto done;
        }
    }

    if (aca->add_cred_to_input_handle) {
        add_out_cred = &in_cred;
    } else {
        add_out_cred = &out_cred;
    }

    ret = gp_conv_gssx_to_oid_set(&aca->desired_mechs, &desired_mechs);
    if (ret) {
        ret_maj = GSS_S_FAILURE;
        ret_min = ret;
        goto done;
    }

    /* if a mech list is specified check if it includes the mechs
     * allowed by this service configuration */
    if (desired_mechs != GSS_C_NO_OID_SET) {
        ret_maj = gss_create_empty_oid_set(&ret_min, &use_mechs);
        if (ret_maj) {
            goto done;
        }

        for (i = 0; i < desired_mechs->count; i++) {
            desired_mech = &desired_mechs->elements[i];

            if (!gp_creds_allowed_mech(gpcall, desired_mech)) {
                continue;
            }

            ret_maj = gss_add_oid_set_member(&ret_min,
                                             desired_mech, &use_mechs);
            if (ret_maj) {
                goto done;
            }
        }

        if (use_mechs->count == 0) {
            /* no allowed mech, return nothing */
            desired_mech = GSS_C_NO_OID;
            ret_maj = GSS_S_NO_CRED;
            ret_min = 0;
            goto done;
        }
    } else {
        ret_maj = gp_get_supported_mechs(&ret_min, &use_mechs);
        if (ret_maj) {
            goto done;
        }
    }

    cred_usage = gp_conv_gssx_to_cred_usage(aca->cred_usage);

    for (i = 0; i < use_mechs->count; i++) {
        desired_mech = &use_mechs->elements[i];
        /* this should really be folded into an extended
         * gss_add_cred in gssapi that can accept a set of URIs
         * that define keytabs and ccaches and principals */
        if (gss_oid_equal(desired_mech, gss_mech_krb5)) {
            ret_maj = gp_add_krb5_creds(&ret_min,
                                        gpcall,
                                        in_cred,
                                        aca->desired_name,
                                        cred_usage,
                                        aca->initiator_time_req,
                                        aca->acceptor_time_req,
                                        add_out_cred,
                                        NULL,
                                        NULL,
                                        NULL);
            if (ret_maj) {
                goto done;
            }
        } else {
            /* we support only the krb5 mech for now */
            ret_maj = GSS_S_BAD_MECH;
            goto done;
        }
    }

    if (out_cred == GSS_C_NO_CREDENTIAL) {
        if (in_cred != GSS_C_NO_CREDENTIAL) {
            out_cred = in_cred;
        } else {
            ret_maj = GSS_S_NO_CRED;
            ret_min = 0;
            goto done;
        }
    }

    acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
    if (!acr->output_cred_handle) {
        ret_maj = GSS_S_FAILURE;
        ret_min = ENOMEM;
        goto done;
    }
    ret_maj = gp_export_gssx_cred(&ret_min, gpcall,
                                  &out_cred, acr->output_cred_handle);
    if (ret_maj) {
        goto done;
    }

done:
    ret = gp_conv_status_to_gssx(&aca->call_ctx,
                                 ret_maj, ret_min, desired_mech,
                                 &acr->status);

    GPRPCDEBUG(gssx_res_acquire_cred, acr);

    gss_release_cred(&ret_min, &out_cred);
    gss_release_oid_set(&ret_min, &use_mechs);
    gss_release_oid_set(&ret_min, &desired_mechs);
    return ret;
}