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 bool send_getdc_request(TALLOC_CTX *mem_ctx,
117 struct messaging_context *msg_ctx,
118 const struct sockaddr_storage *dc_ss,
119 const char *domain_name,
120 const struct dom_sid *sid,
124 struct in_addr dc_ip;
125 const char *my_acct_name = NULL;
126 const char *my_mailslot = NULL;
127 struct nbt_netlogon_packet packet;
128 struct NETLOGON_SAM_LOGON_REQUEST *s;
129 enum ndr_err_code ndr_err;
131 struct dom_sid my_sid;
132 struct packet_struct p;
135 if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
136 DEBUG(3, ("No nmbd found\n"));
140 if (dc_ss->ss_family != AF_INET) {
151 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
152 my_mailslot = mailslot_name(mem_ctx, dc_ip);
157 my_acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
162 packet.command = LOGON_SAM_LOGON_REQUEST;
163 s = &packet.req.logon;
165 s->request_count = 0;
166 s->computer_name = global_myname();
167 s->user_name = my_acct_name;
168 s->mailslot_name = my_mailslot;
169 s->acct_control = ACB_WSTRUST;
171 s->nt_version = nt_version;
172 s->lmnt_token = 0xffff;
173 s->lm20_token = 0xffff;
175 if (DEBUGLEVEL >= 10) {
176 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
179 ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &packet,
180 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
181 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
185 if (!cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
186 (char *)blob.data, blob.length,
187 global_myname(), 0, domain_name, 0x1c,
188 dc_ss, dgm_id, &p)) {
191 return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
192 pid_to_procid(nmbd_pid),
194 (uint8 *)&p, sizeof(p)));
197 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
198 const struct sockaddr_storage *dc_ss,
199 const char *domain_name,
201 uint32_t *nt_version,
202 const char **dc_name,
203 struct netlogon_samlogon_response **samlogon_response)
205 struct packet_struct *packet;
206 const char *my_mailslot = NULL;
207 struct in_addr dc_ip;
209 struct netlogon_samlogon_response *r;
210 union dgram_message_body p;
211 enum ndr_err_code ndr_err;
214 const char *returned_dc = NULL;
215 const char *returned_domain = NULL;
217 if (dc_ss->ss_family != AF_INET) {
221 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
223 my_mailslot = mailslot_name(mem_ctx, dc_ip);
228 packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
230 if (packet == NULL) {
231 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
235 DEBUG(5, ("Received packet for %s\n", my_mailslot));
237 blob = data_blob_const(packet->packet.dgram.data,
238 packet->packet.dgram.datasize);
240 if (blob.length < 4) {
241 DEBUG(0,("invalid length: %d\n", (int)blob.length));
245 if (RIVAL(blob.data,0) != DGRAM_SMB) {
246 DEBUG(0,("invalid packet\n"));
253 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
254 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
255 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
256 DEBUG(0,("failed to parse packet\n"));
260 if (p.smb.smb_command != SMB_TRANSACTION) {
261 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
265 if (DEBUGLEVEL >= 10) {
266 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
269 blob = p.smb.body.trans.data;
271 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
276 status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
277 if (!NT_STATUS_IS_OK(status)) {
282 map_netlogon_samlogon_response(r);
284 /* do we still need this ? */
285 *nt_version = r->ntver;
287 returned_domain = r->data.nt5_ex.domain_name;
288 returned_dc = r->data.nt5_ex.pdc_name;
290 if (!strequal(returned_domain, domain_name)) {
291 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
292 domain_name, returned_domain));
297 *dc_name = talloc_strdup(mem_ctx, returned_dc);
303 if (**dc_name == '\\') *dc_name += 1;
304 if (**dc_name == '\\') *dc_name += 1;
306 if (samlogon_response) {
307 *samlogon_response = r;
312 DEBUG(10, ("GetDC gave name %s for domain %s\n",
313 *dc_name, returned_domain));