Whamcloud - gitweb
b=15266
[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 spinlock_t registered_mechs_lock = SPIN_LOCK_UNLOCKED;
73
74 int lgss_mech_register(struct gss_api_mech *gm)
75 {
76         spin_lock(&registered_mechs_lock);
77         list_add(&gm->gm_list, &registered_mechs);
78         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         spin_lock(&registered_mechs_lock);
86         list_del(&gm->gm_list);
87         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         __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         spin_lock(&registered_mechs_lock);
103         list_for_each_entry(pos, &registered_mechs, gm_list) {
104                 if (0 == strcmp(name, pos->gm_name)) {
105                         if (!try_module_get(pos->gm_owner))
106                                 continue;
107                         gm = pos;
108                         break;
109                 }
110         }
111         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         spin_lock(&registered_mechs_lock);
133         list_for_each_entry(pos, &registered_mechs, gm_list) {
134                 if (!try_module_get(pos->gm_owner))
135                         continue;
136                 if (!mech_supports_subflavor(pos, subflavor)) {
137                         module_put(pos->gm_owner);
138                         continue;
139                 }
140                 gm = pos;
141                 break;
142         }
143         spin_unlock(&registered_mechs_lock);
144         return gm;
145 }
146
147 void lgss_mech_put(struct gss_api_mech *gm)
148 {
149         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                    rawobj_t *mic_token)
218 {
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);
223
224         return context_handle->mech_type->gm_ops
225                 ->gss_get_mic(context_handle,
226                               msgcnt,
227                               msg,
228                               mic_token);
229 }
230
231 /* gss_verify_mic: check whether the provided mic_token verifies message. */
232 __u32 lgss_verify_mic(struct gss_ctx *context_handle,
233                       int msgcnt,
234                       rawobj_t *msg,
235                       rawobj_t *mic_token)
236 {
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);
241
242         return context_handle->mech_type->gm_ops
243                 ->gss_verify_mic(context_handle,
244                                  msgcnt,
245                                  msg,
246                                  mic_token);
247 }
248
249 __u32 lgss_wrap(struct gss_ctx *context_handle,
250                 rawobj_t *gsshdr,
251                 rawobj_t *msg,
252                 int msg_buflen,
253                 rawobj_t *out_token)
254 {
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);
259
260         return context_handle->mech_type->gm_ops
261                 ->gss_wrap(context_handle, gsshdr, msg, msg_buflen, out_token);
262 }
263
264 __u32 lgss_unwrap(struct gss_ctx *context_handle,
265                   rawobj_t *gsshdr,
266                   rawobj_t *token,
267                   rawobj_t *out_msg)
268 {
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);
273
274         return context_handle->mech_type->gm_ops
275                 ->gss_unwrap(context_handle, gsshdr, token, out_msg);
276 }
277
278
279 __u32 lgss_plain_encrypt(struct gss_ctx *ctx,
280                          int decrypt,
281                          int length,
282                          void *in_buf,
283                          void *out_buf)
284 {
285         LASSERT(ctx);
286         LASSERT(ctx->mech_type);
287         LASSERT(ctx->mech_type->gm_ops);
288         LASSERT(ctx->mech_type->gm_ops->gss_plain_encrypt);
289
290         return ctx->mech_type->gm_ops
291                 ->gss_plain_encrypt(ctx, decrypt, length, in_buf, out_buf);
292 }
293
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. */
297
298 __u32 lgss_delete_sec_context(struct gss_ctx **context_handle)
299 {
300         struct gss_api_mech *mech;
301
302         CDEBUG(D_SEC, "deleting %p\n", *context_handle);
303
304         if (!*context_handle)
305                 return(GSS_S_NO_CONTEXT);
306
307         mech = (*context_handle)->mech_type;
308         if ((*context_handle)->internal_ctx_id != 0) {
309                 LASSERT(mech);
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);
314         }
315         if (mech)
316                 lgss_mech_put(mech);
317
318         OBD_FREE_PTR(*context_handle);
319         *context_handle=NULL;
320         return GSS_S_COMPLETE;
321 }
322
323 int lgss_display(struct gss_ctx *ctx,
324                  char           *buf,
325                  int             bufsize)
326 {
327         LASSERT(ctx);
328         LASSERT(ctx->mech_type);
329         LASSERT(ctx->mech_type->gm_ops);
330         LASSERT(ctx->mech_type->gm_ops->gss_display);
331
332         return ctx->mech_type->gm_ops->gss_display(ctx, buf, bufsize);
333 }