svcauth_unix.c 11 KB

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