librpc: Shorten dcerpc_binding_handle_call a bit
[Samba/gebeck_regimport.git] / source3 / lib / netapi / examples / netlogon / nltest.c
blob0c393ea3beb97d96f17358df2a80700fd3667837
1 /*
2 * Samba Unix/Linux SMB client library
3 * Distributed SMB/CIFS Server Management Utility
4 * Nltest netlogon testing tool
6 * Copyright (C) Guenther Deschner 2009
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.
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.
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/>.
22 #include <sys/types.h>
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
28 #include <netapi.h>
30 #include "common.h"
32 enum {
33 OPT_SERVER = 1,
34 OPT_DBFLAG,
35 OPT_SC_QUERY,
36 OPT_SC_RESET,
37 OPT_SC_VERIFY,
38 OPT_SC_CHANGE_PWD,
39 OPT_DSGETDC,
40 OPT_PDC,
41 OPT_DS,
42 OPT_DSP,
43 OPT_GC,
44 OPT_KDC,
45 OPT_TIMESERV,
46 OPT_GTIMESERV,
47 OPT_WS,
48 OPT_NETBIOS,
49 OPT_DNS,
50 OPT_IP,
51 OPT_FORCE,
52 OPT_WRITABLE,
53 OPT_AVOIDSELF,
54 OPT_LDAPONLY,
55 OPT_BACKG,
56 OPT_DS_6,
57 OPT_TRY_NEXT_CLOSEST_SITE,
58 OPT_SITE,
59 OPT_ACCOUNT,
60 OPT_RET_DNS,
61 OPT_RET_NETBIOS,
62 OPT_DSREGDNS
65 /****************************************************************
66 ****************************************************************/
68 static void print_netlogon_info_result(uint32_t level,
69 uint8_t *buffer)
71 struct NETLOGON_INFO_1 *i1 = NULL;
72 struct NETLOGON_INFO_2 *i2 = NULL;
73 struct NETLOGON_INFO_3 *i3 = NULL;
74 struct NETLOGON_INFO_4 *i4 = NULL;
76 if (!buffer) {
77 return;
80 switch (level) {
81 case 1:
82 i1 = (struct NETLOGON_INFO_1 *)buffer;
84 printf("Flags: %x\n", i1->netlog1_flags);
85 printf("Connection Status Status = %d 0x%x %s\n",
86 i1->netlog1_pdc_connection_status,
87 i1->netlog1_pdc_connection_status,
88 libnetapi_errstr(i1->netlog1_pdc_connection_status));
90 break;
91 case 2:
92 i2 = (struct NETLOGON_INFO_2 *)buffer;
94 printf("Flags: %x\n", i2->netlog2_flags);
95 printf("Trusted DC Name %s\n", i2->netlog2_trusted_dc_name);
96 printf("Trusted DC Connection Status Status = %d 0x%x %s\n",
97 i2->netlog2_tc_connection_status,
98 i2->netlog2_tc_connection_status,
99 libnetapi_errstr(i2->netlog2_tc_connection_status));
100 printf("Trust Verification Status Status = %d 0x%x %s\n",
101 i2->netlog2_pdc_connection_status,
102 i2->netlog2_pdc_connection_status,
103 libnetapi_errstr(i2->netlog2_pdc_connection_status));
105 break;
106 case 3:
107 i3 = (struct NETLOGON_INFO_3 *)buffer;
109 printf("Flags: %x\n", i3->netlog1_flags);
110 printf("Logon Attempts: %d\n", i3->netlog3_logon_attempts);
112 break;
113 case 4:
114 i4 = (struct NETLOGON_INFO_4 *)buffer;
116 printf("Trusted DC Name %s\n", i4->netlog4_trusted_dc_name);
117 printf("Trusted Domain Name %s\n", i4->netlog4_trusted_domain_name);
119 break;
120 default:
121 break;
125 /****************************************************************
126 ****************************************************************/
128 static void print_dc_info_flags(uint32_t flags)
130 if (flags & DS_PDC_FLAG)
131 printf("PDC ");
132 if (flags & DS_GC_FLAG)
133 printf("GC ");
134 if (flags & DS_DS_FLAG)
135 printf("DS ");
136 if (flags & DS_LDAP_FLAG)
137 printf("LDAP ");
138 if (flags & DS_KDC_FLAG)
139 printf("KDC ");
140 if (flags & DS_TIMESERV_FLAG)
141 printf("TIMESERV ");
142 if (flags & DS_GOOD_TIMESERV_FLAG)
143 printf("GTIMESERV ");
144 if (flags & DS_WRITABLE_FLAG)
145 printf("WRITABLE ");
146 if (flags & DS_DNS_FOREST_FLAG)
147 printf("DNS_FOREST ");
148 if (flags & DS_CLOSEST_FLAG)
149 printf("CLOSE_SITE ");
150 if (flags & DS_FULL_SECRET_DOMAIN_6_FLAG)
151 printf("FULL_SECRET ");
152 /* "WS" */
153 printf("\n");
156 /****************************************************************
157 ****************************************************************/
159 static void print_dc_info(struct DOMAIN_CONTROLLER_INFO *dc_info)
161 if (dc_info->flags) {
162 printf(" DC: %s\n", dc_info->domain_controller_name);
163 printf(" Address: %s\n", dc_info->domain_controller_address);
164 /* printf(" Dom Guid: %s\n", X(domain_guid)); */
165 printf(" Dom Name: %s\n", dc_info->domain_name);
166 printf(" Forest Name: %s\n", dc_info->dns_forest_name);
167 printf(" Dc Site Name: %s\n", dc_info->dc_site_name);
168 printf("Our Site Name: %s\n", dc_info->client_site_name);
169 printf(" Flags: ");
170 print_dc_info_flags(dc_info->flags);
171 } else {
172 printf(" DC: %s\n", dc_info->domain_controller_name);
173 printf(" Address: %s\n", dc_info->domain_controller_address);
174 printf(" Dom Name: %s\n", dc_info->domain_name);
178 /****************************************************************
179 ****************************************************************/
181 int main(int argc, const char **argv)
183 int opt;
184 NET_API_STATUS status;
185 struct libnetapi_ctx *ctx = NULL;
186 char *opt_server = NULL;
187 char *opt_domain = NULL;
188 int opt_dbflag = 0;
189 int opt_pdc = 0;
190 int opt_ds = 0;
191 int opt_dsp = 0;
192 int opt_gc = 0;
193 int opt_kdc = 0;
194 int opt_timeserv = 0;
195 int opt_gtimeserv = 0;
196 int opt_ws = 0;
197 int opt_netbios = 0;
198 int opt_dns = 0;
199 int opt_ip = 0;
200 int opt_force = 0;
201 int opt_writable = 0;
202 int opt_avoidself = 0;
203 int opt_ldaponly = 0;
204 int opt_backg = 0;
205 int opt_ds_6 = 0;
206 int opt_try_next_closest_site = 0;
207 char *opt_site = NULL;
208 char *opt_account = NULL;
209 int opt_ret_dns = 0;
210 int opt_ret_netbios = 0;
211 int opt_dsregdns = 0;
212 uint32_t query_level = 0;
213 uint8_t *buffer = NULL;
214 uint32_t flags = 0;
215 struct DOMAIN_CONTROLLER_INFO *dc_info = NULL;
217 poptContext pc;
218 struct poptOption long_options[] = {
219 POPT_AUTOHELP
220 {"server", 0, POPT_ARG_STRING, &opt_server, OPT_SERVER, "Servername", "SERVER"},
221 {"dbflag", 0, POPT_ARG_INT, &opt_dbflag, OPT_DBFLAG, "New Debug Flag", "HEXFLAGS"},
222 {"sc_query", 0, POPT_ARG_STRING, &opt_domain, OPT_SC_QUERY, "Query secure channel for domain on server", "DOMAIN"},
223 {"sc_reset", 0, POPT_ARG_STRING, &opt_domain, OPT_SC_RESET, "Reset secure channel for domain on server to dcname", "DOMAIN"},
224 {"sc_verify", 0, POPT_ARG_STRING, &opt_domain, OPT_SC_VERIFY, "Verify secure channel for domain on server", "DOMAIN"},
225 {"sc_change_pwd", 0, POPT_ARG_STRING, &opt_domain, OPT_SC_CHANGE_PWD, "Change a secure channel password for domain on server", "DOMAIN"},
226 {"dsgetdc", 0, POPT_ARG_STRING, &opt_domain, OPT_DSGETDC, "Call DsGetDcName", "DOMAIN"},
227 {"pdc", 0, POPT_ARG_NONE, &opt_pdc, OPT_PDC, NULL},
228 {"ds", 0, POPT_ARG_NONE, &opt_ds, OPT_DS, NULL},
229 {"dsp", 0, POPT_ARG_NONE, &opt_dsp, OPT_DSP, NULL},
230 {"gc", 0, POPT_ARG_NONE, &opt_gc, OPT_GC, NULL},
231 {"kdc", 0, POPT_ARG_NONE, &opt_kdc, OPT_KDC, NULL},
232 {"timeserv", 0, POPT_ARG_NONE, &opt_timeserv, OPT_TIMESERV, NULL},
233 {"gtimeserv", 0, POPT_ARG_NONE, &opt_gtimeserv, OPT_GTIMESERV, NULL},
234 {"ws", 0, POPT_ARG_NONE, &opt_ws, OPT_WS, NULL},
235 {"netbios", 0, POPT_ARG_NONE, &opt_netbios, OPT_NETBIOS, NULL},
236 {"dns", 0, POPT_ARG_NONE, &opt_dns, OPT_DNS, NULL},
237 {"ip", 0, POPT_ARG_NONE, &opt_ip, OPT_IP, NULL},
238 {"force", 0, POPT_ARG_NONE, &opt_force, OPT_FORCE, NULL},
239 {"writable", 0, POPT_ARG_NONE, &opt_writable, OPT_WRITABLE, NULL},
240 {"avoidself", 0, POPT_ARG_NONE, &opt_avoidself, OPT_AVOIDSELF, NULL},
241 {"ldaponly", 0, POPT_ARG_NONE, &opt_ldaponly, OPT_LDAPONLY, NULL},
242 {"backg", 0, POPT_ARG_NONE, &opt_backg, OPT_BACKG, NULL},
243 {"ds_6", 0, POPT_ARG_NONE, &opt_ds_6, OPT_DS_6, NULL},
244 {"try_next_closest_site", 0, POPT_ARG_NONE, &opt_try_next_closest_site, OPT_TRY_NEXT_CLOSEST_SITE, NULL},
245 {"site", 0, POPT_ARG_STRING, &opt_site, OPT_SITE, "SITE"},
246 {"account", 0, POPT_ARG_STRING, &opt_account, OPT_ACCOUNT, "ACCOUNT"},
247 {"ret_dns", 0, POPT_ARG_NONE, &opt_ret_dns, OPT_RET_DNS, NULL},
248 {"ret_netbios", 0, POPT_ARG_NONE, &opt_ret_netbios, OPT_RET_NETBIOS, NULL},
249 {"dsregdns", 0, POPT_ARG_NONE, &opt_dsregdns, OPT_DSREGDNS, "Force registration of all DC-specific DNS records"},
250 POPT_COMMON_LIBNETAPI_EXAMPLES
251 POPT_TABLEEND
254 status = libnetapi_init(&ctx);
255 if (status != 0) {
256 return status;
259 pc = poptGetContext("nltest", argc, argv, long_options, 0);
261 poptSetOtherOptionHelp(pc, "<options>");
262 while((opt = poptGetNextOpt(pc)) != -1) {
265 if (argc == 1) {
266 poptPrintHelp(pc, stderr, 0);
267 goto done;
270 poptResetContext(pc);
272 while ((opt = poptGetNextOpt(pc)) != -1) {
273 switch (opt) {
275 case OPT_SERVER:
277 if ((opt_server[0] == '/' && opt_server[1] == '/') ||
278 (opt_server[0] == '\\' && opt_server[1] == '\\')) {
279 opt_server += 2;
282 break;
284 case OPT_DBFLAG:
285 query_level = 1;
286 status = I_NetLogonControl2(opt_server,
287 NETLOGON_CONTROL_SET_DBFLAG,
288 query_level,
289 (uint8_t *)opt_dbflag,
290 &buffer);
291 if (status != 0) {
292 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
293 status, status,
294 libnetapi_get_error_string(ctx, status));
295 goto done;
298 print_netlogon_info_result(query_level, buffer);
300 break;
301 case OPT_SC_QUERY:
302 query_level = 2;
303 status = I_NetLogonControl2(opt_server,
304 NETLOGON_CONTROL_TC_QUERY,
305 query_level,
306 (uint8_t *)opt_domain,
307 &buffer);
308 if (status != 0) {
309 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
310 status, status,
311 libnetapi_get_error_string(ctx, status));
312 goto done;
315 print_netlogon_info_result(query_level, buffer);
317 break;
318 case OPT_SC_VERIFY:
319 query_level = 2;
320 status = I_NetLogonControl2(opt_server,
321 NETLOGON_CONTROL_TC_VERIFY,
322 query_level,
323 (uint8_t *)opt_domain,
324 &buffer);
325 if (status != 0) {
326 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
327 status, status,
328 libnetapi_get_error_string(ctx, status));
329 goto done;
332 print_netlogon_info_result(query_level, buffer);
334 break;
335 case OPT_SC_RESET:
336 query_level = 2;
337 status = I_NetLogonControl2(opt_server,
338 NETLOGON_CONTROL_REDISCOVER,
339 query_level,
340 (uint8_t *)opt_domain,
341 &buffer);
342 if (status != 0) {
343 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
344 status, status,
345 libnetapi_get_error_string(ctx, status));
346 goto done;
349 print_netlogon_info_result(query_level, buffer);
351 break;
352 case OPT_SC_CHANGE_PWD:
353 query_level = 1;
354 status = I_NetLogonControl2(opt_server,
355 NETLOGON_CONTROL_CHANGE_PASSWORD,
356 query_level,
357 (uint8_t *)opt_domain,
358 &buffer);
359 if (status != 0) {
360 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
361 status, status,
362 libnetapi_get_error_string(ctx, status));
363 goto done;
366 print_netlogon_info_result(query_level, buffer);
368 break;
369 case OPT_DSREGDNS:
370 query_level = 1;
371 status = I_NetLogonControl2(opt_server,
372 NETLOGON_CONTROL_FORCE_DNS_REG,
373 query_level,
374 NULL,
375 &buffer);
376 if (status != 0) {
377 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
378 status, status,
379 libnetapi_get_error_string(ctx, status));
380 goto done;
383 print_netlogon_info_result(query_level, buffer);
385 break;
386 case OPT_DSGETDC:
387 if (opt_pdc)
388 flags |= DS_PDC_REQUIRED;
389 if (opt_ds)
390 flags |= DS_DIRECTORY_SERVICE_REQUIRED;
391 if (opt_dsp)
392 flags |= DS_DIRECTORY_SERVICE_PREFERRED;
393 if (opt_kdc)
394 flags |= DS_KDC_REQUIRED;
395 if (opt_timeserv)
396 flags |= DS_TIMESERV_REQUIRED;
397 if (opt_gtimeserv)
398 flags |= DS_GOOD_TIMESERV_PREFERRED;
399 if (opt_ws)
400 flags |= DS_WEB_SERVICE_REQUIRED;
401 if (opt_netbios)
402 flags |= DS_IS_FLAT_NAME;
403 if (opt_dns)
404 flags |= DS_IS_DNS_NAME;
405 if (opt_ip)
406 flags |= DS_IP_REQUIRED;
407 if (opt_force)
408 flags |= DS_FORCE_REDISCOVERY;
409 if (opt_writable)
410 flags |= DS_WRITABLE_REQUIRED;
411 if (opt_avoidself)
412 flags |= DS_AVOID_SELF;
413 if (opt_ldaponly)
414 flags |= DS_ONLY_LDAP_NEEDED;
415 if (opt_backg)
416 flags |= DS_BACKGROUND_ONLY;
417 if (opt_ds_6)
418 flags |= DS_DIRECTORY_SERVICE_6_REQUIRED;
419 if (opt_try_next_closest_site)
420 flags |= DS_TRY_NEXTCLOSEST_SITE;
421 if (opt_ret_dns)
422 flags |= DS_RETURN_DNS_NAME;
423 if (opt_ret_netbios)
424 flags |= DS_RETURN_FLAT_NAME;
426 status = DsGetDcName(opt_server,
427 opt_domain,
428 NULL, /* domain_guid */
429 opt_site,
430 flags,
431 &dc_info);
432 if (status != 0) {
433 fprintf(stderr, "DsGetDcName failed: Status = %d 0x%x %s\n",
434 status, status,
435 libnetapi_get_error_string(ctx, status));
436 goto done;
439 print_dc_info(dc_info);
441 break;
442 default:
443 continue;
447 printf("The command completed successfully\n");
448 status = 0;
450 done:
452 printf("\n");
453 libnetapi_free(ctx);
454 poptFreeContext(pc);
456 return status;