auth.c 9.4 KB

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