svcauth_unix.c 20 KB

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