svcauth.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. * linux/net/sunrpc/svcauth.c
  3. *
  4. * The generic interface for RPC authentication on the server side.
  5. *
  6. * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
  7. *
  8. * CHANGES
  9. * 19-Apr-2000 Chris Evans - Security fix
  10. */
  11. #include <linux/types.h>
  12. #include <linux/module.h>
  13. #include <linux/sunrpc/types.h>
  14. #include <linux/sunrpc/xdr.h>
  15. #include <linux/sunrpc/svcsock.h>
  16. #include <linux/sunrpc/svcauth.h>
  17. #include <linux/err.h>
  18. #include <linux/hash.h>
  19. #define RPCDBG_FACILITY RPCDBG_AUTH
  20. /*
  21. * Table of authenticators
  22. */
  23. extern struct auth_ops svcauth_null;
  24. extern struct auth_ops svcauth_unix;
  25. static struct auth_ops __rcu *authtab[RPC_AUTH_MAXFLAVOR] = {
  26. [RPC_AUTH_NULL] = (struct auth_ops __force __rcu *)&svcauth_null,
  27. [RPC_AUTH_UNIX] = (struct auth_ops __force __rcu *)&svcauth_unix,
  28. };
  29. static struct auth_ops *
  30. svc_get_auth_ops(rpc_authflavor_t flavor)
  31. {
  32. struct auth_ops *aops;
  33. if (flavor >= RPC_AUTH_MAXFLAVOR)
  34. return NULL;
  35. rcu_read_lock();
  36. aops = rcu_dereference(authtab[flavor]);
  37. if (aops != NULL && !try_module_get(aops->owner))
  38. aops = NULL;
  39. rcu_read_unlock();
  40. return aops;
  41. }
  42. static void
  43. svc_put_auth_ops(struct auth_ops *aops)
  44. {
  45. module_put(aops->owner);
  46. }
  47. int
  48. svc_authenticate(struct svc_rqst *rqstp, __be32 *authp)
  49. {
  50. rpc_authflavor_t flavor;
  51. struct auth_ops *aops;
  52. *authp = rpc_auth_ok;
  53. flavor = svc_getnl(&rqstp->rq_arg.head[0]);
  54. dprintk("svc: svc_authenticate (%d)\n", flavor);
  55. aops = svc_get_auth_ops(flavor);
  56. if (aops == NULL) {
  57. *authp = rpc_autherr_badcred;
  58. return SVC_DENIED;
  59. }
  60. rqstp->rq_auth_slack = 0;
  61. init_svc_cred(&rqstp->rq_cred);
  62. rqstp->rq_authop = aops;
  63. return aops->accept(rqstp, authp);
  64. }
  65. EXPORT_SYMBOL_GPL(svc_authenticate);
  66. int svc_set_client(struct svc_rqst *rqstp)
  67. {
  68. rqstp->rq_client = NULL;
  69. return rqstp->rq_authop->set_client(rqstp);
  70. }
  71. EXPORT_SYMBOL_GPL(svc_set_client);
  72. /* A request, which was authenticated, has now executed.
  73. * Time to finalise the credentials and verifier
  74. * and release and resources
  75. */
  76. int svc_authorise(struct svc_rqst *rqstp)
  77. {
  78. struct auth_ops *aops = rqstp->rq_authop;
  79. int rv = 0;
  80. rqstp->rq_authop = NULL;
  81. if (aops) {
  82. rv = aops->release(rqstp);
  83. svc_put_auth_ops(aops);
  84. }
  85. return rv;
  86. }
  87. int
  88. svc_auth_register(rpc_authflavor_t flavor, struct auth_ops *aops)
  89. {
  90. struct auth_ops *old;
  91. int rv = -EINVAL;
  92. if (flavor < RPC_AUTH_MAXFLAVOR) {
  93. old = cmpxchg((struct auth_ops ** __force)&authtab[flavor], NULL, aops);
  94. if (old == NULL || old == aops)
  95. rv = 0;
  96. }
  97. return rv;
  98. }
  99. EXPORT_SYMBOL_GPL(svc_auth_register);
  100. void
  101. svc_auth_unregister(rpc_authflavor_t flavor)
  102. {
  103. if (flavor < RPC_AUTH_MAXFLAVOR)
  104. rcu_assign_pointer(authtab[flavor], NULL);
  105. }
  106. EXPORT_SYMBOL_GPL(svc_auth_unregister);
  107. /**************************************************
  108. * 'auth_domains' are stored in a hash table indexed by name.
  109. * When the last reference to an 'auth_domain' is dropped,
  110. * the object is unhashed and freed.
  111. * If auth_domain_lookup fails to find an entry, it will return
  112. * it's second argument 'new'. If this is non-null, it will
  113. * have been atomically linked into the table.
  114. */
  115. #define DN_HASHBITS 6
  116. #define DN_HASHMAX (1<<DN_HASHBITS)
  117. static struct hlist_head auth_domain_table[DN_HASHMAX];
  118. static DEFINE_SPINLOCK(auth_domain_lock);
  119. static void auth_domain_release(struct kref *kref)
  120. __releases(&auth_domain_lock)
  121. {
  122. struct auth_domain *dom = container_of(kref, struct auth_domain, ref);
  123. hlist_del_rcu(&dom->hash);
  124. dom->flavour->domain_release(dom);
  125. spin_unlock(&auth_domain_lock);
  126. }
  127. void auth_domain_put(struct auth_domain *dom)
  128. {
  129. kref_put_lock(&dom->ref, auth_domain_release, &auth_domain_lock);
  130. }
  131. EXPORT_SYMBOL_GPL(auth_domain_put);
  132. struct auth_domain *
  133. auth_domain_lookup(char *name, struct auth_domain *new)
  134. {
  135. struct auth_domain *hp;
  136. struct hlist_head *head;
  137. head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
  138. spin_lock(&auth_domain_lock);
  139. hlist_for_each_entry(hp, head, hash) {
  140. if (strcmp(hp->name, name)==0) {
  141. kref_get(&hp->ref);
  142. spin_unlock(&auth_domain_lock);
  143. return hp;
  144. }
  145. }
  146. if (new)
  147. hlist_add_head_rcu(&new->hash, head);
  148. spin_unlock(&auth_domain_lock);
  149. return new;
  150. }
  151. EXPORT_SYMBOL_GPL(auth_domain_lookup);
  152. struct auth_domain *auth_domain_find(char *name)
  153. {
  154. struct auth_domain *hp;
  155. struct hlist_head *head;
  156. head = &auth_domain_table[hash_str(name, DN_HASHBITS)];
  157. rcu_read_lock();
  158. hlist_for_each_entry_rcu(hp, head, hash) {
  159. if (strcmp(hp->name, name)==0) {
  160. if (!kref_get_unless_zero(&hp->ref))
  161. hp = NULL;
  162. rcu_read_unlock();
  163. return hp;
  164. }
  165. }
  166. rcu_read_unlock();
  167. return NULL;
  168. }
  169. EXPORT_SYMBOL_GPL(auth_domain_find);