1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Modifications for Lustre
5 * Copyright 2004, Cluster File Systems, Inc.
7 * Author: Eric Mei <ericm@clusterfs.com>
11 * linux/net/sunrpc/gss_krb5_mech.c
13 * Copyright (c) 2001 The Regents of the University of Michigan.
14 * All rights reserved.
16 * Andy Adamson <andros@umich.edu>
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/crypto.h>
56 #include <liblustre.h>
57 //#include "../kcrypto/libcrypto.h"
60 #include <libcfs/kp30.h>
61 #include <linux/obd.h>
62 #include <linux/obd_class.h>
63 #include <linux/obd_support.h>
64 #include <linux/lustre_idl.h>
65 #include <linux/lustre_net.h>
66 #include <linux/lustre_import.h>
67 #include <linux/lustre_sec.h>
70 #include "gss_internal.h"
74 rawobj_t gss_mech_krb5_oid =
75 {9, (__u8 *)"\052\206\110\206\367\022\001\002\002"};
78 get_bytes(char **ptr, const char *end, void *res, int len)
91 get_rawobj(char **ptr, const char *end, rawobj_t *res)
95 if (get_bytes(&p, end, &res->len, sizeof(res->len)))
100 OBD_ALLOC(res->data, res->len);
103 memcpy(res->data, p, res->len);
109 get_key(char **p, char *end, struct crypto_tfm **res)
115 if (get_bytes(p, end, &alg, sizeof(alg)))
117 if ((get_rawobj(p, end, &key)))
121 case ENCTYPE_DES_CBC_RAW:
123 alg_mode = CRYPTO_TFM_MODE_CBC;
126 CERROR("unsupported algorithm %d\n", alg);
127 goto out_err_free_key;
129 if (!(*res = crypto_alloc_tfm(alg_name, alg_mode)))
130 goto out_err_free_key;
131 if (crypto_cipher_setkey(*res, key.data, key.len))
132 goto out_err_free_tfm;
134 OBD_FREE(key.data, key.len);
138 crypto_free_tfm(*res);
140 OBD_FREE(key.data, key.len);
146 gss_import_sec_context_kerberos(rawobj_t *inbuf,
147 struct gss_ctx *ctx_id)
149 char *p = (char *)inbuf->data;
150 char *end = (char *)(inbuf->data + inbuf->len);
151 struct krb5_ctx *ctx;
153 OBD_ALLOC(ctx, sizeof(*ctx));
157 if (get_bytes(&p, end, &ctx->initiate, sizeof(ctx->initiate)))
158 goto out_err_free_ctx;
159 if (get_bytes(&p, end, &ctx->seed_init, sizeof(ctx->seed_init)))
160 goto out_err_free_ctx;
161 if (get_bytes(&p, end, ctx->seed, sizeof(ctx->seed)))
162 goto out_err_free_ctx;
163 if (get_bytes(&p, end, &ctx->signalg, sizeof(ctx->signalg)))
164 goto out_err_free_ctx;
165 if (get_bytes(&p, end, &ctx->sealalg, sizeof(ctx->sealalg)))
166 goto out_err_free_ctx;
167 if (get_bytes(&p, end, &ctx->endtime, sizeof(ctx->endtime)))
168 goto out_err_free_ctx;
169 if (get_bytes(&p, end, &ctx->seq_send, sizeof(ctx->seq_send)))
170 goto out_err_free_ctx;
171 if (get_rawobj(&p, end, &ctx->mech_used))
172 goto out_err_free_ctx;
173 if (get_key(&p, end, &ctx->enc))
174 goto out_err_free_mech;
175 if (get_key(&p, end, &ctx->seq))
176 goto out_err_free_key1;
178 goto out_err_free_key2;
180 ctx_id->internal_ctx_id = ctx;
181 CDEBUG(D_SEC, "Succesfully imported new context.\n");
185 crypto_free_tfm(ctx->seq);
187 crypto_free_tfm(ctx->enc);
189 OBD_FREE(ctx->mech_used.data, ctx->mech_used.len);
191 OBD_FREE(ctx, sizeof(*ctx));
193 return GSS_S_FAILURE;
197 gss_inquire_context_kerberos(struct gss_ctx *context_handle,
200 struct krb5_ctx *kctx = context_handle->internal_ctx_id;
202 *endtime = (__u64) ((__u32) kctx->endtime);
203 return GSS_S_COMPLETE;
207 gss_delete_sec_context_kerberos(void *internal_ctx)
209 struct krb5_ctx *ctx = internal_ctx;
212 crypto_free_tfm(ctx->seq);
214 crypto_free_tfm(ctx->enc);
215 if (ctx->mech_used.data)
216 OBD_FREE(ctx->mech_used.data, ctx->mech_used.len);
217 OBD_FREE(ctx, sizeof(*ctx));
220 /* XXX the following wrappers have become pointless; kill them. */
222 gss_verify_mic_kerberos(struct gss_ctx *ctx,
227 struct krb5_ctx *kctx = ctx->internal_ctx_id;
231 maj_stat = krb5_read_token(kctx, mic_token, message, &qop_state);
232 if (!maj_stat && qop_state)
235 CDEBUG(D_SEC, "returning %d\n", maj_stat);
240 gss_get_mic_kerberos(struct gss_ctx *ctx,
245 struct krb5_ctx *kctx = ctx->internal_ctx_id;
248 err = krb5_make_token(kctx, qop, message, mic_token);
250 CDEBUG(D_SEC, "returning %d\n",err);
254 static struct gss_api_ops gss_kerberos_ops = {
255 .gss_import_sec_context = gss_import_sec_context_kerberos,
256 .gss_inquire_context = gss_inquire_context_kerberos,
257 .gss_get_mic = gss_get_mic_kerberos,
258 .gss_verify_mic = gss_verify_mic_kerberos,
259 .gss_wrap = gss_wrap_kerberos,
260 .gss_unwrap = gss_unwrap_kerberos,
261 .gss_delete_sec_context = gss_delete_sec_context_kerberos,
264 static struct subflavor_desc gss_kerberos_sfs[] = {
266 .subflavor = PTLRPCS_SUBFLVR_KRB5,
268 .service = PTLRPCS_SVC_NONE,
272 .subflavor = PTLRPCS_SUBFLVR_KRB5I,
274 .service = PTLRPCS_SVC_AUTH,
278 .subflavor = PTLRPCS_SUBFLVR_KRB5P,
280 .service = PTLRPCS_SVC_PRIV,
285 static struct gss_api_mech gss_kerberos_mech = {
287 .gm_owner = THIS_MODULE,
288 .gm_ops = &gss_kerberos_ops,
290 .gm_sfs = gss_kerberos_sfs,
293 /*static*/ int __init init_kerberos_module(void)
297 status = kgss_mech_register(&gss_kerberos_mech);
299 CERROR("Failed to register kerberos gss mechanism!\n");
303 /*static*/ void __exit cleanup_kerberos_module(void)
305 kgss_mech_unregister(&gss_kerberos_mech);
308 /* XXX enable this when module works */
310 MODULE_LICENSE("GPL");
311 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
312 MODULE_DESCRIPTION("GSS Krb5 mechanism for Lustre");
314 module_init(init_kerberos_module);
315 module_exit(cleanup_kerberos_module);