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