svcauth_unix.c 11 KB

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