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