auth_unix.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * linux/net/sunrpc/auth_unix.c
  3. *
  4. * UNIX-style authentication; no AUTH_SHORT support
  5. *
  6. * Copyright (C) 1996, Olaf Kirch <okir@monad.swb.de>
  7. */
  8. #include <linux/slab.h>
  9. #include <linux/types.h>
  10. #include <linux/sched.h>
  11. #include <linux/module.h>
  12. #include <linux/sunrpc/clnt.h>
  13. #include <linux/sunrpc/auth.h>
  14. #define NFS_NGROUPS 16
  15. struct unx_cred {
  16. struct rpc_cred uc_base;
  17. gid_t uc_gid;
  18. gid_t uc_gids[NFS_NGROUPS];
  19. };
  20. #define uc_uid uc_base.cr_uid
  21. #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
  22. #ifdef RPC_DEBUG
  23. # define RPCDBG_FACILITY RPCDBG_AUTH
  24. #endif
  25. static struct rpc_auth unix_auth;
  26. static struct rpc_cred_cache unix_cred_cache;
  27. static const struct rpc_credops unix_credops;
  28. static struct rpc_auth *
  29. unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  30. {
  31. dprintk("RPC: creating UNIX authenticator for client %p\n",
  32. clnt);
  33. atomic_inc(&unix_auth.au_count);
  34. return &unix_auth;
  35. }
  36. static void
  37. unx_destroy(struct rpc_auth *auth)
  38. {
  39. dprintk("RPC: destroying UNIX authenticator %p\n", auth);
  40. rpcauth_clear_credcache(auth->au_credcache);
  41. }
  42. /*
  43. * Lookup AUTH_UNIX creds for current process
  44. */
  45. static struct rpc_cred *
  46. unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  47. {
  48. return rpcauth_lookup_credcache(auth, acred, flags);
  49. }
  50. static struct rpc_cred *
  51. unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  52. {
  53. struct unx_cred *cred;
  54. unsigned int groups = 0;
  55. unsigned int i;
  56. dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
  57. acred->uid, acred->gid);
  58. if (!(cred = kmalloc(sizeof(*cred), GFP_NOFS)))
  59. return ERR_PTR(-ENOMEM);
  60. rpcauth_init_cred(&cred->uc_base, acred, auth, &unix_credops);
  61. cred->uc_base.cr_flags = 1UL << RPCAUTH_CRED_UPTODATE;
  62. if (acred->group_info != NULL)
  63. groups = acred->group_info->ngroups;
  64. if (groups > NFS_NGROUPS)
  65. groups = NFS_NGROUPS;
  66. cred->uc_gid = acred->gid;
  67. for (i = 0; i < groups; i++)
  68. cred->uc_gids[i] = GROUP_AT(acred->group_info, i);
  69. if (i < NFS_NGROUPS)
  70. cred->uc_gids[i] = NOGROUP;
  71. return &cred->uc_base;
  72. }
  73. static void
  74. unx_free_cred(struct unx_cred *unx_cred)
  75. {
  76. dprintk("RPC: unx_free_cred %p\n", unx_cred);
  77. kfree(unx_cred);
  78. }
  79. static void
  80. unx_free_cred_callback(struct rcu_head *head)
  81. {
  82. struct unx_cred *unx_cred = container_of(head, struct unx_cred, uc_base.cr_rcu);
  83. unx_free_cred(unx_cred);
  84. }
  85. static void
  86. unx_destroy_cred(struct rpc_cred *cred)
  87. {
  88. call_rcu(&cred->cr_rcu, unx_free_cred_callback);
  89. }
  90. /*
  91. * Match credentials against current process creds.
  92. * The root_override argument takes care of cases where the caller may
  93. * request root creds (e.g. for NFS swapping).
  94. */
  95. static int
  96. unx_match(struct auth_cred *acred, struct rpc_cred *rcred, int flags)
  97. {
  98. struct unx_cred *cred = container_of(rcred, struct unx_cred, uc_base);
  99. unsigned int groups = 0;
  100. unsigned int i;
  101. if (cred->uc_uid != acred->uid || cred->uc_gid != acred->gid)
  102. return 0;
  103. if (acred->group_info != NULL)
  104. groups = acred->group_info->ngroups;
  105. if (groups > NFS_NGROUPS)
  106. groups = NFS_NGROUPS;
  107. for (i = 0; i < groups ; i++)
  108. if (cred->uc_gids[i] != GROUP_AT(acred->group_info, i))
  109. return 0;
  110. return 1;
  111. }
  112. /*
  113. * Marshal credentials.
  114. * Maybe we should keep a cached credential for performance reasons.
  115. */
  116. static __be32 *
  117. unx_marshal(struct rpc_task *task, __be32 *p)
  118. {
  119. struct rpc_clnt *clnt = task->tk_client;
  120. struct unx_cred *cred = container_of(task->tk_msg.rpc_cred, struct unx_cred, uc_base);
  121. __be32 *base, *hold;
  122. int i;
  123. *p++ = htonl(RPC_AUTH_UNIX);
  124. base = p++;
  125. *p++ = htonl(jiffies/HZ);
  126. /*
  127. * Copy the UTS nodename captured when the client was created.
  128. */
  129. p = xdr_encode_array(p, clnt->cl_nodename, clnt->cl_nodelen);
  130. *p++ = htonl((u32) cred->uc_uid);
  131. *p++ = htonl((u32) cred->uc_gid);
  132. hold = p++;
  133. for (i = 0; i < 16 && cred->uc_gids[i] != (gid_t) NOGROUP; i++)
  134. *p++ = htonl((u32) cred->uc_gids[i]);
  135. *hold = htonl(p - hold - 1); /* gid array length */
  136. *base = htonl((p - base - 1) << 2); /* cred length */
  137. *p++ = htonl(RPC_AUTH_NULL);
  138. *p++ = htonl(0);
  139. return p;
  140. }
  141. /*
  142. * Refresh credentials. This is a no-op for AUTH_UNIX
  143. */
  144. static int
  145. unx_refresh(struct rpc_task *task)
  146. {
  147. set_bit(RPCAUTH_CRED_UPTODATE, &task->tk_msg.rpc_cred->cr_flags);
  148. return 0;
  149. }
  150. static __be32 *
  151. unx_validate(struct rpc_task *task, __be32 *p)
  152. {
  153. rpc_authflavor_t flavor;
  154. u32 size;
  155. flavor = ntohl(*p++);
  156. if (flavor != RPC_AUTH_NULL &&
  157. flavor != RPC_AUTH_UNIX &&
  158. flavor != RPC_AUTH_SHORT) {
  159. printk("RPC: bad verf flavor: %u\n", flavor);
  160. return NULL;
  161. }
  162. size = ntohl(*p++);
  163. if (size > RPC_MAX_AUTH_SIZE) {
  164. printk("RPC: giant verf size: %u\n", size);
  165. return NULL;
  166. }
  167. task->tk_msg.rpc_cred->cr_auth->au_rslack = (size >> 2) + 2;
  168. p += (size >> 2);
  169. return p;
  170. }
  171. void __init rpc_init_authunix(void)
  172. {
  173. spin_lock_init(&unix_cred_cache.lock);
  174. }
  175. const struct rpc_authops authunix_ops = {
  176. .owner = THIS_MODULE,
  177. .au_flavor = RPC_AUTH_UNIX,
  178. .au_name = "UNIX",
  179. .create = unx_create,
  180. .destroy = unx_destroy,
  181. .lookup_cred = unx_lookup_cred,
  182. .crcreate = unx_create_cred,
  183. };
  184. static
  185. struct rpc_cred_cache unix_cred_cache = {
  186. };
  187. static
  188. struct rpc_auth unix_auth = {
  189. .au_cslack = UNX_WRITESLACK,
  190. .au_rslack = 2, /* assume AUTH_NULL verf */
  191. .au_ops = &authunix_ops,
  192. .au_flavor = RPC_AUTH_UNIX,
  193. .au_count = ATOMIC_INIT(0),
  194. .au_credcache = &unix_cred_cache,
  195. };
  196. static
  197. const struct rpc_credops unix_credops = {
  198. .cr_name = "AUTH_UNIX",
  199. .crdestroy = unx_destroy_cred,
  200. .crbind = rpcauth_generic_bind_cred,
  201. .crmatch = unx_match,
  202. .crmarshal = unx_marshal,
  203. .crrefresh = unx_refresh,
  204. .crvalidate = unx_validate,
  205. };