dns_resolve.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * fs/cifs/dns_resolve.c
  3. *
  4. * Copyright (c) 2007 Igor Mammedov
  5. * Author(s): Igor Mammedov (niallain@gmail.com)
  6. * Steve French (sfrench@us.ibm.com)
  7. *
  8. * Contains the CIFS DFS upcall routines used for hostname to
  9. * IP address translation.
  10. *
  11. * This library is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU Lesser General Public License as published
  13. * by the Free Software Foundation; either version 2.1 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This library is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  19. * the GNU Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public License
  22. * along with this library; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #include <keys/user-type.h>
  26. #include "dns_resolve.h"
  27. #include "cifsglob.h"
  28. #include "cifsproto.h"
  29. #include "cifs_debug.h"
  30. static int dns_resolver_instantiate(struct key *key, const void *data,
  31. size_t datalen)
  32. {
  33. int rc = 0;
  34. char *ip;
  35. ip = kmalloc(datalen+1, GFP_KERNEL);
  36. if (!ip)
  37. return -ENOMEM;
  38. memcpy(ip, data, datalen);
  39. ip[datalen] = '\0';
  40. rcu_assign_pointer(key->payload.data, ip);
  41. return rc;
  42. }
  43. struct key_type key_type_dns_resolver = {
  44. .name = "dns_resolver",
  45. .def_datalen = sizeof(struct in_addr),
  46. .describe = user_describe,
  47. .instantiate = dns_resolver_instantiate,
  48. .match = user_match,
  49. };
  50. /* Resolves server name to ip address.
  51. * input:
  52. * unc - server UNC
  53. * output:
  54. * *ip_addr - pointer to server ip, caller responcible for freeing it.
  55. * return 0 on success
  56. */
  57. int
  58. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  59. {
  60. int rc = -EAGAIN;
  61. struct key *rkey;
  62. char *name;
  63. int len;
  64. if (!ip_addr || !unc)
  65. return -EINVAL;
  66. /* search for server name delimiter */
  67. len = strlen(unc);
  68. if (len < 3) {
  69. cFYI(1, ("%s: unc is too short: %s", __func__, unc));
  70. return -EINVAL;
  71. }
  72. len -= 2;
  73. name = memchr(unc+2, '\\', len);
  74. if (!name) {
  75. cFYI(1, ("%s: probably server name is whole unc: %s",
  76. __func__, unc));
  77. } else {
  78. len = (name - unc) - 2/* leading // */;
  79. }
  80. name = kmalloc(len+1, GFP_KERNEL);
  81. if (!name) {
  82. rc = -ENOMEM;
  83. return rc;
  84. }
  85. memcpy(name, unc+2, len);
  86. name[len] = 0;
  87. rkey = request_key(&key_type_dns_resolver, name, "");
  88. if (!IS_ERR(rkey)) {
  89. len = strlen(rkey->payload.data);
  90. *ip_addr = kmalloc(len+1, GFP_KERNEL);
  91. if (*ip_addr) {
  92. memcpy(*ip_addr, rkey->payload.data, len);
  93. (*ip_addr)[len] = '\0';
  94. cFYI(1, ("%s: resolved: %s to %s", __func__,
  95. rkey->description,
  96. *ip_addr
  97. ));
  98. rc = 0;
  99. } else {
  100. rc = -ENOMEM;
  101. }
  102. key_put(rkey);
  103. } else {
  104. cERROR(1, ("%s: unable to resolve: %s", __func__, name));
  105. }
  106. kfree(name);
  107. return rc;
  108. }