dns_resolve.c 3.6 KB

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