svcauth_unix.c 18 KB

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