svcauth_unix.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907
  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. /*
  20. * AUTHUNIX and AUTHNULL credentials are both handled here.
  21. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  22. * are always nobody (-2). i.e. we do the same IP address checks for
  23. * AUTHNULL as for AUTHUNIX, and that is done here.
  24. */
  25. struct unix_domain {
  26. struct auth_domain h;
  27. int addr_changes;
  28. /* other stuff later */
  29. };
  30. extern struct auth_ops svcauth_unix;
  31. struct auth_domain *unix_domain_find(char *name)
  32. {
  33. struct auth_domain *rv;
  34. struct unix_domain *new = NULL;
  35. rv = auth_domain_lookup(name, NULL);
  36. while(1) {
  37. if (rv) {
  38. if (new && rv != &new->h)
  39. auth_domain_put(&new->h);
  40. if (rv->flavour != &svcauth_unix) {
  41. auth_domain_put(rv);
  42. return NULL;
  43. }
  44. return rv;
  45. }
  46. new = kmalloc(sizeof(*new), GFP_KERNEL);
  47. if (new == NULL)
  48. return NULL;
  49. kref_init(&new->h.ref);
  50. new->h.name = kstrdup(name, GFP_KERNEL);
  51. if (new->h.name == NULL) {
  52. kfree(new);
  53. return NULL;
  54. }
  55. new->h.flavour = &svcauth_unix;
  56. new->addr_changes = 0;
  57. rv = auth_domain_lookup(name, &new->h);
  58. }
  59. }
  60. EXPORT_SYMBOL_GPL(unix_domain_find);
  61. static void svcauth_unix_domain_release(struct auth_domain *dom)
  62. {
  63. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  64. kfree(dom->name);
  65. kfree(ud);
  66. }
  67. /**************************************************
  68. * cache for IP address to unix_domain
  69. * as needed by AUTH_UNIX
  70. */
  71. #define IP_HASHBITS 8
  72. #define IP_HASHMAX (1<<IP_HASHBITS)
  73. #define IP_HASHMASK (IP_HASHMAX-1)
  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 struct cache_head *ip_table[IP_HASHMAX];
  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. memset(&sin6, 0, sizeof(sin6));
  194. sin6.sin6_family = AF_INET6;
  195. sin6.sin6_addr.s6_addr32[2] = htonl(0xffff);
  196. sin6.sin6_addr.s6_addr32[3] = address.s4.sin_addr.s_addr;
  197. break;
  198. #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
  199. case AF_INET6:
  200. memcpy(&sin6, &address.s6, sizeof(sin6));
  201. break;
  202. #endif
  203. default:
  204. return -EINVAL;
  205. }
  206. expiry = get_expiry(&mesg);
  207. if (expiry ==0)
  208. return -EINVAL;
  209. /* domainname, or empty for NEGATIVE */
  210. len = qword_get(&mesg, buf, mlen);
  211. if (len < 0) return -EINVAL;
  212. if (len) {
  213. dom = unix_domain_find(buf);
  214. if (dom == NULL)
  215. return -ENOENT;
  216. } else
  217. dom = NULL;
  218. /* IPv6 scope IDs are ignored for now */
  219. ipmp = __ip_map_lookup(cd, class, &sin6.sin6_addr);
  220. if (ipmp) {
  221. err = __ip_map_update(cd, ipmp,
  222. container_of(dom, struct unix_domain, h),
  223. expiry);
  224. } else
  225. err = -ENOMEM;
  226. if (dom)
  227. auth_domain_put(dom);
  228. cache_flush();
  229. return err;
  230. }
  231. static int ip_map_show(struct seq_file *m,
  232. struct cache_detail *cd,
  233. struct cache_head *h)
  234. {
  235. struct ip_map *im;
  236. struct in6_addr addr;
  237. char *dom = "-no-domain-";
  238. if (h == NULL) {
  239. seq_puts(m, "#class IP domain\n");
  240. return 0;
  241. }
  242. im = container_of(h, struct ip_map, h);
  243. /* class addr domain */
  244. ipv6_addr_copy(&addr, &im->m_addr);
  245. if (test_bit(CACHE_VALID, &h->flags) &&
  246. !test_bit(CACHE_NEGATIVE, &h->flags))
  247. dom = im->m_client->h.name;
  248. if (ipv6_addr_v4mapped(&addr)) {
  249. seq_printf(m, "%s %pI4 %s\n",
  250. im->m_class, &addr.s6_addr32[3], dom);
  251. } else {
  252. seq_printf(m, "%s %pI6 %s\n", im->m_class, &addr, dom);
  253. }
  254. return 0;
  255. }
  256. struct cache_detail ip_map_cache = {
  257. .owner = THIS_MODULE,
  258. .hash_size = IP_HASHMAX,
  259. .hash_table = ip_table,
  260. .name = "auth.unix.ip",
  261. .cache_put = ip_map_put,
  262. .cache_upcall = ip_map_upcall,
  263. .cache_parse = ip_map_parse,
  264. .cache_show = ip_map_show,
  265. .match = ip_map_match,
  266. .init = ip_map_init,
  267. .update = update,
  268. .alloc = ip_map_alloc,
  269. };
  270. static struct ip_map *__ip_map_lookup(struct cache_detail *cd, char *class,
  271. struct in6_addr *addr)
  272. {
  273. struct ip_map ip;
  274. struct cache_head *ch;
  275. strcpy(ip.m_class, class);
  276. ipv6_addr_copy(&ip.m_addr, addr);
  277. ch = sunrpc_cache_lookup(cd, &ip.h,
  278. hash_str(class, IP_HASHBITS) ^
  279. hash_ip6(*addr));
  280. if (ch)
  281. return container_of(ch, struct ip_map, h);
  282. else
  283. return NULL;
  284. }
  285. static inline struct ip_map *ip_map_lookup(char *class, struct in6_addr *addr)
  286. {
  287. return __ip_map_lookup(&ip_map_cache, class, addr);
  288. }
  289. static int __ip_map_update(struct cache_detail *cd, struct ip_map *ipm,
  290. struct unix_domain *udom, time_t expiry)
  291. {
  292. struct ip_map ip;
  293. struct cache_head *ch;
  294. ip.m_client = udom;
  295. ip.h.flags = 0;
  296. if (!udom)
  297. set_bit(CACHE_NEGATIVE, &ip.h.flags);
  298. else {
  299. ip.m_add_change = udom->addr_changes;
  300. /* if this is from the legacy set_client system call,
  301. * we need m_add_change to be one higher
  302. */
  303. if (expiry == NEVER)
  304. ip.m_add_change++;
  305. }
  306. ip.h.expiry_time = expiry;
  307. ch = sunrpc_cache_update(cd, &ip.h, &ipm->h,
  308. hash_str(ipm->m_class, IP_HASHBITS) ^
  309. hash_ip6(ipm->m_addr));
  310. if (!ch)
  311. return -ENOMEM;
  312. cache_put(ch, cd);
  313. return 0;
  314. }
  315. static inline int ip_map_update(struct ip_map *ipm, struct unix_domain *udom, time_t expiry)
  316. {
  317. return __ip_map_update(&ip_map_cache, ipm, udom, expiry);
  318. }
  319. int auth_unix_add_addr(struct in6_addr *addr, struct auth_domain *dom)
  320. {
  321. struct unix_domain *udom;
  322. struct ip_map *ipmp;
  323. if (dom->flavour != &svcauth_unix)
  324. return -EINVAL;
  325. udom = container_of(dom, struct unix_domain, h);
  326. ipmp = ip_map_lookup("nfsd", addr);
  327. if (ipmp)
  328. return ip_map_update(ipmp, udom, NEVER);
  329. else
  330. return -ENOMEM;
  331. }
  332. EXPORT_SYMBOL_GPL(auth_unix_add_addr);
  333. int auth_unix_forget_old(struct auth_domain *dom)
  334. {
  335. struct unix_domain *udom;
  336. if (dom->flavour != &svcauth_unix)
  337. return -EINVAL;
  338. udom = container_of(dom, struct unix_domain, h);
  339. udom->addr_changes++;
  340. return 0;
  341. }
  342. EXPORT_SYMBOL_GPL(auth_unix_forget_old);
  343. struct auth_domain *auth_unix_lookup(struct in6_addr *addr)
  344. {
  345. struct ip_map *ipm;
  346. struct auth_domain *rv;
  347. ipm = ip_map_lookup("nfsd", addr);
  348. if (!ipm)
  349. return NULL;
  350. if (cache_check(&ip_map_cache, &ipm->h, NULL))
  351. return NULL;
  352. if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
  353. if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
  354. auth_domain_put(&ipm->m_client->h);
  355. rv = NULL;
  356. } else {
  357. rv = &ipm->m_client->h;
  358. kref_get(&rv->ref);
  359. }
  360. cache_put(&ipm->h, &ip_map_cache);
  361. return rv;
  362. }
  363. EXPORT_SYMBOL_GPL(auth_unix_lookup);
  364. void svcauth_unix_purge(void)
  365. {
  366. cache_purge(&ip_map_cache);
  367. }
  368. EXPORT_SYMBOL_GPL(svcauth_unix_purge);
  369. static inline struct ip_map *
  370. ip_map_cached_get(struct svc_rqst *rqstp)
  371. {
  372. struct ip_map *ipm = NULL;
  373. struct svc_xprt *xprt = rqstp->rq_xprt;
  374. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  375. spin_lock(&xprt->xpt_lock);
  376. ipm = xprt->xpt_auth_cache;
  377. if (ipm != NULL) {
  378. if (!cache_valid(&ipm->h)) {
  379. /*
  380. * The entry has been invalidated since it was
  381. * remembered, e.g. by a second mount from the
  382. * same IP address.
  383. */
  384. xprt->xpt_auth_cache = NULL;
  385. spin_unlock(&xprt->xpt_lock);
  386. cache_put(&ipm->h, &ip_map_cache);
  387. return NULL;
  388. }
  389. cache_get(&ipm->h);
  390. }
  391. spin_unlock(&xprt->xpt_lock);
  392. }
  393. return ipm;
  394. }
  395. static inline void
  396. ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
  397. {
  398. struct svc_xprt *xprt = rqstp->rq_xprt;
  399. if (test_bit(XPT_CACHE_AUTH, &xprt->xpt_flags)) {
  400. spin_lock(&xprt->xpt_lock);
  401. if (xprt->xpt_auth_cache == NULL) {
  402. /* newly cached, keep the reference */
  403. xprt->xpt_auth_cache = ipm;
  404. ipm = NULL;
  405. }
  406. spin_unlock(&xprt->xpt_lock);
  407. }
  408. if (ipm)
  409. cache_put(&ipm->h, &ip_map_cache);
  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. cache_put(&ipm->h, &ip_map_cache);
  418. }
  419. /****************************************************************************
  420. * auth.unix.gid cache
  421. * simple cache to map a UID to a list of GIDs
  422. * because AUTH_UNIX aka AUTH_SYS has a max of 16
  423. */
  424. #define GID_HASHBITS 8
  425. #define GID_HASHMAX (1<<GID_HASHBITS)
  426. #define GID_HASHMASK (GID_HASHMAX - 1)
  427. struct unix_gid {
  428. struct cache_head h;
  429. uid_t uid;
  430. struct group_info *gi;
  431. };
  432. static struct cache_head *gid_table[GID_HASHMAX];
  433. static void unix_gid_put(struct kref *kref)
  434. {
  435. struct cache_head *item = container_of(kref, struct cache_head, ref);
  436. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  437. if (test_bit(CACHE_VALID, &item->flags) &&
  438. !test_bit(CACHE_NEGATIVE, &item->flags))
  439. put_group_info(ug->gi);
  440. kfree(ug);
  441. }
  442. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  443. {
  444. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  445. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  446. return orig->uid == new->uid;
  447. }
  448. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  449. {
  450. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  451. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  452. new->uid = item->uid;
  453. }
  454. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  455. {
  456. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  457. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  458. get_group_info(item->gi);
  459. new->gi = item->gi;
  460. }
  461. static struct cache_head *unix_gid_alloc(void)
  462. {
  463. struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
  464. if (g)
  465. return &g->h;
  466. else
  467. return NULL;
  468. }
  469. static void unix_gid_request(struct cache_detail *cd,
  470. struct cache_head *h,
  471. char **bpp, int *blen)
  472. {
  473. char tuid[20];
  474. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  475. snprintf(tuid, 20, "%u", ug->uid);
  476. qword_add(bpp, blen, tuid);
  477. (*bpp)[-1] = '\n';
  478. }
  479. static int unix_gid_upcall(struct cache_detail *cd, struct cache_head *h)
  480. {
  481. return sunrpc_cache_pipe_upcall(cd, h, unix_gid_request);
  482. }
  483. static struct unix_gid *unix_gid_lookup(uid_t uid);
  484. extern struct cache_detail unix_gid_cache;
  485. static int unix_gid_parse(struct cache_detail *cd,
  486. char *mesg, int mlen)
  487. {
  488. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  489. int uid;
  490. int gids;
  491. int rv;
  492. int i;
  493. int err;
  494. time_t expiry;
  495. struct unix_gid ug, *ugp;
  496. if (mlen <= 0 || mesg[mlen-1] != '\n')
  497. return -EINVAL;
  498. mesg[mlen-1] = 0;
  499. rv = get_int(&mesg, &uid);
  500. if (rv)
  501. return -EINVAL;
  502. ug.uid = uid;
  503. expiry = get_expiry(&mesg);
  504. if (expiry == 0)
  505. return -EINVAL;
  506. rv = get_int(&mesg, &gids);
  507. if (rv || gids < 0 || gids > 8192)
  508. return -EINVAL;
  509. ug.gi = groups_alloc(gids);
  510. if (!ug.gi)
  511. return -ENOMEM;
  512. for (i = 0 ; i < gids ; i++) {
  513. int gid;
  514. rv = get_int(&mesg, &gid);
  515. err = -EINVAL;
  516. if (rv)
  517. goto out;
  518. GROUP_AT(ug.gi, i) = gid;
  519. }
  520. ugp = unix_gid_lookup(uid);
  521. if (ugp) {
  522. struct cache_head *ch;
  523. ug.h.flags = 0;
  524. ug.h.expiry_time = expiry;
  525. ch = sunrpc_cache_update(&unix_gid_cache,
  526. &ug.h, &ugp->h,
  527. hash_long(uid, GID_HASHBITS));
  528. if (!ch)
  529. err = -ENOMEM;
  530. else {
  531. err = 0;
  532. cache_put(ch, &unix_gid_cache);
  533. }
  534. } else
  535. err = -ENOMEM;
  536. out:
  537. if (ug.gi)
  538. put_group_info(ug.gi);
  539. return err;
  540. }
  541. static int unix_gid_show(struct seq_file *m,
  542. struct cache_detail *cd,
  543. struct cache_head *h)
  544. {
  545. struct unix_gid *ug;
  546. int i;
  547. int glen;
  548. if (h == NULL) {
  549. seq_puts(m, "#uid cnt: gids...\n");
  550. return 0;
  551. }
  552. ug = container_of(h, struct unix_gid, h);
  553. if (test_bit(CACHE_VALID, &h->flags) &&
  554. !test_bit(CACHE_NEGATIVE, &h->flags))
  555. glen = ug->gi->ngroups;
  556. else
  557. glen = 0;
  558. seq_printf(m, "%u %d:", ug->uid, glen);
  559. for (i = 0; i < glen; i++)
  560. seq_printf(m, " %d", GROUP_AT(ug->gi, i));
  561. seq_printf(m, "\n");
  562. return 0;
  563. }
  564. struct cache_detail unix_gid_cache = {
  565. .owner = THIS_MODULE,
  566. .hash_size = GID_HASHMAX,
  567. .hash_table = gid_table,
  568. .name = "auth.unix.gid",
  569. .cache_put = unix_gid_put,
  570. .cache_upcall = unix_gid_upcall,
  571. .cache_parse = unix_gid_parse,
  572. .cache_show = unix_gid_show,
  573. .match = unix_gid_match,
  574. .init = unix_gid_init,
  575. .update = unix_gid_update,
  576. .alloc = unix_gid_alloc,
  577. };
  578. static struct unix_gid *unix_gid_lookup(uid_t uid)
  579. {
  580. struct unix_gid ug;
  581. struct cache_head *ch;
  582. ug.uid = uid;
  583. ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
  584. hash_long(uid, GID_HASHBITS));
  585. if (ch)
  586. return container_of(ch, struct unix_gid, h);
  587. else
  588. return NULL;
  589. }
  590. static struct group_info *unix_gid_find(uid_t uid, struct svc_rqst *rqstp)
  591. {
  592. struct unix_gid *ug;
  593. struct group_info *gi;
  594. int ret;
  595. ug = unix_gid_lookup(uid);
  596. if (!ug)
  597. return ERR_PTR(-EAGAIN);
  598. ret = cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle);
  599. switch (ret) {
  600. case -ENOENT:
  601. return ERR_PTR(-ENOENT);
  602. case -ETIMEDOUT:
  603. return ERR_PTR(-ESHUTDOWN);
  604. case 0:
  605. gi = get_group_info(ug->gi);
  606. cache_put(&ug->h, &unix_gid_cache);
  607. return gi;
  608. default:
  609. return ERR_PTR(-EAGAIN);
  610. }
  611. }
  612. int
  613. svcauth_unix_set_client(struct svc_rqst *rqstp)
  614. {
  615. struct sockaddr_in *sin;
  616. struct sockaddr_in6 *sin6, sin6_storage;
  617. struct ip_map *ipm;
  618. struct group_info *gi;
  619. struct svc_cred *cred = &rqstp->rq_cred;
  620. switch (rqstp->rq_addr.ss_family) {
  621. case AF_INET:
  622. sin = svc_addr_in(rqstp);
  623. sin6 = &sin6_storage;
  624. ipv6_addr_set_v4mapped(sin->sin_addr.s_addr, &sin6->sin6_addr);
  625. break;
  626. case AF_INET6:
  627. sin6 = svc_addr_in6(rqstp);
  628. break;
  629. default:
  630. BUG();
  631. }
  632. rqstp->rq_client = NULL;
  633. if (rqstp->rq_proc == 0)
  634. return SVC_OK;
  635. ipm = ip_map_cached_get(rqstp);
  636. if (ipm == NULL)
  637. ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
  638. &sin6->sin6_addr);
  639. if (ipm == NULL)
  640. return SVC_DENIED;
  641. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  642. default:
  643. BUG();
  644. case -ETIMEDOUT:
  645. return SVC_CLOSE;
  646. case -EAGAIN:
  647. return SVC_DROP;
  648. case -ENOENT:
  649. return SVC_DENIED;
  650. case 0:
  651. rqstp->rq_client = &ipm->m_client->h;
  652. kref_get(&rqstp->rq_client->ref);
  653. ip_map_cached_put(rqstp, ipm);
  654. break;
  655. }
  656. gi = unix_gid_find(cred->cr_uid, rqstp);
  657. switch (PTR_ERR(gi)) {
  658. case -EAGAIN:
  659. return SVC_DROP;
  660. case -ESHUTDOWN:
  661. return SVC_CLOSE;
  662. case -ENOENT:
  663. break;
  664. default:
  665. put_group_info(cred->cr_group_info);
  666. cred->cr_group_info = gi;
  667. }
  668. return SVC_OK;
  669. }
  670. EXPORT_SYMBOL_GPL(svcauth_unix_set_client);
  671. static int
  672. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  673. {
  674. struct kvec *argv = &rqstp->rq_arg.head[0];
  675. struct kvec *resv = &rqstp->rq_res.head[0];
  676. struct svc_cred *cred = &rqstp->rq_cred;
  677. cred->cr_group_info = NULL;
  678. rqstp->rq_client = NULL;
  679. if (argv->iov_len < 3*4)
  680. return SVC_GARBAGE;
  681. if (svc_getu32(argv) != 0) {
  682. dprintk("svc: bad null cred\n");
  683. *authp = rpc_autherr_badcred;
  684. return SVC_DENIED;
  685. }
  686. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  687. dprintk("svc: bad null verf\n");
  688. *authp = rpc_autherr_badverf;
  689. return SVC_DENIED;
  690. }
  691. /* Signal that mapping to nobody uid/gid is required */
  692. cred->cr_uid = (uid_t) -1;
  693. cred->cr_gid = (gid_t) -1;
  694. cred->cr_group_info = groups_alloc(0);
  695. if (cred->cr_group_info == NULL)
  696. return SVC_CLOSE; /* kmalloc failure - client must retry */
  697. /* Put NULL verifier */
  698. svc_putnl(resv, RPC_AUTH_NULL);
  699. svc_putnl(resv, 0);
  700. rqstp->rq_flavor = RPC_AUTH_NULL;
  701. return SVC_OK;
  702. }
  703. static int
  704. svcauth_null_release(struct svc_rqst *rqstp)
  705. {
  706. if (rqstp->rq_client)
  707. auth_domain_put(rqstp->rq_client);
  708. rqstp->rq_client = NULL;
  709. if (rqstp->rq_cred.cr_group_info)
  710. put_group_info(rqstp->rq_cred.cr_group_info);
  711. rqstp->rq_cred.cr_group_info = NULL;
  712. return 0; /* don't drop */
  713. }
  714. struct auth_ops svcauth_null = {
  715. .name = "null",
  716. .owner = THIS_MODULE,
  717. .flavour = RPC_AUTH_NULL,
  718. .accept = svcauth_null_accept,
  719. .release = svcauth_null_release,
  720. .set_client = svcauth_unix_set_client,
  721. };
  722. static int
  723. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  724. {
  725. struct kvec *argv = &rqstp->rq_arg.head[0];
  726. struct kvec *resv = &rqstp->rq_res.head[0];
  727. struct svc_cred *cred = &rqstp->rq_cred;
  728. u32 slen, i;
  729. int len = argv->iov_len;
  730. cred->cr_group_info = NULL;
  731. rqstp->rq_client = NULL;
  732. if ((len -= 3*4) < 0)
  733. return SVC_GARBAGE;
  734. svc_getu32(argv); /* length */
  735. svc_getu32(argv); /* time stamp */
  736. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  737. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  738. goto badcred;
  739. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  740. argv->iov_len -= slen*4;
  741. cred->cr_uid = svc_getnl(argv); /* uid */
  742. cred->cr_gid = svc_getnl(argv); /* gid */
  743. slen = svc_getnl(argv); /* gids length */
  744. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  745. goto badcred;
  746. cred->cr_group_info = groups_alloc(slen);
  747. if (cred->cr_group_info == NULL)
  748. return SVC_CLOSE;
  749. for (i = 0; i < slen; i++)
  750. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  751. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  752. *authp = rpc_autherr_badverf;
  753. return SVC_DENIED;
  754. }
  755. /* Put NULL verifier */
  756. svc_putnl(resv, RPC_AUTH_NULL);
  757. svc_putnl(resv, 0);
  758. rqstp->rq_flavor = RPC_AUTH_UNIX;
  759. return SVC_OK;
  760. badcred:
  761. *authp = rpc_autherr_badcred;
  762. return SVC_DENIED;
  763. }
  764. static int
  765. svcauth_unix_release(struct svc_rqst *rqstp)
  766. {
  767. /* Verifier (such as it is) is already in place.
  768. */
  769. if (rqstp->rq_client)
  770. auth_domain_put(rqstp->rq_client);
  771. rqstp->rq_client = NULL;
  772. if (rqstp->rq_cred.cr_group_info)
  773. put_group_info(rqstp->rq_cred.cr_group_info);
  774. rqstp->rq_cred.cr_group_info = NULL;
  775. return 0;
  776. }
  777. struct auth_ops svcauth_unix = {
  778. .name = "unix",
  779. .owner = THIS_MODULE,
  780. .flavour = RPC_AUTH_UNIX,
  781. .accept = svcauth_unix_accept,
  782. .release = svcauth_unix_release,
  783. .domain_release = svcauth_unix_domain_release,
  784. .set_client = svcauth_unix_set_client,
  785. };