auth_generic.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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(const char *service_name)
  38. {
  39. struct auth_cred acred = {
  40. .uid = RPC_MACHINE_CRED_USERID,
  41. .gid = RPC_MACHINE_CRED_GROUPID,
  42. .principal = service_name,
  43. .machine_cred = 1,
  44. };
  45. dprintk("RPC: looking up machine cred for service %s\n",
  46. service_name);
  47. return generic_auth.au_ops->lookup_cred(&generic_auth, &acred, 0);
  48. }
  49. EXPORT_SYMBOL_GPL(rpc_lookup_machine_cred);
  50. static struct rpc_cred *generic_bind_cred(struct rpc_task *task,
  51. struct rpc_cred *cred, int lookupflags)
  52. {
  53. struct rpc_auth *auth = task->tk_client->cl_auth;
  54. struct auth_cred *acred = &container_of(cred, struct generic_cred, gc_base)->acred;
  55. return auth->au_ops->lookup_cred(auth, acred, lookupflags);
  56. }
  57. /*
  58. * Lookup generic creds for current process
  59. */
  60. static struct rpc_cred *
  61. generic_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  62. {
  63. return rpcauth_lookup_credcache(&generic_auth, acred, flags);
  64. }
  65. static struct rpc_cred *
  66. generic_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  67. {
  68. struct generic_cred *gcred;
  69. gcred = kmalloc(sizeof(*gcred), GFP_KERNEL);
  70. if (gcred == NULL)
  71. return ERR_PTR(-ENOMEM);
  72. rpcauth_init_cred(&gcred->gc_base, acred, &generic_auth, &generic_credops);
  73. gcred->gc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  74. gcred->acred.uid = acred->uid;
  75. gcred->acred.gid = acred->gid;
  76. gcred->acred.group_info = acred->group_info;
  77. if (gcred->acred.group_info != NULL)
  78. get_group_info(gcred->acred.group_info);
  79. gcred->acred.machine_cred = acred->machine_cred;
  80. gcred->acred.principal = acred->principal;
  81. dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
  82. gcred->acred.machine_cred ? "machine" : "generic",
  83. gcred, acred->uid, acred->gid);
  84. return &gcred->gc_base;
  85. }
  86. static void
  87. generic_free_cred(struct rpc_cred *cred)
  88. {
  89. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  90. dprintk("RPC: generic_free_cred %p\n", gcred);
  91. if (gcred->acred.group_info != NULL)
  92. put_group_info(gcred->acred.group_info);
  93. kfree(gcred);
  94. }
  95. static void
  96. generic_free_cred_callback(struct rcu_head *head)
  97. {
  98. struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
  99. generic_free_cred(cred);
  100. }
  101. static void
  102. generic_destroy_cred(struct rpc_cred *cred)
  103. {
  104. call_rcu(&cred->cr_rcu, generic_free_cred_callback);
  105. }
  106. static int
  107. machine_cred_match(struct auth_cred *acred, struct generic_cred *gcred, int flags)
  108. {
  109. if (!gcred->acred.machine_cred ||
  110. gcred->acred.principal != acred->principal ||
  111. gcred->acred.uid != acred->uid ||
  112. gcred->acred.gid != acred->gid)
  113. return 0;
  114. return 1;
  115. }
  116. /*
  117. * Match credentials against current process creds.
  118. */
  119. static int
  120. generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  121. {
  122. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  123. int i;
  124. if (acred->machine_cred)
  125. return machine_cred_match(acred, gcred, flags);
  126. if (gcred->acred.uid != acred->uid ||
  127. gcred->acred.gid != acred->gid ||
  128. gcred->acred.machine_cred != 0)
  129. goto out_nomatch;
  130. /* Optimisation in the case where pointers are identical... */
  131. if (gcred->acred.group_info == acred->group_info)
  132. goto out_match;
  133. /* Slow path... */
  134. if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
  135. goto out_nomatch;
  136. for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
  137. if (!gid_eq(GROUP_AT(gcred->acred.group_info, i),
  138. GROUP_AT(acred->group_info, i)))
  139. goto out_nomatch;
  140. }
  141. out_match:
  142. return 1;
  143. out_nomatch:
  144. return 0;
  145. }
  146. int __init rpc_init_generic_auth(void)
  147. {
  148. return rpcauth_init_credcache(&generic_auth);
  149. }
  150. void rpc_destroy_generic_auth(void)
  151. {
  152. rpcauth_destroy_credcache(&generic_auth);
  153. }
  154. static const struct rpc_authops generic_auth_ops = {
  155. .owner = THIS_MODULE,
  156. .au_name = "Generic",
  157. .lookup_cred = generic_lookup_cred,
  158. .crcreate = generic_create_cred,
  159. };
  160. static struct rpc_auth generic_auth = {
  161. .au_ops = &generic_auth_ops,
  162. .au_count = ATOMIC_INIT(0),
  163. };
  164. static const struct rpc_credops generic_credops = {
  165. .cr_name = "Generic cred",
  166. .crdestroy = generic_destroy_cred,
  167. .crbind = generic_bind_cred,
  168. .crmatch = generic_match,
  169. };