Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_keyring.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2007 Cluster File Systems, Inc.
5  *   Author: Eric Mei <ericm@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */
22
23 #ifndef EXPORT_SYMTAB
24 # define EXPORT_SYMTAB
25 #endif
26 #define DEBUG_SUBSYSTEM S_SEC
27 #ifdef __KERNEL__
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/dcache.h>
32 #include <linux/fs.h>
33 #include <linux/random.h>
34 #include <linux/crypto.h>
35 #include <linux/key.h>
36 #include <linux/keyctl.h>
37 #include <linux/mutex.h>
38 #include <asm/atomic.h>
39 #else
40 #include <liblustre.h>
41 #endif
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_sec.h>
48 #include <lustre_net.h>
49 #include <lustre_import.h>
50
51 #include "gss_err.h"
52 #include "gss_internal.h"
53 #include "gss_api.h"
54
55 static struct ptlrpc_sec_policy gss_policy_keyring;
56 static struct ptlrpc_ctx_ops gss_keyring_ctxops;
57 static struct key_type gss_key_type;
58
59 static int sec_install_rctx_kr(struct ptlrpc_sec *sec,
60                                struct ptlrpc_svc_ctx *svc_ctx);
61
62 /*
63  * the timeout is only for the case that upcall child process die abnormally.
64  * in any other cases it should finally update kernel key. so we set this
65  * timeout value excessive long.
66  */
67 #define KEYRING_UPCALL_TIMEOUT  (obd_timeout + obd_timeout)
68
69 /****************************************
70  * internal helpers                     *
71  ****************************************/
72
73 #define DUMP_PROCESS_KEYRINGS(tsk)                                      \
74 {                                                                       \
75         CWARN("DUMP PK: %s[%u,%u/%u](<-%s[%u,%u/%u]): "                 \
76               "a %d, t %d, p %d, s %d, u %d, us %d, df %d\n",           \
77               tsk->comm, tsk->pid, tsk->uid, tsk->fsuid,                \
78               tsk->parent->comm, tsk->parent->pid,                      \
79               tsk->parent->uid, tsk->parent->fsuid,                     \
80               task_aux(tsk)->request_key_auth ?                         \
81               task_aux(tsk)->request_key_auth->serial : 0,              \
82               task_aux(tsk)->thread_keyring ?                           \
83               task_aux(tsk)->thread_keyring->serial : 0,                \
84               tsk->signal->process_keyring ?                            \
85               tsk->signal->process_keyring->serial : 0,                 \
86               tsk->signal->session_keyring ?                            \
87               tsk->signal->session_keyring->serial : 0,                 \
88               tsk->user->uid_keyring ?                                  \
89               tsk->user->uid_keyring->serial : 0,                       \
90               tsk->user->session_keyring ?                              \
91               tsk->user->session_keyring->serial : 0,                   \
92               task_aux(tsk)->jit_keyring                                \
93              );                                                         \
94 }
95
96 #define DUMP_KEY(key)                                                   \
97 {                                                                       \
98         CWARN("DUMP KEY: %p(%d) ref %d u%u/g%u desc %s\n",              \
99               key, key->serial, atomic_read(&key->usage),               \
100               key->uid, key->gid,                                       \
101               key->description ? key->description : "n/a"               \
102              );                                                         \
103 }
104
105
106 static inline void keyring_upcall_lock(struct gss_sec_keyring *gsec_kr)
107 {
108 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
109         mutex_lock(&gsec_kr->gsk_uc_lock);
110 #endif
111 }
112
113 static inline void keyring_upcall_unlock(struct gss_sec_keyring *gsec_kr)
114 {
115 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
116         mutex_unlock(&gsec_kr->gsk_uc_lock);
117 #endif
118 }
119
120 static inline void key_revoke_locked(struct key *key)
121 {
122         set_bit(KEY_FLAG_REVOKED, &key->flags);
123 }
124
125 static void ctx_upcall_timeout_kr(unsigned long data)
126 {
127         struct ptlrpc_cli_ctx *ctx = (struct ptlrpc_cli_ctx *) data;
128         struct key            *key = ctx2gctx_keyring(ctx)->gck_key;
129
130         CWARN("ctx %p, key %p\n", ctx, key);
131
132         LASSERT(key);
133
134         cli_ctx_expire(ctx);
135         key_revoke_locked(key);
136         sptlrpc_cli_ctx_wakeup(ctx);
137 }
138
139 static
140 void ctx_start_timer_kr(struct ptlrpc_cli_ctx *ctx, long timeout)
141 {
142         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
143         struct timer_list          *timer = gctx_kr->gck_timer;
144
145         LASSERT(timer);
146
147         CDEBUG(D_SEC, "ctx %p: start timer %lds\n", ctx, timeout);
148         timeout = timeout * HZ + cfs_time_current();
149
150         init_timer(timer);
151         timer->expires = timeout;
152         timer->data = (unsigned long ) ctx;
153         timer->function = ctx_upcall_timeout_kr;
154
155         add_timer(timer);
156 }
157
158 /*
159  * caller should make sure no race with other threads
160  */
161 static
162 void ctx_clear_timer_kr(struct ptlrpc_cli_ctx *ctx)
163 {
164         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
165         struct timer_list          *timer = gctx_kr->gck_timer;
166
167         if (timer == NULL)
168                 return;
169
170         CDEBUG(D_SEC, "ctx %p, key %p\n", ctx, gctx_kr->gck_key);
171
172         gctx_kr->gck_timer = NULL;
173
174         del_singleshot_timer_sync(timer);
175
176         OBD_FREE_PTR(timer);
177 }
178
179 static
180 struct ptlrpc_cli_ctx *ctx_create_kr(struct ptlrpc_sec *sec,
181                                      struct vfs_cred *vcred)
182 {
183         struct ptlrpc_cli_ctx      *ctx;
184         struct gss_cli_ctx_keyring *gctx_kr;
185
186         OBD_ALLOC_PTR(gctx_kr);
187         if (gctx_kr == NULL)
188                 return NULL;
189
190         OBD_ALLOC_PTR(gctx_kr->gck_timer);
191         if (gctx_kr->gck_timer == NULL) {
192                 OBD_FREE_PTR(gctx_kr);
193                 return NULL;
194         }
195         init_timer(gctx_kr->gck_timer);
196
197         ctx = &gctx_kr->gck_base.gc_base;
198
199         if (gss_cli_ctx_init_common(sec, ctx, &gss_keyring_ctxops, vcred)) {
200                 OBD_FREE_PTR(gctx_kr->gck_timer);
201                 OBD_FREE_PTR(gctx_kr);
202                 return NULL;
203         }
204
205         ctx->cc_expire = cfs_time_current_sec() + KEYRING_UPCALL_TIMEOUT;
206         clear_bit(PTLRPC_CTX_NEW_BIT, &ctx->cc_flags);
207         atomic_inc(&ctx->cc_refcount); /* for the caller */
208
209         return ctx;
210 }
211
212 static void ctx_destroy_kr(struct ptlrpc_cli_ctx *ctx)
213 {
214         struct ptlrpc_sec          *sec = ctx->cc_sec;
215         struct gss_cli_ctx_keyring *gctx_kr = ctx2gctx_keyring(ctx);
216         int                         rc;
217
218         CDEBUG(D_SEC, "destroying ctx %p\n", ctx);
219
220         /* at this time the association with key has been broken. */
221         LASSERT(sec);
222         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
223         LASSERT(gctx_kr->gck_key == NULL);
224
225         ctx_clear_timer_kr(ctx);
226         LASSERT(gctx_kr->gck_timer == NULL);
227
228         rc = gss_cli_ctx_fini_common(sec, ctx);
229         if (rc < 0)
230                 return;
231
232         OBD_FREE_PTR(gctx_kr);
233
234         if (rc > 0) {
235                 CWARN("released the last ctx, proceed to destroy sec %s@%p\n",
236                       sec->ps_policy->sp_name, sec);
237                 sptlrpc_sec_destroy(sec);
238         }
239 }
240
241 static void ctx_put_kr(struct ptlrpc_cli_ctx *ctx)
242 {
243         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
244
245         if (atomic_dec_and_test(&ctx->cc_refcount))
246                 ctx_destroy_kr(ctx);
247 }
248
249 /*
250  * key <-> ctx association and rules:
251  * - ctx might not bind with any key
252  * - key/ctx binding is protected by key semaphore (if the key present)
253  * - key and ctx each take a reference of the other
254  * - ctx enlist/unlist is protected by ctx spinlock
255  * - never enlist a ctx after it's been unlisted
256  * - whoever do enlist should also do bind, lock key before enlist:
257  *   - lock key -> lock ctx -> enlist -> unlock ctx -> bind -> unlock key
258  * - whoever do unlist should also do unbind:
259  *   - lock key -> lock ctx -> unlist -> unlock ctx -> unbind -> unlock key
260  *   - lock ctx -> unlist -> unlock ctx -> lock key -> unbind -> unlock key
261  */
262
263 static inline void spin_lock_if(spinlock_t *lock, int condition)
264 {
265         if (condition)
266                 spin_lock(lock);
267 }
268
269 static inline void spin_unlock_if(spinlock_t *lock, int condition)
270 {
271         if (condition)
272                 spin_unlock(lock);
273 }
274
275 static
276 void ctx_enlist_kr(struct ptlrpc_cli_ctx *ctx, int is_root, int locked)
277 {
278         struct ptlrpc_sec      *sec = ctx->cc_sec;
279         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
280
281         LASSERT(!test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags));
282         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
283
284         spin_lock_if(&sec->ps_lock, !locked);
285
286         atomic_inc(&ctx->cc_refcount);
287         set_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags);
288         hlist_add_head(&ctx->cc_cache, &gsec_kr->gsk_clist);
289         if (is_root)
290                 gsec_kr->gsk_root_ctx = ctx;
291
292         spin_unlock_if(&sec->ps_lock, !locked);
293 }
294
295 /*
296  * Note after this get called, caller should not access ctx again because
297  * it might have been freed, unless caller hold at least one refcount of
298  * the ctx.
299  *
300  * return non-zero if we indeed unlist this ctx.
301  */
302 static
303 int ctx_unlist_kr(struct ptlrpc_cli_ctx *ctx, int locked)
304 {
305         struct ptlrpc_sec       *sec = ctx->cc_sec;
306         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
307
308         /* if hashed bit has gone, leave the job to somebody who is doing it */
309         if (test_and_clear_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0)
310                 return 0;
311
312         /* drop ref inside spin lock to prevent race with other operations */
313         spin_lock_if(&sec->ps_lock, !locked);
314
315         if (gsec_kr->gsk_root_ctx == ctx)
316                 gsec_kr->gsk_root_ctx = NULL;
317         hlist_del_init(&ctx->cc_cache);
318         atomic_dec(&ctx->cc_refcount);
319
320         spin_unlock_if(&sec->ps_lock, !locked);
321
322         return 1;
323 }
324
325 /*
326  * bind a key with a ctx together.
327  * caller must hold write lock of the key, as well as ref on key & ctx.
328  */
329 static
330 void bind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
331 {
332         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
333         LASSERT(atomic_read(&key->usage) > 0);
334         LASSERT(ctx2gctx_keyring(ctx)->gck_key == NULL);
335         LASSERT(key->payload.data == NULL);
336
337         /* at this time context may or may not in list. */
338         key_get(key);
339         atomic_inc(&ctx->cc_refcount);
340         ctx2gctx_keyring(ctx)->gck_key = key;
341         key->payload.data = ctx;
342 }
343
344 /*
345  * unbind a key and a ctx.
346  * caller must hold write lock, as well as a ref of the key.
347  */
348 static
349 void unbind_key_ctx(struct key *key, struct ptlrpc_cli_ctx *ctx)
350 {
351         LASSERT(key->payload.data == ctx);
352         LASSERT(test_bit(PTLRPC_CTX_CACHED_BIT, &ctx->cc_flags) == 0);
353
354         /* must revoke the key, or others may treat it as newly created */
355         key_revoke_locked(key);
356
357         key->payload.data = NULL;
358         ctx2gctx_keyring(ctx)->gck_key = NULL;
359
360         /* once ctx get split from key, the timer is meaningless */
361         ctx_clear_timer_kr(ctx);
362
363         ctx_put_kr(ctx);
364         key_put(key);
365 }
366
367 /*
368  * given a ctx, unbind with its coupled key, if any.
369  * unbind could only be called once, so we don't worry the key be released
370  * by someone else.
371  */
372 static void unbind_ctx_kr(struct ptlrpc_cli_ctx *ctx)
373 {
374         struct key      *key = ctx2gctx_keyring(ctx)->gck_key;
375
376         if (key) {
377                 LASSERT(key->payload.data == ctx);
378
379                 key_get(key);
380                 down_write(&key->sem);
381                 unbind_key_ctx(key, ctx);
382                 up_write(&key->sem);
383                 key_put(key);
384         }
385 }
386
387 /*
388  * given a key, unbind with its coupled ctx, if any.
389  * caller must hold write lock, as well as a ref of the key.
390  */
391 static void unbind_key_locked(struct key *key)
392 {
393         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
394
395         if (ctx)
396                 unbind_key_ctx(key, ctx);
397 }
398
399 /*
400  * unlist a ctx, and unbind from coupled key
401  */
402 static void kill_ctx_kr(struct ptlrpc_cli_ctx *ctx)
403 {
404         if (ctx_unlist_kr(ctx, 0))
405                 unbind_ctx_kr(ctx);
406 }
407
408 /*
409  * given a key, unlist and unbind with the coupled ctx (if any).
410  * caller must hold write lock, as well as a ref of the key.
411  */
412 static void kill_key_locked(struct key *key)
413 {
414         struct ptlrpc_cli_ctx *ctx = key->payload.data;
415
416         if (ctx && ctx_unlist_kr(ctx, 0))
417                 unbind_key_locked(key);
418 }
419
420 /*
421  * caller should hold one ref on contexts in freelist.
422  */
423 static void dispose_ctx_list_kr(struct hlist_head *freelist)
424 {
425         struct hlist_node      *pos, *next;
426         struct ptlrpc_cli_ctx  *ctx;
427
428         hlist_for_each_entry_safe(ctx, pos, next, freelist, cc_cache) {
429                 hlist_del_init(&ctx->cc_cache);
430
431                 /* we need to wakeup waiting reqs here. the context might
432                  * be forced released before upcall finished, then the
433                  * late-arrived downcall can't find the ctx even. */
434                 sptlrpc_cli_ctx_wakeup(ctx);
435
436                 unbind_ctx_kr(ctx);
437                 ctx_put_kr(ctx);
438         }
439 }
440
441 /*
442  * lookup a root context directly in a sec, return root ctx with a
443  * reference taken or NULL.
444  */
445 static
446 struct ptlrpc_cli_ctx * sec_lookup_root_ctx_kr(struct ptlrpc_sec *sec)
447 {
448         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
449         struct ptlrpc_cli_ctx   *ctx = NULL;
450
451         spin_lock(&sec->ps_lock);
452
453         ctx = gsec_kr->gsk_root_ctx;
454
455         if (ctx == NULL && unlikely(sec_is_reverse(sec))) {
456                 struct hlist_node      *node;
457                 struct ptlrpc_cli_ctx  *tmp;
458
459                 /* reverse ctx, search root ctx in list, choose the one
460                  * with shortest expire time, which is most possibly have
461                  * an established peer ctx at client side. */
462                 hlist_for_each_entry(tmp, node, &gsec_kr->gsk_clist, cc_cache) {
463                         if (ctx == NULL || ctx->cc_expire == 0 ||
464                             ctx->cc_expire > tmp->cc_expire) {
465                                 ctx = tmp;
466                                 /* promote to be root_ctx */
467                                 gsec_kr->gsk_root_ctx = ctx;
468                         }
469                 }
470         }
471
472         if (ctx) {
473                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
474                 LASSERT(!hlist_empty(&gsec_kr->gsk_clist));
475                 atomic_inc(&ctx->cc_refcount);
476         }
477
478         spin_unlock(&sec->ps_lock);
479
480         return ctx;
481 }
482
483 #define RVS_CTX_EXPIRE_NICE    (10)
484
485 static
486 void rvs_sec_install_root_ctx_kr(struct ptlrpc_sec *sec,
487                                  struct ptlrpc_cli_ctx *new_ctx,
488                                  struct key *key)
489 {
490         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
491         struct hlist_node      *hnode;
492         struct ptlrpc_cli_ctx  *ctx;
493         cfs_time_t              now;
494         ENTRY;
495
496         LASSERT(sec_is_reverse(sec));
497
498         spin_lock(&sec->ps_lock);
499
500         now = cfs_time_current_sec();
501
502         /* set all existing ctxs short expiry */
503         hlist_for_each_entry(ctx, hnode, &gsec_kr->gsk_clist, cc_cache) {
504                 if (ctx->cc_expire > now + RVS_CTX_EXPIRE_NICE) {
505                         ctx->cc_early_expire = 1;
506                         ctx->cc_expire = now + RVS_CTX_EXPIRE_NICE;
507                 }
508         }
509
510         /* if there's root_ctx there, instead obsolete the current
511          * immediately, we leave it continue operating for a little while.
512          * hopefully when the first backward rpc with newest ctx send out,
513          * the client side already have the peer ctx well established. */
514         ctx_enlist_kr(new_ctx, gsec_kr->gsk_root_ctx ? 0 : 1, 1);
515
516         if (key)
517                 bind_key_ctx(key, new_ctx);
518
519         spin_unlock(&sec->ps_lock);
520 }
521
522 static void construct_key_desc(void *buf, int bufsize,
523                                struct ptlrpc_sec *sec, uid_t uid)
524 {
525         snprintf(buf, bufsize, "%d@%x", uid, sec2gsec_keyring(sec)->gsk_id);
526         ((char *)buf)[bufsize - 1] = '\0';
527 }
528
529 /****************************************
530  * sec apis                             *
531  ****************************************/
532
533 static atomic_t gss_sec_id_kr = ATOMIC_INIT(0);
534
535 static
536 struct ptlrpc_sec * gss_sec_create_kr(struct obd_import *imp,
537                                       struct ptlrpc_svc_ctx *ctx,
538                                       __u32 flavor,
539                                       unsigned long flags)
540 {
541         struct gss_sec_keyring  *gsec_kr;
542         ENTRY;
543
544         OBD_ALLOC(gsec_kr, sizeof(*gsec_kr));
545         if (gsec_kr == NULL)
546                 RETURN(NULL);
547
548         gsec_kr->gsk_id = atomic_inc_return(&gss_sec_id_kr);
549         CFS_INIT_HLIST_HEAD(&gsec_kr->gsk_clist);
550         gsec_kr->gsk_root_ctx = NULL;
551         mutex_init(&gsec_kr->gsk_root_uc_lock);
552 #ifdef HAVE_KEYRING_UPCALL_SERIALIZED
553         mutex_init(&gsec_kr->gsk_uc_lock);
554 #endif
555
556         if (gss_sec_create_common(&gsec_kr->gsk_base, &gss_policy_keyring,
557                                   imp, ctx, flavor, flags))
558                 goto err_free;
559
560         if (ctx != NULL) {
561                 if (sec_install_rctx_kr(&gsec_kr->gsk_base.gs_base, ctx)) {
562                         gss_sec_destroy_common(&gsec_kr->gsk_base);
563                         goto err_free;
564                 }
565         }
566
567         RETURN(&gsec_kr->gsk_base.gs_base);
568
569 err_free:
570         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
571         RETURN(NULL);
572 }
573
574 static
575 void gss_sec_destroy_kr(struct ptlrpc_sec *sec)
576 {
577         struct gss_sec          *gsec = sec2gsec(sec);
578         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
579
580         CDEBUG(D_SEC, "destroy %s@%p\n", sec->ps_policy->sp_name, sec);
581
582         LASSERT(hlist_empty(&gsec_kr->gsk_clist));
583         LASSERT(gsec_kr->gsk_root_ctx == NULL);
584
585         gss_sec_destroy_common(gsec);
586
587         OBD_FREE(gsec_kr, sizeof(*gsec_kr));
588 }
589
590 static
591 int user_is_root(struct ptlrpc_sec *sec, struct vfs_cred *vcred)
592 {
593         if (sec->ps_flags & PTLRPC_SEC_FL_ROOTONLY)
594                 return 1;
595
596         /* FIXME
597          * more precisely deal with setuid. maybe add more infomation
598          * into vfs_cred ?? */
599         return (vcred->vc_uid == 0);
600 }
601
602 /*
603  * unlink request key from it's ring, which is linked during request_key().
604  * sadly, we have to 'guess' which keyring it's linked to.
605  *
606  * FIXME this code is fragile, depend on how request_key_link() is implemented.
607  */
608 static void request_key_unlink(struct key *key)
609 {
610         struct task_struct *tsk = current;
611         struct key *ring;
612
613         switch (task_aux(tsk)->jit_keyring) {
614         case KEY_REQKEY_DEFL_DEFAULT:
615         case KEY_REQKEY_DEFL_THREAD_KEYRING:
616                 ring = key_get(task_aux(tsk)->thread_keyring);
617                 if (ring)
618                         break;
619         case KEY_REQKEY_DEFL_PROCESS_KEYRING:
620                 ring = key_get(tsk->signal->process_keyring);
621                 if (ring)
622                         break;
623         case KEY_REQKEY_DEFL_SESSION_KEYRING:
624                 rcu_read_lock();
625                 ring = key_get(rcu_dereference(tsk->signal->session_keyring));
626                 rcu_read_unlock();
627                 if (ring)
628                         break;
629         case KEY_REQKEY_DEFL_USER_SESSION_KEYRING:
630                 ring = key_get(tsk->user->session_keyring);
631                 break;
632         case KEY_REQKEY_DEFL_USER_KEYRING:
633                 ring = key_get(tsk->user->uid_keyring);
634                 break;
635         case KEY_REQKEY_DEFL_GROUP_KEYRING:
636         default:
637                 LBUG();
638         }
639
640         LASSERT(ring);
641         key_unlink(ring, key);
642         key_put(ring);
643 }
644
645 static
646 struct ptlrpc_cli_ctx * gss_sec_lookup_ctx_kr(struct ptlrpc_sec *sec,
647                                               struct vfs_cred *vcred,
648                                               int create, int remove_dead)
649 {
650         struct obd_import       *imp = sec->ps_import;
651         struct gss_sec_keyring  *gsec_kr = sec2gsec_keyring(sec);
652         struct ptlrpc_cli_ctx   *ctx = NULL;
653         unsigned int             is_root = 0, create_new = 0;
654         struct key              *key;
655         char                     desc[24];
656         char                    *coinfo;
657         const int                coinfo_size = sizeof(struct obd_uuid) + 64;
658         char                    *co_flags = "";
659         ENTRY;
660
661         LASSERT(imp != NULL);
662
663         is_root = user_is_root(sec, vcred);
664
665         /* a little bit optimization for root context */
666         if (is_root) {
667                 ctx = sec_lookup_root_ctx_kr(sec);
668                 /*
669                  * Only lookup directly for REVERSE sec, which should
670                  * always succeed.
671                  */
672                 if (ctx || sec_is_reverse(sec))
673                         RETURN(ctx);
674         }
675
676         LASSERT(create != 0);
677
678         /* for root context, obtain lock and check again, this time hold
679          * the root upcall lock, make sure nobody else populated new root
680          * context after last check. */
681         if (is_root) {
682                 mutex_lock(&gsec_kr->gsk_root_uc_lock);
683
684                 ctx = sec_lookup_root_ctx_kr(sec);
685                 if (ctx)
686                         goto out;
687
688                 /* update reverse handle for root user */
689                 sec2gsec(sec)->gs_rvs_hdl = gss_get_next_ctx_index();
690
691                 co_flags = "r";
692         }
693
694         construct_key_desc(desc, sizeof(desc), sec, vcred->vc_uid);
695
696         /* callout info: mech:flags:svc_type:peer_nid:target_uuid */
697         OBD_ALLOC(coinfo, coinfo_size);
698         if (coinfo == NULL)
699                 goto out;
700
701         snprintf(coinfo, coinfo_size, "%s:%s:%d:"LPX64":%s",
702                  sec2gsec(sec)->gs_mech->gm_name,
703                  co_flags, import_to_gss_svc(imp),
704                  imp->imp_connection->c_peer.nid, imp->imp_obd->obd_name);
705
706         keyring_upcall_lock(gsec_kr);
707         key = request_key(&gss_key_type, desc, coinfo);
708         keyring_upcall_unlock(gsec_kr);
709
710         OBD_FREE(coinfo, coinfo_size);
711
712         if (IS_ERR(key)) {
713                 CERROR("failed request key: %ld\n", PTR_ERR(key));
714                 goto out;
715         }
716
717         /* once payload.data was pointed to a ctx, it never changes until
718          * we de-associate them; but parallel request_key() may return
719          * a key with payload.data == NULL at the same time. so we still
720          * need wirtelock of key->sem to serialize them. */
721         down_write(&key->sem);
722
723         if (likely(key->payload.data != NULL)) {
724                 ctx = key->payload.data;
725
726                 LASSERT(atomic_read(&ctx->cc_refcount) >= 1);
727                 LASSERT(ctx2gctx_keyring(ctx)->gck_key == key);
728                 LASSERT(atomic_read(&key->usage) >= 2);
729
730                 /* simply take a ref and return. it's upper layer's
731                  * responsibility to detect & replace dead ctx. */
732                 atomic_inc(&ctx->cc_refcount);
733         } else {
734                 /* pre initialization with a cli_ctx. this can't be done in
735                  * key_instantiate() because we'v no enough information
736                  * there. */
737                 ctx = ctx_create_kr(sec, vcred);
738                 if (ctx != NULL) {
739                         ctx_enlist_kr(ctx, is_root, 0);
740                         bind_key_ctx(key, ctx);
741
742                         ctx_start_timer_kr(ctx, KEYRING_UPCALL_TIMEOUT);
743
744                         CDEBUG(D_SEC, "installed key %p <-> ctx %p (sec %p)\n",
745                                key, ctx, sec);
746                 } else {
747                         /* we'd prefer to call key_revoke(), but we more like
748                          * to revoke it within this key->sem locked period. */
749                         key_revoke_locked(key);
750                 }
751
752                 create_new = 1;
753         }
754
755         up_write(&key->sem);
756
757         if (is_root && create_new)
758                 request_key_unlink(key);
759
760         key_put(key);
761 out:
762         if (is_root)
763                 mutex_unlock(&gsec_kr->gsk_root_uc_lock);
764         RETURN(ctx);
765 }
766
767 static
768 void gss_sec_release_ctx_kr(struct ptlrpc_sec *sec,
769                             struct ptlrpc_cli_ctx *ctx,
770                             int sync)
771 {
772         LASSERT(atomic_read(&ctx->cc_refcount) == 0);
773
774         if (sync) {
775                 ctx_destroy_kr(ctx);
776         } else {
777                 atomic_inc(&ctx->cc_refcount);
778                 sptlrpc_gc_add_ctx(ctx);
779         }
780 }
781
782 /*
783  * flush context of normal user, we must resort to keyring itself to find out
784  * contexts which belong to me.
785  *
786  * Note here we suppose only to flush _my_ context, the "uid" will
787  * be ignored in the search.
788  */
789 static
790 void flush_user_ctx_cache_kr(struct ptlrpc_sec *sec,
791                              uid_t uid,
792                              int grace, int force)
793 {
794         struct key              *key;
795         char                     desc[24];
796
797         /* nothing to do for reverse or rootonly sec */
798         if (sec_is_reverse(sec) || sec_is_rootonly(sec))
799                 return;
800
801         construct_key_desc(desc, sizeof(desc), sec, uid);
802
803         /* there should be only one valid key, but we put it in the
804          * loop in case of any weird cases */
805         for (;;) {
806                 key = request_key(&gss_key_type, desc, NULL);
807                 if (IS_ERR(key)) {
808                         CWARN("No more key found for current user\n");
809                         break;
810                 }
811
812                 down_write(&key->sem);
813
814                 kill_key_locked(key);
815
816                 /* kill_key_locked() should usually revoke the key, but we
817                  * revoke it again to make sure, e.g. some case the key may
818                  * not well coupled with a context. */
819                 key_revoke_locked(key);
820
821                 up_write(&key->sem);
822
823                 key_put(key);
824         }
825 }
826
827 /*
828  * flush context of root or all, we iterate through the list.
829  */
830 static
831 void flush_spec_ctx_cache_kr(struct ptlrpc_sec *sec,
832                              uid_t uid,
833                              int grace, int force)
834 {
835         struct gss_sec_keyring *gsec_kr;
836         struct hlist_head       freelist = CFS_HLIST_HEAD_INIT;
837         struct hlist_node      *pos, *next;
838         struct ptlrpc_cli_ctx  *ctx;
839         ENTRY;
840
841         gsec_kr = sec2gsec_keyring(sec);
842
843         spin_lock(&sec->ps_lock);
844         hlist_for_each_entry_safe(ctx, pos, next,
845                                   &gsec_kr->gsk_clist, cc_cache) {
846                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
847
848                 if (uid != -1 && uid != ctx->cc_vcred.vc_uid)
849                         continue;
850
851                 /* at this moment there's at least 2 base reference:
852                  * key association and in-list. */
853                 if (atomic_read(&ctx->cc_refcount) > 2) {
854                         if (!force)
855                                 continue;
856                         CWARN("flush busy ctx %p(%u->%s, extra ref %d)\n",
857                               ctx, ctx->cc_vcred.vc_uid,
858                               sec2target_str(ctx->cc_sec),
859                               atomic_read(&ctx->cc_refcount) - 2);
860                 }
861
862                 set_bit(PTLRPC_CTX_DEAD_BIT, &ctx->cc_flags);
863                 if (!grace)
864                         clear_bit(PTLRPC_CTX_UPTODATE_BIT, &ctx->cc_flags);
865
866                 atomic_inc(&ctx->cc_refcount);
867
868                 if (ctx_unlist_kr(ctx, 1))
869                         hlist_add_head(&ctx->cc_cache, &freelist);
870                 else {
871                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
872                         atomic_dec(&ctx->cc_refcount);
873                 }
874
875         }
876         spin_unlock(&sec->ps_lock);
877
878         dispose_ctx_list_kr(&freelist);
879         EXIT;
880 }
881
882 static
883 int gss_sec_flush_ctx_cache_kr(struct ptlrpc_sec *sec,
884                                uid_t uid,
885                                int grace, int force)
886 {
887         ENTRY;
888
889         CDEBUG(D_SEC, "sec %p(%d, busy %d), uid %d, grace %d, force %d\n",
890                sec, atomic_read(&sec->ps_refcount), atomic_read(&sec->ps_busy),
891                uid, grace, force);
892
893         if (uid != -1 && uid != 0)
894                 flush_user_ctx_cache_kr(sec, uid, grace, force);
895         else
896                 flush_spec_ctx_cache_kr(sec, uid, grace, force);
897
898         RETURN(0);
899 }
900
901 static
902 void gss_sec_gc_ctx_kr(struct ptlrpc_sec *sec)
903 {
904         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
905         struct hlist_head       freelist = CFS_HLIST_HEAD_INIT;
906         struct hlist_node      *pos, *next;
907         struct ptlrpc_cli_ctx  *ctx;
908         ENTRY;
909
910         CWARN("running gc\n");
911
912         spin_lock(&sec->ps_lock);
913         hlist_for_each_entry_safe(ctx, pos, next,
914                                   &gsec_kr->gsk_clist, cc_cache) {
915                 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
916
917                 atomic_inc(&ctx->cc_refcount);
918
919                 if (cli_ctx_check_death(ctx) && ctx_unlist_kr(ctx, 1)) {
920                         hlist_add_head(&ctx->cc_cache, &freelist);
921                         CWARN("unhashed ctx %p\n", ctx);
922                 } else {
923                         LASSERT(atomic_read(&ctx->cc_refcount) >= 2);
924                         atomic_dec(&ctx->cc_refcount);
925                 }
926         }
927         spin_unlock(&sec->ps_lock);
928
929         dispose_ctx_list_kr(&freelist);
930         EXIT;
931         return;
932 }
933
934 static
935 int gss_sec_display_kr(struct ptlrpc_sec *sec, char *buf, int bufsize)
936 {
937         struct gss_sec_keyring *gsec_kr = sec2gsec_keyring(sec);
938         struct hlist_node      *pos, *next;
939         struct ptlrpc_cli_ctx  *ctx;
940         int                     written = 0;
941         ENTRY;
942
943         written = snprintf(buf, bufsize, "context list ===>\n");
944         bufsize -= written;
945         buf += written;
946
947         spin_lock(&sec->ps_lock);
948         hlist_for_each_entry_safe(ctx, pos, next,
949                                   &gsec_kr->gsk_clist, cc_cache) {
950                 struct gss_cli_ctx     *gctx;
951                 struct key             *key;
952                 char                    flags_str[40];
953                 int                     len;
954
955                 gctx = ctx2gctx(ctx);
956                 key = ctx2gctx_keyring(ctx)->gck_key;
957
958                 gss_cli_ctx_flags2str(ctx->cc_flags,
959                                       flags_str, sizeof(flags_str));
960
961                 len = snprintf(buf, bufsize, "%p(%d): uid %u, exp %ld(%ld)s, "
962                                "fl %s, seq %d, win %u, key %08x(%d), ",
963                                ctx, atomic_read(&ctx->cc_refcount),
964                                ctx->cc_vcred.vc_uid,
965                                ctx->cc_expire,
966                                ctx->cc_expire - cfs_time_current_sec(),
967                                flags_str,
968                                atomic_read(&gctx->gc_seq),
969                                gctx->gc_win,
970                                key ? key->serial : 0,
971                                key ? atomic_read(&key->usage) : 0);
972
973                 written += len;
974                 buf += len;
975                 bufsize -= len;
976
977                 if (bufsize <= 0)
978                         break;
979
980                 if (gctx->gc_mechctx)
981                         len = lgss_display(gctx->gc_mechctx, buf, bufsize);
982                 else
983                         len = snprintf(buf, bufsize, "mech N/A\n");
984
985                 written += len;
986                 buf += len;
987                 bufsize -= len;
988
989                 if (bufsize <= 0)
990                         break;
991         }
992         spin_unlock(&sec->ps_lock);
993
994         RETURN(written);
995 }
996
997 /****************************************
998  * cli_ctx apis                         *
999  ****************************************/
1000
1001 static
1002 int gss_cli_ctx_refresh_kr(struct ptlrpc_cli_ctx *ctx)
1003 {
1004         /* upcall is already on the way */
1005         return 0;
1006 }
1007
1008 static
1009 int gss_cli_ctx_validate_kr(struct ptlrpc_cli_ctx *ctx)
1010 {
1011         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1012         LASSERT(ctx->cc_sec);
1013
1014         if (cli_ctx_check_death(ctx)) {
1015                 kill_ctx_kr(ctx);
1016                 return 1;
1017         }
1018
1019         if (cli_ctx_is_ready(ctx))
1020                 return 0;
1021         return 1;
1022 }
1023
1024 static
1025 void gss_cli_ctx_die_kr(struct ptlrpc_cli_ctx *ctx, int grace)
1026 {
1027         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1028         LASSERT(ctx->cc_sec);
1029
1030         CWARN("ctx %p(%d)\n", ctx, atomic_read(&ctx->cc_refcount));
1031         cli_ctx_expire(ctx);
1032         kill_ctx_kr(ctx);
1033 }
1034
1035 /****************************************
1036  * (reverse) service                    *
1037  ****************************************/
1038
1039 /*
1040  * reverse context could have nothing to do with keyrings. here we still keep
1041  * the version which bind to a key, for future reference.
1042  */
1043 #define HAVE_REVERSE_CTX_NOKEY
1044
1045 #ifdef HAVE_REVERSE_CTX_NOKEY
1046
1047 static
1048 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1049                         struct ptlrpc_svc_ctx *svc_ctx)
1050 {
1051         struct ptlrpc_cli_ctx   *cli_ctx;
1052         struct vfs_cred          vcred = { 0, 0 };
1053         int                      rc;
1054
1055         LASSERT(sec);
1056         LASSERT(svc_ctx);
1057
1058         cli_ctx = ctx_create_kr(sec, &vcred);
1059         if (cli_ctx == NULL)
1060                 return -ENOMEM;
1061
1062         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1063         if (rc) {
1064                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1065
1066                 ctx_put_kr(cli_ctx);
1067                 return rc;
1068         }
1069
1070         rvs_sec_install_root_ctx_kr(sec, cli_ctx, NULL);
1071
1072         ctx_put_kr(cli_ctx);
1073
1074         return 0;
1075 }
1076
1077 #else /* ! HAVE_REVERSE_CTX_NOKEY */
1078
1079 static
1080 int sec_install_rctx_kr(struct ptlrpc_sec *sec,
1081                         struct ptlrpc_svc_ctx *svc_ctx)
1082 {
1083         struct ptlrpc_cli_ctx   *cli_ctx = NULL;
1084         struct key              *key;
1085         struct vfs_cred          vcred = { 0, 0 };
1086         char                     desc[64];
1087         int                      rc;
1088
1089         LASSERT(sec);
1090         LASSERT(svc_ctx);
1091         CWARN("called\n");
1092
1093         construct_key_desc(desc, sizeof(desc), sec, 0);
1094
1095         key = key_alloc(&gss_key_type, desc, 0, 0,
1096                         KEY_POS_ALL | KEY_USR_ALL, 1);
1097         if (IS_ERR(key)) {
1098                 CERROR("failed to alloc key: %ld\n", PTR_ERR(key));
1099                 return PTR_ERR(key);
1100         }
1101
1102         rc = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
1103         if (rc) {
1104                 CERROR("failed to instantiate key: %d\n", rc);
1105                 goto err_revoke;
1106         }
1107
1108         down_write(&key->sem);
1109
1110         LASSERT(key->payload.data == NULL);
1111
1112         cli_ctx = ctx_create_kr(sec, &vcred);
1113         if (cli_ctx == NULL) {
1114                 rc = -ENOMEM;
1115                 goto err_up;
1116         }
1117
1118         rc = gss_copy_rvc_cli_ctx(cli_ctx, svc_ctx);
1119         if (rc) {
1120                 CERROR("failed copy reverse cli ctx: %d\n", rc);
1121                 goto err_put;
1122         }
1123
1124         rvs_sec_install_root_ctx_kr(sec, cli_ctx, key);
1125
1126         ctx_put_kr(cli_ctx);
1127         up_write(&key->sem);
1128
1129         rc = 0;
1130         CWARN("ok!\n");
1131 out:
1132         key_put(key);
1133         return rc;
1134
1135 err_put:
1136         ctx_put_kr(cli_ctx);
1137 err_up:
1138         up_write(&key->sem);
1139 err_revoke:
1140         key_revoke(key);
1141         goto out;
1142 }
1143
1144 #endif /* HAVE_REVERSE_CTX_NOKEY */
1145
1146 /****************************************
1147  * service apis                         *
1148  ****************************************/
1149
1150 static
1151 int gss_svc_accept_kr(struct ptlrpc_request *req)
1152 {
1153         return gss_svc_accept(&gss_policy_keyring, req);
1154 }
1155
1156 static
1157 int gss_svc_install_rctx_kr(struct obd_import *imp,
1158                             struct ptlrpc_svc_ctx *svc_ctx)
1159 {
1160         LASSERT(imp->imp_sec);
1161
1162         return sec_install_rctx_kr(imp->imp_sec, svc_ctx);
1163 }
1164
1165 /****************************************
1166  * key apis                             *
1167  ****************************************/
1168
1169 static
1170 int gss_kt_instantiate(struct key *key, const void *data, size_t datalen)
1171 {
1172         ENTRY;
1173
1174         if (data != NULL || datalen != 0) {
1175                 CERROR("invalid: data %p, len %d\n", data, datalen);
1176                 RETURN(-EINVAL);
1177         }
1178
1179         if (key->payload.data != 0) {
1180                 CERROR("key already have payload\n");
1181                 RETURN(-EINVAL);
1182         }
1183
1184         /* XXX */
1185         key->perm |= KEY_POS_ALL | KEY_USR_ALL;
1186         CDEBUG(D_SEC, "key %p instantiated, ctx %p\n", key, key->payload.data);
1187         RETURN(0);
1188 }
1189
1190 /*
1191  * called with key semaphore write locked. it means we can operate
1192  * on the context without fear of loosing refcount.
1193  */
1194 static
1195 int gss_kt_update(struct key *key, const void *data, size_t datalen)
1196 {
1197         struct ptlrpc_cli_ctx   *ctx = key->payload.data;
1198         struct gss_cli_ctx      *gctx;
1199         rawobj_t                 tmpobj = RAWOBJ_EMPTY;
1200         int                      rc;
1201         ENTRY;
1202
1203         if (data == NULL || datalen == 0) {
1204                 CWARN("invalid: data %p, len %d\n", data, datalen);
1205                 RETURN(-EINVAL);
1206         }
1207
1208         /* there's a race between userspace parent - child processes. if
1209          * child finish negotiation too fast and call kt_update(), the ctx
1210          * might be still NULL. but the key will finally be associate
1211          * with a context, or be revoked. if key status is fine, return
1212          * -EAGAIN to allow userspace sleep a while and call again. */
1213         if (ctx == NULL) {
1214                 CWARN("race in userspace. key %p(%x) flags %lx\n",
1215                       key, key->serial, key->flags);
1216
1217                 rc = key_validate(key);
1218                 if (rc == 0)
1219                         RETURN(-EAGAIN);
1220                 else
1221                         RETURN(rc);
1222         }
1223
1224         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
1225         LASSERT(ctx->cc_sec);
1226
1227         ctx_clear_timer_kr(ctx);
1228
1229         /* don't proceed if already refreshed */
1230         if (cli_ctx_is_refreshed(ctx)) {
1231                 CWARN("ctx already done refresh\n");
1232                 sptlrpc_cli_ctx_wakeup(ctx);
1233                 RETURN(0);
1234         }
1235
1236         sptlrpc_cli_ctx_get(ctx);
1237         gctx = ctx2gctx(ctx);
1238
1239         rc = buffer_extract_bytes(&data, &datalen, &gctx->gc_win,
1240                                   sizeof(gctx->gc_win));
1241         if (rc) {
1242                 CERROR("failed extract seq_win\n");
1243                 goto out;
1244         }
1245
1246         if (gctx->gc_win == 0) {
1247                 __u32   nego_rpc_err, nego_gss_err;
1248
1249                 rc = buffer_extract_bytes(&data, &datalen, &nego_rpc_err,
1250                                           sizeof(nego_rpc_err));
1251                 if (rc) {
1252                         CERROR("failed to extrace rpc rc\n");
1253                         goto out;
1254                 }
1255
1256                 rc = buffer_extract_bytes(&data, &datalen, &nego_gss_err,
1257                                           sizeof(nego_gss_err));
1258                 if (rc) {
1259                         CERROR("failed to extrace gss rc\n");
1260                         goto out;
1261                 }
1262
1263                 CERROR("negotiation: rpc err %d, gss err %x\n",
1264                        nego_rpc_err, nego_gss_err);
1265
1266                 rc = nego_rpc_err ? nego_rpc_err : -EACCES;
1267         } else {
1268                 rc = rawobj_extract_local_alloc(&gctx->gc_handle,
1269                                                 (__u32 **) &data, &datalen);
1270                 if (rc) {
1271                         CERROR("failed extract handle\n");
1272                         goto out;
1273                 }
1274
1275                 rc = rawobj_extract_local(&tmpobj, (__u32 **) &data, &datalen);
1276                 if (rc) {
1277                         CERROR("failed extract mech\n");
1278                         goto out;
1279                 }
1280
1281                 rc = lgss_import_sec_context(&tmpobj,
1282                                              sec2gsec(ctx->cc_sec)->gs_mech,
1283                                              &gctx->gc_mechctx);
1284                 if (rc != GSS_S_COMPLETE)
1285                         CERROR("failed import context\n");
1286                 else
1287                         rc = 0;
1288         }
1289 out:
1290         /* we don't care what current status of this ctx, even someone else
1291          * is operating on the ctx at the same time. we just add up our own
1292          * opinions here. */
1293         if (rc == 0) {
1294                 gss_cli_ctx_uptodate(gctx);
1295         } else {
1296                 /* this will also revoke the key. has to be done before
1297                  * wakeup waiters otherwise they can find the stale key */
1298                 kill_key_locked(key);
1299
1300                 cli_ctx_expire(ctx);
1301
1302                 if (rc != -ERESTART)
1303                         set_bit(PTLRPC_CTX_ERROR_BIT, &ctx->cc_flags);
1304         }
1305
1306         sptlrpc_cli_ctx_wakeup(ctx);
1307
1308         /* let user space think it's a success */
1309         sptlrpc_cli_ctx_put(ctx, 1);
1310         RETURN(0);
1311 }
1312
1313 static
1314 int gss_kt_match(const struct key *key, const void *desc)
1315 {
1316         return (strcmp(key->description, (const char *) desc) == 0);
1317 }
1318
1319 static
1320 void gss_kt_destroy(struct key *key)
1321 {
1322         ENTRY;
1323         LASSERT(key->payload.data == NULL);
1324         CDEBUG(D_SEC, "destroy key %p\n", key);
1325         EXIT;
1326 }
1327
1328 static
1329 void gss_kt_describe(const struct key *key, struct seq_file *s)
1330 {
1331         if (key->description == NULL)
1332                 seq_puts(s, "[null]");
1333         else
1334                 seq_puts(s, key->description);
1335 }
1336
1337 static struct key_type gss_key_type =
1338 {
1339         .name           = "lgssc",
1340         .def_datalen    = 0,
1341         .instantiate    = gss_kt_instantiate,
1342         .update         = gss_kt_update,
1343         .match          = gss_kt_match,
1344         .destroy        = gss_kt_destroy,
1345         .describe       = gss_kt_describe,
1346 };
1347
1348 /****************************************
1349  * lustre gss keyring policy            *
1350  ****************************************/
1351
1352 static struct ptlrpc_ctx_ops gss_keyring_ctxops = {
1353         .match                  = gss_cli_ctx_match,
1354         .refresh                = gss_cli_ctx_refresh_kr,
1355         .validate               = gss_cli_ctx_validate_kr,
1356         .die                    = gss_cli_ctx_die_kr,
1357         .sign                   = gss_cli_ctx_sign,
1358         .verify                 = gss_cli_ctx_verify,
1359         .seal                   = gss_cli_ctx_seal,
1360         .unseal                 = gss_cli_ctx_unseal,
1361         .wrap_bulk              = gss_cli_ctx_wrap_bulk,
1362         .unwrap_bulk            = gss_cli_ctx_unwrap_bulk,
1363 };
1364
1365 static struct ptlrpc_sec_cops gss_sec_keyring_cops = {
1366         .create_sec             = gss_sec_create_kr,
1367         .destroy_sec            = gss_sec_destroy_kr,
1368         .lookup_ctx             = gss_sec_lookup_ctx_kr,
1369         .release_ctx            = gss_sec_release_ctx_kr,
1370         .flush_ctx_cache        = gss_sec_flush_ctx_cache_kr,
1371         .gc_ctx                 = gss_sec_gc_ctx_kr,
1372         .install_rctx           = gss_sec_install_rctx,
1373         .alloc_reqbuf           = gss_alloc_reqbuf,
1374         .free_reqbuf            = gss_free_reqbuf,
1375         .alloc_repbuf           = gss_alloc_repbuf,
1376         .free_repbuf            = gss_free_repbuf,
1377         .enlarge_reqbuf         = gss_enlarge_reqbuf,
1378         .display                = gss_sec_display_kr,
1379 };
1380
1381 static struct ptlrpc_sec_sops gss_sec_keyring_sops = {
1382         .accept                 = gss_svc_accept_kr,
1383         .invalidate_ctx         = gss_svc_invalidate_ctx,
1384         .alloc_rs               = gss_svc_alloc_rs,
1385         .authorize              = gss_svc_authorize,
1386         .free_rs                = gss_svc_free_rs,
1387         .free_ctx               = gss_svc_free_ctx,
1388         .unwrap_bulk            = gss_svc_unwrap_bulk,
1389         .wrap_bulk              = gss_svc_wrap_bulk,
1390         .install_rctx           = gss_svc_install_rctx_kr,
1391 };
1392
1393 static struct ptlrpc_sec_policy gss_policy_keyring = {
1394         .sp_owner               = THIS_MODULE,
1395         .sp_name                = "gss.keyring",
1396         .sp_policy              = SPTLRPC_POLICY_GSS,
1397         .sp_cops                = &gss_sec_keyring_cops,
1398         .sp_sops                = &gss_sec_keyring_sops,
1399 };
1400
1401
1402 int __init gss_init_keyring(void)
1403 {
1404         int rc;
1405
1406         rc = register_key_type(&gss_key_type);
1407         if (rc) {
1408                 CERROR("failed to register keyring type: %d\n", rc);
1409                 return rc;
1410         }
1411
1412         rc = sptlrpc_register_policy(&gss_policy_keyring);
1413         if (rc) {
1414                 unregister_key_type(&gss_key_type);
1415                 return rc;
1416         }
1417
1418         return 0;
1419 }
1420
1421 void __exit gss_exit_keyring(void)
1422 {
1423         unregister_key_type(&gss_key_type);
1424         sptlrpc_unregister_policy(&gss_policy_keyring);
1425 }