8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / idmap / nltest / nltest.c
blob002fe0f15746eb57f1cdefa6b21dfdd53c3d3f22
1 /*
2 * This file and its contents are supplied under the terms of the
3 * Common Development and Distribution License ("CDDL"), version 1.0.
4 * You may only use this file in accordance with the terms of version
5 * 1.0 of the CDDL.
7 * A full copy of the text of the CDDL should have accompanied this
8 * source. A copy of the CDDL is also available via the Internet at
9 * http://www.illumos.org/license/CDDL.
13 * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
17 #include <stdio.h>
18 #include <libintl.h>
19 #include <stdlib.h>
20 #include <strings.h>
21 #include <err.h>
22 #include <ads/dsgetdc.h>
23 #include <smb/nterror.h>
24 #include <uuid/uuid.h>
27 static void dclist_usage(void);
28 static int cmd_dclist(char *);
29 static void dcname_usage(void);
30 static int cmd_dcname(char *);
31 static void dsgetdc_usage(void);
32 static int cmd_dsgetdc(char *);
33 static void dsgetdcname_usage(void);
34 static int cmd_dsgetdcname(char *);
35 static void kick_usage(void);
36 static int cmd_kick(char *);
37 static void help(void);
39 typedef int cmd_fn_t (char *);
40 typedef void cmd_usage_t (void);
43 static struct commands {
44 const char *name; /* name of subcommand */
45 cmd_fn_t *fn; /* pointer to subcommand handler function */
46 cmd_usage_t *usage; /* pointer to subcommand help function */
47 int optreq; /* does this have a required optval */
48 } commands[] = {
49 {"dclist", cmd_dclist, dclist_usage, 0},
50 {"dcname", cmd_dcname, dcname_usage, 0},
51 {"dsgetdc", cmd_dsgetdc, dsgetdc_usage, 0},
52 {"dsgetdcname", cmd_dsgetdcname, dsgetdcname_usage, 0},
53 {"kick", cmd_kick, kick_usage, 0},
54 {NULL, NULL, NULL, 0}
59 * lookupcmd
61 static struct commands *
62 lookupcmd(const char *name)
64 struct commands *cmd;
66 for (cmd = commands; cmd->name; cmd++) {
67 if (strcasecmp(cmd->name, name) == 0)
68 return (cmd);
70 return (NULL);
74 * dclist
76 static void
77 dclist_usage(void)
79 (void) printf(gettext("usage: nltest dclist... \n"));
80 exit(1);
83 /* ARGSUSED */
84 static int
85 cmd_dclist(char *optval)
87 (void) printf("cmd_dclist() \n");
88 return (0);
92 * dcname
94 static void
95 dcname_usage(void)
97 (void) printf(gettext("usage: nltest dcname... \n"));
98 exit(1);
101 /* ARGSUSED */
102 static int
103 cmd_dcname(char *optval)
105 (void) printf("cmd_dcname() \n");
106 return (0);
110 * dsgetdc
112 static void
113 dsgetdc_usage(void)
115 (void) printf(gettext("usage: nltest dsgetdc... \n"));
116 exit(1);
119 /* ARGSUSED */
120 static int
121 cmd_dsgetdc(char *optval)
123 (void) printf("cmd_dsgetdc() \n");
124 return (0);
128 * dsgetdcname
130 static void
131 dsgetdcname_usage(void)
133 (void) printf(gettext("usage: nltest dsgetdcname domainname \n"));
134 exit(1);
137 static int
138 cmd_dsgetdcname(char *domname)
140 char uuid_buf[UUID_PRINTABLE_STRING_LENGTH];
141 int err = 0;
142 char *atype;
143 DOMAIN_CONTROLLER_INFO *dcinfo;
145 if (domname != NULL)
146 (void) printf(" Domain name supplied: %s \n", domname);
148 err = DsGetDcName(NULL, domname, NULL, NULL, 0, &dcinfo);
150 switch (err) {
151 case 0:
152 break;
153 case ERROR_NO_SUCH_DOMAIN:
154 (void) printf("Domain controller not found.\n");
155 (void) printf("See: /var/run/idmap/discovery.log\n");
156 exit(1);
157 default:
158 (void) printf("Unexpected error %d\n", err);
159 exit(1);
162 switch (dcinfo->DomainControllerAddressType) {
163 case DS_INET_ADDRESS:
164 atype = "inet";
165 break;
166 case DS_NETBIOS_ADDRESS:
167 atype = "netbios";
168 break;
169 default:
170 atype = "?";
171 break;
174 uuid_unparse(dcinfo->DomainGuid, uuid_buf);
176 (void) printf("Data Returned from DsGetDcName() call: \n");
177 (void) printf(" DC Name: %s \n", dcinfo->DomainControllerName);
178 (void) printf(" DC Addr: %s \n", dcinfo->DomainControllerAddress);
179 (void) printf(" DC Addr Type: %s \n", atype);
180 (void) printf(" Domain Name: %s \n", dcinfo->DomainName);
181 (void) printf(" Domain GUID: %s \n", uuid_buf);
182 (void) printf(" DNS Forest Name: %s \n", dcinfo->DnsForestName);
183 (void) printf(" Flags: 0x%x \n", dcinfo->Flags);
184 (void) printf(" DC Site Name: %s \n", dcinfo->DcSiteName);
185 (void) printf(" Client Site Name: %s \n", dcinfo->ClientSiteName);
187 return (0);
191 * kick
193 static void
194 kick_usage(void)
196 (void) printf(gettext("usage: nltest /KICK \n"));
197 exit(1);
201 static int
202 cmd_kick(char *domname)
204 int flags = 0;
205 int result;
207 result = _DsForceRediscovery(domname, flags);
209 return (result);
213 * help functions
216 static void
217 help(void) {
218 (void) printf("\n");
220 * TODO: We may want to revise this help text. It's basically
221 * a copy-paste from:
222 * http://technet.microsoft.com/en-us/library/cc731935.aspx
224 (void) printf(gettext("usage: %s /subcommand\n"),
225 (char *)getexecname());
226 (void) printf(gettext("where subcommands are:\n"
227 #if 0 /* not yet */
228 " dclist Lists all domain controllers in the domain.\n"
229 " dcname Lists the PDC or PDC emulator.\n"
230 " dsgetdc Queries DNS server for list of DCs and"
231 " their IP addresses and contacts each DC to check"
232 " for connectivity.\n"
233 #endif
234 " dsgetdcname returns the name of a domain controller in a"
235 " specified domain\n"
236 " help display help on specified subcommand\n"
237 " kick trigger domain controller re-discovery\n"
238 "\n"));
239 exit(1);
243 main(int argc, char *argv[])
245 struct commands *cmd;
246 int err = 0;
247 char *option_cmd = NULL;
248 char *arg;
249 char *p;
250 char *optname;
251 char *optval = NULL;
252 int i;
253 int optind = 1;
256 * Parse options.
258 while (optind < argc) {
259 arg = argv[optind];
260 optname = NULL;
261 optval = NULL;
263 /* Is this an option? */
264 if (arg[0] == '/') {
265 optname = arg + 1;
266 optind++;
269 * May have /optname:value
271 if ((p = strchr(optname, ':')) != NULL) {
272 *p++ = '\0';
273 optval = p;
275 } else if (arg[0] == '-' && arg[1] == '-') {
276 optname = arg + 2;
277 optind++;
280 * May have --optname=value
282 if ((p = strchr(optname, '=')) != NULL) {
283 *p++ = '\0';
284 optval = p;
286 } else {
287 /* Not an option. Stop parsing. */
288 break;
292 * Handle each optname (and maybe its optval)
293 * Might put this logic in a table of options.
294 * (including a flag for "optval required",
295 * so that check could be factored out)
297 for (cmd = commands; cmd->name; cmd++) {
298 if (!strcasecmp(optname, cmd->name)) {
299 /* cmd->name requires an optval */
300 if (optval == NULL && optind < argc)
301 optval = argv[optind++];
303 if (optval == NULL && cmd->optreq > 0) {
304 (void) fprintf(stderr,
305 "%s: option %s requires a value\n",
306 argv[0], optname);
307 return (1);
309 option_cmd = optname;
315 * Handle remaining non-option arguments
317 for (i = optind; i < argc; i++) {
318 (void) printf("arg: %s\n", argv[i]);
321 if (option_cmd == NULL)
322 help();
324 cmd = lookupcmd(option_cmd);
325 if (cmd == NULL)
326 err = 1;
327 else
328 err = cmd->fn(optval);
330 return (err);