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