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