svcauth_unix.c 20 KB

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