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,
221 LASSERT(context_handle);
222 LASSERT(context_handle->mech_type);
223 LASSERT(context_handle->mech_type->gm_ops);
224 LASSERT(context_handle->mech_type->gm_ops->gss_get_mic);
226 return context_handle->mech_type->gm_ops
227 ->gss_get_mic(context_handle,
235 /* gss_verify_mic: check whether the provided mic_token verifies message. */
236 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
243 LASSERT(context_handle);
244 LASSERT(context_handle->mech_type);
245 LASSERT(context_handle->mech_type->gm_ops);
246 LASSERT(context_handle->mech_type->gm_ops->gss_verify_mic);
248 return context_handle->mech_type->gm_ops
249 ->gss_verify_mic(context_handle,
257 __u32 lgss_wrap(struct gss_ctx *context_handle,
263 LASSERT(context_handle);
264 LASSERT(context_handle->mech_type);
265 LASSERT(context_handle->mech_type->gm_ops);
266 LASSERT(context_handle->mech_type->gm_ops->gss_wrap);
268 return context_handle->mech_type->gm_ops
269 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
272 __u32 lgss_unwrap(struct gss_ctx *context_handle,
277 LASSERT(context_handle);
278 LASSERT(context_handle->mech_type);
279 LASSERT(context_handle->mech_type->gm_ops);
280 LASSERT(context_handle->mech_type->gm_ops->gss_unwrap);
282 return context_handle->mech_type->gm_ops
283 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
287 __u32 lgss_prep_bulk(struct gss_ctx *context_handle,
288 struct ptlrpc_bulk_desc *desc)
290 LASSERT(context_handle);
291 LASSERT(context_handle->mech_type);
292 LASSERT(context_handle->mech_type->gm_ops);
293 LASSERT(context_handle->mech_type->gm_ops->gss_prep_bulk);
295 return context_handle->mech_type->gm_ops
296 ->gss_prep_bulk(context_handle, desc);
299 __u32 lgss_wrap_bulk(struct gss_ctx *context_handle,
300 struct ptlrpc_bulk_desc *desc,
304 LASSERT(context_handle);
305 LASSERT(context_handle->mech_type);
306 LASSERT(context_handle->mech_type->gm_ops);
307 LASSERT(context_handle->mech_type->gm_ops->gss_wrap_bulk);
309 return context_handle->mech_type->gm_ops
310 ->gss_wrap_bulk(context_handle, desc, token, adj_nob);
313 __u32 lgss_unwrap_bulk(struct gss_ctx *context_handle,
314 struct ptlrpc_bulk_desc *desc,
317 LASSERT(context_handle);
318 LASSERT(context_handle->mech_type);
319 LASSERT(context_handle->mech_type->gm_ops);
320 LASSERT(context_handle->mech_type->gm_ops->gss_unwrap_bulk);
322 return context_handle->mech_type->gm_ops
323 ->gss_unwrap_bulk(context_handle, desc, token);
326 /* gss_delete_sec_context: free all resources associated with context_handle.
327 * Note this differs from the RFC 2744-specified prototype in that we don't
328 * bother returning an output token, since it would never be used anyway. */
330 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
332 struct gss_api_mech *mech;
334 CDEBUG(D_SEC, "deleting %p\n", *context_handle);
336 if (!*context_handle)
337 return(GSS_S_NO_CONTEXT);
339 mech = (*context_handle)->mech_type;
340 if ((*context_handle)->internal_ctx_id != 0) {
342 LASSERT(mech->gm_ops);
343 LASSERT(mech->gm_ops->gss_delete_sec_context);
344 mech->gm_ops->gss_delete_sec_context(
345 (*context_handle)->internal_ctx_id);
350 OBD_FREE_PTR(*context_handle);
351 *context_handle=NULL;
352 return GSS_S_COMPLETE;
355 int lgss_display(struct gss_ctx *ctx,
360 LASSERT(ctx->mech_type);
361 LASSERT(ctx->mech_type->gm_ops);
362 LASSERT(ctx->mech_type->gm_ops->gss_display);
364 return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);