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/>.
25 * cli_send_mailslot, send a mailslot for client code ...
28 bool cli_send_mailslot(struct messaging_context
*msg_ctx
,
29 bool unique
, const char *mailslot
,
32 const char *srcname
, int src_type
,
33 const char *dstname
, int dest_type
,
34 const struct sockaddr_storage
*dest_ss
)
36 struct packet_struct p
;
37 struct dgram_packet
*dgram
= &p
.packet
.dgram
;
41 char addr
[INET6_ADDRSTRLEN
];
43 if ((nmbd_pid
= pidfile_pid("nmbd")) == 0) {
44 DEBUG(3, ("No nmbd found\n"));
48 if (dest_ss
->ss_family
!= AF_INET
) {
49 DEBUG(3, ("cli_send_mailslot: can't send to IPv6 address.\n"));
53 memset((char *)&p
, '\0', sizeof(p
));
56 * Next, build the DGRAM ...
59 /* DIRECT GROUP or UNIQUE datagram. */
60 dgram
->header
.msg_type
= unique
? 0x10 : 0x11;
61 dgram
->header
.flags
.node_type
= M_NODE
;
62 dgram
->header
.flags
.first
= True
;
63 dgram
->header
.flags
.more
= False
;
64 dgram
->header
.dgm_id
= ((unsigned)time(NULL
)%(unsigned)0x7FFF) +
65 ((unsigned)sys_getpid()%(unsigned)100);
66 /* source ip is filled by nmbd */
67 dgram
->header
.dgm_length
= 0; /* Let build_dgram() handle this. */
68 dgram
->header
.packet_offset
= 0;
70 make_nmb_name(&dgram
->source_name
,srcname
,src_type
);
71 make_nmb_name(&dgram
->dest_name
,dstname
,dest_type
);
73 ptr
= &dgram
->data
[0];
75 /* Setup the smb part. */
76 ptr
-= 4; /* XXX Ugliness because of handling of tcp SMB length. */
79 if (smb_size
+ 17*2 + strlen(mailslot
) + 1 + len
> MAX_DGRAM_SIZE
) {
80 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
84 cli_set_message(ptr
,17,strlen(mailslot
) + 1 + len
,True
);
87 SCVAL(ptr
,smb_com
,SMBtrans
);
88 SSVAL(ptr
,smb_vwv1
,len
);
89 SSVAL(ptr
,smb_vwv11
,len
);
90 SSVAL(ptr
,smb_vwv12
,70 + strlen(mailslot
));
91 SSVAL(ptr
,smb_vwv13
,3);
92 SSVAL(ptr
,smb_vwv14
,1);
93 SSVAL(ptr
,smb_vwv15
,priority
);
94 SSVAL(ptr
,smb_vwv16
,2);
97 p2
= skip_string(ptr
,MAX_DGRAM_SIZE
,p2
);
105 dgram
->datasize
= PTR_DIFF(p2
,ptr
+4); /* +4 for tcp length. */
107 p
.packet_type
= DGRAM_PACKET
;
108 p
.ip
= ((const struct sockaddr_in
*)dest_ss
)->sin_addr
;
109 p
.timestamp
= time(NULL
);
111 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
112 mailslot
, nmb_namestr(&dgram
->source_name
)));
113 print_sockaddr(addr
, sizeof(addr
), dest_ss
);
115 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram
->dest_name
), addr
));
117 return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx
,
118 pid_to_procid(nmbd_pid
),
120 (uint8
*)&p
, sizeof(p
)));
123 static const char *mailslot_name(TALLOC_CTX
*mem_ctx
, struct in_addr dc_ip
)
125 return talloc_asprintf(mem_ctx
, "%s%X",
126 NBT_MAILSLOT_GETDC
, dc_ip
.s_addr
);
129 bool send_getdc_request(TALLOC_CTX
*mem_ctx
,
130 struct messaging_context
*msg_ctx
,
131 struct sockaddr_storage
*dc_ss
,
132 const char *domain_name
,
136 struct in_addr dc_ip
;
137 const char *my_acct_name
= NULL
;
138 const char *my_mailslot
= NULL
;
139 struct nbt_ntlogon_packet packet
;
140 struct nbt_ntlogon_sam_logon
*s
;
141 enum ndr_err_code ndr_err
;
143 struct dom_sid my_sid
;
148 if (dc_ss
->ss_family
!= AF_INET
) {
156 dc_ip
= ((struct sockaddr_in
*)dc_ss
)->sin_addr
;
157 my_mailslot
= mailslot_name(mem_ctx
, dc_ip
);
162 my_acct_name
= talloc_asprintf(mem_ctx
, "%s$", global_myname());
167 packet
.command
= NTLOGON_SAM_LOGON
;
168 s
= &packet
.req
.logon
;
170 s
->request_count
= 0;
171 s
->computer_name
= global_myname();
172 s
->user_name
= my_acct_name
;
173 s
->mailslot_name
= my_mailslot
;
174 s
->acct_control
= ACB_WSTRUST
;
176 s
->nt_version
= nt_version
;
177 s
->lmnt_token
= 0xffff;
178 s
->lm20_token
= 0xffff;
180 if (DEBUGLEVEL
>= 10) {
181 NDR_PRINT_DEBUG(nbt_ntlogon_packet
, &packet
);
184 ndr_err
= ndr_push_struct_blob(&blob
, mem_ctx
, &packet
,
185 (ndr_push_flags_fn_t
)ndr_push_nbt_ntlogon_packet
);
186 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
190 return cli_send_mailslot(msg_ctx
,
191 false, NBT_MAILSLOT_NTLOGON
, 0,
192 (char *)blob
.data
, blob
.length
,
193 global_myname(), 0, domain_name
, 0x1c,
197 bool receive_getdc_response(TALLOC_CTX
*mem_ctx
,
198 struct sockaddr_storage
*dc_ss
,
199 const char *domain_name
,
200 uint32_t *nt_version
,
201 const char **dc_name
,
202 union nbt_cldap_netlogon
**reply
)
204 struct packet_struct
*packet
;
205 const char *my_mailslot
= NULL
;
206 struct in_addr dc_ip
;
208 union nbt_cldap_netlogon r
;
209 union dgram_message_body p
;
210 enum ndr_err_code ndr_err
;
212 const char *returned_dc
= NULL
;
213 const char *returned_domain
= NULL
;
215 if (dc_ss
->ss_family
!= AF_INET
) {
219 dc_ip
= ((struct sockaddr_in
*)dc_ss
)->sin_addr
;
221 my_mailslot
= mailslot_name(mem_ctx
, dc_ip
);
226 packet
= receive_unexpected(DGRAM_PACKET
, 0, my_mailslot
);
228 if (packet
== NULL
) {
229 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot
));
233 DEBUG(5, ("Received packet for %s\n", my_mailslot
));
235 blob
= data_blob_const(packet
->packet
.dgram
.data
,
236 packet
->packet
.dgram
.datasize
);
238 if (blob
.length
< 4) {
239 DEBUG(0,("invalid length: %d\n", (int)blob
.length
));
243 if (RIVAL(blob
.data
,0) != DGRAM_SMB
) {
244 DEBUG(0,("invalid packet\n"));
251 ndr_err
= ndr_pull_union_blob_all(&blob
, mem_ctx
, &p
, DGRAM_SMB
,
252 (ndr_pull_flags_fn_t
)ndr_pull_dgram_smb_packet
);
253 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err
)) {
254 DEBUG(0,("failed to parse packet\n"));
258 if (p
.smb
.smb_command
!= SMB_TRANSACTION
) {
259 DEBUG(0,("invalid smb_command: %d\n", p
.smb
.smb_command
));
263 if (DEBUGLEVEL
>= 10) {
264 NDR_PRINT_DEBUG(dgram_smb_packet
, &p
);
267 blob
= p
.smb
.body
.trans
.data
;
269 if (!pull_mailslot_cldap_reply(mem_ctx
, &blob
,
275 switch (*nt_version
) {
280 returned_domain
= r
.logon1
.domain_name
;
281 returned_dc
= r
.logon1
.pdc_name
;
287 returned_domain
= r
.logon3
.domain_name
;
288 returned_dc
= r
.logon3
.pdc_name
;
294 returned_domain
= r
.logon5
.domain
;
295 returned_dc
= r
.logon5
.pdc_name
;
305 returned_domain
= r
.logon13
.domain
;
306 returned_dc
= r
.logon13
.pdc_name
;
317 returned_domain
= r
.logon15
.domain
;
318 returned_dc
= r
.logon15
.pdc_name
;
323 returned_domain
= r
.logon29
.domain
;
324 returned_dc
= r
.logon29
.pdc_name
;
330 if (!strequal(returned_domain
, domain_name
)) {
331 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
332 domain_name
, returned_domain
));
336 *dc_name
= talloc_strdup(mem_ctx
, returned_dc
);
341 if (**dc_name
== '\\') *dc_name
+= 1;
342 if (**dc_name
== '\\') *dc_name
+= 1;
345 *reply
= (union nbt_cldap_netlogon
*)talloc_memdup(
346 mem_ctx
, &r
, sizeof(union nbt_cldap_netlogon
));
352 DEBUG(10, ("GetDC gave name %s for domain %s\n",
353 *dc_name
, returned_domain
));