2 * Copyright (c) 2000-2011 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 256
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 { "help", no_argument
, NULL
, '?' },
100 { "net", no_argument
, NULL
, 0 },
101 { "prefs", no_argument
, NULL
, 0 },
102 { "proxy", no_argument
, NULL
, 0 },
103 { "set", required_argument
, NULL
, 0 },
104 { "nc", required_argument
, NULL
, 0 },
111 _copyStringFromSTDIN()
117 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
122 if (buf
[len
-1] == '\n') {
126 utf8
= CFStringCreateWithBytes(NULL
, (UInt8
*)buf
, len
, kCFStringEncodingUTF8
, TRUE
);
131 getLine(char *buf
, int len
, InputRef src
)
139 line
= el_gets(src
->el
, &count
);
143 strncpy(buf
, line
, len
);
145 if (fgets(buf
, len
, src
->fp
) == NULL
)
150 if (buf
[n
-1] == '\n') {
151 /* the entire line fit in the buffer, remove the newline */
153 } else if (!src
->el
) {
154 /* eat the remainder of the line */
157 } while ((n
!= '\n') && (n
!= EOF
));
160 if (src
->h
&& (buf
[0] != '\0')) {
163 history(src
->h
, &ev
, H_ENTER
, buf
);
172 getString(char **line
)
174 char *s
, *e
, c
, *string
;
175 int i
, isQuoted
= 0, escaped
= 0;
177 if (*line
== NULL
) return NULL
;
178 if (**line
== '\0') return NULL
;
180 /* Skip leading white space */
181 while (isspace(**line
)) *line
+= 1;
183 /* Grab the next string */
186 return NULL
; /* no string available */
187 } else if (*s
== '"') {
188 isQuoted
= 1; /* it's a quoted string */
192 for (e
= s
; (c
= *e
) != '\0'; e
++) {
193 if (isQuoted
&& (c
== '"'))
194 break; /* end of quoted string */
198 break; /* if premature end-of-string */
199 if ((*e
== '"') || isspace(*e
))
200 escaped
++; /* if escaped quote or white space */
202 if (!isQuoted
&& isspace(c
))
203 break; /* end of non-quoted string */
206 string
= malloc(e
- s
- escaped
+ 1);
208 for (i
= 0; s
< e
; s
++) {
210 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
215 e
++; /* move past end of quoted string */
224 process_line(InputRef src
)
230 char line
[LINE_LENGTH
];
233 // if end-of-file, exit
234 if (getLine(line
, sizeof(line
), src
) == NULL
)
238 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
241 // break up the input line
242 while ((arg
= getString(&s
)) != NULL
) {
244 argv
= (char **)malloc(2 * sizeof(char *));
246 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
251 return TRUE
; // if no arguments
254 /* process the command */
255 if (*argv
[0] != '#') {
256 argv
[argc
] = NULL
; // just in case...
258 do_command(argc
, argv
);
261 /* free the arguments */
262 for (i
= 0; i
< argc
; i
++) {
267 return !termRequested
;
272 usage(const char *command
)
274 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
275 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
276 SCPrint(TRUE
, stderr
, CFSTR("\n"));
277 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs [preference-file]\n"), command
);
278 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
279 SCPrint(TRUE
, stderr
, CFSTR("\n"));
280 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
281 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
282 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
283 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
284 SCPrint(TRUE
, stderr
, CFSTR("\n"));
285 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
286 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
287 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
288 SCPrint(TRUE
, stderr
, CFSTR("\n"));
289 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
290 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
291 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
292 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
293 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
294 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
295 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
296 SCPrint(TRUE
, stderr
, CFSTR("\n"));
297 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
298 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
299 SCPrint(TRUE
, stderr
, CFSTR("\n"));
300 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
301 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
303 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
304 SCPrint(TRUE
, stderr
, CFSTR("\n"));
305 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
306 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
321 main(int argc
, char * const argv
[])
323 Boolean doDNS
= FALSE
;
324 Boolean doNet
= FALSE
;
325 Boolean doPrefs
= FALSE
;
326 Boolean doProxy
= FALSE
;
327 Boolean doReach
= FALSE
;
332 const char *prog
= argv
[0];
336 int timeout
= 15; /* default timeout (in seconds) */
338 Boolean watch
= FALSE
;
339 int xStore
= 0; /* non dynamic store command line options */
341 /* process any arguments */
343 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1)
347 _sc_log
= FALSE
; /* enable framework logging */
354 _sc_log
= FALSE
; /* enable framework logging */
357 enablePrivateAPI
= TRUE
;
364 timeout
= atoi(optarg
);
374 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
377 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
380 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
383 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
386 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
389 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
392 } else if (strcmp(longopts
[opti
].name
, "nc") == 0) {
405 // if we are attempting to process more than one type of request
408 /* are we checking (or watching) the reachability of a host/address */
414 do_watchReachability(argc
, (char **)argv
);
416 do_checkReachability(argc
, (char **)argv
);
421 /* are we waiting on the presense of a dynamic store key */
423 do_wait(wait
, timeout
);
427 /* are we looking up the DNS configuration */
429 do_showDNSConfiguration(argc
, (char **)argv
);
433 /* are we looking up a preference value */
435 if (findPref(get
) < 0) {
438 do_getPref(get
, argc
, (char **)argv
);
442 /* are we looking up the proxy configuration */
444 do_showProxyConfiguration(argc
, (char **)argv
);
448 /* are we changing a preference value */
450 if (findPref(set
) < 0) {
453 do_setPref(set
, argc
, (char **)argv
);
457 /* network connection commands */
459 if (find_nc_cmd(nc_cmd
) < 0) {
462 do_nc_cmd(nc_cmd
, argc
, (char **)argv
, watch
);
467 /* if we are going to be managing the network configuration */
468 commands
= (cmdInfo
*)commands_net
;
469 nCommands
= nCommands_net
;
471 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
475 do_net_init(); /* initialization */
476 do_net_open(0, NULL
); /* open default prefs */
477 } else if (doPrefs
) {
478 /* if we are going to be managing the network configuration */
479 commands
= (cmdInfo
*)commands_prefs
;
480 nCommands
= nCommands_prefs
;
482 do_dictInit(0, NULL
); /* start with an empty dictionary */
483 do_prefs_init(); /* initialization */
484 do_prefs_open(argc
, (char **)argv
); /* open prefs */
486 /* if we are going to be managing the dynamic store */
487 commands
= (cmdInfo
*)commands_store
;
488 nCommands
= nCommands_store
;
490 do_dictInit(0, NULL
); /* start with an empty dictionary */
491 do_open(0, NULL
); /* open the dynamic store */
494 /* allocate command input stream */
495 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
500 if (isatty(fileno(src
->fp
))) {
505 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
506 if ((t
.c_lflag
& ECHO
) == 0) {
510 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
511 src
->h
= history_init();
513 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
514 el_set(src
->el
, EL_HIST
, history
, src
->h
);
517 el_set(src
->el
, EL_EDITMODE
, 0);
520 el_set(src
->el
, EL_EDITOR
, "emacs");
521 el_set(src
->el
, EL_PROMPT
, prompt
);
523 el_source(src
->el
, NULL
);
525 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
526 el_set(src
->el
, EL_SIGNAL
, 1);
538 ok
= process_line(src
);
544 /* close the socket, free resources */
545 if (src
->h
) history_end(src
->h
);
546 if (src
->el
) el_end(src
->el
);
547 (void)fclose(src
->fp
);
548 CFAllocatorDeallocate(NULL
, src
);
550 exit (EX_OK
); // insure the process exit status is 0
551 return 0; // ...and make main fit the ANSI spec.