auth_generic.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. dprintk("RPC: allocated %s cred %p for uid %d gid %d\n",
  81. gcred->acred.machine_cred ? "machine" : "generic",
  82. gcred, acred->uid, acred->gid);
  83. return &gcred->gc_base;
  84. }
  85. static void
  86. generic_free_cred(struct rpc_cred *cred)
  87. {
  88. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  89. dprintk("RPC: generic_free_cred %p\n", gcred);
  90. if (gcred->acred.group_info != NULL)
  91. put_group_info(gcred->acred.group_info);
  92. kfree(gcred);
  93. }
  94. static void
  95. generic_free_cred_callback(struct rcu_head *head)
  96. {
  97. struct rpc_cred *cred = container_of(head, struct rpc_cred, cr_rcu);
  98. generic_free_cred(cred);
  99. }
  100. static void
  101. generic_destroy_cred(struct rpc_cred *cred)
  102. {
  103. call_rcu(&cred->cr_rcu, generic_free_cred_callback);
  104. }
  105. /*
  106. * Match credentials against current process creds.
  107. */
  108. static int
  109. generic_match(struct auth_cred *acred, struct rpc_cred *cred, int flags)
  110. {
  111. struct generic_cred *gcred = container_of(cred, struct generic_cred, gc_base);
  112. int i;
  113. if (gcred->acred.uid != acred->uid ||
  114. gcred->acred.gid != acred->gid ||
  115. gcred->acred.machine_cred != acred->machine_cred)
  116. goto out_nomatch;
  117. /* Optimisation in the case where pointers are identical... */
  118. if (gcred->acred.group_info == acred->group_info)
  119. goto out_match;
  120. /* Slow path... */
  121. if (gcred->acred.group_info->ngroups != acred->group_info->ngroups)
  122. goto out_nomatch;
  123. for (i = 0; i < gcred->acred.group_info->ngroups; i++) {
  124. if (GROUP_AT(gcred->acred.group_info, i) !=
  125. GROUP_AT(acred->group_info, i))
  126. goto out_nomatch;
  127. }
  128. out_match:
  129. return 1;
  130. out_nomatch:
  131. return 0;
  132. }
  133. int __init rpc_init_generic_auth(void)
  134. {
  135. return rpcauth_init_credcache(&generic_auth);
  136. }
  137. void rpc_destroy_generic_auth(void)
  138. {
  139. rpcauth_destroy_credcache(&generic_auth);
  140. }
  141. static const struct rpc_authops generic_auth_ops = {
  142. .owner = THIS_MODULE,
  143. .au_name = "Generic",
  144. .lookup_cred = generic_lookup_cred,
  145. .crcreate = generic_create_cred,
  146. };
  147. static struct rpc_auth generic_auth = {
  148. .au_ops = &generic_auth_ops,
  149. .au_count = ATOMIC_INIT(0),
  150. };
  151. static const struct rpc_credops generic_credops = {
  152. .cr_name = "Generic cred",
  153. .crdestroy = generic_destroy_cred,
  154. .crbind = generic_bind_cred,
  155. .crmatch = generic_match,
  156. };