1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
6 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
8 * Author: Eric Mei <ericm@clusterfs.com>
12 * linux/net/sunrpc/gss_mech_switch.c
14 * Copyright (c) 2001 The Regents of the University of Michigan.
15 * All rights reserved.
17 * J. Bruce Fields <bfields@umich.edu>
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. Neither the name of the University nor the names of its
29 * contributors may be used to endorse or promote products derived
30 * from this software without specific prior written permission.
32 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
33 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
36 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
37 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
38 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
39 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47 # define EXPORT_SYMTAB
49 #define DEBUG_SUBSYSTEM S_SEC
51 #include <linux/init.h>
52 #include <linux/module.h>
53 #include <linux/slab.h>
54 #include <linux/mutex.h>
56 #include <liblustre.h>
60 #include <obd_class.h>
61 #include <obd_support.h>
62 #include <lustre/lustre_idl.h>
63 #include <lustre_net.h>
64 #include <lustre_import.h>
65 #include <lustre_sec.h>
68 #include "gss_internal.h"
71 static CFS_LIST_HEAD(registered_mechs);
72 static spinlock_t registered_mechs_lock = SPIN_LOCK_UNLOCKED;
74 int lgss_mech_register(struct gss_api_mech *gm)
76 spin_lock(®istered_mechs_lock);
77 list_add(&gm->gm_list, ®istered_mechs);
78 spin_unlock(®istered_mechs_lock);
79 CWARN("Register %s mechanism\n", gm->gm_name);
83 void lgss_mech_unregister(struct gss_api_mech *gm)
85 spin_lock(®istered_mechs_lock);
86 list_del(&gm->gm_list);
87 spin_unlock(®istered_mechs_lock);
88 CWARN("Unregister %s mechanism\n", gm->gm_name);
92 struct gss_api_mech *lgss_mech_get(struct gss_api_mech *gm)
94 __module_get(gm->gm_owner);
98 struct gss_api_mech *lgss_name_to_mech(char *name)
100 struct gss_api_mech *pos, *gm = NULL;
102 spin_lock(®istered_mechs_lock);
103 list_for_each_entry(pos, ®istered_mechs, gm_list) {
104 if (0 == strcmp(name, pos->gm_name)) {
105 if (!try_module_get(pos->gm_owner))
111 spin_unlock(®istered_mechs_lock);
117 int mech_supports_subflavor(struct gss_api_mech *gm, __u32 subflavor)
121 for (i = 0; i < gm->gm_sf_num; i++) {
122 if (gm->gm_sfs[i].sf_subflavor == subflavor)
128 struct gss_api_mech *lgss_subflavor_to_mech(__u32 subflavor)
130 struct gss_api_mech *pos, *gm = NULL;
132 spin_lock(®istered_mechs_lock);
133 list_for_each_entry(pos, ®istered_mechs, gm_list) {
134 if (!try_module_get(pos->gm_owner))
136 if (!mech_supports_subflavor(pos, subflavor)) {
137 module_put(pos->gm_owner);
143 spin_unlock(®istered_mechs_lock);
147 void lgss_mech_put(struct gss_api_mech *gm)
149 module_put(gm->gm_owner);
152 /* The mech could probably be determined from the token instead, but it's just
153 * as easy for now to pass it in. */
154 __u32 lgss_import_sec_context(rawobj_t *input_token,
155 struct gss_api_mech *mech,
156 struct gss_ctx **ctx_id)
158 OBD_ALLOC_PTR(*ctx_id);
160 return GSS_S_FAILURE;
162 (*ctx_id)->mech_type = lgss_mech_get(mech);
165 LASSERT(mech->gm_ops);
166 LASSERT(mech->gm_ops->gss_import_sec_context);
167 return mech->gm_ops->gss_import_sec_context(input_token, *ctx_id);
170 __u32 lgss_copy_reverse_context(struct gss_ctx *ctx_id,
171 struct gss_ctx **ctx_id_new)
173 struct gss_api_mech *mech = ctx_id->mech_type;
178 OBD_ALLOC_PTR(*ctx_id_new);
179 if (*ctx_id_new == NULL)
180 return GSS_S_FAILURE;
182 (*ctx_id_new)->mech_type = lgss_mech_get(mech);
185 LASSERT(mech->gm_ops);
186 LASSERT(mech->gm_ops->gss_copy_reverse_context);
188 major = mech->gm_ops->gss_copy_reverse_context(ctx_id, *ctx_id_new);
189 if (major != GSS_S_COMPLETE) {
191 OBD_FREE_PTR(*ctx_id_new);
198 * this interface is much simplified, currently we only need endtime.
200 __u32 lgss_inquire_context(struct gss_ctx *context_handle,
201 unsigned long *endtime)
203 LASSERT(context_handle);
204 LASSERT(context_handle->mech_type);
205 LASSERT(context_handle->mech_type->gm_ops);
206 LASSERT(context_handle->mech_type->gm_ops->gss_inquire_context);
208 return context_handle->mech_type->gm_ops
209 ->gss_inquire_context(context_handle,
213 /* gss_get_mic: compute a mic over message and return mic_token. */
214 __u32 lgss_get_mic(struct gss_ctx *context_handle,
219 LASSERT(context_handle);
220 LASSERT(context_handle->mech_type);
221 LASSERT(context_handle->mech_type->gm_ops);
222 LASSERT(context_handle->mech_type->gm_ops->gss_get_mic);
224 return context_handle->mech_type->gm_ops
225 ->gss_get_mic(context_handle,
231 /* gss_verify_mic: check whether the provided mic_token verifies message. */
232 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
237 LASSERT(context_handle);
238 LASSERT(context_handle->mech_type);
239 LASSERT(context_handle->mech_type->gm_ops);
240 LASSERT(context_handle->mech_type->gm_ops->gss_verify_mic);
242 return context_handle->mech_type->gm_ops
243 ->gss_verify_mic(context_handle,
249 __u32 lgss_wrap(struct gss_ctx *context_handle,
255 LASSERT(context_handle);
256 LASSERT(context_handle->mech_type);
257 LASSERT(context_handle->mech_type->gm_ops);
258 LASSERT(context_handle->mech_type->gm_ops->gss_wrap);
260 return context_handle->mech_type->gm_ops
261 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
264 __u32 lgss_unwrap(struct gss_ctx *context_handle,
269 LASSERT(context_handle);
270 LASSERT(context_handle->mech_type);
271 LASSERT(context_handle->mech_type->gm_ops);
272 LASSERT(context_handle->mech_type->gm_ops->gss_unwrap);
274 return context_handle->mech_type->gm_ops
275 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
279 __u32 lgss_plain_encrypt(struct gss_ctx *ctx,
286 LASSERT(ctx->mech_type);
287 LASSERT(ctx->mech_type->gm_ops);
288 LASSERT(ctx->mech_type->gm_ops->gss_plain_encrypt);
290 return ctx->mech_type->gm_ops
291 ->gss_plain_encrypt(ctx, decrypt, length, in_buf, out_buf);
294 /* gss_delete_sec_context: free all resources associated with context_handle.
295 * Note this differs from the RFC 2744-specified prototype in that we don't
296 * bother returning an output token, since it would never be used anyway. */
298 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
300 struct gss_api_mech *mech;
302 CDEBUG(D_SEC, "deleting %p\n", *context_handle);
304 if (!*context_handle)
305 return(GSS_S_NO_CONTEXT);
307 mech = (*context_handle)->mech_type;
308 if ((*context_handle)->internal_ctx_id != 0) {
310 LASSERT(mech->gm_ops);
311 LASSERT(mech->gm_ops->gss_delete_sec_context);
312 mech->gm_ops->gss_delete_sec_context(
313 (*context_handle)->internal_ctx_id);
318 OBD_FREE_PTR(*context_handle);
319 *context_handle=NULL;
320 return GSS_S_COMPLETE;
323 int lgss_display(struct gss_ctx *ctx,
328 LASSERT(ctx->mech_type);
329 LASSERT(ctx->mech_type->gm_ops);
330 LASSERT(ctx->mech_type->gm_ops->gss_display);
332 return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);