svcauth_unix.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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(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, NIPQUAD_FMT,
  145. ntohl(im->m_addr.s6_addr32[3]) >> 24 & 0xff,
  146. ntohl(im->m_addr.s6_addr32[3]) >> 16 & 0xff,
  147. ntohl(im->m_addr.s6_addr32[3]) >> 8 & 0xff,
  148. ntohl(im->m_addr.s6_addr32[3]) >> 0 & 0xff);
  149. } else {
  150. snprintf(text_addr, 40, "%p6", &im->m_addr);
  151. }
  152. qword_add(bpp, blen, im->m_class);
  153. qword_add(bpp, blen, text_addr);
  154. (*bpp)[-1] = '\n';
  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, NIPQUAD_FMT "%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, NIP6_FMT "%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 " NIPQUAD_FMT " %s\n",
  244. im->m_class,
  245. ntohl(addr.s6_addr32[3]) >> 24 & 0xff,
  246. ntohl(addr.s6_addr32[3]) >> 16 & 0xff,
  247. ntohl(addr.s6_addr32[3]) >> 8 & 0xff,
  248. ntohl(addr.s6_addr32[3]) >> 0 & 0xff,
  249. dom);
  250. } else {
  251. seq_printf(m, "%s %p6 %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_request = ip_map_request,
  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(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(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(auth_unix_lookup);
  354. void svcauth_unix_purge(void)
  355. {
  356. cache_purge(&ip_map_cache);
  357. }
  358. EXPORT_SYMBOL(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 struct unix_gid *unix_gid_lookup(uid_t uid);
  468. extern struct cache_detail unix_gid_cache;
  469. static int unix_gid_parse(struct cache_detail *cd,
  470. char *mesg, int mlen)
  471. {
  472. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  473. int uid;
  474. int gids;
  475. int rv;
  476. int i;
  477. int err;
  478. time_t expiry;
  479. struct unix_gid ug, *ugp;
  480. if (mlen <= 0 || mesg[mlen-1] != '\n')
  481. return -EINVAL;
  482. mesg[mlen-1] = 0;
  483. rv = get_int(&mesg, &uid);
  484. if (rv)
  485. return -EINVAL;
  486. ug.uid = uid;
  487. expiry = get_expiry(&mesg);
  488. if (expiry == 0)
  489. return -EINVAL;
  490. rv = get_int(&mesg, &gids);
  491. if (rv || gids < 0 || gids > 8192)
  492. return -EINVAL;
  493. ug.gi = groups_alloc(gids);
  494. if (!ug.gi)
  495. return -ENOMEM;
  496. for (i = 0 ; i < gids ; i++) {
  497. int gid;
  498. rv = get_int(&mesg, &gid);
  499. err = -EINVAL;
  500. if (rv)
  501. goto out;
  502. GROUP_AT(ug.gi, i) = gid;
  503. }
  504. ugp = unix_gid_lookup(uid);
  505. if (ugp) {
  506. struct cache_head *ch;
  507. ug.h.flags = 0;
  508. ug.h.expiry_time = expiry;
  509. ch = sunrpc_cache_update(&unix_gid_cache,
  510. &ug.h, &ugp->h,
  511. hash_long(uid, GID_HASHBITS));
  512. if (!ch)
  513. err = -ENOMEM;
  514. else {
  515. err = 0;
  516. cache_put(ch, &unix_gid_cache);
  517. }
  518. } else
  519. err = -ENOMEM;
  520. out:
  521. if (ug.gi)
  522. put_group_info(ug.gi);
  523. return err;
  524. }
  525. static int unix_gid_show(struct seq_file *m,
  526. struct cache_detail *cd,
  527. struct cache_head *h)
  528. {
  529. struct unix_gid *ug;
  530. int i;
  531. int glen;
  532. if (h == NULL) {
  533. seq_puts(m, "#uid cnt: gids...\n");
  534. return 0;
  535. }
  536. ug = container_of(h, struct unix_gid, h);
  537. if (test_bit(CACHE_VALID, &h->flags) &&
  538. !test_bit(CACHE_NEGATIVE, &h->flags))
  539. glen = ug->gi->ngroups;
  540. else
  541. glen = 0;
  542. seq_printf(m, "%d %d:", ug->uid, glen);
  543. for (i = 0; i < glen; i++)
  544. seq_printf(m, " %d", GROUP_AT(ug->gi, i));
  545. seq_printf(m, "\n");
  546. return 0;
  547. }
  548. struct cache_detail unix_gid_cache = {
  549. .owner = THIS_MODULE,
  550. .hash_size = GID_HASHMAX,
  551. .hash_table = gid_table,
  552. .name = "auth.unix.gid",
  553. .cache_put = unix_gid_put,
  554. .cache_request = unix_gid_request,
  555. .cache_parse = unix_gid_parse,
  556. .cache_show = unix_gid_show,
  557. .match = unix_gid_match,
  558. .init = unix_gid_init,
  559. .update = unix_gid_update,
  560. .alloc = unix_gid_alloc,
  561. };
  562. static struct unix_gid *unix_gid_lookup(uid_t uid)
  563. {
  564. struct unix_gid ug;
  565. struct cache_head *ch;
  566. ug.uid = uid;
  567. ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
  568. hash_long(uid, GID_HASHBITS));
  569. if (ch)
  570. return container_of(ch, struct unix_gid, h);
  571. else
  572. return NULL;
  573. }
  574. static int unix_gid_find(uid_t uid, struct group_info **gip,
  575. struct svc_rqst *rqstp)
  576. {
  577. struct unix_gid *ug = unix_gid_lookup(uid);
  578. if (!ug)
  579. return -EAGAIN;
  580. switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
  581. case -ENOENT:
  582. *gip = NULL;
  583. return 0;
  584. case 0:
  585. *gip = ug->gi;
  586. get_group_info(*gip);
  587. return 0;
  588. default:
  589. return -EAGAIN;
  590. }
  591. }
  592. int
  593. svcauth_unix_set_client(struct svc_rqst *rqstp)
  594. {
  595. struct sockaddr_in *sin;
  596. struct sockaddr_in6 *sin6, sin6_storage;
  597. struct ip_map *ipm;
  598. switch (rqstp->rq_addr.ss_family) {
  599. case AF_INET:
  600. sin = svc_addr_in(rqstp);
  601. sin6 = &sin6_storage;
  602. ipv6_addr_set(&sin6->sin6_addr, 0, 0,
  603. htonl(0x0000FFFF), sin->sin_addr.s_addr);
  604. break;
  605. case AF_INET6:
  606. sin6 = svc_addr_in6(rqstp);
  607. break;
  608. default:
  609. BUG();
  610. }
  611. rqstp->rq_client = NULL;
  612. if (rqstp->rq_proc == 0)
  613. return SVC_OK;
  614. ipm = ip_map_cached_get(rqstp);
  615. if (ipm == NULL)
  616. ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
  617. &sin6->sin6_addr);
  618. if (ipm == NULL)
  619. return SVC_DENIED;
  620. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  621. default:
  622. BUG();
  623. case -EAGAIN:
  624. case -ETIMEDOUT:
  625. return SVC_DROP;
  626. case -ENOENT:
  627. return SVC_DENIED;
  628. case 0:
  629. rqstp->rq_client = &ipm->m_client->h;
  630. kref_get(&rqstp->rq_client->ref);
  631. ip_map_cached_put(rqstp, ipm);
  632. break;
  633. }
  634. return SVC_OK;
  635. }
  636. EXPORT_SYMBOL(svcauth_unix_set_client);
  637. static int
  638. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  639. {
  640. struct kvec *argv = &rqstp->rq_arg.head[0];
  641. struct kvec *resv = &rqstp->rq_res.head[0];
  642. struct svc_cred *cred = &rqstp->rq_cred;
  643. cred->cr_group_info = NULL;
  644. rqstp->rq_client = NULL;
  645. if (argv->iov_len < 3*4)
  646. return SVC_GARBAGE;
  647. if (svc_getu32(argv) != 0) {
  648. dprintk("svc: bad null cred\n");
  649. *authp = rpc_autherr_badcred;
  650. return SVC_DENIED;
  651. }
  652. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  653. dprintk("svc: bad null verf\n");
  654. *authp = rpc_autherr_badverf;
  655. return SVC_DENIED;
  656. }
  657. /* Signal that mapping to nobody uid/gid is required */
  658. cred->cr_uid = (uid_t) -1;
  659. cred->cr_gid = (gid_t) -1;
  660. cred->cr_group_info = groups_alloc(0);
  661. if (cred->cr_group_info == NULL)
  662. return SVC_DROP; /* kmalloc failure - client must retry */
  663. /* Put NULL verifier */
  664. svc_putnl(resv, RPC_AUTH_NULL);
  665. svc_putnl(resv, 0);
  666. rqstp->rq_flavor = RPC_AUTH_NULL;
  667. return SVC_OK;
  668. }
  669. static int
  670. svcauth_null_release(struct svc_rqst *rqstp)
  671. {
  672. if (rqstp->rq_client)
  673. auth_domain_put(rqstp->rq_client);
  674. rqstp->rq_client = NULL;
  675. if (rqstp->rq_cred.cr_group_info)
  676. put_group_info(rqstp->rq_cred.cr_group_info);
  677. rqstp->rq_cred.cr_group_info = NULL;
  678. return 0; /* don't drop */
  679. }
  680. struct auth_ops svcauth_null = {
  681. .name = "null",
  682. .owner = THIS_MODULE,
  683. .flavour = RPC_AUTH_NULL,
  684. .accept = svcauth_null_accept,
  685. .release = svcauth_null_release,
  686. .set_client = svcauth_unix_set_client,
  687. };
  688. static int
  689. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  690. {
  691. struct kvec *argv = &rqstp->rq_arg.head[0];
  692. struct kvec *resv = &rqstp->rq_res.head[0];
  693. struct svc_cred *cred = &rqstp->rq_cred;
  694. u32 slen, i;
  695. int len = argv->iov_len;
  696. cred->cr_group_info = NULL;
  697. rqstp->rq_client = NULL;
  698. if ((len -= 3*4) < 0)
  699. return SVC_GARBAGE;
  700. svc_getu32(argv); /* length */
  701. svc_getu32(argv); /* time stamp */
  702. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  703. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  704. goto badcred;
  705. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  706. argv->iov_len -= slen*4;
  707. cred->cr_uid = svc_getnl(argv); /* uid */
  708. cred->cr_gid = svc_getnl(argv); /* gid */
  709. slen = svc_getnl(argv); /* gids length */
  710. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  711. goto badcred;
  712. if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
  713. == -EAGAIN)
  714. return SVC_DROP;
  715. if (cred->cr_group_info == NULL) {
  716. cred->cr_group_info = groups_alloc(slen);
  717. if (cred->cr_group_info == NULL)
  718. return SVC_DROP;
  719. for (i = 0; i < slen; i++)
  720. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  721. } else {
  722. for (i = 0; i < slen ; i++)
  723. svc_getnl(argv);
  724. }
  725. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  726. *authp = rpc_autherr_badverf;
  727. return SVC_DENIED;
  728. }
  729. /* Put NULL verifier */
  730. svc_putnl(resv, RPC_AUTH_NULL);
  731. svc_putnl(resv, 0);
  732. rqstp->rq_flavor = RPC_AUTH_UNIX;
  733. return SVC_OK;
  734. badcred:
  735. *authp = rpc_autherr_badcred;
  736. return SVC_DENIED;
  737. }
  738. static int
  739. svcauth_unix_release(struct svc_rqst *rqstp)
  740. {
  741. /* Verifier (such as it is) is already in place.
  742. */
  743. if (rqstp->rq_client)
  744. auth_domain_put(rqstp->rq_client);
  745. rqstp->rq_client = NULL;
  746. if (rqstp->rq_cred.cr_group_info)
  747. put_group_info(rqstp->rq_cred.cr_group_info);
  748. rqstp->rq_cred.cr_group_info = NULL;
  749. return 0;
  750. }
  751. struct auth_ops svcauth_unix = {
  752. .name = "unix",
  753. .owner = THIS_MODULE,
  754. .flavour = RPC_AUTH_UNIX,
  755. .accept = svcauth_unix_accept,
  756. .release = svcauth_unix_release,
  757. .domain_release = svcauth_unix_domain_release,
  758. .set_client = svcauth_unix_set_client,
  759. };