svcauth_unix.c 18 KB

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