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