2 * Copyright (c) 2000-2018 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"
72 #define LINE_LENGTH 2048
74 __private_extern__ AuthorizationRef authorization
= NULL
;
75 __private_extern__ InputRef currentInput
= NULL
;
76 __private_extern__ Boolean doDispatch
= FALSE
;
77 __private_extern__
int nesting
= 0;
78 __private_extern__ CFRunLoopRef notifyRl
= NULL
;
79 __private_extern__ CFRunLoopSourceRef notifyRls
= NULL
;
80 __private_extern__ SCPreferencesRef prefs
= NULL
;
81 __private_extern__ SCDynamicStoreRef store
= NULL
;
82 __private_extern__ CFPropertyListRef value
= NULL
;
83 __private_extern__ CFMutableArrayRef watchedKeys
= NULL
;
84 __private_extern__ CFMutableArrayRef watchedPatterns
= NULL
;
86 static const struct option longopts
[] = {
87 // { "debug", no_argument, NULL, 'd' },
88 // { "dispatch", no_argument, NULL, 'D' },
89 // { "verbose", no_argument, NULL, 'v' },
90 // { "SPI", no_argument, NULL, 'p' },
91 // { "check-reachability", required_argument, NULL, 'r' },
92 // { "timeout", required_argument, NULL, 't' },
93 // { "wait-key", required_argument, NULL, 'w' },
94 // { "watch-reachability", no_argument, NULL, 'W' },
95 { "dns", no_argument
, NULL
, 0 },
96 { "get", required_argument
, NULL
, 0 },
97 { "error", required_argument
, NULL
, 0 },
98 { "help", no_argument
, NULL
, '?' },
99 { "nc", required_argument
, NULL
, 0 },
100 { "net", no_argument
, NULL
, 0 },
101 { "nwi", no_argument
, NULL
, 0 },
102 { "prefs", no_argument
, NULL
, 0 },
103 { "proxy", no_argument
, NULL
, 0 },
104 { "renew", required_argument
, NULL
, 0 },
105 { "set", required_argument
, NULL
, 0 },
106 { "snapshot", no_argument
, NULL
, 0 },
107 { "user", required_argument
, NULL
, 0 },
108 { "password", required_argument
, NULL
, 0 },
109 { "secret", required_argument
, NULL
, 0 },
110 { "log", required_argument
, NULL
, 0 },
111 { "advisory", required_argument
, NULL
, 0 },
112 #if !TARGET_OS_IPHONE
113 { "allow-new-interfaces", no_argument
, NULL
, 0 },
114 #endif // !TARGET_OS_IPHONE
115 { "disable-until-needed", no_argument
, NULL
, 0 },
122 _copyStringFromSTDIN(CFStringRef prompt
, CFStringRef defaultValue
)
126 Boolean is_user_prompt
= (prompt
!= NULL
&& isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
));
132 /* Print out a prompt to user that entry is desired */
133 if (is_user_prompt
) {
134 if (defaultValue
!= NULL
) {
135 SCPrint(TRUE
, stdout
, CFSTR("%@ [%@]: "), prompt
, defaultValue
);
137 SCPrint(TRUE
, stdout
, CFSTR("%@: "), prompt
);
142 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
146 /* Prepare for trim */
147 len
= (int)strlen(buf
);
152 if ((modlen
> 0) && (modbuf
[modlen
- 1] == '\n')) {
153 modbuf
[modlen
- 1] = '\0';
157 /* If nothing was entered at the user prompt, set default */
158 if (is_user_prompt
&& defaultValue
!= NULL
&& modlen
== 0) {
159 CFRetain(defaultValue
);
163 /* Trim spaces from front */
164 while (modlen
> 0 && isspace(modbuf
[0])) {
169 /* Trim spaces from back */
170 for (i
= modlen
- 1; i
>= 0; i
--) {
171 if (isspace(buf
[i
])) {
179 utf8
= CFStringCreateWithBytes(NULL
, (UInt8
*)modbuf
, modlen
, kCFStringEncodingUTF8
, TRUE
);
184 getLine(char *buf
, int len
, InputRef src
)
192 line
= el_gets(src
->el
, &count
);
196 strlcpy(buf
, line
, len
);
198 if (fgets(buf
, len
, src
->fp
) == NULL
)
202 n
= (int)strlen(buf
);
203 if (buf
[n
-1] == '\n') {
204 /* the entire line fit in the buffer, remove the newline */
206 } else if (!src
->el
) {
207 /* eat the remainder of the line */
210 } while ((n
!= '\n') && (n
!= EOF
));
213 if (src
->h
&& (buf
[0] != '\0')) {
216 history(src
->h
, &ev
, H_ENTER
, buf
);
224 getString(char **line
)
226 char *s
, *e
, c
, *string
;
227 int i
, isQuoted
= 0, escaped
= 0;
229 if (*line
== NULL
) return NULL
;
230 if (**line
== '\0') return NULL
;
232 /* Skip leading white space */
233 while (isspace(**line
)) *line
+= 1;
235 /* Grab the next string */
238 return NULL
; /* no string available */
239 } else if (*s
== '"') {
240 isQuoted
= 1; /* it's a quoted string */
244 for (e
= s
; (c
= *e
) != '\0'; e
++) {
245 if (isQuoted
&& (c
== '"'))
246 break; /* end of quoted string */
250 break; /* if premature end-of-string */
251 if ((*e
== '"') || isspace(*e
))
252 escaped
++; /* if escaped quote or white space */
254 if (!isQuoted
&& isspace(c
))
255 break; /* end of non-quoted string */
258 string
= malloc(e
- s
- escaped
+ 1);
260 for (i
= 0; s
< e
; s
++) {
262 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
267 e
++; /* move past end of quoted string */
276 process_line(InputRef src
)
282 char line
[LINE_LENGTH
];
285 // if end-of-file, exit
286 if (getLine(line
, sizeof(line
), src
) == NULL
)
290 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
293 // break up the input line
294 while ((arg
= getString(&s
)) != NULL
) {
296 argv
= (char **)malloc(2 * sizeof(char *));
298 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
303 return TRUE
; // if no arguments
306 /* process the command */
307 if (*argv
[0] != '#') {
308 argv
[argc
] = NULL
; // just in case...
310 do_command(argc
, argv
);
313 /* free the arguments */
314 for (i
= 0; i
< argc
; i
++) {
319 return !termRequested
;
324 usage(const char *command
)
326 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
327 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
328 SCPrint(TRUE
, stderr
, CFSTR("\n"));
329 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs [preference-file]\n"), command
);
330 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
331 SCPrint(TRUE
, stderr
, CFSTR("\n"));
332 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
333 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
334 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
335 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
336 SCPrint(TRUE
, stderr
, CFSTR("\n"));
337 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
338 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
339 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
340 SCPrint(TRUE
, stderr
, CFSTR("\n"));
341 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
342 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
343 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get filename path key \n"), command
);
344 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
345 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
346 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
347 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
348 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
349 SCPrint(TRUE
, stderr
, CFSTR("\n"));
350 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
351 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
352 SCPrint(TRUE
, stderr
, CFSTR("\n"));
353 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
354 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
355 SCPrint(TRUE
, stderr
, CFSTR("\n"));
356 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nwi\n"), command
);
357 SCPrint(TRUE
, stderr
, CFSTR("\tshow network information\n"));
358 SCPrint(TRUE
, stderr
, CFSTR("\n"));
359 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nc\n"), command
);
360 SCPrint(TRUE
, stderr
, CFSTR("\tshow VPN network configuration information. Use --nc help for full command list\n"));
363 SCPrint(TRUE
, stderr
, CFSTR("\n"));
364 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --log IPMonitor [off|on]\n"), command
);
365 SCPrint(TRUE
, stderr
, CFSTR("\tmanage logging.\n"));
367 SCPrint(TRUE
, stderr
, CFSTR("\n"));
368 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --disable-until-needed <interfaceName> [on|off ]\n"), command
);
369 SCPrint(TRUE
, stderr
, CFSTR("\tmanage secondary interface demand.\n"));
372 #if !TARGET_OS_IPHONE
373 SCPrint(TRUE
, stderr
, CFSTR("\n"));
374 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --allow-new-interfaces [off|on]\n"), command
);
375 SCPrint(TRUE
, stderr
, CFSTR("\tmanage new interface creation with screen locked.\n"));
376 #endif // !TARGET_OS_IPHONE
378 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
379 SCPrint(TRUE
, stderr
, CFSTR("\n"));
380 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
381 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
384 SCPrint(TRUE
, stderr
, CFSTR("\n"));
385 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --error err#\n"), command
);
386 SCPrint(TRUE
, stderr
, CFSTR("\tdisplay a descriptive message for the given error code\n"));
396 #if !TARGET_OS_SIMULATOR
398 #else // !TARGET_OS_SIMULATOR
400 #endif // !TARGET_OS_SIMULATOR
405 main(int argc
, char * const argv
[])
407 #if !TARGET_OS_IPHONE
408 Boolean allowNewInterfaces
= FALSE
;
409 #endif // !TARGET_OS_IPHONE
410 Boolean disableUntilNeeded
= FALSE
;
411 const char * advisoryInterface
= NULL
;
412 Boolean doAdvisory
= FALSE
;
413 Boolean doDNS
= FALSE
;
414 Boolean doNet
= FALSE
;
415 Boolean doNWI
= FALSE
;
416 Boolean doPrefs
= FALSE
;
417 Boolean doProxy
= FALSE
;
418 Boolean doReach
= FALSE
;
419 Boolean doSnap
= FALSE
;
426 const char *prog
= argv
[0];
431 int timeout
= 15; /* default timeout (in seconds) */
433 Boolean watch
= FALSE
;
434 int xStore
= 0; /* non dynamic store command line options */
436 /* process any arguments */
438 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1) {
442 _sc_log
= FALSE
; /* enable framework logging */
449 _sc_log
= FALSE
; /* enable framework logging */
452 enablePrivateAPI
= TRUE
;
459 timeout
= atoi(optarg
);
469 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
472 } else if (strcmp(longopts
[opti
].name
, "error") == 0) {
475 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
478 } else if (strcmp(longopts
[opti
].name
, "nc") == 0) {
481 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
484 } else if (strcmp(longopts
[opti
].name
, "nwi") == 0) {
487 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
490 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
493 } else if (strcmp(longopts
[opti
].name
, "renew") == 0) {
496 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
499 } else if (strcmp(longopts
[opti
].name
, "snapshot") == 0) {
502 } else if (strcmp(longopts
[opti
].name
, "log") == 0) {
505 #if !TARGET_OS_IPHONE
506 } else if (strcmp(longopts
[opti
].name
, "allow-new-interfaces") == 0) {
507 allowNewInterfaces
= TRUE
;
509 #endif // !TARGET_OS_IPHONE
510 } else if (strcmp(longopts
[opti
].name
, "disable-until-needed") == 0) {
511 disableUntilNeeded
= TRUE
;
513 } else if (strcmp(longopts
[opti
].name
, "user") == 0) {
514 username
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
515 } else if (strcmp(longopts
[opti
].name
, "password") == 0) {
516 password
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
517 } else if (strcmp(longopts
[opti
].name
, "secret") == 0) {
518 sharedsecret
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
519 } else if (strcmp(longopts
[opti
].name
, "advisory") == 0) {
521 advisoryInterface
= optarg
;
535 // if we are attempting to process more than one type of request
539 /* are we checking (or watching) the reachability of a host/address */
545 do_watchReachability(argc
, (char **)argv
);
547 do_checkReachability(argc
, (char **)argv
);
552 /* are we waiting on the presense of a dynamic store key */
554 do_wait(wait
, timeout
);
558 /* are we looking up the DNS configuration */
561 do_watchDNSConfiguration(argc
, (char **)argv
);
563 do_showDNSConfiguration(argc
, (char **)argv
);
570 do_watchNWI(argc
, (char**)argv
);
572 do_showNWI(argc
, (char**)argv
);
578 if (!enablePrivateAPI
) {
582 do_open(0, NULL
); /* open the dynamic store */
583 do_snapshot(argc
, (char**)argv
);
588 do_advisory(advisoryInterface
, watch
, argc
, (char**)argv
);
592 /* are we translating error #'s to descriptive text */
594 int sc_status
= atoi(error
);
596 SCPrint(TRUE
, stdout
, CFSTR("Error: 0x%08x %d %s\n"),
599 SCErrorString(sc_status
));
603 /* are we looking up a preference value */
606 if (findPref(get
) < 0) {
609 } else if (argc
== 2) {
612 * i.e. scutil --get <filename> <prefs path> <key>
614 * need to go back one argument to re-use the 1st "--get"
615 * argument as the prefs path name
623 do_getPref(get
, argc
, (char **)argv
);
627 /* are we looking up the proxy configuration */
629 do_showProxyConfiguration(argc
, (char **)argv
);
633 /* are we changing a preference value */
635 if (findPref(set
) < 0) {
638 do_setPref(set
, argc
, (char **)argv
);
644 if (strcasecmp(log
, "IPMonitor")) {
647 do_log(log
, argc
, (char * *)argv
);
651 #if !TARGET_OS_IPHONE
652 /* allowNewInterfaces */
653 if (allowNewInterfaces
) {
654 do_ifnamer("allow-new-interfaces", argc
, (char * *)argv
);
657 #endif // !TARGET_OS_IPHONE
659 /* disableUntilNeeded */
660 if (disableUntilNeeded
) {
661 do_disable_until_needed(argc
, (char * *)argv
);
665 /* network connection commands */
667 if (find_nc_cmd(nc_cmd
) < 0) {
670 do_nc_cmd(nc_cmd
, argc
, (char **)argv
, watch
);
675 /* if we are going to be managing the network configuration */
676 commands
= (cmdInfo
*)commands_net
;
677 nCommands
= nCommands_net
;
679 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
683 do_net_init(); /* initialization */
684 do_net_open(argc
, (char **)argv
); /* open prefs */
685 } else if (doPrefs
) {
686 /* if we are going to be managing the network configuration */
687 commands
= (cmdInfo
*)commands_prefs
;
688 nCommands
= nCommands_prefs
;
690 do_dictInit(0, NULL
); /* start with an empty dictionary */
691 do_prefs_init(); /* initialization */
692 do_prefs_open(argc
, (char **)argv
); /* open prefs */
694 /* if we are going to be managing the dynamic store */
695 commands
= (cmdInfo
*)commands_store
;
696 nCommands
= nCommands_store
;
698 do_dictInit(0, NULL
); /* start with an empty dictionary */
699 do_open(0, NULL
); /* open the dynamic store */
702 /* are we trying to renew a DHCP lease */
708 /* allocate command input stream */
709 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
714 if (isatty(fileno(src
->fp
))) {
719 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
720 if ((t
.c_lflag
& ECHO
) == 0) {
724 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
725 src
->h
= history_init();
727 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
728 el_set(src
->el
, EL_HIST
, history
, src
->h
);
731 el_set(src
->el
, EL_EDITMODE
, 0);
734 el_set(src
->el
, EL_EDITOR
, "emacs");
735 el_set(src
->el
, EL_PROMPT
, prompt
);
737 el_source(src
->el
, NULL
);
739 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
740 el_set(src
->el
, EL_SIGNAL
, 1);
752 ok
= process_line(src
);
758 /* close the socket, free resources */
759 if (src
->h
) history_end(src
->h
);
760 if (src
->el
) el_end(src
->el
);
761 (void)fclose(src
->fp
);
762 CFAllocatorDeallocate(NULL
, src
);
764 exit (EX_OK
); // insure the process exit status is 0
765 return 0; // ...and make main fit the ANSI spec.