svcauth_unix.c 11 KB

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