auth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  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 const 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(const 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(const 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. const 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_release(clnt->cl_auth);
  86. clnt->cl_auth = auth;
  87. out:
  88. return auth;
  89. }
  90. void
  91. rpcauth_release(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. goto retry;
  240. cred = new;
  241. } else if ((cred->cr_flags & RPCAUTH_CRED_NEW)
  242. && cred->cr_ops->cr_init != NULL
  243. && !(flags & RPCAUTH_LOOKUP_NEW)) {
  244. int res = cred->cr_ops->cr_init(auth, cred);
  245. if (res < 0) {
  246. put_rpccred(cred);
  247. cred = ERR_PTR(res);
  248. }
  249. }
  250. return (struct rpc_cred *) cred;
  251. }
  252. struct rpc_cred *
  253. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  254. {
  255. struct auth_cred acred = {
  256. .uid = current->fsuid,
  257. .gid = current->fsgid,
  258. .group_info = current->group_info,
  259. };
  260. struct rpc_cred *ret;
  261. dprintk("RPC: looking up %s cred\n",
  262. auth->au_ops->au_name);
  263. get_group_info(acred.group_info);
  264. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  265. put_group_info(acred.group_info);
  266. return ret;
  267. }
  268. void
  269. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  270. struct rpc_auth *auth, const struct rpc_credops *ops)
  271. {
  272. INIT_HLIST_NODE(&cred->cr_hash);
  273. atomic_set(&cred->cr_count, 1);
  274. cred->cr_auth = auth;
  275. cred->cr_ops = ops;
  276. cred->cr_expire = jiffies;
  277. #ifdef RPC_DEBUG
  278. cred->cr_magic = RPCAUTH_CRED_MAGIC;
  279. #endif
  280. cred->cr_uid = acred->uid;
  281. }
  282. EXPORT_SYMBOL(rpcauth_init_cred);
  283. struct rpc_cred *
  284. rpcauth_bindcred(struct rpc_task *task)
  285. {
  286. struct rpc_auth *auth = task->tk_auth;
  287. struct auth_cred acred = {
  288. .uid = current->fsuid,
  289. .gid = current->fsgid,
  290. .group_info = current->group_info,
  291. };
  292. struct rpc_cred *ret;
  293. int flags = 0;
  294. dprintk("RPC: %5u looking up %s cred\n",
  295. task->tk_pid, task->tk_auth->au_ops->au_name);
  296. get_group_info(acred.group_info);
  297. if (task->tk_flags & RPC_TASK_ROOTCREDS)
  298. flags |= RPCAUTH_LOOKUP_ROOTCREDS;
  299. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  300. if (!IS_ERR(ret))
  301. task->tk_msg.rpc_cred = ret;
  302. else
  303. task->tk_status = PTR_ERR(ret);
  304. put_group_info(acred.group_info);
  305. return ret;
  306. }
  307. void
  308. rpcauth_holdcred(struct rpc_task *task)
  309. {
  310. dprintk("RPC: %5u holding %s cred %p\n",
  311. task->tk_pid, task->tk_auth->au_ops->au_name,
  312. task->tk_msg.rpc_cred);
  313. if (task->tk_msg.rpc_cred)
  314. get_rpccred(task->tk_msg.rpc_cred);
  315. }
  316. void
  317. put_rpccred(struct rpc_cred *cred)
  318. {
  319. cred->cr_expire = jiffies;
  320. if (!atomic_dec_and_test(&cred->cr_count))
  321. return;
  322. cred->cr_ops->crdestroy(cred);
  323. }
  324. void
  325. rpcauth_unbindcred(struct rpc_task *task)
  326. {
  327. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  328. dprintk("RPC: %5u releasing %s cred %p\n",
  329. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  330. put_rpccred(cred);
  331. task->tk_msg.rpc_cred = NULL;
  332. }
  333. __be32 *
  334. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  335. {
  336. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  337. dprintk("RPC: %5u marshaling %s cred %p\n",
  338. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  339. return cred->cr_ops->crmarshal(task, p);
  340. }
  341. __be32 *
  342. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  343. {
  344. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  345. dprintk("RPC: %5u validating %s cred %p\n",
  346. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  347. return cred->cr_ops->crvalidate(task, p);
  348. }
  349. int
  350. rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
  351. __be32 *data, void *obj)
  352. {
  353. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  354. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  355. task->tk_pid, cred->cr_ops->cr_name, cred);
  356. if (cred->cr_ops->crwrap_req)
  357. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  358. /* By default, we encode the arguments normally. */
  359. return encode(rqstp, data, obj);
  360. }
  361. int
  362. rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
  363. __be32 *data, void *obj)
  364. {
  365. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  366. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  367. task->tk_pid, cred->cr_ops->cr_name, cred);
  368. if (cred->cr_ops->crunwrap_resp)
  369. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  370. data, obj);
  371. /* By default, we decode the arguments normally. */
  372. return decode(rqstp, data, obj);
  373. }
  374. int
  375. rpcauth_refreshcred(struct rpc_task *task)
  376. {
  377. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  378. int err;
  379. dprintk("RPC: %5u refreshing %s cred %p\n",
  380. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  381. err = cred->cr_ops->crrefresh(task);
  382. if (err < 0)
  383. task->tk_status = err;
  384. return err;
  385. }
  386. void
  387. rpcauth_invalcred(struct rpc_task *task)
  388. {
  389. dprintk("RPC: %5u invalidating %s cred %p\n",
  390. task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
  391. spin_lock(&rpc_credcache_lock);
  392. if (task->tk_msg.rpc_cred)
  393. task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
  394. spin_unlock(&rpc_credcache_lock);
  395. }
  396. int
  397. rpcauth_uptodatecred(struct rpc_task *task)
  398. {
  399. return !(task->tk_msg.rpc_cred) ||
  400. (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
  401. }