svcauth_unix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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(struct net *net)
  299. {
  300. struct sunrpc_net *sn;
  301. sn = net_generic(net, sunrpc_net_id);
  302. cache_purge(sn->ip_map_cache);
  303. }
  304. EXPORT_SYMBOL_GPL(svcauth_unix_purge);
  305. static inline struct ip_map *
  306. ip_map_cached_get(struct svc_xprt *xprt)
  307. {
  308. struct ip_map *ipm = NULL;
  309. struct sunrpc_net *sn;
  310. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  311. spin_lock(&xprt->xpt_lock);
  312. ipm = xprt->xpt_auth_cache;
  313. if (ipm != NULL) {
  314. if (!cache_valid(&ipm->h)) {
  315. /*
  316. * The entry has been invalidated since it was
  317. * remembered, e.g. by a second mount from the
  318. * same IP address.
  319. */
  320. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  321. xprt->xpt_auth_cache = NULL;
  322. spin_unlock(&xprt->xpt_lock);
  323. cache_put(&ipm->h, sn->ip_map_cache);
  324. return NULL;
  325. }
  326. cache_get(&ipm->h);
  327. }
  328. spin_unlock(&xprt->xpt_lock);
  329. }
  330. return ipm;
  331. }
  332. static inline void
  333. ip_map_cached_put(struct svc_xprt *xprt, struct ip_map *ipm)
  334. {
  335. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  336. spin_lock(&xprt->xpt_lock);
  337. if (xprt->xpt_auth_cache == NULL) {
  338. /* newly cached, keep the reference */
  339. xprt->xpt_auth_cache = ipm;
  340. ipm = NULL;
  341. }
  342. spin_unlock(&xprt->xpt_lock);
  343. }
  344. if (ipm) {
  345. struct sunrpc_net *sn;
  346. sn = net_generic(xprt->xpt_net, sunrpc_net_id);
  347. cache_put(&ipm->h, sn->ip_map_cache);
  348. }
  349. }
  350. void
  351. svcauth_unix_info_release(struct svc_xprt *xpt)
  352. {
  353. struct ip_map *ipm;
  354. ipm = xpt->xpt_auth_cache;
  355. if (ipm != NULL) {
  356. struct sunrpc_net *sn;
  357. sn = net_generic(xpt->xpt_net, sunrpc_net_id);
  358. cache_put(&ipm->h, sn->ip_map_cache);
  359. }
  360. }
  361. /****************************************************************************
  362. * auth.unix.gid cache
  363. * simple cache to map a UID to a list of GIDs
  364. * because AUTH_UNIX aka AUTH_SYS has a max of 16
  365. */
  366. #define GID_HASHBITS 8
  367. #define GID_HASHMAX (1<<GID_HASHBITS)
  368. struct unix_gid {
  369. struct cache_head h;
  370. uid_t uid;
  371. struct group_info *gi;
  372. };
  373. static void unix_gid_put(struct kref *kref)
  374. {
  375. struct cache_head *item = container_of(kref, struct cache_head, ref);
  376. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  377. if (test_bit(CACHE_VALID, &item->flags) &&
  378. !test_bit(CACHE_NEGATIVE, &item->flags))
  379. put_group_info(ug->gi);
  380. kfree(ug);
  381. }
  382. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  383. {
  384. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  385. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  386. return orig->uid == new->uid;
  387. }
  388. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  389. {
  390. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  391. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  392. new->uid = item->uid;
  393. }
  394. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  395. {
  396. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  397. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  398. get_group_info(item->gi);
  399. new->gi = item->gi;
  400. }
  401. static struct cache_head *unix_gid_alloc(void)
  402. {
  403. struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
  404. if (g)
  405. return &g->h;
  406. else
  407. return NULL;
  408. }
  409. static void unix_gid_request(struct cache_detail *cd,
  410. struct cache_head *h,
  411. char **bpp, int *blen)
  412. {
  413. char tuid[20];
  414. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  415. snprintf(tuid, 20, "%u", ug->uid);
  416. qword_add(bpp, blen, tuid);
  417. (*bpp)[-1] = '\n';
  418. }
  419. static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
  420. {
  421. return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
  422. }
  423. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid);
  424. static int unix_gid_parse(struct cache_detail *cd,
  425. char *mesg, int mlen)
  426. {
  427. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  428. int uid;
  429. int gids;
  430. int rv;
  431. int i;
  432. int err;
  433. time_t expiry;
  434. struct unix_gid ug, *ugp;
  435. if (mesg[mlen - 1] != '\n')
  436. return -EINVAL;
  437. mesg[mlen-1] = 0;
  438. rv = get_int(&mesg, &uid);
  439. if (rv)
  440. return -EINVAL;
  441. ug.uid = uid;
  442. expiry = get_expiry(&mesg);
  443. if (expiry == 0)
  444. return -EINVAL;
  445. rv = get_int(&mesg, &gids);
  446. if (rv || gids < 0 || gids > 8192)
  447. return -EINVAL;
  448. ug.gi = groups_alloc(gids);
  449. if (!ug.gi)
  450. return -ENOMEM;
  451. for (i = 0 ; i < gids ; i++) {
  452. int gid;
  453. rv = get_int(&mesg, &gid);
  454. err = -EINVAL;
  455. if (rv)
  456. goto out;
  457. GROUP_AT(ug.gi, i) = gid;
  458. }
  459. ugp = unix_gid_lookup(cd, uid);
  460. if (ugp) {
  461. struct cache_head *ch;
  462. ug.h.flags = 0;
  463. ug.h.expiry_time = expiry;
  464. ch = sunrpc_cache_update(cd,
  465. &ug.h, &ugp->h,
  466. hash_long(uid, GID_HASHBITS));
  467. if (!ch)
  468. err = -ENOMEM;
  469. else {
  470. err = 0;
  471. cache_put(ch, cd);
  472. }
  473. } else
  474. err = -ENOMEM;
  475. out:
  476. if (ug.gi)
  477. put_group_info(ug.gi);
  478. return err;
  479. }
  480. static int unix_gid_show(struct seq_file *m,
  481. struct cache_detail *cd,
  482. struct cache_head *h)
  483. {
  484. struct unix_gid *ug;
  485. int i;
  486. int glen;
  487. if (h == NULL) {
  488. seq_puts(m, "#uid cnt: gids...\n");
  489. return 0;
  490. }
  491. ug = container_of(h, struct unix_gid, h);
  492. if (test_bit(CACHE_VALID, &h->flags) &&
  493. !test_bit(CACHE_NEGATIVE, &h->flags))
  494. glen = ug->gi->ngroups;
  495. else
  496. glen = 0;
  497. seq_printf(m, "%u %d:", ug->uid, glen);
  498. for (i = 0; i < glen; i++)
  499. seq_printf(m, " %d", GROUP_AT(ug->gi, i));
  500. seq_printf(m, "\n");
  501. return 0;
  502. }
  503. static struct cache_detail unix_gid_cache_template = {
  504. .owner = THIS_MODULE,
  505. .hash_size = GID_HASHMAX,
  506. .name = "auth.unix.gid",
  507. .cache_put = unix_gid_put,
  508. .cache_upcall = unix_gid_upcall,
  509. .cache_parse = unix_gid_parse,
  510. .cache_show = unix_gid_show,
  511. .match = unix_gid_match,
  512. .init = unix_gid_init,
  513. .update = unix_gid_update,
  514. .alloc = unix_gid_alloc,
  515. };
  516. int unix_gid_cache_create(struct net *net)
  517. {
  518. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  519. struct cache_detail *cd;
  520. int err;
  521. cd = cache_create_net(&unix_gid_cache_template, net);
  522. if (IS_ERR(cd))
  523. return PTR_ERR(cd);
  524. err = cache_register_net(cd, net);
  525. if (err) {
  526. cache_destroy_net(cd, net);
  527. return err;
  528. }
  529. sn->unix_gid_cache = cd;
  530. return 0;
  531. }
  532. void unix_gid_cache_destroy(struct net *net)
  533. {
  534. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  535. struct cache_detail *cd = sn->unix_gid_cache;
  536. sn->unix_gid_cache = NULL;
  537. cache_purge(cd);
  538. cache_unregister_net(cd, net);
  539. cache_destroy_net(cd, net);
  540. }
  541. static struct unix_gid *unix_gid_lookup(struct cache_detail *cd, uid_t uid)
  542. {
  543. struct unix_gid ug;
  544. struct cache_head *ch;
  545. ug.uid = uid;
  546. ch = sunrpc_cache_lookup(cd, &ug.h, hash_long(uid, GID_HASHBITS));
  547. if (ch)
  548. return container_of(ch, struct unix_gid, h);
  549. else
  550. return NULL;
  551. }
  552. static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
  553. {
  554. struct unix_gid *ug;
  555. struct group_info *gi;
  556. int ret;
  557. struct sunrpc_net *sn = net_generic(rqstp->rq_xprt->xpt_net,
  558. sunrpc_net_id);
  559. ug = unix_gid_lookup(sn->unix_gid_cache, uid);
  560. if (!ug)
  561. return ERR_PTR(-EAGAIN);
  562. ret = cache_check(sn->unix_gid_cache, &ug->h, &rqstp->rq_chandle);
  563. switch (ret) {
  564. case -ENOENT:
  565. return ERR_PTR(-ENOENT);
  566. case -ETIMEDOUT:
  567. return ERR_PTR(-ESHUTDOWN);
  568. case 0:
  569. gi = get_group_info(ug->gi);
  570. cache_put(&ug->h, sn->unix_gid_cache);
  571. return gi;
  572. default:
  573. return ERR_PTR(-EAGAIN);
  574. }
  575. }
  576. int
  577. svcauth_unix_set_client(struct svc_rqst *rqstp)
  578. {
  579. struct sockaddr_in *sin;
  580. struct sockaddr_in6 *sin6, sin6_storage;
  581. struct ip_map *ipm;
  582. struct group_info *gi;
  583. struct svc_cred *cred = &rqstp->rq_cred;
  584. struct svc_xprt *xprt = rqstp->rq_xprt;
  585. struct net *net = xprt->xpt_net;
  586. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  587. switch (rqstp->rq_addr.ss_family) {
  588. case AF_INET:
  589. sin = svc_addr_in(rqstp);
  590. sin6 = &sin6_storage;
  591. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
  592. break;
  593. case AF_INET6:
  594. sin6 = svc_addr_in6(rqstp);
  595. break;
  596. default:
  597. BUG();
  598. }
  599. rqstp->rq_client = NULL;
  600. if (rqstp->rq_proc == 0)
  601. return SVC_OK;
  602. ipm = ip_map_cached_get(xprt);
  603. if (ipm == NULL)
  604. ipm = __ip_map_lookup(sn->ip_map_cache, rqstp->rq_server->sv_program->pg_class,
  605. &sin6->sin6_addr);
  606. if (ipm == NULL)
  607. return SVC_DENIED;
  608. switch (cache_check(sn->ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  609. default:
  610. BUG();
  611. case -ETIMEDOUT:
  612. return SVC_CLOSE;
  613. case -EAGAIN:
  614. return SVC_DROP;
  615. case -ENOENT:
  616. return SVC_DENIED;
  617. case 0:
  618. rqstp->rq_client = &ipm->m_client->h;
  619. kref_get(&rqstp->rq_client->ref);
  620. ip_map_cached_put(xprt, ipm);
  621. break;
  622. }
  623. gi = unix_gid_find(cred->cr_uid, rqstp);
  624. switch (PTR_ERR(gi)) {
  625. case -EAGAIN:
  626. return SVC_DROP;
  627. case -ESHUTDOWN:
  628. return SVC_CLOSE;
  629. case -ENOENT:
  630. break;
  631. default:
  632. put_group_info(cred->cr_group_info);
  633. cred->cr_group_info = gi;
  634. }
  635. return SVC_OK;
  636. }
  637. EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
  638. static int
  639. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  640. {
  641. struct kvec *argv = &rqstp->rq_arg.head[0];
  642. struct kvec *resv = &rqstp->rq_res.head[0];
  643. struct svc_cred *cred = &rqstp->rq_cred;
  644. cred->cr_group_info = NULL;
  645. rqstp->rq_client = NULL;
  646. if (argv->iov_len < 3*4)
  647. return SVC_GARBAGE;
  648. if (svc_getu32(argv) != 0) {
  649. dprintk("svc: bad null cred\n");
  650. *authp = rpc_autherr_badcred;
  651. return SVC_DENIED;
  652. }
  653. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  654. dprintk("svc: bad null verf\n");
  655. *authp = rpc_autherr_badverf;
  656. return SVC_DENIED;
  657. }
  658. /* Signal that mapping to nobody uid/gid is required */
  659. cred->cr_uid = (uid_t) -1;
  660. cred->cr_gid = (gid_t) -1;
  661. cred->cr_group_info = groups_alloc(0);
  662. if (cred->cr_group_info == NULL)
  663. return SVC_CLOSE; /* kmalloc failure - client must retry */
  664. /* Put NULL verifier */
  665. svc_putnl(resv, RPC_AUTH_NULL);
  666. svc_putnl(resv, 0);
  667. rqstp->rq_flavor = RPC_AUTH_NULL;
  668. return SVC_OK;
  669. }
  670. static int
  671. svcauth_null_release(struct svc_rqst *rqstp)
  672. {
  673. if (rqstp->rq_client)
  674. auth_domain_put(rqstp->rq_client);
  675. rqstp->rq_client = NULL;
  676. if (rqstp->rq_cred.cr_group_info)
  677. put_group_info(rqstp->rq_cred.cr_group_info);
  678. rqstp->rq_cred.cr_group_info = NULL;
  679. return 0; /* don't drop */
  680. }
  681. struct auth_ops svcauth_null = {
  682. .name = "null",
  683. .owner = THIS_MODULE,
  684. .flavour = RPC_AUTH_NULL,
  685. .accept = svcauth_null_accept,
  686. .release = svcauth_null_release,
  687. .set_client = svcauth_unix_set_client,
  688. };
  689. static int
  690. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  691. {
  692. struct kvec *argv = &rqstp->rq_arg.head[0];
  693. struct kvec *resv = &rqstp->rq_res.head[0];
  694. struct svc_cred *cred = &rqstp->rq_cred;
  695. u32 slen, i;
  696. int len = argv->iov_len;
  697. cred->cr_group_info = NULL;
  698. rqstp->rq_client = NULL;
  699. if ((len -= 3*4) < 0)
  700. return SVC_GARBAGE;
  701. svc_getu32(argv); /* length */
  702. svc_getu32(argv); /* time stamp */
  703. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  704. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  705. goto badcred;
  706. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  707. argv->iov_len -= slen*4;
  708. cred->cr_uid = svc_getnl(argv); /* uid */
  709. cred->cr_gid = svc_getnl(argv); /* gid */
  710. slen = svc_getnl(argv); /* gids length */
  711. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  712. goto badcred;
  713. cred->cr_group_info = groups_alloc(slen);
  714. if (cred->cr_group_info == NULL)
  715. return SVC_CLOSE;
  716. for (i = 0; i < slen; i++)
  717. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  718. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  719. *authp = rpc_autherr_badverf;
  720. return SVC_DENIED;
  721. }
  722. /* Put NULL verifier */
  723. svc_putnl(resv, RPC_AUTH_NULL);
  724. svc_putnl(resv, 0);
  725. rqstp->rq_flavor = RPC_AUTH_UNIX;
  726. return SVC_OK;
  727. badcred:
  728. *authp = rpc_autherr_badcred;
  729. return SVC_DENIED;
  730. }
  731. static int
  732. svcauth_unix_release(struct svc_rqst *rqstp)
  733. {
  734. /* Verifier (such as it is) is already in place.
  735. */
  736. if (rqstp->rq_client)
  737. auth_domain_put(rqstp->rq_client);
  738. rqstp->rq_client = NULL;
  739. if (rqstp->rq_cred.cr_group_info)
  740. put_group_info(rqstp->rq_cred.cr_group_info);
  741. rqstp->rq_cred.cr_group_info = NULL;
  742. return 0;
  743. }
  744. struct auth_ops svcauth_unix = {
  745. .name = "unix",
  746. .owner = THIS_MODULE,
  747. .flavour = RPC_AUTH_UNIX,
  748. .accept = svcauth_unix_accept,
  749. .release = svcauth_unix_release,
  750. .domain_release = svcauth_unix_domain_release,
  751. .set_client = svcauth_unix_set_client,
  752. };
  753. static struct cache_detail ip_map_cache_template = {
  754. .owner = THIS_MODULE,
  755. .hash_size = IP_HASHMAX,
  756. .name = "auth.unix.ip",
  757. .cache_put = ip_map_put,
  758. .cache_upcall = ip_map_upcall,
  759. .cache_parse = ip_map_parse,
  760. .cache_show = ip_map_show,
  761. .match = ip_map_match,
  762. .init = ip_map_init,
  763. .update = update,
  764. .alloc = ip_map_alloc,
  765. };
  766. int ip_map_cache_create(struct net *net)
  767. {
  768. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  769. struct cache_detail *cd;
  770. int err;
  771. cd = cache_create_net(&ip_map_cache_template, net);
  772. if (IS_ERR(cd))
  773. return PTR_ERR(cd);
  774. err = cache_register_net(cd, net);
  775. if (err) {
  776. cache_destroy_net(cd, net);
  777. return err;
  778. }
  779. sn->ip_map_cache = cd;
  780. return 0;
  781. }
  782. void ip_map_cache_destroy(struct net *net)
  783. {
  784. struct sunrpc_net *sn = net_generic(net, sunrpc_net_id);
  785. struct cache_detail *cd = sn->ip_map_cache;
  786. sn->ip_map_cache = NULL;
  787. cache_purge(cd);
  788. cache_unregister_net(cd, net);
  789. cache_destroy_net(cd, net);
  790. }