auth_unix.c 5.3 KB

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