r1874: add dcerpc_errstr() function to display a fault code by name,
[abartlet/samba.git/.git] / source4 / 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 struct netlogon_string {
28         uint32_t comp_len;
29         char **component;
30         uint8_t extra_flag;
31 };
32
33 struct cldap_netlogon_reply {
34         uint32_t type;
35         uint32_t flags;
36         GUID guid;
37
38         struct netlogon_string forest;
39         struct netlogon_string domain;
40         struct netlogon_string hostname;
41
42         struct netlogon_string netbios_domain;
43         struct netlogon_string netbios_hostname;
44
45         struct netlogon_string user_name;
46         struct netlogon_string site_name;
47
48         struct netlogon_string unk0;
49
50         uint32_t version;
51         uint16_t lmnt_token;
52         uint16_t lm20_token;
53 };
54
55 /*
56   These strings are rather interesting... They are composed of a series of
57   length encoded strings, terminated by either 1) a zero length string or 2)
58   a 0xc0 byte with what appears to be a one byte flags immediately following.
59 */
60 static uint_t pull_netlogon_string(struct netlogon_string *ret,const char *d)
61 {
62         char *p = (char *)d;
63
64         ZERO_STRUCTP(ret);
65
66         do {
67                 uint_t len = (uint8_t)*p;
68                 p++;
69
70                 if (len > 0 && len != 0xc0) {
71                         ret->component = realloc(ret->component,
72                                                  ++ret->comp_len *
73                                                  sizeof(char *));
74
75                         ret->component[ret->comp_len - 1] = 
76                                 smb_xstrndup(p, len);
77                         p += len;
78                 } else {
79                         if (len == 0xc0) {
80                                 ret->extra_flag = *p;
81                                 p++;
82                         };
83                         break;
84                 }
85         } while (1);
86
87         return (p - d);
88 }
89
90 /*
91   do a cldap netlogon query
92 */
93 static int send_cldap_netlogon(int sock, const char *domain, 
94                                const char *hostname, uint_t ntversion)
95 {
96         ASN1_DATA data;
97         char ntver[4];
98
99         SIVAL(ntver, 0, ntversion);
100
101         memset(&data, 0, sizeof(data));
102
103         asn1_push_tag(&data,ASN1_SEQUENCE(0));
104         asn1_write_Integer(&data, 4);
105         asn1_push_tag(&data, ASN1_APPLICATION(3));
106         asn1_write_OctetString(&data, NULL, 0);
107         asn1_write_enumerated(&data, 0);
108         asn1_write_enumerated(&data, 0);
109         asn1_write_Integer(&data, 0);
110         asn1_write_Integer(&data, 0);
111         asn1_write_BOOLEAN2(&data, False);
112         asn1_push_tag(&data, ASN1_CONTEXT(0));
113
114         asn1_push_tag(&data, ASN1_CONTEXT(3));
115         asn1_write_OctetString(&data, "DnsDomain", 9);
116         asn1_write_OctetString(&data, domain, strlen(domain));
117         asn1_pop_tag(&data);
118
119         asn1_push_tag(&data, ASN1_CONTEXT(3));
120         asn1_write_OctetString(&data, "Host", 4);
121         asn1_write_OctetString(&data, hostname, strlen(hostname));
122         asn1_pop_tag(&data);
123
124         asn1_push_tag(&data, ASN1_CONTEXT(3));
125         asn1_write_OctetString(&data, "NtVer", 5);
126         asn1_write_OctetString(&data, ntver, 4);
127         asn1_pop_tag(&data);
128
129         asn1_pop_tag(&data);
130
131         asn1_push_tag(&data,ASN1_SEQUENCE(0));
132         asn1_write_OctetString(&data, "NetLogon", 8);
133         asn1_pop_tag(&data);
134         asn1_pop_tag(&data);
135         asn1_pop_tag(&data);
136
137         if (data.has_error) {
138                 d_printf("Failed to build cldap netlogon at offset %d\n", (int)data.ofs);
139                 asn1_free(&data);
140                 return -1;
141         }
142
143         if (write(sock, data.data, data.length) != data.length) {
144                 d_printf("failed to send cldap query (%s)\n", strerror(errno));
145         }
146
147         file_save("cldap_query.dat", data.data, data.length);
148         asn1_free(&data);
149
150         return 0;
151 }
152
153
154 /*
155   receive a cldap netlogon reply
156 */
157 static int recv_cldap_netlogon(int sock, struct cldap_netlogon_reply *reply)
158 {
159         int ret;
160         ASN1_DATA data;
161         DATA_BLOB blob;
162         DATA_BLOB os1, os2, os3;
163         uint32_t i1;
164         char *p;
165
166         blob = data_blob(NULL, 8192);
167
168         ret = read(sock, blob.data, blob.length);
169
170         if (ret <= 0) {
171                 d_printf("no reply received to cldap netlogon\n");
172                 return -1;
173         }
174         blob.length = ret;
175
176         file_save("cldap_reply.dat", blob.data, blob.length);
177
178         asn1_load(&data, blob);
179         asn1_start_tag(&data, ASN1_SEQUENCE(0));
180         asn1_read_Integer(&data, &i1);
181         asn1_start_tag(&data, ASN1_APPLICATION(4));
182         asn1_read_OctetString(&data, &os1);
183         asn1_start_tag(&data, ASN1_SEQUENCE(0));
184         asn1_start_tag(&data, ASN1_SEQUENCE(0));
185         asn1_read_OctetString(&data, &os2);
186         asn1_start_tag(&data, ASN1_SET);
187         asn1_read_OctetString(&data, &os3);
188         asn1_end_tag(&data);
189         asn1_end_tag(&data);
190         asn1_end_tag(&data);
191         asn1_end_tag(&data);
192         asn1_end_tag(&data);
193
194         if (data.has_error) {
195                 d_printf("Failed to parse cldap reply\n");
196                 return -1;
197         }
198
199         file_save("cldap_reply_core.dat", os3.data, os3.length);
200
201         p = os3.data;
202
203         reply->type = IVAL(p, 0); p += 4;
204         reply->flags = IVAL(p, 0); p += 4;
205
206         memcpy(&reply->guid.info, p, GUID_SIZE);
207         p += GUID_SIZE;
208
209         p += pull_netlogon_string(&reply->forest, p);
210         p += pull_netlogon_string(&reply->domain, p);
211         p += pull_netlogon_string(&reply->hostname, p);
212         p += pull_netlogon_string(&reply->netbios_domain, p);
213         p += pull_netlogon_string(&reply->netbios_hostname, p);
214         p += pull_netlogon_string(&reply->user_name, p);
215         p += pull_netlogon_string(&reply->site_name, p);
216
217         p += pull_netlogon_string(&reply->unk0, p);
218
219         reply->version = IVAL(p, 0);
220         reply->lmnt_token = SVAL(p, 4);
221         reply->lm20_token = SVAL(p, 6);
222
223         data_blob_free(&os1);
224         data_blob_free(&os2);
225         data_blob_free(&os3);
226         data_blob_free(&blob);
227         
228         return 0;
229 }
230
231 /*
232   free a netlogon string
233 */
234 static void netlogon_string_free(struct netlogon_string *str)
235 {
236         int i;
237
238         for (i = 0; i < str->comp_len; ++i) {
239                 SAFE_FREE(str->component[i]);
240         }
241         SAFE_FREE(str->component);
242 }
243
244 /*
245   free a cldap reply packet
246 */
247 static void cldap_reply_free(struct cldap_netlogon_reply *reply)
248 {
249         netlogon_string_free(&reply->forest);
250         netlogon_string_free(&reply->domain);
251         netlogon_string_free(&reply->hostname);
252         netlogon_string_free(&reply->netbios_domain);
253         netlogon_string_free(&reply->netbios_hostname);
254         netlogon_string_free(&reply->user_name);
255         netlogon_string_free(&reply->site_name);
256         netlogon_string_free(&reply->unk0);
257 }
258
259 static void d_print_netlogon_string(const char *label, 
260                                     struct netlogon_string *str)
261 {
262         int i;
263
264         if (str->comp_len) {
265                 d_printf("%s", label);
266                 if (str->extra_flag) {
267                         d_printf("[%d]", str->extra_flag);
268                 }
269                 d_printf(": ");
270                 for (i = 0; i < str->comp_len; ++i) {
271                         d_printf("%s%s", (i ? "." : ""), str->component[i]);
272                 }
273                 d_printf("\n");
274         }
275 }
276
277 /*
278   do a cldap netlogon query
279 */
280 int ads_cldap_netlogon(ADS_STRUCT *ads)
281 {
282         int sock;
283         int ret;
284         struct cldap_netlogon_reply reply;
285
286         sock = open_udp_socket(inet_ntoa(ads->ldap_ip), ads->ldap_port);
287         if (sock == -1) {
288                 d_printf("Failed to open udp socket to %s:%u\n", 
289                          inet_ntoa(ads->ldap_ip), 
290                          ads->ldap_port);
291                 return -1;
292         }
293
294         ret = send_cldap_netlogon(sock, ads->config.realm, lp_netbios_name(), 6);
295         if (ret != 0) {
296                 return ret;
297         }
298         ret = recv_cldap_netlogon(sock, &reply);
299         close(sock);
300
301         if (ret == -1) {
302                 return -1;
303         }
304
305         d_printf("Information for Domain Controller: %s\n\n", 
306                  ads->config.ldap_server_name);
307
308         d_printf("Response Type: 0x%x\n", reply.type);
309         d_printf("GUID: "); 
310         print_guid(&reply.guid);
311         d_printf("Flags:\n"
312                  "\tIs a PDC:                                   %s\n"
313                  "\tIs a GC of the forest:                      %s\n"
314                  "\tIs an LDAP server:                          %s\n"
315                  "\tSupports DS:                                %s\n"
316                  "\tIs running a KDC:                           %s\n"
317                  "\tIs running time services:                   %s\n"
318                  "\tIs the closest DC:                          %s\n"
319                  "\tIs writable:                                %s\n"
320                  "\tHas a hardware clock:                       %s\n"
321                  "\tIs a non-domain NC serviced by LDAP server: %s\n",
322                  (reply.flags & ADS_PDC) ? "yes" : "no",
323                  (reply.flags & ADS_GC) ? "yes" : "no",
324                  (reply.flags & ADS_LDAP) ? "yes" : "no",
325                  (reply.flags & ADS_DS) ? "yes" : "no",
326                  (reply.flags & ADS_KDC) ? "yes" : "no",
327                  (reply.flags & ADS_TIMESERV) ? "yes" : "no",
328                  (reply.flags & ADS_CLOSEST) ? "yes" : "no",
329                  (reply.flags & ADS_WRITABLE) ? "yes" : "no",
330                  (reply.flags & ADS_GOOD_TIMESERV) ? "yes" : "no",
331                  (reply.flags & ADS_NDNC) ? "yes" : "no");
332
333         d_print_netlogon_string("Forest", &reply.forest);
334         d_print_netlogon_string("Domain", &reply.domain);
335         d_print_netlogon_string("Hostname", &reply.hostname);
336
337         d_print_netlogon_string("Pre-Win2k Domain", &reply.netbios_domain);
338         d_print_netlogon_string("Pre-Win2k Hostname", &reply.netbios_hostname);
339
340         d_print_netlogon_string("User name", &reply.user_name);
341         d_print_netlogon_string("Site Name", &reply.site_name);
342         d_print_netlogon_string("Unknown Field", &reply.unk0);
343
344         d_printf("NT Version: %d\n", reply.version);
345         d_printf("LMNT Token: %.2x\n", reply.lmnt_token);
346         d_printf("LM20 Token: %.2x\n", reply.lm20_token);
347
348         cldap_reply_free(&reply);
349         
350         return ret;
351 }
352
353
354 #endif