dns_resolve.c 3.7 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. 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. /* Checks if supplied name is IP address
  51. * returns:
  52. * 1 - name is IP
  53. * 0 - name is not IP
  54. */
  55. static int is_ip(const char *name)
  56. {
  57. int rc;
  58. struct sockaddr_in sin_server;
  59. struct sockaddr_in6 sin_server6;
  60. rc = cifs_inet_pton(AF_INET, name,
  61. &sin_server.sin_addr.s_addr);
  62. if (rc <= 0) {
  63. /* not ipv4 address, try ipv6 */
  64. rc = cifs_inet_pton(AF_INET6, name,
  65. &sin_server6.sin6_addr.in6_u);
  66. if (rc > 0)
  67. return 1;
  68. } else {
  69. return 1;
  70. }
  71. /* we failed translating address */
  72. return 0;
  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. data = rkey->payload.data;
  121. } else {
  122. cERROR(1, ("%s: unable to resolve: %s", __func__, name));
  123. goto out;
  124. }
  125. skip_upcall:
  126. if (data) {
  127. len = strlen(data);
  128. *ip_addr = kmalloc(len+1, GFP_KERNEL);
  129. if (*ip_addr) {
  130. memcpy(*ip_addr, data, len);
  131. (*ip_addr)[len] = '\0';
  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. }