s3: cli_send_mailslot -> cli_prep_mailslot
[ddiss/samba.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 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,
121                         uint32_t nt_version,
122                         int dgm_id)
123 {
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;
130         DATA_BLOB blob;
131         struct dom_sid my_sid;
132         struct packet_struct p;
133         pid_t nmbd_pid;
134
135         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
136                 DEBUG(3, ("No nmbd found\n"));
137                 return False;
138         }
139
140         if (dc_ss->ss_family != AF_INET) {
141                 return false;
142         }
143
144         ZERO_STRUCT(packet);
145         ZERO_STRUCT(my_sid);
146
147         if (sid) {
148                 my_sid = *sid;
149         }
150
151         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
152         my_mailslot = mailslot_name(mem_ctx, dc_ip);
153         if (!my_mailslot) {
154                 return false;
155         }
156
157         my_acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
158         if (!my_acct_name) {
159                 return false;
160         }
161
162         packet.command  = LOGON_SAM_LOGON_REQUEST;
163         s               = &packet.req.logon;
164
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;
170         s->sid                  = my_sid;
171         s->nt_version           = nt_version;
172         s->lmnt_token           = 0xffff;
173         s->lm20_token           = 0xffff;
174
175         if (DEBUGLEVEL >= 10) {
176                 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
177         }
178
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)) {
182                 return false;
183         }
184
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)) {
189                 return false;
190         }
191         return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
192                                                   pid_to_procid(nmbd_pid),
193                                                   MSG_SEND_PACKET,
194                                                   (uint8 *)&p, sizeof(p)));
195 }
196
197 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
198                             const struct sockaddr_storage *dc_ss,
199                             const char *domain_name,
200                             int dgm_id,
201                             uint32_t *nt_version,
202                             const char **dc_name,
203                             struct netlogon_samlogon_response **samlogon_response)
204 {
205         struct packet_struct *packet;
206         const char *my_mailslot = NULL;
207         struct in_addr dc_ip;
208         DATA_BLOB blob;
209         struct netlogon_samlogon_response *r;
210         union dgram_message_body p;
211         enum ndr_err_code ndr_err;
212         NTSTATUS status;
213
214         const char *returned_dc = NULL;
215         const char *returned_domain = NULL;
216
217         if (dc_ss->ss_family != AF_INET) {
218                 return false;
219         }
220
221         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
222
223         my_mailslot = mailslot_name(mem_ctx, dc_ip);
224         if (!my_mailslot) {
225                 return false;
226         }
227
228         packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
229
230         if (packet == NULL) {
231                 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
232                 return False;
233         }
234
235         DEBUG(5, ("Received packet for %s\n", my_mailslot));
236
237         blob = data_blob_const(packet->packet.dgram.data,
238                                packet->packet.dgram.datasize);
239
240         if (blob.length < 4) {
241                 DEBUG(0,("invalid length: %d\n", (int)blob.length));
242                 return false;
243         }
244
245         if (RIVAL(blob.data,0) != DGRAM_SMB) {
246                 DEBUG(0,("invalid packet\n"));
247                 return false;
248         }
249
250         blob.data += 4;
251         blob.length -= 4;
252
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"));
257                 return false;
258         }
259
260         if (p.smb.smb_command != SMB_TRANSACTION) {
261                 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
262                 return false;
263         }
264
265         if (DEBUGLEVEL >= 10) {
266                 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
267         }
268
269         blob = p.smb.body.trans.data;
270
271         r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
272         if (!r) {
273                 return false;
274         }
275
276         status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
277         if (!NT_STATUS_IS_OK(status)) {
278                 TALLOC_FREE(r);
279                 return false;
280         }
281
282         map_netlogon_samlogon_response(r);
283
284         /* do we still need this ? */
285         *nt_version = r->ntver;
286
287         returned_domain = r->data.nt5_ex.domain_name;
288         returned_dc = r->data.nt5_ex.pdc_name;
289
290         if (!strequal(returned_domain, domain_name)) {
291                 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
292                           domain_name, returned_domain));
293                 TALLOC_FREE(r);
294                 return false;
295         }
296
297         *dc_name = talloc_strdup(mem_ctx, returned_dc);
298         if (!*dc_name) {
299                 TALLOC_FREE(r);
300                 return false;
301         }
302
303         if (**dc_name == '\\')  *dc_name += 1;
304         if (**dc_name == '\\')  *dc_name += 1;
305
306         if (samlogon_response) {
307                 *samlogon_response = r;
308         } else {
309                 TALLOC_FREE(r);
310         }
311
312         DEBUG(10, ("GetDC gave name %s for domain %s\n",
313                    *dc_name, returned_domain));
314
315         return True;
316 }