svcauth_unix.c 20 KB

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