libsmb: Remove unused setup_stat_from_stat_ex()
[Samba.git] / source3 / libsmb / clidgram.c
blobc87c870345f3156981acfe418182817698c1c051
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 "libsmb/unexpected.h"
28 #include "messages.h"
29 #include "librpc/gen_ndr/samr.h"
30 #include "../lib/util/pidfile.h"
31 #include "lib/util/string_wrappers.h"
34 * cli_send_mailslot, send a mailslot for client code ...
37 static bool cli_prep_mailslot(bool unique, const char *mailslot,
38 uint16_t priority,
39 char *buf, int len,
40 const char *srcname, int src_type,
41 const char *dstname, int dest_type,
42 const struct sockaddr_storage *dest_ss,
43 int dgm_id,
44 struct packet_struct *p)
46 struct dgram_packet *dgram = &p->packet.dgram;
47 char *ptr, *p2;
48 char tmp[4];
49 char addr[INET6_ADDRSTRLEN];
51 ZERO_STRUCTP(p);
54 * Next, build the DGRAM ...
57 /* DIRECT GROUP or UNIQUE datagram. */
58 dgram->header.msg_type = unique ? 0x10 : 0x11;
59 dgram->header.flags.node_type = M_NODE;
60 dgram->header.flags.first = True;
61 dgram->header.flags.more = False;
62 dgram->header.dgm_id = dgm_id;
63 /* source ip is filled by nmbd */
64 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
65 dgram->header.packet_offset = 0;
67 make_nmb_name(&dgram->source_name,srcname,src_type);
68 make_nmb_name(&dgram->dest_name,dstname,dest_type);
70 ptr = &dgram->data[0];
72 /* Setup the smb part. */
73 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
74 memcpy(tmp,ptr,4);
76 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
77 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
78 return False;
81 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
82 memcpy(ptr,tmp,4);
84 SCVAL(ptr,smb_com,SMBtrans);
85 SSVAL(ptr,smb_vwv1,len);
86 SSVAL(ptr,smb_vwv11,len);
87 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
88 SSVAL(ptr,smb_vwv13,3);
89 SSVAL(ptr,smb_vwv14,1);
90 SSVAL(ptr,smb_vwv15,priority);
91 SSVAL(ptr,smb_vwv16,2);
92 p2 = smb_buf(ptr);
93 fstrcpy(p2,mailslot);
94 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
95 if (!p2) {
96 return False;
99 memcpy(p2,buf,len);
100 p2 += len;
102 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
104 p->packet_type = DGRAM_PACKET;
105 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
106 p->timestamp = time(NULL);
108 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
109 mailslot, nmb_namestr(&dgram->source_name)));
110 print_sockaddr(addr, sizeof(addr), dest_ss);
112 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
114 return true;
117 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
119 return talloc_asprintf(mem_ctx, "%s%X",
120 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
123 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
124 const char *account_name,
125 uint32_t account_flags,
126 const char *domain_name,
127 const struct dom_sid *sid,
128 uint32_t nt_version,
129 const char *my_mailslot,
130 int dgm_id,
131 struct packet_struct *p)
133 TALLOC_CTX *frame = talloc_stackframe();
134 struct nbt_netlogon_packet packet;
135 struct NETLOGON_SAM_LOGON_REQUEST *s;
136 enum ndr_err_code ndr_err;
137 DATA_BLOB blob = data_blob_null;
138 struct dom_sid my_sid;
139 bool ret = false;
141 ZERO_STRUCT(packet);
142 ZERO_STRUCT(my_sid);
144 if (sid != NULL) {
145 my_sid = *sid;
148 packet.command = LOGON_SAM_LOGON_REQUEST;
149 s = &packet.req.logon;
151 s->request_count = 0;
152 s->computer_name = lp_netbios_name();
153 s->user_name = account_name;
154 s->mailslot_name = my_mailslot;
155 s->acct_control = account_flags;
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 lp_netbios_name(), 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(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 const char *account_name,
304 uint32_t account_flags,
305 uint32_t nt_version)
307 struct tevent_req *req, *subreq;
308 struct nbt_getdc_state *state;
309 uint16_t dgm_id;
310 bool ok;
312 req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
313 if (req == NULL) {
314 return NULL;
316 state->ev = ev;
317 state->msg_ctx = msg_ctx;
318 state->dc_addr = dc_addr;
319 state->domain_name = domain_name;
320 state->sid = sid;
321 state->nt_version = nt_version;
323 if (dc_addr->ss_family != AF_INET) {
324 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
325 return tevent_req_post(req, ev);
327 state->my_mailslot = mailslot_name(
328 state, ((const struct sockaddr_in *)dc_addr)->sin_addr);
329 if (tevent_req_nomem(state->my_mailslot, req)) {
330 return tevent_req_post(req, ev);
332 state->nmbd_pid = pidfile_pid(lp_pid_directory(), "nmbd");
333 if (state->nmbd_pid == 0) {
334 DEBUG(3, ("No nmbd found\n"));
335 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
336 return tevent_req_post(req, ev);
339 generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
341 ok = prep_getdc_request(dc_addr, account_name, account_flags,
342 domain_name, sid, nt_version,
343 state->my_mailslot, dgm_id & 0x7fff,
344 &state->p);
346 if (!ok) {
347 DEBUG(3, ("prep_getdc_request failed\n"));
348 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
349 return tevent_req_post(req, ev);
352 subreq = nb_packet_reader_send(state,
354 global_nmbd_socket_dir(),
355 DGRAM_PACKET,
357 state->my_mailslot);
358 if (tevent_req_nomem(subreq, req)) {
359 return tevent_req_post(req, ev);
361 tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
362 return req;
365 static void nbt_getdc_got_reader(struct tevent_req *subreq)
367 struct tevent_req *req = tevent_req_callback_data(
368 subreq, struct tevent_req);
369 struct nbt_getdc_state *state = tevent_req_data(
370 req, struct nbt_getdc_state);
371 NTSTATUS status;
373 status = nb_packet_reader_recv(subreq, state, &state->reader);
374 TALLOC_FREE(subreq);
375 if (tevent_req_nterror(req, status)) {
376 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
377 nt_errstr(status)));
378 return;
381 status = messaging_send_buf(
382 state->msg_ctx, pid_to_procid(state->nmbd_pid),
383 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
385 if (tevent_req_nterror(req, status)) {
386 DEBUG(10, ("messaging_send_buf returned %s\n",
387 nt_errstr(status)));
388 return;
390 subreq = nb_packet_read_send(state, state->ev, state->reader);
391 if (tevent_req_nomem(subreq, req)) {
392 return;
394 tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
397 static void nbt_getdc_got_response(struct tevent_req *subreq)
399 struct tevent_req *req = tevent_req_callback_data(
400 subreq, struct tevent_req);
401 struct nbt_getdc_state *state = tevent_req_data(
402 req, struct nbt_getdc_state);
403 struct packet_struct *p;
404 NTSTATUS status;
405 bool ret;
407 status = nb_packet_read_recv(subreq, state, &p);
408 TALLOC_FREE(subreq);
409 if (tevent_req_nterror(req, status)) {
410 return;
413 ret = parse_getdc_response(p, state, state->domain_name,
414 &state->nt_version, &state->dc_name,
415 &state->samlogon_response);
416 if (!ret) {
417 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
418 return;
420 tevent_req_done(req);
423 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
424 uint32_t *nt_version, const char **dc_name,
425 struct netlogon_samlogon_response **samlogon_response)
427 struct nbt_getdc_state *state = tevent_req_data(
428 req, struct nbt_getdc_state);
429 NTSTATUS status;
431 if (tevent_req_is_nterror(req, &status)) {
432 return status;
434 if (nt_version != NULL) {
435 *nt_version = state->nt_version;
437 if (dc_name != NULL) {
438 *dc_name = talloc_move(mem_ctx, &state->dc_name);
440 if (samlogon_response != NULL) {
441 *samlogon_response = talloc_move(
442 mem_ctx, &state->samlogon_response);
444 return NT_STATUS_OK;
447 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
448 uint32_t timeout_in_seconds,
449 const struct sockaddr_storage *dc_addr,
450 const char *domain_name,
451 const struct dom_sid *sid,
452 const char *account_name,
453 uint32_t account_flags,
454 uint32_t nt_version,
455 TALLOC_CTX *mem_ctx,
456 uint32_t *pnt_version,
457 const char **dc_name,
458 struct netlogon_samlogon_response **samlogon_response)
460 TALLOC_CTX *frame = talloc_stackframe();
461 struct tevent_context *ev;
462 struct tevent_req *req;
463 NTSTATUS status = NT_STATUS_NO_MEMORY;
465 ev = samba_tevent_context_init(frame);
466 if (ev == NULL) {
467 goto fail;
469 req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
470 sid, account_name, account_flags, nt_version);
471 if (req == NULL) {
472 goto fail;
474 if (!tevent_req_set_endtime(req, ev,
475 timeval_current_ofs(timeout_in_seconds, 0))) {
476 goto fail;
478 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
479 goto fail;
481 status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
482 samlogon_response);
483 fail:
484 TALLOC_FREE(frame);
485 return status;