svcauth_unix.c 22 KB

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