s3-libsmb: put namequery headers to nmblib.h
[Samba.git] / source3 / libsmb / clidgram.c
blob78f9ea0692cd543035c0a018416256e0b358cfa4
1 /*
2 Unix SMB/CIFS implementation.
3 client dgram calls
4 Copyright (C) Andrew Tridgell 1994-1998
5 Copyright (C) Richard Sharpe 2001
6 Copyright (C) John Terpstra 2001
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.
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.
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/>.
22 #include "includes.h"
23 #include "librpc/gen_ndr/messaging.h"
24 #include "libsmb/clidgram.h"
25 #include "libsmb/nmblib.h"
28 * cli_send_mailslot, send a mailslot for client code ...
31 static bool cli_prep_mailslot(bool unique, const char *mailslot,
32 uint16 priority,
33 char *buf, int len,
34 const char *srcname, int src_type,
35 const char *dstname, int dest_type,
36 const struct sockaddr_storage *dest_ss,
37 int dgm_id,
38 struct packet_struct *p)
40 struct dgram_packet *dgram = &p->packet.dgram;
41 char *ptr, *p2;
42 char tmp[4];
43 char addr[INET6_ADDRSTRLEN];
45 ZERO_STRUCTP(p);
48 * Next, build the DGRAM ...
51 /* DIRECT GROUP or UNIQUE datagram. */
52 dgram->header.msg_type = unique ? 0x10 : 0x11;
53 dgram->header.flags.node_type = M_NODE;
54 dgram->header.flags.first = True;
55 dgram->header.flags.more = False;
56 dgram->header.dgm_id = dgm_id;
57 /* source ip is filled by nmbd */
58 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
59 dgram->header.packet_offset = 0;
61 make_nmb_name(&dgram->source_name,srcname,src_type);
62 make_nmb_name(&dgram->dest_name,dstname,dest_type);
64 ptr = &dgram->data[0];
66 /* Setup the smb part. */
67 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
68 memcpy(tmp,ptr,4);
70 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
71 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
72 return False;
75 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
76 memcpy(ptr,tmp,4);
78 SCVAL(ptr,smb_com,SMBtrans);
79 SSVAL(ptr,smb_vwv1,len);
80 SSVAL(ptr,smb_vwv11,len);
81 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
82 SSVAL(ptr,smb_vwv13,3);
83 SSVAL(ptr,smb_vwv14,1);
84 SSVAL(ptr,smb_vwv15,priority);
85 SSVAL(ptr,smb_vwv16,2);
86 p2 = smb_buf(ptr);
87 fstrcpy(p2,mailslot);
88 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
89 if (!p2) {
90 return False;
93 memcpy(p2,buf,len);
94 p2 += len;
96 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
98 p->packet_type = DGRAM_PACKET;
99 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
100 p->timestamp = time(NULL);
102 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
103 mailslot, nmb_namestr(&dgram->source_name)));
104 print_sockaddr(addr, sizeof(addr), dest_ss);
106 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
108 return true;
111 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
113 return talloc_asprintf(mem_ctx, "%s%X",
114 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
117 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
118 const char *domain_name,
119 const struct dom_sid *sid,
120 uint32_t nt_version,
121 const char *my_mailslot,
122 int dgm_id,
123 struct packet_struct *p)
125 TALLOC_CTX *frame = talloc_stackframe();
126 const char *my_acct_name;
127 struct nbt_netlogon_packet packet;
128 struct NETLOGON_SAM_LOGON_REQUEST *s;
129 enum ndr_err_code ndr_err;
130 DATA_BLOB blob = data_blob_null;
131 struct dom_sid my_sid;
132 bool ret = false;
134 ZERO_STRUCT(packet);
135 ZERO_STRUCT(my_sid);
137 if (sid != NULL) {
138 my_sid = *sid;
141 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", global_myname());
142 if (my_acct_name == NULL) {
143 goto fail;
146 packet.command = LOGON_SAM_LOGON_REQUEST;
147 s = &packet.req.logon;
149 s->request_count = 0;
150 s->computer_name = global_myname();
151 s->user_name = my_acct_name;
152 s->mailslot_name = my_mailslot;
153 s->acct_control = ACB_WSTRUST;
154 s->sid = my_sid;
155 s->nt_version = nt_version;
156 s->lmnt_token = 0xffff;
157 s->lm20_token = 0xffff;
159 if (DEBUGLEVEL >= 10) {
160 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
163 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
164 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
165 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
166 goto fail;
169 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
170 (char *)blob.data, blob.length,
171 global_myname(), 0, domain_name, 0x1c,
172 dc_ss, dgm_id, p);
173 fail:
174 TALLOC_FREE(frame);
175 return ret;
178 static bool parse_getdc_response(
179 struct packet_struct *packet,
180 TALLOC_CTX *mem_ctx,
181 const char *domain_name,
182 uint32_t *nt_version,
183 const char **dc_name,
184 struct netlogon_samlogon_response **samlogon_response)
186 DATA_BLOB blob;
187 struct netlogon_samlogon_response *r;
188 union dgram_message_body p;
189 enum ndr_err_code ndr_err;
190 NTSTATUS status;
192 const char *returned_dc = NULL;
193 const char *returned_domain = NULL;
195 blob = data_blob_const(packet->packet.dgram.data,
196 packet->packet.dgram.datasize);
197 if (blob.length < 4) {
198 DEBUG(1, ("invalid length: %d\n", (int)blob.length));
199 return false;
202 if (RIVAL(blob.data,0) != DGRAM_SMB) {
203 DEBUG(1, ("invalid packet\n"));
204 return false;
207 blob.data += 4;
208 blob.length -= 4;
210 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
211 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
212 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
213 DEBUG(1, ("failed to parse packet\n"));
214 return false;
217 if (p.smb.smb_command != SMB_TRANSACTION) {
218 DEBUG(1, ("invalid smb_command: %d\n", p.smb.smb_command));
219 return false;
222 if (DEBUGLEVEL >= 10) {
223 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
226 blob = p.smb.body.trans.data;
228 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
229 if (!r) {
230 return false;
233 status = pull_netlogon_samlogon_response(&blob, r, r);
234 if (!NT_STATUS_IS_OK(status)) {
235 TALLOC_FREE(r);
236 return false;
239 map_netlogon_samlogon_response(r);
241 /* do we still need this ? */
242 *nt_version = r->ntver;
244 returned_domain = r->data.nt5_ex.domain_name;
245 returned_dc = r->data.nt5_ex.pdc_name;
247 if (!strequal(returned_domain, domain_name)) {
248 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
249 domain_name, returned_domain));
250 TALLOC_FREE(r);
251 return false;
254 if (*returned_dc == '\\') returned_dc += 1;
255 if (*returned_dc == '\\') returned_dc += 1;
257 *dc_name = talloc_strdup(mem_ctx, returned_dc);
258 if (!*dc_name) {
259 TALLOC_FREE(r);
260 return false;
263 if (samlogon_response) {
264 *samlogon_response = r;
265 } else {
266 TALLOC_FREE(r);
269 DEBUG(10, ("GetDC gave name %s for domain %s\n",
270 *dc_name, returned_domain));
272 return True;
275 struct nbt_getdc_state {
276 struct tevent_context *ev;
277 struct messaging_context *msg_ctx;
278 struct nb_packet_reader *reader;
279 const char *my_mailslot;
280 pid_t nmbd_pid;
282 const struct sockaddr_storage *dc_addr;
283 const char *domain_name;
284 const struct dom_sid *sid;
285 uint32_t nt_version;
286 const char *dc_name;
287 struct netlogon_samlogon_response *samlogon_response;
289 struct packet_struct p;
292 static void nbt_getdc_got_reader(struct tevent_req *subreq);
293 static void nbt_getdc_got_response(struct tevent_req *subreq);
295 struct tevent_req *nbt_getdc_send(TALLOC_CTX *mem_ctx,
296 struct tevent_context *ev,
297 struct messaging_context *msg_ctx,
298 const struct sockaddr_storage *dc_addr,
299 const char *domain_name,
300 const struct dom_sid *sid,
301 uint32_t nt_version)
303 struct tevent_req *req, *subreq;
304 struct nbt_getdc_state *state;
305 uint16_t dgm_id;
307 req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
308 if (req == NULL) {
309 return NULL;
311 state->ev = ev;
312 state->msg_ctx = msg_ctx;
313 state->dc_addr = dc_addr;
314 state->domain_name = domain_name;
315 state->sid = sid;
316 state->nt_version = nt_version;
318 if (dc_addr->ss_family != AF_INET) {
319 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
320 return tevent_req_post(req, ev);
322 state->my_mailslot = mailslot_name(
323 state, ((struct sockaddr_in *)dc_addr)->sin_addr);
324 if (tevent_req_nomem(state->my_mailslot, req)) {
325 return tevent_req_post(req, ev);
327 state->nmbd_pid = pidfile_pid("nmbd");
328 if (state->nmbd_pid == 0) {
329 DEBUG(3, ("No nmbd found\n"));
330 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
331 return tevent_req_post(req, ev);
334 generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
336 if (!prep_getdc_request(dc_addr, domain_name, sid, nt_version,
337 state->my_mailslot, dgm_id & 0x7fff,
338 &state->p)) {
339 DEBUG(3, ("prep_getdc_request failed\n"));
340 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
341 return tevent_req_post(req, ev);
344 subreq = nb_packet_reader_send(state, ev, DGRAM_PACKET, -1,
345 state->my_mailslot);
346 if (tevent_req_nomem(subreq, req)) {
347 return tevent_req_post(req, ev);
349 tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
350 return req;
353 static void nbt_getdc_got_reader(struct tevent_req *subreq)
355 struct tevent_req *req = tevent_req_callback_data(
356 subreq, struct tevent_req);
357 struct nbt_getdc_state *state = tevent_req_data(
358 req, struct nbt_getdc_state);
359 NTSTATUS status;
361 status = nb_packet_reader_recv(subreq, state, &state->reader);
362 TALLOC_FREE(subreq);
363 if (tevent_req_nterror(req, status)) {
364 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
365 nt_errstr(status)));
366 return;
369 status = messaging_send_buf(
370 state->msg_ctx, pid_to_procid(state->nmbd_pid),
371 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
373 if (tevent_req_nterror(req, status)) {
374 DEBUG(10, ("messaging_send_buf returned %s\n",
375 nt_errstr(status)));
376 return;
378 subreq = nb_packet_read_send(state, state->ev, state->reader);
379 if (tevent_req_nomem(subreq, req)) {
380 return;
382 tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
385 static void nbt_getdc_got_response(struct tevent_req *subreq)
387 struct tevent_req *req = tevent_req_callback_data(
388 subreq, struct tevent_req);
389 struct nbt_getdc_state *state = tevent_req_data(
390 req, struct nbt_getdc_state);
391 struct packet_struct *p;
392 NTSTATUS status;
393 bool ret;
395 status = nb_packet_read_recv(subreq, &p);
396 TALLOC_FREE(subreq);
397 if (tevent_req_nterror(req, status)) {
398 return;
401 ret = parse_getdc_response(p, state, state->domain_name,
402 &state->nt_version, &state->dc_name,
403 &state->samlogon_response);
404 free_packet(p);
405 if (!ret) {
406 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
407 return;
409 tevent_req_done(req);
412 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
413 uint32_t *nt_version, const char **dc_name,
414 struct netlogon_samlogon_response **samlogon_response)
416 struct nbt_getdc_state *state = tevent_req_data(
417 req, struct nbt_getdc_state);
418 NTSTATUS status;
420 if (tevent_req_is_nterror(req, &status)) {
421 return status;
423 if (nt_version != NULL) {
424 *nt_version = state->nt_version;
426 if (dc_name != NULL) {
427 *dc_name = talloc_move(mem_ctx, &state->dc_name);
429 if (samlogon_response != NULL) {
430 *samlogon_response = talloc_move(
431 mem_ctx, &state->samlogon_response);
433 return NT_STATUS_OK;
436 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
437 const struct sockaddr_storage *dc_addr,
438 const char *domain_name,
439 const struct dom_sid *sid,
440 uint32_t nt_version,
441 TALLOC_CTX *mem_ctx,
442 uint32_t *pnt_version,
443 const char **dc_name,
444 struct netlogon_samlogon_response **samlogon_response)
446 TALLOC_CTX *frame = talloc_stackframe();
447 struct tevent_context *ev;
448 struct tevent_req *req;
449 NTSTATUS status = NT_STATUS_NO_MEMORY;
451 ev = tevent_context_init(frame);
452 if (ev == NULL) {
453 goto fail;
455 req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
456 sid, nt_version);
457 if (req == NULL) {
458 goto fail;
460 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
461 goto fail;
463 status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
464 samlogon_response);
465 fail:
466 TALLOC_FREE(frame);
467 return status;