auth_unix.c 5.7 KB

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