auth.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 struct rpc_authops * auth_flavors[RPC_AUTH_MAXFLAVOR] = {
  19. &authnull_ops, /* AUTH_NULL */
  20. &authunix_ops, /* AUTH_UNIX */
  21. NULL, /* others can be loadable modules */
  22. };
  23. static u32
  24. pseudoflavor_to_flavor(u32 flavor) {
  25. if (flavor >= RPC_AUTH_MAXFLAVOR)
  26. return RPC_AUTH_GSS;
  27. return flavor;
  28. }
  29. int
  30. rpcauth_register(struct rpc_authops *ops)
  31. {
  32. rpc_authflavor_t flavor;
  33. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  34. return -EINVAL;
  35. if (auth_flavors[flavor] != NULL)
  36. return -EPERM; /* what else? */
  37. auth_flavors[flavor] = ops;
  38. return 0;
  39. }
  40. int
  41. rpcauth_unregister(struct rpc_authops *ops)
  42. {
  43. rpc_authflavor_t flavor;
  44. if ((flavor = ops->au_flavor) >= RPC_AUTH_MAXFLAVOR)
  45. return -EINVAL;
  46. if (auth_flavors[flavor] != ops)
  47. return -EPERM; /* what else? */
  48. auth_flavors[flavor] = NULL;
  49. return 0;
  50. }
  51. struct rpc_auth *
  52. rpcauth_create(rpc_authflavor_t pseudoflavor, struct rpc_clnt *clnt)
  53. {
  54. struct rpc_auth *auth;
  55. struct rpc_authops *ops;
  56. u32 flavor = pseudoflavor_to_flavor(pseudoflavor);
  57. auth = ERR_PTR(-EINVAL);
  58. if (flavor >= RPC_AUTH_MAXFLAVOR)
  59. goto out;
  60. /* FIXME - auth_flavors[] really needs an rw lock,
  61. * and module refcounting. */
  62. #ifdef CONFIG_KMOD
  63. if ((ops = auth_flavors[flavor]) == NULL)
  64. request_module("rpc-auth-%u", flavor);
  65. #endif
  66. if ((ops = auth_flavors[flavor]) == NULL)
  67. goto out;
  68. auth = ops->create(clnt, pseudoflavor);
  69. if (IS_ERR(auth))
  70. return auth;
  71. if (clnt->cl_auth)
  72. rpcauth_destroy(clnt->cl_auth);
  73. clnt->cl_auth = auth;
  74. out:
  75. return auth;
  76. }
  77. void
  78. rpcauth_destroy(struct rpc_auth *auth)
  79. {
  80. if (!atomic_dec_and_test(&auth->au_count))
  81. return;
  82. auth->au_ops->destroy(auth);
  83. }
  84. static DEFINE_SPINLOCK(rpc_credcache_lock);
  85. /*
  86. * Initialize RPC credential cache
  87. */
  88. int
  89. rpcauth_init_credcache(struct rpc_auth *auth, unsigned long expire)
  90. {
  91. struct rpc_cred_cache *new;
  92. int i;
  93. new = kmalloc(sizeof(*new), GFP_KERNEL);
  94. if (!new)
  95. return -ENOMEM;
  96. for (i = 0; i < RPC_CREDCACHE_NR; i++)
  97. INIT_HLIST_HEAD(&new->hashtable[i]);
  98. new->expire = expire;
  99. new->nextgc = jiffies + (expire >> 1);
  100. auth->au_credcache = new;
  101. return 0;
  102. }
  103. /*
  104. * Destroy a list of credentials
  105. */
  106. static inline
  107. void rpcauth_destroy_credlist(struct hlist_head *head)
  108. {
  109. struct rpc_cred *cred;
  110. while (!hlist_empty(head)) {
  111. cred = hlist_entry(head->first, struct rpc_cred, cr_hash);
  112. hlist_del_init(&cred->cr_hash);
  113. put_rpccred(cred);
  114. }
  115. }
  116. /*
  117. * Clear the RPC credential cache, and delete those credentials
  118. * that are not referenced.
  119. */
  120. void
  121. rpcauth_clear_credcache(struct rpc_cred_cache *cache)
  122. {
  123. HLIST_HEAD(free);
  124. struct hlist_node *pos, *next;
  125. struct rpc_cred *cred;
  126. int i;
  127. spin_lock(&rpc_credcache_lock);
  128. for (i = 0; i < RPC_CREDCACHE_NR; i++) {
  129. hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
  130. cred = hlist_entry(pos, struct rpc_cred, cr_hash);
  131. __hlist_del(&cred->cr_hash);
  132. hlist_add_head(&cred->cr_hash, &free);
  133. }
  134. }
  135. spin_unlock(&rpc_credcache_lock);
  136. rpcauth_destroy_credlist(&free);
  137. }
  138. /*
  139. * Destroy the RPC credential cache
  140. */
  141. void
  142. rpcauth_destroy_credcache(struct rpc_auth *auth)
  143. {
  144. struct rpc_cred_cache *cache = auth->au_credcache;
  145. if (cache) {
  146. auth->au_credcache = NULL;
  147. rpcauth_clear_credcache(cache);
  148. kfree(cache);
  149. }
  150. }
  151. static void
  152. rpcauth_prune_expired(struct rpc_auth *auth, struct rpc_cred *cred, struct hlist_head *free)
  153. {
  154. if (atomic_read(&cred->cr_count) != 1)
  155. return;
  156. if (time_after(jiffies, cred->cr_expire + auth->au_credcache->expire))
  157. cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
  158. if (!(cred->cr_flags & RPCAUTH_CRED_UPTODATE)) {
  159. __hlist_del(&cred->cr_hash);
  160. hlist_add_head(&cred->cr_hash, free);
  161. }
  162. }
  163. /*
  164. * Remove stale credentials. Avoid sleeping inside the loop.
  165. */
  166. static void
  167. rpcauth_gc_credcache(struct rpc_auth *auth, struct hlist_head *free)
  168. {
  169. struct rpc_cred_cache *cache = auth->au_credcache;
  170. struct hlist_node *pos, *next;
  171. struct rpc_cred *cred;
  172. int i;
  173. dprintk("RPC: gc'ing RPC credentials for auth %p\n", auth);
  174. for (i = 0; i < RPC_CREDCACHE_NR; i++) {
  175. hlist_for_each_safe(pos, next, &cache->hashtable[i]) {
  176. cred = hlist_entry(pos, struct rpc_cred, cr_hash);
  177. rpcauth_prune_expired(auth, cred, free);
  178. }
  179. }
  180. cache->nextgc = jiffies + cache->expire;
  181. }
  182. /*
  183. * Look up a process' credentials in the authentication cache
  184. */
  185. struct rpc_cred *
  186. rpcauth_lookup_credcache(struct rpc_auth *auth, struct auth_cred * acred,
  187. int flags)
  188. {
  189. struct rpc_cred_cache *cache = auth->au_credcache;
  190. HLIST_HEAD(free);
  191. struct hlist_node *pos, *next;
  192. struct rpc_cred *new = NULL,
  193. *cred = NULL;
  194. int nr = 0;
  195. if (!(flags & RPCAUTH_LOOKUP_ROOTCREDS))
  196. nr = acred->uid & RPC_CREDCACHE_MASK;
  197. retry:
  198. spin_lock(&rpc_credcache_lock);
  199. if (time_before(cache->nextgc, jiffies))
  200. rpcauth_gc_credcache(auth, &free);
  201. hlist_for_each_safe(pos, next, &cache->hashtable[nr]) {
  202. struct rpc_cred *entry;
  203. entry = hlist_entry(pos, struct rpc_cred, cr_hash);
  204. if (entry->cr_ops->crmatch(acred, entry, flags)) {
  205. hlist_del(&entry->cr_hash);
  206. cred = entry;
  207. break;
  208. }
  209. rpcauth_prune_expired(auth, entry, &free);
  210. }
  211. if (new) {
  212. if (cred)
  213. hlist_add_head(&new->cr_hash, &free);
  214. else
  215. cred = new;
  216. }
  217. if (cred) {
  218. hlist_add_head(&cred->cr_hash, &cache->hashtable[nr]);
  219. get_rpccred(cred);
  220. }
  221. spin_unlock(&rpc_credcache_lock);
  222. rpcauth_destroy_credlist(&free);
  223. if (!cred) {
  224. new = auth->au_ops->crcreate(auth, acred, flags);
  225. if (!IS_ERR(new)) {
  226. #ifdef RPC_DEBUG
  227. new->cr_magic = RPCAUTH_CRED_MAGIC;
  228. #endif
  229. goto retry;
  230. } else
  231. cred = new;
  232. } else if ((cred->cr_flags & RPCAUTH_CRED_NEW)
  233. && cred->cr_ops->cr_init != NULL
  234. && !(flags & RPCAUTH_LOOKUP_NEW)) {
  235. int res = cred->cr_ops->cr_init(auth, cred);
  236. if (res < 0) {
  237. put_rpccred(cred);
  238. cred = ERR_PTR(res);
  239. }
  240. }
  241. return (struct rpc_cred *) cred;
  242. }
  243. struct rpc_cred *
  244. rpcauth_lookupcred(struct rpc_auth *auth, int flags)
  245. {
  246. struct auth_cred acred = {
  247. .uid = current->fsuid,
  248. .gid = current->fsgid,
  249. .group_info = current->group_info,
  250. };
  251. struct rpc_cred *ret;
  252. dprintk("RPC: looking up %s cred\n",
  253. auth->au_ops->au_name);
  254. get_group_info(acred.group_info);
  255. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  256. put_group_info(acred.group_info);
  257. return ret;
  258. }
  259. struct rpc_cred *
  260. rpcauth_bindcred(struct rpc_task *task)
  261. {
  262. struct rpc_auth *auth = task->tk_auth;
  263. struct auth_cred acred = {
  264. .uid = current->fsuid,
  265. .gid = current->fsgid,
  266. .group_info = current->group_info,
  267. };
  268. struct rpc_cred *ret;
  269. int flags = 0;
  270. dprintk("RPC: %5u looking up %s cred\n",
  271. task->tk_pid, task->tk_auth->au_ops->au_name);
  272. get_group_info(acred.group_info);
  273. if (task->tk_flags & RPC_TASK_ROOTCREDS)
  274. flags |= RPCAUTH_LOOKUP_ROOTCREDS;
  275. ret = auth->au_ops->lookup_cred(auth, &acred, flags);
  276. if (!IS_ERR(ret))
  277. task->tk_msg.rpc_cred = ret;
  278. else
  279. task->tk_status = PTR_ERR(ret);
  280. put_group_info(acred.group_info);
  281. return ret;
  282. }
  283. void
  284. rpcauth_holdcred(struct rpc_task *task)
  285. {
  286. dprintk("RPC: %5u holding %s cred %p\n",
  287. task->tk_pid, task->tk_auth->au_ops->au_name,
  288. task->tk_msg.rpc_cred);
  289. if (task->tk_msg.rpc_cred)
  290. get_rpccred(task->tk_msg.rpc_cred);
  291. }
  292. void
  293. put_rpccred(struct rpc_cred *cred)
  294. {
  295. cred->cr_expire = jiffies;
  296. if (!atomic_dec_and_test(&cred->cr_count))
  297. return;
  298. cred->cr_ops->crdestroy(cred);
  299. }
  300. void
  301. rpcauth_unbindcred(struct rpc_task *task)
  302. {
  303. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  304. dprintk("RPC: %5u releasing %s cred %p\n",
  305. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  306. put_rpccred(cred);
  307. task->tk_msg.rpc_cred = NULL;
  308. }
  309. __be32 *
  310. rpcauth_marshcred(struct rpc_task *task, __be32 *p)
  311. {
  312. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  313. dprintk("RPC: %5u marshaling %s cred %p\n",
  314. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  315. return cred->cr_ops->crmarshal(task, p);
  316. }
  317. __be32 *
  318. rpcauth_checkverf(struct rpc_task *task, __be32 *p)
  319. {
  320. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  321. dprintk("RPC: %5u validating %s cred %p\n",
  322. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  323. return cred->cr_ops->crvalidate(task, p);
  324. }
  325. int
  326. rpcauth_wrap_req(struct rpc_task *task, kxdrproc_t encode, void *rqstp,
  327. __be32 *data, void *obj)
  328. {
  329. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  330. dprintk("RPC: %5u using %s cred %p to wrap rpc data\n",
  331. task->tk_pid, cred->cr_ops->cr_name, cred);
  332. if (cred->cr_ops->crwrap_req)
  333. return cred->cr_ops->crwrap_req(task, encode, rqstp, data, obj);
  334. /* By default, we encode the arguments normally. */
  335. return encode(rqstp, data, obj);
  336. }
  337. int
  338. rpcauth_unwrap_resp(struct rpc_task *task, kxdrproc_t decode, void *rqstp,
  339. __be32 *data, void *obj)
  340. {
  341. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  342. dprintk("RPC: %5u using %s cred %p to unwrap rpc data\n",
  343. task->tk_pid, cred->cr_ops->cr_name, cred);
  344. if (cred->cr_ops->crunwrap_resp)
  345. return cred->cr_ops->crunwrap_resp(task, decode, rqstp,
  346. data, obj);
  347. /* By default, we decode the arguments normally. */
  348. return decode(rqstp, data, obj);
  349. }
  350. int
  351. rpcauth_refreshcred(struct rpc_task *task)
  352. {
  353. struct rpc_cred *cred = task->tk_msg.rpc_cred;
  354. int err;
  355. dprintk("RPC: %5u refreshing %s cred %p\n",
  356. task->tk_pid, task->tk_auth->au_ops->au_name, cred);
  357. err = cred->cr_ops->crrefresh(task);
  358. if (err < 0)
  359. task->tk_status = err;
  360. return err;
  361. }
  362. void
  363. rpcauth_invalcred(struct rpc_task *task)
  364. {
  365. dprintk("RPC: %5u invalidating %s cred %p\n",
  366. task->tk_pid, task->tk_auth->au_ops->au_name, task->tk_msg.rpc_cred);
  367. spin_lock(&rpc_credcache_lock);
  368. if (task->tk_msg.rpc_cred)
  369. task->tk_msg.rpc_cred->cr_flags &= ~RPCAUTH_CRED_UPTODATE;
  370. spin_unlock(&rpc_credcache_lock);
  371. }
  372. int
  373. rpcauth_uptodatecred(struct rpc_task *task)
  374. {
  375. return !(task->tk_msg.rpc_cred) ||
  376. (task->tk_msg.rpc_cred->cr_flags & RPCAUTH_CRED_UPTODATE);
  377. }