svcauth_unix.c 22 KB

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