auth.c 10 KB

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