dns_resolve.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. /* Checks if supplied name is IP address
  31. * returns:
  32. * 1 - name is IP
  33. * 0 - name is not IP
  34. */
  35. static int
  36. is_ip(char *name)
  37. {
  38. struct sockaddr_storage ss;
  39. return cifs_convert_address(name, &ss);
  40. }
  41. static int
  42. dns_resolver_instantiate(struct key *key, const void *data,
  43. size_t datalen)
  44. {
  45. int rc = 0;
  46. char *ip;
  47. ip = kmalloc(datalen + 1, GFP_KERNEL);
  48. if (!ip)
  49. return -ENOMEM;
  50. memcpy(ip, data, datalen);
  51. ip[datalen] = '\0';
  52. /* make sure this looks like an address */
  53. if (!is_ip(ip)) {
  54. kfree(ip);
  55. return -EINVAL;
  56. }
  57. key->type_data.x[0] = datalen;
  58. key->payload.data = ip;
  59. return rc;
  60. }
  61. static void
  62. dns_resolver_destroy(struct key *key)
  63. {
  64. kfree(key->payload.data);
  65. }
  66. struct key_type key_type_dns_resolver = {
  67. .name = "dns_resolver",
  68. .def_datalen = sizeof(struct in_addr),
  69. .describe = user_describe,
  70. .instantiate = dns_resolver_instantiate,
  71. .destroy = dns_resolver_destroy,
  72. .match = user_match,
  73. };
  74. /* Resolves server name to ip address.
  75. * input:
  76. * unc - server UNC
  77. * output:
  78. * *ip_addr - pointer to server ip, caller responcible for freeing it.
  79. * return 0 on success
  80. */
  81. int
  82. dns_resolve_server_name_to_ip(const char *unc, char **ip_addr)
  83. {
  84. int rc = -EAGAIN;
  85. struct key *rkey = ERR_PTR(-EAGAIN);
  86. char *name;
  87. char *data = NULL;
  88. int len;
  89. if (!ip_addr || !unc)
  90. return -EINVAL;
  91. /* search for server name delimiter */
  92. len = strlen(unc);
  93. if (len < 3) {
  94. cFYI(1, ("%s: unc is too short: %s", __func__, unc));
  95. return -EINVAL;
  96. }
  97. len -= 2;
  98. name = memchr(unc+2, '\\', len);
  99. if (!name) {
  100. cFYI(1, ("%s: probably server name is whole unc: %s",
  101. __func__, unc));
  102. } else {
  103. len = (name - unc) - 2/* leading // */;
  104. }
  105. name = kmalloc(len+1, GFP_KERNEL);
  106. if (!name) {
  107. rc = -ENOMEM;
  108. return rc;
  109. }
  110. memcpy(name, unc+2, len);
  111. name[len] = 0;
  112. if (is_ip(name)) {
  113. cFYI(1, ("%s: it is IP, skipping dns upcall: %s",
  114. __func__, name));
  115. data = name;
  116. goto skip_upcall;
  117. }
  118. rkey = request_key(&key_type_dns_resolver, name, "");
  119. if (!IS_ERR(rkey)) {
  120. len = rkey->type_data.x[0];
  121. data = rkey->payload.data;
  122. } else {
  123. cERROR(1, ("%s: unable to resolve: %s", __func__, name));
  124. goto out;
  125. }
  126. skip_upcall:
  127. if (data) {
  128. *ip_addr = kmalloc(len + 1, GFP_KERNEL);
  129. if (*ip_addr) {
  130. memcpy(*ip_addr, data, len + 1);
  131. if (!IS_ERR(rkey))
  132. cFYI(1, ("%s: resolved: %s to %s", __func__,
  133. name,
  134. *ip_addr
  135. ));
  136. rc = 0;
  137. } else {
  138. rc = -ENOMEM;
  139. }
  140. if (!IS_ERR(rkey))
  141. key_put(rkey);
  142. }
  143. out:
  144. kfree(name);
  145. return rc;
  146. }