Whamcloud - gitweb
b395f918daf57070e9506773463f9d4bd53e5ee3
[fs/lustre-release.git] / lustre / sec / gss / sec_gss.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  * Copyright 2004, Cluster File Systems, Inc.
6  * All rights reserved
7  * Author: Eric Mei <ericm@clusterfs.com>
8  */
9
10 /*
11  * linux/net/sunrpc/auth_gss.c
12  *
13  * RPCSEC_GSS client authentication.
14  *
15  *  Copyright (c) 2000 The Regents of the University of Michigan.
16  *  All rights reserved.
17  *
18  *  Dug Song       <dugsong@monkey.org>
19  *  Andy Adamson   <andros@umich.edu>
20  *
21  *  Redistribution and use in source and binary forms, with or without
22  *  modification, are permitted provided that the following conditions
23  *  are met:
24  *
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.
33  *
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.
45  *
46  */
47
48 #ifndef EXPORT_SYMTAB
49 # define EXPORT_SYMTAB
50 #endif
51 #define DEBUG_SUBSYSTEM S_SEC
52 #ifdef __KERNEL__
53 #include <linux/init.h>
54 #include <linux/module.h>
55 #include <linux/slab.h>
56 #include <linux/dcache.h>
57 #include <linux/fs.h>
58 #include <linux/random.h>
59 /* for rpc_pipefs */
60 struct rpc_clnt;
61 #include <linux/sunrpc/rpc_pipe_fs.h>
62 #else
63 #include <liblustre.h>
64 #endif
65
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>
74
75 #include "gss_err.h"
76 #include "gss_internal.h"
77 #include "gss_api.h"
78
79 #define LUSTRE_PIPEDIR          "/lustre"
80
81 #define GSS_CREDCACHE_EXPIRE    (30 * 60)          /* 30 minute */
82
83 #define GSS_TIMEOUT_DELTA       (5)
84 #define CRED_REFRESH_UPCALL_TIMEOUT                             \
85         ({                                                      \
86                 int timeout = obd_timeout - GSS_TIMEOUT_DELTA;  \
87                                                                 \
88                 if (timeout < GSS_TIMEOUT_DELTA * 2)            \
89                         timeout = GSS_TIMEOUT_DELTA * 2;        \
90                 timeout;                                        \
91         })
92 #define SECINIT_RPC_TIMEOUT                                     \
93         ({                                                      \
94                 int timeout = CRED_REFRESH_UPCALL_TIMEOUT -     \
95                               GSS_TIMEOUT_DELTA;                \
96                 if (timeout < GSS_TIMEOUT_DELTA)                \
97                         timeout = GSS_TIMEOUT_DELTA;            \
98                 timeout;                                        \
99         })
100 #define SECFINI_RPC_TIMEOUT     (GSS_TIMEOUT_DELTA)
101
102
103 /**********************************************
104  * gss security init/fini helper              *
105  **********************************************/
106
107 static int secinit_compose_request(struct obd_import *imp,
108                                    char *buf, int bufsize,
109                                    int lustre_srv,
110                                    uid_t uid, gid_t gid,
111                                    long token_size,
112                                    char __user *token)
113 {
114         struct ptlrpcs_wire_hdr *hdr;
115         struct lustre_msg       *lmsg;
116         struct mds_req_sec_desc *secdesc;
117         int                      size = sizeof(*secdesc);
118         __u32                    lmsg_size, *p;
119         int                      rc;
120
121         lmsg_size = lustre_msg_size(1, &size);
122
123         if (sizeof(*hdr) + lmsg_size + size_round(token_size) > bufsize) {
124                 CERROR("token size %ld too large\n", token_size);
125                 return -EINVAL;
126         }
127
128         /* security wire hdr */
129         hdr = buf_to_sec_hdr(buf);
130         hdr->flavor  = cpu_to_le32(PTLRPCS_FLVR_GSS_NONE);
131         hdr->msg_len = cpu_to_le32(lmsg_size);
132         hdr->sec_len = cpu_to_le32(8 * 4 + token_size);
133
134         /* lustre message & secdesc */
135         lmsg = buf_to_lustre_msg(buf);
136
137         lustre_init_msg(lmsg, 1, &size, NULL);
138         secdesc = lustre_msg_buf(lmsg, 0, size);
139         secdesc->rsd_uid = secdesc->rsd_fsuid = uid;
140         secdesc->rsd_gid = secdesc->rsd_fsgid = gid;
141         secdesc->rsd_cap = secdesc->rsd_ngroups = 0;
142
143         lmsg->handle   = imp->imp_remote_handle;
144         lmsg->type     = PTL_RPC_MSG_REQUEST;
145         lmsg->opc      = SEC_INIT;
146         lmsg->flags    = 0;
147         lmsg->conn_cnt = imp->imp_conn_cnt;
148
149         p = (__u32 *) (buf + sizeof(*hdr) + lmsg_size);
150
151         /* gss hdr */
152         *p++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION);     /* gss version */
153         *p++ = cpu_to_le32(PTLRPCS_FLVR_KRB5I);         /* subflavor */
154         *p++ = cpu_to_le32(PTLRPCS_GSS_PROC_INIT);      /* proc */
155         *p++ = cpu_to_le32(0);                          /* seq */
156         *p++ = cpu_to_le32(PTLRPCS_GSS_SVC_NONE);       /* service */
157         *p++ = cpu_to_le32(0);                          /* context handle */
158
159         /* plus lustre svc type */
160         *p++ = cpu_to_le32(lustre_srv);
161
162         /* now the token part */
163         *p++ = cpu_to_le32((__u32) token_size);
164         LASSERT(((char *)p - buf) + token_size <= bufsize);
165
166         rc = copy_from_user(p, token, token_size);
167         if (rc) {
168                 CERROR("can't copy token\n");
169                 return -EFAULT;
170         }
171
172         rc = size_round(((char *)p - buf) + token_size);
173         return rc;
174 }
175
176 static int secinit_parse_reply(char *repbuf, int replen,
177                                char __user *outbuf, long outlen)
178 {
179         __u32                   *p = (__u32 *)repbuf;
180         struct ptlrpcs_wire_hdr *hdr = (struct ptlrpcs_wire_hdr *) repbuf;
181         __u32                    lmsg_len, sec_len, status;
182         __u32                    major, minor, seq, obj_len, round_len;
183         __u32                    effective = 0;
184
185         if (replen <= (4 + 6) * 4) {
186                 CERROR("reply size %d too small\n", replen);
187                 return -EINVAL;
188         }
189
190         hdr->flavor = le32_to_cpu(hdr->flavor);
191         hdr->msg_len = le32_to_cpu(hdr->msg_len);
192         hdr->sec_len = le32_to_cpu(hdr->sec_len);
193
194         lmsg_len = le32_to_cpu(p[2]);
195         sec_len = le32_to_cpu(p[3]);
196
197         /* sanity checks */
198         if (hdr->flavor != PTLRPCS_FLVR_GSS_NONE) {
199                 CERROR("unexpected reply\n");
200                 return -EINVAL;
201         }
202         if (hdr->msg_len % 8 ||
203             sizeof(*hdr) + hdr->msg_len + hdr->sec_len > replen) {
204                 CERROR("unexpected reply\n");
205                 return -EINVAL;
206         }
207         if (hdr->sec_len > outlen) {
208                 CERROR("outbuf too small\n");
209                 return -EINVAL;
210         }
211
212         p = (__u32 *) buf_to_sec_data(repbuf);
213         effective = 0;
214
215         status = le32_to_cpu(*p++);
216         major = le32_to_cpu(*p++);
217         minor = le32_to_cpu(*p++);
218         seq = le32_to_cpu(*p++);
219         effective += 4 * 4;
220
221         if (copy_to_user(outbuf, &status, 4))
222                 return -EFAULT;
223         outbuf += 4;
224         if (copy_to_user(outbuf, &major, 4))
225                 return -EFAULT;
226         outbuf += 4;
227         if (copy_to_user(outbuf, &minor, 4))
228                 return -EFAULT;
229         outbuf += 4;
230         if (copy_to_user(outbuf, &seq, 4))
231                 return -EFAULT;
232         outbuf += 4;
233
234         obj_len = le32_to_cpu(*p++);
235         round_len = (obj_len + 3) & ~ 3;
236         if (copy_to_user(outbuf, &obj_len, 4))
237                 return -EFAULT;
238         outbuf += 4;
239         if (copy_to_user(outbuf, (char *)p, round_len))
240                 return -EFAULT;
241         p += round_len / 4;
242         outbuf += round_len;
243         effective += 4 + round_len;
244
245         obj_len = le32_to_cpu(*p++);
246         round_len = (obj_len + 3) & ~ 3;
247         if (copy_to_user(outbuf, &obj_len, 4))
248                 return -EFAULT;
249         outbuf += 4;
250         if (copy_to_user(outbuf, (char *)p, round_len))
251                 return -EFAULT;
252         p += round_len / 4;
253         outbuf += round_len;
254         effective += 4 + round_len;
255
256         return effective;
257 }
258
259 /* XXX move to where lgssd could see */
260 struct lgssd_ioctl_param {
261         int             version;        /* in   */
262         char           *uuid;           /* in   */
263         int             lustre_svc;     /* in   */
264         uid_t           uid;            /* in   */
265         gid_t           gid;            /* in   */
266         long            send_token_size;/* in   */
267         char           *send_token;     /* in   */
268         long            reply_buf_size; /* in   */
269         char           *reply_buf;      /* in   */
270         long            status;         /* out  */
271         long            reply_length;   /* out  */
272 };
273
274 static int gss_send_secinit_rpc(__user char *buffer, unsigned long count)
275 {
276         struct obd_import        *imp;
277         struct ptlrpc_request    *request = NULL;
278         struct lgssd_ioctl_param  param;
279         const int                 reqbuf_size = 1024;
280         const int                 repbuf_size = 1024;
281         char                     *reqbuf, *repbuf;
282         struct obd_device        *obd;
283         char                      obdname[64];
284         long                      lsize;
285         int                       rc, reqlen, replen;
286
287         if (count != sizeof(param)) {
288                 CERROR("ioctl size %lu, expect %d, please check lgssd version\n",
289                         count, sizeof(param));
290                 RETURN(-EINVAL);
291         }
292         if (copy_from_user(&param, buffer, sizeof(param))) {
293                 CERROR("failed copy data from lgssd\n");
294                 RETURN(-EFAULT);
295         }
296
297         if (param.version != GSSD_INTERFACE_VERSION) {
298                 CERROR("gssd interface version %d (expect %d)\n",
299                         param.version, GSSD_INTERFACE_VERSION);
300                 RETURN(-EINVAL);
301         }
302
303         /* take name */
304         if (strncpy_from_user(obdname, param.uuid,
305                               sizeof(obdname)) <= 0) {
306                 CERROR("Invalid obdname pointer\n");
307                 RETURN(-EFAULT);
308         }
309
310         obd = class_name2obd(obdname);
311         if (!obd) {
312                 CERROR("no such obd %s\n", obdname);
313                 RETURN(-EINVAL);
314         }
315
316         imp = class_import_get(obd->u.cli.cl_import);
317
318         OBD_ALLOC(reqbuf, reqbuf_size);
319         OBD_ALLOC(repbuf, reqbuf_size);
320
321         if (!reqbuf || !repbuf) {
322                 CERROR("Can't alloc buffer: %p/%p\n", reqbuf, repbuf);
323                 param.status = -ENOMEM;
324                 goto out_copy;
325         }
326
327         /* get token */
328         reqlen = secinit_compose_request(imp, reqbuf, reqbuf_size,
329                                          param.lustre_svc,
330                                          param.uid, param.gid,
331                                          param.send_token_size,
332                                          param.send_token);
333         if (reqlen < 0) {
334                 param.status = reqlen;
335                 goto out_copy;
336         }
337
338         request = ptl_do_rawrpc(imp, reqbuf, reqbuf_size, reqlen,
339                                 repbuf, repbuf_size, &replen,
340                                 SECINIT_RPC_TIMEOUT, &rc);
341         if (request == NULL || rc) {
342                 param.status = rc;
343                 goto out_copy;
344         }
345
346         if (replen > param.reply_buf_size) {
347                 CERROR("output buffer size %ld too small, need %d\n",
348                         param.reply_buf_size, replen);
349                 param.status = -EINVAL;
350                 goto out_copy;
351         }
352
353         lsize = secinit_parse_reply(repbuf, replen,
354                                     param.reply_buf, param.reply_buf_size);
355         if (lsize < 0) {
356                 param.status = (int) lsize;
357                 goto out_copy;
358         }
359
360         param.status = 0;
361         param.reply_length = lsize;
362
363 out_copy:
364         if (copy_to_user(buffer, &param, sizeof(param)))
365                 rc = -EFAULT;
366         else
367                 rc = 0;
368
369         class_import_put(imp);
370         if (request == NULL) {
371                 if (repbuf)
372                         OBD_FREE(repbuf, repbuf_size);
373                 if (reqbuf)
374                         OBD_FREE(reqbuf, reqbuf_size);
375         } else {
376                 rawrpc_req_finished(request);
377         }
378         RETURN(rc);
379 }
380
381 /**********************************************
382  * structure definitions                      *
383  **********************************************/
384 struct gss_sec {
385         struct ptlrpc_sec       gs_base;
386         struct gss_api_mech    *gs_mech;
387 #ifdef __KERNEL__
388         spinlock_t              gs_lock;
389         struct list_head        gs_upcalls;
390         char                   *gs_pipepath;
391         struct dentry          *gs_depipe;
392 #endif
393 };
394
395 #ifdef __KERNEL__
396
397 static rwlock_t gss_ctx_lock = RW_LOCK_UNLOCKED;
398
399 struct gss_upcall_msg_data {
400         __u32                           gum_uid;
401         __u32                           gum_svc;
402         __u32                           gum_nal;
403         __u32                           gum_netid;
404         __u64                           gum_nid;
405 };
406
407 struct gss_upcall_msg {
408         struct rpc_pipe_msg             gum_base;
409         atomic_t                        gum_refcount;
410         struct list_head                gum_list;
411         struct gss_sec                 *gum_gsec;
412         wait_queue_head_t               gum_waitq;
413         char                            gum_obdname[64];
414         struct gss_upcall_msg_data      gum_data;
415 };
416
417 /**********************************************
418  * rpc_pipe upcall helpers                    *
419  **********************************************/
420 static
421 void gss_release_msg(struct gss_upcall_msg *gmsg)
422 {
423         ENTRY;
424         LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
425
426         if (!atomic_dec_and_test(&gmsg->gum_refcount)) {
427                 CDEBUG(D_SEC, "gmsg %p ref %d\n", gmsg,
428                        atomic_read(&gmsg->gum_refcount));
429                 EXIT;
430                 return;
431         }
432         LASSERT(list_empty(&gmsg->gum_list));
433 #if 0
434         LASSERT(list_empty(&gmsg->gum_base.list));
435 #else
436         /* XXX */
437         if (!list_empty(&gmsg->gum_base.list)) {
438                 CWARN("msg %p: list: %p/%p/%p, copied %d, err %d, wq %d\n",
439                       gmsg, &gmsg->gum_base.list,
440                       gmsg->gum_base.list.prev, gmsg->gum_base.list.next,
441                       gmsg->gum_base.copied, gmsg->gum_base.errno,
442                       list_empty(&gmsg->gum_waitq.task_list));
443                 LBUG();
444         }
445 #endif
446         OBD_FREE(gmsg, sizeof(*gmsg));
447         EXIT;
448 }
449
450 static void
451 gss_unhash_msg_nolock(struct gss_upcall_msg *gmsg)
452 {
453 #if defined(CONFIG_SMP)
454         LASSERT(spin_is_locked(&gmsg->gum_gsec->gs_lock));
455 #endif
456
457         if (list_empty(&gmsg->gum_list))
458                 return;
459
460         list_del_init(&gmsg->gum_list);
461         wake_up(&gmsg->gum_waitq);
462         LASSERT(atomic_read(&gmsg->gum_refcount) > 1);
463         atomic_dec(&gmsg->gum_refcount);
464 }
465
466 static void
467 gss_unhash_msg(struct gss_upcall_msg *gmsg)
468 {
469         struct gss_sec *gsec = gmsg->gum_gsec;
470
471         spin_lock(&gsec->gs_lock);
472         gss_unhash_msg_nolock(gmsg);
473         spin_unlock(&gsec->gs_lock);
474 }
475
476 static
477 struct gss_upcall_msg * gss_find_upcall(struct gss_sec *gsec,
478                                         char *obdname,
479                                         struct gss_upcall_msg_data *gmd)
480 {
481         struct gss_upcall_msg *gmsg;
482         ENTRY;
483
484 #if defined(CONFIG_SMP)
485         LASSERT(spin_is_locked(&gsec->gs_lock));
486 #endif
487
488         list_for_each_entry(gmsg, &gsec->gs_upcalls, gum_list) {
489                 if (memcmp(&gmsg->gum_data, gmd, sizeof(*gmd)))
490                         continue;
491                 if (strcmp(gmsg->gum_obdname, obdname))
492                         continue;
493                 LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
494                 atomic_inc(&gmsg->gum_refcount);
495                 CDEBUG(D_SEC, "found gmsg at %p: obdname %s, uid %d, ref %d\n",
496                        gmsg, obdname, gmd->gum_uid,
497                        atomic_read(&gmsg->gum_refcount));
498                 RETURN(gmsg);
499         }
500         RETURN(NULL);
501 }
502
503 static void gss_init_upcall_msg(struct gss_upcall_msg *gmsg,
504                                 struct gss_sec *gsec, char *obdname,
505                                 struct gss_upcall_msg_data *gmd)
506 {
507         struct rpc_pipe_msg *rpcmsg;
508         ENTRY;
509
510         /* 2 refs: 1 for hash, 1 for current user */
511         init_waitqueue_head(&gmsg->gum_waitq);
512         list_add(&gmsg->gum_list, &gsec->gs_upcalls);
513         atomic_set(&gmsg->gum_refcount, 2);
514         gmsg->gum_gsec = gsec;
515         strncpy(gmsg->gum_obdname, obdname, sizeof(gmsg->gum_obdname));
516         memcpy(&gmsg->gum_data, gmd, sizeof(*gmd));
517
518         rpcmsg = &gmsg->gum_base;
519         INIT_LIST_HEAD(&rpcmsg->list);
520         rpcmsg->data = &gmsg->gum_data;
521         rpcmsg->len = sizeof(gmsg->gum_data);
522         rpcmsg->copied = 0;
523         rpcmsg->errno = 0;
524         EXIT;
525 }
526 #endif /* __KERNEL__ */
527
528 /********************************************
529  * gss cred manipulation helpers            *
530  ********************************************/
531 #if 0
532 static
533 int gss_cred_is_uptodate_ctx(struct ptlrpc_cred *cred)
534 {
535         struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
536         int res = 0;
537
538         read_lock(&gss_ctx_lock);
539         if (((cred->pc_flags & PTLRPC_CRED_FLAGS_MASK) ==
540              PTLRPC_CRED_UPTODATE) &&
541             gcred->gc_ctx)
542                 res = 1;
543         read_unlock(&gss_ctx_lock);
544         return res;
545 }
546 #endif
547
548 static inline
549 struct gss_cl_ctx * gss_get_ctx(struct gss_cl_ctx *ctx)
550 {
551         atomic_inc(&ctx->gc_refcount);
552         return ctx;
553 }
554
555 static
556 void gss_destroy_ctx(struct gss_cl_ctx *ctx)
557 {
558         ENTRY;
559
560         CDEBUG(D_SEC, "destroy cl_ctx %p\n", ctx);
561         if (ctx->gc_gss_ctx)
562                 kgss_delete_sec_context(&ctx->gc_gss_ctx);
563
564         if (ctx->gc_wire_ctx.len > 0) {
565                 OBD_FREE(ctx->gc_wire_ctx.data, ctx->gc_wire_ctx.len);
566                 ctx->gc_wire_ctx.len = 0;
567         }
568
569         OBD_FREE(ctx, sizeof(*ctx));
570 }
571
572 static
573 void gss_put_ctx(struct gss_cl_ctx *ctx)
574 {
575         if (atomic_dec_and_test(&ctx->gc_refcount))
576                 gss_destroy_ctx(ctx);
577 }
578
579 static
580 struct gss_cl_ctx *gss_cred_get_ctx(struct ptlrpc_cred *cred)
581 {
582         struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
583         struct gss_cl_ctx *ctx = NULL;
584
585         read_lock(&gss_ctx_lock);
586         if (gcred->gc_ctx)
587                 ctx = gss_get_ctx(gcred->gc_ctx);
588         read_unlock(&gss_ctx_lock);
589         return ctx;
590 }
591
592 static
593 void gss_cred_set_ctx(struct ptlrpc_cred *cred, struct gss_cl_ctx *ctx)
594 {
595         struct gss_cred *gcred = container_of(cred, struct gss_cred, gc_base);
596         struct gss_cl_ctx *old;
597         __u64 ctx_expiry;
598         ENTRY;
599
600         if (kgss_inquire_context(ctx->gc_gss_ctx, &ctx_expiry)) {
601                 CERROR("unable to get expire time\n");
602                 ctx_expiry = 1; /* make it expired now */
603         }
604         cred->pc_expire = (unsigned long) ctx_expiry;
605
606         write_lock(&gss_ctx_lock);
607         old = gcred->gc_ctx;
608         gcred->gc_ctx = ctx;
609         set_bit(PTLRPC_CRED_UPTODATE_BIT, &cred->pc_flags);
610         write_unlock(&gss_ctx_lock);
611         if (old)
612                 gss_put_ctx(old);
613
614         CDEBUG(D_SEC, "client refreshed gss cred %p(uid %u)\n",
615                cred, cred->pc_uid);
616         EXIT;
617 }
618
619 static int
620 simple_get_bytes(char **buf, __u32 *buflen, void *res, __u32 reslen)
621 {
622         if (*buflen < reslen) {
623                 CERROR("buflen %u < %u\n", *buflen, reslen);
624                 return -EINVAL;
625         }
626
627         memcpy(res, *buf, reslen);
628         *buf += reslen;
629         *buflen -= reslen;
630         return 0;
631 }
632
633 /* data passed down:
634  *  - uid
635  *  - timeout
636  *  - gc_win / error
637  *  - wire_ctx (rawobj)
638  *  - mech_ctx? (rawobj)
639  */
640 static
641 int gss_parse_init_downcall(struct gss_api_mech *gm, rawobj_t *buf,
642                             struct gss_cl_ctx **gc,
643                             struct gss_upcall_msg_data *gmd, int *gss_err)
644 {
645         char *p = (char *)buf->data;
646         struct gss_cl_ctx *ctx;
647         __u32 len = buf->len;
648         unsigned int timeout;
649         rawobj_t tmp_buf;
650         int err = -EPERM;
651         ENTRY;
652
653         *gc = NULL;
654         *gss_err = 0;
655
656         OBD_ALLOC(ctx, sizeof(*ctx));
657         if (!ctx)
658                 RETURN(-ENOMEM);
659
660         ctx->gc_proc = RPC_GSS_PROC_DATA;
661         ctx->gc_seq = 0;
662         spin_lock_init(&ctx->gc_seq_lock);
663         atomic_set(&ctx->gc_refcount,1);
664
665         if (simple_get_bytes(&p, &len, &gmd->gum_uid, sizeof(gmd->gum_uid)))
666                 goto err_free_ctx;
667         if (simple_get_bytes(&p, &len, &gmd->gum_svc, sizeof(gmd->gum_svc)))
668                 goto err_free_ctx;
669         if (simple_get_bytes(&p, &len, &gmd->gum_nal, sizeof(gmd->gum_nal)))
670                 goto err_free_ctx;
671         if (simple_get_bytes(&p, &len, &gmd->gum_netid, sizeof(gmd->gum_netid)))
672                 goto err_free_ctx;
673         if (simple_get_bytes(&p, &len, &gmd->gum_nid, sizeof(gmd->gum_nid)))
674                 goto err_free_ctx;
675         /* FIXME: discarded timeout for now */
676         if (simple_get_bytes(&p, &len, &timeout, sizeof(timeout)))
677                 goto err_free_ctx;
678         if (simple_get_bytes(&p, &len, &ctx->gc_win, sizeof(ctx->gc_win)))
679                 goto err_free_ctx;
680
681         /* lgssd signals an error by passing ctx->gc_win = 0: */
682         if (!ctx->gc_win) {
683                 /* in which case the next 2 int are:
684                  * - rpc error
685                  * - gss error
686                  */
687                 if (simple_get_bytes(&p, &len, &err, sizeof(err))) {
688                         err = -EPERM;
689                         goto err_free_ctx;
690                 }
691                 if (simple_get_bytes(&p, &len, gss_err, sizeof(*gss_err))) {
692                         err = -EPERM;
693                         goto err_free_ctx;
694                 }
695                 if (err == 0 && *gss_err == 0) {
696                         CERROR("no error passed from downcall\n");
697                         err = -EPERM;
698                 }
699                 goto err_free_ctx;
700         }
701
702         if (rawobj_extract_local(&tmp_buf, (__u32 **) ((void *)&p), &len))
703                 goto err_free_ctx;
704         if (rawobj_dup(&ctx->gc_wire_ctx, &tmp_buf)) {
705                 err = -ENOMEM;
706                 goto err_free_ctx;
707         }
708         if (rawobj_extract_local(&tmp_buf, (__u32 **) ((void *)&p), &len))
709                 goto err_free_wire_ctx;
710         if (len) {
711                 CERROR("unexpected trailing %u bytes\n", len);
712                 goto err_free_wire_ctx;
713         }
714         if (kgss_import_sec_context(&tmp_buf, gm, &ctx->gc_gss_ctx))
715                 goto err_free_wire_ctx;
716
717         *gc = ctx;
718         RETURN(0);
719
720 err_free_wire_ctx:
721         if (ctx->gc_wire_ctx.data)
722                 OBD_FREE(ctx->gc_wire_ctx.data, ctx->gc_wire_ctx.len);
723 err_free_ctx:
724         OBD_FREE(ctx, sizeof(*ctx));
725         CDEBUG(D_SEC, "err_code %d, gss code %d\n", err, *gss_err);
726         return err;
727 }
728
729 /***************************************
730  * cred APIs                           *
731  ***************************************/
732 #ifdef __KERNEL__
733 static int gss_cred_refresh(struct ptlrpc_cred *cred)
734 {
735         struct obd_import          *import;
736         struct gss_sec             *gsec;
737         struct gss_upcall_msg      *gss_msg, *gss_new;
738         struct gss_upcall_msg_data  gmd;
739         struct dentry              *dentry;
740         char                       *obdname, *obdtype;
741         wait_queue_t                wait;
742         uid_t                       uid = cred->pc_uid;
743         int                         res;
744         ENTRY;
745
746         might_sleep();
747
748         /* any flags means it has been handled, do nothing */
749         if (cred->pc_flags & PTLRPC_CRED_FLAGS_MASK)
750                 RETURN(0);
751
752         LASSERT(cred->pc_sec);
753         LASSERT(cred->pc_sec->ps_import);
754         LASSERT(cred->pc_sec->ps_import->imp_obd);
755
756         import = cred->pc_sec->ps_import;
757         if (!import->imp_connection) {
758                 CERROR("import has no connection set\n");
759                 RETURN(-EINVAL);
760         }
761
762         gmd.gum_uid = uid;
763         gmd.gum_nal = import->imp_connection->c_peer.peer_ni->pni_number;
764         gmd.gum_netid = 0;
765         gmd.gum_nid = import->imp_connection->c_peer.peer_id.nid;
766
767         obdtype = import->imp_obd->obd_type->typ_name;
768         if (!strcmp(obdtype, "mdc"))
769                 gmd.gum_svc = LUSTRE_GSS_SVC_MDS;
770         else if (!strcmp(obdtype, "osc"))
771                 gmd.gum_svc = LUSTRE_GSS_SVC_OSS;
772         else {
773                 CERROR("gss on %s?\n", obdtype);
774                 RETURN(-EINVAL);
775         }
776
777         gsec = container_of(cred->pc_sec, struct gss_sec, gs_base);
778         obdname = import->imp_obd->obd_name;
779         dentry = gsec->gs_depipe;
780         gss_new = NULL;
781         res = 0;
782
783         CDEBUG(D_SEC, "Initiate gss context %p(%u@%s)\n",
784                container_of(cred, struct gss_cred, gc_base),
785                uid, import->imp_target_uuid.uuid);
786
787 again:
788         spin_lock(&gsec->gs_lock);
789         gss_msg = gss_find_upcall(gsec, obdname, &gmd);
790         if (gss_msg) {
791                 if (gss_new) {
792                         OBD_FREE(gss_new, sizeof(*gss_new));
793                         gss_new = NULL;
794                 }
795                 GOTO(waiting, res);
796         }
797
798         if (!gss_new) {
799                 spin_unlock(&gsec->gs_lock);
800                 OBD_ALLOC(gss_new, sizeof(*gss_new));
801                 if (!gss_new)
802                         RETURN(-ENOMEM);
803                 goto again;
804         }
805         /* so far we'v created gss_new */
806         gss_init_upcall_msg(gss_new, gsec, obdname, &gmd);
807
808         /* we'v created upcall msg, nobody else should touch the
809          * flag of this cred, unless be set as dead/expire by
810          * administrator via lctl etc.
811          */
812         if (cred->pc_flags & PTLRPC_CRED_FLAGS_MASK) {
813                 CWARN("cred %p("LPU64"/%u) was set flags %lx unexpectedly\n",
814                       cred, cred->pc_pag, cred->pc_uid, cred->pc_flags);
815                 cred->pc_flags |= PTLRPC_CRED_DEAD | PTLRPC_CRED_ERROR;
816                 gss_unhash_msg_nolock(gss_new);
817                 spin_unlock(&gsec->gs_lock);
818                 gss_release_msg(gss_new);
819                 RETURN(0);
820         }
821
822         /* need to make upcall now */
823         spin_unlock(&gsec->gs_lock);
824         res = rpc_queue_upcall(dentry->d_inode, &gss_new->gum_base);
825         if (res) {
826                 CERROR("rpc_queue_upcall failed: %d\n", res);
827                 gss_unhash_msg(gss_new);
828                 gss_release_msg(gss_new);
829                 cred->pc_flags |= PTLRPC_CRED_DEAD | PTLRPC_CRED_ERROR;
830                 RETURN(res);
831         }
832         gss_msg = gss_new;
833         spin_lock(&gsec->gs_lock);
834
835 waiting:
836         /* upcall might finish quickly */
837         if (list_empty(&gss_msg->gum_list)) {
838                 spin_unlock(&gsec->gs_lock);
839                 res = 0;
840                 goto out;
841         }
842
843         init_waitqueue_entry(&wait, current);
844         set_current_state(TASK_INTERRUPTIBLE);
845         add_wait_queue(&gss_msg->gum_waitq, &wait);
846         spin_unlock(&gsec->gs_lock);
847
848         if (gss_new)
849                 res = schedule_timeout(CRED_REFRESH_UPCALL_TIMEOUT * HZ);
850         else {
851                 schedule();
852                 res = 0;
853         }
854
855         remove_wait_queue(&gss_msg->gum_waitq, &wait);
856
857         /* - the one who refresh the cred for us should also be responsible
858          *   to set the status of cred, we can simply return.
859          * - if cred flags has been set, we also don't need to do that again,
860          *   no matter signal pending or timeout etc.
861          */
862         if (!gss_new || cred->pc_flags & PTLRPC_CRED_FLAGS_MASK)
863                 goto out;
864
865         if (signal_pending(current)) {
866                 CERROR("%s: cred %p: interrupted upcall\n",
867                        current->comm, cred);
868                 cred->pc_flags |= PTLRPC_CRED_DEAD | PTLRPC_CRED_ERROR;
869                 res = -EINTR;
870         } else if (res == 0) {
871                 CERROR("cred %p: upcall timedout\n", cred);
872                 set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags);
873                 res = -ETIMEDOUT;
874         } else
875                 res = 0;
876
877 out:
878         gss_release_msg(gss_msg);
879
880         RETURN(res);
881 }
882 #else /* !__KERNEL__ */
883 extern int lgss_handle_krb5_upcall(uid_t uid, __u32 dest_ip,
884                                    char *obd_name,
885                                    char *buf, int bufsize,
886                                    int (*callback)(char*, unsigned long));
887
888 static int gss_cred_refresh(struct ptlrpc_cred *cred)
889 {
890         char                    buf[4096];
891         rawobj_t                obj;
892         struct obd_import      *imp;
893         struct gss_sec         *gsec;
894         struct gss_api_mech    *mech;
895         struct gss_cl_ctx      *ctx = NULL;
896         struct vfs_cred         vcred = { 0 };
897         ptl_nid_t               peer_nid;
898         __u32                   dest_ip;
899         __u32                   subflavor;
900         int                     rc, gss_err;
901
902         LASSERT(cred);
903         LASSERT(cred->pc_sec);
904         LASSERT(cred->pc_sec->ps_import);
905         LASSERT(cred->pc_sec->ps_import->imp_obd);
906
907         if (ptlrpcs_cred_is_uptodate(cred))
908                 RETURN(0);
909
910         imp = cred->pc_sec->ps_import;
911         peer_nid = imp->imp_connection->c_peer.peer_id.nid;
912         dest_ip = (__u32) (peer_nid & 0xFFFFFFFF);
913         subflavor = cred->pc_sec->ps_flavor.subflavor;
914
915         if (subflavor != PTLRPC_SEC_GSS_KRB5I) {
916                 CERROR("unknown subflavor %u\n", subflavor);
917                 GOTO(err_out, rc = -EINVAL);
918         }
919
920         rc = lgss_handle_krb5_upcall(cred->pc_uid, dest_ip,
921                                      imp->imp_obd->obd_name,
922                                      buf, sizeof(buf),
923                                      gss_send_secinit_rpc);
924         LASSERT(rc != 0);
925         if (rc < 0)
926                 goto err_out;
927
928         obj.data = buf;
929         obj.len = rc;
930
931         gsec = container_of(cred->pc_sec, struct gss_sec, gs_base);
932         mech = gsec->gs_mech;
933         LASSERT(mech);
934         rc = gss_parse_init_downcall(mech, &obj, &ctx, &vcred, &dest_ip,
935                                      &gss_err);
936         if (rc || gss_err) {
937                 CERROR("parse init downcall: rpc %d, gss 0x%x\n", rc, gss_err);
938                 if (rc != -ERESTART || gss_err != 0)
939                         set_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags);
940                 if (rc == 0)
941                         rc = -EPERM;
942                 goto err_out;
943         }
944
945         LASSERT(ctx);
946         gss_cred_set_ctx(cred, ctx);
947         LASSERT(gss_cred_is_uptodate_ctx(cred));
948
949         return 0;
950 err_out:
951         set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags);
952         return rc;
953 }
954 #endif
955
956 static int gss_cred_match(struct ptlrpc_cred *cred,
957                           struct vfs_cred *vcred)
958 {
959         RETURN(cred->pc_pag == vcred->vc_pag);
960 }
961
962 static int gss_cred_sign(struct ptlrpc_cred *cred,
963                          struct ptlrpc_request *req)
964 {
965         struct gss_cred         *gcred;
966         struct gss_cl_ctx       *ctx;
967         rawobj_t                 lmsg, mic;
968         __u32                   *vp, *vpsave, vlen, seclen;
969         __u32                    seqnum, major, rc = 0;
970         ENTRY;
971
972         LASSERT(req->rq_reqbuf);
973         LASSERT(req->rq_cred == cred);
974
975         gcred = container_of(cred, struct gss_cred, gc_base);
976         ctx = gss_cred_get_ctx(cred);
977         if (!ctx) {
978                 CERROR("cred %p("LPU64"/%u) invalidated?\n",
979                         cred, cred->pc_pag, cred->pc_uid);
980                 RETURN(-EPERM);
981         }
982
983         lmsg.len = req->rq_reqlen;
984         lmsg.data = (__u8 *) req->rq_reqmsg;
985
986         vp = (__u32 *) (lmsg.data + lmsg.len);
987         vlen = req->rq_reqbuf_len - sizeof(struct ptlrpcs_wire_hdr) -
988                lmsg.len;
989         seclen = vlen;
990
991         if (vlen < 6 * 4 + size_round4(ctx->gc_wire_ctx.len)) {
992                 CERROR("vlen %d, need %d\n",
993                         vlen, 6 * 4 + size_round4(ctx->gc_wire_ctx.len));
994                 rc = -EIO;
995                 goto out;
996         }
997
998         spin_lock(&ctx->gc_seq_lock);
999         seqnum = ctx->gc_seq++;
1000         spin_unlock(&ctx->gc_seq_lock);
1001
1002         *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION);    /* version */
1003         *vp++ = cpu_to_le32(PTLRPCS_FLVR_KRB5I);        /* subflavor */
1004         *vp++ = cpu_to_le32(ctx->gc_proc);              /* proc */
1005         *vp++ = cpu_to_le32(seqnum);                    /* seq */
1006         *vp++ = cpu_to_le32(PTLRPCS_GSS_SVC_INTEGRITY); /* service */
1007         vlen -= 5 * 4;
1008
1009         if (rawobj_serialize(&ctx->gc_wire_ctx, &vp, &vlen)) {
1010                 rc = -EIO;
1011                 goto out;
1012         }
1013         CDEBUG(D_SEC, "encoded wire_ctx length %d\n", ctx->gc_wire_ctx.len);
1014
1015         vpsave = vp++;  /* reserve for size */
1016         vlen -= 4;
1017
1018         mic.len = vlen;
1019         mic.data = (unsigned char *)vp;
1020
1021         CDEBUG(D_SEC, "reqbuf at %p, lmsg at %p, len %d, mic at %p, len %d\n",
1022                req->rq_reqbuf, lmsg.data, lmsg.len, mic.data, mic.len);
1023         major = kgss_get_mic(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT, &lmsg, &mic);
1024         if (major) {
1025                 CERROR("cred %p: req %p compute mic error, major %x\n",
1026                        cred, req, major);
1027                 rc = -EACCES;
1028                 goto out;
1029         }
1030
1031         *vpsave = cpu_to_le32(mic.len);
1032         
1033         seclen = seclen - vlen + mic.len;
1034         buf_to_sec_hdr(req->rq_reqbuf)->sec_len = cpu_to_le32(seclen);
1035         req->rq_reqdata_len += size_round(seclen);
1036         CDEBUG(D_SEC, "msg size %d, checksum size %d, total sec size %d\n",
1037                lmsg.len, mic.len, seclen);
1038 out:
1039         gss_put_ctx(ctx);
1040         RETURN(rc);
1041 }
1042
1043 static int gss_cred_verify(struct ptlrpc_cred *cred,
1044                            struct ptlrpc_request *req)
1045 {
1046         struct gss_cred        *gcred;
1047         struct gss_cl_ctx      *ctx;
1048         struct ptlrpcs_wire_hdr *sec_hdr;
1049         rawobj_t                lmsg, mic;
1050         __u32                   *vp, vlen, subflavor, proc, seq, svc;
1051         __u32                   major, minor, rc;
1052         ENTRY;
1053
1054         LASSERT(req->rq_repbuf);
1055         LASSERT(req->rq_cred == cred);
1056
1057         sec_hdr = buf_to_sec_hdr(req->rq_repbuf);
1058         vp = (__u32 *) (req->rq_repbuf + sizeof(*sec_hdr) + sec_hdr->msg_len);
1059         vlen = sec_hdr->sec_len;
1060
1061         if (vlen < 7 * 4) {
1062                 CERROR("reply sec size %u too small\n", vlen);
1063                 RETURN(-EPROTO);
1064         }
1065
1066         if (*vp++ != cpu_to_le32(PTLRPC_SEC_GSS_VERSION)) {
1067                 CERROR("reply have different gss version\n");
1068                 RETURN(-EPROTO);
1069         }
1070         subflavor = le32_to_cpu(*vp++);
1071         proc = le32_to_cpu(*vp++);
1072         vlen -= 3 * 4;
1073
1074         switch (proc) {
1075         case PTLRPCS_GSS_PROC_DATA:
1076                 seq = le32_to_cpu(*vp++);
1077                 svc = le32_to_cpu(*vp++);
1078                 if (svc != PTLRPCS_GSS_SVC_INTEGRITY) {
1079                         CERROR("Unknown svc %d\n", svc);
1080                         RETURN(-EPROTO);
1081                 }
1082                 if (*vp++ != 0) {
1083                         CERROR("Unexpected ctx handle\n");
1084                         RETURN(-EPROTO);
1085                 }
1086                 mic.len = le32_to_cpu(*vp++);
1087                 vlen -= 4 * 4;
1088                 if (vlen < mic.len) {
1089                         CERROR("vlen %d, mic.len %d\n", vlen, mic.len);
1090                         RETURN(-EINVAL);
1091                 }
1092                 mic.data = (unsigned char *)vp;
1093
1094                 gcred = container_of(cred, struct gss_cred, gc_base);
1095                 ctx = gss_cred_get_ctx(cred);
1096                 LASSERT(ctx);
1097
1098                 lmsg.len = sec_hdr->msg_len;
1099                 lmsg.data = (__u8 *) buf_to_lustre_msg(req->rq_repbuf);
1100
1101                 major = kgss_verify_mic(ctx->gc_gss_ctx, &lmsg, &mic, NULL);
1102                 if (major != GSS_S_COMPLETE) {
1103                         CERROR("cred %p: req %p verify mic error: major %x\n",
1104                                cred, req, major);
1105
1106                         if (major == GSS_S_CREDENTIALS_EXPIRED ||
1107                             major == GSS_S_CONTEXT_EXPIRED) {
1108                                 ptlrpcs_cred_expire(cred);
1109                                 req->rq_ptlrpcs_restart = 1;
1110                                 rc = 0;
1111                         } else
1112                                 rc = -EINVAL;
1113
1114                         GOTO(proc_data_out, rc);
1115                 }
1116
1117                 req->rq_repmsg = (struct lustre_msg *) lmsg.data;
1118                 req->rq_replen = lmsg.len;
1119
1120                 /* here we could check the seq number is the same one
1121                  * we sent to server. but portals has prevent us from
1122                  * replay attack, so maybe we don't need check it again.
1123                  */
1124                 rc = 0;
1125 proc_data_out:
1126                 gss_put_ctx(ctx);
1127                 break;
1128         case PTLRPCS_GSS_PROC_ERR:
1129                 major = le32_to_cpu(*vp++);
1130                 minor = le32_to_cpu(*vp++);
1131                 /* server return NO_CONTEXT might be caused by context expire
1132                  * or server reboot/failover. we refresh the cred transparently
1133                  * to upper layer.
1134                  * In some cases, our gss handle is possible to be incidentally
1135                  * identical to another handle since the handle itself is not
1136                  * fully random. In krb5 case, the GSS_S_BAD_SIG will be
1137                  * returned, maybe other gss error for other mechanism. Here we
1138                  * only consider krb5 mech (FIXME) and try to establish new
1139                  * context.
1140                  */
1141                 if (major == GSS_S_NO_CONTEXT ||
1142                     major == GSS_S_BAD_SIG) {
1143                         CWARN("req %p: server report cred %p %s, expired?\n",
1144                                req, cred, (major == GSS_S_NO_CONTEXT) ?
1145                                            "NO_CONTEXT" : "BAD_SIG");
1146
1147                         ptlrpcs_cred_expire(cred);
1148                         req->rq_ptlrpcs_restart = 1;
1149                         rc = 0;
1150                 } else {
1151                         CERROR("req %p: unrecognized gss error (%x/%x)\n",
1152                                 req, major, minor);
1153                         rc = -EACCES;
1154                 }
1155                 break;
1156         default:
1157                 CERROR("unknown gss proc %d\n", proc);
1158                 rc = -EPROTO;
1159         }
1160
1161         RETURN(rc);
1162 }
1163
1164 static int gss_cred_seal(struct ptlrpc_cred *cred,
1165                          struct ptlrpc_request *req)
1166 {
1167         struct gss_cred         *gcred;
1168         struct gss_cl_ctx       *ctx;
1169         struct ptlrpcs_wire_hdr *sec_hdr;
1170         rawobj_buf_t             msg_buf;
1171         rawobj_t                 cipher_buf;
1172         __u32                   *vp, *vpsave, vlen, seclen;
1173         __u32                    major, seqnum, rc = 0;
1174         ENTRY;
1175
1176         LASSERT(req->rq_reqbuf);
1177         LASSERT(req->rq_cred == cred);
1178
1179         gcred = container_of(cred, struct gss_cred, gc_base);
1180         ctx = gss_cred_get_ctx(cred);
1181         if (!ctx) {
1182                 CERROR("cred %p("LPU64"/%u) invalidated?\n",
1183                         cred, cred->pc_pag, cred->pc_uid);
1184                 RETURN(-EPERM);
1185         }
1186
1187         vp = (__u32 *) (req->rq_reqbuf + sizeof(*sec_hdr));
1188         vlen = req->rq_reqbuf_len - sizeof(*sec_hdr);
1189         seclen = vlen;
1190
1191         if (vlen < 6 * 4 + size_round4(ctx->gc_wire_ctx.len)) {
1192                 CERROR("vlen %d, need %d\n",
1193                         vlen, 6 * 4 + size_round4(ctx->gc_wire_ctx.len));
1194                 rc = -EIO;
1195                 goto out;
1196         }
1197
1198         spin_lock(&ctx->gc_seq_lock);
1199         seqnum = ctx->gc_seq++;
1200         spin_unlock(&ctx->gc_seq_lock);
1201
1202         *vp++ = cpu_to_le32(PTLRPC_SEC_GSS_VERSION);    /* version */
1203         *vp++ = cpu_to_le32(PTLRPCS_FLVR_KRB5P);        /* subflavor */
1204         *vp++ = cpu_to_le32(ctx->gc_proc);              /* proc */
1205         *vp++ = cpu_to_le32(seqnum);                    /* seq */
1206         *vp++ = cpu_to_le32(PTLRPCS_GSS_SVC_PRIVACY);   /* service */
1207         vlen -= 5 * 4;
1208
1209         if (rawobj_serialize(&ctx->gc_wire_ctx, &vp, &vlen)) {
1210                 rc = -EIO;
1211                 goto out;
1212         }
1213         CDEBUG(D_SEC, "encoded wire_ctx length %d\n", ctx->gc_wire_ctx.len);
1214
1215         vpsave = vp++;  /* reserve for size */
1216         vlen -= 4;
1217
1218         msg_buf.buf = (__u8 *) req->rq_reqmsg - GSS_PRIVBUF_PREFIX_LEN;
1219         msg_buf.buflen = req->rq_reqlen + GSS_PRIVBUF_PREFIX_LEN +
1220                                           GSS_PRIVBUF_SUFFIX_LEN;
1221         msg_buf.dataoff = GSS_PRIVBUF_PREFIX_LEN;
1222         msg_buf.datalen = req->rq_reqlen;
1223
1224         cipher_buf.data = (__u8 *) vp;
1225         cipher_buf.len = vlen;
1226
1227         major = kgss_wrap(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT,
1228                           &msg_buf, &cipher_buf);
1229         if (major) {
1230                 CERROR("cred %p: error wrap: major 0x%x\n", cred, major);
1231                 GOTO(out, rc = -EINVAL);
1232         }
1233
1234         *vpsave = cpu_to_le32(cipher_buf.len);
1235
1236         seclen = seclen - vlen + cipher_buf.len;
1237         sec_hdr = buf_to_sec_hdr(req->rq_reqbuf);
1238         sec_hdr->sec_len = cpu_to_le32(seclen);
1239         req->rq_reqdata_len += size_round(seclen);
1240
1241         CDEBUG(D_SEC, "msg size %d, total sec size %d\n",
1242                req->rq_reqlen, seclen);
1243 out:
1244         gss_put_ctx(ctx);
1245         RETURN(rc);
1246 }
1247
1248 static int gss_cred_unseal(struct ptlrpc_cred *cred,
1249                            struct ptlrpc_request *req)
1250 {
1251         struct gss_cred        *gcred;
1252         struct gss_cl_ctx      *ctx;
1253         struct ptlrpcs_wire_hdr *sec_hdr;
1254         rawobj_t                cipher_text, plain_text;
1255         __u32                   *vp, vlen, subflavor, proc, seq, svc;
1256         __u32                   major, rc;
1257         ENTRY;
1258
1259         LASSERT(req->rq_repbuf);
1260         LASSERT(req->rq_cred == cred);
1261
1262         sec_hdr = buf_to_sec_hdr(req->rq_repbuf);
1263         if (sec_hdr->msg_len != 0) {
1264                 CERROR("unexpected msg_len %u\n", sec_hdr->msg_len);
1265                 RETURN(-EPROTO);
1266         }
1267
1268         vp = (__u32 *) (req->rq_repbuf + sizeof(*sec_hdr));
1269         vlen = sec_hdr->sec_len;
1270
1271         if (vlen < 7 * 4) {
1272                 CERROR("reply sec size %u too small\n", vlen);
1273                 RETURN(-EPROTO);
1274         }
1275
1276         if (*vp++ != cpu_to_le32(PTLRPC_SEC_GSS_VERSION)) {
1277                 CERROR("reply have different gss version\n");
1278                 RETURN(-EPROTO);
1279         }
1280         subflavor = le32_to_cpu(*vp++);
1281         proc = le32_to_cpu(*vp++);
1282         seq = le32_to_cpu(*vp++);
1283         svc = le32_to_cpu(*vp++);
1284         vlen -= 5 * 4;
1285
1286         switch (proc) {
1287         case PTLRPCS_GSS_PROC_DATA:
1288                 if (svc != PTLRPCS_GSS_SVC_PRIVACY) {
1289                         CERROR("Unknown svc %d\n", svc);
1290                         RETURN(-EPROTO);
1291                 }
1292                 if (*vp++ != 0) {
1293                         CERROR("Unexpected ctx handle\n");
1294                         RETURN(-EPROTO);
1295                 }
1296                 vlen -= 4;
1297
1298                 cipher_text.len = le32_to_cpu(*vp++);
1299                 cipher_text.data = (__u8 *) vp;
1300                 vlen -= 4;
1301
1302                 if (vlen < cipher_text.len) {
1303                         CERROR("cipher text to be %u while buf only %u\n",
1304                                 cipher_text.len, vlen);
1305                         RETURN(-EPROTO);
1306                 }
1307
1308                 plain_text = cipher_text;
1309
1310                 gcred = container_of(cred, struct gss_cred, gc_base);
1311                 ctx = gss_cred_get_ctx(cred);
1312                 LASSERT(ctx);
1313
1314                 major = kgss_unwrap(ctx->gc_gss_ctx, GSS_C_QOP_DEFAULT,
1315                                     &cipher_text, &plain_text);
1316                 if (major) {
1317                         CERROR("cred %p: error unwrap: major 0x%x\n",
1318                                cred, major);
1319
1320                         if (major == GSS_S_CREDENTIALS_EXPIRED ||
1321                             major == GSS_S_CONTEXT_EXPIRED) {
1322                                 ptlrpcs_cred_expire(cred);
1323                                 req->rq_ptlrpcs_restart = 1;
1324                                 rc = 0;
1325                         } else
1326                                 rc = -EINVAL;
1327
1328                         GOTO(proc_out, rc);
1329                 }
1330
1331                 req->rq_repmsg = (struct lustre_msg *) vp;
1332                 req->rq_replen = plain_text.len;
1333
1334                 rc = 0;
1335 proc_out:
1336                 gss_put_ctx(ctx);
1337                 break;
1338         default:
1339                 CERROR("unknown gss proc %d\n", proc);
1340                 rc = -EPROTO;
1341         }
1342
1343         RETURN(rc);
1344 }
1345
1346 static void destroy_gss_context(struct ptlrpc_cred *cred)
1347 {
1348         struct ptlrpcs_wire_hdr *hdr;
1349         struct lustre_msg       *lmsg;
1350         struct gss_cred         *gcred;
1351         struct ptlrpc_request    req;
1352         struct obd_import       *imp;
1353         __u32                   *vp, lmsg_size;
1354         struct ptlrpc_request   *raw_req = NULL;
1355         const int                repbuf_len = 256;
1356         char                    *repbuf;
1357         int                      replen, rc;
1358         ENTRY;
1359
1360         imp = cred->pc_sec->ps_import;
1361         LASSERT(imp);
1362
1363         if (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags) ||
1364             !test_bit(PTLRPC_CRED_UPTODATE_BIT, &cred->pc_flags)) {
1365                 CDEBUG(D_SEC, "Destroy dead cred %p(%u@%s)\n",
1366                        cred, cred->pc_uid, imp->imp_target_uuid.uuid);
1367                 EXIT;
1368                 return;
1369         }
1370
1371         might_sleep();
1372
1373         /* cred's refcount is 0, steal one */
1374         atomic_inc(&cred->pc_refcount);
1375
1376         gcred = container_of(cred, struct gss_cred, gc_base);
1377         gcred->gc_ctx->gc_proc = PTLRPCS_GSS_PROC_DESTROY;
1378
1379         CDEBUG(D_SEC, "client destroy gss cred %p(%u@%s)\n",
1380                gcred, cred->pc_uid, imp->imp_target_uuid.uuid);
1381
1382         lmsg_size = lustre_msg_size(0, NULL);
1383         req.rq_req_secflvr = cred->pc_sec->ps_flavor;
1384         req.rq_cred = cred;
1385         req.rq_reqbuf_len = sizeof(*hdr) + lmsg_size +
1386                             ptlrpcs_est_req_payload(&req, lmsg_size);
1387
1388         OBD_ALLOC(req.rq_reqbuf, req.rq_reqbuf_len);
1389         if (!req.rq_reqbuf) {
1390                 CERROR("Fail to alloc reqbuf, cancel anyway\n");
1391                 atomic_dec(&cred->pc_refcount);
1392                 EXIT;
1393                 return;
1394         }
1395
1396         /* wire hdr */
1397         hdr = buf_to_sec_hdr(req.rq_reqbuf);
1398         hdr->flavor  = cpu_to_le32(PTLRPCS_FLVR_GSS_AUTH);
1399         hdr->msg_len = cpu_to_le32(lmsg_size);
1400         hdr->sec_len = cpu_to_le32(0);
1401
1402         /* lustre message */
1403         lmsg = buf_to_lustre_msg(req.rq_reqbuf);
1404         lustre_init_msg(lmsg, 0, NULL, NULL);
1405         lmsg->handle   = imp->imp_remote_handle;
1406         lmsg->type     = PTL_RPC_MSG_REQUEST;
1407         lmsg->opc      = SEC_FINI;
1408         lmsg->flags    = 0;
1409         lmsg->conn_cnt = imp->imp_conn_cnt;
1410         /* add this for randomize */
1411         get_random_bytes(&lmsg->last_xid, sizeof(lmsg->last_xid));
1412         get_random_bytes(&lmsg->transno, sizeof(lmsg->transno));
1413
1414         vp = (__u32 *) req.rq_reqbuf;
1415
1416         req.rq_cred = cred;
1417         req.rq_reqmsg = buf_to_lustre_msg(req.rq_reqbuf);
1418         req.rq_reqlen = lmsg_size;
1419         req.rq_reqdata_len = sizeof(*hdr) + lmsg_size;
1420
1421         if (gss_cred_sign(cred, &req)) {
1422                 CERROR("failed to sign, cancel anyway\n");
1423                 atomic_dec(&cred->pc_refcount);
1424                 goto exit;
1425         }
1426         atomic_dec(&cred->pc_refcount);
1427
1428         OBD_ALLOC(repbuf, repbuf_len);
1429         if (!repbuf)
1430                 goto exit;
1431
1432         raw_req = ptl_do_rawrpc(imp, req.rq_reqbuf, req.rq_reqbuf_len,
1433                                 req.rq_reqdata_len, repbuf, repbuf_len, &replen,
1434                                 SECFINI_RPC_TIMEOUT, &rc);
1435         if (!raw_req)
1436                 OBD_FREE(repbuf, repbuf_len);
1437
1438 exit:
1439         if (raw_req == NULL)
1440                 OBD_FREE(req.rq_reqbuf, req.rq_reqbuf_len);
1441         else
1442                 rawrpc_req_finished(raw_req);
1443         EXIT;
1444 }
1445
1446 static void gss_cred_destroy(struct ptlrpc_cred *cred)
1447 {
1448         struct gss_cred *gcred;
1449         ENTRY;
1450
1451         LASSERT(cred);
1452         LASSERT(!atomic_read(&cred->pc_refcount));
1453
1454         gcred = container_of(cred, struct gss_cred, gc_base);
1455         if (gcred->gc_ctx) {
1456                 destroy_gss_context(cred);
1457                 gss_put_ctx(gcred->gc_ctx);
1458         }
1459
1460         CDEBUG(D_SEC, "sec.gss %p: destroy cred %p\n", cred->pc_sec, gcred);
1461
1462         OBD_FREE(gcred, sizeof(*gcred));
1463         EXIT;
1464 }
1465
1466 static struct ptlrpc_credops gss_credops = {
1467         .refresh        = gss_cred_refresh,
1468         .match          = gss_cred_match,
1469         .sign           = gss_cred_sign,
1470         .verify         = gss_cred_verify,
1471         .seal           = gss_cred_seal,
1472         .unseal         = gss_cred_unseal,
1473         .destroy        = gss_cred_destroy,
1474 };
1475
1476 #ifdef __KERNEL__
1477 /*******************************************
1478  * rpc_pipe APIs                           *
1479  *******************************************/
1480 static ssize_t
1481 gss_pipe_upcall(struct file *filp, struct rpc_pipe_msg *msg,
1482                 char *dst, size_t buflen)
1483 {
1484         char *data = (char *)msg->data + msg->copied;
1485         ssize_t mlen = msg->len;
1486         ssize_t left;
1487         ENTRY;
1488
1489         if (mlen > buflen)
1490                 mlen = buflen;
1491         left = copy_to_user(dst, data, mlen);
1492         if (left < 0) {
1493                 msg->errno = left;
1494                 RETURN(left);
1495         }
1496         mlen -= left;
1497         msg->copied += mlen;
1498         msg->errno = 0;
1499         RETURN(mlen);
1500 }
1501
1502 static ssize_t
1503 gss_pipe_downcall(struct file *filp, const char *src, size_t mlen)
1504 {
1505         char *buf;
1506         const int bufsize = 1024;
1507         rawobj_t obj;
1508         struct inode *inode = filp->f_dentry->d_inode;
1509         struct rpc_inode *rpci = RPC_I(inode);
1510         struct obd_import *import;
1511         struct ptlrpc_sec *sec;
1512         struct gss_sec *gsec;
1513         char *obdname;
1514         struct gss_api_mech *mech;
1515         struct vfs_cred vcred = { 0 };
1516         struct ptlrpc_cred *cred;
1517         struct gss_upcall_msg *gss_msg;
1518         struct gss_upcall_msg_data gmd = { 0 };
1519         struct gss_cl_ctx *ctx = NULL;
1520         ssize_t left;
1521         int err, gss_err;
1522         ENTRY;
1523
1524         if (mlen > bufsize) {
1525                 CERROR("mlen %ld > bufsize %d\n", (long)mlen, bufsize);
1526                 RETURN(-ENOSPC);
1527         }
1528
1529         OBD_ALLOC(buf, bufsize);
1530         if (!buf) {
1531                 CERROR("alloc mem failed\n");
1532                 RETURN(-ENOMEM);
1533         }
1534
1535         left = copy_from_user(buf, src, mlen);
1536         if (left)
1537                 GOTO(err_free, err = -EFAULT);
1538
1539         obj.data = (unsigned char *)buf;
1540         obj.len = mlen;
1541
1542         LASSERT(rpci->private);
1543         gsec = (struct gss_sec *)rpci->private;
1544         sec = &gsec->gs_base;
1545         LASSERT(sec->ps_import);
1546         import = class_import_get(sec->ps_import);
1547         LASSERT(import->imp_obd);
1548         obdname = import->imp_obd->obd_name;
1549         mech = gsec->gs_mech;
1550
1551         err = gss_parse_init_downcall(mech, &obj, &ctx, &gmd, &gss_err);
1552         if (err)
1553                 CERROR("parse init downcall err %d\n", err);
1554
1555         vcred.vc_uid = gmd.gum_uid;
1556         vcred.vc_pag = vcred.vc_uid; /* FIXME */
1557
1558         cred = ptlrpcs_cred_lookup(sec, &vcred);
1559         if (!cred) {
1560                 CWARN("didn't find cred for uid %u\n", vcred.vc_uid);
1561                 GOTO(err, err = -EINVAL);
1562         }
1563
1564         if (err || gss_err) {
1565                 set_bit(PTLRPC_CRED_DEAD_BIT, &cred->pc_flags);
1566                 if (err != -ERESTART || gss_err != 0)
1567                         set_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags);
1568                 CERROR("cred %p: rpc err %d, gss err 0x%x, fatal %d\n",
1569                        cred, err, gss_err,
1570                        (test_bit(PTLRPC_CRED_ERROR_BIT, &cred->pc_flags) != 0));
1571         } else {
1572                 CDEBUG(D_SEC, "get initial ctx:\n");
1573                 gss_cred_set_ctx(cred, ctx);
1574         }
1575
1576         spin_lock(&gsec->gs_lock);
1577         gss_msg = gss_find_upcall(gsec, obdname, &gmd);
1578         if (gss_msg) {
1579                 gss_unhash_msg_nolock(gss_msg);
1580                 spin_unlock(&gsec->gs_lock);
1581                 gss_release_msg(gss_msg);
1582         } else
1583                 spin_unlock(&gsec->gs_lock);
1584
1585         ptlrpcs_cred_put(cred, 1);
1586         class_import_put(import);
1587         OBD_FREE(buf, bufsize);
1588         RETURN(mlen);
1589 err:
1590         if (ctx)
1591                 gss_destroy_ctx(ctx);
1592         class_import_put(import);
1593 err_free:
1594         OBD_FREE(buf, bufsize);
1595         CDEBUG(D_SEC, "gss_pipe_downcall returning %d\n", err);
1596         RETURN(err);
1597 }
1598
1599 static
1600 void gss_pipe_destroy_msg(struct rpc_pipe_msg *msg)
1601 {
1602         struct gss_upcall_msg *gmsg;
1603         static unsigned long ratelimit;
1604         ENTRY;
1605
1606         LASSERT(list_empty(&msg->list));
1607
1608         if (msg->errno >= 0) {
1609                 EXIT;
1610                 return;
1611         }
1612
1613         gmsg = container_of(msg, struct gss_upcall_msg, gum_base);
1614         CDEBUG(D_SEC, "destroy gmsg %p\n", gmsg);
1615         LASSERT(atomic_read(&gmsg->gum_refcount) > 0);
1616         atomic_inc(&gmsg->gum_refcount);
1617         gss_unhash_msg(gmsg);
1618         if (msg->errno == -ETIMEDOUT || msg->errno == -EPIPE) {
1619                 unsigned long now = get_seconds();
1620                 if (time_after(now, ratelimit)) {
1621                         CWARN("sec.gss upcall timed out.\n"
1622                               "Please check user daemon is running!\n");
1623                         ratelimit = now + 15;
1624                 }
1625         }
1626         gss_release_msg(gmsg);
1627         EXIT;
1628 }
1629
1630 static
1631 void gss_pipe_release(struct inode *inode)
1632 {
1633         struct rpc_inode *rpci = RPC_I(inode);
1634         struct ptlrpc_sec *sec;
1635         struct gss_sec *gsec;
1636         ENTRY;
1637
1638         gsec = (struct gss_sec *)rpci->private;
1639         sec = &gsec->gs_base;
1640         spin_lock(&gsec->gs_lock);
1641         while (!list_empty(&gsec->gs_upcalls)) {
1642                 struct gss_upcall_msg *gmsg;
1643
1644                 gmsg = list_entry(gsec->gs_upcalls.next,
1645                                   struct gss_upcall_msg, gum_list);
1646                 LASSERT(list_empty(&gmsg->gum_base.list));
1647                 gmsg->gum_base.errno = -EPIPE;
1648                 atomic_inc(&gmsg->gum_refcount);
1649                 gss_unhash_msg_nolock(gmsg);
1650                 gss_release_msg(gmsg);
1651         }
1652         spin_unlock(&gsec->gs_lock);
1653         EXIT;
1654 }
1655
1656 static struct rpc_pipe_ops gss_upcall_ops = {
1657         .upcall         = gss_pipe_upcall,
1658         .downcall       = gss_pipe_downcall,
1659         .destroy_msg    = gss_pipe_destroy_msg,
1660         .release_pipe   = gss_pipe_release,
1661 };
1662 #endif /* __KERNEL__ */
1663
1664 /*********************************************
1665  * GSS security APIs                         *
1666  *********************************************/
1667
1668 static
1669 struct ptlrpc_sec* gss_create_sec(__u32 flavor,
1670                                   const char *pipe_dir,
1671                                   void *pipe_data)
1672 {
1673         struct gss_sec *gsec;
1674         struct ptlrpc_sec *sec;
1675 #ifdef __KERNEL__
1676         char *pos;
1677         int   pipepath_len;
1678         uid_t save_uid;
1679 #endif
1680         ENTRY;
1681
1682         LASSERT(SEC_FLAVOR_MAJOR(flavor) == PTLRPCS_FLVR_MAJOR_GSS);
1683
1684         OBD_ALLOC(gsec, sizeof(*gsec));
1685         if (!gsec) {
1686                 CERROR("can't alloc gsec\n");
1687                 RETURN(NULL);
1688         }
1689
1690         gsec->gs_mech = kgss_subflavor_to_mech(SEC_FLAVOR_SUB(flavor));
1691         if (!gsec->gs_mech) {
1692                 CERROR("subflavor 0x%x not found\n", flavor);
1693                 goto err_free;
1694         }
1695
1696         /* initialize gss sec */
1697 #ifdef __KERNEL__
1698         INIT_LIST_HEAD(&gsec->gs_upcalls);
1699         spin_lock_init(&gsec->gs_lock);
1700
1701         pipepath_len = strlen(LUSTRE_PIPEDIR) + strlen(pipe_dir) +
1702                        strlen(gsec->gs_mech->gm_name) + 3;
1703         OBD_ALLOC(gsec->gs_pipepath, pipepath_len);
1704         if (!gsec->gs_pipepath)
1705                 goto err_mech_put;
1706
1707         /* pipe rpc require root permission */
1708         save_uid = current->fsuid;
1709         current->fsuid = 0;
1710
1711         sprintf(gsec->gs_pipepath, LUSTRE_PIPEDIR"/%s", pipe_dir);
1712         if (IS_ERR(rpc_mkdir(gsec->gs_pipepath, NULL))) {
1713                 CERROR("can't make pipedir %s\n", gsec->gs_pipepath);
1714                 goto err_free_path;
1715         }
1716
1717         sprintf(gsec->gs_pipepath, LUSTRE_PIPEDIR"/%s/%s", pipe_dir,
1718                 gsec->gs_mech->gm_name); 
1719         gsec->gs_depipe = rpc_mkpipe(gsec->gs_pipepath, gsec,
1720                                      &gss_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN);
1721         if (IS_ERR(gsec->gs_depipe)) {
1722                 CERROR("failed to make rpc_pipe %s: %ld\n",
1723                         gsec->gs_pipepath, PTR_ERR(gsec->gs_depipe));
1724                 goto err_rmdir;
1725         }
1726         CDEBUG(D_SEC, "gss sec %p, pipe path %s\n", gsec, gsec->gs_pipepath);
1727 #endif
1728
1729         sec = &gsec->gs_base;
1730         sec->ps_expire = GSS_CREDCACHE_EXPIRE;
1731         sec->ps_nextgc = get_seconds() + sec->ps_expire;
1732         sec->ps_flags = 0;
1733
1734         current->fsuid = save_uid;
1735
1736         CDEBUG(D_SEC, "Create sec.gss %p\n", gsec);
1737         RETURN(sec);
1738
1739 #ifdef __KERNEL__
1740 err_rmdir:
1741         pos = strrchr(gsec->gs_pipepath, '/');
1742         LASSERT(pos);
1743         *pos = 0;
1744         rpc_rmdir(gsec->gs_pipepath);
1745 err_free_path:
1746         current->fsuid = save_uid;
1747         OBD_FREE(gsec->gs_pipepath, pipepath_len);
1748 err_mech_put:
1749 #endif
1750         kgss_mech_put(gsec->gs_mech);
1751 err_free:
1752         OBD_FREE(gsec, sizeof(*gsec));
1753         RETURN(NULL);
1754 }
1755
1756 static
1757 void gss_destroy_sec(struct ptlrpc_sec *sec)
1758 {
1759         struct gss_sec *gsec;
1760 #ifdef __KERNEL__
1761         char *pos;
1762         int   pipepath_len;
1763 #endif
1764         ENTRY;
1765
1766         gsec = container_of(sec, struct gss_sec, gs_base);
1767         CDEBUG(D_SEC, "Destroy sec.gss %p\n", gsec);
1768
1769         LASSERT(gsec->gs_mech);
1770         LASSERT(!atomic_read(&sec->ps_refcount));
1771         LASSERT(!atomic_read(&sec->ps_credcount));
1772 #ifdef __KERNEL__
1773         pipepath_len = strlen(gsec->gs_pipepath) + 1;
1774         rpc_unlink(gsec->gs_pipepath);
1775         pos = strrchr(gsec->gs_pipepath, '/');
1776         LASSERT(pos);
1777         *pos = 0;
1778         rpc_rmdir(gsec->gs_pipepath);
1779         OBD_FREE(gsec->gs_pipepath, pipepath_len);
1780 #endif
1781
1782         kgss_mech_put(gsec->gs_mech);
1783         OBD_FREE(gsec, sizeof(*gsec));
1784         EXIT;
1785 }
1786
1787 static
1788 struct ptlrpc_cred * gss_create_cred(struct ptlrpc_sec *sec,
1789                                      struct vfs_cred *vcred)
1790 {
1791         struct gss_cred *gcred;
1792         struct ptlrpc_cred *cred;
1793         ENTRY;
1794
1795         OBD_ALLOC(gcred, sizeof(*gcred));
1796         if (!gcred)
1797                 RETURN(NULL);
1798
1799         cred = &gcred->gc_base;
1800         INIT_LIST_HEAD(&cred->pc_hash);
1801         atomic_set(&cred->pc_refcount, 0);
1802         cred->pc_sec = sec;
1803         cred->pc_ops = &gss_credops;
1804         cred->pc_expire = 0;
1805         cred->pc_flags = 0;
1806         cred->pc_pag = vcred->vc_pag;
1807         cred->pc_uid = vcred->vc_uid;
1808         CDEBUG(D_SEC, "create a gss cred at %p("LPU64"/%u)\n",
1809                cred, vcred->vc_pag, vcred->vc_uid);
1810
1811         RETURN(cred);
1812 }
1813
1814 static int gss_estimate_payload(struct ptlrpc_sec *sec,
1815                                 struct ptlrpc_request *req,
1816                                 int msgsize)
1817 {
1818         switch (SEC_FLAVOR_SVC(req->rq_req_secflvr)) {
1819         case PTLRPCS_SVC_AUTH:
1820                 return GSS_MAX_AUTH_PAYLOAD;
1821         case PTLRPCS_SVC_PRIV:
1822                 return size_round16(GSS_MAX_AUTH_PAYLOAD + msgsize +
1823                                     GSS_PRIVBUF_PREFIX_LEN +
1824                                     GSS_PRIVBUF_SUFFIX_LEN);
1825         default:
1826                 LBUG();
1827                 return 0;
1828         }
1829 }
1830
1831 static int gss_alloc_reqbuf(struct ptlrpc_sec *sec,
1832                             struct ptlrpc_request *req,
1833                             int lmsg_size)
1834 {
1835         int msg_payload, sec_payload;
1836         int privacy, rc;
1837         ENTRY;
1838
1839         /* In PRIVACY mode, lustre message is always 0 (already encoded into
1840          * security payload).
1841          */
1842         privacy = (SEC_FLAVOR_SVC(req->rq_req_secflvr) == PTLRPCS_SVC_PRIV);
1843         msg_payload = privacy ? 0 : lmsg_size;
1844         sec_payload = gss_estimate_payload(sec, req, lmsg_size);
1845
1846         rc = sec_alloc_reqbuf(sec, req, msg_payload, sec_payload);
1847         if (rc)
1848                 return rc;
1849
1850         if (privacy) {
1851                 int buflen = lmsg_size + GSS_PRIVBUF_PREFIX_LEN +
1852                              GSS_PRIVBUF_SUFFIX_LEN;
1853                 char *buf;
1854
1855                 OBD_ALLOC(buf, buflen);
1856                 if (!buf) {
1857                         CERROR("Fail to alloc %d\n", buflen);
1858                         sec_free_reqbuf(sec, req);
1859                         RETURN(-ENOMEM);
1860                 }
1861                 req->rq_reqmsg = (struct lustre_msg *)
1862                                         (buf + GSS_PRIVBUF_PREFIX_LEN);
1863         }
1864
1865         RETURN(0);
1866 }
1867
1868 static void gss_free_reqbuf(struct ptlrpc_sec *sec,
1869                             struct ptlrpc_request *req)
1870 {
1871         char *buf;
1872         int privacy;
1873         ENTRY;
1874
1875         LASSERT(req->rq_reqmsg);
1876         LASSERT(req->rq_reqlen);
1877
1878         privacy = SEC_FLAVOR_SVC(req->rq_req_secflvr) == PTLRPCS_SVC_PRIV;
1879         if (privacy) {
1880                 buf = (char *) req->rq_reqmsg - GSS_PRIVBUF_PREFIX_LEN;
1881                 LASSERT(buf < req->rq_reqbuf ||
1882                         buf >= req->rq_reqbuf + req->rq_reqbuf_len);
1883                 OBD_FREE(buf, req->rq_reqlen + GSS_PRIVBUF_PREFIX_LEN +
1884                               GSS_PRIVBUF_SUFFIX_LEN);
1885                 req->rq_reqmsg = NULL;
1886         }
1887
1888         sec_free_reqbuf(sec, req);
1889 }
1890
1891 static struct ptlrpc_secops gss_secops = {
1892         .create_sec             = gss_create_sec,
1893         .destroy_sec            = gss_destroy_sec,
1894         .create_cred            = gss_create_cred,
1895         .est_req_payload        = gss_estimate_payload,
1896         .est_rep_payload        = gss_estimate_payload,
1897         .alloc_reqbuf           = gss_alloc_reqbuf,
1898         .free_reqbuf            = gss_free_reqbuf,
1899 };
1900
1901 static struct ptlrpc_sec_type gss_type = {
1902         .pst_owner      = THIS_MODULE,
1903         .pst_name       = "sec.gss",
1904         .pst_inst       = ATOMIC_INIT(0),
1905         .pst_flavor     = PTLRPCS_FLVR_MAJOR_GSS,
1906         .pst_ops        = &gss_secops,
1907 };
1908
1909 extern int
1910 (*lustre_secinit_downcall_handler)(char *buffer, unsigned long count);
1911
1912 int __init ptlrpcs_gss_init(void)
1913 {
1914         int rc;
1915
1916         rc = ptlrpcs_register(&gss_type);
1917         if (rc)
1918                 return rc;
1919
1920 #ifdef __KERNEL__
1921         gss_svc_init();
1922
1923         rc = PTR_ERR(rpc_mkdir(LUSTRE_PIPEDIR, NULL));
1924         if (IS_ERR((void *)rc) && rc != -EEXIST) {
1925                 CERROR("fail to make rpcpipedir for lustre\n");
1926                 gss_svc_exit();
1927                 ptlrpcs_unregister(&gss_type);
1928                 return -1;
1929         }
1930         rc = 0;
1931 #else
1932 #endif
1933         rc = init_kerberos_module();
1934         if (rc) {
1935                 ptlrpcs_unregister(&gss_type);
1936         }
1937
1938         lustre_secinit_downcall_handler = gss_send_secinit_rpc;
1939
1940         return rc;
1941 }
1942
1943 #ifdef __KERNEL__
1944 static void __exit ptlrpcs_gss_exit(void)
1945 {
1946         lustre_secinit_downcall_handler = NULL;
1947
1948         cleanup_kerberos_module();
1949         rpc_rmdir(LUSTRE_PIPEDIR);
1950         gss_svc_exit();
1951         ptlrpcs_unregister(&gss_type);
1952 }
1953 #endif
1954
1955 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
1956 MODULE_DESCRIPTION("GSS Security module for Lustre");
1957 MODULE_LICENSE("GPL");
1958
1959 module_init(ptlrpcs_gss_init);
1960 module_exit(ptlrpcs_gss_exit);