auth_unix.c 5.4 KB

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