svcauth_unix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  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/sunrpc/gss_api.h>
  9. #include <linux/err.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/hash.h>
  12. #include <linux/string.h>
  13. #include <linux/slab.h>
  14. #include <net/sock.h>
  15. #include <net/ipv6.h>
  16. #include <linux/kernel.h>
  17. #define RPCDBG_FACILITY RPCDBG_AUTH
  18. #include <linux/sunrpc/clnt.h>
  19. #include "netns.h"
  20. /*
  21. * AUTHUNIX and AUTHNULL credentials are both handled here.
  22. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  23. * are always nobody (-2). i.e. we do the same IP address checks for
  24. * AUTHNULL as for AUTHUNIX, and that is done here.
  25. */
  26. struct unix_domain {
  27. struct auth_domain h;
  28. /* other stuff later */
  29. };
  30. extern struct auth_ops svcauth_null;
  31. extern struct auth_ops svcauth_unix;
  32. static void svcauth_unix_domain_release(struct auth_domain *dom)
  33. {
  34. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  35. kfree(dom->name);
  36. kfree(ud);
  37. }
  38. struct auth_domain *unix_domain_find(char *name)
  39. {
  40. struct auth_domain *rv;
  41. struct unix_domain *new = NULL;
  42. rv = auth_domain_lookup(name, NULL);
  43. while(1) {
  44. if (rv) {
  45. if (new && rv != &new->h)
  46. svcauth_unix_domain_release(&new->h);
  47. if (rv->flavour != &svcauth_unix) {
  48. auth_domain_put(rv);
  49. return NULL;
  50. }
  51. return rv;
  52. }
  53. new = kmalloc(sizeof(*new), GFP_KERNEL);
  54. if (new == NULL)
  55. return NULL;
  56. kref_init(&new->h.ref);
  57. new->h.name = kstrdup(name, GFP_KERNEL);
  58. if (new->h.name == NULL) {
  59. kfree(new);
  60. return NULL;
  61. }
  62. new->h.flavour = &svcauth_unix;
  63. rv = auth_domain_lookup(name, &new->h);
  64. }
  65. }
  66. EXPORT_SYMBOL_GPL(unix_domain_find);
  67. /**************************************************
  68. * cache for IP address to unix_domain
  69. * as needed by AUTH_UNIX
  70. */
  71. #define IP_HASHBITS 8
  72. #define IP_HASHMAX (1<<IP_HASHBITS)
  73. struct ip_map {
  74. struct cache_head h;
  75. char m_class[8]; /* e.g. "nfsd" */
  76. struct in6_addr m_addr;
  77. struct unix_domain *m_client;
  78. };
  79. static void ip_map_put(struct kref *kref)
  80. {
  81. struct cache_head *item = container_of(kref, struct cache_head, ref);
  82. struct ip_map *im = container_of(item, struct ip_map,h);
  83. if (test_bit(CACHE_VALID, &item->flags) &&
  84. !test_bit(CACHE_NEGATIVE, &item->flags))
  85. auth_domain_put(&im->m_client->h);
  86. kfree(im);
  87. }
  88. #if IP_HASHBITS == 8
  89. /* hash_long on a 64 bit machine is currently REALLY BAD for
  90. * IP addresses in reverse-endian (i.e. on a little-endian machine).
  91. * So use a trivial but reliable hash instead
  92. */
  93. static inline int hash_ip(__be32 ip)
  94. {
  95. int hash = (__force u32)ip ^ ((__force u32)ip>>16);
  96. return (hash ^ (hash>>8)) & 0xff;
  97. }
  98. #endif
  99. static inline int hash_ip6(struct in6_addr ip)
  100. {
  101. return (hash_ip(ip.s6_addr32[0]) ^
  102. hash_ip(ip.s6_addr32[1]) ^
  103. hash_ip(ip.s6_addr32[2]) ^
  104. hash_ip(ip.s6_addr32[3]));
  105. }
  106. static int ip_map_match(struct cache_head *corig, struct cache_head *cnew)
  107. {
  108. struct ip_map *orig = container_of(corig, struct ip_map, h);
  109. struct ip_map *new = container_of(cnew, struct ip_map, h);
  110. return strcmp(orig->m_class, new->m_class) == 0 &&
  111. ipv6_addr_equal(&orig->m_addr, &new->m_addr);
  112. }
  113. static void ip_map_init(struct cache_head *cnew, struct cache_head *citem)
  114. {
  115. struct ip_map *new = container_of(cnew, struct ip_map, h);
  116. struct ip_map *item = container_of(citem, struct ip_map, h);
  117. strcpy(new->m_class, item->m_class);
  118. new->m_addr = item->m_addr;
  119. }
  120. static void update(struct cache_head *cnew, struct cache_head *citem)
  121. {
  122. struct ip_map *new = container_of(cnew, struct ip_map, h);
  123. struct ip_map *item = container_of(citem, struct ip_map, h);
  124. kref_get(&item->m_client->h.ref);
  125. new->m_client = item->m_client;
  126. }
  127. static struct cache_head *ip_map_alloc(void)
  128. {
  129. struct ip_map *i = kmalloc(sizeof(*i), GFP_KERNEL);
  130. if (i)
  131. return &i->h;
  132. else
  133. return NULL;
  134. }
  135. static void ip_map_request(struct cache_detail *cd,
  136. struct cache_head *h,
  137. char **bpp, int *blen)
  138. {
  139. char text_addr[40];
  140. struct ip_map *im = container_of(h, struct ip_map, h);
  141. if (ipv6_addr_v4mapped(&(im->m_addr))) {
  142. snprintf(text_addr, 20, "%pI4", &im->m_addr.s6_addr32[3]);
  143. } else {
  144. snprintf(text_addr, 40, "%pI6", &im->m_addr);
  145. }
  146. qword_add(bpp, blen, im->m_class);
  147. qword_add(bpp, blen, text_addr);
  148. (*bpp)[-1] = '\n';
  149. }
  150. static int ip_map_upcall(struct cache_detail *cd, struct cache_head *h)
  151. {
  152. return sunrpc_cache_pipe_upcall(cd, h, ip_map_request);
  153. }
  154. static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class, struct in6_addr *addr);
  155. static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm, struct unix_domain *udom, time_t expiry);
  156. static int ip_map_parse(struct cache_detail *cd,
  157. char *mesg, int mlen)
  158. {
  159. /* class ipaddress [domainname] */
  160. /* should be safe just to use the start of the input buffer
  161. * for scratch: */
  162. char *buf = mesg;
  163. int len;
  164. char class[8];
  165. union {
  166. struct sockaddr sa;
  167. struct sockaddr_in s4;
  168. struct sockaddr_in6 s6;
  169. } address;
  170. struct sockaddr_in6 sin6;
  171. int err;
  172. struct ip_map *ipmp;
  173. struct auth_domain *dom;
  174. time_t expiry;
  175. if (mesg[mlen-1] != '\n')
  176. return -EINVAL;
  177. mesg[mlen-1] = 0;
  178. /* class */
  179. len = qword_get(&mesg, class, sizeof(class));
  180. if (len <= 0) return -EINVAL;
  181. /* ip address */
  182. len = qword_get(&mesg, buf, mlen);
  183. if (len <= 0) return -EINVAL;
  184. if (rpc_pton(cd->net, buf, len, &address.sa, sizeof(address)) == 0)
  185. return -EINVAL;
  186. switch (address.sa.sa_family) {
  187. case AF_INET:
  188. /* Form a mapped IPv4 address in sin6 */
  189. sin6.sin6_family = AF_INET6;
  190. ipv6_addr_set_v4mapped(address.s4.sin_addr.s_addr,
  191. &sin6.sin6_addr);
  192. break;
  193. #if IS_ENABLED(CONFIG_IPV6)
  194. case AF_INET6:
  195. memcpy(&sin6, &address.s6, sizeof(sin6));
  196. break;
  197. #endif
  198. default:
  199. return -EINVAL;
  200. }
  201. expiry = get_expiry(&mesg);
  202. if (expiry ==0)
  203. return -EINVAL;
  204. /* domainname, or empty for NEGATIVE */
  205. len = qword_get(&mesg, buf, mlen);
  206. if (len < 0) return -EINVAL;
  207. if (len) {
  208. dom = unix_domain_find(buf);
  209. if (dom == NULL)
  210. return -ENOENT;
  211. } else
  212. dom = NULL;
  213. /* IPv6 scope IDs are ignored for now */
  214. ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
  215. if (ipmp) {
  216. err = __ip_map_update(cd, ipmp,
  217. container_of(dom, struct unix_domain, h),
  218. expiry);
  219. } else
  220. err = -ENOMEM;
  221. if (dom)
  222. auth_domain_put(dom);
  223. cache_flush();
  224. return err;
  225. }
  226. static int ip_map_show(struct seq_file *m,
  227. struct cache_detail *cd,
  228. struct cache_head *h)
  229. {
  230. struct ip_map *im;
  231. struct in6_addr addr;
  232. char *dom = "-no-domain-";
  233. if (h == NULL) {
  234. seq_puts(m, "#class IP domain\n");
  235. return 0;
  236. }
  237. im = container_of(h, struct ip_map, h);
  238. /* class addr domain */
  239. addr = im->m_addr;
  240. if (test_bit(CACHE_VALID, &h->flags) &&
  241. !test_bit(CACHE_NEGATIVE, &h->flags))
  242. dom = im->m_client->h.name;
  243. if (ipv6_addr_v4mapped(&addr)) {
  244. seq_printf(m, "%s %pI4 %s\n",
  245. im->m_class, &addr.s6_addr32[3], dom);
  246. } else {
  247. seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
  248. }
  249. return 0;
  250. }
  251. static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
  252. struct in6_addr *addr)
  253. {
  254. struct ip_map ip;
  255. struct cache_head *ch;
  256. strcpy(ip.m_class, class);
  257. ip.m_addr = *addr;
  258. ch = sunrpc_cache_lookup(cd, &ip.h,
  259. hash_str(class, IP_HASHBITS) ^
  260. hash_ip6(*addr));
  261. if (ch)
  262. return container_of(ch, struct ip_map, h);
  263. else
  264. return NULL;
  265. }
  266. static inline struct ip_map *ip_map_lookup(struct net *net, char *class,
  267. struct in6_addr *addr)
  268. {
  269. struct sunrpc_net *sn;
  270. sn = net_generic(net, sunrpc_net_id);
  271. return __ip_map_lookup(sn->ip_map_cache, class, addr);
  272. }
  273. static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
  274. struct unix_domain *udom, time_t expiry)
  275. {
  276. struct ip_map ip;
  277. struct cache_head *ch;
  278. ip.m_client = udom;
  279. ip.h.flags = 0;
  280. if (!udom)
  281. set_bit(CACHE_NEGATIVE, &ip.h.flags);
  282. ip.h.expiry_time = expiry;
  283. ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
  284. hash_str(ipm->m_class, IP_HASHBITS) ^
  285. hash_ip6(ipm->m_addr));
  286. if (!ch)
  287. return -ENOMEM;
  288. cache_put(ch, cd);
  289. return 0;
  290. }
  291. static inline int ip_map_update(struct net *net, struct ip_map *ipm,
  292. struct unix_domain *udom, time_t expiry)
  293. {
  294. struct sunrpc_net *sn;
  295. sn = net_generic(net, sunrpc_net_id);
  296. return __ip_map_update(sn->ip_map_cache, ipm, udom, expiry);
  297. }
  298. void svcauth_unix_purge(void)
  299. {
  300. struct net *net;
  301. for_each_net(net) {
  302. struct sunrpc_net *sn;
  303. sn = net_generic(net, sunrpc_net_id);
  304. cache_purge(sn->ip_map_cache);
  305. }
  306. }
  307. EXPORT_SYMBOL_GPL(svcauth_unix_purge);
  308. static inline struct ip_map *
  309. ip_map_cached_get(struct svc_xprt *xprt)
  310. {
  311. struct ip_map *ipm = NULL;
  312. struct sunrpc_net *sn;
  313. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  314. spin_lock(&xprt->xpt_lock);
  315. ipm = xprt->xpt_auth_cache;
  316. if (ipm != NULL) {
  317. if (!cache_valid(&ipm->h)) {
  318. /*
  319. * The entry has been invalidated since it was
  320. * remembered, e.g. by a second mount from the
  321. * same IP address.
  322. */
  323. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  324. xprt->xpt_auth_cache = NULL;
  325. spin_unlock(&xprt->xpt_lock);
  326. cache_put(&ipm->h, sn->ip_map_cache);
  327. return NULL;
  328. }
  329. cache_get(&ipm->h);
  330. }
  331. spin_unlock(&xprt->xpt_lock);
  332. }
  333. return ipm;
  334. }
  335. static inline void
  336. ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
  337. {
  338. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  339. spin_lock(&xprt->xpt_lock);
  340. if (xprt->xpt_auth_cache == NULL) {
  341. /* newly cached, keep the reference */
  342. xprt->xpt_auth_cache = ipm;
  343. ipm = NULL;
  344. }
  345. spin_unlock(&xprt->xpt_lock);
  346. }
  347. if (ipm) {
  348. struct sunrpc_net *sn;
  349. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  350. cache_put(&ipm->h, sn->ip_map_cache);
  351. }
  352. }
  353. void
  354. svcauth_unix_info_release(struct svc_xprt *xpt)
  355. {
  356. struct ip_map *ipm;
  357. ipm = xpt->xpt_auth_cache;
  358. if (ipm != NULL) {
  359. struct sunrpc_net *sn;
  360. sn = net_generic(xpt->xpt_net, sunrpc_net_id);
  361. cache_put(&ipm->h, sn->ip_map_cache);
  362. }
  363. }
  364. /****************************************************************************
  365. * auth.unix.gid cache
  366. * simple cache to map a UID to a list of GIDs
  367. * because AUTH_UNIX aka AUTH_SYS has a max of 16
  368. */
  369. #define GID_HASHBITS 8
  370. #define GID_HASHMAX (1<<GID_HASHBITS)
  371. struct unix_gid {
  372. struct cache_head h;
  373. uid_t uid;
  374. struct group_info *gi;
  375. };
  376. static void unix_gid_put(struct kref *kref)
  377. {
  378. struct cache_head *item = container_of(kref, struct cache_head, ref);
  379. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  380. if (test_bit(CACHE_VALID, &item->flags) &&
  381. !test_bit(CACHE_NEGATIVE, &item->flags))
  382. put_group_info(ug->gi);
  383. kfree(ug);
  384. }
  385. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  386. {
  387. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  388. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  389. return orig->uid == new->uid;
  390. }
  391. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  392. {
  393. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  394. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  395. new->uid = item->uid;
  396. }
  397. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  398. {
  399. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  400. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  401. get_group_info(item->gi);
  402. new->gi = item->gi;
  403. }
  404. static struct cache_head *unix_gid_alloc(void)
  405. {
  406. struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
  407. if (g)
  408. return &g->h;
  409. else
  410. return NULL;
  411. }
  412. static void unix_gid_request(struct cache_detail *cd,
  413. struct cache_head *h,
  414. char **bpp, int *blen)
  415. {
  416. char tuid[20];
  417. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  418. snprintf(tuid, 20, "%u", ug->uid);
  419. qword_add(bpp, blen, tuid);
  420. (*bpp)[-1] = '\n';
  421. }
  422. static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
  423. {
  424. return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
  425. }
  426. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid);
  427. static int unix_gid_parse(struct cache_detail *cd,
  428. char *mesg, int mlen)
  429. {
  430. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  431. int uid;
  432. int gids;
  433. int rv;
  434. int i;
  435. int err;
  436. time_t expiry;
  437. struct unix_gid ug, *ugp;
  438. if (mesg[mlen - 1] != '\n')
  439. return -EINVAL;
  440. mesg[mlen-1] = 0;
  441. rv = get_int(&mesg, &uid);
  442. if (rv)
  443. return -EINVAL;
  444. ug.uid = uid;
  445. expiry = get_expiry(&mesg);
  446. if (expiry == 0)
  447. return -EINVAL;
  448. rv = get_int(&mesg, &gids);
  449. if (rv || gids < 0 || gids > 8192)
  450. return -EINVAL;
  451. ug.gi = groups_alloc(gids);
  452. if (!ug.gi)
  453. return -ENOMEM;
  454. for (i = 0 ; i < gids ; i++) {
  455. int gid;
  456. rv = get_int(&mesg, &gid);
  457. err = -EINVAL;
  458. if (rv)
  459. goto out;
  460. GROUP_AT(ug.gi, i) = gid;
  461. }
  462. ugp = unix_gid_lookup(cd, uid);
  463. if (ugp) {
  464. struct cache_head *ch;
  465. ug.h.flags = 0;
  466. ug.h.expiry_time = expiry;
  467. ch = sunrpc_cache_update(cd,
  468. &ug.h, &ugp->h,
  469. hash_long(uid, GID_HASHBITS));
  470. if (!ch)
  471. err = -ENOMEM;
  472. else {
  473. err = 0;
  474. cache_put(ch, cd);
  475. }
  476. } else
  477. err = -ENOMEM;
  478. out:
  479. if (ug.gi)
  480. put_group_info(ug.gi);
  481. return err;
  482. }
  483. static int unix_gid_show(struct seq_file *m,
  484. struct cache_detail *cd,
  485. struct cache_head *h)
  486. {
  487. struct unix_gid *ug;
  488. int i;
  489. int glen;
  490. if (h == NULL) {
  491. seq_puts(m, "#uid cnt: gids...\n");
  492. return 0;
  493. }
  494. ug = container_of(h, struct unix_gid, h);
  495. if (test_bit(CACHE_VALID, &h->flags) &&
  496. !test_bit(CACHE_NEGATIVE, &h->flags))
  497. glen = ug->gi->ngroups;
  498. else
  499. glen = 0;
  500. seq_printf(m, "%u %d:", ug->uid, glen);
  501. for (i = 0; i < glen; i++)
  502. seq_printf(m, " %d", GROUP_AT(ug->gi, i));
  503. seq_printf(m, "\n");
  504. return 0;
  505. }
  506. static struct cache_detail unix_gid_cache_template = {
  507. .owner = THIS_MODULE,
  508. .hash_size = GID_HASHMAX,
  509. .name = "auth.unix.gid",
  510. .cache_put = unix_gid_put,
  511. .cache_upcall = unix_gid_upcall,
  512. .cache_parse = unix_gid_parse,
  513. .cache_show = unix_gid_show,
  514. .match = unix_gid_match,
  515. .init = unix_gid_init,
  516. .update = unix_gid_update,
  517. .alloc = unix_gid_alloc,
  518. };
  519. int unix_gid_cache_create(struct net *net)
  520. {
  521. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  522. struct cache_detail *cd;
  523. int err;
  524. cd = cache_create_net(&unix_gid_cache_template, net);
  525. if (IS_ERR(cd))
  526. return PTR_ERR(cd);
  527. err = cache_register_net(cd, net);
  528. if (err) {
  529. cache_destroy_net(cd, net);
  530. return err;
  531. }
  532. sn->unix_gid_cache = cd;
  533. return 0;
  534. }
  535. void unix_gid_cache_destroy(struct net *net)
  536. {
  537. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  538. struct cache_detail *cd = sn->unix_gid_cache;
  539. sn->unix_gid_cache = NULL;
  540. cache_purge(cd);
  541. cache_unregister_net(cd, net);
  542. cache_destroy_net(cd, net);
  543. }
  544. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid)
  545. {
  546. struct unix_gid ug;
  547. struct cache_head *ch;
  548. ug.uid = uid;
  549. ch = sunrpc_cache_lookup(cd, &ug.h, hash_long(uid, GID_HASHBITS));
  550. if (ch)
  551. return container_of(ch, struct unix_gid, h);
  552. else
  553. return NULL;
  554. }
  555. static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
  556. {
  557. struct unix_gid *ug;
  558. struct group_info *gi;
  559. int ret;
  560. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
  561. sunrpc_net_id);
  562. ug = unix_gid_lookup(sn->unix_gid_cache, uid);
  563. if (!ug)
  564. return ERR_PTR(-EAGAIN);
  565. ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
  566. switch (ret) {
  567. case -ENOENT:
  568. return ERR_PTR(-ENOENT);
  569. case -ETIMEDOUT:
  570. return ERR_PTR(-ESHUTDOWN);
  571. case 0:
  572. gi = get_group_info(ug->gi);
  573. cache_put(&ug->h, sn->unix_gid_cache);
  574. return gi;
  575. default:
  576. return ERR_PTR(-EAGAIN);
  577. }
  578. }
  579. int
  580. svcauth_unix_set_client(struct svc_rqst *rqstp)
  581. {
  582. struct sockaddr_in *sin;
  583. struct sockaddr_in6 *sin6, sin6_storage;
  584. struct ip_map *ipm;
  585. struct group_info *gi;
  586. struct svc_cred *cred = &rqstp->rq_cred;
  587. struct svc_xprt *xprt = rqstp->rq_xprt;
  588. struct net *net = xprt->xpt_net;
  589. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  590. switch (rqstp->rq_addr.ss_family) {
  591. case AF_INET:
  592. sin = svc_addr_in(rqstp);
  593. sin6 = &sin6_storage;
  594. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
  595. break;
  596. case AF_INET6:
  597. sin6 = svc_addr_in6(rqstp);
  598. break;
  599. default:
  600. BUG();
  601. }
  602. rqstp->rq_client = NULL;
  603. if (rqstp->rq_proc == 0)
  604. return SVC_OK;
  605. ipm = ip_map_cached_get(xprt);
  606. if (ipm == NULL)
  607. ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
  608. &sin6->sin6_addr);
  609. if (ipm == NULL)
  610. return SVC_DENIED;
  611. switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  612. default:
  613. BUG();
  614. case -ETIMEDOUT:
  615. return SVC_CLOSE;
  616. case -EAGAIN:
  617. return SVC_DROP;
  618. case -ENOENT:
  619. return SVC_DENIED;
  620. case 0:
  621. rqstp->rq_client = &ipm->m_client->h;
  622. kref_get(&rqstp->rq_client->ref);
  623. ip_map_cached_put(xprt, ipm);
  624. break;
  625. }
  626. gi = unix_gid_find(cred->cr_uid, rqstp);
  627. switch (PTR_ERR(gi)) {
  628. case -EAGAIN:
  629. return SVC_DROP;
  630. case -ESHUTDOWN:
  631. return SVC_CLOSE;
  632. case -ENOENT:
  633. break;
  634. default:
  635. put_group_info(cred->cr_group_info);
  636. cred->cr_group_info = gi;
  637. }
  638. return SVC_OK;
  639. }
  640. EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
  641. static int
  642. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  643. {
  644. struct kvec *argv = &rqstp->rq_arg.head[0];
  645. struct kvec *resv = &rqstp->rq_res.head[0];
  646. struct svc_cred *cred = &rqstp->rq_cred;
  647. cred->cr_group_info = NULL;
  648. rqstp->rq_client = NULL;
  649. if (argv->iov_len < 3*4)
  650. return SVC_GARBAGE;
  651. if (svc_getu32(argv) != 0) {
  652. dprintk("svc: bad null cred\n");
  653. *authp = rpc_autherr_badcred;
  654. return SVC_DENIED;
  655. }
  656. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  657. dprintk("svc: bad null verf\n");
  658. *authp = rpc_autherr_badverf;
  659. return SVC_DENIED;
  660. }
  661. /* Signal that mapping to nobody uid/gid is required */
  662. cred->cr_uid = (uid_t) -1;
  663. cred->cr_gid = (gid_t) -1;
  664. cred->cr_group_info = groups_alloc(0);
  665. if (cred->cr_group_info == NULL)
  666. return SVC_CLOSE; /* kmalloc failure - client must retry */
  667. /* Put NULL verifier */
  668. svc_putnl(resv, RPC_AUTH_NULL);
  669. svc_putnl(resv, 0);
  670. rqstp->rq_flavor = RPC_AUTH_NULL;
  671. return SVC_OK;
  672. }
  673. static int
  674. svcauth_null_release(struct svc_rqst *rqstp)
  675. {
  676. if (rqstp->rq_client)
  677. auth_domain_put(rqstp->rq_client);
  678. rqstp->rq_client = NULL;
  679. if (rqstp->rq_cred.cr_group_info)
  680. put_group_info(rqstp->rq_cred.cr_group_info);
  681. rqstp->rq_cred.cr_group_info = NULL;
  682. return 0; /* don't drop */
  683. }
  684. struct auth_ops svcauth_null = {
  685. .name = "null",
  686. .owner = THIS_MODULE,
  687. .flavour = RPC_AUTH_NULL,
  688. .accept = svcauth_null_accept,
  689. .release = svcauth_null_release,
  690. .set_client = svcauth_unix_set_client,
  691. };
  692. static int
  693. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  694. {
  695. struct kvec *argv = &rqstp->rq_arg.head[0];
  696. struct kvec *resv = &rqstp->rq_res.head[0];
  697. struct svc_cred *cred = &rqstp->rq_cred;
  698. u32 slen, i;
  699. int len = argv->iov_len;
  700. cred->cr_group_info = NULL;
  701. rqstp->rq_client = NULL;
  702. if ((len -= 3*4) < 0)
  703. return SVC_GARBAGE;
  704. svc_getu32(argv); /* length */
  705. svc_getu32(argv); /* time stamp */
  706. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  707. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  708. goto badcred;
  709. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  710. argv->iov_len -= slen*4;
  711. cred->cr_uid = svc_getnl(argv); /* uid */
  712. cred->cr_gid = svc_getnl(argv); /* gid */
  713. slen = svc_getnl(argv); /* gids length */
  714. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  715. goto badcred;
  716. cred->cr_group_info = groups_alloc(slen);
  717. if (cred->cr_group_info == NULL)
  718. return SVC_CLOSE;
  719. for (i = 0; i < slen; i++)
  720. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  721. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  722. *authp = rpc_autherr_badverf;
  723. return SVC_DENIED;
  724. }
  725. /* Put NULL verifier */
  726. svc_putnl(resv, RPC_AUTH_NULL);
  727. svc_putnl(resv, 0);
  728. rqstp->rq_flavor = RPC_AUTH_UNIX;
  729. return SVC_OK;
  730. badcred:
  731. *authp = rpc_autherr_badcred;
  732. return SVC_DENIED;
  733. }
  734. static int
  735. svcauth_unix_release(struct svc_rqst *rqstp)
  736. {
  737. /* Verifier (such as it is) is already in place.
  738. */
  739. if (rqstp->rq_client)
  740. auth_domain_put(rqstp->rq_client);
  741. rqstp->rq_client = NULL;
  742. if (rqstp->rq_cred.cr_group_info)
  743. put_group_info(rqstp->rq_cred.cr_group_info);
  744. rqstp->rq_cred.cr_group_info = NULL;
  745. return 0;
  746. }
  747. struct auth_ops svcauth_unix = {
  748. .name = "unix",
  749. .owner = THIS_MODULE,
  750. .flavour = RPC_AUTH_UNIX,
  751. .accept = svcauth_unix_accept,
  752. .release = svcauth_unix_release,
  753. .domain_release = svcauth_unix_domain_release,
  754. .set_client = svcauth_unix_set_client,
  755. };
  756. static struct cache_detail ip_map_cache_template = {
  757. .owner = THIS_MODULE,
  758. .hash_size = IP_HASHMAX,
  759. .name = "auth.unix.ip",
  760. .cache_put = ip_map_put,
  761. .cache_upcall = ip_map_upcall,
  762. .cache_parse = ip_map_parse,
  763. .cache_show = ip_map_show,
  764. .match = ip_map_match,
  765. .init = ip_map_init,
  766. .update = update,
  767. .alloc = ip_map_alloc,
  768. };
  769. int ip_map_cache_create(struct net *net)
  770. {
  771. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  772. struct cache_detail *cd;
  773. int err;
  774. cd = cache_create_net(&ip_map_cache_template, net);
  775. if (IS_ERR(cd))
  776. return PTR_ERR(cd);
  777. err = cache_register_net(cd, net);
  778. if (err) {
  779. cache_destroy_net(cd, net);
  780. return err;
  781. }
  782. sn->ip_map_cache = cd;
  783. return 0;
  784. }
  785. void ip_map_cache_destroy(struct net *net)
  786. {
  787. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  788. struct cache_detail *cd = sn->ip_map_cache;
  789. sn->ip_map_cache = NULL;
  790. cache_purge(cd);
  791. cache_unregister_net(cd, net);
  792. cache_destroy_net(cd, net);
  793. }