4ae57a30d33d0972c9871ae13cebca2377b2c993
[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 "libsmb/libsmb.h"
24 #include "../lib/util/tevent_ntstatus.h"
25 #include "libsmb/clidgram.h"
26 #include "libsmb/nmblib.h"
27 #include "libsmb/unexpected.h"
28 #include "messages.h"
29 #include "librpc/gen_ndr/samr.h"
30 #include "../lib/util/pidfile.h"
31
32 /*
33  * cli_send_mailslot, send a mailslot for client code ...
34  */
35
36 static bool cli_prep_mailslot(bool unique, const char *mailslot,
37                        uint16_t priority,
38                        char *buf, int len,
39                        const char *srcname, int src_type,
40                        const char *dstname, int dest_type,
41                        const struct sockaddr_storage *dest_ss,
42                        int dgm_id,
43                        struct packet_struct *p)
44 {
45         struct dgram_packet *dgram = &p->packet.dgram;
46         char *ptr, *p2;
47         char tmp[4];
48         char addr[INET6_ADDRSTRLEN];
49
50         ZERO_STRUCTP(p);
51
52         /*
53          * Next, build the DGRAM ...
54          */
55
56         /* DIRECT GROUP or UNIQUE datagram. */
57         dgram->header.msg_type = unique ? 0x10 : 0x11;
58         dgram->header.flags.node_type = M_NODE;
59         dgram->header.flags.first = True;
60         dgram->header.flags.more = False;
61         dgram->header.dgm_id = dgm_id;
62         /* source ip is filled by nmbd */
63         dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
64         dgram->header.packet_offset = 0;
65
66         make_nmb_name(&dgram->source_name,srcname,src_type);
67         make_nmb_name(&dgram->dest_name,dstname,dest_type);
68
69         ptr = &dgram->data[0];
70
71         /* Setup the smb part. */
72         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
73         memcpy(tmp,ptr,4);
74
75         if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
76                 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
77                 return False;
78         }
79
80         cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
81         memcpy(ptr,tmp,4);
82
83         SCVAL(ptr,smb_com,SMBtrans);
84         SSVAL(ptr,smb_vwv1,len);
85         SSVAL(ptr,smb_vwv11,len);
86         SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
87         SSVAL(ptr,smb_vwv13,3);
88         SSVAL(ptr,smb_vwv14,1);
89         SSVAL(ptr,smb_vwv15,priority);
90         SSVAL(ptr,smb_vwv16,2);
91         p2 = smb_buf(ptr);
92         fstrcpy(p2,mailslot);
93         p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
94         if (!p2) {
95                 return False;
96         }
97
98         memcpy(p2,buf,len);
99         p2 += len;
100
101         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
102
103         p->packet_type = DGRAM_PACKET;
104         p->ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
105         p->timestamp = time(NULL);
106
107         DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
108                  mailslot, nmb_namestr(&dgram->source_name)));
109         print_sockaddr(addr, sizeof(addr), dest_ss);
110
111         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
112
113         return true;
114 }
115
116 static char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
117 {
118         return talloc_asprintf(mem_ctx, "%s%X",
119                                NBT_MAILSLOT_GETDC, dc_ip.s_addr);
120 }
121
122 static bool prep_getdc_request(const struct sockaddr_storage *dc_ss,
123                                const char *domain_name,
124                                const struct dom_sid *sid,
125                                uint32_t nt_version,
126                                const char *my_mailslot,
127                                int dgm_id,
128                                struct packet_struct *p)
129 {
130         TALLOC_CTX *frame = talloc_stackframe();
131         const char *my_acct_name;
132         struct nbt_netlogon_packet packet;
133         struct NETLOGON_SAM_LOGON_REQUEST *s;
134         enum ndr_err_code ndr_err;
135         DATA_BLOB blob = data_blob_null;
136         struct dom_sid my_sid;
137         bool ret = false;
138
139         ZERO_STRUCT(packet);
140         ZERO_STRUCT(my_sid);
141
142         if (sid != NULL) {
143                 my_sid = *sid;
144         }
145
146         my_acct_name = talloc_asprintf(talloc_tos(), "%s$", lp_netbios_name());
147         if (my_acct_name == NULL) {
148                 goto fail;
149         }
150
151         packet.command  = LOGON_SAM_LOGON_REQUEST;
152         s               = &packet.req.logon;
153
154         s->request_count        = 0;
155         s->computer_name        = lp_netbios_name();
156         s->user_name            = my_acct_name;
157         s->mailslot_name        = my_mailslot;
158         s->acct_control         = ACB_WSTRUST;
159         s->sid                  = my_sid;
160         s->nt_version           = nt_version;
161         s->lmnt_token           = 0xffff;
162         s->lm20_token           = 0xffff;
163
164         if (DEBUGLEVEL >= 10) {
165                 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
166         }
167
168         ndr_err = ndr_push_struct_blob(&blob, talloc_tos(), &packet,
169                        (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
170         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
171                 goto fail;
172         }
173
174         ret = cli_prep_mailslot(false, NBT_MAILSLOT_NTLOGON, 0,
175                                 (char *)blob.data, blob.length,
176                                 lp_netbios_name(), 0, domain_name, 0x1c,
177                                 dc_ss, dgm_id, p);
178 fail:
179         TALLOC_FREE(frame);
180         return ret;
181 }
182
183 static bool parse_getdc_response(
184         struct packet_struct *packet,
185         TALLOC_CTX *mem_ctx,
186         const char *domain_name,
187         uint32_t *nt_version,
188         const char **dc_name,
189         struct netlogon_samlogon_response **samlogon_response)
190 {
191         DATA_BLOB blob;
192         struct netlogon_samlogon_response *r;
193         union dgram_message_body p;
194         enum ndr_err_code ndr_err;
195         NTSTATUS status;
196
197         const char *returned_dc = NULL;
198         const char *returned_domain = NULL;
199
200         blob = data_blob_const(packet->packet.dgram.data,
201                                packet->packet.dgram.datasize);
202         if (blob.length < 4) {
203                 DEBUG(1, ("invalid length: %d\n", (int)blob.length));
204                 return false;
205         }
206
207         if (RIVAL(blob.data,0) != DGRAM_SMB) {
208                 DEBUG(1, ("invalid packet\n"));
209                 return false;
210         }
211
212         blob.data += 4;
213         blob.length -= 4;
214
215         ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
216                        (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
217         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
218                 DEBUG(1, ("failed to parse packet\n"));
219                 return false;
220         }
221
222         if (p.smb.smb_command != SMB_TRANSACTION) {
223                 DEBUG(1, ("invalid smb_command: %d\n", p.smb.smb_command));
224                 return false;
225         }
226
227         if (DEBUGLEVEL >= 10) {
228                 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
229         }
230
231         blob = p.smb.body.trans.data;
232
233         r = talloc_zero(mem_ctx, struct netlogon_samlogon_response);
234         if (!r) {
235                 return false;
236         }
237
238         status = pull_netlogon_samlogon_response(&blob, r, r);
239         if (!NT_STATUS_IS_OK(status)) {
240                 TALLOC_FREE(r);
241                 return false;
242         }
243
244         map_netlogon_samlogon_response(r);
245
246         /* do we still need this ? */
247         *nt_version = r->ntver;
248
249         returned_domain = r->data.nt5_ex.domain_name;
250         returned_dc = r->data.nt5_ex.pdc_name;
251
252         if (!strequal(returned_domain, domain_name)) {
253                 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
254                           domain_name, returned_domain));
255                 TALLOC_FREE(r);
256                 return false;
257         }
258
259         if (*returned_dc == '\\') returned_dc += 1;
260         if (*returned_dc == '\\') returned_dc += 1;
261
262         *dc_name = talloc_strdup(mem_ctx, returned_dc);
263         if (!*dc_name) {
264                 TALLOC_FREE(r);
265                 return false;
266         }
267
268         if (samlogon_response) {
269                 *samlogon_response = r;
270         } else {
271                 TALLOC_FREE(r);
272         }
273
274         DEBUG(10, ("GetDC gave name %s for domain %s\n",
275                    *dc_name, returned_domain));
276
277         return True;
278 }
279
280 struct nbt_getdc_state {
281         struct tevent_context *ev;
282         struct messaging_context *msg_ctx;
283         struct nb_packet_reader *reader;
284         const char *my_mailslot;
285         pid_t nmbd_pid;
286
287         const struct sockaddr_storage *dc_addr;
288         const char *domain_name;
289         const struct dom_sid *sid;
290         uint32_t nt_version;
291         const char *dc_name;
292         struct netlogon_samlogon_response *samlogon_response;
293
294         struct packet_struct p;
295 };
296
297 static void nbt_getdc_got_reader(struct tevent_req *subreq);
298 static void nbt_getdc_got_response(struct tevent_req *subreq);
299
300 struct tevent_req *nbt_getdc_send(TALLOC_CTX *mem_ctx,
301                                   struct tevent_context *ev,
302                                   struct messaging_context *msg_ctx,
303                                   const struct sockaddr_storage *dc_addr,
304                                   const char *domain_name,
305                                   const struct dom_sid *sid,
306                                   uint32_t nt_version)
307 {
308         struct tevent_req *req, *subreq;
309         struct nbt_getdc_state *state;
310         uint16_t dgm_id;
311
312         req = tevent_req_create(mem_ctx, &state, struct nbt_getdc_state);
313         if (req == NULL) {
314                 return NULL;
315         }
316         state->ev = ev;
317         state->msg_ctx = msg_ctx;
318         state->dc_addr = dc_addr;
319         state->domain_name = domain_name;
320         state->sid = sid;
321         state->nt_version = nt_version;
322
323         if (dc_addr->ss_family != AF_INET) {
324                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
325                 return tevent_req_post(req, ev);
326         }
327         state->my_mailslot = mailslot_name(
328                 state, ((const struct sockaddr_in *)dc_addr)->sin_addr);
329         if (tevent_req_nomem(state->my_mailslot, req)) {
330                 return tevent_req_post(req, ev);
331         }
332         state->nmbd_pid = pidfile_pid(lp_pid_directory(), "nmbd");
333         if (state->nmbd_pid == 0) {
334                 DEBUG(3, ("No nmbd found\n"));
335                 tevent_req_nterror(req, NT_STATUS_NOT_SUPPORTED);
336                 return tevent_req_post(req, ev);
337         }
338
339         generate_random_buffer((uint8_t *)(void *)&dgm_id, sizeof(dgm_id));
340
341         if (!prep_getdc_request(dc_addr, domain_name, sid, nt_version,
342                                 state->my_mailslot, dgm_id & 0x7fff,
343                                 &state->p)) {
344                 DEBUG(3, ("prep_getdc_request failed\n"));
345                 tevent_req_nterror(req, NT_STATUS_INVALID_PARAMETER);
346                 return tevent_req_post(req, ev);
347         }
348
349         subreq = nb_packet_reader_send(state, ev, DGRAM_PACKET, -1,
350                                        state->my_mailslot);
351         if (tevent_req_nomem(subreq, req)) {
352                 return tevent_req_post(req, ev);
353         }
354         tevent_req_set_callback(subreq, nbt_getdc_got_reader, req);
355         return req;
356 }
357
358 static void nbt_getdc_got_reader(struct tevent_req *subreq)
359 {
360         struct tevent_req *req = tevent_req_callback_data(
361                 subreq, struct tevent_req);
362         struct nbt_getdc_state *state = tevent_req_data(
363                 req, struct nbt_getdc_state);
364         NTSTATUS status;
365
366         status = nb_packet_reader_recv(subreq, state, &state->reader);
367         TALLOC_FREE(subreq);
368         if (tevent_req_nterror(req, status)) {
369                 DEBUG(10, ("nb_packet_reader_recv returned %s\n",
370                            nt_errstr(status)));
371                 return;
372         }
373
374         status = messaging_send_buf(
375                 state->msg_ctx, pid_to_procid(state->nmbd_pid),
376                 MSG_SEND_PACKET, (uint8_t *)&state->p, sizeof(state->p));
377
378         if (tevent_req_nterror(req, status)) {
379                 DEBUG(10, ("messaging_send_buf returned %s\n",
380                            nt_errstr(status)));
381                 return;
382         }
383         subreq = nb_packet_read_send(state, state->ev, state->reader);
384         if (tevent_req_nomem(subreq, req)) {
385                 return;
386         }
387         tevent_req_set_callback(subreq, nbt_getdc_got_response, req);
388 }
389
390 static void nbt_getdc_got_response(struct tevent_req *subreq)
391 {
392         struct tevent_req *req = tevent_req_callback_data(
393                 subreq, struct tevent_req);
394         struct nbt_getdc_state *state = tevent_req_data(
395                 req, struct nbt_getdc_state);
396         struct packet_struct *p;
397         NTSTATUS status;
398         bool ret;
399
400         status = nb_packet_read_recv(subreq, state, &p);
401         TALLOC_FREE(subreq);
402         if (tevent_req_nterror(req, status)) {
403                 return;
404         }
405
406         ret = parse_getdc_response(p, state, state->domain_name,
407                                    &state->nt_version, &state->dc_name,
408                                    &state->samlogon_response);
409         if (!ret) {
410                 tevent_req_nterror(req, NT_STATUS_INVALID_NETWORK_RESPONSE);
411                 return;
412         }
413         tevent_req_done(req);
414 }
415
416 NTSTATUS nbt_getdc_recv(struct tevent_req *req, TALLOC_CTX *mem_ctx,
417                         uint32_t *nt_version, const char **dc_name,
418                         struct netlogon_samlogon_response **samlogon_response)
419 {
420         struct nbt_getdc_state *state = tevent_req_data(
421                 req, struct nbt_getdc_state);
422         NTSTATUS status;
423
424         if (tevent_req_is_nterror(req, &status)) {
425                 return status;
426         }
427         if (nt_version != NULL) {
428                 *nt_version = state->nt_version;
429         }
430         if (dc_name != NULL) {
431                 *dc_name = talloc_move(mem_ctx, &state->dc_name);
432         }
433         if (samlogon_response != NULL) {
434                 *samlogon_response = talloc_move(
435                         mem_ctx, &state->samlogon_response);
436         }
437         return NT_STATUS_OK;
438 }
439
440 NTSTATUS nbt_getdc(struct messaging_context *msg_ctx,
441                    uint32_t timeout_in_seconds,
442                    const struct sockaddr_storage *dc_addr,
443                    const char *domain_name,
444                    const struct dom_sid *sid,
445                    uint32_t nt_version,
446                    TALLOC_CTX *mem_ctx,
447                    uint32_t *pnt_version,
448                    const char **dc_name,
449                    struct netlogon_samlogon_response **samlogon_response)
450 {
451         TALLOC_CTX *frame = talloc_stackframe();
452         struct tevent_context *ev;
453         struct tevent_req *req;
454         NTSTATUS status = NT_STATUS_NO_MEMORY;
455
456         ev = samba_tevent_context_init(frame);
457         if (ev == NULL) {
458                 goto fail;
459         }
460         req = nbt_getdc_send(ev, ev, msg_ctx, dc_addr, domain_name,
461                              sid, nt_version);
462         if (req == NULL) {
463                 goto fail;
464         }
465         if (!tevent_req_set_endtime(req, ev,
466                         timeval_current_ofs(timeout_in_seconds, 0))) {
467                 goto fail;
468         }
469         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
470                 goto fail;
471         }
472         status = nbt_getdc_recv(req, mem_ctx, pnt_version, dc_name,
473                                 samlogon_response);
474  fail:
475         TALLOC_FREE(frame);
476         return status;
477 }