auth.c 12 KB

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