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