netapi: add -f switch for DsGetDCName() example and be more verbose on output.
[Samba.git] / source / lib / netapi / examples / dsgetdc / dsgetdc.c
blob6265e66a257bc72ff62640260a48d60decccf02f
1 /*
2 * Unix SMB/CIFS implementation.
3 * DsGetDcName query
4 * Copyright (C) Guenther Deschner 2008
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <sys/types.h>
21 #include <inttypes.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include <netapi.h>
28 #include "common.h"
30 int main(int argc, const char **argv)
32 NET_API_STATUS status;
33 struct libnetapi_ctx *ctx = NULL;
35 const char *hostname = NULL;
36 const char *domain = NULL;
37 uint32_t flags = 0;
38 struct DOMAIN_CONTROLLER_INFO *info = NULL;
40 poptContext pc;
41 int opt;
43 struct poptOption long_options[] = {
44 POPT_AUTOHELP
45 POPT_COMMON_LIBNETAPI_EXAMPLES
46 { "flags", 0, POPT_ARG_INT, NULL, 'f', "Query flags", "FLAGS" },
47 POPT_TABLEEND
50 status = libnetapi_init(&ctx);
51 if (status != 0) {
52 return status;
55 pc = poptGetContext("dsgetdc", argc, argv, long_options, 0);
57 poptSetOtherOptionHelp(pc, "hostname domainname");
58 while((opt = poptGetNextOpt(pc)) != -1) {
59 switch (opt) {
60 case 'f':
61 sscanf(poptGetOptArg(pc), "%x", &flags);
65 if (!poptPeekArg(pc)) {
66 poptPrintHelp(pc, stderr, 0);
67 goto out;
69 hostname = poptGetArg(pc);
71 if (!poptPeekArg(pc)) {
72 poptPrintHelp(pc, stderr, 0);
73 goto out;
75 domain = poptGetArg(pc);
77 /* DsGetDcName */
79 status = DsGetDcName(hostname, domain, NULL, NULL, flags, &info);
80 if (status != 0) {
81 printf("DsGetDcName failed with: %s\n",
82 libnetapi_errstr(status));
83 return status;
86 printf("DC Name:\t\t%s\n", info->domain_controller_name);
87 printf("DC Address:\t\t%s\n", info->domain_controller_address);
88 printf("DC Address Type:\t%d\n", info->domain_controller_address_type);
89 printf("Domain Name:\t\t%s\n", info->domain_name);
90 printf("DNS Forest Name:\t%s\n", info->dns_forest_name);
91 printf("DC flags:\t\t0x%08x\n", info->flags);
92 printf("DC Sitename:\t\t%s\n", info->dc_site_name);
93 printf("Client Sitename:\t%s\n", info->client_site_name);
95 out:
96 NetApiBufferFree(info);
97 libnetapi_free(ctx);
98 poptFreeContext(pc);
100 return status;