svcauth_unix.c 20 KB

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