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