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"
27 * cli_send_mailslot, send a mailslot for client code ...
30 static bool cli_prep_mailslot(bool unique, const char *mailslot,
33 const char *srcname, int src_type,
34 const char *dstname, int dest_type,
35 const struct sockaddr_storage *dest_ss,
37 struct packet_struct *p)
39 struct dgram_packet *dgram = &p->packet.dgram;
42 char addr[INET6_ADDRSTRLEN];
47 * Next, build the DGRAM ...
50 /* DIRECT GROUP or UNIQUE datagram. */
51 dgram->header.msg_type = unique ? 0x10 : 0x11;
52 dgram->header.flags.node_type = M_NODE;
53 dgram->header.flags.first = True;
54 dgram->header.flags.more = False;
55 dgram->header.dgm_id = dgm_id;
56 /* source ip is filled by nmbd */
57 dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
58 dgram->header.packet_offset = 0;
60 make_nmb_name(&dgram->source_name,srcname,src_type);
61 make_nmb_name(&dgram->dest_name,dstname,dest_type);
63 ptr = &dgram->data[0];
65 /* Setup the smb part. */
66 ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
69 if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
70 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
74 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
77 SCVAL(ptr,smb_com,SMBtrans);
78 SSVAL(ptr,smb_vwv1,len);
79 SSVAL(ptr,smb_vwv11,len);
80 SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
81 SSVAL(ptr,smb_vwv13,3);
82 SSVAL(ptr,smb_vwv14,1);
83 SSVAL(ptr,smb_vwv15,priority);
84 SSVAL(ptr,smb_vwv16,2);
87 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
95 dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
97 p->packet_type = DGRAM_PACKET;
98 p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
99 p->timestamp = time(NULL);
101 DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
102 mailslot, nmb_namestr(&dgram->source_name)));
103 print_sockaddr(addr, sizeof(addr), dest_ss);
105 DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
110 static const char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
112 return talloc_asprintf(mem_ctx, "%s%X",
113 NBT_MAILSLOT_GETDC, dc_ip.s_addr);
116 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
117 const char *domain_name,
118 const struct dom_sid *sid,
121 struct packet_struct *p)
123 TALLOC_CTX *frame = talloc_stackframe();
124 struct in_addr dc_ip;
125 const char *my_acct_name;
126 const char *my_mailslot;
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;
134 if (dc_ss->ss_family != AF_INET) {
145 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
147 my_mailslot = mailslot_name(talloc_tos(), dc_ip);
148 if (my_mailslot == NULL) {
152 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", global_myname());
153 if (my_acct_name == NULL) {
157 packet.command = LOGON_SAM_LOGON_REQUEST;
158 s = &packet.req.logon;
160 s->request_count = 0;
161 s->computer_name = global_myname();
162 s->user_name = my_acct_name;
163 s->mailslot_name = my_mailslot;
164 s->acct_control = ACB_WSTRUST;
166 s->nt_version = nt_version;
167 s->lmnt_token = 0xffff;
168 s->lm20_token = 0xffff;
170 if (DEBUGLEVEL >= 10) {
171 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
174 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
175 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
176 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
180 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
181 (char *)blob.data, blob.length,
182 global_myname(), 0, domain_name, 0x1c,
189 bool send_getdc_request(struct messaging_context *msg_ctx,
190 const struct sockaddr_storage *dc_ss,
191 const char *domain_name,
192 const struct dom_sid *sid,
196 struct packet_struct p;
199 if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
200 DEBUG(3, ("No nmbd found\n"));
204 if (!prep_getdc_request(dc_ss, domain_name, sid, nt_version,
209 return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
210 pid_to_procid(nmbd_pid),
212 (uint8 *)&p, sizeof(p)));
215 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
216 const struct sockaddr_storage *dc_ss,
217 const char *domain_name,
219 uint32_t *nt_version,
220 const char **dc_name,
221 struct netlogon_samlogon_response **samlogon_response)
223 struct packet_struct *packet;
224 const char *my_mailslot = NULL;
225 struct in_addr dc_ip;
227 struct netlogon_samlogon_response *r;
228 union dgram_message_body p;
229 enum ndr_err_code ndr_err;
232 const char *returned_dc = NULL;
233 const char *returned_domain = NULL;
235 if (dc_ss->ss_family != AF_INET) {
239 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
241 my_mailslot = mailslot_name(mem_ctx, dc_ip);
246 packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
248 if (packet == NULL) {
249 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
253 DEBUG(5, ("Received packet for %s\n", my_mailslot));
255 blob = data_blob_const(packet->packet.dgram.data,
256 packet->packet.dgram.datasize);
258 if (blob.length < 4) {
259 DEBUG(0,("invalid length: %d\n", (int)blob.length));
263 if (RIVAL(blob.data,0) != DGRAM_SMB) {
264 DEBUG(0,("invalid packet\n"));
271 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
272 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
273 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
274 DEBUG(0,("failed to parse packet\n"));
278 if (p.smb.smb_command != SMB_TRANSACTION) {
279 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
283 if (DEBUGLEVEL >= 10) {
284 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
287 blob = p.smb.body.trans.data;
289 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
294 status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
295 if (!NT_STATUS_IS_OK(status)) {
300 map_netlogon_samlogon_response(r);
302 /* do we still need this ? */
303 *nt_version = r->ntver;
305 returned_domain = r->data.nt5_ex.domain_name;
306 returned_dc = r->data.nt5_ex.pdc_name;
308 if (!strequal(returned_domain, domain_name)) {
309 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
310 domain_name, returned_domain));
315 *dc_name = talloc_strdup(mem_ctx, returned_dc);
321 if (**dc_name == '\\') *dc_name += 1;
322 if (**dc_name == '\\') *dc_name += 1;
324 if (samlogon_response) {
325 *samlogon_response = r;
330 DEBUG(10, ("GetDC gave name %s for domain %s\n",
331 *dc_name, returned_domain));