auth.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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_get_pseudoflavor - check if security flavor is supported
  109. * @flavor: a security flavor
  110. * @info: a GSS mech OID, quality of protection, and service value
  111. *
  112. * Verifies that an appropriate kernel module is available or already loaded.
  113. * Returns an equivalent pseudoflavor, or RPC_AUTH_MAXFLAVOR if "flavor" is
  114. * not supported locally.
  115. */
  116. rpc_authflavor_t
  117. rpcauth_get_pseudoflavor(rpc_authflavor_t flavor, struct rpcsec_gss_info *info)
  118. {
  119. const struct rpc_authops *ops;
  120. rpc_authflavor_t pseudoflavor;
  121. ops = auth_flavors[flavor];
  122. if (ops == NULL)
  123. request_module("rpc-auth-%u", flavor);
  124. spin_lock(&rpc_authflavor_lock);
  125. ops = auth_flavors[flavor];
  126. if (ops == NULL || !try_module_get(ops->owner)) {
  127. spin_unlock(&rpc_authflavor_lock);
  128. return RPC_AUTH_MAXFLAVOR;
  129. }
  130. spin_unlock(&rpc_authflavor_lock);
  131. pseudoflavor = flavor;
  132. if (ops->info2flavor != NULL)
  133. pseudoflavor = ops->info2flavor(info);
  134. module_put(ops->owner);
  135. return pseudoflavor;
  136. }
  137. EXPORT_SYMBOL_GPL(rpcauth_get_pseudoflavor);
  138. /**
  139. * rpcauth_get_gssinfo - find GSS tuple matching a GSS pseudoflavor
  140. * @pseudoflavor: GSS pseudoflavor to match
  141. * @info: rpcsec_gss_info structure to fill in
  142. *
  143. * Returns zero and fills in "info" if pseudoflavor matches a
  144. * supported mechanism.
  145. */
  146. int
  147. rpcauth_get_gssinfo(rpc_authflavor_t pseudoflavor, struct rpcsec_gss_info *info)
  148. {
  149. rpc_authflavor_t flavor = pseudoflavor_to_flavor(pseudoflavor);
  150. const struct rpc_authops *ops;
  151. int result;
  152. if (flavor >= RPC_AUTH_MAXFLAVOR)
  153. return -EINVAL;
  154. ops = auth_flavors[flavor];
  155. if (ops == NULL)
  156. request_module("rpc-auth-%u", flavor);
  157. spin_lock(&rpc_authflavor_lock);
  158. ops = auth_flavors[flavor];
  159. if (ops == NULL || !try_module_get(ops->owner)) {
  160. spin_unlock(&rpc_authflavor_lock);
  161. return -ENOENT;
  162. }
  163. spin_unlock(&rpc_authflavor_lock);
  164. result = -ENOENT;
  165. if (ops->flavor2info != NULL)
  166. result = ops->flavor2info(pseudoflavor, info);
  167. module_put(ops->owner);
  168. return result;
  169. }
  170. EXPORT_SYMBOL_GPL(rpcauth_get_gssinfo);
  171. /**
  172. * rpcauth_list_flavors - discover registered flavors and pseudoflavors
  173. * @array: array to fill in
  174. * @size: size of "array"
  175. *
  176. * Returns the number of array items filled in, or a negative errno.
  177. *
  178. * The returned array is not sorted by any policy. Callers should not
  179. * rely on the order of the items in the returned array.
  180. */
  181. int
  182. rpcauth_list_flavors(rpc_authflavor_t *array, int size)
  183. {
  184. rpc_authflavor_t flavor;
  185. int result = 0;
  186. spin_lock(&rpc_authflavor_lock);
  187. for (flavor = 0; flavor < RPC_AUTH_MAXFLAVOR; flavor++) {
  188. const struct rpc_authops *ops = auth_flavors[flavor];
  189. rpc_authflavor_t pseudos[4];
  190. int i, len;
  191. if (result >= size) {
  192. result = -ENOMEM;
  193. break;
  194. }
  195. if (ops == NULL)
  196. continue;
  197. if (ops->list_pseudoflavors == NULL) {
  198. array[result++] = ops->au_flavor;
  199. continue;
  200. }
  201. len = ops->list_pseudoflavors(pseudos, ARRAY_SIZE(pseudos));
  202. if (len < 0) {
  203. result = len;
  204. break;
  205. }
  206. for (i = 0; i < len; i++) {
  207. if (result >= size) {
  208. result = -ENOMEM;
  209. break;
  210. }
  211. array[result++] = pseudos[i];
  212. }
  213. }
  214. spin_unlock(&rpc_authflavor_lock);
  215. dprintk("RPC: %s returns %d\n", __func__, result);
  216. return result;
  217. }
  218. EXPORT_SYMBOL_GPL(rpcauth_list_flavors);
  219. struct rpc_auth *
  220. rpcauth_create(struct rpc_auth_create_args *args, struct rpc_clnt *clnt)
  221. {
  222. struct rpc_auth *auth;
  223. const struct rpc_authops *ops;
  224. u32 flavor = pseudoflavor_to_flavor(args->pseudoflavor);
  225. auth = ERR_PTR(-EINVAL);
  226. if (flavor >= RPC_AUTH_MAXFLAVOR)
  227. goto out;
  228. if ((ops = auth_flavors[flavor]) == NULL)
  229. request_module("rpc-auth-%u", flavor);
  230. spin_lock(&rpc_authflavor_lock);
  231. ops = auth_flavors[flavor];
  232. if (ops == NULL || !try_module_get(ops->owner)) {
  233. spin_unlock(&rpc_authflavor_lock);
  234. goto out;
  235. }
  236. spin_unlock(&rpc_authflavor_lock);
  237. auth = ops->create(args, clnt);
  238. module_put(ops->owner);
  239. if (IS_ERR(auth))
  240. return auth;
  241. if (clnt->cl_auth)
  242. rpcauth_release(clnt->cl_auth);
  243. clnt->cl_auth = auth;
  244. out:
  245. return auth;
  246. }
  247. EXPORT_SYMBOL_GPL(rpcauth_create);
  248. void
  249. rpcauth_release(struct rpc_auth *auth)
  250. {
  251. if (!atomic_dec_and_test(&auth->au_count))
  252. return;
  253. auth->au_ops->destroy(auth);
  254. }
  255. static DEFINE_SPINLOCK(rpc_credcache_lock);
  256. static void
  257. rpcauth_unhash_cred_locked(struct rpc_cred *cred)
  258. {
  259. hlist_del_rcu(&cred->cr_hash);
  260. smp_mb__before_clear_bit();
  261. clear_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  262. }
  263. static int
  264. rpcauth_unhash_cred(struct rpc_cred *cred)
  265. {
  266. spinlock_t *cache_lock;
  267. int ret;
  268. cache_lock = &cred->cr_auth->au_credcache->lock;
  269. spin_lock(cache_lock);
  270. ret = atomic_read(&cred->cr_count) == 0;
  271. if (ret)
  272. rpcauth_unhash_cred_locked(cred);
  273. spin_unlock(cache_lock);
  274. return ret;
  275. }
  276. /*
  277. * Initialize RPC credential cache
  278. */
  279. int
  280. rpcauth_init_credcache(struct rpc_auth *auth)
  281. {
  282. struct rpc_cred_cache *new;
  283. unsigned int hashsize;
  284. new = kmalloc(sizeof(*new), GFP_KERNEL);
  285. if (!new)
  286. goto out_nocache;
  287. new->hashbits = auth_hashbits;
  288. hashsize = 1U << new->hashbits;
  289. new->hashtable = kcalloc(hashsize, sizeof(new->hashtable[0]), GFP_KERNEL);
  290. if (!new->hashtable)
  291. goto out_nohashtbl;
  292. spin_lock_init(&new->lock);
  293. auth->au_credcache = new;
  294. return 0;
  295. out_nohashtbl:
  296. kfree(new);
  297. out_nocache:
  298. return -ENOMEM;
  299. }
  300. EXPORT_SYMBOL_GPL(rpcauth_init_credcache);
  301. /*
  302. * Setup a credential key lifetime timeout notification
  303. */
  304. int
  305. rpcauth_key_timeout_notify(struct rpc_auth *auth, struct rpc_cred *cred)
  306. {
  307. if (!cred->cr_auth->au_ops->key_timeout)
  308. return 0;
  309. return cred->cr_auth->au_ops->key_timeout(auth, cred);
  310. }
  311. EXPORT_SYMBOL_GPL(rpcauth_key_timeout_notify);
  312. bool
  313. rpcauth_cred_key_to_expire(struct rpc_cred *cred)
  314. {
  315. if (!cred->cr_ops->crkey_to_expire)
  316. return false;
  317. return cred->cr_ops->crkey_to_expire(cred);
  318. }
  319. EXPORT_SYMBOL_GPL(rpcauth_cred_key_to_expire);
  320. /*
  321. * Destroy a list of credentials
  322. */
  323. static inline
  324. void rpcauth_destroy_credlist(struct list_head *head)
  325. {
  326. struct rpc_cred *cred;
  327. while (!list_empty(head)) {
  328. cred = list_entry(head->next, struct rpc_cred, cr_lru);
  329. list_del_init(&cred->cr_lru);
  330. put_rpccred(cred);
  331. }
  332. }
  333. /*
  334. * Clear the RPC credential cache, and delete those credentials
  335. * that are not referenced.
  336. */
  337. void
  338. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  339. {
  340. LIST_HEAD(free);
  341. struct hlist_head *head;
  342. struct rpc_cred *cred;
  343. unsigned int hashsize = 1U << cache->hashbits;
  344. int i;
  345. spin_lock(&rpc_credcache_lock);
  346. spin_lock(&cache->lock);
  347. for (i = 0; i < hashsize; i++) {
  348. head = &cache->hashtable[i];
  349. while (!hlist_empty(head)) {
  350. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  351. get_rpccred(cred);
  352. if (!list_empty(&cred->cr_lru)) {
  353. list_del(&cred->cr_lru);
  354. number_cred_unused--;
  355. }
  356. list_add_tail(&cred->cr_lru, &free);
  357. rpcauth_unhash_cred_locked(cred);
  358. }
  359. }
  360. spin_unlock(&cache->lock);
  361. spin_unlock(&rpc_credcache_lock);
  362. rpcauth_destroy_credlist(&free);
  363. }
  364. /*
  365. * Destroy the RPC credential cache
  366. */
  367. void
  368. rpcauth_destroy_credcache(struct rpc_auth *auth)
  369. {
  370. struct rpc_cred_cache *cache = auth->au_credcache;
  371. if (cache) {
  372. auth->au_credcache = NULL;
  373. rpcauth_clear_credcache(cache);
  374. kfree(cache->hashtable);
  375. kfree(cache);
  376. }
  377. }
  378. EXPORT_SYMBOL_GPL(rpcauth_destroy_credcache);
  379. #define RPC_AUTH_EXPIRY_MORATORIUM (60 * HZ)
  380. /*
  381. * Remove stale credentials. Avoid sleeping inside the loop.
  382. */
  383. static int
  384. rpcauth_prune_expired(struct list_head *free, int nr_to_scan)
  385. {
  386. spinlock_t *cache_lock;
  387. struct rpc_cred *cred, *next;
  388. unsigned long expired = jiffies - RPC_AUTH_EXPIRY_MORATORIUM;
  389. list_for_each_entry_safe(cred, next, &cred_unused, cr_lru) {
  390. if (nr_to_scan-- == 0)
  391. break;
  392. /*
  393. * Enforce a 60 second garbage collection moratorium
  394. * Note that the cred_unused list must be time-ordered.
  395. */
  396. if (time_in_range(cred->cr_expire, expired, jiffies) &&
  397. test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0)
  398. return 0;
  399. list_del_init(&cred->cr_lru);
  400. number_cred_unused--;
  401. if (atomic_read(&cred->cr_count) != 0)
  402. continue;
  403. cache_lock = &cred->cr_auth->au_credcache->lock;
  404. spin_lock(cache_lock);
  405. if (atomic_read(&cred->cr_count) == 0) {
  406. get_rpccred(cred);
  407. list_add_tail(&cred->cr_lru, free);
  408. rpcauth_unhash_cred_locked(cred);
  409. }
  410. spin_unlock(cache_lock);
  411. }
  412. return (number_cred_unused / 100) * sysctl_vfs_cache_pressure;
  413. }
  414. /*
  415. * Run memory cache shrinker.
  416. */
  417. static int
  418. rpcauth_cache_shrinker(struct shrinker *shrink, struct shrink_control *sc)
  419. {
  420. LIST_HEAD(free);
  421. int res;
  422. int nr_to_scan = sc->nr_to_scan;
  423. gfp_t gfp_mask = sc->gfp_mask;
  424. if ((gfp_mask & GFP_KERNEL) != GFP_KERNEL)
  425. return (nr_to_scan == 0) ? 0 : -1;
  426. if (list_empty(&cred_unused))
  427. return 0;
  428. spin_lock(&rpc_credcache_lock);
  429. res = rpcauth_prune_expired(&free, nr_to_scan);
  430. spin_unlock(&rpc_credcache_lock);
  431. rpcauth_destroy_credlist(&free);
  432. return res;
  433. }
  434. /*
  435. * Look up a process' credentials in the authentication cache
  436. */
  437. struct rpc_cred *
  438. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  439. int flags)
  440. {
  441. LIST_HEAD(free);
  442. struct rpc_cred_cache *cache = auth->au_credcache;
  443. struct rpc_cred *cred = NULL,
  444. *entry, *new;
  445. unsigned int nr;
  446. nr = hash_long(from_kuid(&init_user_ns, acred->uid), cache->hashbits);
  447. rcu_read_lock();
  448. hlist_for_each_entry_rcu(entry, &cache->hashtable[nr], cr_hash) {
  449. if (!entry->cr_ops->crmatch(acred, entry, flags))
  450. continue;
  451. spin_lock(&cache->lock);
  452. if (test_bit(RPCAUTH_CRED_HASHED, &entry->cr_flags) == 0) {
  453. spin_unlock(&cache->lock);
  454. continue;
  455. }
  456. cred = get_rpccred(entry);
  457. spin_unlock(&cache->lock);
  458. break;
  459. }
  460. rcu_read_unlock();
  461. if (cred != NULL)
  462. goto found;
  463. new = auth->au_ops->crcreate(auth, acred, flags);
  464. if (IS_ERR(new)) {
  465. cred = new;
  466. goto out;
  467. }
  468. spin_lock(&cache->lock);
  469. hlist_for_each_entry(entry, &cache->hashtable[nr], cr_hash) {
  470. if (!entry->cr_ops->crmatch(acred, entry, flags))
  471. continue;
  472. cred = get_rpccred(entry);
  473. break;
  474. }
  475. if (cred == NULL) {
  476. cred = new;
  477. set_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags);
  478. hlist_add_head_rcu(&cred->cr_hash, &cache->hashtable[nr]);
  479. } else
  480. list_add_tail(&new->cr_lru, &free);
  481. spin_unlock(&cache->lock);
  482. found:
  483. if (test_bit(RPCAUTH_CRED_NEW, &cred->cr_flags) &&
  484. cred->cr_ops->cr_init != NULL &&
  485. !(flags & RPCAUTH_LOOKUP_NEW)) {
  486. int res = cred->cr_ops->cr_init(auth, cred);
  487. if (res < 0) {
  488. put_rpccred(cred);
  489. cred = ERR_PTR(res);
  490. }
  491. }
  492. rpcauth_destroy_credlist(&free);
  493. out:
  494. return cred;
  495. }
  496. EXPORT_SYMBOL_GPL(rpcauth_lookup_credcache);
  497. struct rpc_cred *
  498. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  499. {
  500. struct auth_cred acred;
  501. struct rpc_cred *ret;
  502. const struct cred *cred = current_cred();
  503. dprintk("RPC: looking up %s cred\n",
  504. auth->au_ops->au_name);
  505. memset(&acred, 0, sizeof(acred));
  506. acred.uid = cred->fsuid;
  507. acred.gid = cred->fsgid;
  508. acred.group_info = get_group_info(((struct cred *)cred)->group_info);
  509. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  510. put_group_info(acred.group_info);
  511. return ret;
  512. }
  513. void
  514. rpcauth_init_cred(struct rpc_cred *cred, const struct auth_cred *acred,
  515. struct rpc_auth *auth, const struct rpc_credops *ops)
  516. {
  517. INIT_HLIST_NODE(&cred->cr_hash);
  518. INIT_LIST_HEAD(&cred->cr_lru);
  519. atomic_set(&cred->cr_count, 1);
  520. cred->cr_auth = auth;
  521. cred->cr_ops = ops;
  522. cred->cr_expire = jiffies;
  523. #ifdef RPC_DEBUG
  524. cred->cr_magic = RPCAUTH_CRED_MAGIC;
  525. #endif
  526. cred->cr_uid = acred->uid;
  527. }
  528. EXPORT_SYMBOL_GPL(rpcauth_init_cred);
  529. struct rpc_cred *
  530. rpcauth_generic_bind_cred(struct rpc_task *task, struct rpc_cred *cred, int lookupflags)
  531. {
  532. dprintk("RPC: %5u holding %s cred %p\n", task->tk_pid,
  533. cred->cr_auth->au_ops->au_name, cred);
  534. return get_rpccred(cred);
  535. }
  536. EXPORT_SYMBOL_GPL(rpcauth_generic_bind_cred);
  537. static struct rpc_cred *
  538. rpcauth_bind_root_cred(struct rpc_task *task, int lookupflags)
  539. {
  540. struct rpc_auth *auth = task->tk_client->cl_auth;
  541. struct auth_cred acred = {
  542. .uid = GLOBAL_ROOT_UID,
  543. .gid = GLOBAL_ROOT_GID,
  544. };
  545. dprintk("RPC: %5u looking up %s cred\n",
  546. task->tk_pid, task->tk_client->cl_auth->au_ops->au_name);
  547. return auth->au_ops->lookup_cred(auth, &acred, lookupflags);
  548. }
  549. static struct rpc_cred *
  550. rpcauth_bind_new_cred(struct rpc_task *task, int lookupflags)
  551. {
  552. struct rpc_auth *auth = task->tk_client->cl_auth;
  553. dprintk("RPC: %5u looking up %s cred\n",
  554. task->tk_pid, auth->au_ops->au_name);
  555. return rpcauth_lookupcred(auth, lookupflags);
  556. }
  557. static int
  558. rpcauth_bindcred(struct rpc_task *task, struct rpc_cred *cred, int flags)
  559. {
  560. struct rpc_rqst *req = task->tk_rqstp;
  561. struct rpc_cred *new;
  562. int lookupflags = 0;
  563. if (flags & RPC_TASK_ASYNC)
  564. lookupflags |= RPCAUTH_LOOKUP_NEW;
  565. if (cred != NULL)
  566. new = cred->cr_ops->crbind(task, cred, lookupflags);
  567. else if (flags & RPC_TASK_ROOTCREDS)
  568. new = rpcauth_bind_root_cred(task, lookupflags);
  569. else
  570. new = rpcauth_bind_new_cred(task, lookupflags);
  571. if (IS_ERR(new))
  572. return PTR_ERR(new);
  573. if (req->rq_cred != NULL)
  574. put_rpccred(req->rq_cred);
  575. req->rq_cred = new;
  576. return 0;
  577. }
  578. void
  579. put_rpccred(struct rpc_cred *cred)
  580. {
  581. /* Fast path for unhashed credentials */
  582. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) == 0) {
  583. if (atomic_dec_and_test(&cred->cr_count))
  584. cred->cr_ops->crdestroy(cred);
  585. return;
  586. }
  587. if (!atomic_dec_and_lock(&cred->cr_count, &rpc_credcache_lock))
  588. return;
  589. if (!list_empty(&cred->cr_lru)) {
  590. number_cred_unused--;
  591. list_del_init(&cred->cr_lru);
  592. }
  593. if (test_bit(RPCAUTH_CRED_HASHED, &cred->cr_flags) != 0) {
  594. if (test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0) {
  595. cred->cr_expire = jiffies;
  596. list_add_tail(&cred->cr_lru, &cred_unused);
  597. number_cred_unused++;
  598. goto out_nodestroy;
  599. }
  600. if (!rpcauth_unhash_cred(cred)) {
  601. /* We were hashed and someone looked us up... */
  602. goto out_nodestroy;
  603. }
  604. }
  605. spin_unlock(&rpc_credcache_lock);
  606. cred->cr_ops->crdestroy(cred);
  607. return;
  608. out_nodestroy:
  609. spin_unlock(&rpc_credcache_lock);
  610. }
  611. EXPORT_SYMBOL_GPL(put_rpccred);
  612. __be32 *
  613. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  614. {
  615. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  616. dprintk("RPC: %5u marshaling %s cred %p\n",
  617. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  618. return cred->cr_ops->crmarshal(task, p);
  619. }
  620. __be32 *
  621. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  622. {
  623. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  624. dprintk("RPC: %5u validating %s cred %p\n",
  625. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  626. return cred->cr_ops->crvalidate(task, p);
  627. }
  628. static void rpcauth_wrap_req_encode(kxdreproc_t encode, struct rpc_rqst *rqstp,
  629. __be32 *data, void *obj)
  630. {
  631. struct xdr_stream xdr;
  632. xdr_init_encode(&xdr, &rqstp->rq_snd_buf, data);
  633. encode(rqstp, &xdr, obj);
  634. }
  635. int
  636. rpcauth_wrap_req(struct rpc_task *task, kxdreproc_t encode, void *rqstp,
  637. __be32 *data, void *obj)
  638. {
  639. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  640. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  641. task->tk_pid, cred->cr_ops->cr_name, cred);
  642. if (cred->cr_ops->crwrap_req)
  643. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  644. /* By default, we encode the arguments normally. */
  645. rpcauth_wrap_req_encode(encode, rqstp, data, obj);
  646. return 0;
  647. }
  648. static int
  649. rpcauth_unwrap_req_decode(kxdrdproc_t decode, struct rpc_rqst *rqstp,
  650. __be32 *data, void *obj)
  651. {
  652. struct xdr_stream xdr;
  653. xdr_init_decode(&xdr, &rqstp->rq_rcv_buf, data);
  654. return decode(rqstp, &xdr, obj);
  655. }
  656. int
  657. rpcauth_unwrap_resp(struct rpc_task *task, kxdrdproc_t decode, void *rqstp,
  658. __be32 *data, void *obj)
  659. {
  660. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  661. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  662. task->tk_pid, cred->cr_ops->cr_name, cred);
  663. if (cred->cr_ops->crunwrap_resp)
  664. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  665. data, obj);
  666. /* By default, we decode the arguments normally. */
  667. return rpcauth_unwrap_req_decode(decode, rqstp, data, obj);
  668. }
  669. int
  670. rpcauth_refreshcred(struct rpc_task *task)
  671. {
  672. struct rpc_cred *cred;
  673. int err;
  674. cred = task->tk_rqstp->rq_cred;
  675. if (cred == NULL) {
  676. err = rpcauth_bindcred(task, task->tk_msg.rpc_cred, task->tk_flags);
  677. if (err < 0)
  678. goto out;
  679. cred = task->tk_rqstp->rq_cred;
  680. }
  681. dprintk("RPC: %5u refreshing %s cred %p\n",
  682. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  683. err = cred->cr_ops->crrefresh(task);
  684. out:
  685. if (err < 0)
  686. task->tk_status = err;
  687. return err;
  688. }
  689. void
  690. rpcauth_invalcred(struct rpc_task *task)
  691. {
  692. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  693. dprintk("RPC: %5u invalidating %s cred %p\n",
  694. task->tk_pid, cred->cr_auth->au_ops->au_name, cred);
  695. if (cred)
  696. clear_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags);
  697. }
  698. int
  699. rpcauth_uptodatecred(struct rpc_task *task)
  700. {
  701. struct rpc_cred *cred = task->tk_rqstp->rq_cred;
  702. return cred == NULL ||
  703. test_bit(RPCAUTH_CRED_UPTODATE, &cred->cr_flags) != 0;
  704. }
  705. static struct shrinker rpc_cred_shrinker = {
  706. .shrink = rpcauth_cache_shrinker,
  707. .seeks = DEFAULT_SEEKS,
  708. };
  709. int __init rpcauth_init_module(void)
  710. {
  711. int err;
  712. err = rpc_init_authunix();
  713. if (err < 0)
  714. goto out1;
  715. err = rpc_init_generic_auth();
  716. if (err < 0)
  717. goto out2;
  718. register_shrinker(&rpc_cred_shrinker);
  719. return 0;
  720. out2:
  721. rpc_destroy_authunix();
  722. out1:
  723. return err;
  724. }
  725. void rpcauth_remove_module(void)
  726. {
  727. rpc_destroy_authunix();
  728. rpc_destroy_generic_auth();
  729. unregister_shrinker(&rpc_cred_shrinker);
  730. }