This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[sfrench/samba-autobuild/.git] / source3 / utils / net_ads_cldap.c
1 /* 
2    Samba Unix/Linux SMB client library 
3    net ads cldap functions 
4    Copyright (C) 2001 Andrew Tridgell (tridge@samba.org)
5    Copyright (C) 2003 Jim McDonough (jmcd@us.ibm.com)
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
20 */
21
22 #include "includes.h"
23 #include "../utils/net.h"
24
25 #ifdef HAVE_ADS
26
27 #define MAX_DNS_LABEL 255 + 1
28
29 struct cldap_netlogon_reply {
30         uint32 type;
31         uint32 flags;
32         GUID guid;
33
34         char forest[MAX_DNS_LABEL];
35         char unk0[MAX_DNS_LABEL];
36         char domain[MAX_DNS_LABEL];
37         char hostname[MAX_DNS_LABEL];
38
39         char netbios_domain[MAX_DNS_LABEL];
40         char unk1[MAX_DNS_LABEL];
41         char netbios_hostname[MAX_DNS_LABEL];
42
43         char unk2[MAX_DNS_LABEL];
44         char user_name[MAX_DNS_LABEL];
45         char unk3[MAX_DNS_LABEL];
46         char site_name[MAX_DNS_LABEL];
47         char unk4[MAX_DNS_LABEL];
48         char site_name_2[MAX_DNS_LABEL];
49
50         uint32 version;
51         uint16 lmnt_token;
52         uint16 lm20_token;
53 };
54
55 /*
56   These seem to be strings as described in RFC1035 4.1.4 and can be:
57
58    - a sequence of labels ending in a zero octet
59    - a pointer
60    - a sequence of labels ending with a pointer
61
62   A label is a byte where the first two bits must be zero and the remaining
63   bits represent the length of the label followed by the label itself.
64   Therefore, the length of a label is at max 64 bytes.  Under RFC1035, a
65   sequence of labels cannot exceed 255 bytes.
66
67   A pointer consists of a 14 bit offset from the beginning of the data.
68
69   struct ptr {
70     unsigned ident:2; // must be 11
71     unsigned offset:14; // from the beginning of data
72   };
73
74   This is used as a method to compress the packet by eliminated duplicate
75   domain components.  Since a UDP packet should probably be < 512 bytes and a
76   DNS name can be up to 255 bytes, this actually makes a lot of sense.
77 */
78 static unsigned pull_netlogon_string(char *ret, const char *ptr,
79                                      const char *data)
80 {
81         char *pret = ret;
82         int followed_ptr = 0;
83         unsigned ret_len = 0;
84
85         memset(pret, 0, MAX_DNS_LABEL);
86         do {
87                 if ((*ptr & 0xc0) == 0xc0) {
88                         uint16 len;
89
90                         if (!followed_ptr) {
91                                 ret_len += 2;
92                                 followed_ptr = 1;
93                         }
94                         len = ((ptr[0] & 0x3f) << 8) | ptr[1];
95                         ptr = data + len;
96                 } else if (*ptr) {
97                         uint8 len = (uint8)*(ptr++);
98
99                         if ((pret - ret + len + 1) >= MAX_DNS_LABEL) {
100                                 d_printf("DC returning too long DNS name\n");
101                                 return 0;
102                         }
103
104                         if (pret != ret) {
105                                 *pret = '.';
106                                 pret++;
107                         }
108                         memcpy(pret, ptr, len);
109                         pret += len;
110                         ptr += len;
111
112                         if (!followed_ptr) {
113                                 ret_len += (len + 1);
114                         }
115                 }
116         } while (*ptr);
117
118         return ret_len ? ret_len : 1;
119 }
120
121 /*
122   do a cldap netlogon query
123 */
124 static int send_cldap_netlogon(int sock, const char *domain, 
125                                const char *hostname, unsigned ntversion)
126 {
127         ASN1_DATA data;
128         char ntver[4];
129 #ifdef CLDAP_USER_QUERY
130         char aac[4];
131
132         SIVAL(aac, 0, 0x00000180);
133 #endif
134         SIVAL(ntver, 0, ntversion);
135
136         memset(&data, 0, sizeof(data));
137
138         asn1_push_tag(&data,ASN1_SEQUENCE(0));
139         asn1_write_Integer(&data, 4);
140         asn1_push_tag(&data, ASN1_APPLICATION(3));
141         asn1_write_OctetString(&data, NULL, 0);
142         asn1_write_enumerated(&data, 0);
143         asn1_write_enumerated(&data, 0);
144         asn1_write_Integer(&data, 0);
145         asn1_write_Integer(&data, 0);
146         asn1_write_BOOLEAN2(&data, False);
147         asn1_push_tag(&data, ASN1_CONTEXT(0));
148
149         asn1_push_tag(&data, ASN1_CONTEXT(3));
150         asn1_write_OctetString(&data, "DnsDomain", 9);
151         asn1_write_OctetString(&data, domain, strlen(domain));
152         asn1_pop_tag(&data);
153
154         asn1_push_tag(&data, ASN1_CONTEXT(3));
155         asn1_write_OctetString(&data, "Host", 4);
156         asn1_write_OctetString(&data, hostname, strlen(hostname));
157         asn1_pop_tag(&data);
158
159 #ifdef CLDAP_USER_QUERY
160         asn1_push_tag(&data, ASN1_CONTEXT(3));
161         asn1_write_OctetString(&data, "User", 4);
162         asn1_write_OctetString(&data, "SAMBA$", 6);
163         asn1_pop_tag(&data);
164
165         asn1_push_tag(&data, ASN1_CONTEXT(3));
166         asn1_write_OctetString(&data, "AAC", 4);
167         asn1_write_OctetString(&data, aac, 4);
168         asn1_pop_tag(&data);
169 #endif
170
171         asn1_push_tag(&data, ASN1_CONTEXT(3));
172         asn1_write_OctetString(&data, "NtVer", 5);
173         asn1_write_OctetString(&data, ntver, 4);
174         asn1_pop_tag(&data);
175
176         asn1_pop_tag(&data);
177
178         asn1_push_tag(&data,ASN1_SEQUENCE(0));
179         asn1_write_OctetString(&data, "NetLogon", 8);
180         asn1_pop_tag(&data);
181         asn1_pop_tag(&data);
182         asn1_pop_tag(&data);
183
184         if (data.has_error) {
185                 d_printf("Failed to build cldap netlogon at offset %d\n", (int)data.ofs);
186                 asn1_free(&data);
187                 return -1;
188         }
189
190         if (write(sock, data.data, data.length) != (ssize_t)data.length) {
191                 d_printf("failed to send cldap query (%s)\n", strerror(errno));
192         }
193
194         asn1_free(&data);
195
196         return 0;
197 }
198
199
200 /*
201   receive a cldap netlogon reply
202 */
203 static int recv_cldap_netlogon(int sock, struct cldap_netlogon_reply *reply)
204 {
205         int ret;
206         ASN1_DATA data;
207         DATA_BLOB blob;
208         DATA_BLOB os1, os2, os3;
209         uint32 i1;
210         char *p;
211
212         blob = data_blob(NULL, 8192);
213
214         ret = read(sock, blob.data, blob.length);
215
216         if (ret <= 0) {
217                 d_printf("no reply received to cldap netlogon\n");
218                 return -1;
219         }
220         blob.length = ret;
221
222         asn1_load(&data, blob);
223         asn1_start_tag(&data, ASN1_SEQUENCE(0));
224         asn1_read_Integer(&data, &i1);
225         asn1_start_tag(&data, ASN1_APPLICATION(4));
226         asn1_read_OctetString(&data, &os1);
227         asn1_start_tag(&data, ASN1_SEQUENCE(0));
228         asn1_start_tag(&data, ASN1_SEQUENCE(0));
229         asn1_read_OctetString(&data, &os2);
230         asn1_start_tag(&data, ASN1_SET);
231         asn1_read_OctetString(&data, &os3);
232         asn1_end_tag(&data);
233         asn1_end_tag(&data);
234         asn1_end_tag(&data);
235         asn1_end_tag(&data);
236         asn1_end_tag(&data);
237
238         if (data.has_error) {
239                 d_printf("Failed to parse cldap reply\n");
240                 return -1;
241         }
242
243         p = os3.data;
244
245         reply->type = IVAL(p, 0); p += 4;
246         reply->flags = IVAL(p, 0); p += 4;
247
248         memcpy(&reply->guid.info, p, GUID_SIZE);
249         p += GUID_SIZE;
250
251         p += pull_netlogon_string(reply->forest, p, os3.data);
252         p += pull_netlogon_string(reply->unk0, p, os3.data);
253         p += pull_netlogon_string(reply->domain, p, os3.data);
254         p += pull_netlogon_string(reply->hostname, p, os3.data);
255         p += pull_netlogon_string(reply->netbios_domain, p, os3.data);
256         p += pull_netlogon_string(reply->unk1, p, os3.data);
257         p += pull_netlogon_string(reply->netbios_hostname, p, os3.data);
258         p += pull_netlogon_string(reply->unk2, p, os3.data);
259
260         if (reply->type == SAMLOGON_AD_R) {
261                 p += pull_netlogon_string(reply->user_name, p, os3.data);
262         } else {
263                 *reply->user_name = 0;
264         }
265
266         p += pull_netlogon_string(reply->unk3, p, os3.data);
267         p += pull_netlogon_string(reply->site_name, p, os3.data);
268         p += pull_netlogon_string(reply->unk4, p, os3.data);
269         p += pull_netlogon_string(reply->site_name_2, p, os3.data);
270
271         reply->version = IVAL(p, 0);
272         reply->lmnt_token = SVAL(p, 4);
273         reply->lm20_token = SVAL(p, 6);
274
275         data_blob_free(&os1);
276         data_blob_free(&os2);
277         data_blob_free(&os3);
278         data_blob_free(&blob);
279         
280         return 0;
281 }
282
283 /*
284   do a cldap netlogon query
285 */
286 int ads_cldap_netlogon(ADS_STRUCT *ads)
287 {
288         int sock;
289         int ret;
290         struct cldap_netlogon_reply reply;
291
292         sock = open_udp_socket(inet_ntoa(ads->ldap_ip), ads->ldap_port);
293         if (sock == -1) {
294                 d_printf("Failed to open udp socket to %s:%u\n", 
295                          inet_ntoa(ads->ldap_ip), 
296                          ads->ldap_port);
297                 return -1;
298
299         }
300
301         ret = send_cldap_netlogon(sock, ads->config.realm, global_myname(), 6);
302         if (ret != 0) {
303                 return ret;
304         }
305         ret = recv_cldap_netlogon(sock, &reply);
306         close(sock);
307
308         if (ret == -1) {
309                 return -1;
310         }
311
312         d_printf("Information for Domain Controller: %s\n\n", 
313                  ads->config.ldap_server_name);
314
315         d_printf("Response Type: ");
316         switch (reply.type) {
317         case SAMLOGON_AD_UNK_R:
318                 d_printf("SAMLOGON\n");
319                 break;
320         case SAMLOGON_AD_R:
321                 d_printf("SAMLOGON_USER\n");
322                 break;
323         default:
324                 d_printf("0x%x\n", reply.type);
325                 break;
326         }
327         d_printf("GUID: "); 
328         print_guid(&reply.guid);
329         d_printf("Flags:\n"
330                  "\tIs a PDC:                                   %s\n"
331                  "\tIs a GC of the forest:                      %s\n"
332                  "\tIs an LDAP server:                          %s\n"
333                  "\tSupports DS:                                %s\n"
334                  "\tIs running a KDC:                           %s\n"
335                  "\tIs running time services:                   %s\n"
336                  "\tIs the closest DC:                          %s\n"
337                  "\tIs writable:                                %s\n"
338                  "\tHas a hardware clock:                       %s\n"
339                  "\tIs a non-domain NC serviced by LDAP server: %s\n",
340                  (reply.flags & ADS_PDC) ? "yes" : "no",
341                  (reply.flags & ADS_GC) ? "yes" : "no",
342                  (reply.flags & ADS_LDAP) ? "yes" : "no",
343                  (reply.flags & ADS_DS) ? "yes" : "no",
344                  (reply.flags & ADS_KDC) ? "yes" : "no",
345                  (reply.flags & ADS_TIMESERV) ? "yes" : "no",
346                  (reply.flags & ADS_CLOSEST) ? "yes" : "no",
347                  (reply.flags & ADS_WRITABLE) ? "yes" : "no",
348                  (reply.flags & ADS_GOOD_TIMESERV) ? "yes" : "no",
349                  (reply.flags & ADS_NDNC) ? "yes" : "no");
350
351         printf("Forest:\t\t\t%s\n", reply.forest);
352         if (*reply.unk0) printf("Unk0:\t\t\t%s\n", reply.unk0);
353         printf("Domain:\t\t\t%s\n", reply.domain);
354         printf("Domain Controller:\t%s\n", reply.hostname);
355
356         printf("Pre-Win2k Domain:\t%s\n", reply.netbios_domain);
357         if (*reply.unk1) printf("Unk1:\t\t\t%s\n", reply.unk1);
358         printf("Pre-Win2k Hostname:\t%s\n", reply.netbios_hostname);
359
360         if (*reply.unk2) printf("Unk2:\t\t\t%s\n", reply.unk2);
361         if (*reply.user_name) printf("User name:\t%s\n", reply.user_name);
362
363         if (*reply.unk3) printf("Unk3:\t\t\t%s\n", reply.unk3);
364         printf("Site Name:\t\t%s\n", reply.site_name);
365         if (*reply.unk4) printf("Unk4:\t\t\t%s\n", reply.unk4);
366         printf("Site Name (2):\t\t%s\n", reply.site_name_2);
367
368         d_printf("NT Version: %d\n", reply.version);
369         d_printf("LMNT Token: %.2x\n", reply.lmnt_token);
370         d_printf("LM20 Token: %.2x\n", reply.lm20_token);
371
372         return ret;
373 }
374
375
376 #endif