svcauth_unix.c 20 KB

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