svcauth_unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. #include <linux/types.h>
  2. #include <linux/sched.h>
  3. #include <linux/module.h>
  4. #include <linux/sunrpc/types.h>
  5. #include <linux/sunrpc/xdr.h>
  6. #include <linux/sunrpc/svcsock.h>
  7. #include <linux/sunrpc/svcauth.h>
  8. #include <linux/err.h>
  9. #include <linux/seq_file.h>
  10. #include <linux/hash.h>
  11. #include <linux/string.h>
  12. #define RPCDBG_FACILITY RPCDBG_AUTH
  13. /*
  14. * AUTHUNIX and AUTHNULL credentials are both handled here.
  15. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  16. * are always nobody (-2). i.e. we do the same IP address checks for
  17. * AUTHNULL as for AUTHUNIX, and that is done here.
  18. */
  19. struct unix_domain {
  20. struct auth_domain h;
  21. int addr_changes;
  22. /* other stuff later */
  23. };
  24. struct auth_domain *unix_domain_find(char *name)
  25. {
  26. struct auth_domain *rv, ud;
  27. struct unix_domain *new;
  28. ud.name = name;
  29. rv = auth_domain_lookup(&ud, 0);
  30. foundit:
  31. if (rv && rv->flavour != RPC_AUTH_UNIX) {
  32. auth_domain_put(rv);
  33. return NULL;
  34. }
  35. if (rv)
  36. return rv;
  37. new = kmalloc(sizeof(*new), GFP_KERNEL);
  38. if (new == NULL)
  39. return NULL;
  40. cache_init(&new->h.h);
  41. new->h.name = kstrdup(name, GFP_KERNEL);
  42. new->h.flavour = RPC_AUTH_UNIX;
  43. new->addr_changes = 0;
  44. new->h.h.expiry_time = NEVER;
  45. rv = auth_domain_lookup(&new->h, 2);
  46. if (rv == &new->h) {
  47. if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
  48. } else {
  49. auth_domain_put(&new->h);
  50. goto foundit;
  51. }
  52. return rv;
  53. }
  54. static void svcauth_unix_domain_release(struct auth_domain *dom)
  55. {
  56. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  57. kfree(dom->name);
  58. kfree(ud);
  59. }
  60. /**************************************************
  61. * cache for IP address to unix_domain
  62. * as needed by AUTH_UNIX
  63. */
  64. #define IP_HASHBITS 8
  65. #define IP_HASHMAX (1<<IP_HASHBITS)
  66. #define IP_HASHMASK (IP_HASHMAX-1)
  67. struct ip_map {
  68. struct cache_head h;
  69. char m_class[8]; /* e.g. "nfsd" */
  70. struct in_addr m_addr;
  71. struct unix_domain *m_client;
  72. int m_add_change;
  73. };
  74. static struct cache_head *ip_table[IP_HASHMAX];
  75. static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
  76. {
  77. struct ip_map *im = container_of(item, struct ip_map,h);
  78. if (cache_put(item, cd)) {
  79. if (test_bit(CACHE_VALID, &item->flags) &&
  80. !test_bit(CACHE_NEGATIVE, &item->flags))
  81. auth_domain_put(&im->m_client->h);
  82. kfree(im);
  83. }
  84. }
  85. static inline int ip_map_hash(struct ip_map *item)
  86. {
  87. return hash_str(item->m_class, IP_HASHBITS) ^
  88. hash_long((unsigned long)item->m_addr.s_addr, IP_HASHBITS);
  89. }
  90. static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
  91. {
  92. return strcmp(tmp->m_class, item->m_class) == 0
  93. && tmp->m_addr.s_addr == item->m_addr.s_addr;
  94. }
  95. static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
  96. {
  97. strcpy(new->m_class, item->m_class);
  98. new->m_addr.s_addr = item->m_addr.s_addr;
  99. }
  100. static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
  101. {
  102. cache_get(&item->m_client->h.h);
  103. new->m_client = item->m_client;
  104. new->m_add_change = item->m_add_change;
  105. }
  106. static void ip_map_request(struct cache_detail *cd,
  107. struct cache_head *h,
  108. char **bpp, int *blen)
  109. {
  110. char text_addr[20];
  111. struct ip_map *im = container_of(h, struct ip_map, h);
  112. __u32 addr = im->m_addr.s_addr;
  113. snprintf(text_addr, 20, "%u.%u.%u.%u",
  114. ntohl(addr) >> 24 & 0xff,
  115. ntohl(addr) >> 16 & 0xff,
  116. ntohl(addr) >> 8 & 0xff,
  117. ntohl(addr) >> 0 & 0xff);
  118. qword_add(bpp, blen, im->m_class);
  119. qword_add(bpp, blen, text_addr);
  120. (*bpp)[-1] = '\n';
  121. }
  122. static struct ip_map *ip_map_lookup(struct ip_map *, int);
  123. static int ip_map_parse(struct cache_detail *cd,
  124. char *mesg, int mlen)
  125. {
  126. /* class ipaddress [domainname] */
  127. /* should be safe just to use the start of the input buffer
  128. * for scratch: */
  129. char *buf = mesg;
  130. int len;
  131. int b1,b2,b3,b4;
  132. char c;
  133. struct ip_map ipm, *ipmp;
  134. struct auth_domain *dom;
  135. time_t expiry;
  136. if (mesg[mlen-1] != '\n')
  137. return -EINVAL;
  138. mesg[mlen-1] = 0;
  139. /* class */
  140. len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
  141. if (len <= 0) return -EINVAL;
  142. /* ip address */
  143. len = qword_get(&mesg, buf, mlen);
  144. if (len <= 0) return -EINVAL;
  145. if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
  146. return -EINVAL;
  147. expiry = get_expiry(&mesg);
  148. if (expiry ==0)
  149. return -EINVAL;
  150. /* domainname, or empty for NEGATIVE */
  151. len = qword_get(&mesg, buf, mlen);
  152. if (len < 0) return -EINVAL;
  153. if (len) {
  154. dom = unix_domain_find(buf);
  155. if (dom == NULL)
  156. return -ENOENT;
  157. } else
  158. dom = NULL;
  159. ipm.m_addr.s_addr =
  160. htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
  161. ipm.h.flags = 0;
  162. if (dom) {
  163. ipm.m_client = container_of(dom, struct unix_domain, h);
  164. ipm.m_add_change = ipm.m_client->addr_changes;
  165. } else
  166. set_bit(CACHE_NEGATIVE, &ipm.h.flags);
  167. ipm.h.expiry_time = expiry;
  168. ipmp = ip_map_lookup(&ipm, 1);
  169. if (ipmp)
  170. ip_map_put(&ipmp->h, &ip_map_cache);
  171. if (dom)
  172. auth_domain_put(dom);
  173. if (!ipmp)
  174. return -ENOMEM;
  175. cache_flush();
  176. return 0;
  177. }
  178. static int ip_map_show(struct seq_file *m,
  179. struct cache_detail *cd,
  180. struct cache_head *h)
  181. {
  182. struct ip_map *im;
  183. struct in_addr addr;
  184. char *dom = "-no-domain-";
  185. if (h == NULL) {
  186. seq_puts(m, "#class IP domain\n");
  187. return 0;
  188. }
  189. im = container_of(h, struct ip_map, h);
  190. /* class addr domain */
  191. addr = im->m_addr;
  192. if (test_bit(CACHE_VALID, &h->flags) &&
  193. !test_bit(CACHE_NEGATIVE, &h->flags))
  194. dom = im->m_client->h.name;
  195. seq_printf(m, "%s %d.%d.%d.%d %s\n",
  196. im->m_class,
  197. htonl(addr.s_addr) >> 24 & 0xff,
  198. htonl(addr.s_addr) >> 16 & 0xff,
  199. htonl(addr.s_addr) >> 8 & 0xff,
  200. htonl(addr.s_addr) >> 0 & 0xff,
  201. dom
  202. );
  203. return 0;
  204. }
  205. struct cache_detail ip_map_cache = {
  206. .hash_size = IP_HASHMAX,
  207. .hash_table = ip_table,
  208. .name = "auth.unix.ip",
  209. .cache_put = ip_map_put,
  210. .cache_request = ip_map_request,
  211. .cache_parse = ip_map_parse,
  212. .cache_show = ip_map_show,
  213. };
  214. static DefineSimpleCacheLookup(ip_map, 0)
  215. int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
  216. {
  217. struct unix_domain *udom;
  218. struct ip_map ip, *ipmp;
  219. if (dom->flavour != RPC_AUTH_UNIX)
  220. return -EINVAL;
  221. udom = container_of(dom, struct unix_domain, h);
  222. strcpy(ip.m_class, "nfsd");
  223. ip.m_addr = addr;
  224. ip.m_client = udom;
  225. ip.m_add_change = udom->addr_changes+1;
  226. ip.h.flags = 0;
  227. ip.h.expiry_time = NEVER;
  228. ipmp = ip_map_lookup(&ip, 1);
  229. if (ipmp) {
  230. ip_map_put(&ipmp->h, &ip_map_cache);
  231. return 0;
  232. } else
  233. return -ENOMEM;
  234. }
  235. int auth_unix_forget_old(struct auth_domain *dom)
  236. {
  237. struct unix_domain *udom;
  238. if (dom->flavour != RPC_AUTH_UNIX)
  239. return -EINVAL;
  240. udom = container_of(dom, struct unix_domain, h);
  241. udom->addr_changes++;
  242. return 0;
  243. }
  244. struct auth_domain *auth_unix_lookup(struct in_addr addr)
  245. {
  246. struct ip_map key, *ipm;
  247. struct auth_domain *rv;
  248. strcpy(key.m_class, "nfsd");
  249. key.m_addr = addr;
  250. ipm = ip_map_lookup(&key, 0);
  251. if (!ipm)
  252. return NULL;
  253. if (cache_check(&ip_map_cache, &ipm->h, NULL))
  254. return NULL;
  255. if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
  256. if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
  257. auth_domain_put(&ipm->m_client->h);
  258. rv = NULL;
  259. } else {
  260. rv = &ipm->m_client->h;
  261. cache_get(&rv->h);
  262. }
  263. ip_map_put(&ipm->h, &ip_map_cache);
  264. return rv;
  265. }
  266. void svcauth_unix_purge(void)
  267. {
  268. cache_purge(&ip_map_cache);
  269. cache_purge(&auth_domain_cache);
  270. }
  271. static int
  272. svcauth_unix_set_client(struct svc_rqst *rqstp)
  273. {
  274. struct ip_map key, *ipm;
  275. rqstp->rq_client = NULL;
  276. if (rqstp->rq_proc == 0)
  277. return SVC_OK;
  278. strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
  279. key.m_addr = rqstp->rq_addr.sin_addr;
  280. ipm = ip_map_lookup(&key, 0);
  281. if (ipm == NULL)
  282. return SVC_DENIED;
  283. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  284. default:
  285. BUG();
  286. case -EAGAIN:
  287. return SVC_DROP;
  288. case -ENOENT:
  289. return SVC_DENIED;
  290. case 0:
  291. rqstp->rq_client = &ipm->m_client->h;
  292. cache_get(&rqstp->rq_client->h);
  293. ip_map_put(&ipm->h, &ip_map_cache);
  294. break;
  295. }
  296. return SVC_OK;
  297. }
  298. static int
  299. svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
  300. {
  301. struct kvec *argv = &rqstp->rq_arg.head[0];
  302. struct kvec *resv = &rqstp->rq_res.head[0];
  303. struct svc_cred *cred = &rqstp->rq_cred;
  304. cred->cr_group_info = NULL;
  305. rqstp->rq_client = NULL;
  306. if (argv->iov_len < 3*4)
  307. return SVC_GARBAGE;
  308. if (svc_getu32(argv) != 0) {
  309. dprintk("svc: bad null cred\n");
  310. *authp = rpc_autherr_badcred;
  311. return SVC_DENIED;
  312. }
  313. if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
  314. dprintk("svc: bad null verf\n");
  315. *authp = rpc_autherr_badverf;
  316. return SVC_DENIED;
  317. }
  318. /* Signal that mapping to nobody uid/gid is required */
  319. cred->cr_uid = (uid_t) -1;
  320. cred->cr_gid = (gid_t) -1;
  321. cred->cr_group_info = groups_alloc(0);
  322. if (cred->cr_group_info == NULL)
  323. return SVC_DROP; /* kmalloc failure - client must retry */
  324. /* Put NULL verifier */
  325. svc_putu32(resv, RPC_AUTH_NULL);
  326. svc_putu32(resv, 0);
  327. return SVC_OK;
  328. }
  329. static int
  330. svcauth_null_release(struct svc_rqst *rqstp)
  331. {
  332. if (rqstp->rq_client)
  333. auth_domain_put(rqstp->rq_client);
  334. rqstp->rq_client = NULL;
  335. if (rqstp->rq_cred.cr_group_info)
  336. put_group_info(rqstp->rq_cred.cr_group_info);
  337. rqstp->rq_cred.cr_group_info = NULL;
  338. return 0; /* don't drop */
  339. }
  340. struct auth_ops svcauth_null = {
  341. .name = "null",
  342. .owner = THIS_MODULE,
  343. .flavour = RPC_AUTH_NULL,
  344. .accept = svcauth_null_accept,
  345. .release = svcauth_null_release,
  346. .set_client = svcauth_unix_set_client,
  347. };
  348. static int
  349. svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
  350. {
  351. struct kvec *argv = &rqstp->rq_arg.head[0];
  352. struct kvec *resv = &rqstp->rq_res.head[0];
  353. struct svc_cred *cred = &rqstp->rq_cred;
  354. u32 slen, i;
  355. int len = argv->iov_len;
  356. cred->cr_group_info = NULL;
  357. rqstp->rq_client = NULL;
  358. if ((len -= 3*4) < 0)
  359. return SVC_GARBAGE;
  360. svc_getu32(argv); /* length */
  361. svc_getu32(argv); /* time stamp */
  362. slen = XDR_QUADLEN(ntohl(svc_getu32(argv))); /* machname length */
  363. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  364. goto badcred;
  365. argv->iov_base = (void*)((u32*)argv->iov_base + slen); /* skip machname */
  366. argv->iov_len -= slen*4;
  367. cred->cr_uid = ntohl(svc_getu32(argv)); /* uid */
  368. cred->cr_gid = ntohl(svc_getu32(argv)); /* gid */
  369. slen = ntohl(svc_getu32(argv)); /* gids length */
  370. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  371. goto badcred;
  372. cred->cr_group_info = groups_alloc(slen);
  373. if (cred->cr_group_info == NULL)
  374. return SVC_DROP;
  375. for (i = 0; i < slen; i++)
  376. GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
  377. if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
  378. *authp = rpc_autherr_badverf;
  379. return SVC_DENIED;
  380. }
  381. /* Put NULL verifier */
  382. svc_putu32(resv, RPC_AUTH_NULL);
  383. svc_putu32(resv, 0);
  384. return SVC_OK;
  385. badcred:
  386. *authp = rpc_autherr_badcred;
  387. return SVC_DENIED;
  388. }
  389. static int
  390. svcauth_unix_release(struct svc_rqst *rqstp)
  391. {
  392. /* Verifier (such as it is) is already in place.
  393. */
  394. if (rqstp->rq_client)
  395. auth_domain_put(rqstp->rq_client);
  396. rqstp->rq_client = NULL;
  397. if (rqstp->rq_cred.cr_group_info)
  398. put_group_info(rqstp->rq_cred.cr_group_info);
  399. rqstp->rq_cred.cr_group_info = NULL;
  400. return 0;
  401. }
  402. struct auth_ops svcauth_unix = {
  403. .name = "unix",
  404. .owner = THIS_MODULE,
  405. .flavour = RPC_AUTH_UNIX,
  406. .accept = svcauth_unix_accept,
  407. .release = svcauth_unix_release,
  408. .domain_release = svcauth_unix_domain_release,
  409. .set_client = svcauth_unix_set_client,
  410. };