Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_mech_switch.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Modifications for Lustre
5  *
6  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
7  *
8  * Author: Eric Mei <ericm@clusterfs.com>
9  */
10
11 /*
12  *  linux/net/sunrpc/gss_mech_switch.c
13  *
14  *  Copyright (c) 2001 The Regents of the University of Michigan.
15  *  All rights reserved.
16  *
17  *  J. Bruce Fields   <bfields@umich.edu>
18  *
19  *  Redistribution and use in source and binary forms, with or without
20  *  modification, are permitted provided that the following conditions
21  *  are met:
22  *
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.
31  *
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.
43  *
44  */
45
46 #ifndef EXPORT_SYMTAB
47 # define EXPORT_SYMTAB
48 #endif
49 #define DEBUG_SUBSYSTEM S_SEC
50 #ifdef __KERNEL__
51 #include <linux/init.h>
52 #include <linux/module.h>
53 #include <linux/slab.h>
54 #include <linux/mutex.h>
55 #else
56 #include <liblustre.h>
57 #endif
58
59 #include <obd.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>
66
67 #include "gss_err.h"
68 #include "gss_internal.h"
69 #include "gss_api.h"
70
71 static CFS_LIST_HEAD(registered_mechs);
72 static cfs_spinlock_t registered_mechs_lock = CFS_SPIN_LOCK_UNLOCKED;
73
74 int lgss_mech_register(struct gss_api_mech *gm)
75 {
76         cfs_spin_lock(&registered_mechs_lock);
77         cfs_list_add(&gm->gm_list, &registered_mechs);
78         cfs_spin_unlock(&registered_mechs_lock);
79         CWARN("Register %s mechanism\n", gm->gm_name);
80         return 0;
81 }
82
83 void lgss_mech_unregister(struct gss_api_mech *gm)
84 {
85         cfs_spin_lock(&registered_mechs_lock);
86         cfs_list_del(&gm->gm_list);
87         cfs_spin_unlock(&registered_mechs_lock);
88         CWARN("Unregister %s mechanism\n", gm->gm_name);
89 }
90
91
92 struct gss_api_mech *lgss_mech_get(struct gss_api_mech *gm)
93 {
94         __cfs_module_get(gm->gm_owner);
95         return gm;
96 }
97
98 struct gss_api_mech *lgss_name_to_mech(char *name)
99 {
100         struct gss_api_mech *pos, *gm = NULL;
101
102         cfs_spin_lock(&registered_mechs_lock);
103         cfs_list_for_each_entry(pos, &registered_mechs, gm_list) {
104                 if (0 == strcmp(name, pos->gm_name)) {
105                         if (!cfs_try_module_get(pos->gm_owner))
106                                 continue;
107                         gm = pos;
108                         break;
109                 }
110         }
111         cfs_spin_unlock(&registered_mechs_lock);
112         return gm;
113
114 }
115
116 static inline
117 int mech_supports_subflavor(struct gss_api_mech *gm, __u32 subflavor)
118 {
119         int i;
120
121         for (i = 0; i < gm->gm_sf_num; i++) {
122                 if (gm->gm_sfs[i].sf_subflavor == subflavor)
123                         return 1;
124         }
125         return 0;
126 }
127
128 struct gss_api_mech *lgss_subflavor_to_mech(__u32 subflavor)
129 {
130         struct gss_api_mech *pos, *gm = NULL;
131
132         cfs_spin_lock(&registered_mechs_lock);
133         cfs_list_for_each_entry(pos, &registered_mechs, gm_list) {
134                 if (!cfs_try_module_get(pos->gm_owner))
135                         continue;
136                 if (!mech_supports_subflavor(pos, subflavor)) {
137                         cfs_module_put(pos->gm_owner);
138                         continue;
139                 }
140                 gm = pos;
141                 break;
142         }
143         cfs_spin_unlock(&registered_mechs_lock);
144         return gm;
145 }
146
147 void lgss_mech_put(struct gss_api_mech *gm)
148 {
149         cfs_module_put(gm->gm_owner);
150 }
151
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)
157 {
158         OBD_ALLOC_PTR(*ctx_id);
159         if (*ctx_id == NULL)
160                 return GSS_S_FAILURE;
161
162         (*ctx_id)->mech_type = lgss_mech_get(mech);
163
164         LASSERT(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);
168 }
169
170 __u32 lgss_copy_reverse_context(struct gss_ctx *ctx_id,
171                                 struct gss_ctx **ctx_id_new)
172 {
173         struct gss_api_mech *mech = ctx_id->mech_type;
174         __u32                major;
175
176         LASSERT(mech);
177
178         OBD_ALLOC_PTR(*ctx_id_new);
179         if (*ctx_id_new == NULL)
180                 return GSS_S_FAILURE;
181
182         (*ctx_id_new)->mech_type = lgss_mech_get(mech);
183
184         LASSERT(mech);
185         LASSERT(mech->gm_ops);
186         LASSERT(mech->gm_ops->gss_copy_reverse_context);
187
188         major = mech->gm_ops->gss_copy_reverse_context(ctx_id, *ctx_id_new);
189         if (major != GSS_S_COMPLETE) {
190                 lgss_mech_put(mech);
191                 OBD_FREE_PTR(*ctx_id_new);
192                 *ctx_id_new = NULL;
193         }
194         return major;
195 }
196
197 /*
198  * this interface is much simplified, currently we only need endtime.
199  */
200 __u32 lgss_inquire_context(struct gss_ctx *context_handle,
201                            unsigned long  *endtime)
202 {
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);
207
208         return context_handle->mech_type->gm_ops
209                 ->gss_inquire_context(context_handle,
210                                       endtime);
211 }
212
213 /* gss_get_mic: compute a mic over message and return mic_token. */
214 __u32 lgss_get_mic(struct gss_ctx *context_handle,
215                    int msgcnt,
216                    rawobj_t *msg,
217                    int iovcnt,
218                    lnet_kiov_t *iovs,
219                    rawobj_t *mic_token)
220 {
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);
225
226         return context_handle->mech_type->gm_ops
227                 ->gss_get_mic(context_handle,
228                               msgcnt,
229                               msg,
230                               iovcnt,
231                               iovs,
232                               mic_token);
233 }
234
235 /* gss_verify_mic: check whether the provided mic_token verifies message. */
236 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
237                       int msgcnt,
238                       rawobj_t *msg,
239                       int iovcnt,
240                       lnet_kiov_t *iovs,
241                       rawobj_t *mic_token)
242 {
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);
247
248         return context_handle->mech_type->gm_ops
249                 ->gss_verify_mic(context_handle,
250                                  msgcnt,
251                                  msg,
252                                  iovcnt,
253                                  iovs,
254                                  mic_token);
255 }
256
257 __u32 lgss_wrap(struct gss_ctx *context_handle,
258                 rawobj_t *gsshdr,
259                 rawobj_t *msg,
260                 int msg_buflen,
261                 rawobj_t *out_token)
262 {
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);
267
268         return context_handle->mech_type->gm_ops
269                 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
270 }
271
272 __u32 lgss_unwrap(struct gss_ctx *context_handle,
273                   rawobj_t *gsshdr,
274                   rawobj_t *token,
275                   rawobj_t *out_msg)
276 {
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);
281
282         return context_handle->mech_type->gm_ops
283                 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
284 }
285
286
287 __u32 lgss_prep_bulk(struct gss_ctx *context_handle,
288                      struct ptlrpc_bulk_desc *desc)
289 {
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);
294
295         return context_handle->mech_type->gm_ops
296                 ->gss_prep_bulk(context_handle, desc);
297 }
298
299 __u32 lgss_wrap_bulk(struct gss_ctx *context_handle,
300                      struct ptlrpc_bulk_desc *desc,
301                      rawobj_t *token,
302                      int adj_nob)
303 {
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);
308
309         return context_handle->mech_type->gm_ops
310                 ->gss_wrap_bulk(context_handle, desc, token, adj_nob);
311 }
312
313 __u32 lgss_unwrap_bulk(struct gss_ctx *context_handle,
314                        struct ptlrpc_bulk_desc *desc,
315                        rawobj_t *token,
316                        int adj_nob)
317 {
318         LASSERT(context_handle);
319         LASSERT(context_handle->mech_type);
320         LASSERT(context_handle->mech_type->gm_ops);
321         LASSERT(context_handle->mech_type->gm_ops->gss_unwrap_bulk);
322
323         return context_handle->mech_type->gm_ops
324                 ->gss_unwrap_bulk(context_handle, desc, token, adj_nob);
325 }
326
327 /* gss_delete_sec_context: free all resources associated with context_handle.
328  * Note this differs from the RFC 2744-specified prototype in that we don't
329  * bother returning an output token, since it would never be used anyway. */
330
331 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
332 {
333         struct gss_api_mech *mech;
334
335         CDEBUG(D_SEC, "deleting %p\n", *context_handle);
336
337         if (!*context_handle)
338                 return(GSS_S_NO_CONTEXT);
339
340         mech = (*context_handle)->mech_type;
341         if ((*context_handle)->internal_ctx_id != 0) {
342                 LASSERT(mech);
343                 LASSERT(mech->gm_ops);
344                 LASSERT(mech->gm_ops->gss_delete_sec_context);
345                 mech->gm_ops->gss_delete_sec_context(
346                                         (*context_handle)->internal_ctx_id);
347         }
348         if (mech)
349                 lgss_mech_put(mech);
350
351         OBD_FREE_PTR(*context_handle);
352         *context_handle=NULL;
353         return GSS_S_COMPLETE;
354 }
355
356 int lgss_display(struct gss_ctx *ctx,
357                  char           *buf,
358                  int             bufsize)
359 {
360         LASSERT(ctx);
361         LASSERT(ctx->mech_type);
362         LASSERT(ctx->mech_type->gm_ops);
363         LASSERT(ctx->mech_type->gm_ops->gss_display);
364
365         return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);
366 }