auth.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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/hash.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. #define RPC_CREDCACHE_DEFAULT_HASHBITS (4)
  20. struct rpc_cred_cache {
  21. struct hlist_head *hashtable;
  22. unsigned int hashbits;
  23. spinlock_t lock;
  24. };
  25. static unsigned int auth_hashbits = RPC_CREDCACHE_DEFAULT_HASHBITS;
  26. static DEFINE_SPINLOCK(rpc_authflavor_lock);
  27. static const struct rpc_authops *auth_flavors[RPC_AUTH_MAXFLAVOR] = {
  28. &authnull_ops, /* AUTH_NULL */
  29. &authunix_ops, /* AUTH_UNIX */
  30. NULL, /* others can be loadable modules */
  31. };
  32. static LIST_HEAD(cred_unused);
  33. static unsigned long number_cred_unused;
  34. #define MAX_HASHTABLE_BITS (14)
  35. static int param_set_hashtbl_sz(const char *val, const struct kernel_param *kp)
  36. {
  37. unsigned long num;
  38. unsigned int nbits;
  39. int ret;
  40. if (!val)
  41. goto out_inval;
  42. ret = strict_strtoul(val, 0, &num);
  43. if (ret == -EINVAL)
  44. goto out_inval;
  45. nbits = fls(num);
  46. if (num > (1U << nbits))
  47. nbits++;
  48. if (nbits > MAX_HASHTABLE_BITS || nbits < 2)
  49. goto out_inval;
  50. *(unsigned int *)kp->arg = nbits;
  51. return 0;
  52. out_inval:
  53. return -EINVAL;
  54. }
  55. static int param_get_hashtbl_sz(char *buffer, const struct kernel_param *kp)
  56. {
  57. unsigned int nbits;
  58. nbits = *(unsigned int *)kp->arg;
  59. return sprintf(buffer, "%u", 1U << nbits);
  60. }
  61. #define param_check_hashtbl_sz(name, p) __param_check(name, p, unsigned int);
  62. static struct kernel_param_ops param_ops_hashtbl_sz = {
  63. .set = param_set_hashtbl_sz,
  64. .get = param_get_hashtbl_sz,
  65. };
  66. module_param_named(auth_hashtable_size, auth_hashbits, hashtbl_sz, 0644);
  67. MODULE_PARM_DESC(auth_hashtable_size, "RPC credential cache hashtable size");
  68. static u32
  69. pseudoflavor_to_flavor(u32 flavor) {
  70. if (flavor >= RPC_AUTH_MAXFLAVOR)
  71. return RPC_AUTH_GSS;
  72. return flavor;
  73. }
  74. int
  75. rpcauth_register(const struct rpc_authops *ops)
  76. {
  77. rpc_authflavor_t flavor;
  78. int ret = -EPERM;
  79. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  80. return -EINVAL;
  81. spin_lock(&rpc_authflavor_lock);
  82. if (auth_flavors[flavor] == NULL) {
  83. auth_flavors[flavor] = ops;
  84. ret = 0;
  85. }
  86. spin_unlock(&rpc_authflavor_lock);
  87. return ret;
  88. }
  89. EXPORT_SYMBOL_GPL(rpcauth_register);
  90. int
  91. rpcauth_unregister(const struct rpc_authops *ops)
  92. {
  93. rpc_authflavor_t flavor;
  94. int ret = -EPERM;
  95. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  96. return -EINVAL;
  97. spin_lock(&rpc_authflavor_lock);
  98. if (auth_flavors[flavor] == ops) {
  99. auth_flavors[flavor] = NULL;
  100. ret = 0;
  101. }
  102. spin_unlock(&rpc_authflavor_lock);
  103. return ret;
  104. }
  105. EXPORT_SYMBOL_GPL(rpcauth_unregister);
  106. struct rpc_auth *
  107. rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
  108. {
  109. struct rpc_auth *auth;
  110. const struct rpc_authops *ops;
  111. u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
  112. auth = ERR_PTR(-EINVAL);
  113. if (flavor >= RPC_AUTH_MAXFLAVOR)
  114. goto out;
  115. if ((ops = auth_flavors[flavor]) == NULL)
  116. request_module("rpc-auth-%u", flavor);
  117. spin_lock(&rpc_authflavor_lock);
  118. ops = auth_flavors[flavor];
  119. if (ops == NULL || !try_module_get(ops->owner)) {
  120. spin_unlock(&rpc_authflavor_lock);
  121. goto out;
  122. }
  123. spin_unlock(&rpc_authflavor_lock);
  124. auth = ops->create(clnt, pseudoflavor);
  125. module_put(ops->owner);
  126. if (IS_ERR(auth))
  127. return auth;
  128. if (clnt->cl_auth)
  129. rpcauth_release(clnt->cl_auth);
  130. clnt->cl_auth = auth;
  131. out:
  132. return auth;
  133. }
  134. EXPORT_SYMBOL_GPL(rpcauth_create);
  135. void
  136. rpcauth_release(struct rpc_auth *auth)
  137. {
  138. if (!atomic_dec_and_test(&auth->au_count))
  139. return;
  140. auth->au_ops->destroy(auth);
  141. }
  142. static DEFINE_SPINLOCK(rpc_credcache_lock);
  143. static void
  144. rpcauth_unhash_cred_locked(struct rpc_cred *cred)
  145. {
  146. hlist_del_rcu(&cred->cr_hash);
  147. smp_mb__before_clear_bit();
  148. clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  149. }
  150. static int
  151. rpcauth_unhash_cred(struct rpc_cred *cred)
  152. {
  153. spinlock_t *cache_lock;
  154. int ret;
  155. cache_lock = &cred->cr_auth->au_credcache->lock;
  156. spin_lock(cache_lock);
  157. ret = atomic_read(&cred->cr_count) == 0;
  158. if (ret)
  159. rpcauth_unhash_cred_locked(cred);
  160. spin_unlock(cache_lock);
  161. return ret;
  162. }
  163. /*
  164. * Initialize RPC credential cache
  165. */
  166. int
  167. rpcauth_init_credcache(struct rpc_auth *auth)
  168. {
  169. struct rpc_cred_cache *new;
  170. unsigned int hashsize;
  171. new = kmalloc(sizeof(*new), GFP_KERNEL);
  172. if (!new)
  173. goto out_nocache;
  174. new->hashbits = auth_hashbits;
  175. hashsize = 1U << new->hashbits;
  176. new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
  177. if (!new->hashtable)
  178. goto out_nohashtbl;
  179. spin_lock_init(&new->lock);
  180. auth->au_credcache = new;
  181. return 0;
  182. out_nohashtbl:
  183. kfree(new);
  184. out_nocache:
  185. return -ENOMEM;
  186. }
  187. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  188. /*
  189. * Destroy a list of credentials
  190. */
  191. static inline
  192. void rpcauth_destroy_credlist(struct list_head *head)
  193. {
  194. struct rpc_cred *cred;
  195. while (!list_empty(head)) {
  196. cred = list_entry(head->next, struct rpc_cred, cr_lru);
  197. list_del_init(&cred->cr_lru);
  198. put_rpccred(cred);
  199. }
  200. }
  201. /*
  202. * Clear the RPC credential cache, and delete those credentials
  203. * that are not referenced.
  204. */
  205. void
  206. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  207. {
  208. LIST_HEAD(free);
  209. struct hlist_head *head;
  210. struct rpc_cred *cred;
  211. unsigned int hashsize = 1U << cache->hashbits;
  212. int i;
  213. spin_lock(&rpc_credcache_lock);
  214. spin_lock(&cache->lock);
  215. for (i = 0; i < hashsize; i++) {
  216. head = &cache->hashtable[i];
  217. while (!hlist_empty(head)) {
  218. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  219. get_rpccred(cred);
  220. if (!list_empty(&cred->cr_lru)) {
  221. list_del(&cred->cr_lru);
  222. number_cred_unused--;
  223. }
  224. list_add_tail(&cred->cr_lru, &free);
  225. rpcauth_unhash_cred_locked(cred);
  226. }
  227. }
  228. spin_unlock(&cache->lock);
  229. spin_unlock(&rpc_credcache_lock);
  230. rpcauth_destroy_credlist(&free);
  231. }
  232. /*
  233. * Destroy the RPC credential cache
  234. */
  235. void
  236. rpcauth_destroy_credcache(struct rpc_auth *auth)
  237. {
  238. struct rpc_cred_cache *cache = auth->au_credcache;
  239. if (cache) {
  240. auth->au_credcache = NULL;
  241. rpcauth_clear_credcache(cache);
  242. kfree(cache->hashtable);
  243. kfree(cache);
  244. }
  245. }
  246. EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
  247. #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
  248. /*
  249. * Remove stale credentials. Avoid sleeping inside the loop.
  250. */
  251. static int
  252. rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
  253. {
  254. spinlock_t *cache_lock;
  255. struct rpc_cred *cred, *next;
  256. unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
  257. list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
  258. if (nr_to_scan-- == 0)
  259. break;
  260. /*
  261. * Enforce a 60 second garbage collection moratorium
  262. * Note that the cred_unused list must be time-ordered.
  263. */
  264. if (time_in_range(cred->cr_expire, expired, jiffies) &&
  265. test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
  266. return 0;
  267. list_del_init(&cred->cr_lru);
  268. number_cred_unused--;
  269. if (atomic_read(&cred->cr_count) != 0)
  270. continue;
  271. cache_lock = &cred->cr_auth->au_credcache->lock;
  272. spin_lock(cache_lock);
  273. if (atomic_read(&cred->cr_count) == 0) {
  274. get_rpccred(cred);
  275. list_add_tail(&cred->cr_lru, free);
  276. rpcauth_unhash_cred_locked(cred);
  277. }
  278. spin_unlock(cache_lock);
  279. }
  280. return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
  281. }
  282. /*
  283. * Run memory cache shrinker.
  284. */
  285. static int
  286. rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
  287. {
  288. LIST_HEAD(free);
  289. int res;
  290. int nr_to_scan = sc->nr_to_scan;
  291. gfp_t gfp_mask = sc->gfp_mask;
  292. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  293. return (nr_to_scan == 0) ? 0 : -1;
  294. if (list_empty(&cred_unused))
  295. return 0;
  296. spin_lock(&rpc_credcache_lock);
  297. res = rpcauth_prune_expired(&free, nr_to_scan);
  298. spin_unlock(&rpc_credcache_lock);
  299. rpcauth_destroy_credlist(&free);
  300. return res;
  301. }
  302. /*
  303. * Look up a process' credentials in the authentication cache
  304. */
  305. struct rpc_cred *
  306. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  307. int flags)
  308. {
  309. LIST_HEAD(free);
  310. struct rpc_cred_cache *cache = auth->au_credcache;
  311. struct hlist_node *pos;
  312. struct rpc_cred *cred = NULL,
  313. *entry, *new;
  314. unsigned int nr;
  315. nr = hash_long(acred->uid, cache->hashbits);
  316. rcu_read_lock();
  317. hlist_for_each_entry_rcu(entry, pos, &cache->hashtable[nr], cr_hash) {
  318. if (!entry->cr_ops->crmatch(acred, entry, flags))
  319. continue;
  320. spin_lock(&cache->lock);
  321. if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
  322. spin_unlock(&cache->lock);
  323. continue;
  324. }
  325. cred = get_rpccred(entry);
  326. spin_unlock(&cache->lock);
  327. break;
  328. }
  329. rcu_read_unlock();
  330. if (cred != NULL)
  331. goto found;
  332. new = auth->au_ops->crcreate(auth, acred, flags);
  333. if (IS_ERR(new)) {
  334. cred = new;
  335. goto out;
  336. }
  337. spin_lock(&cache->lock);
  338. hlist_for_each_entry(entry, pos, &cache->hashtable[nr], cr_hash) {
  339. if (!entry->cr_ops->crmatch(acred, entry, flags))
  340. continue;
  341. cred = get_rpccred(entry);
  342. break;
  343. }
  344. if (cred == NULL) {
  345. cred = new;
  346. set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  347. hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
  348. } else
  349. list_add_tail(&new->cr_lru, &free);
  350. spin_unlock(&cache->lock);
  351. found:
  352. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  353. cred->cr_ops->cr_init != NULL &&
  354. !(flags & RPCAUTH_LOOKUP_NEW)) {
  355. int res = cred->cr_ops->cr_init(auth, cred);
  356. if (res < 0) {
  357. put_rpccred(cred);
  358. cred = ERR_PTR(res);
  359. }
  360. }
  361. rpcauth_destroy_credlist(&free);
  362. out:
  363. return cred;
  364. }
  365. EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
  366. struct rpc_cred *
  367. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  368. {
  369. struct auth_cred acred;
  370. struct rpc_cred *ret;
  371. const struct cred *cred = current_cred();
  372. dprintk("RPC: looking up %s cred\n",
  373. auth->au_ops->au_name);
  374. memset(&acred, 0, sizeof(acred));
  375. acred.uid = cred->fsuid;
  376. acred.gid = cred->fsgid;
  377. acred.group_info = get_group_info(((struct cred *)cred)->group_info);
  378. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  379. put_group_info(acred.group_info);
  380. return ret;
  381. }
  382. void
  383. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  384. struct rpc_auth *auth, const struct rpc_credops *ops)
  385. {
  386. INIT_HLIST_NODE(&cred->cr_hash);
  387. INIT_LIST_HEAD(&cred->cr_lru);
  388. atomic_set(&cred->cr_count, 1);
  389. cred->cr_auth = auth;
  390. cred->cr_ops = ops;
  391. cred->cr_expire = jiffies;
  392. #ifdef RPC_DEBUG
  393. cred->cr_magic = RPCAUTH_CRED_MAGIC;
  394. #endif
  395. cred->cr_uid = acred->uid;
  396. }
  397. EXPORT_SYMBOL_GPL(rpcauth_init_cred);
  398. struct rpc_cred *
  399. rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
  400. {
  401. dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
  402. cred->cr_auth->au_ops->au_name, cred);
  403. return get_rpccred(cred);
  404. }
  405. EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
  406. static struct rpc_cred *
  407. rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
  408. {
  409. struct rpc_auth *auth = task->tk_client->cl_auth;
  410. struct auth_cred acred = {
  411. .uid = 0,
  412. .gid = 0,
  413. };
  414. dprintk("RPC: %5u looking up %s cred\n",
  415. task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
  416. return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  417. }
  418. static struct rpc_cred *
  419. rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
  420. {
  421. struct rpc_auth *auth = task->tk_client->cl_auth;
  422. dprintk("RPC: %5u looking up %s cred\n",
  423. task->tk_pid, auth->au_ops->au_name);
  424. return rpcauth_lookupcred(auth, lookupflags);
  425. }
  426. static int
  427. rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
  428. {
  429. struct rpc_rqst *req = task->tk_rqstp;
  430. struct rpc_cred *new;
  431. int lookupflags = 0;
  432. if (flags & RPC_TASK_ASYNC)
  433. lookupflags |= RPCAUTH_LOOKUP_NEW;
  434. if (cred != NULL)
  435. new = cred->cr_ops->crbind(task, cred, lookupflags);
  436. else if (flags & RPC_TASK_ROOTCREDS)
  437. new = rpcauth_bind_root_cred(task, lookupflags);
  438. else
  439. new = rpcauth_bind_new_cred(task, lookupflags);
  440. if (IS_ERR(new))
  441. return PTR_ERR(new);
  442. if (req->rq_cred != NULL)
  443. put_rpccred(req->rq_cred);
  444. req->rq_cred = new;
  445. return 0;
  446. }
  447. void
  448. put_rpccred(struct rpc_cred *cred)
  449. {
  450. /* Fast path for unhashed credentials */
  451. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
  452. if (atomic_dec_and_test(&cred->cr_count))
  453. cred->cr_ops->crdestroy(cred);
  454. return;
  455. }
  456. if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
  457. return;
  458. if (!list_empty(&cred->cr_lru)) {
  459. number_cred_unused--;
  460. list_del_init(&cred->cr_lru);
  461. }
  462. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
  463. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  464. cred->cr_expire = jiffies;
  465. list_add_tail(&cred->cr_lru, &cred_unused);
  466. number_cred_unused++;
  467. goto out_nodestroy;
  468. }
  469. if (!rpcauth_unhash_cred(cred)) {
  470. /* We were hashed and someone looked us up... */
  471. goto out_nodestroy;
  472. }
  473. }
  474. spin_unlock(&rpc_credcache_lock);
  475. cred->cr_ops->crdestroy(cred);
  476. return;
  477. out_nodestroy:
  478. spin_unlock(&rpc_credcache_lock);
  479. }
  480. EXPORT_SYMBOL_GPL(put_rpccred);
  481. __be32 *
  482. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  483. {
  484. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  485. dprintk("RPC: %5u marshaling %s cred %p\n",
  486. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  487. return cred->cr_ops->crmarshal(task, p);
  488. }
  489. __be32 *
  490. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  491. {
  492. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  493. dprintk("RPC: %5u validating %s cred %p\n",
  494. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  495. return cred->cr_ops->crvalidate(task, p);
  496. }
  497. static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  498. __be32 *data, void *obj)
  499. {
  500. struct xdr_stream xdr;
  501. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
  502. encode(rqstp, &xdr, obj);
  503. }
  504. int
  505. rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
  506. __be32 *data, void *obj)
  507. {
  508. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  509. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  510. task->tk_pid, cred->cr_ops->cr_name, cred);
  511. if (cred->cr_ops->crwrap_req)
  512. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  513. /* By default, we encode the arguments normally. */
  514. rpcauth_wrap_req_encode(encode, rqstp, data, obj);
  515. return 0;
  516. }
  517. static int
  518. rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  519. __be32 *data, void *obj)
  520. {
  521. struct xdr_stream xdr;
  522. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
  523. return decode(rqstp, &xdr, obj);
  524. }
  525. int
  526. rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
  527. __be32 *data, void *obj)
  528. {
  529. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  530. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  531. task->tk_pid, cred->cr_ops->cr_name, cred);
  532. if (cred->cr_ops->crunwrap_resp)
  533. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  534. data, obj);
  535. /* By default, we decode the arguments normally. */
  536. return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
  537. }
  538. int
  539. rpcauth_refreshcred(struct rpc_task *task)
  540. {
  541. struct rpc_cred *cred;
  542. int err;
  543. cred = task->tk_rqstp->rq_cred;
  544. if (cred == NULL) {
  545. err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
  546. if (err < 0)
  547. goto out;
  548. cred = task->tk_rqstp->rq_cred;
  549. };
  550. dprintk("RPC: %5u refreshing %s cred %p\n",
  551. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  552. err = cred->cr_ops->crrefresh(task);
  553. out:
  554. if (err < 0)
  555. task->tk_status = err;
  556. return err;
  557. }
  558. void
  559. rpcauth_invalcred(struct rpc_task *task)
  560. {
  561. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  562. dprintk("RPC: %5u invalidating %s cred %p\n",
  563. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  564. if (cred)
  565. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  566. }
  567. int
  568. rpcauth_uptodatecred(struct rpc_task *task)
  569. {
  570. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  571. return cred == NULL ||
  572. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  573. }
  574. static struct shrinker rpc_cred_shrinker = {
  575. .shrink = rpcauth_cache_shrinker,
  576. .seeks = DEFAULT_SEEKS,
  577. };
  578. int __init rpcauth_init_module(void)
  579. {
  580. int err;
  581. err = rpc_init_authunix();
  582. if (err < 0)
  583. goto out1;
  584. err = rpc_init_generic_auth();
  585. if (err < 0)
  586. goto out2;
  587. register_shrinker(&rpc_cred_shrinker);
  588. return 0;
  589. out2:
  590. rpc_destroy_authunix();
  591. out1:
  592. return err;
  593. }
  594. void rpcauth_remove_module(void)
  595. {
  596. rpc_destroy_authunix();
  597. rpc_destroy_generic_auth();
  598. unregister_shrinker(&rpc_cred_shrinker);
  599. }