2 Unix SMB/CIFS implementation.
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/>.
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"
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
,
40 const char *srcname
, int src_type
,
41 const char *dstname
, int dest_type
,
42 const struct sockaddr_storage
*dest_ss
,
44 struct packet_struct
*p
)
46 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
49 char addr
[INET6_ADDRSTRLEN
];
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. */
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"));
81 cli_set_message(ptr
,17,strlen(mailslot
) + 1 + len
,True
);
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);
94 p2
= skip_string(ptr
,MAX_DGRAM_SIZE
,p2
);
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
));
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
,
129 const char *my_mailslot
,
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
;
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
;
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
)) {
171 ret
= cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON
, 0,
172 (char *)blob
.data
, blob
.length
,
173 lp_netbios_name(), 0, domain_name
, 0x1c,
180 static bool parse_getdc_response(
181 struct packet_struct
*packet
,
183 const char *domain_name
,
184 uint32_t *nt_version
,
185 const char **dc_name
,
186 struct netlogon_samlogon_response
**samlogon_response
)
189 struct netlogon_samlogon_response
*r
;
190 union dgram_message_body p
;
191 enum ndr_err_code ndr_err
;
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
));
204 if (RIVAL(blob
.data
,0) != DGRAM_SMB
) {
205 DEBUG(1, ("invalid packet\n"));
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"));
219 if (p
.smb
.smb_command
!= SMB_TRANSACTION
) {
220 DEBUG(1, ("invalid smb_command: %d\n", p
.smb
.smb_command
));
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
);
235 status
= pull_netlogon_samlogon_response(&blob
, r
, r
);
236 if (!NT_STATUS_IS_OK(status
)) {
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
));
256 if (*returned_dc
== '\\') returned_dc
+= 1;
257 if (*returned_dc
== '\\') returned_dc
+= 1;
259 *dc_name
= talloc_strdup(mem_ctx
, returned_dc
);
265 if (samlogon_response
) {
266 *samlogon_response
= r
;
271 DEBUG(10, ("GetDC gave name %s for domain %s\n",
272 *dc_name
, returned_domain
));
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
;
284 const struct sockaddr_storage
*dc_addr
;
285 const char *domain_name
;
286 const struct dom_sid
*sid
;
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
,
307 struct tevent_req
*req
, *subreq
;
308 struct nbt_getdc_state
*state
;
312 req
= tevent_req_create(mem_ctx
, &state
, struct nbt_getdc_state
);
317 state
->msg_ctx
= msg_ctx
;
318 state
->dc_addr
= dc_addr
;
319 state
->domain_name
= domain_name
;
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,
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
, ev
, DGRAM_PACKET
, -1,
354 if (tevent_req_nomem(subreq
, req
)) {
355 return tevent_req_post(req
, ev
);
357 tevent_req_set_callback(subreq
, nbt_getdc_got_reader
, req
);
361 static void nbt_getdc_got_reader(struct tevent_req
*subreq
)
363 struct tevent_req
*req
= tevent_req_callback_data(
364 subreq
, struct tevent_req
);
365 struct nbt_getdc_state
*state
= tevent_req_data(
366 req
, struct nbt_getdc_state
);
369 status
= nb_packet_reader_recv(subreq
, state
, &state
->reader
);
371 if (tevent_req_nterror(req
, status
)) {
372 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
377 status
= messaging_send_buf(
378 state
->msg_ctx
, pid_to_procid(state
->nmbd_pid
),
379 MSG_SEND_PACKET
, (uint8_t *)&state
->p
, sizeof(state
->p
));
381 if (tevent_req_nterror(req
, status
)) {
382 DEBUG(10, ("messaging_send_buf returned %s\n",
386 subreq
= nb_packet_read_send(state
, state
->ev
, state
->reader
);
387 if (tevent_req_nomem(subreq
, req
)) {
390 tevent_req_set_callback(subreq
, nbt_getdc_got_response
, req
);
393 static void nbt_getdc_got_response(struct tevent_req
*subreq
)
395 struct tevent_req
*req
= tevent_req_callback_data(
396 subreq
, struct tevent_req
);
397 struct nbt_getdc_state
*state
= tevent_req_data(
398 req
, struct nbt_getdc_state
);
399 struct packet_struct
*p
;
403 status
= nb_packet_read_recv(subreq
, state
, &p
);
405 if (tevent_req_nterror(req
, status
)) {
409 ret
= parse_getdc_response(p
, state
, state
->domain_name
,
410 &state
->nt_version
, &state
->dc_name
,
411 &state
->samlogon_response
);
413 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
416 tevent_req_done(req
);
419 NTSTATUS
nbt_getdc_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
420 uint32_t *nt_version
, const char **dc_name
,
421 struct netlogon_samlogon_response
**samlogon_response
)
423 struct nbt_getdc_state
*state
= tevent_req_data(
424 req
, struct nbt_getdc_state
);
427 if (tevent_req_is_nterror(req
, &status
)) {
430 if (nt_version
!= NULL
) {
431 *nt_version
= state
->nt_version
;
433 if (dc_name
!= NULL
) {
434 *dc_name
= talloc_move(mem_ctx
, &state
->dc_name
);
436 if (samlogon_response
!= NULL
) {
437 *samlogon_response
= talloc_move(
438 mem_ctx
, &state
->samlogon_response
);
443 NTSTATUS
nbt_getdc(struct messaging_context
*msg_ctx
,
444 uint32_t timeout_in_seconds
,
445 const struct sockaddr_storage
*dc_addr
,
446 const char *domain_name
,
447 const struct dom_sid
*sid
,
448 const char *account_name
,
449 uint32_t account_flags
,
452 uint32_t *pnt_version
,
453 const char **dc_name
,
454 struct netlogon_samlogon_response
**samlogon_response
)
456 TALLOC_CTX
*frame
= talloc_stackframe();
457 struct tevent_context
*ev
;
458 struct tevent_req
*req
;
459 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
461 ev
= samba_tevent_context_init(frame
);
465 req
= nbt_getdc_send(ev
, ev
, msg_ctx
, dc_addr
, domain_name
,
466 sid
, account_name
, account_flags
, nt_version
);
470 if (!tevent_req_set_endtime(req
, ev
,
471 timeval_current_ofs(timeout_in_seconds
, 0))) {
474 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
477 status
= nbt_getdc_recv(req
, mem_ctx
, pnt_version
, dc_name
,