netcmd/dns: fix typo
[Samba/bjacke.git] / source3 / libsmb / clidgram.c
blobac4a00d423d82769d800523704a65f59f5f4a7f9
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 "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "libsmb/clidgram.h"
26 #include "libsmb/nmblib.h"
27 #include "messages.h"
28 #include "librpc/gen_ndr/samr.h"
29 #include "../lib/util/pidfile.h"
32 * cli_send_mailslot, send a mailslot for client code ...
35 static bool cli_prep_mailslot(bool unique, const char *mailslot,
36 uint16 priority,
37 char *buf, int len,
38 const char *srcname, int src_type,
39 const char *dstname, int dest_type,
40 const struct sockaddr_storage *dest_ss,
41 int dgm_id,
42 struct packet_struct *p)
44 struct dgram_packet *dgram = &p->packet.dgram;
45 char *ptr, *p2;
46 char tmp[4];
47 char addr[INET6_ADDRSTRLEN];
49 ZERO_STRUCTP(p);
52 * Next, build the DGRAM ...
55 /* DIRECT GROUP or UNIQUE datagram. */
56 dgram->header.msg_type = unique ? 0x10 : 0x11;
57 dgram->header.flags.node_type = M_NODE;
58 dgram->header.flags.first = True;
59 dgram->header.flags.more = False;
60 dgram->header.dgm_id = dgm_id;
61 /* source ip is filled by nmbd */
62 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
63 dgram->header.packet_offset = 0;
65 make_nmb_name(&dgram->source_name,srcname,src_type);
66 make_nmb_name(&dgram->dest_name,dstname,dest_type);
68 ptr = &dgram->data[0];
70 /* Setup the smb part. */
71 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
72 memcpy(tmp,ptr,4);
74 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
75 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
76 return False;
79 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
80 memcpy(ptr,tmp,4);
82 SCVAL(ptr,smb_com,SMBtrans);
83 SSVAL(ptr,smb_vwv1,len);
84 SSVAL(ptr,smb_vwv11,len);
85 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
86 SSVAL(ptr,smb_vwv13,3);
87 SSVAL(ptr,smb_vwv14,1);
88 SSVAL(ptr,smb_vwv15,priority);
89 SSVAL(ptr,smb_vwv16,2);
90 p2 = smb_buf(ptr);
91 fstrcpy(p2,mailslot);
92 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
93 if (!p2) {
94 return False;
97 memcpy(p2,buf,len);
98 p2 += len;
100 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
102 p->packet_type = DGRAM_PACKET;
103 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
104 p->timestamp = time(NULL);
106 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
107 mailslot, nmb_namestr(&dgram->source_name)));
108 print_sockaddr(addr, sizeof(addr), dest_ss);
110 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
112 return true;
115 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
117 return talloc_asprintf(mem_ctx, "%s%X",
118 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
121 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
122 const char *domain_name,
123 const struct dom_sid *sid,
124 uint32_t nt_version,
125 const char *my_mailslot,
126 int dgm_id,
127 struct packet_struct *p)
129 TALLOC_CTX *frame = talloc_stackframe();
130 const char *my_acct_name;
131 struct nbt_netlogon_packet packet;
132 struct NETLOGON_SAM_LOGON_REQUEST *s;
133 enum ndr_err_code ndr_err;
134 DATA_BLOB blob = data_blob_null;
135 struct dom_sid my_sid;
136 bool ret = false;
138 ZERO_STRUCT(packet);
139 ZERO_STRUCT(my_sid);
141 if (sid != NULL) {
142 my_sid = *sid;
145 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", lp_netbios_name());
146 if (my_acct_name == NULL) {
147 goto fail;
150 packet.command = LOGON_SAM_LOGON_REQUEST;
151 s = &packet.req.logon;
153 s->request_count = 0;
154 s->computer_name = lp_netbios_name();
155 s->user_name = my_acct_name;
156 s->mailslot_name = my_mailslot;
157 s->acct_control = ACB_WSTRUST;
158 s->sid = my_sid;
159 s->nt_version = nt_version;
160 s->lmnt_token = 0xffff;
161 s->lm20_token = 0xffff;
163 if (DEBUGLEVEL >= 10) {
164 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
167 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
168 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
169 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
170 goto fail;
173 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
174 (char *)blob.data, blob.length,
175 lp_netbios_name(), 0, domain_name, 0x1c,
176 dc_ss, dgm_id, p);
177 fail:
178 TALLOC_FREE(frame);
179 return ret;
182 static bool parse_getdc_response(
183 struct packet_struct *packet,
184 TALLOC_CTX *mem_ctx,
185 const char *domain_name,
186 uint32_t *nt_version,
187 const char **dc_name,
188 struct netlogon_samlogon_response **samlogon_response)
190 DATA_BLOB blob;
191 struct netlogon_samlogon_response *r;
192 union dgram_message_body p;
193 enum ndr_err_code ndr_err;
194 NTSTATUS status;
196 const char *returned_dc = NULL;
197 const char *returned_domain = NULL;
199 blob = data_blob_const(packet->packet.dgram.data,
200 packet->packet.dgram.datasize);
201 if (blob.length < 4) {
202 DEBUG(1, ("invalid length: %d\n", (int)blob.length));
203 return false;
206 if (RIVAL(blob.data,0) != DGRAM_SMB) {
207 DEBUG(1, ("invalid packet\n"));
208 return false;
211 blob.data += 4;
212 blob.length -= 4;
214 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
215 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
216 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
217 DEBUG(1, ("failed to parse packet\n"));
218 return false;
221 if (p.smb.smb_command != SMB_TRANSACTION) {
222 DEBUG(1, ("invalid smb_command: %d\n", p.smb.smb_command));
223 return false;
226 if (DEBUGLEVEL >= 10) {
227 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
230 blob = p.smb.body.trans.data;
232 r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
233 if (!r) {
234 return false;
237 status = pull_netlogon_samlogon_response(&blob, r, r);
238 if (!NT_STATUS_IS_OK(status)) {
239 TALLOC_FREE(r);
240 return false;
243 map_netlogon_samlogon_response(r);
245 /* do we still need this ? */
246 *nt_version = r->ntver;
248 returned_domain = r->data.nt5_ex.domain_name;
249 returned_dc = r->data.nt5_ex.pdc_name;
251 if (!strequal(returned_domain, domain_name)) {
252 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
253 domain_name, returned_domain));
254 TALLOC_FREE(r);
255 return false;
258 if (*returned_dc == '\\') returned_dc += 1;
259 if (*returned_dc == '\\') returned_dc += 1;
261 *dc_name = talloc_strdup(mem_ctx, returned_dc);
262 if (!*dc_name) {
263 TALLOC_FREE(r);
264 return false;
267 if (samlogon_response) {
268 *samlogon_response = r;
269 } else {
270 TALLOC_FREE(r);
273 DEBUG(10, ("GetDC gave name %s for domain %s\n",
274 *dc_name, returned_domain));
276 return True;
279 struct nbt_getdc_state {
280 struct tevent_context *ev;
281 struct messaging_context *msg_ctx;
282 struct nb_packet_reader *reader;
283 const char *my_mailslot;
284 pid_t nmbd_pid;
286 const struct sockaddr_storage *dc_addr;
287 const char *domain_name;
288 const struct dom_sid *sid;
289 uint32_t nt_version;
290 const char *dc_name;
291 struct netlogon_samlogon_response *samlogon_response;
293 struct packet_struct p;
296 static void nbt_getdc_got_reader(struct tevent_req *subreq);
297 static void nbt_getdc_got_response(struct tevent_req *subreq);
299 struct tevent_req *nbt_getdc_send(TALLOC_CTX *mem_ctx,
300 struct tevent_context *ev,
301 struct messaging_context *msg_ctx,
302 const struct sockaddr_storage *dc_addr,
303 const char *domain_name,
304 const struct dom_sid *sid,
305 uint32_t nt_version)
307 struct tevent_req *req, *subreq;
308 struct nbt_getdc_state *state;
309 uint16_t dgm_id;
311 req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
312 if (req == NULL) {
313 return NULL;
315 state->ev = ev;
316 state->msg_ctx = msg_ctx;
317 state->dc_addr = dc_addr;
318 state->domain_name = domain_name;
319 state->sid = sid;
320 state->nt_version = nt_version;
322 if (dc_addr->ss_family != AF_INET) {
323 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
324 return tevent_req_post(req, ev);
326 state->my_mailslot = mailslot_name(
327 state, ((const struct sockaddr_in *)dc_addr)->sin_addr);
328 if (tevent_req_nomem(state->my_mailslot, req)) {
329 return tevent_req_post(req, ev);
331 state->nmbd_pid = pidfile_pid(lp_piddir(), "nmbd");
332 if (state->nmbd_pid == 0) {
333 DEBUG(3, ("No nmbd found\n"));
334 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
335 return tevent_req_post(req, ev);
338 generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
340 if (!prep_getdc_request(dc_addr, domain_name, sid, nt_version,
341 state->my_mailslot, dgm_id & 0x7fff,
342 &state->p)) {
343 DEBUG(3, ("prep_getdc_request failed\n"));
344 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
345 return tevent_req_post(req, ev);
348 subreq = nb_packet_reader_send(state, ev, DGRAM_PACKET, -1,
349 state->my_mailslot);
350 if (tevent_req_nomem(subreq, req)) {
351 return tevent_req_post(req, ev);
353 tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
354 return req;
357 static void nbt_getdc_got_reader(struct tevent_req *subreq)
359 struct tevent_req *req = tevent_req_callback_data(
360 subreq, struct tevent_req);
361 struct nbt_getdc_state *state = tevent_req_data(
362 req, struct nbt_getdc_state);
363 NTSTATUS status;
365 status = nb_packet_reader_recv(subreq, state, &state->reader);
366 TALLOC_FREE(subreq);
367 if (tevent_req_nterror(req, status)) {
368 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
369 nt_errstr(status)));
370 return;
373 status = messaging_send_buf(
374 state->msg_ctx, pid_to_procid(state->nmbd_pid),
375 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
377 if (tevent_req_nterror(req, status)) {
378 DEBUG(10, ("messaging_send_buf returned %s\n",
379 nt_errstr(status)));
380 return;
382 subreq = nb_packet_read_send(state, state->ev, state->reader);
383 if (tevent_req_nomem(subreq, req)) {
384 return;
386 tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
389 static void nbt_getdc_got_response(struct tevent_req *subreq)
391 struct tevent_req *req = tevent_req_callback_data(
392 subreq, struct tevent_req);
393 struct nbt_getdc_state *state = tevent_req_data(
394 req, struct nbt_getdc_state);
395 struct packet_struct *p;
396 NTSTATUS status;
397 bool ret;
399 status = nb_packet_read_recv(subreq, &p);
400 TALLOC_FREE(subreq);
401 if (tevent_req_nterror(req, status)) {
402 return;
405 ret = parse_getdc_response(p, state, state->domain_name,
406 &state->nt_version, &state->dc_name,
407 &state->samlogon_response);
408 free_packet(p);
409 if (!ret) {
410 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
411 return;
413 tevent_req_done(req);
416 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
417 uint32_t *nt_version, const char **dc_name,
418 struct netlogon_samlogon_response **samlogon_response)
420 struct nbt_getdc_state *state = tevent_req_data(
421 req, struct nbt_getdc_state);
422 NTSTATUS status;
424 if (tevent_req_is_nterror(req, &status)) {
425 return status;
427 if (nt_version != NULL) {
428 *nt_version = state->nt_version;
430 if (dc_name != NULL) {
431 *dc_name = talloc_move(mem_ctx, &state->dc_name);
433 if (samlogon_response != NULL) {
434 *samlogon_response = talloc_move(
435 mem_ctx, &state->samlogon_response);
437 return NT_STATUS_OK;
440 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
441 uint32_t timeout_in_seconds,
442 const struct sockaddr_storage *dc_addr,
443 const char *domain_name,
444 const struct dom_sid *sid,
445 uint32_t nt_version,
446 TALLOC_CTX *mem_ctx,
447 uint32_t *pnt_version,
448 const char **dc_name,
449 struct netlogon_samlogon_response **samlogon_response)
451 TALLOC_CTX *frame = talloc_stackframe();
452 struct tevent_context *ev;
453 struct tevent_req *req;
454 NTSTATUS status = NT_STATUS_NO_MEMORY;
456 ev = samba_tevent_context_init(frame);
457 if (ev == NULL) {
458 goto fail;
460 req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
461 sid, nt_version);
462 if (req == NULL) {
463 goto fail;
465 if (!tevent_req_set_endtime(req, ev,
466 timeval_current_ofs(timeout_in_seconds, 0))) {
467 goto fail;
469 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
470 goto fail;
472 status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
473 samlogon_response);
474 fail:
475 TALLOC_FREE(frame);
476 return status;