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