s3:libsmb: the workgroup in the non-extended-security negprot is not aligned (#8573)
[Samba.git] / source3 / libsmb / clidgram.c
blobd48811b0d919177bf1a3e46af736e64673712355
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"
30 * cli_send_mailslot, send a mailslot for client code ...
33 static bool cli_prep_mailslot(bool unique, const char *mailslot,
34 uint16 priority,
35 char *buf, int len,
36 const char *srcname, int src_type,
37 const char *dstname, int dest_type,
38 const struct sockaddr_storage *dest_ss,
39 int dgm_id,
40 struct packet_struct *p)
42 struct dgram_packet *dgram = &p->packet.dgram;
43 char *ptr, *p2;
44 char tmp[4];
45 char addr[INET6_ADDRSTRLEN];
47 ZERO_STRUCTP(p);
50 * Next, build the DGRAM ...
53 /* DIRECT GROUP or UNIQUE datagram. */
54 dgram->header.msg_type = unique ? 0x10 : 0x11;
55 dgram->header.flags.node_type = M_NODE;
56 dgram->header.flags.first = True;
57 dgram->header.flags.more = False;
58 dgram->header.dgm_id = dgm_id;
59 /* source ip is filled by nmbd */
60 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
61 dgram->header.packet_offset = 0;
63 make_nmb_name(&dgram->source_name,srcname,src_type);
64 make_nmb_name(&dgram->dest_name,dstname,dest_type);
66 ptr = &dgram->data[0];
68 /* Setup the smb part. */
69 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
70 memcpy(tmp,ptr,4);
72 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
73 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
74 return False;
77 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
78 memcpy(ptr,tmp,4);
80 SCVAL(ptr,smb_com,SMBtrans);
81 SSVAL(ptr,smb_vwv1,len);
82 SSVAL(ptr,smb_vwv11,len);
83 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
84 SSVAL(ptr,smb_vwv13,3);
85 SSVAL(ptr,smb_vwv14,1);
86 SSVAL(ptr,smb_vwv15,priority);
87 SSVAL(ptr,smb_vwv16,2);
88 p2 = smb_buf(ptr);
89 fstrcpy(p2,mailslot);
90 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
91 if (!p2) {
92 return False;
95 memcpy(p2,buf,len);
96 p2 += len;
98 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
100 p->packet_type = DGRAM_PACKET;
101 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
102 p->timestamp = time(NULL);
104 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
105 mailslot, nmb_namestr(&dgram->source_name)));
106 print_sockaddr(addr, sizeof(addr), dest_ss);
108 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
110 return true;
113 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
115 return talloc_asprintf(mem_ctx, "%s%X",
116 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
119 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
120 const char *domain_name,
121 const struct dom_sid *sid,
122 uint32_t nt_version,
123 const char *my_mailslot,
124 int dgm_id,
125 struct packet_struct *p)
127 TALLOC_CTX *frame = talloc_stackframe();
128 const char *my_acct_name;
129 struct nbt_netlogon_packet packet;
130 struct NETLOGON_SAM_LOGON_REQUEST *s;
131 enum ndr_err_code ndr_err;
132 DATA_BLOB blob = data_blob_null;
133 struct dom_sid my_sid;
134 bool ret = false;
136 ZERO_STRUCT(packet);
137 ZERO_STRUCT(my_sid);
139 if (sid != NULL) {
140 my_sid = *sid;
143 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", global_myname());
144 if (my_acct_name == NULL) {
145 goto fail;
148 packet.command = LOGON_SAM_LOGON_REQUEST;
149 s = &packet.req.logon;
151 s->request_count = 0;
152 s->computer_name = global_myname();
153 s->user_name = my_acct_name;
154 s->mailslot_name = my_mailslot;
155 s->acct_control = ACB_WSTRUST;
156 s->sid = my_sid;
157 s->nt_version = nt_version;
158 s->lmnt_token = 0xffff;
159 s->lm20_token = 0xffff;
161 if (DEBUGLEVEL >= 10) {
162 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
165 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
166 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
167 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
168 goto fail;
171 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
172 (char *)blob.data, blob.length,
173 global_myname(), 0, domain_name, 0x1c,
174 dc_ss, dgm_id, p);
175 fail:
176 TALLOC_FREE(frame);
177 return ret;
180 static bool parse_getdc_response(
181 struct packet_struct *packet,
182 TALLOC_CTX *mem_ctx,
183 const char *domain_name,
184 uint32_t *nt_version,
185 const char **dc_name,
186 struct netlogon_samlogon_response **samlogon_response)
188 DATA_BLOB blob;
189 struct netlogon_samlogon_response *r;
190 union dgram_message_body p;
191 enum ndr_err_code ndr_err;
192 NTSTATUS status;
194 const char *returned_dc = NULL;
195 const char *returned_domain = NULL;
197 blob = data_blob_const(packet->packet.dgram.data,
198 packet->packet.dgram.datasize);
199 if (blob.length < 4) {
200 DEBUG(1, ("invalid length: %d\n", (int)blob.length));
201 return false;
204 if (RIVAL(blob.data,0) != DGRAM_SMB) {
205 DEBUG(1, ("invalid packet\n"));
206 return false;
209 blob.data += 4;
210 blob.length -= 4;
212 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
213 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
214 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
215 DEBUG(1, ("failed to parse packet\n"));
216 return false;
219 if (p.smb.smb_command != SMB_TRANSACTION) {
220 DEBUG(1, ("invalid smb_command: %d\n", p.smb.smb_command));
221 return false;
224 if (DEBUGLEVEL >= 10) {
225 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
228 blob = p.smb.body.trans.data;
230 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
231 if (!r) {
232 return false;
235 status = pull_netlogon_samlogon_response(&blob, r, r);
236 if (!NT_STATUS_IS_OK(status)) {
237 TALLOC_FREE(r);
238 return false;
241 map_netlogon_samlogon_response(r);
243 /* do we still need this ? */
244 *nt_version = r->ntver;
246 returned_domain = r->data.nt5_ex.domain_name;
247 returned_dc = r->data.nt5_ex.pdc_name;
249 if (!strequal(returned_domain, domain_name)) {
250 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
251 domain_name, returned_domain));
252 TALLOC_FREE(r);
253 return false;
256 if (*returned_dc == '\\') returned_dc += 1;
257 if (*returned_dc == '\\') returned_dc += 1;
259 *dc_name = talloc_strdup(mem_ctx, returned_dc);
260 if (!*dc_name) {
261 TALLOC_FREE(r);
262 return false;
265 if (samlogon_response) {
266 *samlogon_response = r;
267 } else {
268 TALLOC_FREE(r);
271 DEBUG(10, ("GetDC gave name %s for domain %s\n",
272 *dc_name, returned_domain));
274 return True;
277 struct nbt_getdc_state {
278 struct tevent_context *ev;
279 struct messaging_context *msg_ctx;
280 struct nb_packet_reader *reader;
281 const char *my_mailslot;
282 pid_t nmbd_pid;
284 const struct sockaddr_storage *dc_addr;
285 const char *domain_name;
286 const struct dom_sid *sid;
287 uint32_t nt_version;
288 const char *dc_name;
289 struct netlogon_samlogon_response *samlogon_response;
291 struct packet_struct p;
294 static void nbt_getdc_got_reader(struct tevent_req *subreq);
295 static void nbt_getdc_got_response(struct tevent_req *subreq);
297 struct tevent_req *nbt_getdc_send(TALLOC_CTX *mem_ctx,
298 struct tevent_context *ev,
299 struct messaging_context *msg_ctx,
300 const struct sockaddr_storage *dc_addr,
301 const char *domain_name,
302 const struct dom_sid *sid,
303 uint32_t nt_version)
305 struct tevent_req *req, *subreq;
306 struct nbt_getdc_state *state;
307 uint16_t dgm_id;
309 req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
310 if (req == NULL) {
311 return NULL;
313 state->ev = ev;
314 state->msg_ctx = msg_ctx;
315 state->dc_addr = dc_addr;
316 state->domain_name = domain_name;
317 state->sid = sid;
318 state->nt_version = nt_version;
320 if (dc_addr->ss_family != AF_INET) {
321 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
322 return tevent_req_post(req, ev);
324 state->my_mailslot = mailslot_name(
325 state, ((struct sockaddr_in *)dc_addr)->sin_addr);
326 if (tevent_req_nomem(state->my_mailslot, req)) {
327 return tevent_req_post(req, ev);
329 state->nmbd_pid = pidfile_pid("nmbd");
330 if (state->nmbd_pid == 0) {
331 DEBUG(3, ("No nmbd found\n"));
332 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
333 return tevent_req_post(req, ev);
336 generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
338 if (!prep_getdc_request(dc_addr, domain_name, sid, nt_version,
339 state->my_mailslot, dgm_id & 0x7fff,
340 &state->p)) {
341 DEBUG(3, ("prep_getdc_request failed\n"));
342 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
343 return tevent_req_post(req, ev);
346 subreq = nb_packet_reader_send(state, ev, DGRAM_PACKET, -1,
347 state->my_mailslot);
348 if (tevent_req_nomem(subreq, req)) {
349 return tevent_req_post(req, ev);
351 tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
352 return req;
355 static void nbt_getdc_got_reader(struct tevent_req *subreq)
357 struct tevent_req *req = tevent_req_callback_data(
358 subreq, struct tevent_req);
359 struct nbt_getdc_state *state = tevent_req_data(
360 req, struct nbt_getdc_state);
361 NTSTATUS status;
363 status = nb_packet_reader_recv(subreq, state, &state->reader);
364 TALLOC_FREE(subreq);
365 if (tevent_req_nterror(req, status)) {
366 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
367 nt_errstr(status)));
368 return;
371 status = messaging_send_buf(
372 state->msg_ctx, pid_to_procid(state->nmbd_pid),
373 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
375 if (tevent_req_nterror(req, status)) {
376 DEBUG(10, ("messaging_send_buf returned %s\n",
377 nt_errstr(status)));
378 return;
380 subreq = nb_packet_read_send(state, state->ev, state->reader);
381 if (tevent_req_nomem(subreq, req)) {
382 return;
384 tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
387 static void nbt_getdc_got_response(struct tevent_req *subreq)
389 struct tevent_req *req = tevent_req_callback_data(
390 subreq, struct tevent_req);
391 struct nbt_getdc_state *state = tevent_req_data(
392 req, struct nbt_getdc_state);
393 struct packet_struct *p;
394 NTSTATUS status;
395 bool ret;
397 status = nb_packet_read_recv(subreq, &p);
398 TALLOC_FREE(subreq);
399 if (tevent_req_nterror(req, status)) {
400 return;
403 ret = parse_getdc_response(p, state, state->domain_name,
404 &state->nt_version, &state->dc_name,
405 &state->samlogon_response);
406 free_packet(p);
407 if (!ret) {
408 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
409 return;
411 tevent_req_done(req);
414 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
415 uint32_t *nt_version, const char **dc_name,
416 struct netlogon_samlogon_response **samlogon_response)
418 struct nbt_getdc_state *state = tevent_req_data(
419 req, struct nbt_getdc_state);
420 NTSTATUS status;
422 if (tevent_req_is_nterror(req, &status)) {
423 return status;
425 if (nt_version != NULL) {
426 *nt_version = state->nt_version;
428 if (dc_name != NULL) {
429 *dc_name = talloc_move(mem_ctx, &state->dc_name);
431 if (samlogon_response != NULL) {
432 *samlogon_response = talloc_move(
433 mem_ctx, &state->samlogon_response);
435 return NT_STATUS_OK;
438 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
439 const struct sockaddr_storage *dc_addr,
440 const char *domain_name,
441 const struct dom_sid *sid,
442 uint32_t nt_version,
443 TALLOC_CTX *mem_ctx,
444 uint32_t *pnt_version,
445 const char **dc_name,
446 struct netlogon_samlogon_response **samlogon_response)
448 TALLOC_CTX *frame = talloc_stackframe();
449 struct tevent_context *ev;
450 struct tevent_req *req;
451 NTSTATUS status = NT_STATUS_NO_MEMORY;
453 ev = tevent_context_init(frame);
454 if (ev == NULL) {
455 goto fail;
457 req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
458 sid, nt_version);
459 if (req == NULL) {
460 goto fail;
462 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
463 goto fail;
465 status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
466 samlogon_response);
467 fail:
468 TALLOC_FREE(frame);
469 return status;