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