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