Merge branch 'master' of ssh://jra@git.samba.org/data/git/samba
[kai/samba-autobuild/.git] / source3 / lib / netapi / examples / netlogon / nltest.c
1 /*
2  * Samba Unix/Linux SMB client library
3  * Distributed SMB/CIFS Server Management Utility
4  * Nltest netlogon testing tool
5  *
6  * Copyright (C) Guenther Deschner 2009
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #include <sys/types.h>
23 #include <inttypes.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <netapi.h>
29
30 #include "common.h"
31
32 enum {
33         OPT_DBFLAG = 1,
34         OPT_SC_QUERY,
35         OPT_SC_RESET,
36         OPT_SC_VERIFY,
37         OPT_SC_CHANGE_PWD
38 };
39
40 /****************************************************************
41 ****************************************************************/
42
43 static void print_result(uint32_t level,
44                          uint8_t *buffer)
45 {
46         struct NETLOGON_INFO_1 *i1 = NULL;
47         struct NETLOGON_INFO_2 *i2 = NULL;
48         struct NETLOGON_INFO_3 *i3 = NULL;
49         struct NETLOGON_INFO_4 *i4 = NULL;
50
51         if (!buffer) {
52                 return;
53         }
54
55         switch (level) {
56         case 1:
57                 i1 = (struct NETLOGON_INFO_1 *)buffer;
58
59                 printf("Flags: %x\n", i1->netlog1_flags);
60                 printf("Connection Status Status = %d 0x%x %s\n",
61                         i1->netlog1_pdc_connection_status,
62                         i1->netlog1_pdc_connection_status,
63                         libnetapi_errstr(i1->netlog1_pdc_connection_status));
64
65                 break;
66         case 2:
67                 i2 = (struct NETLOGON_INFO_2 *)buffer;
68
69                 printf("Flags: %x\n", i2->netlog2_flags);
70                 printf("Trusted DC Name %s\n", i2->netlog2_trusted_dc_name);
71                 printf("Trusted DC Connection Status Status = %d 0x%x %s\n",
72                         i2->netlog2_tc_connection_status,
73                         i2->netlog2_tc_connection_status,
74                         libnetapi_errstr(i2->netlog2_tc_connection_status));
75                 printf("Trust Verification Status Status = %d 0x%x %s\n",
76                         i2->netlog2_pdc_connection_status,
77                         i2->netlog2_pdc_connection_status,
78                         libnetapi_errstr(i2->netlog2_pdc_connection_status));
79
80                 break;
81         case 3:
82                 i3 = (struct NETLOGON_INFO_3 *)buffer;
83
84                 printf("Flags: %x\n", i3->netlog1_flags);
85                 printf("Logon Attempts: %d\n", i3->netlog3_logon_attempts);
86
87                 break;
88         case 4:
89                 i4 = (struct NETLOGON_INFO_4 *)buffer;
90
91                 printf("Trusted DC Name %s\n", i4->netlog4_trusted_dc_name);
92                 printf("Trusted Domain Name %s\n", i4->netlog4_trusted_domain_name);
93
94                 break;
95         default:
96                 break;
97         }
98 }
99
100 /****************************************************************
101 ****************************************************************/
102
103 int main(int argc, const char **argv)
104 {
105         int opt;
106         NET_API_STATUS status;
107         struct libnetapi_ctx *ctx = NULL;
108         const char *server_name = NULL;
109         char *opt_domain = NULL;
110         int opt_dbflag = 0;
111         uint32_t query_level;
112         uint8_t *buffer = NULL;
113
114         poptContext pc;
115         struct poptOption long_options[] = {
116                 POPT_AUTOHELP
117                 {"dbflag", 0, POPT_ARG_INT,   &opt_dbflag, OPT_DBFLAG, "New Debug Flag", "HEXFLAGS"},
118                 {"sc_query", 0, POPT_ARG_STRING,   &opt_domain, OPT_SC_QUERY, "Query secure channel for domain on server", "DOMAIN"},
119                 {"sc_reset", 0, POPT_ARG_STRING,   &opt_domain, OPT_SC_RESET, "Reset secure channel for domain on server to dcname", "DOMAIN"},
120                 {"sc_verify", 0, POPT_ARG_STRING,   &opt_domain, OPT_SC_VERIFY, "Verify secure channel for domain on server", "DOMAIN"},
121                 {"sc_change_pwd", 0, POPT_ARG_STRING,   &opt_domain, OPT_SC_CHANGE_PWD, "Change a secure channel password for domain on server", "DOMAIN"},
122                 POPT_COMMON_LIBNETAPI_EXAMPLES
123                 POPT_TABLEEND
124         };
125
126         status = libnetapi_init(&ctx);
127         if (status != 0) {
128                 return status;
129         }
130
131         pc = poptGetContext("nltest", argc, argv, long_options, 0);
132
133         poptSetOtherOptionHelp(pc, "server_name");
134         while((opt = poptGetNextOpt(pc)) != -1) {
135         }
136
137         if (!poptPeekArg(pc)) {
138                 poptPrintHelp(pc, stderr, 0);
139                 goto done;
140         }
141         server_name = poptGetArg(pc);
142
143         if (argc == 1) {
144                 poptPrintHelp(pc, stderr, 0);
145                 goto done;
146         }
147
148         if (!server_name || poptGetArg(pc)) {
149                 poptPrintHelp(pc, stderr, 0);
150                 goto done;
151         }
152
153         if ((server_name[0] == '/' && server_name[1] == '/') ||
154             (server_name[0] == '\\' && server_name[1] ==  '\\')) {
155                 server_name += 2;
156         }
157
158         poptResetContext(pc);
159
160         while ((opt = poptGetNextOpt(pc)) != -1) {
161                 switch (opt) {
162
163                 case OPT_DBFLAG:
164                         query_level = 1;
165                         status = I_NetLogonControl2(server_name,
166                                                     NETLOGON_CONTROL_SET_DBFLAG,
167                                                     query_level,
168                                                     (uint8_t *)opt_dbflag,
169                                                     &buffer);
170                         if (status != 0) {
171                                 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
172                                         status, status,
173                                         libnetapi_get_error_string(ctx, status));
174                                 goto done;
175                         }
176                         break;
177                 case OPT_SC_QUERY:
178                         query_level = 2;
179                         status = I_NetLogonControl2(server_name,
180                                                     NETLOGON_CONTROL_TC_QUERY,
181                                                     query_level,
182                                                     (uint8_t *)opt_domain,
183                                                     &buffer);
184                         if (status != 0) {
185                                 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
186                                         status, status,
187                                         libnetapi_get_error_string(ctx, status));
188                                 goto done;
189                         }
190                         break;
191                 case OPT_SC_VERIFY:
192                         query_level = 2;
193                         status = I_NetLogonControl2(server_name,
194                                                     NETLOGON_CONTROL_TC_VERIFY,
195                                                     query_level,
196                                                     (uint8_t *)opt_domain,
197                                                     &buffer);
198                         if (status != 0) {
199                                 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
200                                         status, status,
201                                         libnetapi_get_error_string(ctx, status));
202                                 goto done;
203                         }
204                         break;
205                 case OPT_SC_RESET:
206                         query_level = 2;
207                         status = I_NetLogonControl2(server_name,
208                                                     NETLOGON_CONTROL_REDISCOVER,
209                                                     query_level,
210                                                     (uint8_t *)opt_domain,
211                                                     &buffer);
212                         if (status != 0) {
213                                 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
214                                         status, status,
215                                         libnetapi_get_error_string(ctx, status));
216                                 goto done;
217                         }
218                         break;
219                 case OPT_SC_CHANGE_PWD:
220                         query_level = 1;
221                         status = I_NetLogonControl2(server_name,
222                                                     NETLOGON_CONTROL_CHANGE_PASSWORD,
223                                                     query_level,
224                                                     (uint8_t *)opt_domain,
225                                                     &buffer);
226                         if (status != 0) {
227                                 fprintf(stderr, "I_NetlogonControl failed: Status = %d 0x%x %s\n",
228                                         status, status,
229                                         libnetapi_get_error_string(ctx, status));
230                                 goto done;
231                         }
232                         break;
233                 default:
234                         poptPrintHelp(pc, stderr, 0);
235                         goto done;
236                 }
237         }
238
239         print_result(query_level, buffer);
240
241         printf("The command completed successfully\n");
242         status = 0;
243
244  done:
245
246         printf("\n");
247         libnetapi_free(ctx);
248         poptFreeContext(pc);
249
250         return status;
251 }