auth_unix.c 5.4 KB

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