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