svcauth_unix.c 22 KB

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