auth_unix.c 5.6 KB

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