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