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/auth_gss.c
13 * RPCSEC_GSS client authentication.
15 * Copyright (c) 2000 The Regents of the University of Michigan.
16 * All rights reserved.
18 * Dug Song <dugsong@monkey.org>
19 * Andy Adamson <andros@umich.edu>
21 * Redistribution and use in source and binary forms, with or without
22 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. Neither the name of the University nor the names of its
31 * contributors may be used to endorse or promote products derived
32 * from this software without specific prior written permission.
34 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
35 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
36 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
39 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
40 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
41 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
42 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
43 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
44 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 # define EXPORT_SYMTAB
51 #define DEBUG_SUBSYSTEM S_SEC
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/dcache.h>
58 #include <linux/random.h>
61 #include <linux/sunrpc/rpc_pipe_fs.h>
63 #include <liblustre.h>
66 #include <libcfs/kp30.h>
67 #include <linux/obd.h>
68 #include <linux/obd_class.h>
69 #include <linux/obd_support.h>
70 #include <linux/lustre_idl.h>
71 #include <linux/lustre_net.h>
72 #include <linux/lustre_import.h>
73 #include <linux/lustre_sec.h>
76 #include "gss_internal.h"
79 #define GSS_CREDCACHE_EXPIRE (60) /* 1 minute */
80 #define GSS_CRED_EXPIRE (8 * 60 * 60) /* 8 hours */
81 #define GSS_CRED_SIGN_SIZE (1024)
82 #define GSS_CRED_VERIFY_SIZE (56)
84 #define LUSTRE_PIPEDIR "/lustre"
86 /**********************************************
87 * gss security init/fini helper *
88 **********************************************/
90 #define SECINIT_RPC_TIMEOUT (10)
91 #define SECFINI_RPC_TIMEOUT (10)
93 static int secinit_compose_request(struct obd_import *imp,
94 char *buf, int bufsize,
99 struct ptlrpcs_wire_hdr *hdr;
100 struct lustre_msg *lmsg;
101 struct mds_req_sec_desc *secdesc;
102 __u32 size = sizeof(*secdesc);
106 lmsg_size = lustre_msg_size(1, &size);
108 if (sizeof(*hdr) + lmsg_size + size_round(token_size) > bufsize) {
109 CERROR("token size %ld too large\n", token_size);
113 /* security wire hdr */
114 hdr = buf_to_sec_hdr(buf);
115 hdr->flavor = cpu_to_le32(PTLRPC_SEC_GSS);
116 hdr->sectype = cpu_to_le32(PTLRPC_SEC_TYPE_NONE);
117 hdr->msg_len = cpu_to_le32(lmsg_size);
118 hdr->sec_len = cpu_to_le32(7 * 4 + token_size);
120 /* lustre message & secdesc */
121 lmsg = buf_to_lustre_msg(buf);
123 lustre_init_msg(lmsg, 1, &size, NULL);
124 secdesc = lustre_msg_buf(lmsg, 0, size);
125 secdesc->rsd_uid = secdesc->rsd_fsuid = uid;
126 secdesc->rsd_gid = secdesc->rsd_fsgid = gid;
127 secdesc->rsd_cap = secdesc->rsd_ngroups = 0;
129 lmsg->handle = imp->imp_remote_handle;
130 lmsg->type = PTL_RPC_MSG_REQUEST;
131 lmsg->opc = SEC_INIT;
133 lmsg->conn_cnt = imp->imp_conn_cnt;
135 p = (__u32 *) (buf + sizeof(*hdr) + lmsg_size);
138 *p++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION); /* gss version */
139 *p++ = cpu_to_le32(PTLRPC_SEC_GSS_KRB5I); /* subflavor */
140 *p++ = cpu_to_le32(PTLRPC_GSS_PROC_INIT); /* proc */
141 *p++ = cpu_to_le32(0); /* seq */
142 *p++ = cpu_to_le32(PTLRPC_GSS_SVC_NONE); /* service */
143 *p++ = cpu_to_le32(0); /* context handle */
145 /* now the token part */
146 *p++ = cpu_to_le32((__u32) token_size);
147 LASSERT(((char *)p - buf) + token_size <= bufsize);
149 rc = copy_from_user(p, token, token_size);
151 CERROR("can't copy token\n");
155 rc = size_round(((char *)p - buf) + token_size);
159 static int secinit_parse_reply(char *repbuf, int replen,
160 char __user *outbuf, long outlen)
162 __u32 *p = (__u32 *)repbuf;
163 struct ptlrpcs_wire_hdr *hdr = (struct ptlrpcs_wire_hdr *) repbuf;
164 __u32 lmsg_len, sec_len, status;
165 __u32 major, minor, seq, obj_len, round_len;
168 if (replen <= (4 + 6) * 4) {
169 CERROR("reply size %d too small\n", replen);
173 hdr->flavor = le32_to_cpu(hdr->flavor);
174 hdr->sectype = le32_to_cpu(hdr->sectype);
175 hdr->msg_len = le32_to_cpu(hdr->msg_len);
176 hdr->sec_len = le32_to_cpu(hdr->sec_len);
178 lmsg_len = le32_to_cpu(p[2]);
179 sec_len = le32_to_cpu(p[3]);
182 if (hdr->flavor != PTLRPC_SEC_GSS ||
183 hdr->sectype != PTLRPC_SEC_TYPE_NONE) {
184 CERROR("unexpected reply\n");
187 if (hdr->msg_len % 8 ||
188 sizeof(*hdr) + hdr->msg_len + hdr->sec_len > replen) {
189 CERROR("unexpected reply\n");
192 if (hdr->sec_len > outlen) {
193 CERROR("outbuf too small\n");
197 p = (__u32 *) buf_to_sec_data(repbuf);
200 status = le32_to_cpu(*p++);
201 major = le32_to_cpu(*p++);
202 minor = le32_to_cpu(*p++);
203 seq = le32_to_cpu(*p++);
206 if (copy_to_user(outbuf, &status, 4))
209 if (copy_to_user(outbuf, &major, 4))
212 if (copy_to_user(outbuf, &minor, 4))
215 if (copy_to_user(outbuf, &seq, 4))
219 obj_len = le32_to_cpu(*p++);
220 round_len = (obj_len + 3) & ~ 3;
221 if (copy_to_user(outbuf, &obj_len, 4))
224 if (copy_to_user(outbuf, (char *)p, round_len))
228 effective += 4 + round_len;
230 obj_len = le32_to_cpu(*p++);
231 round_len = (obj_len + 3) & ~ 3;
232 if (copy_to_user(outbuf, &obj_len, 4))
235 if (copy_to_user(outbuf, (char *)p, round_len))
239 effective += 4 + round_len;
244 /* XXX move to where lgssd could see */
245 struct lgssd_ioctl_param {
246 int version; /* in */
250 long send_token_size;/* in */
251 char *send_token; /* in */
252 long reply_buf_size; /* in */
253 char *reply_buf; /* in */
254 long status; /* out */
255 long reply_length; /* out */
258 static int gss_send_secinit_rpc(__user char *buffer, unsigned long count)
260 struct obd_import *imp;
261 struct lgssd_ioctl_param param;
262 const int reqbuf_size = 1024;
263 const int repbuf_size = 1024;
264 char *reqbuf, *repbuf;
265 struct obd_device *obd;
268 int rc, reqlen, replen;
270 if (count != sizeof(param)) {
271 CERROR("partial write\n");
274 if (copy_from_user(¶m, buffer, sizeof(param)))
277 if (param.version != GSSD_INTERFACE_VERSION) {
278 CERROR("gssd interface version %d (expect %d)\n",
279 param.version, GSSD_INTERFACE_VERSION);
284 if (strncpy_from_user(obdname, param.uuid,
285 sizeof(obdname)) <= 0) {
286 CERROR("Invalid obdname pointer\n");
290 obd = class_name2obd(obdname);
292 CERROR("no such obd %s\n", obdname);
295 if (strcmp(obd->obd_type->typ_name, "mdc") &&
296 strcmp(obd->obd_type->typ_name, "osc")) {
297 CERROR("%s not a mdc/osc device\n", obdname);
301 imp = class_import_get(obd->u.cli.cl_import);
303 OBD_ALLOC(reqbuf, reqbuf_size);
304 OBD_ALLOC(repbuf, reqbuf_size);
306 if (!reqbuf || !repbuf) {
307 CERROR("Can't alloc buffer: %p/%p\n", reqbuf, repbuf);
308 param.status = -ENOMEM;
313 reqlen = secinit_compose_request(imp, reqbuf, reqbuf_size,
314 param.uid, param.gid,
315 param.send_token_size,
318 param.status = reqlen;
322 replen = repbuf_size;
323 rc = ptlrpc_do_rawrpc(imp, reqbuf, reqlen,
324 repbuf, &replen, SECINIT_RPC_TIMEOUT);
330 if (replen > param.reply_buf_size) {
331 CERROR("output buffer size %ld too small, need %d\n",
332 param.reply_buf_size, replen);
333 param.status = -EINVAL;
337 lsize = secinit_parse_reply(repbuf, replen,
338 param.reply_buf, param.reply_buf_size);
340 param.status = (int) lsize;
345 param.reply_length = lsize;
348 if (copy_to_user(buffer, ¶m, sizeof(param)))
353 class_import_put(imp);
355 OBD_FREE(repbuf, repbuf_size);
357 OBD_FREE(reqbuf, reqbuf_size);
361 static int gss_send_secfini_rpc(struct obd_import *imp,
362 char *reqbuf, int reqlen)
364 const int repbuf_size = 1024;
366 int replen = repbuf_size;
369 OBD_ALLOC(repbuf, repbuf_size);
371 CERROR("Out of memory\n");
375 rc = ptlrpc_do_rawrpc(imp, reqbuf, reqlen, repbuf, &replen,
376 SECFINI_RPC_TIMEOUT);
378 OBD_FREE(repbuf, repbuf_size);
382 /**********************************************
383 * structure definitions *
384 **********************************************/
386 struct ptlrpc_sec gs_base;
387 struct gss_api_mech *gs_mech;
390 struct list_head gs_upcalls;
392 struct dentry *gs_depipe;
398 static rwlock_t gss_ctx_lock = RW_LOCK_UNLOCKED;
400 struct gss_upcall_msg_data {
408 struct gss_upcall_msg {
409 struct rpc_pipe_msg gum_base;
410 atomic_t gum_refcount;
411 struct list_head gum_list;
412 struct gss_sec *gum_gsec;
413 wait_queue_head_t gum_waitq;
414 char gum_obdname[64];
415 struct gss_upcall_msg_data gum_data;
418 /**********************************************
419 * rpc_pipe upcall helpers *
420 **********************************************/
422 void gss_release_msg(struct gss_upcall_msg *gmsg)
425 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
427 if (!atomic_dec_and_test(&gmsg->gum_refcount)) {
428 CDEBUG(D_SEC, "gmsg %p ref %d\n", gmsg,
429 atomic_read(&gmsg->gum_refcount));
433 LASSERT(list_empty(&gmsg->gum_list));
434 LASSERT(list_empty(&gmsg->gum_base.list));
435 OBD_FREE(gmsg, sizeof(*gmsg));
440 gss_unhash_msg_nolock(struct gss_upcall_msg *gmsg)
443 if (list_empty(&gmsg->gum_list)) {
448 list_del_init(&gmsg->gum_list);
449 wake_up(&gmsg->gum_waitq);
450 atomic_dec(&gmsg->gum_refcount);
451 CDEBUG(D_SEC, "gmsg %p refcount now %d\n",
452 gmsg, atomic_read(&gmsg->gum_refcount));
453 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
458 gss_unhash_msg(struct gss_upcall_msg *gmsg)
460 struct gss_sec *gsec = gmsg->gum_gsec;
462 spin_lock(&gsec->gs_lock);
463 gss_unhash_msg_nolock(gmsg);
464 spin_unlock(&gsec->gs_lock);
468 struct gss_upcall_msg * gss_find_upcall(struct gss_sec *gsec,
470 struct gss_upcall_msg_data *gmd)
472 struct gss_upcall_msg *gmsg;
475 list_for_each_entry(gmsg, &gsec->gs_upcalls, gum_list) {
476 if (memcmp(&gmsg->gum_data, gmd, sizeof(*gmd)))
478 if (strcmp(gmsg->gum_obdname, obdname))
480 atomic_inc(&gmsg->gum_refcount);
481 CDEBUG(D_SEC, "found gmsg at %p: obdname %s, uid %d, ref %d\n",
482 gmsg, obdname, gmd->gum_uid,
483 atomic_read(&gmsg->gum_refcount));
489 static void gss_init_upcall_msg(struct gss_upcall_msg *gmsg,
490 struct gss_sec *gsec, char *obdname,
491 struct gss_upcall_msg_data *gmd)
493 struct rpc_pipe_msg *rpcmsg;
496 /* 2 refs: 1 for hash, 1 for current user */
497 init_waitqueue_head(&gmsg->gum_waitq);
498 list_add(&gmsg->gum_list, &gsec->gs_upcalls);
499 atomic_set(&gmsg->gum_refcount, 2);
500 gmsg->gum_gsec = gsec;
501 strncpy(gmsg->gum_obdname, obdname, sizeof(gmsg->gum_obdname));
502 memcpy(&gmsg->gum_data, gmd, sizeof(*gmd));
504 rpcmsg = &gmsg->gum_base;
505 rpcmsg->data = &gmsg->gum_data;
506 rpcmsg->len = sizeof(gmsg->gum_data);
509 #endif /* __KERNEL__ */
511 /********************************************
512 * gss cred manipulation helpers *
513 ********************************************/
515 int gss_cred_is_uptodate_ctx(struct ptlrpc_cred *cred)
517 struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
520 read_lock(&gss_ctx_lock);
521 if ((cred->pc_flags & PTLRPC_CRED_UPTODATE) && gcred->gc_ctx)
523 read_unlock(&gss_ctx_lock);
528 struct gss_cl_ctx * gss_get_ctx(struct gss_cl_ctx *ctx)
530 atomic_inc(&ctx->gc_refcount);
535 void gss_destroy_ctx(struct gss_cl_ctx *ctx)
539 CDEBUG(D_SEC, "destroy cl_ctx %p\n", ctx);
541 kgss_delete_sec_context(&ctx->gc_gss_ctx);
543 if (ctx->gc_wire_ctx.len > 0) {
544 OBD_FREE(ctx->gc_wire_ctx.data, ctx->gc_wire_ctx.len);
545 ctx->gc_wire_ctx.len = 0;
548 OBD_FREE(ctx, sizeof(*ctx));
552 void gss_put_ctx(struct gss_cl_ctx *ctx)
554 if (atomic_dec_and_test(&ctx->gc_refcount))
555 gss_destroy_ctx(ctx);
559 struct gss_cl_ctx *gss_cred_get_ctx(struct ptlrpc_cred *cred)
561 struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
562 struct gss_cl_ctx *ctx = NULL;
564 read_lock(&gss_ctx_lock);
566 ctx = gss_get_ctx(gcred->gc_ctx);
567 read_unlock(&gss_ctx_lock);
572 void gss_cred_set_ctx(struct ptlrpc_cred *cred, struct gss_cl_ctx *ctx)
574 struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
575 struct gss_cl_ctx *old;
579 if (kgss_inquire_context(ctx->gc_gss_ctx, &ctx_expiry)) {
580 CERROR("unable to get expire time\n");
581 ctx_expiry = 1; /* make it expired now */
583 cred->pc_expire = (unsigned long) ctx_expiry;
585 write_lock(&gss_ctx_lock);
588 cred->pc_flags |= PTLRPC_CRED_UPTODATE;
589 write_unlock(&gss_ctx_lock);
593 CWARN("client refreshed gss cred %p(uid %u)\n", cred, cred->pc_uid);
598 simple_get_bytes(char **buf, __u32 *buflen, void *res, __u32 reslen)
600 if (*buflen < reslen) {
601 CERROR("buflen %u < %u\n", *buflen, reslen);
605 memcpy(res, *buf, reslen);
615 * - wire_ctx (rawobj)
616 * - mech_ctx? (rawobj)
619 int gss_parse_init_downcall(struct gss_api_mech *gm, rawobj_t *buf,
620 struct gss_cl_ctx **gc,
621 struct gss_upcall_msg_data *gmd, int *gss_err)
624 __u32 len = buf->len;
625 struct gss_cl_ctx *ctx;
627 unsigned int timeout;
634 OBD_ALLOC(ctx, sizeof(*ctx));
638 ctx->gc_proc = RPC_GSS_PROC_DATA;
640 spin_lock_init(&ctx->gc_seq_lock);
641 atomic_set(&ctx->gc_refcount,1);
643 if (simple_get_bytes(&p, &len, &gmd->gum_uid, sizeof(gmd->gum_uid)))
645 if (simple_get_bytes(&p, &len, &gmd->gum_svc, sizeof(gmd->gum_svc)))
647 if (simple_get_bytes(&p, &len, &gmd->gum_nal, sizeof(gmd->gum_nal)))
649 if (simple_get_bytes(&p, &len, &gmd->gum_netid, sizeof(gmd->gum_netid)))
651 if (simple_get_bytes(&p, &len, &gmd->gum_nid, sizeof(gmd->gum_nid)))
653 /* FIXME: discarded timeout for now */
654 if (simple_get_bytes(&p, &len, &timeout, sizeof(timeout)))
656 if (simple_get_bytes(&p, &len, &ctx->gc_win, sizeof(ctx->gc_win)))
659 /* lgssd signals an error by passing ctx->gc_win = 0: */
661 /* in which case the next 2 int are:
665 if (simple_get_bytes(&p, &len, &err, sizeof(err))) {
669 if (simple_get_bytes(&p, &len, gss_err, sizeof(*gss_err))) {
673 if (err == 0 && *gss_err == 0) {
674 CERROR("no error passed from downcall\n");
680 if (rawobj_extract_local(&tmp_buf, (__u32 **) ((void *)&p), &len))
682 if (rawobj_dup(&ctx->gc_wire_ctx, &tmp_buf)) {
686 if (rawobj_extract_local(&tmp_buf, (__u32 **) ((void *)&p), &len))
687 goto err_free_wire_ctx;
689 CERROR("unexpected trailing %u bytes\n", len);
690 goto err_free_wire_ctx;
692 if (kgss_import_sec_context(&tmp_buf, gm, &ctx->gc_gss_ctx))
693 goto err_free_wire_ctx;
699 if (ctx->gc_wire_ctx.data)
700 OBD_FREE(ctx->gc_wire_ctx.data, ctx->gc_wire_ctx.len);
702 OBD_FREE(ctx, sizeof(*ctx));
703 CDEBUG(D_SEC, "err_code %d, gss code %d\n", err, *gss_err);
707 /***************************************
709 ***************************************/
711 #define CRED_REFRESH_UPCALL_TIMEOUT (20)
712 static int gss_cred_refresh(struct ptlrpc_cred *cred)
714 struct obd_import *import;
715 struct gss_sec *gsec;
716 struct gss_upcall_msg *gss_msg, *gss_new;
717 struct gss_upcall_msg_data gmd;
718 struct dentry *dentry;
719 char *obdname, *obdtype;
721 uid_t uid = cred->pc_uid;
725 if (ptlrpcs_cred_is_uptodate(cred))
728 LASSERT(cred->pc_sec);
729 LASSERT(cred->pc_sec->ps_import);
730 LASSERT(cred->pc_sec->ps_import->imp_obd);
732 import = cred->pc_sec->ps_import;
733 if (!import->imp_connection) {
734 CERROR("import has no connection set\n");
739 gmd.gum_nal = import->imp_connection->c_peer.peer_ni->pni_number;
741 gmd.gum_nid = import->imp_connection->c_peer.peer_id.nid;
743 obdtype = import->imp_obd->obd_type->typ_name;
744 if (!strcmp(obdtype, "mdc"))
746 else if (!strcmp(obdtype, "osc"))
749 CERROR("gss on %s?\n", obdtype);
753 gsec = container_of(cred->pc_sec, struct gss_sec, gs_base);
754 obdname = import->imp_obd->obd_name;
755 dentry = gsec->gs_depipe;
759 CWARN("Initiate gss context %p(%u@%s)\n",
760 container_of(cred, struct gss_cred, gc_base),
761 uid, import->imp_target_uuid.uuid);
764 spin_lock(&gsec->gs_lock);
765 gss_msg = gss_find_upcall(gsec, obdname, &gmd);
768 OBD_FREE(gss_new, sizeof(*gss_new));
775 spin_unlock(&gsec->gs_lock);
776 OBD_ALLOC(gss_new, sizeof(*gss_new));
781 /* so far we'v created gss_new */
782 gss_init_upcall_msg(gss_new, gsec, obdname, &gmd);
784 if (gss_cred_is_uptodate_ctx(cred)) {
785 /* someone else had done it for us, simply cancel
787 CDEBUG(D_SEC, "cred("LPU64"/%u) has been refreshed by someone "
788 "else, simply drop our request\n",
789 cred->pc_pag, cred->pc_uid);
790 gss_unhash_msg_nolock(gss_new);
791 spin_unlock(&gsec->gs_lock);
792 gss_release_msg(gss_new);
796 /* need to make upcall now */
797 spin_unlock(&gsec->gs_lock);
798 res = rpc_queue_upcall(dentry->d_inode, &gss_new->gum_base);
800 CERROR("rpc_queue_upcall failed: %d\n", res);
801 gss_unhash_msg(gss_new);
802 gss_release_msg(gss_new);
803 cred->pc_flags |= PTLRPC_CRED_ERROR;
807 spin_lock(&gsec->gs_lock);
810 /* upcall might finish quickly */
811 if (list_empty(&gss_msg->gum_list)) {
812 spin_unlock(&gsec->gs_lock);
817 init_waitqueue_entry(&wait, current);
818 set_current_state(TASK_INTERRUPTIBLE);
819 add_wait_queue(&gss_msg->gum_waitq, &wait);
820 spin_unlock(&gsec->gs_lock);
822 res = schedule_timeout(CRED_REFRESH_UPCALL_TIMEOUT * HZ);
823 remove_wait_queue(&gss_msg->gum_waitq, &wait);
825 if (signal_pending(current)) {
826 CERROR("cred %p: interrupted upcall\n", cred);
828 cred->pc_flags |= PTLRPC_CRED_DEAD | PTLRPC_CRED_ERROR;
830 } else if (res == 0) {
831 CERROR("cred %p: upcall timedout\n", cred);
832 cred->pc_flags |= PTLRPC_CRED_DEAD | PTLRPC_CRED_ERROR;
838 gss_release_msg(gss_msg);
842 #else /* !__KERNEL__ */
843 extern int lgss_handle_krb5_upcall(uid_t uid, __u32 dest_ip,
845 char *buf, int bufsize,
846 int (*callback)(char*, unsigned long));
848 static int gss_cred_refresh(struct ptlrpc_cred *cred)
852 struct obd_import *imp;
853 struct gss_sec *gsec;
854 struct gss_api_mech *mech;
855 struct gss_cl_ctx *ctx = NULL;
856 struct vfs_cred vcred = { 0 };
863 LASSERT(cred->pc_sec);
864 LASSERT(cred->pc_sec->ps_import);
865 LASSERT(cred->pc_sec->ps_import->imp_obd);
867 if (ptlrpcs_cred_is_uptodate(cred))
870 imp = cred->pc_sec->ps_import;
871 peer_nid = imp->imp_connection->c_peer.peer_id.nid;
872 dest_ip = (__u32) (peer_nid & 0xFFFFFFFF);
873 subflavor = cred->pc_sec->ps_flavor.subflavor;
875 if (subflavor != PTLRPC_SEC_GSS_KRB5I) {
876 CERROR("unknown subflavor %u\n", subflavor);
877 GOTO(err_out, rc = -EINVAL);
880 rc = lgss_handle_krb5_upcall(cred->pc_uid, dest_ip,
881 imp->imp_obd->obd_name,
883 gss_send_secinit_rpc);
891 gsec = container_of(cred->pc_sec, struct gss_sec, gs_base);
892 mech = gsec->gs_mech;
894 rc = gss_parse_init_downcall(mech, &obj, &ctx, &vcred, &dest_ip,
897 CERROR("parse init downcall: rpc %d, gss 0x%x\n", rc, gss_err);
898 if (rc != -ERESTART || gss_err != 0)
899 cred->pc_flags |= PTLRPC_CRED_ERROR;
906 gss_cred_set_ctx(cred, ctx);
907 LASSERT(gss_cred_is_uptodate_ctx(cred));
911 cred->pc_flags |= PTLRPC_CRED_DEAD;
916 static int gss_cred_match(struct ptlrpc_cred *cred,
917 struct vfs_cred *vcred)
919 RETURN(cred->pc_pag == vcred->vc_pag);
922 static int gss_cred_sign(struct ptlrpc_cred *cred,
923 struct ptlrpc_request *req)
925 struct gss_cred *gcred;
926 struct gss_cl_ctx *ctx;
928 __u32 *vp, *vpsave, vlen, seclen;
929 __u32 seqnum, major, rc = 0;
932 LASSERT(req->rq_reqbuf);
933 LASSERT(req->rq_cred == cred);
935 gcred = container_of(cred, struct gss_cred, gc_base);
936 ctx = gss_cred_get_ctx(cred);
938 CERROR("cred %p("LPU64"/%u) invalidated?\n",
939 cred, cred->pc_pag, cred->pc_uid);
943 lmsg.len = req->rq_reqlen;
944 lmsg.data = (__u8 *) req->rq_reqmsg;
946 vp = (__u32 *) (lmsg.data + lmsg.len);
947 vlen = req->rq_reqbuf_len - sizeof(struct ptlrpcs_wire_hdr) -
951 if (vlen < 6 * 4 + size_round4(ctx->gc_wire_ctx.len)) {
952 CERROR("vlen %d, need %d\n",
953 vlen, 6 * 4 + size_round4(ctx->gc_wire_ctx.len));
958 spin_lock(&ctx->gc_seq_lock);
959 seqnum = ctx->gc_seq++;
960 spin_unlock(&ctx->gc_seq_lock);
962 *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION); /* version */
963 *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_KRB5I); /* subflavor */
964 *vp++ = cpu_to_le32(ctx->gc_proc); /* proc */
965 *vp++ = cpu_to_le32(seqnum); /* seq */
966 *vp++ = cpu_to_le32(PTLRPC_GSS_SVC_INTEGRITY); /* service */
969 if (rawobj_serialize(&ctx->gc_wire_ctx, &vp, &vlen)) {
973 CDEBUG(D_SEC, "encoded wire_ctx length %d\n", ctx->gc_wire_ctx.len);
975 vpsave = vp++; /* reserve for size */
979 mic.data = (char *) vp;
981 CDEBUG(D_SEC, "reqbuf at %p, lmsg at %p, len %d, mic at %p, len %d\n",
982 req->rq_reqbuf, lmsg.data, lmsg.len, mic.data, mic.len);
983 major = kgss_get_mic(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT, &lmsg, &mic);
985 CERROR("gss compute mic error, major %x\n", major);
990 *vpsave = cpu_to_le32(mic.len);
992 seclen = seclen - vlen + mic.len;
993 buf_to_sec_hdr(req->rq_reqbuf)->sec_len = cpu_to_le32(seclen);
994 req->rq_reqdata_len += size_round(seclen);
995 CDEBUG(D_SEC, "msg size %d, checksum size %d, total sec size %d\n",
996 lmsg.len, mic.len, seclen);
1002 static int gss_cred_verify(struct ptlrpc_cred *cred,
1003 struct ptlrpc_request *req)
1005 struct gss_cred *gcred;
1006 struct gss_cl_ctx *ctx;
1007 struct ptlrpcs_wire_hdr *sec_hdr;
1009 __u32 *vp, vlen, subflavor, proc, seq, svc;
1010 __u32 major, minor, rc;
1013 LASSERT(req->rq_repbuf);
1014 LASSERT(req->rq_cred == cred);
1016 sec_hdr = buf_to_sec_hdr(req->rq_repbuf);
1017 vp = (__u32 *) (req->rq_repbuf + sizeof(*sec_hdr) + sec_hdr->msg_len);
1018 vlen = sec_hdr->sec_len;
1021 CERROR("reply sec size %u too small\n", vlen);
1025 if (*vp++ != cpu_to_le32(PTLRPC_SEC_GSS_VERSION)) {
1026 CERROR("reply have different gss version\n");
1029 subflavor = le32_to_cpu(*vp++);
1030 proc = le32_to_cpu(*vp++);
1034 case PTLRPC_GSS_PROC_DATA:
1035 seq = le32_to_cpu(*vp++);
1036 svc = le32_to_cpu(*vp++);
1037 if (svc != PTLRPC_GSS_SVC_INTEGRITY) {
1038 CERROR("Unknown svc %d\n", svc);
1042 CERROR("Unexpected ctx handle\n");
1045 mic.len = le32_to_cpu(*vp++);
1047 if (vlen < mic.len) {
1048 CERROR("vlen %d, mic.len %d\n", vlen, mic.len);
1051 mic.data = (char *) vp;
1053 gcred = container_of(cred, struct gss_cred, gc_base);
1054 ctx = gss_cred_get_ctx(cred);
1057 lmsg.len = sec_hdr->msg_len;
1058 lmsg.data = (__u8 *) buf_to_lustre_msg(req->rq_repbuf);
1060 major = kgss_verify_mic(ctx->gc_gss_ctx, &lmsg, &mic, NULL);
1061 if (major != GSS_S_COMPLETE) {
1062 CERROR("gss verify mic error: major %x\n", major);
1063 GOTO(proc_data_out, rc = -EINVAL);
1066 req->rq_repmsg = (struct lustre_msg *) lmsg.data;
1067 req->rq_replen = lmsg.len;
1069 /* here we could check the seq number is the same one
1070 * we sent to server. but portals has prevent us from
1071 * replay attack, so maybe we don't need check it again.
1077 case PTLRPC_GSS_PROC_ERR:
1078 major = le32_to_cpu(*vp++);
1079 minor = le32_to_cpu(*vp++);
1080 /* server return NO_CONTEXT might be caused by context expire
1081 * or server reboot/failover. we refresh the cred transparently
1083 * In some cases, our gss handle is possible to be incidentally
1084 * identical to another handle since the handle itself is not
1085 * fully random. In krb5 case, the GSS_S_BAD_SIG will be
1086 * returned, maybe other gss error for other mechanism. Here we
1087 * only consider krb5 mech (FIXME) and try to establish new
1090 if (major == GSS_S_NO_CONTEXT ||
1091 major == GSS_S_BAD_SIG) {
1092 CWARN("req %p: server report cred %p %s, expired?\n",
1093 req, cred, (major == GSS_S_NO_CONTEXT) ?
1094 "NO_CONTEXT" : "BAD_SIG");
1096 ptlrpcs_cred_die(cred);
1097 rc = ptlrpcs_req_replace_dead_cred(req);
1099 req->rq_ptlrpcs_restart = 1;
1101 CERROR("replace dead cred failed %d\n", rc);
1103 CERROR("Unrecognized gss error (%x/%x)\n",
1109 CERROR("unknown gss proc %d\n", proc);
1116 static int gss_cred_seal(struct ptlrpc_cred *cred,
1117 struct ptlrpc_request *req)
1119 struct gss_cred *gcred;
1120 struct gss_cl_ctx *ctx;
1121 struct ptlrpcs_wire_hdr *sec_hdr;
1122 rawobj_buf_t msg_buf;
1123 rawobj_t cipher_buf;
1124 __u32 *vp, *vpsave, vlen, seclen;
1125 __u32 major, seqnum, rc = 0;
1128 LASSERT(req->rq_reqbuf);
1129 LASSERT(req->rq_cred == cred);
1131 gcred = container_of(cred, struct gss_cred, gc_base);
1132 ctx = gss_cred_get_ctx(cred);
1134 CERROR("cred %p("LPU64"/%u) invalidated?\n",
1135 cred, cred->pc_pag, cred->pc_uid);
1139 vp = (__u32 *) (req->rq_reqbuf + sizeof(*sec_hdr));
1140 vlen = req->rq_reqbuf_len - sizeof(*sec_hdr);
1143 if (vlen < 6 * 4 + size_round4(ctx->gc_wire_ctx.len)) {
1144 CERROR("vlen %d, need %d\n",
1145 vlen, 6 * 4 + size_round4(ctx->gc_wire_ctx.len));
1150 spin_lock(&ctx->gc_seq_lock);
1151 seqnum = ctx->gc_seq++;
1152 spin_unlock(&ctx->gc_seq_lock);
1154 *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION); /* version */
1155 *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_KRB5P); /* subflavor */
1156 *vp++ = cpu_to_le32(ctx->gc_proc); /* proc */
1157 *vp++ = cpu_to_le32(seqnum); /* seq */
1158 *vp++ = cpu_to_le32(PTLRPC_GSS_SVC_PRIVACY); /* service */
1161 if (rawobj_serialize(&ctx->gc_wire_ctx, &vp, &vlen)) {
1165 CDEBUG(D_SEC, "encoded wire_ctx length %d\n", ctx->gc_wire_ctx.len);
1167 vpsave = vp++; /* reserve for size */
1170 msg_buf.buf = (__u8 *) req->rq_reqmsg - GSS_PRIVBUF_PREFIX_LEN;
1171 msg_buf.buflen = req->rq_reqlen + GSS_PRIVBUF_PREFIX_LEN + GSS_PRIVBUF_SUFFIX_LEN;
1172 msg_buf.dataoff = GSS_PRIVBUF_PREFIX_LEN;
1173 msg_buf.datalen = req->rq_reqlen;
1175 cipher_buf.data = (__u8 *) vp;
1176 cipher_buf.len = vlen;
1178 major = kgss_wrap(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT,
1179 &msg_buf, &cipher_buf);
1181 CERROR("error wrap: major 0x%x\n", major);
1182 GOTO(out, rc = -EINVAL);
1185 *vpsave = cpu_to_le32(cipher_buf.len);
1187 seclen = seclen - vlen + cipher_buf.len;
1188 sec_hdr = buf_to_sec_hdr(req->rq_reqbuf);
1189 sec_hdr->sec_len = cpu_to_le32(seclen);
1190 req->rq_reqdata_len += size_round(seclen);
1192 CDEBUG(D_SEC, "msg size %d, total sec size %d\n",
1193 req->rq_reqlen, seclen);
1199 static int gss_cred_unseal(struct ptlrpc_cred *cred,
1200 struct ptlrpc_request *req)
1202 struct gss_cred *gcred;
1203 struct gss_cl_ctx *ctx;
1204 struct ptlrpcs_wire_hdr *sec_hdr;
1205 rawobj_t cipher_text, plain_text;
1206 __u32 *vp, vlen, subflavor, proc, seq, svc;
1210 LASSERT(req->rq_repbuf);
1211 LASSERT(req->rq_cred == cred);
1213 sec_hdr = buf_to_sec_hdr(req->rq_repbuf);
1214 if (sec_hdr->msg_len != 0) {
1215 CERROR("unexpected msg_len %u\n", sec_hdr->msg_len);
1219 vp = (__u32 *) (req->rq_repbuf + sizeof(*sec_hdr));
1220 vlen = sec_hdr->sec_len;
1223 CERROR("reply sec size %u too small\n", vlen);
1227 if (*vp++ != cpu_to_le32(PTLRPC_SEC_GSS_VERSION)) {
1228 CERROR("reply have different gss version\n");
1231 subflavor = le32_to_cpu(*vp++);
1232 proc = le32_to_cpu(*vp++);
1233 seq = le32_to_cpu(*vp++);
1234 svc = le32_to_cpu(*vp++);
1238 case PTLRPC_GSS_PROC_DATA:
1239 if (svc != PTLRPC_GSS_SVC_PRIVACY) {
1240 CERROR("Unknown svc %d\n", svc);
1244 CERROR("Unexpected ctx handle\n");
1249 cipher_text.len = le32_to_cpu(*vp++);
1250 cipher_text.data = (__u8 *) vp;
1253 if (vlen < cipher_text.len) {
1254 CERROR("cipher text to be %u while buf only %u\n",
1255 cipher_text.len, vlen);
1259 plain_text = cipher_text;
1261 gcred = container_of(cred, struct gss_cred, gc_base);
1262 ctx = gss_cred_get_ctx(cred);
1265 rc = kgss_unwrap(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT,
1266 &cipher_text, &plain_text);
1268 CERROR("error unwrap: 0x%x\n", rc);
1269 GOTO(proc_out, rc = -EINVAL);
1272 req->rq_repmsg = (struct lustre_msg *) vp;
1273 req->rq_replen = plain_text.len;
1280 CERROR("unknown gss proc %d\n", proc);
1287 static void destroy_gss_context(struct ptlrpc_cred *cred)
1289 struct ptlrpcs_wire_hdr *hdr;
1290 struct lustre_msg *lmsg;
1291 struct gss_cred *gcred;
1292 struct ptlrpc_request req;
1293 struct obd_import *imp;
1294 __u32 *vp, lmsg_size;
1297 /* cred's refcount is 0, steal one */
1298 atomic_inc(&cred->pc_refcount);
1300 gcred = container_of(cred, struct gss_cred, gc_base);
1301 gcred->gc_ctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
1302 imp = cred->pc_sec->ps_import;
1305 if (!(cred->pc_flags & PTLRPC_CRED_UPTODATE)) {
1306 CWARN("Destroy a dead gss cred %p(%u@%s), don't send rpc\n",
1307 gcred, cred->pc_uid, imp->imp_target_uuid.uuid);
1308 atomic_dec(&cred->pc_refcount);
1313 CWARN("client destroy gss cred %p(%u@%s)\n",
1314 gcred, cred->pc_uid, imp->imp_target_uuid.uuid);
1316 lmsg_size = lustre_msg_size(0, NULL);
1317 req.rq_reqbuf_len = sizeof(*hdr) + lmsg_size +
1318 ptlrpcs_est_req_payload(cred->pc_sec, lmsg_size);
1320 OBD_ALLOC(req.rq_reqbuf, req.rq_reqbuf_len);
1321 if (!req.rq_reqbuf) {
1322 CERROR("Fail to alloc reqbuf, cancel anyway\n");
1323 atomic_dec(&cred->pc_refcount);
1329 hdr = buf_to_sec_hdr(req.rq_reqbuf);
1330 hdr->flavor = cpu_to_le32(PTLRPC_SEC_GSS);
1331 hdr->sectype = cpu_to_le32(PTLRPC_SEC_TYPE_AUTH);
1332 hdr->msg_len = cpu_to_le32(lmsg_size);
1333 hdr->sec_len = cpu_to_le32(0);
1335 /* lustre message */
1336 lmsg = buf_to_lustre_msg(req.rq_reqbuf);
1337 lustre_init_msg(lmsg, 0, NULL, NULL);
1338 lmsg->handle = imp->imp_remote_handle;
1339 lmsg->type = PTL_RPC_MSG_REQUEST;
1340 lmsg->opc = SEC_FINI;
1342 lmsg->conn_cnt = imp->imp_conn_cnt;
1343 /* add this for randomize */
1344 get_random_bytes(&lmsg->last_xid, sizeof(lmsg->last_xid));
1345 get_random_bytes(&lmsg->transno, sizeof(lmsg->transno));
1347 vp = (__u32 *) req.rq_reqbuf;
1350 req.rq_reqmsg = buf_to_lustre_msg(req.rq_reqbuf);
1351 req.rq_reqlen = lmsg_size;
1352 req.rq_reqdata_len = sizeof(*hdr) + lmsg_size;
1354 if (gss_cred_sign(cred, &req)) {
1355 CERROR("failed to sign, cancel anyway\n");
1356 atomic_dec(&cred->pc_refcount);
1359 atomic_dec(&cred->pc_refcount);
1362 gss_send_secfini_rpc(imp, req.rq_reqbuf, req.rq_reqdata_len);
1364 OBD_FREE(req.rq_reqbuf, req.rq_reqbuf_len);
1368 static void gss_cred_destroy(struct ptlrpc_cred *cred)
1370 struct gss_cred *gcred;
1374 LASSERT(!atomic_read(&cred->pc_refcount));
1376 gcred = container_of(cred, struct gss_cred, gc_base);
1377 if (gcred->gc_ctx) {
1378 destroy_gss_context(cred);
1379 gss_put_ctx(gcred->gc_ctx);
1382 CDEBUG(D_SEC, "GSS_SEC: destroy cred %p\n", gcred);
1384 OBD_FREE(gcred, sizeof(*gcred));
1388 static struct ptlrpc_credops gss_credops = {
1389 .refresh = gss_cred_refresh,
1390 .match = gss_cred_match,
1391 .sign = gss_cred_sign,
1392 .verify = gss_cred_verify,
1393 .seal = gss_cred_seal,
1394 .unseal = gss_cred_unseal,
1395 .destroy = gss_cred_destroy,
1399 /*******************************************
1401 *******************************************/
1403 gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
1404 char *dst, size_t buflen)
1406 char *data = (char *)msg->data + msg->copied;
1407 ssize_t mlen = msg->len;
1413 left = copy_to_user(dst, data, mlen);
1419 msg->copied += mlen;
1425 gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
1428 const int bufsize = 1024;
1430 struct inode *inode = filp->f_dentry->d_inode;
1431 struct rpc_inode *rpci = RPC_I(inode);
1432 struct obd_import *import;
1433 struct ptlrpc_sec *sec;
1434 struct gss_sec *gsec;
1436 struct gss_api_mech *mech;
1437 struct vfs_cred vcred = { 0 };
1438 struct ptlrpc_cred *cred;
1439 struct gss_upcall_msg *gss_msg;
1440 struct gss_upcall_msg_data gmd = { 0 };
1441 struct gss_cl_ctx *ctx = NULL;
1446 if (mlen > bufsize) {
1447 CERROR("mlen %ld > bufsize %d\n", (long)mlen, bufsize);
1451 OBD_ALLOC(buf, bufsize);
1453 CERROR("alloc mem failed\n");
1457 left = copy_from_user(buf, src, mlen);
1459 GOTO(err_free, err = -EFAULT);
1464 LASSERT(rpci->private);
1465 gsec = (struct gss_sec *)rpci->private;
1466 sec = &gsec->gs_base;
1467 LASSERT(sec->ps_import);
1468 import = class_import_get(sec->ps_import);
1469 LASSERT(import->imp_obd);
1470 obdname = import->imp_obd->obd_name;
1471 mech = gsec->gs_mech;
1473 err = gss_parse_init_downcall(mech, &obj, &ctx, &gmd, &gss_err);
1475 CERROR("parse init downcall err %d\n", err);
1477 vcred.vc_uid = gmd.gum_uid;
1478 vcred.vc_pag = vcred.vc_uid; /* FIXME */
1480 cred = ptlrpcs_cred_lookup(sec, &vcred);
1482 CWARN("didn't find cred for uid %u\n", vcred.vc_uid);
1483 GOTO(err, err = -EINVAL);
1486 if (err || gss_err) {
1487 cred->pc_flags |= PTLRPC_CRED_DEAD;
1488 if (err != -ERESTART || gss_err != 0)
1489 cred->pc_flags |= PTLRPC_CRED_ERROR;
1490 CERROR("cred %p: rpc err %d, gss err 0x%x, fatal %d\n",
1492 ((cred->pc_flags & PTLRPC_CRED_ERROR) != 0));
1494 CDEBUG(D_SEC, "get initial ctx:\n");
1495 gss_cred_set_ctx(cred, ctx);
1498 spin_lock(&gsec->gs_lock);
1499 gss_msg = gss_find_upcall(gsec, obdname, &gmd);
1501 gss_unhash_msg_nolock(gss_msg);
1502 spin_unlock(&gsec->gs_lock);
1503 gss_release_msg(gss_msg);
1505 spin_unlock(&gsec->gs_lock);
1507 ptlrpcs_cred_put(cred, 1);
1508 class_import_put(import);
1509 OBD_FREE(buf, bufsize);
1513 gss_destroy_ctx(ctx);
1514 class_import_put(import);
1516 OBD_FREE(buf, bufsize);
1517 CDEBUG(D_SEC, "gss_pipe_downcall returning %d\n", err);
1522 void gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
1524 struct gss_upcall_msg *gmsg;
1525 static unsigned long ratelimit;
1528 if (msg->errno >= 0) {
1533 gmsg = container_of(msg, struct gss_upcall_msg, gum_base);
1534 CDEBUG(D_SEC, "destroy gmsg %p\n", gmsg);
1535 atomic_inc(&gmsg->gum_refcount);
1536 gss_unhash_msg(gmsg);
1537 if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
1538 unsigned long now = get_seconds();
1539 if (time_after(now, ratelimit)) {
1540 CWARN("GSS_SEC upcall timed out.\n"
1541 "Please check user daemon is running!\n");
1542 ratelimit = now + 15;
1545 gss_release_msg(gmsg);
1550 void gss_pipe_release(struct inode *inode)
1552 struct rpc_inode *rpci = RPC_I(inode);
1553 struct ptlrpc_sec *sec;
1554 struct gss_sec *gsec;
1557 gsec = (struct gss_sec *)rpci->private;
1558 sec = &gsec->gs_base;
1559 spin_lock(&gsec->gs_lock);
1560 while (!list_empty(&gsec->gs_upcalls)) {
1561 struct gss_upcall_msg *gmsg;
1563 gmsg = list_entry(gsec->gs_upcalls.next,
1564 struct gss_upcall_msg, gum_list);
1565 gmsg->gum_base.errno = -EPIPE;
1566 atomic_inc(&gmsg->gum_refcount);
1567 gss_unhash_msg_nolock(gmsg);
1568 gss_release_msg(gmsg);
1570 spin_unlock(&gsec->gs_lock);
1574 static struct rpc_pipe_ops gss_upcall_ops = {
1575 .upcall = gss_pipe_upcall,
1576 .downcall = gss_pipe_downcall,
1577 .destroy_msg = gss_pipe_destroy_msg,
1578 .release_pipe = gss_pipe_release,
1580 #endif /* __KERNEL__ */
1582 /*********************************************
1583 * GSS security APIs *
1584 *********************************************/
1587 struct ptlrpc_sec* gss_create_sec(ptlrpcs_flavor_t *flavor,
1588 const char *pipe_dir,
1591 struct gss_sec *gsec;
1592 struct ptlrpc_sec *sec;
1599 LASSERT(flavor->flavor == PTLRPC_SEC_GSS);
1601 OBD_ALLOC(gsec, sizeof(*gsec));
1603 CERROR("can't alloc gsec\n");
1607 gsec->gs_mech = kgss_subflavor_to_mech(flavor->subflavor);
1608 if (!gsec->gs_mech) {
1609 CERROR("subflavor %d not found\n", flavor->subflavor);
1613 /* initialize gss sec */
1615 INIT_LIST_HEAD(&gsec->gs_upcalls);
1616 spin_lock_init(&gsec->gs_lock);
1618 pipepath_len = strlen(LUSTRE_PIPEDIR) + strlen(pipe_dir) +
1619 strlen(gsec->gs_mech->gm_name) + 3;
1620 OBD_ALLOC(gsec->gs_pipepath, pipepath_len);
1621 if (!gsec->gs_pipepath)
1624 sprintf(gsec->gs_pipepath, LUSTRE_PIPEDIR"/%s", pipe_dir);
1625 if (IS_ERR(rpc_mkdir(gsec->gs_pipepath, NULL))) {
1626 CERROR("can't make pipedir %s\n", gsec->gs_pipepath);
1630 sprintf(gsec->gs_pipepath, LUSTRE_PIPEDIR"/%s/%s", pipe_dir,
1631 gsec->gs_mech->gm_name);
1632 gsec->gs_depipe = rpc_mkpipe(gsec->gs_pipepath, gsec,
1633 &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
1634 if (IS_ERR(gsec->gs_depipe)) {
1635 CERROR("failed to make rpc_pipe %s: %ld\n",
1636 gsec->gs_pipepath, PTR_ERR(gsec->gs_depipe));
1639 CDEBUG(D_SEC, "gss sec %p, pipe path %s\n", gsec, gsec->gs_pipepath);
1642 sec = &gsec->gs_base;
1644 switch (flavor->subflavor) {
1645 case PTLRPC_SEC_GSS_KRB5I:
1646 sec->ps_sectype = PTLRPC_SEC_TYPE_AUTH;
1648 case PTLRPC_SEC_GSS_KRB5P:
1649 sec->ps_sectype = PTLRPC_SEC_TYPE_PRIV;
1655 sec->ps_expire = GSS_CREDCACHE_EXPIRE;
1656 sec->ps_nextgc = get_seconds() + sec->ps_expire;
1659 CDEBUG(D_SEC, "Create GSS security instance at %p(external %p)\n",
1665 pos = strrchr(gsec->gs_pipepath, '/');
1668 rpc_rmdir(gsec->gs_pipepath);
1670 OBD_FREE(gsec->gs_pipepath, pipepath_len);
1673 kgss_mech_put(gsec->gs_mech);
1675 OBD_FREE(gsec, sizeof(*gsec));
1680 void gss_destroy_sec(struct ptlrpc_sec *sec)
1682 struct gss_sec *gsec;
1689 gsec = container_of(sec, struct gss_sec, gs_base);
1690 CDEBUG(D_SEC, "Destroy GSS security instance at %p\n", gsec);
1692 LASSERT(gsec->gs_mech);
1693 LASSERT(!atomic_read(&sec->ps_refcount));
1694 LASSERT(!atomic_read(&sec->ps_credcount));
1696 pipepath_len = strlen(gsec->gs_pipepath) + 1;
1697 rpc_unlink(gsec->gs_pipepath);
1698 pos = strrchr(gsec->gs_pipepath, '/');
1701 rpc_rmdir(gsec->gs_pipepath);
1702 OBD_FREE(gsec->gs_pipepath, pipepath_len);
1705 kgss_mech_put(gsec->gs_mech);
1706 OBD_FREE(gsec, sizeof(*gsec));
1711 struct ptlrpc_cred * gss_create_cred(struct ptlrpc_sec *sec,
1712 struct vfs_cred *vcred)
1714 struct gss_cred *gcred;
1715 struct ptlrpc_cred *cred;
1718 OBD_ALLOC(gcred, sizeof(*gcred));
1722 cred = &gcred->gc_base;
1723 INIT_LIST_HEAD(&cred->pc_hash);
1724 atomic_set(&cred->pc_refcount, 0);
1726 cred->pc_ops = &gss_credops;
1727 cred->pc_expire = get_seconds() + GSS_CRED_EXPIRE;
1729 cred->pc_pag = vcred->vc_pag;
1730 cred->pc_uid = vcred->vc_uid;
1731 CDEBUG(D_SEC, "create a gss cred at %p("LPU64"/%u)\n",
1732 cred, vcred->vc_pag, vcred->vc_uid);
1737 static int gss_estimate_payload(struct ptlrpc_sec *sec, int msgsize)
1739 switch (sec->ps_sectype) {
1740 case PTLRPC_SEC_TYPE_AUTH:
1741 return GSS_MAX_AUTH_PAYLOAD;
1742 case PTLRPC_SEC_TYPE_PRIV:
1743 return size_round16(GSS_MAX_AUTH_PAYLOAD + msgsize +
1744 GSS_PRIVBUF_PREFIX_LEN +
1745 GSS_PRIVBUF_SUFFIX_LEN);
1752 static int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
1753 struct ptlrpc_request *req,
1756 int msg_payload, sec_payload;
1760 /* In PRIVACY mode, lustre message is always 0 (already encoded into
1761 * security payload).
1763 privacy = sec->ps_sectype == PTLRPC_SEC_TYPE_PRIV;
1764 msg_payload = privacy ? 0 : lmsg_size;
1765 sec_payload = gss_estimate_payload(sec, lmsg_size);
1767 rc = sec_alloc_reqbuf(sec, req, msg_payload, sec_payload);
1772 int buflen = lmsg_size + GSS_PRIVBUF_PREFIX_LEN +
1773 GSS_PRIVBUF_SUFFIX_LEN;
1776 OBD_ALLOC(buf, buflen);
1778 CERROR("Fail to alloc %d\n", buflen);
1779 sec_free_reqbuf(sec, req);
1782 req->rq_reqmsg = (struct lustre_msg *)
1783 (buf + GSS_PRIVBUF_PREFIX_LEN);
1789 static void gss_free_reqbuf(struct ptlrpc_sec *sec,
1790 struct ptlrpc_request *req)
1796 LASSERT(req->rq_reqmsg);
1797 LASSERT(req->rq_reqlen);
1799 privacy = sec->ps_sectype == PTLRPC_SEC_TYPE_PRIV;
1801 buf = (char *) req->rq_reqmsg - GSS_PRIVBUF_PREFIX_LEN;
1802 LASSERT(buf < req->rq_reqbuf ||
1803 buf >= req->rq_reqbuf + req->rq_reqbuf_len);
1804 OBD_FREE(buf, req->rq_reqlen + GSS_PRIVBUF_PREFIX_LEN +
1805 GSS_PRIVBUF_SUFFIX_LEN);
1806 req->rq_reqmsg = NULL;
1809 sec_free_reqbuf(sec, req);
1812 static struct ptlrpc_secops gss_secops = {
1813 .create_sec = gss_create_sec,
1814 .destroy_sec = gss_destroy_sec,
1815 .create_cred = gss_create_cred,
1816 .est_req_payload = gss_estimate_payload,
1817 .est_rep_payload = gss_estimate_payload,
1818 .alloc_reqbuf = gss_alloc_reqbuf,
1819 .free_reqbuf = gss_free_reqbuf,
1822 static struct ptlrpc_sec_type gss_type = {
1823 .pst_owner = THIS_MODULE,
1824 .pst_name = "GSS_SEC",
1825 .pst_inst = ATOMIC_INIT(0),
1826 .pst_flavor = {PTLRPC_SEC_GSS, 0},
1827 .pst_ops = &gss_secops,
1831 (*lustre_secinit_downcall_handler)(char *buffer, unsigned long count);
1833 int __init ptlrpcs_gss_init(void)
1837 rc = ptlrpcs_register(&gss_type);
1844 rc = PTR_ERR(rpc_mkdir(LUSTRE_PIPEDIR, NULL));
1845 if (IS_ERR((void *)rc) && rc != -EEXIST) {
1846 CERROR("fail to make rpcpipedir for lustre\n");
1848 ptlrpcs_unregister(&gss_type);
1854 rc = init_kerberos_module();
1856 ptlrpcs_unregister(&gss_type);
1859 lustre_secinit_downcall_handler = gss_send_secinit_rpc;
1865 static void __exit ptlrpcs_gss_exit(void)
1867 lustre_secinit_downcall_handler = NULL;
1869 cleanup_kerberos_module();
1870 rpc_rmdir(LUSTRE_PIPEDIR);
1872 ptlrpcs_unregister(&gss_type);
1876 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1877 MODULE_DESCRIPTION("GSS Security module for Lustre");
1878 MODULE_LICENSE("GPL");
1880 module_init(ptlrpcs_gss_init);
1881 module_exit(ptlrpcs_gss_exit);