auth.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * linux/net/sunrpc/auth.c
  3. *
  4. * Generic RPC client authentication API.
  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/slab.h>
  12. #include <linux/errno.h>
  13. #include <linux/socket.h>
  14. #include <linux/sunrpc/clnt.h>
  15. #include <linux/spinlock.h>
  16. #ifdef RPC_DEBUG
  17. # define RPCDBG_FACILITY RPCDBG_AUTH
  18. #endif
  19. static struct rpc_authops * auth_flavors[RPC_AUTH_MAXFLAVOR] = {
  20. &authnull_ops, /* AUTH_NULL */
  21. &authunix_ops, /* AUTH_UNIX */
  22. NULL, /* others can be loadable modules */
  23. };
  24. static u32
  25. pseudoflavor_to_flavor(u32 flavor) {
  26. if (flavor >= RPC_AUTH_MAXFLAVOR)
  27. return RPC_AUTH_GSS;
  28. return flavor;
  29. }
  30. int
  31. rpcauth_register(struct rpc_authops *ops)
  32. {
  33. rpc_authflavor_t flavor;
  34. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  35. return -EINVAL;
  36. if (auth_flavors[flavor] != NULL)
  37. return -EPERM; /* what else? */
  38. auth_flavors[flavor] = ops;
  39. return 0;
  40. }
  41. int
  42. rpcauth_unregister(struct rpc_authops *ops)
  43. {
  44. rpc_authflavor_t flavor;
  45. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  46. return -EINVAL;
  47. if (auth_flavors[flavor] != ops)
  48. return -EPERM; /* what else? */
  49. auth_flavors[flavor] = NULL;
  50. return 0;
  51. }
  52. struct rpc_auth *
  53. rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
  54. {
  55. struct rpc_auth *auth;
  56. struct rpc_authops *ops;
  57. u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
  58. if (flavor >= RPC_AUTH_MAXFLAVOR || !(ops = auth_flavors[flavor]))
  59. return ERR_PTR(-EINVAL);
  60. auth = ops->create(clnt, pseudoflavor);
  61. if (IS_ERR(auth))
  62. return auth;
  63. if (clnt->cl_auth)
  64. rpcauth_destroy(clnt->cl_auth);
  65. clnt->cl_auth = auth;
  66. return auth;
  67. }
  68. void
  69. rpcauth_destroy(struct rpc_auth *auth)
  70. {
  71. if (!atomic_dec_and_test(&auth->au_count))
  72. return;
  73. auth->au_ops->destroy(auth);
  74. }
  75. static DEFINE_SPINLOCK(rpc_credcache_lock);
  76. /*
  77. * Initialize RPC credential cache
  78. */
  79. int
  80. rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
  81. {
  82. struct rpc_cred_cache *new;
  83. int i;
  84. new = (struct rpc_cred_cache *)kmalloc(sizeof(*new), GFP_KERNEL);
  85. if (!new)
  86. return -ENOMEM;
  87. for (i = 0; i < RPC_CREDCACHE_NR; i++)
  88. INIT_HLIST_HEAD(&new->hashtable[i]);
  89. new->expire = expire;
  90. new->nextgc = jiffies + (expire >> 1);
  91. auth->au_credcache = new;
  92. return 0;
  93. }
  94. /*
  95. * Destroy a list of credentials
  96. */
  97. static inline
  98. void rpcauth_destroy_credlist(struct hlist_head *head)
  99. {
  100. struct rpc_cred *cred;
  101. while (!hlist_empty(head)) {
  102. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  103. hlist_del_init(&cred->cr_hash);
  104. put_rpccred(cred);
  105. }
  106. }
  107. /*
  108. * Clear the RPC credential cache, and delete those credentials
  109. * that are not referenced.
  110. */
  111. void
  112. rpcauth_free_credcache(struct rpc_auth *auth)
  113. {
  114. struct rpc_cred_cache *cache = auth->au_credcache;
  115. HLIST_HEAD(free);
  116. struct hlist_node *pos, *next;
  117. struct rpc_cred *cred;
  118. int i;
  119. spin_lock(&rpc_credcache_lock);
  120. for (i = 0; i < RPC_CREDCACHE_NR; i++) {
  121. hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
  122. cred = hlist_entry(pos, struct rpc_cred, cr_hash);
  123. __hlist_del(&cred->cr_hash);
  124. hlist_add_head(&cred->cr_hash, &free);
  125. }
  126. }
  127. spin_unlock(&rpc_credcache_lock);
  128. rpcauth_destroy_credlist(&free);
  129. }
  130. static void
  131. rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
  132. {
  133. if (atomic_read(&cred->cr_count) != 1)
  134. return;
  135. if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
  136. cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
  137. if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
  138. __hlist_del(&cred->cr_hash);
  139. hlist_add_head(&cred->cr_hash, free);
  140. }
  141. }
  142. /*
  143. * Remove stale credentials. Avoid sleeping inside the loop.
  144. */
  145. static void
  146. rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
  147. {
  148. struct rpc_cred_cache *cache = auth->au_credcache;
  149. struct hlist_node *pos, *next;
  150. struct rpc_cred *cred;
  151. int i;
  152. dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
  153. for (i = 0; i < RPC_CREDCACHE_NR; i++) {
  154. hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
  155. cred = hlist_entry(pos, struct rpc_cred, cr_hash);
  156. rpcauth_prune_expired(auth, cred, free);
  157. }
  158. }
  159. cache->nextgc = jiffies + cache->expire;
  160. }
  161. /*
  162. * Look up a process' credentials in the authentication cache
  163. */
  164. struct rpc_cred *
  165. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  166. int taskflags)
  167. {
  168. struct rpc_cred_cache *cache = auth->au_credcache;
  169. HLIST_HEAD(free);
  170. struct hlist_node *pos, *next;
  171. struct rpc_cred *new = NULL,
  172. *cred = NULL;
  173. int nr = 0;
  174. if (!(taskflags & RPC_TASK_ROOTCREDS))
  175. nr = acred->uid & RPC_CREDCACHE_MASK;
  176. retry:
  177. spin_lock(&rpc_credcache_lock);
  178. if (time_before(cache->nextgc, jiffies))
  179. rpcauth_gc_credcache(auth, &free);
  180. hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
  181. struct rpc_cred *entry;
  182. entry = hlist_entry(pos, struct rpc_cred, cr_hash);
  183. if (entry->cr_ops->crmatch(acred, entry, taskflags)) {
  184. hlist_del(&entry->cr_hash);
  185. cred = entry;
  186. break;
  187. }
  188. rpcauth_prune_expired(auth, entry, &free);
  189. }
  190. if (new) {
  191. if (cred)
  192. hlist_add_head(&new->cr_hash, &free);
  193. else
  194. cred = new;
  195. }
  196. if (cred) {
  197. hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
  198. get_rpccred(cred);
  199. }
  200. spin_unlock(&rpc_credcache_lock);
  201. rpcauth_destroy_credlist(&free);
  202. if (!cred) {
  203. new = auth->au_ops->crcreate(auth, acred, taskflags);
  204. if (!IS_ERR(new)) {
  205. #ifdef RPC_DEBUG
  206. new->cr_magic = RPCAUTH_CRED_MAGIC;
  207. #endif
  208. goto retry;
  209. } else
  210. cred = new;
  211. }
  212. return (struct rpc_cred *) cred;
  213. }
  214. struct rpc_cred *
  215. rpcauth_lookupcred(struct rpc_auth *auth, int taskflags)
  216. {
  217. struct auth_cred acred = {
  218. .uid = current->fsuid,
  219. .gid = current->fsgid,
  220. .group_info = current->group_info,
  221. };
  222. struct rpc_cred *ret;
  223. dprintk("RPC: looking up %s cred\n",
  224. auth->au_ops->au_name);
  225. get_group_info(acred.group_info);
  226. ret = auth->au_ops->lookup_cred(auth, &acred, taskflags);
  227. put_group_info(acred.group_info);
  228. return ret;
  229. }
  230. struct rpc_cred *
  231. rpcauth_bindcred(struct rpc_task *task)
  232. {
  233. struct rpc_auth *auth = task->tk_auth;
  234. struct auth_cred acred = {
  235. .uid = current->fsuid,
  236. .gid = current->fsgid,
  237. .group_info = current->group_info,
  238. };
  239. struct rpc_cred *ret;
  240. dprintk("RPC: %4d looking up %s cred\n",
  241. task->tk_pid, task->tk_auth->au_ops->au_name);
  242. get_group_info(acred.group_info);
  243. ret = auth->au_ops->lookup_cred(auth, &acred, task->tk_flags);
  244. if (!IS_ERR(ret))
  245. task->tk_msg.rpc_cred = ret;
  246. else
  247. task->tk_status = PTR_ERR(ret);
  248. put_group_info(acred.group_info);
  249. return ret;
  250. }
  251. void
  252. rpcauth_holdcred(struct rpc_task *task)
  253. {
  254. dprintk("RPC: %4d holding %s cred %p\n",
  255. task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
  256. if (task->tk_msg.rpc_cred)
  257. get_rpccred(task->tk_msg.rpc_cred);
  258. }
  259. void
  260. put_rpccred(struct rpc_cred *cred)
  261. {
  262. cred->cr_expire = jiffies;
  263. if (!atomic_dec_and_test(&cred->cr_count))
  264. return;
  265. cred->cr_ops->crdestroy(cred);
  266. }
  267. void
  268. rpcauth_unbindcred(struct rpc_task *task)
  269. {
  270. struct rpc_auth *auth = task->tk_auth;
  271. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  272. dprintk("RPC: %4d releasing %s cred %p\n",
  273. task->tk_pid, auth->au_ops->au_name, cred);
  274. put_rpccred(cred);
  275. task->tk_msg.rpc_cred = NULL;
  276. }
  277. u32 *
  278. rpcauth_marshcred(struct rpc_task *task, u32 *p)
  279. {
  280. struct rpc_auth *auth = task->tk_auth;
  281. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  282. dprintk("RPC: %4d marshaling %s cred %p\n",
  283. task->tk_pid, auth->au_ops->au_name, cred);
  284. return cred->cr_ops->crmarshal(task, p);
  285. }
  286. u32 *
  287. rpcauth_checkverf(struct rpc_task *task, u32 *p)
  288. {
  289. struct rpc_auth *auth = task->tk_auth;
  290. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  291. dprintk("RPC: %4d validating %s cred %p\n",
  292. task->tk_pid, auth->au_ops->au_name, cred);
  293. return cred->cr_ops->crvalidate(task, p);
  294. }
  295. int
  296. rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
  297. u32 *data, void *obj)
  298. {
  299. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  300. dprintk("RPC: %4d using %s cred %p to wrap rpc data\n",
  301. task->tk_pid, cred->cr_ops->cr_name, cred);
  302. if (cred->cr_ops->crwrap_req)
  303. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  304. /* By default, we encode the arguments normally. */
  305. return encode(rqstp, data, obj);
  306. }
  307. int
  308. rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
  309. u32 *data, void *obj)
  310. {
  311. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  312. dprintk("RPC: %4d using %s cred %p to unwrap rpc data\n",
  313. task->tk_pid, cred->cr_ops->cr_name, cred);
  314. if (cred->cr_ops->crunwrap_resp)
  315. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  316. data, obj);
  317. /* By default, we decode the arguments normally. */
  318. return decode(rqstp, data, obj);
  319. }
  320. int
  321. rpcauth_refreshcred(struct rpc_task *task)
  322. {
  323. struct rpc_auth *auth = task->tk_auth;
  324. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  325. int err;
  326. dprintk("RPC: %4d refreshing %s cred %p\n",
  327. task->tk_pid, auth->au_ops->au_name, cred);
  328. err = cred->cr_ops->crrefresh(task);
  329. if (err < 0)
  330. task->tk_status = err;
  331. return err;
  332. }
  333. void
  334. rpcauth_invalcred(struct rpc_task *task)
  335. {
  336. dprintk("RPC: %4d invalidating %s cred %p\n",
  337. task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
  338. spin_lock(&rpc_credcache_lock);
  339. if (task->tk_msg.rpc_cred)
  340. task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
  341. spin_unlock(&rpc_credcache_lock);
  342. }
  343. int
  344. rpcauth_uptodatecred(struct rpc_task *task)
  345. {
  346. return !(task->tk_msg.rpc_cred) ||
  347. (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
  348. }