s3-libnetapi: add I_NetLogonControl{2} example code.
[Samba.git] / source3 / lib / netapi / examples / netlogon / netlogon_control.c
blob34361cdec2ea94dd86b39c5df5b98143d8a95621
1 /*
2 * Unix SMB/CIFS implementation.
3 * I_NetLogonControl query
4 * Copyright (C) Guenther Deschner 2009
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;
34 const char *hostname = NULL;
35 uint32_t function_code = NETLOGON_CONTROL_QUERY;
36 uint32_t level = 1;
37 uint8_t *buffer = NULL;
38 struct NETLOGON_INFO_1 *i1 = NULL;
39 struct NETLOGON_INFO_2 *i2 = NULL;
40 struct NETLOGON_INFO_3 *i3 = NULL;
41 struct NETLOGON_INFO_4 *i4 = NULL;
43 poptContext pc;
44 int opt;
46 struct poptOption long_options[] = {
47 POPT_AUTOHELP
48 POPT_COMMON_LIBNETAPI_EXAMPLES
49 POPT_TABLEEND
52 status = libnetapi_init(&ctx);
53 if (status != 0) {
54 return status;
57 pc = poptGetContext("netlogon_control", argc, argv, long_options, 0);
59 poptSetOtherOptionHelp(pc, "hostname");
60 while((opt = poptGetNextOpt(pc)) != -1) {
63 if (!poptPeekArg(pc)) {
64 poptPrintHelp(pc, stderr, 0);
65 goto out;
67 hostname = poptGetArg(pc);
69 if (poptPeekArg(pc)) {
70 function_code = atoi(poptGetArg(pc));
73 if (poptPeekArg(pc)) {
74 level = atoi(poptGetArg(pc));
77 /* I_NetLogonControl */
79 status = I_NetLogonControl(hostname,
80 function_code,
81 level,
82 &buffer);
83 if (status != 0) {
84 printf("I_NetLogonControl failed with: %s\n",
85 libnetapi_get_error_string(ctx, status));
86 goto out;
89 if (!buffer) {
90 goto out;
93 switch (level) {
94 case 1:
95 i1 = (struct NETLOGON_INFO_1 *)buffer;
97 printf("Flags: %x\n", i1->netlog1_flags);
98 printf("Connection Status Status = %d 0x%x %s\n",
99 i1->netlog1_pdc_connection_status,
100 i1->netlog1_pdc_connection_status,
101 libnetapi_errstr(i1->netlog1_pdc_connection_status));
103 break;
104 case 2:
105 i2 = (struct NETLOGON_INFO_2 *)buffer;
107 printf("Flags: %x\n", i2->netlog2_flags);
108 printf("Trusted DC Name %s\n", i2->netlog2_trusted_dc_name);
109 printf("Trusted DC Connection Status Status = %d 0x%x %s\n",
110 i2->netlog2_tc_connection_status,
111 i2->netlog2_tc_connection_status,
112 libnetapi_errstr(i2->netlog2_tc_connection_status));
113 printf("Trust Verification Status Status = %d 0x%x %s\n",
114 i2->netlog2_pdc_connection_status,
115 i2->netlog2_pdc_connection_status,
116 libnetapi_errstr(i2->netlog2_pdc_connection_status));
118 break;
119 case 3:
120 i3 = (struct NETLOGON_INFO_3 *)buffer;
122 printf("Flags: %x\n", i3->netlog1_flags);
123 printf("Logon Attempts: %d\n", i3->netlog3_logon_attempts);
125 break;
126 case 4:
127 i4 = (struct NETLOGON_INFO_4 *)buffer;
129 printf("Trusted DC Name %s\n", i4->netlog4_trusted_dc_name);
130 printf("Trusted Domain Name %s\n", i4->netlog4_trusted_domain_name);
132 break;
133 default:
134 break;
137 out:
138 NetApiBufferFree(buffer);
139 libnetapi_free(ctx);
140 poptFreeContext(pc);
142 return status;