svcauth_unix.c 19 KB

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