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"
30 * cli_send_mailslot, send a mailslot for client code ...
33 static bool cli_prep_mailslot(bool unique
, const char *mailslot
,
36 const char *srcname
, int src_type
,
37 const char *dstname
, int dest_type
,
38 const struct sockaddr_storage
*dest_ss
,
40 struct packet_struct
*p
)
42 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
45 char addr
[INET6_ADDRSTRLEN
];
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. */
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"));
77 cli_set_message(ptr
,17,strlen(mailslot
) + 1 + len
,True
);
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);
90 p2
= skip_string(ptr
,MAX_DGRAM_SIZE
,p2
);
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
));
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
,
123 const char *my_mailslot
,
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
;
143 my_acct_name
= talloc_asprintf(talloc_tos(), "%s$", global_myname());
144 if (my_acct_name
== NULL
) {
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
;
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 global_myname(), 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_P(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
,
305 struct tevent_req
*req
, *subreq
;
306 struct nbt_getdc_state
*state
;
309 req
= tevent_req_create(mem_ctx
, &state
, struct nbt_getdc_state
);
314 state
->msg_ctx
= msg_ctx
;
315 state
->dc_addr
= dc_addr
;
316 state
->domain_name
= domain_name
;
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,
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,
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
);
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
);
363 status
= nb_packet_reader_recv(subreq
, state
, &state
->reader
);
365 if (tevent_req_nterror(req
, status
)) {
366 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
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",
380 subreq
= nb_packet_read_send(state
, state
->ev
, state
->reader
);
381 if (tevent_req_nomem(subreq
, req
)) {
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
;
397 status
= nb_packet_read_recv(subreq
, &p
);
399 if (tevent_req_nterror(req
, status
)) {
403 ret
= parse_getdc_response(p
, state
, state
->domain_name
,
404 &state
->nt_version
, &state
->dc_name
,
405 &state
->samlogon_response
);
408 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
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
);
422 if (tevent_req_is_nterror(req
, &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
);
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
,
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
);
457 req
= nbt_getdc_send(ev
, ev
, msg_ctx
, dc_addr
, domain_name
,
462 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
465 status
= nbt_getdc_recv(req
, mem_ctx
, pnt_version
, dc_name
,