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 },
119 _copyStringFromSTDIN(CFStringRef prompt
, CFStringRef defaultValue
)
123 Boolean is_user_prompt
= (prompt
!= NULL
&& isatty(STDIN_FILENO
) && isatty(STDOUT_FILENO
));
129 /* Print out a prompt to user that entry is desired */
130 if (is_user_prompt
) {
131 if (defaultValue
!= NULL
) {
132 SCPrint(TRUE
, stdout
, CFSTR("%@ [%@]: "), prompt
, defaultValue
);
134 SCPrint(TRUE
, stdout
, CFSTR("%@: "), prompt
);
139 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
143 /* Prepare for trim */
144 len
= (int)strlen(buf
);
149 if ((modlen
> 0) && (modbuf
[modlen
- 1] == '\n')) {
150 modbuf
[modlen
- 1] = '\0';
154 /* If nothing was entered at the user prompt, set default */
155 if (is_user_prompt
&& defaultValue
!= NULL
&& modlen
== 0) {
156 CFRetain(defaultValue
);
160 /* Trim spaces from front */
161 while (modlen
> 0 && isspace(modbuf
[0])) {
166 /* Trim spaces from back */
167 for (i
= modlen
- 1; i
>= 0; i
--) {
168 if (isspace(buf
[i
])) {
176 utf8
= CFStringCreateWithBytes(NULL
, (UInt8
*)modbuf
, modlen
, kCFStringEncodingUTF8
, TRUE
);
181 getLine(char *buf
, int len
, InputRef src
)
189 line
= el_gets(src
->el
, &count
);
193 strncpy(buf
, line
, len
);
195 if (fgets(buf
, len
, src
->fp
) == NULL
)
199 n
= (int)strlen(buf
);
200 if (buf
[n
-1] == '\n') {
201 /* the entire line fit in the buffer, remove the newline */
203 } else if (!src
->el
) {
204 /* eat the remainder of the line */
207 } while ((n
!= '\n') && (n
!= EOF
));
210 if (src
->h
&& (buf
[0] != '\0')) {
213 history(src
->h
, &ev
, H_ENTER
, buf
);
222 getString(char **line
)
224 char *s
, *e
, c
, *string
;
225 int i
, isQuoted
= 0, escaped
= 0;
227 if (*line
== NULL
) return NULL
;
228 if (**line
== '\0') return NULL
;
230 /* Skip leading white space */
231 while (isspace(**line
)) *line
+= 1;
233 /* Grab the next string */
236 return NULL
; /* no string available */
237 } else if (*s
== '"') {
238 isQuoted
= 1; /* it's a quoted string */
242 for (e
= s
; (c
= *e
) != '\0'; e
++) {
243 if (isQuoted
&& (c
== '"'))
244 break; /* end of quoted string */
248 break; /* if premature end-of-string */
249 if ((*e
== '"') || isspace(*e
))
250 escaped
++; /* if escaped quote or white space */
252 if (!isQuoted
&& isspace(c
))
253 break; /* end of non-quoted string */
256 string
= malloc(e
- s
- escaped
+ 1);
258 for (i
= 0; s
< e
; s
++) {
260 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
265 e
++; /* move past end of quoted string */
274 process_line(InputRef src
)
280 char line
[LINE_LENGTH
];
283 // if end-of-file, exit
284 if (getLine(line
, sizeof(line
), src
) == NULL
)
288 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
291 // break up the input line
292 while ((arg
= getString(&s
)) != NULL
) {
294 argv
= (char **)malloc(2 * sizeof(char *));
296 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
301 return TRUE
; // if no arguments
304 /* process the command */
305 if (*argv
[0] != '#') {
306 argv
[argc
] = NULL
; // just in case...
308 do_command(argc
, argv
);
311 /* free the arguments */
312 for (i
= 0; i
< argc
; i
++) {
317 return !termRequested
;
322 usage(const char *command
)
324 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
325 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
326 SCPrint(TRUE
, stderr
, CFSTR("\n"));
327 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs [preference-file]\n"), command
);
328 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
329 SCPrint(TRUE
, stderr
, CFSTR("\n"));
330 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
331 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
332 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
333 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
334 SCPrint(TRUE
, stderr
, CFSTR("\n"));
335 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
336 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
337 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
338 SCPrint(TRUE
, stderr
, CFSTR("\n"));
339 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
340 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
341 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get filename path key \n"), command
);
342 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
343 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
344 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
345 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
346 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
347 SCPrint(TRUE
, stderr
, CFSTR("\n"));
348 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
349 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
350 SCPrint(TRUE
, stderr
, CFSTR("\n"));
351 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
352 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
353 SCPrint(TRUE
, stderr
, CFSTR("\n"));
354 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nwi\n"), command
);
355 SCPrint(TRUE
, stderr
, CFSTR("\tshow network information\n"));
356 SCPrint(TRUE
, stderr
, CFSTR("\n"));
357 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nc\n"), command
);
358 SCPrint(TRUE
, stderr
, CFSTR("\tshow VPN network configuration information. Use --nc help for full command list\n"));
361 SCPrint(TRUE
, stderr
, CFSTR("\n"));
362 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --log IPMonitor [off|on]\n"), command
);
363 SCPrint(TRUE
, stderr
, CFSTR("\tmanage logging.\n"));
366 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
367 SCPrint(TRUE
, stderr
, CFSTR("\n"));
368 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
369 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
372 SCPrint(TRUE
, stderr
, CFSTR("\n"));
373 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --error err#\n"), command
);
374 SCPrint(TRUE
, stderr
, CFSTR("\tdisplay a descriptive message for the given error code\n"));
383 #if !TARGET_IPHONE_SIMULATOR
385 #else // !TARGET_IPHONE_SIMULATOR
387 #endif // !TARGET_IPHONE_SIMULATOR
392 main(int argc
, char * const argv
[])
394 Boolean doDNS
= FALSE
;
395 Boolean doNet
= FALSE
;
396 Boolean doNWI
= FALSE
;
397 Boolean doPrefs
= FALSE
;
398 Boolean doProxy
= FALSE
;
399 Boolean doReach
= FALSE
;
400 Boolean doSnap
= FALSE
;
407 const char *prog
= argv
[0];
412 int timeout
= 15; /* default timeout (in seconds) */
414 Boolean watch
= FALSE
;
415 int xStore
= 0; /* non dynamic store command line options */
417 /* process any arguments */
419 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1)
423 _sc_log
= FALSE
; /* enable framework logging */
430 _sc_log
= FALSE
; /* enable framework logging */
433 enablePrivateAPI
= TRUE
;
440 timeout
= atoi(optarg
);
450 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
453 } else if (strcmp(longopts
[opti
].name
, "error") == 0) {
456 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
459 } else if (strcmp(longopts
[opti
].name
, "nc") == 0) {
462 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
465 } else if (strcmp(longopts
[opti
].name
, "nwi") == 0) {
468 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
471 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
474 } else if (strcmp(longopts
[opti
].name
, "renew") == 0) {
477 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
480 } else if (strcmp(longopts
[opti
].name
, "snapshot") == 0) {
483 } else if (strcmp(longopts
[opti
].name
, "log") == 0) {
486 } else if (strcmp(longopts
[opti
].name
, "user") == 0) {
487 username
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
488 } else if (strcmp(longopts
[opti
].name
, "password") == 0) {
489 password
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
490 } else if (strcmp(longopts
[opti
].name
, "secret") == 0) {
491 sharedsecret
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
502 // if we are attempting to process more than one type of request
506 /* are we checking (or watching) the reachability of a host/address */
512 do_watchReachability(argc
, (char **)argv
);
514 do_checkReachability(argc
, (char **)argv
);
519 /* are we waiting on the presense of a dynamic store key */
521 do_wait(wait
, timeout
);
525 /* are we looking up the DNS configuration */
528 do_watchDNSConfiguration(argc
, (char **)argv
);
530 do_showDNSConfiguration(argc
, (char **)argv
);
537 do_watchNWI(argc
, (char**)argv
);
539 do_showNWI(argc
, (char**)argv
);
545 if (!enablePrivateAPI
546 #if !TARGET_IPHONE_SIMULATOR
548 #endif // !TARGET_IPHONE_SIMULATOR
553 do_open(0, NULL
); /* open the dynamic store */
554 do_snapshot(argc
, (char**)argv
);
558 /* are we translating error #'s to descriptive text */
560 int sc_status
= atoi(error
);
562 SCPrint(TRUE
, stdout
, CFSTR("Error: 0x%08x %d %s\n"),
565 SCErrorString(sc_status
));
569 /* are we looking up a preference value */
572 if (findPref(get
) < 0) {
576 /* need to go back one argument
577 * for the filename */
582 do_getPref(get
, argc
, (char **)argv
);
586 /* are we looking up the proxy configuration */
588 do_showProxyConfiguration(argc
, (char **)argv
);
592 /* are we changing a preference value */
594 if (findPref(set
) < 0) {
597 do_setPref(set
, argc
, (char **)argv
);
603 if (strcasecmp(log
, "IPMonitor")) {
606 do_log(log
, argc
, (char * *)argv
);
610 /* network connection commands */
612 if (find_nc_cmd(nc_cmd
) < 0) {
615 do_nc_cmd(nc_cmd
, argc
, (char **)argv
, watch
);
620 /* if we are going to be managing the network configuration */
621 commands
= (cmdInfo
*)commands_net
;
622 nCommands
= nCommands_net
;
624 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
628 do_net_init(); /* initialization */
629 do_net_open(argc
, (char **)argv
); /* open prefs */
630 } else if (doPrefs
) {
631 /* if we are going to be managing the network configuration */
632 commands
= (cmdInfo
*)commands_prefs
;
633 nCommands
= nCommands_prefs
;
635 do_dictInit(0, NULL
); /* start with an empty dictionary */
636 do_prefs_init(); /* initialization */
637 do_prefs_open(argc
, (char **)argv
); /* open prefs */
639 /* if we are going to be managing the dynamic store */
640 commands
= (cmdInfo
*)commands_store
;
641 nCommands
= nCommands_store
;
643 do_dictInit(0, NULL
); /* start with an empty dictionary */
644 do_open(0, NULL
); /* open the dynamic store */
647 /* are we trying to renew a DHCP lease */
653 /* allocate command input stream */
654 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
659 if (isatty(fileno(src
->fp
))) {
664 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
665 if ((t
.c_lflag
& ECHO
) == 0) {
669 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
670 src
->h
= history_init();
672 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
673 el_set(src
->el
, EL_HIST
, history
, src
->h
);
676 el_set(src
->el
, EL_EDITMODE
, 0);
679 el_set(src
->el
, EL_EDITOR
, "emacs");
680 el_set(src
->el
, EL_PROMPT
, prompt
);
682 el_source(src
->el
, NULL
);
684 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
685 el_set(src
->el
, EL_SIGNAL
, 1);
697 ok
= process_line(src
);
703 /* close the socket, free resources */
704 if (src
->h
) history_end(src
->h
);
705 if (src
->el
) el_end(src
->el
);
706 (void)fclose(src
->fp
);
707 CFAllocatorDeallocate(NULL
, src
);
709 exit (EX_OK
); // insure the process exit status is 0
710 return 0; // ...and make main fit the ANSI spec.