svcauth_unix.c 19 KB

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