svcauth_unix.c 12 KB

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