2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
25 * Modification History
27 * June 13, 2005 Allan Nathanson <ajn@apple.com>
28 * - added SCPreferences support
30 * August 4, 2004 Allan Nathanson <ajn@apple.com>
31 * - added network configuration (prefs) support
33 * September 25, 2002 Allan Nathanson <ajn@apple.com>
34 * - added command line history & editing
36 * July 9, 2001 Allan Nathanson <ajn@apple.com>
37 * - added "-r" option for checking network reachability
38 * - added "-w" option to check/wait for the presence of a
41 * June 1, 2001 Allan Nathanson <ajn@apple.com>
42 * - public API conversion
44 * November 9, 2000 Allan Nathanson <ajn@apple.com>
48 #include <TargetConditionals.h>
59 #include <mach/mach.h>
60 #include <mach/mach_error.h>
65 #include "dictionary.h"
73 #define LINE_LENGTH 2048
76 __private_extern__ AuthorizationRef authorization
= NULL
;
77 __private_extern__ InputRef currentInput
= NULL
;
78 __private_extern__ Boolean doDispatch
= FALSE
;
79 __private_extern__
int nesting
= 0;
80 __private_extern__ CFRunLoopRef notifyRl
= NULL
;
81 __private_extern__ CFRunLoopSourceRef notifyRls
= NULL
;
82 __private_extern__ SCPreferencesRef prefs
= NULL
;
83 __private_extern__ SCDynamicStoreRef store
= NULL
;
84 __private_extern__ CFPropertyListRef value
= NULL
;
85 __private_extern__ CFMutableArrayRef watchedKeys
= NULL
;
86 __private_extern__ CFMutableArrayRef watchedPatterns
= NULL
;
88 static const struct option longopts
[] = {
89 // { "debug", no_argument, NULL, 'd' },
90 // { "dispatch", no_argument, NULL, 'D' },
91 // { "verbose", no_argument, NULL, 'v' },
92 // { "SPI", no_argument, NULL, 'p' },
93 // { "check-reachability", required_argument, NULL, 'r' },
94 // { "timeout", required_argument, NULL, 't' },
95 // { "wait-key", required_argument, NULL, 'w' },
96 // { "watch-reachability", no_argument, NULL, 'W' },
97 { "dns", no_argument
, NULL
, 0 },
98 { "get", required_argument
, NULL
, 0 },
99 { "error", required_argument
, NULL
, 0 },
100 { "help", no_argument
, NULL
, '?' },
101 { "nc", required_argument
, NULL
, 0 },
102 { "net", no_argument
, NULL
, 0 },
103 { "nwi", no_argument
, NULL
, 0 },
104 { "prefs", no_argument
, NULL
, 0 },
105 { "proxy", no_argument
, NULL
, 0 },
106 { "renew", required_argument
, NULL
, 0 },
107 { "set", required_argument
, NULL
, 0 },
108 { "snapshot", no_argument
, NULL
, 0 },
109 { "user", required_argument
, NULL
, 0 },
110 { "password", required_argument
, NULL
, 0 },
111 { "secret", required_argument
, NULL
, 0 },
112 { "log", required_argument
, NULL
, 0 },
113 { "disable-until-needed", no_argument
, NULL
, 0 },
120 _copyStringFromSTDIN(CFStringRef prompt
, CFStringRef defaultValue
)
124 Boolean is_user_prompt
= (prompt
!= NULL
&& isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
));
130 /* Print out a prompt to user that entry is desired */
131 if (is_user_prompt
) {
132 if (defaultValue
!= NULL
) {
133 SCPrint(TRUE
, stdout
, CFSTR("%@ [%@]: "), prompt
, defaultValue
);
135 SCPrint(TRUE
, stdout
, CFSTR("%@: "), prompt
);
140 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
144 /* Prepare for trim */
145 len
= (int)strlen(buf
);
150 if ((modlen
> 0) && (modbuf
[modlen
- 1] == '\n')) {
151 modbuf
[modlen
- 1] = '\0';
155 /* If nothing was entered at the user prompt, set default */
156 if (is_user_prompt
&& defaultValue
!= NULL
&& modlen
== 0) {
157 CFRetain(defaultValue
);
161 /* Trim spaces from front */
162 while (modlen
> 0 && isspace(modbuf
[0])) {
167 /* Trim spaces from back */
168 for (i
= modlen
- 1; i
>= 0; i
--) {
169 if (isspace(buf
[i
])) {
177 utf8
= CFStringCreateWithBytes(NULL
, (UInt8
*)modbuf
, modlen
, kCFStringEncodingUTF8
, TRUE
);
182 getLine(char *buf
, int len
, InputRef src
)
190 line
= el_gets(src
->el
, &count
);
194 strncpy(buf
, line
, len
);
196 if (fgets(buf
, len
, src
->fp
) == NULL
)
200 n
= (int)strlen(buf
);
201 if (buf
[n
-1] == '\n') {
202 /* the entire line fit in the buffer, remove the newline */
204 } else if (!src
->el
) {
205 /* eat the remainder of the line */
208 } while ((n
!= '\n') && (n
!= EOF
));
211 if (src
->h
&& (buf
[0] != '\0')) {
214 history(src
->h
, &ev
, H_ENTER
, buf
);
223 getString(char **line
)
225 char *s
, *e
, c
, *string
;
226 int i
, isQuoted
= 0, escaped
= 0;
228 if (*line
== NULL
) return NULL
;
229 if (**line
== '\0') return NULL
;
231 /* Skip leading white space */
232 while (isspace(**line
)) *line
+= 1;
234 /* Grab the next string */
237 return NULL
; /* no string available */
238 } else if (*s
== '"') {
239 isQuoted
= 1; /* it's a quoted string */
243 for (e
= s
; (c
= *e
) != '\0'; e
++) {
244 if (isQuoted
&& (c
== '"'))
245 break; /* end of quoted string */
249 break; /* if premature end-of-string */
250 if ((*e
== '"') || isspace(*e
))
251 escaped
++; /* if escaped quote or white space */
253 if (!isQuoted
&& isspace(c
))
254 break; /* end of non-quoted string */
257 string
= malloc(e
- s
- escaped
+ 1);
259 for (i
= 0; s
< e
; s
++) {
261 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
266 e
++; /* move past end of quoted string */
275 process_line(InputRef src
)
281 char line
[LINE_LENGTH
];
284 // if end-of-file, exit
285 if (getLine(line
, sizeof(line
), src
) == NULL
)
289 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
292 // break up the input line
293 while ((arg
= getString(&s
)) != NULL
) {
295 argv
= (char **)malloc(2 * sizeof(char *));
297 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
302 return TRUE
; // if no arguments
305 /* process the command */
306 if (*argv
[0] != '#') {
307 argv
[argc
] = NULL
; // just in case...
309 do_command(argc
, argv
);
312 /* free the arguments */
313 for (i
= 0; i
< argc
; i
++) {
318 return !termRequested
;
323 usage(const char *command
)
325 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
326 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
327 SCPrint(TRUE
, stderr
, CFSTR("\n"));
328 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs [preference-file]\n"), command
);
329 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
330 SCPrint(TRUE
, stderr
, CFSTR("\n"));
331 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
332 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
333 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
334 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
335 SCPrint(TRUE
, stderr
, CFSTR("\n"));
336 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
337 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
338 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
339 SCPrint(TRUE
, stderr
, CFSTR("\n"));
340 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
341 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
342 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get filename path key \n"), command
);
343 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
344 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
345 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
346 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
347 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
348 SCPrint(TRUE
, stderr
, CFSTR("\n"));
349 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
350 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
351 SCPrint(TRUE
, stderr
, CFSTR("\n"));
352 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
353 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
354 SCPrint(TRUE
, stderr
, CFSTR("\n"));
355 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nwi\n"), command
);
356 SCPrint(TRUE
, stderr
, CFSTR("\tshow network information\n"));
357 SCPrint(TRUE
, stderr
, CFSTR("\n"));
358 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nc\n"), command
);
359 SCPrint(TRUE
, stderr
, CFSTR("\tshow VPN network configuration information. Use --nc help for full command list\n"));
362 SCPrint(TRUE
, stderr
, CFSTR("\n"));
363 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --log IPMonitor [off|on]\n"), command
);
364 SCPrint(TRUE
, stderr
, CFSTR("\tmanage logging.\n"));
367 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
368 SCPrint(TRUE
, stderr
, CFSTR("\n"));
369 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
370 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
373 SCPrint(TRUE
, stderr
, CFSTR("\n"));
374 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --error err#\n"), command
);
375 SCPrint(TRUE
, stderr
, CFSTR("\tdisplay a descriptive message for the given error code\n"));
384 #if !TARGET_IPHONE_SIMULATOR
386 #else // !TARGET_IPHONE_SIMULATOR
388 #endif // !TARGET_IPHONE_SIMULATOR
393 main(int argc
, char * const argv
[])
395 Boolean disableUntilNeeded
= FALSE
;
396 Boolean doDNS
= FALSE
;
397 Boolean doNet
= FALSE
;
398 Boolean doNWI
= FALSE
;
399 Boolean doPrefs
= FALSE
;
400 Boolean doProxy
= FALSE
;
401 Boolean doReach
= FALSE
;
402 Boolean doSnap
= FALSE
;
409 const char *prog
= argv
[0];
414 int timeout
= 15; /* default timeout (in seconds) */
416 Boolean watch
= FALSE
;
417 int xStore
= 0; /* non dynamic store command line options */
419 /* process any arguments */
421 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1)
425 _sc_log
= FALSE
; /* enable framework logging */
432 _sc_log
= FALSE
; /* enable framework logging */
435 enablePrivateAPI
= TRUE
;
442 timeout
= atoi(optarg
);
452 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
455 } else if (strcmp(longopts
[opti
].name
, "error") == 0) {
458 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
461 } else if (strcmp(longopts
[opti
].name
, "nc") == 0) {
464 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
467 } else if (strcmp(longopts
[opti
].name
, "nwi") == 0) {
470 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
473 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
476 } else if (strcmp(longopts
[opti
].name
, "renew") == 0) {
479 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
482 } else if (strcmp(longopts
[opti
].name
, "snapshot") == 0) {
485 } else if (strcmp(longopts
[opti
].name
, "log") == 0) {
488 } else if (strcmp(longopts
[opti
].name
, "disable-until-needed") == 0) {
489 disableUntilNeeded
= TRUE
;
491 } else if (strcmp(longopts
[opti
].name
, "user") == 0) {
492 username
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
493 } else if (strcmp(longopts
[opti
].name
, "password") == 0) {
494 password
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
495 } else if (strcmp(longopts
[opti
].name
, "secret") == 0) {
496 sharedsecret
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
507 // if we are attempting to process more than one type of request
511 /* are we checking (or watching) the reachability of a host/address */
517 do_watchReachability(argc
, (char **)argv
);
519 do_checkReachability(argc
, (char **)argv
);
524 /* are we waiting on the presense of a dynamic store key */
526 do_wait(wait
, timeout
);
530 /* are we looking up the DNS configuration */
533 do_watchDNSConfiguration(argc
, (char **)argv
);
535 do_showDNSConfiguration(argc
, (char **)argv
);
542 do_watchNWI(argc
, (char**)argv
);
544 do_showNWI(argc
, (char**)argv
);
550 if (!enablePrivateAPI
551 #if !TARGET_IPHONE_SIMULATOR
553 #endif // !TARGET_IPHONE_SIMULATOR
558 do_open(0, NULL
); /* open the dynamic store */
559 do_snapshot(argc
, (char**)argv
);
563 /* are we translating error #'s to descriptive text */
565 int sc_status
= atoi(error
);
567 SCPrint(TRUE
, stdout
, CFSTR("Error: 0x%08x %d %s\n"),
570 SCErrorString(sc_status
));
574 /* are we looking up a preference value */
577 if (findPref(get
) < 0) {
581 /* need to go back one argument
582 * for the filename */
587 do_getPref(get
, argc
, (char **)argv
);
591 /* are we looking up the proxy configuration */
593 do_showProxyConfiguration(argc
, (char **)argv
);
597 /* are we changing a preference value */
599 if (findPref(set
) < 0) {
602 do_setPref(set
, argc
, (char **)argv
);
608 if (strcasecmp(log
, "IPMonitor")) {
611 do_log(log
, argc
, (char * *)argv
);
615 /* disableUntilNeeded */
616 if (disableUntilNeeded
) {
617 do_disable_until_needed(argc
, (char * *)argv
);
620 /* network connection commands */
622 if (find_nc_cmd(nc_cmd
) < 0) {
625 do_nc_cmd(nc_cmd
, argc
, (char **)argv
, watch
);
630 /* if we are going to be managing the network configuration */
631 commands
= (cmdInfo
*)commands_net
;
632 nCommands
= nCommands_net
;
634 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
638 do_net_init(); /* initialization */
639 do_net_open(argc
, (char **)argv
); /* open prefs */
640 } else if (doPrefs
) {
641 /* if we are going to be managing the network configuration */
642 commands
= (cmdInfo
*)commands_prefs
;
643 nCommands
= nCommands_prefs
;
645 do_dictInit(0, NULL
); /* start with an empty dictionary */
646 do_prefs_init(); /* initialization */
647 do_prefs_open(argc
, (char **)argv
); /* open prefs */
649 /* if we are going to be managing the dynamic store */
650 commands
= (cmdInfo
*)commands_store
;
651 nCommands
= nCommands_store
;
653 do_dictInit(0, NULL
); /* start with an empty dictionary */
654 do_open(0, NULL
); /* open the dynamic store */
657 /* are we trying to renew a DHCP lease */
663 /* allocate command input stream */
664 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
669 if (isatty(fileno(src
->fp
))) {
674 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
675 if ((t
.c_lflag
& ECHO
) == 0) {
679 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
680 src
->h
= history_init();
682 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
683 el_set(src
->el
, EL_HIST
, history
, src
->h
);
686 el_set(src
->el
, EL_EDITMODE
, 0);
689 el_set(src
->el
, EL_EDITOR
, "emacs");
690 el_set(src
->el
, EL_PROMPT
, prompt
);
692 el_source(src
->el
, NULL
);
694 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
695 el_set(src
->el
, EL_SIGNAL
, 1);
707 ok
= process_line(src
);
713 /* close the socket, free resources */
714 if (src
->h
) history_end(src
->h
);
715 if (src
->el
) el_end(src
->el
);
716 (void)fclose(src
->fp
);
717 CFAllocatorDeallocate(NULL
, src
);
719 exit (EX_OK
); // insure the process exit status is 0
720 return 0; // ...and make main fit the ANSI spec.