s3: Lift my_mailslot from prep_getdc_request
[Samba/gbeck.git] / source3 / libsmb / clidgram.c
blob734564dcbb5b349b2a6d7f6dfecc28634cc39659
1 /*
2 Unix SMB/CIFS implementation.
3 client dgram calls
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/>.
22 #include "includes.h"
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,
31 uint16 priority,
32 char *buf, int len,
33 const char *srcname, int src_type,
34 const char *dstname, int dest_type,
35 const struct sockaddr_storage *dest_ss,
36 int dgm_id,
37 struct packet_struct *p)
39 struct dgram_packet *dgram = &p->packet.dgram;
40 char *ptr, *p2;
41 char tmp[4];
42 char addr[INET6_ADDRSTRLEN];
44 ZERO_STRUCTP(p);
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. */
67 memcpy(tmp,ptr,4);
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"));
71 return False;
74 cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
75 memcpy(ptr,tmp,4);
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);
85 p2 = smb_buf(ptr);
86 fstrcpy(p2,mailslot);
87 p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
88 if (!p2) {
89 return False;
92 memcpy(p2,buf,len);
93 p2 += len;
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));
107 return true;
110 static 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,
119 uint32_t nt_version,
120 const char *my_mailslot,
121 int dgm_id,
122 struct packet_struct *p)
124 TALLOC_CTX *frame = talloc_stackframe();
125 const char *my_acct_name;
126 struct nbt_netlogon_packet packet;
127 struct NETLOGON_SAM_LOGON_REQUEST *s;
128 enum ndr_err_code ndr_err;
129 DATA_BLOB blob = data_blob_null;
130 struct dom_sid my_sid;
131 bool ret = false;
133 ZERO_STRUCT(packet);
134 ZERO_STRUCT(my_sid);
136 if (sid != NULL) {
137 my_sid = *sid;
140 my_acct_name = talloc_asprintf(talloc_tos(), "%s$", global_myname());
141 if (my_acct_name == NULL) {
142 goto fail;
145 packet.command = LOGON_SAM_LOGON_REQUEST;
146 s = &packet.req.logon;
148 s->request_count = 0;
149 s->computer_name = global_myname();
150 s->user_name = my_acct_name;
151 s->mailslot_name = my_mailslot;
152 s->acct_control = ACB_WSTRUST;
153 s->sid = my_sid;
154 s->nt_version = nt_version;
155 s->lmnt_token = 0xffff;
156 s->lm20_token = 0xffff;
158 if (DEBUGLEVEL >= 10) {
159 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
162 ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
163 (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
164 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
165 goto fail;
168 ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
169 (char *)blob.data, blob.length,
170 global_myname(), 0, domain_name, 0x1c,
171 dc_ss, dgm_id, p);
172 fail:
173 TALLOC_FREE(frame);
174 return ret;
177 bool send_getdc_request(struct messaging_context *msg_ctx,
178 const struct sockaddr_storage *dc_ss,
179 const char *domain_name,
180 const struct dom_sid *sid,
181 uint32_t nt_version,
182 int dgm_id)
184 struct packet_struct p;
185 pid_t nmbd_pid;
186 char *my_mailslot;
187 struct in_addr dc_ip;
188 NTSTATUS status;
190 if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
191 DEBUG(3, ("No nmbd found\n"));
192 return False;
195 if (dc_ss->ss_family != AF_INET) {
196 return false;
198 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
200 my_mailslot = mailslot_name(talloc_tos(), dc_ip);
201 if (my_mailslot == NULL) {
202 return NULL;
205 if (!prep_getdc_request(dc_ss, domain_name, sid, nt_version,
206 my_mailslot, dgm_id, &p)) {
207 TALLOC_FREE(my_mailslot);
208 return false;
211 status = messaging_send_buf(msg_ctx, pid_to_procid(nmbd_pid),
212 MSG_SEND_PACKET,
213 (uint8 *)&p, sizeof(p));
214 TALLOC_FREE(my_mailslot);
216 return NT_STATUS_IS_OK(status);
219 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
220 const struct sockaddr_storage *dc_ss,
221 const char *domain_name,
222 int dgm_id,
223 uint32_t *nt_version,
224 const char **dc_name,
225 struct netlogon_samlogon_response **samlogon_response)
227 struct packet_struct *packet = NULL;
228 char *my_mailslot = NULL;
229 struct in_addr dc_ip;
230 DATA_BLOB blob;
231 struct netlogon_samlogon_response *r = NULL;
232 union dgram_message_body p;
233 enum ndr_err_code ndr_err;
234 NTSTATUS status;
236 const char *returned_dc = NULL;
237 const char *returned_domain = NULL;
239 if (dc_ss->ss_family != AF_INET) {
240 return false;
243 dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
245 my_mailslot = mailslot_name(mem_ctx, dc_ip);
246 if (!my_mailslot) {
247 return false;
250 packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
252 if (packet == NULL) {
253 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
254 return False;
257 DEBUG(5, ("Received packet for %s\n", my_mailslot));
259 blob = data_blob_const(packet->packet.dgram.data,
260 packet->packet.dgram.datasize);
262 if (blob.length < 4) {
263 DEBUG(0,("invalid length: %d\n", (int)blob.length));
264 goto fail;
267 if (RIVAL(blob.data,0) != DGRAM_SMB) {
268 DEBUG(0,("invalid packet\n"));
269 goto fail;
272 blob.data += 4;
273 blob.length -= 4;
275 ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
276 (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
277 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
278 DEBUG(0,("failed to parse packet\n"));
279 goto fail;
282 if (p.smb.smb_command != SMB_TRANSACTION) {
283 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
284 goto fail;
287 if (DEBUGLEVEL >= 10) {
288 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
291 blob = p.smb.body.trans.data;
293 r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
294 if (!r) {
295 goto fail;
298 status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
299 if (!NT_STATUS_IS_OK(status)) {
300 goto fail;
303 map_netlogon_samlogon_response(r);
305 /* do we still need this ? */
306 *nt_version = r->ntver;
308 returned_domain = r->data.nt5_ex.domain_name;
309 returned_dc = r->data.nt5_ex.pdc_name;
311 if (!strequal(returned_domain, domain_name)) {
312 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
313 domain_name, returned_domain));
314 goto fail;
317 *dc_name = talloc_strdup(mem_ctx, returned_dc);
318 if (!*dc_name) {
319 goto fail;
322 if (**dc_name == '\\') *dc_name += 1;
323 if (**dc_name == '\\') *dc_name += 1;
325 if (samlogon_response) {
326 *samlogon_response = r;
327 } else {
328 TALLOC_FREE(r);
331 DEBUG(10, ("GetDC gave name %s for domain %s\n",
332 *dc_name, returned_domain));
334 free_packet(packet);
335 TALLOC_FREE(my_mailslot);
336 return True;
338 fail:
339 TALLOC_FREE(my_mailslot);
340 TALLOC_FREE(r);
341 if (packet != NULL) {
342 free_packet(packet);
344 return false;