svcauth_unix.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  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;
  329. struct svc_sock *svsk = rqstp->rq_sock;
  330. spin_lock(&svsk->sk_lock);
  331. ipm = svsk->sk_info_authunix;
  332. if (ipm != NULL) {
  333. if (!cache_valid(&ipm->h)) {
  334. /*
  335. * The entry has been invalidated since it was
  336. * remembered, e.g. by a second mount from the
  337. * same IP address.
  338. */
  339. svsk->sk_info_authunix = NULL;
  340. spin_unlock(&svsk->sk_lock);
  341. cache_put(&ipm->h, &ip_map_cache);
  342. return NULL;
  343. }
  344. cache_get(&ipm->h);
  345. }
  346. spin_unlock(&svsk->sk_lock);
  347. return ipm;
  348. }
  349. static inline void
  350. ip_map_cached_put(struct svc_rqst *rqstp, struct ip_map *ipm)
  351. {
  352. struct svc_sock *svsk = rqstp->rq_sock;
  353. spin_lock(&svsk->sk_lock);
  354. if (svsk->sk_sock->type == SOCK_STREAM &&
  355. svsk->sk_info_authunix == NULL) {
  356. /* newly cached, keep the reference */
  357. svsk->sk_info_authunix = ipm;
  358. ipm = NULL;
  359. }
  360. spin_unlock(&svsk->sk_lock);
  361. if (ipm)
  362. cache_put(&ipm->h, &ip_map_cache);
  363. }
  364. void
  365. svcauth_unix_info_release(void *info)
  366. {
  367. struct ip_map *ipm = info;
  368. cache_put(&ipm->h, &ip_map_cache);
  369. }
  370. /****************************************************************************
  371. * auth.unix.gid cache
  372. * simple cache to map a UID to a list of GIDs
  373. * because AUTH_UNIX aka AUTH_SYS has a max of 16
  374. */
  375. #define GID_HASHBITS 8
  376. #define GID_HASHMAX (1<<GID_HASHBITS)
  377. #define GID_HASHMASK (GID_HASHMAX - 1)
  378. struct unix_gid {
  379. struct cache_head h;
  380. uid_t uid;
  381. struct group_info *gi;
  382. };
  383. static struct cache_head *gid_table[GID_HASHMAX];
  384. static void unix_gid_put(struct kref *kref)
  385. {
  386. struct cache_head *item = container_of(kref, struct cache_head, ref);
  387. struct unix_gid *ug = container_of(item, struct unix_gid, h);
  388. if (test_bit(CACHE_VALID, &item->flags) &&
  389. !test_bit(CACHE_NEGATIVE, &item->flags))
  390. put_group_info(ug->gi);
  391. kfree(ug);
  392. }
  393. static int unix_gid_match(struct cache_head *corig, struct cache_head *cnew)
  394. {
  395. struct unix_gid *orig = container_of(corig, struct unix_gid, h);
  396. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  397. return orig->uid == new->uid;
  398. }
  399. static void unix_gid_init(struct cache_head *cnew, struct cache_head *citem)
  400. {
  401. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  402. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  403. new->uid = item->uid;
  404. }
  405. static void unix_gid_update(struct cache_head *cnew, struct cache_head *citem)
  406. {
  407. struct unix_gid *new = container_of(cnew, struct unix_gid, h);
  408. struct unix_gid *item = container_of(citem, struct unix_gid, h);
  409. get_group_info(item->gi);
  410. new->gi = item->gi;
  411. }
  412. static struct cache_head *unix_gid_alloc(void)
  413. {
  414. struct unix_gid *g = kmalloc(sizeof(*g), GFP_KERNEL);
  415. if (g)
  416. return &g->h;
  417. else
  418. return NULL;
  419. }
  420. static void unix_gid_request(struct cache_detail *cd,
  421. struct cache_head *h,
  422. char **bpp, int *blen)
  423. {
  424. char tuid[20];
  425. struct unix_gid *ug = container_of(h, struct unix_gid, h);
  426. snprintf(tuid, 20, "%u", ug->uid);
  427. qword_add(bpp, blen, tuid);
  428. (*bpp)[-1] = '\n';
  429. }
  430. static struct unix_gid *unix_gid_lookup(uid_t uid);
  431. extern struct cache_detail unix_gid_cache;
  432. static int unix_gid_parse(struct cache_detail *cd,
  433. char *mesg, int mlen)
  434. {
  435. /* uid expiry Ngid gid0 gid1 ... gidN-1 */
  436. int uid;
  437. int gids;
  438. int rv;
  439. int i;
  440. int err;
  441. time_t expiry;
  442. struct unix_gid ug, *ugp;
  443. if (mlen <= 0 || mesg[mlen-1] != '\n')
  444. return -EINVAL;
  445. mesg[mlen-1] = 0;
  446. rv = get_int(&mesg, &uid);
  447. if (rv)
  448. return -EINVAL;
  449. ug.uid = uid;
  450. expiry = get_expiry(&mesg);
  451. if (expiry == 0)
  452. return -EINVAL;
  453. rv = get_int(&mesg, &gids);
  454. if (rv || gids < 0 || gids > 8192)
  455. return -EINVAL;
  456. ug.gi = groups_alloc(gids);
  457. if (!ug.gi)
  458. return -ENOMEM;
  459. for (i = 0 ; i < gids ; i++) {
  460. int gid;
  461. rv = get_int(&mesg, &gid);
  462. err = -EINVAL;
  463. if (rv)
  464. goto out;
  465. GROUP_AT(ug.gi, i) = gid;
  466. }
  467. ugp = unix_gid_lookup(uid);
  468. if (ugp) {
  469. struct cache_head *ch;
  470. ug.h.flags = 0;
  471. ug.h.expiry_time = expiry;
  472. ch = sunrpc_cache_update(&unix_gid_cache,
  473. &ug.h, &ugp->h,
  474. hash_long(uid, GID_HASHBITS));
  475. if (!ch)
  476. err = -ENOMEM;
  477. else {
  478. err = 0;
  479. cache_put(ch, &unix_gid_cache);
  480. }
  481. } else
  482. err = -ENOMEM;
  483. out:
  484. if (ug.gi)
  485. put_group_info(ug.gi);
  486. return err;
  487. }
  488. static int unix_gid_show(struct seq_file *m,
  489. struct cache_detail *cd,
  490. struct cache_head *h)
  491. {
  492. struct unix_gid *ug;
  493. int i;
  494. int glen;
  495. if (h == NULL) {
  496. seq_puts(m, "#uid cnt: gids...\n");
  497. return 0;
  498. }
  499. ug = container_of(h, struct unix_gid, h);
  500. if (test_bit(CACHE_VALID, &h->flags) &&
  501. !test_bit(CACHE_NEGATIVE, &h->flags))
  502. glen = ug->gi->ngroups;
  503. else
  504. glen = 0;
  505. seq_printf(m, "%d %d:", ug->uid, glen);
  506. for (i = 0; i < glen; i++)
  507. seq_printf(m, " %d", GROUP_AT(ug->gi, i));
  508. seq_printf(m, "\n");
  509. return 0;
  510. }
  511. struct cache_detail unix_gid_cache = {
  512. .owner = THIS_MODULE,
  513. .hash_size = GID_HASHMAX,
  514. .hash_table = gid_table,
  515. .name = "auth.unix.gid",
  516. .cache_put = unix_gid_put,
  517. .cache_request = unix_gid_request,
  518. .cache_parse = unix_gid_parse,
  519. .cache_show = unix_gid_show,
  520. .match = unix_gid_match,
  521. .init = unix_gid_init,
  522. .update = unix_gid_update,
  523. .alloc = unix_gid_alloc,
  524. };
  525. static struct unix_gid *unix_gid_lookup(uid_t uid)
  526. {
  527. struct unix_gid ug;
  528. struct cache_head *ch;
  529. ug.uid = uid;
  530. ch = sunrpc_cache_lookup(&unix_gid_cache, &ug.h,
  531. hash_long(uid, GID_HASHBITS));
  532. if (ch)
  533. return container_of(ch, struct unix_gid, h);
  534. else
  535. return NULL;
  536. }
  537. static int unix_gid_find(uid_t uid, struct group_info **gip,
  538. struct svc_rqst *rqstp)
  539. {
  540. struct unix_gid *ug = unix_gid_lookup(uid);
  541. if (!ug)
  542. return -EAGAIN;
  543. switch (cache_check(&unix_gid_cache, &ug->h, &rqstp->rq_chandle)) {
  544. case -ENOENT:
  545. *gip = NULL;
  546. return 0;
  547. case 0:
  548. *gip = ug->gi;
  549. get_group_info(*gip);
  550. return 0;
  551. default:
  552. return -EAGAIN;
  553. }
  554. }
  555. int
  556. svcauth_unix_set_client(struct svc_rqst *rqstp)
  557. {
  558. struct sockaddr_in *sin = svc_addr_in(rqstp);
  559. struct ip_map *ipm;
  560. rqstp->rq_client = NULL;
  561. if (rqstp->rq_proc == 0)
  562. return SVC_OK;
  563. ipm = ip_map_cached_get(rqstp);
  564. if (ipm == NULL)
  565. ipm = ip_map_lookup(rqstp->rq_server->sv_program->pg_class,
  566. sin->sin_addr);
  567. if (ipm == NULL)
  568. return SVC_DENIED;
  569. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  570. default:
  571. BUG();
  572. case -EAGAIN:
  573. case -ETIMEDOUT:
  574. return SVC_DROP;
  575. case -ENOENT:
  576. return SVC_DENIED;
  577. case 0:
  578. rqstp->rq_client = &ipm->m_client->h;
  579. kref_get(&rqstp->rq_client->ref);
  580. ip_map_cached_put(rqstp, ipm);
  581. break;
  582. }
  583. return SVC_OK;
  584. }
  585. EXPORT_SYMBOL(svcauth_unix_set_client);
  586. static int
  587. svcauth_null_accept(struct svc_rqst *rqstp, __be32 *authp)
  588. {
  589. struct kvec *argv = &rqstp->rq_arg.head[0];
  590. struct kvec *resv = &rqstp->rq_res.head[0];
  591. struct svc_cred *cred = &rqstp->rq_cred;
  592. cred->cr_group_info = NULL;
  593. rqstp->rq_client = NULL;
  594. if (argv->iov_len < 3*4)
  595. return SVC_GARBAGE;
  596. if (svc_getu32(argv) != 0) {
  597. dprintk("svc: bad null cred\n");
  598. *authp = rpc_autherr_badcred;
  599. return SVC_DENIED;
  600. }
  601. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  602. dprintk("svc: bad null verf\n");
  603. *authp = rpc_autherr_badverf;
  604. return SVC_DENIED;
  605. }
  606. /* Signal that mapping to nobody uid/gid is required */
  607. cred->cr_uid = (uid_t) -1;
  608. cred->cr_gid = (gid_t) -1;
  609. cred->cr_group_info = groups_alloc(0);
  610. if (cred->cr_group_info == NULL)
  611. return SVC_DROP; /* kmalloc failure - client must retry */
  612. /* Put NULL verifier */
  613. svc_putnl(resv, RPC_AUTH_NULL);
  614. svc_putnl(resv, 0);
  615. rqstp->rq_flavor = RPC_AUTH_NULL;
  616. return SVC_OK;
  617. }
  618. static int
  619. svcauth_null_release(struct svc_rqst *rqstp)
  620. {
  621. if (rqstp->rq_client)
  622. auth_domain_put(rqstp->rq_client);
  623. rqstp->rq_client = NULL;
  624. if (rqstp->rq_cred.cr_group_info)
  625. put_group_info(rqstp->rq_cred.cr_group_info);
  626. rqstp->rq_cred.cr_group_info = NULL;
  627. return 0; /* don't drop */
  628. }
  629. struct auth_ops svcauth_null = {
  630. .name = "null",
  631. .owner = THIS_MODULE,
  632. .flavour = RPC_AUTH_NULL,
  633. .accept = svcauth_null_accept,
  634. .release = svcauth_null_release,
  635. .set_client = svcauth_unix_set_client,
  636. };
  637. static int
  638. svcauth_unix_accept(struct svc_rqst *rqstp, __be32 *authp)
  639. {
  640. struct kvec *argv = &rqstp->rq_arg.head[0];
  641. struct kvec *resv = &rqstp->rq_res.head[0];
  642. struct svc_cred *cred = &rqstp->rq_cred;
  643. u32 slen, i;
  644. int len = argv->iov_len;
  645. cred->cr_group_info = NULL;
  646. rqstp->rq_client = NULL;
  647. if ((len -= 3*4) < 0)
  648. return SVC_GARBAGE;
  649. svc_getu32(argv); /* length */
  650. svc_getu32(argv); /* time stamp */
  651. slen = XDR_QUADLEN(svc_getnl(argv)); /* machname length */
  652. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  653. goto badcred;
  654. argv->iov_base = (void*)((__be32*)argv->iov_base + slen); /* skip machname */
  655. argv->iov_len -= slen*4;
  656. cred->cr_uid = svc_getnl(argv); /* uid */
  657. cred->cr_gid = svc_getnl(argv); /* gid */
  658. slen = svc_getnl(argv); /* gids length */
  659. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  660. goto badcred;
  661. if (unix_gid_find(cred->cr_uid, &cred->cr_group_info, rqstp)
  662. == -EAGAIN)
  663. return SVC_DROP;
  664. if (cred->cr_group_info == NULL) {
  665. cred->cr_group_info = groups_alloc(slen);
  666. if (cred->cr_group_info == NULL)
  667. return SVC_DROP;
  668. for (i = 0; i < slen; i++)
  669. GROUP_AT(cred->cr_group_info, i) = svc_getnl(argv);
  670. } else {
  671. for (i = 0; i < slen ; i++)
  672. svc_getnl(argv);
  673. }
  674. if (svc_getu32(argv) != htonl(RPC_AUTH_NULL) || svc_getu32(argv) != 0) {
  675. *authp = rpc_autherr_badverf;
  676. return SVC_DENIED;
  677. }
  678. /* Put NULL verifier */
  679. svc_putnl(resv, RPC_AUTH_NULL);
  680. svc_putnl(resv, 0);
  681. rqstp->rq_flavor = RPC_AUTH_UNIX;
  682. return SVC_OK;
  683. badcred:
  684. *authp = rpc_autherr_badcred;
  685. return SVC_DENIED;
  686. }
  687. static int
  688. svcauth_unix_release(struct svc_rqst *rqstp)
  689. {
  690. /* Verifier (such as it is) is already in place.
  691. */
  692. if (rqstp->rq_client)
  693. auth_domain_put(rqstp->rq_client);
  694. rqstp->rq_client = NULL;
  695. if (rqstp->rq_cred.cr_group_info)
  696. put_group_info(rqstp->rq_cred.cr_group_info);
  697. rqstp->rq_cred.cr_group_info = NULL;
  698. return 0;
  699. }
  700. struct auth_ops svcauth_unix = {
  701. .name = "unix",
  702. .owner = THIS_MODULE,
  703. .flavour = RPC_AUTH_UNIX,
  704. .accept = svcauth_unix_accept,
  705. .release = svcauth_unix_release,
  706. .domain_release = svcauth_unix_domain_release,
  707. .set_client = svcauth_unix_set_client,
  708. };