/libcli/nbt/namequery.c

https://gitlab.com/miztake/samba · C · 234 lines · 154 code · 42 blank · 38 comment · 38 complexity · 9890bc30965751257326c4ef87f5c4a2 MD5 · raw file

  1. /*
  2. Unix SMB/CIFS implementation.
  3. make nbt name query requests
  4. Copyright (C) Andrew Tridgell 2005
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include "includes.h"
  17. #include "../libcli/nbt/libnbt.h"
  18. #include "../libcli/nbt/nbt_proto.h"
  19. #include "lib/socket/socket.h"
  20. /**
  21. send a nbt name query
  22. */
  23. _PUBLIC_ struct nbt_name_request *nbt_name_query_send(struct nbt_name_socket *nbtsock,
  24. struct nbt_name_query *io)
  25. {
  26. struct nbt_name_request *req;
  27. struct nbt_name_packet *packet;
  28. struct socket_address *dest;
  29. packet = talloc_zero(nbtsock, struct nbt_name_packet);
  30. if (packet == NULL) return NULL;
  31. packet->qdcount = 1;
  32. packet->operation = NBT_OPCODE_QUERY;
  33. if (io->in.broadcast) {
  34. packet->operation |= NBT_FLAG_BROADCAST;
  35. }
  36. if (io->in.wins_lookup) {
  37. packet->operation |= NBT_FLAG_RECURSION_DESIRED;
  38. }
  39. packet->questions = talloc_array(packet, struct nbt_name_question, 1);
  40. if (packet->questions == NULL) goto failed;
  41. packet->questions[0].name = io->in.name;
  42. packet->questions[0].question_type = NBT_QTYPE_NETBIOS;
  43. packet->questions[0].question_class = NBT_QCLASS_IP;
  44. dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
  45. io->in.dest_addr, io->in.dest_port);
  46. if (dest == NULL) goto failed;
  47. req = nbt_name_request_send(nbtsock, nbtsock, dest, packet,
  48. io->in.timeout, io->in.retries, false);
  49. if (req == NULL) goto failed;
  50. talloc_free(packet);
  51. return req;
  52. failed:
  53. talloc_free(packet);
  54. return NULL;
  55. }
  56. /**
  57. wait for a name query reply
  58. */
  59. _PUBLIC_ NTSTATUS nbt_name_query_recv(struct nbt_name_request *req,
  60. TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
  61. {
  62. NTSTATUS status;
  63. struct nbt_name_packet *packet;
  64. int i;
  65. status = nbt_name_request_recv(req);
  66. if (!NT_STATUS_IS_OK(status) ||
  67. req->num_replies == 0) {
  68. talloc_free(req);
  69. return status;
  70. }
  71. packet = req->replies[0].packet;
  72. io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
  73. if ((packet->operation & NBT_RCODE) != 0) {
  74. status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
  75. talloc_free(req);
  76. return status;
  77. }
  78. if (packet->ancount != 1 ||
  79. packet->answers[0].rr_type != NBT_QTYPE_NETBIOS ||
  80. packet->answers[0].rr_class != NBT_QCLASS_IP) {
  81. talloc_free(req);
  82. return status;
  83. }
  84. io->out.name = packet->answers[0].name;
  85. io->out.num_addrs = packet->answers[0].rdata.netbios.length / 6;
  86. io->out.reply_addrs = talloc_array(mem_ctx, const char *, io->out.num_addrs+1);
  87. if (io->out.reply_addrs == NULL) {
  88. talloc_free(req);
  89. return NT_STATUS_NO_MEMORY;
  90. }
  91. for (i=0;i<io->out.num_addrs;i++) {
  92. io->out.reply_addrs[i] = talloc_steal(io->out.reply_addrs,
  93. packet->answers[0].rdata.netbios.addresses[i].ipaddr);
  94. }
  95. io->out.reply_addrs[i] = NULL;
  96. talloc_steal(mem_ctx, io->out.name.name);
  97. talloc_steal(mem_ctx, io->out.name.scope);
  98. talloc_free(req);
  99. return NT_STATUS_OK;
  100. }
  101. /**
  102. wait for a name query reply
  103. */
  104. _PUBLIC_ NTSTATUS nbt_name_query(struct nbt_name_socket *nbtsock,
  105. TALLOC_CTX *mem_ctx, struct nbt_name_query *io)
  106. {
  107. struct nbt_name_request *req = nbt_name_query_send(nbtsock, io);
  108. return nbt_name_query_recv(req, mem_ctx, io);
  109. }
  110. /**
  111. send a nbt name status
  112. */
  113. _PUBLIC_ struct nbt_name_request *nbt_name_status_send(struct nbt_name_socket *nbtsock,
  114. struct nbt_name_status *io)
  115. {
  116. struct nbt_name_request *req;
  117. struct nbt_name_packet *packet;
  118. struct socket_address *dest;
  119. packet = talloc_zero(nbtsock, struct nbt_name_packet);
  120. if (packet == NULL) return NULL;
  121. packet->qdcount = 1;
  122. packet->operation = NBT_OPCODE_QUERY;
  123. packet->questions = talloc_array(packet, struct nbt_name_question, 1);
  124. if (packet->questions == NULL) goto failed;
  125. packet->questions[0].name = io->in.name;
  126. packet->questions[0].question_type = NBT_QTYPE_STATUS;
  127. packet->questions[0].question_class = NBT_QCLASS_IP;
  128. dest = socket_address_from_strings(packet, nbtsock->sock->backend_name,
  129. io->in.dest_addr, io->in.dest_port);
  130. if (dest == NULL) goto failed;
  131. req = nbt_name_request_send(nbtsock, nbtsock, dest, packet,
  132. io->in.timeout, io->in.retries, false);
  133. if (req == NULL) goto failed;
  134. talloc_free(packet);
  135. return req;
  136. failed:
  137. talloc_free(packet);
  138. return NULL;
  139. }
  140. /**
  141. wait for a name status reply
  142. */
  143. _PUBLIC_ NTSTATUS nbt_name_status_recv(struct nbt_name_request *req,
  144. TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
  145. {
  146. NTSTATUS status;
  147. struct nbt_name_packet *packet;
  148. int i;
  149. status = nbt_name_request_recv(req);
  150. if (!NT_STATUS_IS_OK(status) ||
  151. req->num_replies == 0) {
  152. talloc_free(req);
  153. return status;
  154. }
  155. packet = req->replies[0].packet;
  156. io->out.reply_from = talloc_steal(mem_ctx, req->replies[0].dest->addr);
  157. if ((packet->operation & NBT_RCODE) != 0) {
  158. status = nbt_rcode_to_ntstatus(packet->operation & NBT_RCODE);
  159. talloc_free(req);
  160. return status;
  161. }
  162. if (packet->ancount != 1 ||
  163. packet->answers[0].rr_type != NBT_QTYPE_STATUS ||
  164. packet->answers[0].rr_class != NBT_QCLASS_IP) {
  165. talloc_free(req);
  166. return NT_STATUS_INVALID_NETWORK_RESPONSE;
  167. }
  168. io->out.name = packet->answers[0].name;
  169. talloc_steal(mem_ctx, io->out.name.name);
  170. talloc_steal(mem_ctx, io->out.name.scope);
  171. io->out.status = packet->answers[0].rdata.status;
  172. talloc_steal(mem_ctx, io->out.status.names);
  173. for (i=0;i<io->out.status.num_names;i++) {
  174. talloc_steal(io->out.status.names, io->out.status.names[i].name);
  175. }
  176. talloc_free(req);
  177. return NT_STATUS_OK;
  178. }
  179. /**
  180. wait for a name status reply
  181. */
  182. _PUBLIC_ NTSTATUS nbt_name_status(struct nbt_name_socket *nbtsock,
  183. TALLOC_CTX *mem_ctx, struct nbt_name_status *io)
  184. {
  185. struct nbt_name_request *req = nbt_name_status_send(nbtsock, io);
  186. return nbt_name_status_recv(req, mem_ctx, io);
  187. }