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