auth_unix.c 5.4 KB

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