s3: Remove unused mem_ctx arg from set_getdc_request
[metze/samba/wip.git] / source3 / libsmb / clidgram.c
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
7
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.
12
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.
17
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/>.
20 */
21
22 #include "includes.h"
23 #include "librpc/gen_ndr/messaging.h"
24 #include "libsmb/clidgram.h"
25
26 /*
27  * cli_send_mailslot, send a mailslot for client code ...
28  */
29
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)
38 {
39         struct dgram_packet *dgram = &p->packet.dgram;
40         char *ptr, *p2;
41         char tmp[4];
42         char addr[INET6_ADDRSTRLEN];
43
44         ZERO_STRUCTP(p);
45
46         /*
47          * Next, build the DGRAM ...
48          */
49
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;
59
60         make_nmb_name(&dgram->source_name,srcname,src_type);
61         make_nmb_name(&dgram->dest_name,dstname,dest_type);
62
63         ptr = &dgram->data[0];
64
65         /* Setup the smb part. */
66         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
67         memcpy(tmp,ptr,4);
68
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;
72         }
73
74         cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
75         memcpy(ptr,tmp,4);
76
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;
90         }
91
92         memcpy(p2,buf,len);
93         p2 += len;
94
95         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
96
97         p->packet_type = DGRAM_PACKET;
98         p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
99         p->timestamp = time(NULL);
100
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);
104
105         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
106
107         return true;
108 }
109
110 static const char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
111 {
112         return talloc_asprintf(mem_ctx, "%s%X",
113                                NBT_MAILSLOT_GETDC, dc_ip.s_addr);
114 }
115
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                                int dgm_id,
121                                struct packet_struct *p)
122 {
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;
132         bool ret = false;
133
134         if (dc_ss->ss_family != AF_INET) {
135                 goto fail;
136         }
137
138         ZERO_STRUCT(packet);
139         ZERO_STRUCT(my_sid);
140
141         if (sid != NULL) {
142                 my_sid = *sid;
143         }
144
145         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
146
147         my_mailslot = mailslot_name(talloc_tos(), dc_ip);
148         if (my_mailslot == NULL) {
149                 goto fail;
150         }
151
152         my_acct_name = talloc_asprintf(talloc_tos(), "%s$", global_myname());
153         if (my_acct_name == NULL) {
154                 goto fail;
155         }
156
157         packet.command  = LOGON_SAM_LOGON_REQUEST;
158         s               = &packet.req.logon;
159
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;
165         s->sid                  = my_sid;
166         s->nt_version           = nt_version;
167         s->lmnt_token           = 0xffff;
168         s->lm20_token           = 0xffff;
169
170         if (DEBUGLEVEL >= 10) {
171                 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
172         }
173
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)) {
177                 goto fail;
178         }
179
180         ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
181                                 (char *)blob.data, blob.length,
182                                 global_myname(), 0, domain_name, 0x1c,
183                                 dc_ss, dgm_id, p);
184 fail:
185         TALLOC_FREE(frame);
186         return ret;
187 }
188
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,
193                         uint32_t nt_version,
194                         int dgm_id)
195 {
196         struct packet_struct p;
197         pid_t nmbd_pid;
198
199         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
200                 DEBUG(3, ("No nmbd found\n"));
201                 return False;
202         }
203
204         if (!prep_getdc_request(dc_ss, domain_name, sid, nt_version,
205                                 dgm_id, &p)) {
206                 return false;
207         }
208
209         return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
210                                                   pid_to_procid(nmbd_pid),
211                                                   MSG_SEND_PACKET,
212                                                   (uint8 *)&p, sizeof(p)));
213 }
214
215 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
216                             const struct sockaddr_storage *dc_ss,
217                             const char *domain_name,
218                             int dgm_id,
219                             uint32_t *nt_version,
220                             const char **dc_name,
221                             struct netlogon_samlogon_response **samlogon_response)
222 {
223         struct packet_struct *packet;
224         const char *my_mailslot = NULL;
225         struct in_addr dc_ip;
226         DATA_BLOB blob;
227         struct netlogon_samlogon_response *r;
228         union dgram_message_body p;
229         enum ndr_err_code ndr_err;
230         NTSTATUS status;
231
232         const char *returned_dc = NULL;
233         const char *returned_domain = NULL;
234
235         if (dc_ss->ss_family != AF_INET) {
236                 return false;
237         }
238
239         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
240
241         my_mailslot = mailslot_name(mem_ctx, dc_ip);
242         if (!my_mailslot) {
243                 return false;
244         }
245
246         packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
247
248         if (packet == NULL) {
249                 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
250                 return False;
251         }
252
253         DEBUG(5, ("Received packet for %s\n", my_mailslot));
254
255         blob = data_blob_const(packet->packet.dgram.data,
256                                packet->packet.dgram.datasize);
257
258         if (blob.length < 4) {
259                 DEBUG(0,("invalid length: %d\n", (int)blob.length));
260                 return false;
261         }
262
263         if (RIVAL(blob.data,0) != DGRAM_SMB) {
264                 DEBUG(0,("invalid packet\n"));
265                 return false;
266         }
267
268         blob.data += 4;
269         blob.length -= 4;
270
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"));
275                 return false;
276         }
277
278         if (p.smb.smb_command != SMB_TRANSACTION) {
279                 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
280                 return false;
281         }
282
283         if (DEBUGLEVEL >= 10) {
284                 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
285         }
286
287         blob = p.smb.body.trans.data;
288
289         r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
290         if (!r) {
291                 return false;
292         }
293
294         status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
295         if (!NT_STATUS_IS_OK(status)) {
296                 TALLOC_FREE(r);
297                 return false;
298         }
299
300         map_netlogon_samlogon_response(r);
301
302         /* do we still need this ? */
303         *nt_version = r->ntver;
304
305         returned_domain = r->data.nt5_ex.domain_name;
306         returned_dc = r->data.nt5_ex.pdc_name;
307
308         if (!strequal(returned_domain, domain_name)) {
309                 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
310                           domain_name, returned_domain));
311                 TALLOC_FREE(r);
312                 return false;
313         }
314
315         *dc_name = talloc_strdup(mem_ctx, returned_dc);
316         if (!*dc_name) {
317                 TALLOC_FREE(r);
318                 return false;
319         }
320
321         if (**dc_name == '\\')  *dc_name += 1;
322         if (**dc_name == '\\')  *dc_name += 1;
323
324         if (samlogon_response) {
325                 *samlogon_response = r;
326         } else {
327                 TALLOC_FREE(r);
328         }
329
330         DEBUG(10, ("GetDC gave name %s for domain %s\n",
331                    *dc_name, returned_domain));
332
333         return True;
334 }