s3: Fix some nonempty blank lines
[kai/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_send_mailslot(struct messaging_context *msg_ctx,
31                        bool unique, const char *mailslot,
32                        uint16 priority,
33                        char *buf, int len,
34                        const char *srcname, int src_type,
35                        const char *dstname, int dest_type,
36                        const struct sockaddr_storage *dest_ss,
37                        int dgm_id)
38 {
39         struct packet_struct p;
40         struct dgram_packet *dgram = &p.packet.dgram;
41         char *ptr, *p2;
42         char tmp[4];
43         pid_t nmbd_pid;
44         char addr[INET6_ADDRSTRLEN];
45
46         if ((nmbd_pid = pidfile_pid("nmbd")) == 0) {
47                 DEBUG(3, ("No nmbd found\n"));
48                 return False;
49         }
50
51         if (dest_ss->ss_family != AF_INET) {
52                 DEBUG(3, ("cli_send_mailslot: can't send to IPv6 address.\n"));
53                 return false;
54         }
55
56         memset((char *)&p, '\0', sizeof(p));
57
58         /*
59          * Next, build the DGRAM ...
60          */
61
62         /* DIRECT GROUP or UNIQUE datagram. */
63         dgram->header.msg_type = unique ? 0x10 : 0x11;
64         dgram->header.flags.node_type = M_NODE;
65         dgram->header.flags.first = True;
66         dgram->header.flags.more = False;
67         dgram->header.dgm_id = dgm_id;
68         /* source ip is filled by nmbd */
69         dgram->header.dgm_length = 0; /* Let build_dgram() handle this. */
70         dgram->header.packet_offset = 0;
71
72         make_nmb_name(&dgram->source_name,srcname,src_type);
73         make_nmb_name(&dgram->dest_name,dstname,dest_type);
74
75         ptr = &dgram->data[0];
76
77         /* Setup the smb part. */
78         ptr -= 4; /* XXX Ugliness because of handling of tcp SMB length. */
79         memcpy(tmp,ptr,4);
80
81         if (smb_size + 17*2 + strlen(mailslot) + 1 + len > MAX_DGRAM_SIZE) {
82                 DEBUG(0, ("cli_send_mailslot: Cannot write beyond end of packet\n"));
83                 return False;
84         }
85
86         cli_set_message(ptr,17,strlen(mailslot) + 1 + len,True);
87         memcpy(ptr,tmp,4);
88
89         SCVAL(ptr,smb_com,SMBtrans);
90         SSVAL(ptr,smb_vwv1,len);
91         SSVAL(ptr,smb_vwv11,len);
92         SSVAL(ptr,smb_vwv12,70 + strlen(mailslot));
93         SSVAL(ptr,smb_vwv13,3);
94         SSVAL(ptr,smb_vwv14,1);
95         SSVAL(ptr,smb_vwv15,priority);
96         SSVAL(ptr,smb_vwv16,2);
97         p2 = smb_buf(ptr);
98         fstrcpy(p2,mailslot);
99         p2 = skip_string(ptr,MAX_DGRAM_SIZE,p2);
100         if (!p2) {
101                 return False;
102         }
103
104         memcpy(p2,buf,len);
105         p2 += len;
106
107         dgram->datasize = PTR_DIFF(p2,ptr+4); /* +4 for tcp length. */
108
109         p.packet_type = DGRAM_PACKET;
110         p.ip = ((const struct sockaddr_in *)dest_ss)->sin_addr;
111         p.timestamp = time(NULL);
112
113         DEBUG(4,("send_mailslot: Sending to mailslot %s from %s ",
114                  mailslot, nmb_namestr(&dgram->source_name)));
115         print_sockaddr(addr, sizeof(addr), dest_ss);
116
117         DEBUGADD(4,("to %s IP %s\n", nmb_namestr(&dgram->dest_name), addr));
118
119         return NT_STATUS_IS_OK(messaging_send_buf(msg_ctx,
120                                                   pid_to_procid(nmbd_pid),
121                                                   MSG_SEND_PACKET,
122                                                   (uint8 *)&p, sizeof(p)));
123 }
124
125 static const char *mailslot_name(TALLOC_CTX *mem_ctx, struct in_addr dc_ip)
126 {
127         return talloc_asprintf(mem_ctx, "%s%X",
128                                NBT_MAILSLOT_GETDC, dc_ip.s_addr);
129 }
130
131 bool send_getdc_request(TALLOC_CTX *mem_ctx,
132                         struct messaging_context *msg_ctx,
133                         const struct sockaddr_storage *dc_ss,
134                         const char *domain_name,
135                         const struct dom_sid *sid,
136                         uint32_t nt_version,
137                         int dgm_id)
138 {
139         struct in_addr dc_ip;
140         const char *my_acct_name = NULL;
141         const char *my_mailslot = NULL;
142         struct nbt_netlogon_packet packet;
143         struct NETLOGON_SAM_LOGON_REQUEST *s;
144         enum ndr_err_code ndr_err;
145         DATA_BLOB blob;
146         struct dom_sid my_sid;
147
148         ZERO_STRUCT(packet);
149         ZERO_STRUCT(my_sid);
150
151         if (dc_ss->ss_family != AF_INET) {
152                 return false;
153         }
154
155         if (sid) {
156                 my_sid = *sid;
157         }
158
159         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
160         my_mailslot = mailslot_name(mem_ctx, dc_ip);
161         if (!my_mailslot) {
162                 return false;
163         }
164
165         my_acct_name = talloc_asprintf(mem_ctx, "%s$", global_myname());
166         if (!my_acct_name) {
167                 return false;
168         }
169
170         packet.command  = LOGON_SAM_LOGON_REQUEST;
171         s               = &packet.req.logon;
172
173         s->request_count        = 0;
174         s->computer_name        = global_myname();
175         s->user_name            = my_acct_name;
176         s->mailslot_name        = my_mailslot;
177         s->acct_control         = ACB_WSTRUST;
178         s->sid                  = my_sid;
179         s->nt_version           = nt_version;
180         s->lmnt_token           = 0xffff;
181         s->lm20_token           = 0xffff;
182
183         if (DEBUGLEVEL >= 10) {
184                 NDR_PRINT_DEBUG(nbt_netlogon_packet, &packet);
185         }
186
187         ndr_err = ndr_push_struct_blob(&blob, mem_ctx, &packet,
188                        (ndr_push_flags_fn_t)ndr_push_nbt_netlogon_packet);
189         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
190                 return false;
191         }
192
193         return cli_send_mailslot(msg_ctx,
194                                  false, NBT_MAILSLOT_NTLOGON, 0,
195                                  (char *)blob.data, blob.length,
196                                  global_myname(), 0, domain_name, 0x1c,
197                                  dc_ss, dgm_id);
198 }
199
200 bool receive_getdc_response(TALLOC_CTX *mem_ctx,
201                             const struct sockaddr_storage *dc_ss,
202                             const char *domain_name,
203                             int dgm_id,
204                             uint32_t *nt_version,
205                             const char **dc_name,
206                             struct netlogon_samlogon_response **samlogon_response)
207 {
208         struct packet_struct *packet;
209         const char *my_mailslot = NULL;
210         struct in_addr dc_ip;
211         DATA_BLOB blob;
212         struct netlogon_samlogon_response *r;
213         union dgram_message_body p;
214         enum ndr_err_code ndr_err;
215         NTSTATUS status;
216
217         const char *returned_dc = NULL;
218         const char *returned_domain = NULL;
219
220         if (dc_ss->ss_family != AF_INET) {
221                 return false;
222         }
223
224         dc_ip = ((struct sockaddr_in *)dc_ss)->sin_addr;
225
226         my_mailslot = mailslot_name(mem_ctx, dc_ip);
227         if (!my_mailslot) {
228                 return false;
229         }
230
231         packet = receive_unexpected(DGRAM_PACKET, dgm_id, my_mailslot);
232
233         if (packet == NULL) {
234                 DEBUG(5, ("Did not receive packet for %s\n", my_mailslot));
235                 return False;
236         }
237
238         DEBUG(5, ("Received packet for %s\n", my_mailslot));
239
240         blob = data_blob_const(packet->packet.dgram.data,
241                                packet->packet.dgram.datasize);
242
243         if (blob.length < 4) {
244                 DEBUG(0,("invalid length: %d\n", (int)blob.length));
245                 return false;
246         }
247
248         if (RIVAL(blob.data,0) != DGRAM_SMB) {
249                 DEBUG(0,("invalid packet\n"));
250                 return false;
251         }
252
253         blob.data += 4;
254         blob.length -= 4;
255
256         ndr_err = ndr_pull_union_blob_all(&blob, mem_ctx, &p, DGRAM_SMB,
257                        (ndr_pull_flags_fn_t)ndr_pull_dgram_smb_packet);
258         if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
259                 DEBUG(0,("failed to parse packet\n"));
260                 return false;
261         }
262
263         if (p.smb.smb_command != SMB_TRANSACTION) {
264                 DEBUG(0,("invalid smb_command: %d\n", p.smb.smb_command));
265                 return false;
266         }
267
268         if (DEBUGLEVEL >= 10) {
269                 NDR_PRINT_DEBUG(dgram_smb_packet, &p);
270         }
271
272         blob = p.smb.body.trans.data;
273
274         r = TALLOC_ZERO_P(mem_ctx, struct netlogon_samlogon_response);
275         if (!r) {
276                 return false;
277         }
278
279         status = pull_netlogon_samlogon_response(&blob, mem_ctx, r);
280         if (!NT_STATUS_IS_OK(status)) {
281                 TALLOC_FREE(r);
282                 return false;
283         }
284
285         map_netlogon_samlogon_response(r);
286
287         /* do we still need this ? */
288         *nt_version = r->ntver;
289
290         returned_domain = r->data.nt5_ex.domain_name;
291         returned_dc = r->data.nt5_ex.pdc_name;
292
293         if (!strequal(returned_domain, domain_name)) {
294                 DEBUG(3, ("GetDC: Expected domain %s, got %s\n",
295                           domain_name, returned_domain));
296                 TALLOC_FREE(r);
297                 return false;
298         }
299
300         *dc_name = talloc_strdup(mem_ctx, returned_dc);
301         if (!*dc_name) {
302                 TALLOC_FREE(r);
303                 return false;
304         }
305
306         if (**dc_name == '\\')  *dc_name += 1;
307         if (**dc_name == '\\')  *dc_name += 1;
308
309         if (samlogon_response) {
310                 *samlogon_response = r;
311         } else {
312                 TALLOC_FREE(r);
313         }
314
315         DEBUG(10, ("GetDC gave name %s for domain %s\n",
316                    *dc_name, returned_domain));
317
318         return True;
319 }