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/socket.h>
  12. #include <linux/in.h>
  13. #include <linux/sunrpc/clnt.h>
  14. #include <linux/sunrpc/auth.h>
  15. #define NFS_NGROUPS 16
  16. struct unx_cred {
  17. struct rpc_cred uc_base;
  18. gid_t uc_gid;
  19. gid_t uc_gids[NFS_NGROUPS];
  20. };
  21. #define uc_uid uc_base.cr_uid
  22. #define uc_count uc_base.cr_count
  23. #define uc_flags uc_base.cr_flags
  24. #define uc_expire uc_base.cr_expire
  25. #define UNX_CRED_EXPIRE (60 * HZ)
  26. #define UNX_WRITESLACK (21 + (UNX_MAXNODENAME >> 2))
  27. #ifdef RPC_DEBUG
  28. # define RPCDBG_FACILITY RPCDBG_AUTH
  29. #endif
  30. static struct rpc_auth unix_auth;
  31. static struct rpc_cred_cache unix_cred_cache;
  32. static struct rpc_credops unix_credops;
  33. static struct rpc_auth *
  34. unx_create(struct rpc_clnt *clnt, rpc_authflavor_t flavor)
  35. {
  36. dprintk("RPC: creating UNIX authenticator for client %p\n", clnt);
  37. if (atomic_inc_return(&unix_auth.au_count) == 0)
  38. unix_cred_cache.nextgc = jiffies + (unix_cred_cache.expire >> 1);
  39. return &unix_auth;
  40. }
  41. static void
  42. unx_destroy(struct rpc_auth *auth)
  43. {
  44. dprintk("RPC: destroying UNIX authenticator %p\n", auth);
  45. rpcauth_free_credcache(auth);
  46. }
  47. /*
  48. * Lookup AUTH_UNIX creds for current process
  49. */
  50. static struct rpc_cred *
  51. unx_lookup_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  52. {
  53. return rpcauth_lookup_credcache(auth, acred, flags);
  54. }
  55. static struct rpc_cred *
  56. unx_create_cred(struct rpc_auth *auth, struct auth_cred *acred, int flags)
  57. {
  58. struct unx_cred *cred;
  59. int i;
  60. dprintk("RPC: allocating UNIX cred for uid %d gid %d\n",
  61. acred->uid, acred->gid);
  62. if (!(cred = (struct unx_cred *) kmalloc(sizeof(*cred), GFP_KERNEL)))
  63. return ERR_PTR(-ENOMEM);
  64. atomic_set(&cred->uc_count, 1);
  65. cred->uc_flags = RPCAUTH_CRED_UPTODATE;
  66. if (flags & RPC_TASK_ROOTCREDS) {
  67. cred->uc_uid = 0;
  68. cred->uc_gid = 0;
  69. cred->uc_gids[0] = NOGROUP;
  70. } else {
  71. int groups = acred->group_info->ngroups;
  72. if (groups > NFS_NGROUPS)
  73. groups = NFS_NGROUPS;
  74. cred->uc_uid = acred->uid;
  75. cred->uc_gid = acred->gid;
  76. for (i = 0; i < groups; i++)
  77. cred->uc_gids[i] = GROUP_AT(acred->group_info, i);
  78. if (i < NFS_NGROUPS)
  79. cred->uc_gids[i] = NOGROUP;
  80. }
  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 taskflags)
  96. {
  97. struct unx_cred *cred = (struct unx_cred *) rcred;
  98. int i;
  99. if (!(taskflags & RPC_TASK_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 u32 *
  121. unx_marshal(struct rpc_task *task, u32 *p)
  122. {
  123. struct rpc_clnt *clnt = task->tk_client;
  124. struct unx_cred *cred = (struct unx_cred *) task->tk_msg.rpc_cred;
  125. u32 *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 u32 *
  155. unx_validate(struct rpc_task *task, u32 *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_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. };