Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_mech_switch.c
1 /*
2  * Modifications for Lustre
3  *
4  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
5  *
6  * Author: Eric Mei <ericm@clusterfs.com>
7  */
8
9 /*
10  *  linux/net/sunrpc/gss_mech_switch.c
11  *
12  *  Copyright (c) 2001 The Regents of the University of Michigan.
13  *  All rights reserved.
14  *
15  *  J. Bruce Fields   <bfields@umich.edu>
16  *
17  *  Redistribution and use in source and binary forms, with or without
18  *  modification, are permitted provided that the following conditions
19  *  are met:
20  *
21  *  1. Redistributions of source code must retain the above copyright
22  *     notice, this list of conditions and the following disclaimer.
23  *  2. Redistributions in binary form must reproduce the above copyright
24  *     notice, this list of conditions and the following disclaimer in the
25  *     documentation and/or other materials provided with the distribution.
26  *  3. Neither the name of the University nor the names of its
27  *     contributors may be used to endorse or promote products derived
28  *     from this software without specific prior written permission.
29  *
30  *  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
31  *  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
32  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
33  *  DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
34  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
37  *  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
38  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
39  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  */
43
44 #define DEBUG_SUBSYSTEM S_SEC
45 #ifdef __KERNEL__
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/mutex.h>
50 #else
51 #include <liblustre.h>
52 #endif
53
54 #include <obd.h>
55 #include <obd_class.h>
56 #include <obd_support.h>
57 #include <lustre/lustre_idl.h>
58 #include <lustre_net.h>
59 #include <lustre_import.h>
60 #include <lustre_sec.h>
61
62 #include "gss_err.h"
63 #include "gss_internal.h"
64 #include "gss_api.h"
65
66 static CFS_LIST_HEAD(registered_mechs);
67 static DEFINE_SPINLOCK(registered_mechs_lock);
68
69 int lgss_mech_register(struct gss_api_mech *gm)
70 {
71         spin_lock(&registered_mechs_lock);
72         cfs_list_add(&gm->gm_list, &registered_mechs);
73         spin_unlock(&registered_mechs_lock);
74         CWARN("Register %s mechanism\n", gm->gm_name);
75         return 0;
76 }
77
78 void lgss_mech_unregister(struct gss_api_mech *gm)
79 {
80         spin_lock(&registered_mechs_lock);
81         cfs_list_del(&gm->gm_list);
82         spin_unlock(&registered_mechs_lock);
83         CWARN("Unregister %s mechanism\n", gm->gm_name);
84 }
85
86
87 struct gss_api_mech *lgss_mech_get(struct gss_api_mech *gm)
88 {
89         __cfs_module_get(gm->gm_owner);
90         return gm;
91 }
92
93 struct gss_api_mech *lgss_name_to_mech(char *name)
94 {
95         struct gss_api_mech *pos, *gm = NULL;
96
97         spin_lock(&registered_mechs_lock);
98         cfs_list_for_each_entry(pos, &registered_mechs, gm_list) {
99                 if (0 == strcmp(name, pos->gm_name)) {
100                         if (!cfs_try_module_get(pos->gm_owner))
101                                 continue;
102                         gm = pos;
103                         break;
104                 }
105         }
106         spin_unlock(&registered_mechs_lock);
107         return gm;
108
109 }
110
111 static inline
112 int mech_supports_subflavor(struct gss_api_mech *gm, __u32 subflavor)
113 {
114         int i;
115
116         for (i = 0; i < gm->gm_sf_num; i++) {
117                 if (gm->gm_sfs[i].sf_subflavor == subflavor)
118                         return 1;
119         }
120         return 0;
121 }
122
123 struct gss_api_mech *lgss_subflavor_to_mech(__u32 subflavor)
124 {
125         struct gss_api_mech *pos, *gm = NULL;
126
127         spin_lock(&registered_mechs_lock);
128         cfs_list_for_each_entry(pos, &registered_mechs, gm_list) {
129                 if (!cfs_try_module_get(pos->gm_owner))
130                         continue;
131                 if (!mech_supports_subflavor(pos, subflavor)) {
132                         cfs_module_put(pos->gm_owner);
133                         continue;
134                 }
135                 gm = pos;
136                 break;
137         }
138         spin_unlock(&registered_mechs_lock);
139         return gm;
140 }
141
142 void lgss_mech_put(struct gss_api_mech *gm)
143 {
144         cfs_module_put(gm->gm_owner);
145 }
146
147 /* The mech could probably be determined from the token instead, but it's just
148  * as easy for now to pass it in. */
149 __u32 lgss_import_sec_context(rawobj_t *input_token,
150                               struct gss_api_mech *mech,
151                               struct gss_ctx **ctx_id)
152 {
153         OBD_ALLOC_PTR(*ctx_id);
154         if (*ctx_id == NULL)
155                 return GSS_S_FAILURE;
156
157         (*ctx_id)->mech_type = lgss_mech_get(mech);
158
159         LASSERT(mech);
160         LASSERT(mech->gm_ops);
161         LASSERT(mech->gm_ops->gss_import_sec_context);
162         return mech->gm_ops->gss_import_sec_context(input_token, *ctx_id);
163 }
164
165 __u32 lgss_copy_reverse_context(struct gss_ctx *ctx_id,
166                                 struct gss_ctx **ctx_id_new)
167 {
168         struct gss_api_mech *mech = ctx_id->mech_type;
169         __u32                major;
170
171         LASSERT(mech);
172
173         OBD_ALLOC_PTR(*ctx_id_new);
174         if (*ctx_id_new == NULL)
175                 return GSS_S_FAILURE;
176
177         (*ctx_id_new)->mech_type = lgss_mech_get(mech);
178
179         LASSERT(mech);
180         LASSERT(mech->gm_ops);
181         LASSERT(mech->gm_ops->gss_copy_reverse_context);
182
183         major = mech->gm_ops->gss_copy_reverse_context(ctx_id, *ctx_id_new);
184         if (major != GSS_S_COMPLETE) {
185                 lgss_mech_put(mech);
186                 OBD_FREE_PTR(*ctx_id_new);
187                 *ctx_id_new = NULL;
188         }
189         return major;
190 }
191
192 /*
193  * this interface is much simplified, currently we only need endtime.
194  */
195 __u32 lgss_inquire_context(struct gss_ctx *context_handle,
196                            unsigned long  *endtime)
197 {
198         LASSERT(context_handle);
199         LASSERT(context_handle->mech_type);
200         LASSERT(context_handle->mech_type->gm_ops);
201         LASSERT(context_handle->mech_type->gm_ops->gss_inquire_context);
202
203         return context_handle->mech_type->gm_ops
204                 ->gss_inquire_context(context_handle,
205                                       endtime);
206 }
207
208 /* gss_get_mic: compute a mic over message and return mic_token. */
209 __u32 lgss_get_mic(struct gss_ctx *context_handle,
210                    int msgcnt,
211                    rawobj_t *msg,
212                    int iovcnt,
213                    lnet_kiov_t *iovs,
214                    rawobj_t *mic_token)
215 {
216         LASSERT(context_handle);
217         LASSERT(context_handle->mech_type);
218         LASSERT(context_handle->mech_type->gm_ops);
219         LASSERT(context_handle->mech_type->gm_ops->gss_get_mic);
220
221         return context_handle->mech_type->gm_ops
222                 ->gss_get_mic(context_handle,
223                               msgcnt,
224                               msg,
225                               iovcnt,
226                               iovs,
227                               mic_token);
228 }
229
230 /* gss_verify_mic: check whether the provided mic_token verifies message. */
231 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
232                       int msgcnt,
233                       rawobj_t *msg,
234                       int iovcnt,
235                       lnet_kiov_t *iovs,
236                       rawobj_t *mic_token)
237 {
238         LASSERT(context_handle);
239         LASSERT(context_handle->mech_type);
240         LASSERT(context_handle->mech_type->gm_ops);
241         LASSERT(context_handle->mech_type->gm_ops->gss_verify_mic);
242
243         return context_handle->mech_type->gm_ops
244                 ->gss_verify_mic(context_handle,
245                                  msgcnt,
246                                  msg,
247                                  iovcnt,
248                                  iovs,
249                                  mic_token);
250 }
251
252 __u32 lgss_wrap(struct gss_ctx *context_handle,
253                 rawobj_t *gsshdr,
254                 rawobj_t *msg,
255                 int msg_buflen,
256                 rawobj_t *out_token)
257 {
258         LASSERT(context_handle);
259         LASSERT(context_handle->mech_type);
260         LASSERT(context_handle->mech_type->gm_ops);
261         LASSERT(context_handle->mech_type->gm_ops->gss_wrap);
262
263         return context_handle->mech_type->gm_ops
264                 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
265 }
266
267 __u32 lgss_unwrap(struct gss_ctx *context_handle,
268                   rawobj_t *gsshdr,
269                   rawobj_t *token,
270                   rawobj_t *out_msg)
271 {
272         LASSERT(context_handle);
273         LASSERT(context_handle->mech_type);
274         LASSERT(context_handle->mech_type->gm_ops);
275         LASSERT(context_handle->mech_type->gm_ops->gss_unwrap);
276
277         return context_handle->mech_type->gm_ops
278                 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
279 }
280
281
282 __u32 lgss_prep_bulk(struct gss_ctx *context_handle,
283                      struct ptlrpc_bulk_desc *desc)
284 {
285         LASSERT(context_handle);
286         LASSERT(context_handle->mech_type);
287         LASSERT(context_handle->mech_type->gm_ops);
288         LASSERT(context_handle->mech_type->gm_ops->gss_prep_bulk);
289
290         return context_handle->mech_type->gm_ops
291                 ->gss_prep_bulk(context_handle, desc);
292 }
293
294 __u32 lgss_wrap_bulk(struct gss_ctx *context_handle,
295                      struct ptlrpc_bulk_desc *desc,
296                      rawobj_t *token,
297                      int adj_nob)
298 {
299         LASSERT(context_handle);
300         LASSERT(context_handle->mech_type);
301         LASSERT(context_handle->mech_type->gm_ops);
302         LASSERT(context_handle->mech_type->gm_ops->gss_wrap_bulk);
303
304         return context_handle->mech_type->gm_ops
305                 ->gss_wrap_bulk(context_handle, desc, token, adj_nob);
306 }
307
308 __u32 lgss_unwrap_bulk(struct gss_ctx *context_handle,
309                        struct ptlrpc_bulk_desc *desc,
310                        rawobj_t *token,
311                        int adj_nob)
312 {
313         LASSERT(context_handle);
314         LASSERT(context_handle->mech_type);
315         LASSERT(context_handle->mech_type->gm_ops);
316         LASSERT(context_handle->mech_type->gm_ops->gss_unwrap_bulk);
317
318         return context_handle->mech_type->gm_ops
319                 ->gss_unwrap_bulk(context_handle, desc, token, adj_nob);
320 }
321
322 /* gss_delete_sec_context: free all resources associated with context_handle.
323  * Note this differs from the RFC 2744-specified prototype in that we don't
324  * bother returning an output token, since it would never be used anyway. */
325
326 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
327 {
328         struct gss_api_mech *mech;
329
330         CDEBUG(D_SEC, "deleting %p\n", *context_handle);
331
332         if (!*context_handle)
333                 return(GSS_S_NO_CONTEXT);
334
335         mech = (*context_handle)->mech_type;
336         if ((*context_handle)->internal_ctx_id != 0) {
337                 LASSERT(mech);
338                 LASSERT(mech->gm_ops);
339                 LASSERT(mech->gm_ops->gss_delete_sec_context);
340                 mech->gm_ops->gss_delete_sec_context(
341                                         (*context_handle)->internal_ctx_id);
342         }
343         if (mech)
344                 lgss_mech_put(mech);
345
346         OBD_FREE_PTR(*context_handle);
347         *context_handle=NULL;
348         return GSS_S_COMPLETE;
349 }
350
351 int lgss_display(struct gss_ctx *ctx,
352                  char           *buf,
353                  int             bufsize)
354 {
355         LASSERT(ctx);
356         LASSERT(ctx->mech_type);
357         LASSERT(ctx->mech_type->gm_ops);
358         LASSERT(ctx->mech_type->gm_ops->gss_display);
359
360         return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);
361 }