summaryrefslogtreecommitdiffstats
path: root/proxy/src/gp_rpc_acquire_cred.c
blob: 7389bf1e9ee1bc4f7556430b704e0569cd1a2586 (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
/*
   GSS-PROXY

   Copyright (C) 2011 Red Hat, Inc.
   Copyright (C) 2011 Simo Sorce <simo.sorce@redhat.com>

   Permission is hereby granted, free of charge, to any person obtaining a
   copy of this software and associated documentation files (the "Software"),
   to deal in the Software without restriction, including without limitation
   the rights to use, copy, modify, merge, publish, distribute, sublicense,
   and/or sell copies of the Software, and to permit persons to whom the
   Software is furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   DEALINGS IN THE SOFTWARE.
*/

#include "gp_rpc_process.h"

int gp_acquire_cred(struct gssproxy_ctx *gpctx,
                    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_name_t desired_name = GSS_C_NO_NAME;
    gss_OID_set desired_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;

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

    if (aca->input_cred_handle) {
        ret = gp_find_cred(aca->input_cred_handle, &in_cred);
        if (ret) {
            goto done;
        }
    }

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

    if (aca->desired_name) {
        ret = gp_conv_gssx_to_name(aca->desired_name, &desired_name);
        if (ret) {
            goto done;
        }
    }

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

    cred_usage = gp_conv_gssx_to_cred_usage(aca->cred_usage);

    if (in_cred == GSS_C_NO_CREDENTIAL) {
        ret_maj = gss_acquire_cred(&ret_min,
                                   desired_name,
                                   aca->time_req,
                                   desired_mechs,
                                   cred_usage,
                                   &out_cred,
                                   NULL,
                                   NULL);
    } else {
        if (desired_mechs != GSS_C_NO_OID_SET) {
            switch (desired_mechs->count) {
            case 0:
                desired_mech = GSS_C_NO_OID;
                break;
            case 1:
                desired_mech = &desired_mechs->elements[0];
                break;
            default:
                ret = EINVAL;
                goto done;
            }
        }
        ret_maj = gss_add_cred(&ret_min,
                               in_cred,
                               desired_name,
                               desired_mech,
                               cred_usage,
                               aca->initiator_time_req,
                               aca->acceptor_time_req,
                               add_out_cred,
                               NULL,
                               NULL,
                               NULL);
    }

    ret = gp_conv_status_to_gssx(&aca->call_ctx,
                                 ret_maj, ret_min, GSS_C_NO_OID,
                                 &acr->status);
    if (ret) {
        goto done;
    }

    if (ret_maj) {
        ret = 0;
        goto done;
    }

    if (!out_cred) {
        if (in_cred) {
            out_cred = in_cred;
        } else {
            ret = EINVAL;
            goto done;
        }
    }
    acr->output_cred_handle = calloc(1, sizeof(gssx_cred));
    if (!acr->output_cred_handle) {
        ret = ENOMEM;
        goto done;
    }
    ret = gp_export_gssx_cred(&out_cred, acr->output_cred_handle);
    if (ret) {
        goto done;
    }

done:
    gss_release_cred(&ret_min, &out_cred);
    return ret;
}