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 "librpc/gen_ndr/messaging.h"
24 #include "libsmb/clidgram.h"
25 #include "libsmb/nmblib.h"
28 * cli_send_mailslot, send a mailslot for client code ...
31 static bool cli_prep_mailslot(bool unique
, const char *mailslot
,
34 const char *srcname
, int src_type
,
35 const char *dstname
, int dest_type
,
36 const struct sockaddr_storage
*dest_ss
,
38 struct packet_struct
*p
)
40 struct dgram_packet
*dgram
= &p
->packet
.dgram
;
43 char addr
[INET6_ADDRSTRLEN
];
48 * Next, build the DGRAM ...
51 /* DIRECT GROUP or UNIQUE datagram. */
52 dgram
->header
.msg_type
= unique
? 0x10 : 0x11;
53 dgram
->header
.flags
.node_type
= M_NODE
;
54 dgram
->header
.flags
.first
= True
;
55 dgram
->header
.flags
.more
= False
;
56 dgram
->header
.dgm_id
= dgm_id
;
57 /* source ip is filled by nmbd */
58 dgram
->header
.dgm_length
= 0; /* Let build_dgram() handle this. */
59 dgram
->header
.packet_offset
= 0;
61 make_nmb_name(&dgram
->source_name
,srcname
,src_type
);
62 make_nmb_name(&dgram
->dest_name
,dstname
,dest_type
);
64 ptr
= &dgram
->data
[0];
66 /* Setup the smb part. */
67 ptr
-= 4; /* XXX Ugliness because of handling of tcp SMB length. */
70 if (smb_size
+ 17*2 + strlen(mailslot
) + 1 + len
> MAX_DGRAM_SIZE
) {
71 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
75 cli_set_message(ptr
,17,strlen(mailslot
) + 1 + len
,True
);
78 SCVAL(ptr
,smb_com
,SMBtrans
);
79 SSVAL(ptr
,smb_vwv1
,len
);
80 SSVAL(ptr
,smb_vwv11
,len
);
81 SSVAL(ptr
,smb_vwv12
,70 + strlen(mailslot
));
82 SSVAL(ptr
,smb_vwv13
,3);
83 SSVAL(ptr
,smb_vwv14
,1);
84 SSVAL(ptr
,smb_vwv15
,priority
);
85 SSVAL(ptr
,smb_vwv16
,2);
88 p2
= skip_string(ptr
,MAX_DGRAM_SIZE
,p2
);
96 dgram
->datasize
= PTR_DIFF(p2
,ptr
+4); /* +4 for tcp length. */
98 p
->packet_type
= DGRAM_PACKET
;
99 p
->ip
= ((const struct sockaddr_in
*)dest_ss
)->sin_addr
;
100 p
->timestamp
= time(NULL
);
102 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
103 mailslot
, nmb_namestr(&dgram
->source_name
)));
104 print_sockaddr(addr
, sizeof(addr
), dest_ss
);
106 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram
->dest_name
), addr
));
111 static char *mailslot_name(TALLOC_CTX
*mem_ctx
, struct in_addr dc_ip
)
113 return talloc_asprintf(mem_ctx
, "%s%X",
114 NBT_MAILSLOT_GETDC
, dc_ip
.s_addr
);
117 static bool prep_getdc_request(const struct sockaddr_storage
*dc_ss
,
118 const char *domain_name
,
119 const struct dom_sid
*sid
,
121 const char *my_mailslot
,
123 struct packet_struct
*p
)
125 TALLOC_CTX
*frame
= talloc_stackframe();
126 const char *my_acct_name
;
127 struct nbt_netlogon_packet packet
;
128 struct NETLOGON_SAM_LOGON_REQUEST
*s
;
129 enum ndr_err_code ndr_err
;
130 DATA_BLOB blob
= data_blob_null
;
131 struct dom_sid my_sid
;
141 my_acct_name
= talloc_asprintf(talloc_tos(), "%s$", global_myname());
142 if (my_acct_name
== NULL
) {
146 packet
.command
= LOGON_SAM_LOGON_REQUEST
;
147 s
= &packet
.req
.logon
;
149 s
->request_count
= 0;
150 s
->computer_name
= global_myname();
151 s
->user_name
= my_acct_name
;
152 s
->mailslot_name
= my_mailslot
;
153 s
->acct_control
= ACB_WSTRUST
;
155 s
->nt_version
= nt_version
;
156 s
->lmnt_token
= 0xffff;
157 s
->lm20_token
= 0xffff;
159 if (DEBUGLEVEL
>= 10) {
160 NDR_PRINT_DEBUG(nbt_netlogon_packet
, &packet
);
163 ndr_err
= ndr_push_struct_blob(&blob
, talloc_tos(), &packet
,
164 (ndr_push_flags_fn_t
)ndr_push_nbt_netlogon_packet
);
165 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
169 ret
= cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON
, 0,
170 (char *)blob
.data
, blob
.length
,
171 global_myname(), 0, domain_name
, 0x1c,
178 static bool parse_getdc_response(
179 struct packet_struct
*packet
,
181 const char *domain_name
,
182 uint32_t *nt_version
,
183 const char **dc_name
,
184 struct netlogon_samlogon_response
**samlogon_response
)
187 struct netlogon_samlogon_response
*r
;
188 union dgram_message_body p
;
189 enum ndr_err_code ndr_err
;
192 const char *returned_dc
= NULL
;
193 const char *returned_domain
= NULL
;
195 blob
= data_blob_const(packet
->packet
.dgram
.data
,
196 packet
->packet
.dgram
.datasize
);
197 if (blob
.length
< 4) {
198 DEBUG(1, ("invalid length: %d\n", (int)blob
.length
));
202 if (RIVAL(blob
.data
,0) != DGRAM_SMB
) {
203 DEBUG(1, ("invalid packet\n"));
210 ndr_err
= ndr_pull_union_blob_all(&blob
, mem_ctx
, &p
, DGRAM_SMB
,
211 (ndr_pull_flags_fn_t
)ndr_pull_dgram_smb_packet
);
212 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
213 DEBUG(1, ("failed to parse packet\n"));
217 if (p
.smb
.smb_command
!= SMB_TRANSACTION
) {
218 DEBUG(1, ("invalid smb_command: %d\n", p
.smb
.smb_command
));
222 if (DEBUGLEVEL
>= 10) {
223 NDR_PRINT_DEBUG(dgram_smb_packet
, &p
);
226 blob
= p
.smb
.body
.trans
.data
;
228 r
= TALLOC_ZERO_P(mem_ctx
, struct netlogon_samlogon_response
);
233 status
= pull_netlogon_samlogon_response(&blob
, r
, r
);
234 if (!NT_STATUS_IS_OK(status
)) {
239 map_netlogon_samlogon_response(r
);
241 /* do we still need this ? */
242 *nt_version
= r
->ntver
;
244 returned_domain
= r
->data
.nt5_ex
.domain_name
;
245 returned_dc
= r
->data
.nt5_ex
.pdc_name
;
247 if (!strequal(returned_domain
, domain_name
)) {
248 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
249 domain_name
, returned_domain
));
254 if (*returned_dc
== '\\') returned_dc
+= 1;
255 if (*returned_dc
== '\\') returned_dc
+= 1;
257 *dc_name
= talloc_strdup(mem_ctx
, returned_dc
);
263 if (samlogon_response
) {
264 *samlogon_response
= r
;
269 DEBUG(10, ("GetDC gave name %s for domain %s\n",
270 *dc_name
, returned_domain
));
275 struct nbt_getdc_state
{
276 struct tevent_context
*ev
;
277 struct messaging_context
*msg_ctx
;
278 struct nb_packet_reader
*reader
;
279 const char *my_mailslot
;
282 const struct sockaddr_storage
*dc_addr
;
283 const char *domain_name
;
284 const struct dom_sid
*sid
;
287 struct netlogon_samlogon_response
*samlogon_response
;
289 struct packet_struct p
;
292 static void nbt_getdc_got_reader(struct tevent_req
*subreq
);
293 static void nbt_getdc_got_response(struct tevent_req
*subreq
);
295 struct tevent_req
*nbt_getdc_send(TALLOC_CTX
*mem_ctx
,
296 struct tevent_context
*ev
,
297 struct messaging_context
*msg_ctx
,
298 const struct sockaddr_storage
*dc_addr
,
299 const char *domain_name
,
300 const struct dom_sid
*sid
,
303 struct tevent_req
*req
, *subreq
;
304 struct nbt_getdc_state
*state
;
307 req
= tevent_req_create(mem_ctx
, &state
, struct nbt_getdc_state
);
312 state
->msg_ctx
= msg_ctx
;
313 state
->dc_addr
= dc_addr
;
314 state
->domain_name
= domain_name
;
316 state
->nt_version
= nt_version
;
318 if (dc_addr
->ss_family
!= AF_INET
) {
319 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
320 return tevent_req_post(req
, ev
);
322 state
->my_mailslot
= mailslot_name(
323 state
, ((struct sockaddr_in
*)dc_addr
)->sin_addr
);
324 if (tevent_req_nomem(state
->my_mailslot
, req
)) {
325 return tevent_req_post(req
, ev
);
327 state
->nmbd_pid
= pidfile_pid("nmbd");
328 if (state
->nmbd_pid
== 0) {
329 DEBUG(3, ("No nmbd found\n"));
330 tevent_req_nterror(req
, NT_STATUS_NOT_SUPPORTED
);
331 return tevent_req_post(req
, ev
);
334 generate_random_buffer((uint8_t *)(void *)&dgm_id
, sizeof(dgm_id
));
336 if (!prep_getdc_request(dc_addr
, domain_name
, sid
, nt_version
,
337 state
->my_mailslot
, dgm_id
& 0x7fff,
339 DEBUG(3, ("prep_getdc_request failed\n"));
340 tevent_req_nterror(req
, NT_STATUS_INVALID_PARAMETER
);
341 return tevent_req_post(req
, ev
);
344 subreq
= nb_packet_reader_send(state
, ev
, DGRAM_PACKET
, -1,
346 if (tevent_req_nomem(subreq
, req
)) {
347 return tevent_req_post(req
, ev
);
349 tevent_req_set_callback(subreq
, nbt_getdc_got_reader
, req
);
353 static void nbt_getdc_got_reader(struct tevent_req
*subreq
)
355 struct tevent_req
*req
= tevent_req_callback_data(
356 subreq
, struct tevent_req
);
357 struct nbt_getdc_state
*state
= tevent_req_data(
358 req
, struct nbt_getdc_state
);
361 status
= nb_packet_reader_recv(subreq
, state
, &state
->reader
);
363 if (tevent_req_nterror(req
, status
)) {
364 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
369 status
= messaging_send_buf(
370 state
->msg_ctx
, pid_to_procid(state
->nmbd_pid
),
371 MSG_SEND_PACKET
, (uint8_t *)&state
->p
, sizeof(state
->p
));
373 if (tevent_req_nterror(req
, status
)) {
374 DEBUG(10, ("messaging_send_buf returned %s\n",
378 subreq
= nb_packet_read_send(state
, state
->ev
, state
->reader
);
379 if (tevent_req_nomem(subreq
, req
)) {
382 tevent_req_set_callback(subreq
, nbt_getdc_got_response
, req
);
385 static void nbt_getdc_got_response(struct tevent_req
*subreq
)
387 struct tevent_req
*req
= tevent_req_callback_data(
388 subreq
, struct tevent_req
);
389 struct nbt_getdc_state
*state
= tevent_req_data(
390 req
, struct nbt_getdc_state
);
391 struct packet_struct
*p
;
395 status
= nb_packet_read_recv(subreq
, &p
);
397 if (tevent_req_nterror(req
, status
)) {
401 ret
= parse_getdc_response(p
, state
, state
->domain_name
,
402 &state
->nt_version
, &state
->dc_name
,
403 &state
->samlogon_response
);
406 tevent_req_nterror(req
, NT_STATUS_INVALID_NETWORK_RESPONSE
);
409 tevent_req_done(req
);
412 NTSTATUS
nbt_getdc_recv(struct tevent_req
*req
, TALLOC_CTX
*mem_ctx
,
413 uint32_t *nt_version
, const char **dc_name
,
414 struct netlogon_samlogon_response
**samlogon_response
)
416 struct nbt_getdc_state
*state
= tevent_req_data(
417 req
, struct nbt_getdc_state
);
420 if (tevent_req_is_nterror(req
, &status
)) {
423 if (nt_version
!= NULL
) {
424 *nt_version
= state
->nt_version
;
426 if (dc_name
!= NULL
) {
427 *dc_name
= talloc_move(mem_ctx
, &state
->dc_name
);
429 if (samlogon_response
!= NULL
) {
430 *samlogon_response
= talloc_move(
431 mem_ctx
, &state
->samlogon_response
);
436 NTSTATUS
nbt_getdc(struct messaging_context
*msg_ctx
,
437 const struct sockaddr_storage
*dc_addr
,
438 const char *domain_name
,
439 const struct dom_sid
*sid
,
442 uint32_t *pnt_version
,
443 const char **dc_name
,
444 struct netlogon_samlogon_response
**samlogon_response
)
446 TALLOC_CTX
*frame
= talloc_stackframe();
447 struct tevent_context
*ev
;
448 struct tevent_req
*req
;
449 NTSTATUS status
= NT_STATUS_NO_MEMORY
;
451 ev
= tevent_context_init(frame
);
455 req
= nbt_getdc_send(ev
, ev
, msg_ctx
, dc_addr
, domain_name
,
460 if (!tevent_req_poll_ntstatus(req
, ev
, &status
)) {
463 status
= nbt_getdc_recv(req
, mem_ctx
, pnt_version
, dc_name
,