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