auth_generic.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * Generic RPC credential
  3. *
  4. * Copyright (C) 2008, Trond Myklebust <Trond.Myklebust@netapp.com>
  5. */
  6. #include <linux/err.h>
  7. #include <linux/slab.h>
  8. #include <linux/types.h>
  9. #include <linux/module.h>
  10. #include <linux/sched.h>
  11. #include <linux/sunrpc/auth.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/debug.h>
  14. #include <linux/sunrpc/sched.h>
  15. #ifdef RPC_DEBUG
  16. # define RPCDBG_FACILITY RPCDBG_AUTH
  17. #endif
  18. #define RPC_MACHINE_CRED_USERID ((uid_t)0)
  19. #define RPC_MACHINE_CRED_GROUPID ((gid_t)0)
  20. struct generic_cred {
  21. struct rpc_cred gc_base;
  22. struct auth_cred acred;
  23. };
  24. static struct rpc_auth generic_auth;
  25. static const struct rpc_credops generic_credops;
  26. /*
  27. * Public call interface
  28. */
  29. struct rpc_cred *rpc_lookup_cred(void)
  30. {
  31. return rpcauth_lookupcred(&generic_auth, 0);
  32. }
  33. EXPORT_SYMBOL_GPL(rpc_lookup_cred);
  34. /*
  35. * Public call interface for looking up machine creds.
  36. */
  37. struct rpc_cred *rpc_lookup_machine_cred(void)
  38. {
  39. struct auth_cred acred = {
  40. .uid = RPC_MACHINE_CRED_USERID,
  41. .gid = RPC_MACHINE_CRED_GROUPID,
  42. .machine_cred = 1,
  43. };
  44. dprintk("RPC: looking up machine cred\n");
  45. return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
  46. }
  47. EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
  48. static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
  49. struct rpc_cred *cred, int lookupflags)
  50. {
  51. struct rpc_auth *auth = task->tk_client->cl_auth;
  52. struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
  53. return auth->au_ops->lookup_cred(auth, acred, lookupflags);
  54. }
  55. /*
  56. * Lookup generic creds for current process
  57. */
  58. static struct rpc_cred *
  59. generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  60. {
  61. return rpcauth_lookup_credcache(&generic_auth, acred, flags);
  62. }
  63. static struct rpc_cred *
  64. generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  65. {
  66. struct generic_cred *gcred;
  67. gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
  68. if (gcred == NULL)
  69. return ERR_PTR(-ENOMEM);
  70. rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
  71. gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  72. gcred->acred.uid = acred->uid;
  73. gcred->acred.gid = acred->gid;
  74. gcred->acred.group_info = acred->group_info;
  75. if (gcred->acred.group_info != NULL)
  76. get_group_info(gcred->acred.group_info);
  77. gcred->acred.machine_cred = acred->machine_cred;
  78. dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
  79. gcred->acred.machine_cred ? "machine" : "generic",
  80. gcred, acred->uid, acred->gid);
  81. return &gcred->gc_base;
  82. }
  83. static void
  84. generic_free_cred(struct rpc_cred *cred)
  85. {
  86. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  87. dprintk("RPC: generic_free_cred %p\n", gcred);
  88. if (gcred->acred.group_info != NULL)
  89. put_group_info(gcred->acred.group_info);
  90. kfree(gcred);
  91. }
  92. static void
  93. generic_free_cred_callback(struct rcu_head *head)
  94. {
  95. struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
  96. generic_free_cred(cred);
  97. }
  98. static void
  99. generic_destroy_cred(struct rpc_cred *cred)
  100. {
  101. call_rcu(&cred->cr_rcu, generic_free_cred_callback);
  102. }
  103. /*
  104. * Match credentials against current process creds.
  105. */
  106. static int
  107. generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  108. {
  109. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  110. int i;
  111. if (gcred->acred.uid != acred->uid ||
  112. gcred->acred.gid != acred->gid ||
  113. gcred->acred.machine_cred != acred->machine_cred)
  114. goto out_nomatch;
  115. /* Optimisation in the case where pointers are identical... */
  116. if (gcred->acred.group_info == acred->group_info)
  117. goto out_match;
  118. /* Slow path... */
  119. if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
  120. goto out_nomatch;
  121. for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
  122. if (GROUP_AT(gcred->acred.group_info, i) !=
  123. GROUP_AT(acred->group_info, i))
  124. goto out_nomatch;
  125. }
  126. out_match:
  127. return 1;
  128. out_nomatch:
  129. return 0;
  130. }
  131. int __init rpc_init_generic_auth(void)
  132. {
  133. return rpcauth_init_credcache(&generic_auth);
  134. }
  135. void rpc_destroy_generic_auth(void)
  136. {
  137. rpcauth_destroy_credcache(&generic_auth);
  138. }
  139. static const struct rpc_authops generic_auth_ops = {
  140. .owner = THIS_MODULE,
  141. .au_name = "Generic",
  142. .lookup_cred = generic_lookup_cred,
  143. .crcreate = generic_create_cred,
  144. };
  145. static struct rpc_auth generic_auth = {
  146. .au_ops = &generic_auth_ops,
  147. .au_count = ATOMIC_INIT(0),
  148. };
  149. static const struct rpc_credops generic_credops = {
  150. .cr_name = "Generic cred",
  151. .crdestroy = generic_destroy_cred,
  152. .crbind = generic_bind_cred,
  153. .crmatch = generic_match,
  154. };