aboutsummaryrefslogtreecommitdiff
path: root/src/lib/gssapi/mechglue/g_set_cred_option.c
blob: 339a0a088b6b1bd82c8995f1bd6fc99f6de3627c (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
/*
 * Copyright 2008-2010 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.
 *
 */

/*
 *  glue routine for gssspi_set_cred_option
 */

#include "mglueP.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>
#include <time.h>

static OM_uint32
alloc_union_cred(OM_uint32 *minor_status,
		 gss_mechanism mech,
		 gss_cred_id_t mech_cred,
		 gss_union_cred_t *pcred)
{
    OM_uint32		status;
    OM_uint32		temp_minor_status;
    gss_union_cred_t	cred = NULL;
    gss_name_t		mech_name = GSS_C_NO_NAME;

    *pcred = NULL;

    if (mech->gss_inquire_cred == NULL) {
	status = GSS_S_BAD_MECH;
	goto cleanup;
    }

    status = GSS_S_FAILURE;

    cred = calloc(1, sizeof(*cred));
    if (cred == NULL) {
	*minor_status = ENOMEM;
	goto cleanup;
    }

    cred->loopback = cred;
    cred->count = 1;

    cred->cred_array = calloc(cred->count, sizeof(gss_cred_id_t));
    if (cred->cred_array == NULL) {
	*minor_status = ENOMEM;
	goto cleanup;
    }
    cred->cred_array[0] = mech_cred;

    status = generic_gss_copy_oid(minor_status,
                                  &mech->mech_type,
                                  &cred->mechs_array);
    if (status != GSS_S_COMPLETE)
        goto cleanup;

    cred->auxinfo.creation_time = (OM_uint32)time(NULL);

    status = mech->gss_inquire_cred(minor_status,
				    mech_cred,
				    &mech_name,
				    &cred->auxinfo.time_rec,
				    &cred->auxinfo.cred_usage,
				    NULL);
    if (status != GSS_S_COMPLETE)
	goto cleanup;

    status = mech->gss_display_name(minor_status,
				    mech_name,
				    &cred->auxinfo.name,
				    &cred->auxinfo.name_type);
    if (status != GSS_S_COMPLETE)
	goto cleanup;

    status = GSS_S_COMPLETE;
    *pcred = cred;

cleanup:
    if (status != GSS_S_COMPLETE)
	gss_release_cred(&temp_minor_status, (gss_cred_id_t *)&cred);
    mech->gss_release_name(&temp_minor_status, &mech_name);

    return status;
}

OM_uint32 KRB5_CALLCONV
gssspi_set_cred_option(OM_uint32 *minor_status,
	               gss_cred_id_t *cred_handle,
	               const gss_OID desired_object,
	               const gss_buffer_t value)
{
    gss_union_cred_t	union_cred;
    gss_mechanism	mech;
    int			i;
    OM_uint32		status;
    OM_uint32		mech_status;
    OM_uint32		mech_minor_status;

    if (minor_status == NULL)
	return GSS_S_CALL_INACCESSIBLE_WRITE;

    if (cred_handle == NULL)
	return GSS_S_CALL_INACCESSIBLE_WRITE;

    *minor_status = 0;

    status = GSS_S_UNAVAILABLE;

    if (*cred_handle == GSS_C_NO_CREDENTIAL) {
	gss_cred_id_t mech_cred = GSS_C_NO_CREDENTIAL;

	/*
	 * We need to give a mechanism the opportunity to allocate a
	 * credentials handle. Unfortunately this does mean that only
	 * the default mechanism can allocate a credentials handle.
	 */
        mech = gssint_get_mechanism(NULL);
        if (mech == NULL)
            return GSS_S_BAD_MECH;

	if (mech->gssspi_set_cred_option == NULL)
	    return GSS_S_UNAVAILABLE;

	status = mech->gssspi_set_cred_option(minor_status,
					      &mech_cred,
					      desired_object,
					      value);
	if (status != GSS_S_COMPLETE)
	    return status;

	if (mech_cred != GSS_C_NO_CREDENTIAL) {
	    status = alloc_union_cred(minor_status,
				      mech,
				      mech_cred,
				      &union_cred);
	    if (status != GSS_S_COMPLETE)
		return status;
	    *cred_handle = (gss_cred_id_t)union_cred;
	}
    } else {
	union_cred = (gss_union_cred_t) *cred_handle;

	for (i = 0; i < union_cred->count; i++) {
	    mech = gssint_get_mechanism(&union_cred->mechs_array[i]);
	    if (mech == NULL) {
		status = GSS_S_BAD_MECH;
		break;
	    }

	    if (mech->gssspi_set_cred_option == NULL)
		continue;

	    mech_status = mech->gssspi_set_cred_option(&mech_minor_status,
						       &union_cred->cred_array[i],
						       desired_object,
						       value);
	    if (mech_status == GSS_S_UNAVAILABLE)
		continue;
	    else {
		status = mech_status;
		*minor_status = mech_minor_status;
	    }
	    if (status != GSS_S_COMPLETE) {
		map_error(minor_status, mech);
		break;
	    }
	}
    }

    return status;
}