auth.c 17 KB

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