svcauth_unix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. #define RPCDBG_FACILITY RPCDBG_AUTH
  13. /*
  14. * AUTHUNIX and AUTHNULL credentials are both handled here.
  15. * AUTHNULL is treated just like AUTHUNIX except that the uid/gid
  16. * are always nobody (-2). i.e. we do the same IP address checks for
  17. * AUTHNULL as for AUTHUNIX, and that is done here.
  18. */
  19. struct unix_domain {
  20. struct auth_domain h;
  21. int addr_changes;
  22. /* other stuff later */
  23. };
  24. struct auth_domain *unix_domain_find(char *name)
  25. {
  26. struct auth_domain *rv, ud;
  27. struct unix_domain *new;
  28. ud.name = name;
  29. rv = auth_domain_lookup(&ud, 0);
  30. foundit:
  31. if (rv && rv->flavour != RPC_AUTH_UNIX) {
  32. auth_domain_put(rv);
  33. return NULL;
  34. }
  35. if (rv)
  36. return rv;
  37. new = kmalloc(sizeof(*new), GFP_KERNEL);
  38. if (new == NULL)
  39. return NULL;
  40. cache_init(&new->h.h);
  41. new->h.name = kstrdup(name, GFP_KERNEL);
  42. new->h.flavour = RPC_AUTH_UNIX;
  43. new->addr_changes = 0;
  44. new->h.h.expiry_time = NEVER;
  45. rv = auth_domain_lookup(&new->h, 2);
  46. if (rv == &new->h) {
  47. if (atomic_dec_and_test(&new->h.h.refcnt)) BUG();
  48. } else {
  49. auth_domain_put(&new->h);
  50. goto foundit;
  51. }
  52. return rv;
  53. }
  54. static void svcauth_unix_domain_release(struct auth_domain *dom)
  55. {
  56. struct unix_domain *ud = container_of(dom, struct unix_domain, h);
  57. kfree(dom->name);
  58. kfree(ud);
  59. }
  60. /**************************************************
  61. * cache for IP address to unix_domain
  62. * as needed by AUTH_UNIX
  63. */
  64. #define IP_HASHBITS 8
  65. #define IP_HASHMAX (1<<IP_HASHBITS)
  66. #define IP_HASHMASK (IP_HASHMAX-1)
  67. struct ip_map {
  68. struct cache_head h;
  69. char m_class[8]; /* e.g. "nfsd" */
  70. struct in_addr m_addr;
  71. struct unix_domain *m_client;
  72. int m_add_change;
  73. };
  74. static struct cache_head *ip_table[IP_HASHMAX];
  75. static void ip_map_put(struct cache_head *item, struct cache_detail *cd)
  76. {
  77. struct ip_map *im = container_of(item, struct ip_map,h);
  78. if (cache_put(item, cd)) {
  79. if (test_bit(CACHE_VALID, &item->flags) &&
  80. !test_bit(CACHE_NEGATIVE, &item->flags))
  81. auth_domain_put(&im->m_client->h);
  82. kfree(im);
  83. }
  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(unsigned long ip)
  91. {
  92. int hash = ip ^ (ip>>16);
  93. return (hash ^ (hash>>8)) & 0xff;
  94. }
  95. #endif
  96. static inline int ip_map_hash(struct ip_map *item)
  97. {
  98. return hash_str(item->m_class, IP_HASHBITS) ^
  99. hash_ip((unsigned long)item->m_addr.s_addr);
  100. }
  101. static inline int ip_map_match(struct ip_map *item, struct ip_map *tmp)
  102. {
  103. return strcmp(tmp->m_class, item->m_class) == 0
  104. && tmp->m_addr.s_addr == item->m_addr.s_addr;
  105. }
  106. static inline void ip_map_init(struct ip_map *new, struct ip_map *item)
  107. {
  108. strcpy(new->m_class, item->m_class);
  109. new->m_addr.s_addr = item->m_addr.s_addr;
  110. }
  111. static inline void ip_map_update(struct ip_map *new, struct ip_map *item)
  112. {
  113. cache_get(&item->m_client->h.h);
  114. new->m_client = item->m_client;
  115. new->m_add_change = item->m_add_change;
  116. }
  117. static void ip_map_request(struct cache_detail *cd,
  118. struct cache_head *h,
  119. char **bpp, int *blen)
  120. {
  121. char text_addr[20];
  122. struct ip_map *im = container_of(h, struct ip_map, h);
  123. __u32 addr = im->m_addr.s_addr;
  124. snprintf(text_addr, 20, "%u.%u.%u.%u",
  125. ntohl(addr) >> 24 & 0xff,
  126. ntohl(addr) >> 16 & 0xff,
  127. ntohl(addr) >> 8 & 0xff,
  128. ntohl(addr) >> 0 & 0xff);
  129. qword_add(bpp, blen, im->m_class);
  130. qword_add(bpp, blen, text_addr);
  131. (*bpp)[-1] = '\n';
  132. }
  133. static struct ip_map *ip_map_lookup(struct ip_map *, int);
  134. static int ip_map_parse(struct cache_detail *cd,
  135. char *mesg, int mlen)
  136. {
  137. /* class ipaddress [domainname] */
  138. /* should be safe just to use the start of the input buffer
  139. * for scratch: */
  140. char *buf = mesg;
  141. int len;
  142. int b1,b2,b3,b4;
  143. char c;
  144. struct ip_map ipm, *ipmp;
  145. struct auth_domain *dom;
  146. time_t expiry;
  147. if (mesg[mlen-1] != '\n')
  148. return -EINVAL;
  149. mesg[mlen-1] = 0;
  150. /* class */
  151. len = qword_get(&mesg, ipm.m_class, sizeof(ipm.m_class));
  152. if (len <= 0) return -EINVAL;
  153. /* ip address */
  154. len = qword_get(&mesg, buf, mlen);
  155. if (len <= 0) return -EINVAL;
  156. if (sscanf(buf, "%u.%u.%u.%u%c", &b1, &b2, &b3, &b4, &c) != 4)
  157. return -EINVAL;
  158. expiry = get_expiry(&mesg);
  159. if (expiry ==0)
  160. return -EINVAL;
  161. /* domainname, or empty for NEGATIVE */
  162. len = qword_get(&mesg, buf, mlen);
  163. if (len < 0) return -EINVAL;
  164. if (len) {
  165. dom = unix_domain_find(buf);
  166. if (dom == NULL)
  167. return -ENOENT;
  168. } else
  169. dom = NULL;
  170. ipm.m_addr.s_addr =
  171. htonl((((((b1<<8)|b2)<<8)|b3)<<8)|b4);
  172. ipm.h.flags = 0;
  173. if (dom) {
  174. ipm.m_client = container_of(dom, struct unix_domain, h);
  175. ipm.m_add_change = ipm.m_client->addr_changes;
  176. } else
  177. set_bit(CACHE_NEGATIVE, &ipm.h.flags);
  178. ipm.h.expiry_time = expiry;
  179. ipmp = ip_map_lookup(&ipm, 1);
  180. if (ipmp)
  181. ip_map_put(&ipmp->h, &ip_map_cache);
  182. if (dom)
  183. auth_domain_put(dom);
  184. if (!ipmp)
  185. return -ENOMEM;
  186. cache_flush();
  187. return 0;
  188. }
  189. static int ip_map_show(struct seq_file *m,
  190. struct cache_detail *cd,
  191. struct cache_head *h)
  192. {
  193. struct ip_map *im;
  194. struct in_addr addr;
  195. char *dom = "-no-domain-";
  196. if (h == NULL) {
  197. seq_puts(m, "#class IP domain\n");
  198. return 0;
  199. }
  200. im = container_of(h, struct ip_map, h);
  201. /* class addr domain */
  202. addr = im->m_addr;
  203. if (test_bit(CACHE_VALID, &h->flags) &&
  204. !test_bit(CACHE_NEGATIVE, &h->flags))
  205. dom = im->m_client->h.name;
  206. seq_printf(m, "%s %d.%d.%d.%d %s\n",
  207. im->m_class,
  208. htonl(addr.s_addr) >> 24 & 0xff,
  209. htonl(addr.s_addr) >> 16 & 0xff,
  210. htonl(addr.s_addr) >> 8 & 0xff,
  211. htonl(addr.s_addr) >> 0 & 0xff,
  212. dom
  213. );
  214. return 0;
  215. }
  216. struct cache_detail ip_map_cache = {
  217. .owner = THIS_MODULE,
  218. .hash_size = IP_HASHMAX,
  219. .hash_table = ip_table,
  220. .name = "auth.unix.ip",
  221. .cache_put = ip_map_put,
  222. .cache_request = ip_map_request,
  223. .cache_parse = ip_map_parse,
  224. .cache_show = ip_map_show,
  225. };
  226. static DefineSimpleCacheLookup(ip_map, 0)
  227. int auth_unix_add_addr(struct in_addr addr, struct auth_domain *dom)
  228. {
  229. struct unix_domain *udom;
  230. struct ip_map ip, *ipmp;
  231. if (dom->flavour != RPC_AUTH_UNIX)
  232. return -EINVAL;
  233. udom = container_of(dom, struct unix_domain, h);
  234. strcpy(ip.m_class, "nfsd");
  235. ip.m_addr = addr;
  236. ip.m_client = udom;
  237. ip.m_add_change = udom->addr_changes+1;
  238. ip.h.flags = 0;
  239. ip.h.expiry_time = NEVER;
  240. ipmp = ip_map_lookup(&ip, 1);
  241. if (ipmp) {
  242. ip_map_put(&ipmp->h, &ip_map_cache);
  243. return 0;
  244. } else
  245. return -ENOMEM;
  246. }
  247. int auth_unix_forget_old(struct auth_domain *dom)
  248. {
  249. struct unix_domain *udom;
  250. if (dom->flavour != RPC_AUTH_UNIX)
  251. return -EINVAL;
  252. udom = container_of(dom, struct unix_domain, h);
  253. udom->addr_changes++;
  254. return 0;
  255. }
  256. struct auth_domain *auth_unix_lookup(struct in_addr addr)
  257. {
  258. struct ip_map key, *ipm;
  259. struct auth_domain *rv;
  260. strcpy(key.m_class, "nfsd");
  261. key.m_addr = addr;
  262. ipm = ip_map_lookup(&key, 0);
  263. if (!ipm)
  264. return NULL;
  265. if (cache_check(&ip_map_cache, &ipm->h, NULL))
  266. return NULL;
  267. if ((ipm->m_client->addr_changes - ipm->m_add_change) >0) {
  268. if (test_and_set_bit(CACHE_NEGATIVE, &ipm->h.flags) == 0)
  269. auth_domain_put(&ipm->m_client->h);
  270. rv = NULL;
  271. } else {
  272. rv = &ipm->m_client->h;
  273. cache_get(&rv->h);
  274. }
  275. ip_map_put(&ipm->h, &ip_map_cache);
  276. return rv;
  277. }
  278. void svcauth_unix_purge(void)
  279. {
  280. cache_purge(&ip_map_cache);
  281. cache_purge(&auth_domain_cache);
  282. }
  283. static int
  284. svcauth_unix_set_client(struct svc_rqst *rqstp)
  285. {
  286. struct ip_map key, *ipm;
  287. rqstp->rq_client = NULL;
  288. if (rqstp->rq_proc == 0)
  289. return SVC_OK;
  290. strcpy(key.m_class, rqstp->rq_server->sv_program->pg_class);
  291. key.m_addr = rqstp->rq_addr.sin_addr;
  292. ipm = ip_map_lookup(&key, 0);
  293. if (ipm == NULL)
  294. return SVC_DENIED;
  295. switch (cache_check(&ip_map_cache, &ipm->h, &rqstp->rq_chandle)) {
  296. default:
  297. BUG();
  298. case -EAGAIN:
  299. return SVC_DROP;
  300. case -ENOENT:
  301. return SVC_DENIED;
  302. case 0:
  303. rqstp->rq_client = &ipm->m_client->h;
  304. cache_get(&rqstp->rq_client->h);
  305. ip_map_put(&ipm->h, &ip_map_cache);
  306. break;
  307. }
  308. return SVC_OK;
  309. }
  310. static int
  311. svcauth_null_accept(struct svc_rqst *rqstp, u32 *authp)
  312. {
  313. struct kvec *argv = &rqstp->rq_arg.head[0];
  314. struct kvec *resv = &rqstp->rq_res.head[0];
  315. struct svc_cred *cred = &rqstp->rq_cred;
  316. cred->cr_group_info = NULL;
  317. rqstp->rq_client = NULL;
  318. if (argv->iov_len < 3*4)
  319. return SVC_GARBAGE;
  320. if (svc_getu32(argv) != 0) {
  321. dprintk("svc: bad null cred\n");
  322. *authp = rpc_autherr_badcred;
  323. return SVC_DENIED;
  324. }
  325. if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
  326. dprintk("svc: bad null verf\n");
  327. *authp = rpc_autherr_badverf;
  328. return SVC_DENIED;
  329. }
  330. /* Signal that mapping to nobody uid/gid is required */
  331. cred->cr_uid = (uid_t) -1;
  332. cred->cr_gid = (gid_t) -1;
  333. cred->cr_group_info = groups_alloc(0);
  334. if (cred->cr_group_info == NULL)
  335. return SVC_DROP; /* kmalloc failure - client must retry */
  336. /* Put NULL verifier */
  337. svc_putu32(resv, RPC_AUTH_NULL);
  338. svc_putu32(resv, 0);
  339. return SVC_OK;
  340. }
  341. static int
  342. svcauth_null_release(struct svc_rqst *rqstp)
  343. {
  344. if (rqstp->rq_client)
  345. auth_domain_put(rqstp->rq_client);
  346. rqstp->rq_client = NULL;
  347. if (rqstp->rq_cred.cr_group_info)
  348. put_group_info(rqstp->rq_cred.cr_group_info);
  349. rqstp->rq_cred.cr_group_info = NULL;
  350. return 0; /* don't drop */
  351. }
  352. struct auth_ops svcauth_null = {
  353. .name = "null",
  354. .owner = THIS_MODULE,
  355. .flavour = RPC_AUTH_NULL,
  356. .accept = svcauth_null_accept,
  357. .release = svcauth_null_release,
  358. .set_client = svcauth_unix_set_client,
  359. };
  360. static int
  361. svcauth_unix_accept(struct svc_rqst *rqstp, u32 *authp)
  362. {
  363. struct kvec *argv = &rqstp->rq_arg.head[0];
  364. struct kvec *resv = &rqstp->rq_res.head[0];
  365. struct svc_cred *cred = &rqstp->rq_cred;
  366. u32 slen, i;
  367. int len = argv->iov_len;
  368. cred->cr_group_info = NULL;
  369. rqstp->rq_client = NULL;
  370. if ((len -= 3*4) < 0)
  371. return SVC_GARBAGE;
  372. svc_getu32(argv); /* length */
  373. svc_getu32(argv); /* time stamp */
  374. slen = XDR_QUADLEN(ntohl(svc_getu32(argv))); /* machname length */
  375. if (slen > 64 || (len -= (slen + 3)*4) < 0)
  376. goto badcred;
  377. argv->iov_base = (void*)((u32*)argv->iov_base + slen); /* skip machname */
  378. argv->iov_len -= slen*4;
  379. cred->cr_uid = ntohl(svc_getu32(argv)); /* uid */
  380. cred->cr_gid = ntohl(svc_getu32(argv)); /* gid */
  381. slen = ntohl(svc_getu32(argv)); /* gids length */
  382. if (slen > 16 || (len -= (slen + 2)*4) < 0)
  383. goto badcred;
  384. cred->cr_group_info = groups_alloc(slen);
  385. if (cred->cr_group_info == NULL)
  386. return SVC_DROP;
  387. for (i = 0; i < slen; i++)
  388. GROUP_AT(cred->cr_group_info, i) = ntohl(svc_getu32(argv));
  389. if (svc_getu32(argv) != RPC_AUTH_NULL || svc_getu32(argv) != 0) {
  390. *authp = rpc_autherr_badverf;
  391. return SVC_DENIED;
  392. }
  393. /* Put NULL verifier */
  394. svc_putu32(resv, RPC_AUTH_NULL);
  395. svc_putu32(resv, 0);
  396. return SVC_OK;
  397. badcred:
  398. *authp = rpc_autherr_badcred;
  399. return SVC_DENIED;
  400. }
  401. static int
  402. svcauth_unix_release(struct svc_rqst *rqstp)
  403. {
  404. /* Verifier (such as it is) is already in place.
  405. */
  406. if (rqstp->rq_client)
  407. auth_domain_put(rqstp->rq_client);
  408. rqstp->rq_client = NULL;
  409. if (rqstp->rq_cred.cr_group_info)
  410. put_group_info(rqstp->rq_cred.cr_group_info);
  411. rqstp->rq_cred.cr_group_info = NULL;
  412. return 0;
  413. }
  414. struct auth_ops svcauth_unix = {
  415. .name = "unix",
  416. .owner = THIS_MODULE,
  417. .flavour = RPC_AUTH_UNIX,
  418. .accept = svcauth_unix_accept,
  419. .release = svcauth_unix_release,
  420. .domain_release = svcauth_unix_domain_release,
  421. .set_client = svcauth_unix_set_client,
  422. };