2 * Copyright (c) 2000-2012 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 { "nc", required_argument
, NULL
, 0 },
101 { "net", no_argument
, NULL
, 0 },
102 { "nwi", no_argument
, NULL
, 0 },
103 { "prefs", no_argument
, NULL
, 0 },
104 { "proxy", no_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 },
116 _copyStringFromSTDIN()
122 if (fgets(buf
, sizeof(buf
), stdin
) == NULL
) {
127 if (buf
[len
-1] == '\n') {
131 utf8
= CFStringCreateWithBytes(NULL
, (UInt8
*)buf
, len
, kCFStringEncodingUTF8
, TRUE
);
136 getLine(char *buf
, int len
, InputRef src
)
144 line
= el_gets(src
->el
, &count
);
148 strncpy(buf
, line
, len
);
150 if (fgets(buf
, len
, src
->fp
) == NULL
)
155 if (buf
[n
-1] == '\n') {
156 /* the entire line fit in the buffer, remove the newline */
158 } else if (!src
->el
) {
159 /* eat the remainder of the line */
162 } while ((n
!= '\n') && (n
!= EOF
));
165 if (src
->h
&& (buf
[0] != '\0')) {
168 history(src
->h
, &ev
, H_ENTER
, buf
);
177 getString(char **line
)
179 char *s
, *e
, c
, *string
;
180 int i
, isQuoted
= 0, escaped
= 0;
182 if (*line
== NULL
) return NULL
;
183 if (**line
== '\0') return NULL
;
185 /* Skip leading white space */
186 while (isspace(**line
)) *line
+= 1;
188 /* Grab the next string */
191 return NULL
; /* no string available */
192 } else if (*s
== '"') {
193 isQuoted
= 1; /* it's a quoted string */
197 for (e
= s
; (c
= *e
) != '\0'; e
++) {
198 if (isQuoted
&& (c
== '"'))
199 break; /* end of quoted string */
203 break; /* if premature end-of-string */
204 if ((*e
== '"') || isspace(*e
))
205 escaped
++; /* if escaped quote or white space */
207 if (!isQuoted
&& isspace(c
))
208 break; /* end of non-quoted string */
211 string
= malloc(e
- s
- escaped
+ 1);
213 for (i
= 0; s
< e
; s
++) {
215 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
220 e
++; /* move past end of quoted string */
229 process_line(InputRef src
)
235 char line
[LINE_LENGTH
];
238 // if end-of-file, exit
239 if (getLine(line
, sizeof(line
), src
) == NULL
)
243 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
246 // break up the input line
247 while ((arg
= getString(&s
)) != NULL
) {
249 argv
= (char **)malloc(2 * sizeof(char *));
251 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
256 return TRUE
; // if no arguments
259 /* process the command */
260 if (*argv
[0] != '#') {
261 argv
[argc
] = NULL
; // just in case...
263 do_command(argc
, argv
);
266 /* free the arguments */
267 for (i
= 0; i
< argc
; i
++) {
272 return !termRequested
;
277 usage(const char *command
)
279 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
280 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
281 SCPrint(TRUE
, stderr
, CFSTR("\n"));
282 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs [preference-file]\n"), command
);
283 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
284 SCPrint(TRUE
, stderr
, CFSTR("\n"));
285 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
286 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
287 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
288 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
289 SCPrint(TRUE
, stderr
, CFSTR("\n"));
290 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
291 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
292 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
293 SCPrint(TRUE
, stderr
, CFSTR("\n"));
294 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
295 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
296 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
297 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
298 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
299 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
300 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
301 SCPrint(TRUE
, stderr
, CFSTR("\n"));
302 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
303 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
304 SCPrint(TRUE
, stderr
, CFSTR("\n"));
305 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
306 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
307 SCPrint(TRUE
, stderr
, CFSTR("\n"));
308 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --nwi\n"), command
);
309 SCPrint(TRUE
, stderr
, CFSTR("\tshow network information\n"));
311 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
312 SCPrint(TRUE
, stderr
, CFSTR("\n"));
313 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
314 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
329 main(int argc
, char * const argv
[])
331 Boolean doDNS
= FALSE
;
332 Boolean doNet
= FALSE
;
333 Boolean doNWI
= FALSE
;
334 Boolean doPrefs
= FALSE
;
335 Boolean doProxy
= FALSE
;
336 Boolean doReach
= FALSE
;
337 Boolean doSnap
= FALSE
;
342 const char *prog
= argv
[0];
346 int timeout
= 15; /* default timeout (in seconds) */
348 Boolean watch
= FALSE
;
349 int xStore
= 0; /* non dynamic store command line options */
351 /* process any arguments */
353 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1)
357 _sc_log
= FALSE
; /* enable framework logging */
364 _sc_log
= FALSE
; /* enable framework logging */
367 enablePrivateAPI
= TRUE
;
374 timeout
= atoi(optarg
);
384 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
387 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
390 } else if (strcmp(longopts
[opti
].name
, "nc") == 0) {
393 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
396 } else if (strcmp(longopts
[opti
].name
, "nwi") == 0) {
399 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
402 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
405 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
408 } else if (strcmp(longopts
[opti
].name
, "snapshot") == 0) {
411 } else if (strcmp(longopts
[opti
].name
, "user") == 0) {
412 username
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
413 } else if (strcmp(longopts
[opti
].name
, "password") == 0) {
414 password
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
415 } else if (strcmp(longopts
[opti
].name
, "secret") == 0) {
416 sharedsecret
= CFStringCreateWithCString(NULL
, optarg
, kCFStringEncodingUTF8
);
427 // if we are attempting to process more than one type of request
430 /* are we checking (or watching) the reachability of a host/address */
436 do_watchReachability(argc
, (char **)argv
);
438 do_checkReachability(argc
, (char **)argv
);
443 /* are we waiting on the presense of a dynamic store key */
445 do_wait(wait
, timeout
);
449 /* are we looking up the DNS configuration */
451 do_showDNSConfiguration(argc
, (char **)argv
);
456 do_nwi(argc
, (char**)argv
);
461 if (!enablePrivateAPI
|| (geteuid() != 0)) {
465 do_open(0, NULL
); /* open the dynamic store */
466 do_snapshot(argc
, (char**)argv
);
470 /* are we looking up a preference value */
472 if (findPref(get
) < 0) {
475 do_getPref(get
, argc
, (char **)argv
);
479 /* are we looking up the proxy configuration */
481 do_showProxyConfiguration(argc
, (char **)argv
);
485 /* are we changing a preference value */
487 if (findPref(set
) < 0) {
490 do_setPref(set
, argc
, (char **)argv
);
494 /* network connection commands */
496 if (find_nc_cmd(nc_cmd
) < 0) {
499 do_nc_cmd(nc_cmd
, argc
, (char **)argv
, watch
);
504 /* if we are going to be managing the network configuration */
505 commands
= (cmdInfo
*)commands_net
;
506 nCommands
= nCommands_net
;
508 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
512 do_net_init(); /* initialization */
513 do_net_open(0, NULL
); /* open default prefs */
514 } else if (doPrefs
) {
515 /* if we are going to be managing the network configuration */
516 commands
= (cmdInfo
*)commands_prefs
;
517 nCommands
= nCommands_prefs
;
519 do_dictInit(0, NULL
); /* start with an empty dictionary */
520 do_prefs_init(); /* initialization */
521 do_prefs_open(argc
, (char **)argv
); /* open prefs */
523 /* if we are going to be managing the dynamic store */
524 commands
= (cmdInfo
*)commands_store
;
525 nCommands
= nCommands_store
;
527 do_dictInit(0, NULL
); /* start with an empty dictionary */
528 do_open(0, NULL
); /* open the dynamic store */
531 /* allocate command input stream */
532 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
537 if (isatty(fileno(src
->fp
))) {
542 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
543 if ((t
.c_lflag
& ECHO
) == 0) {
547 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
548 src
->h
= history_init();
550 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
551 el_set(src
->el
, EL_HIST
, history
, src
->h
);
554 el_set(src
->el
, EL_EDITMODE
, 0);
557 el_set(src
->el
, EL_EDITOR
, "emacs");
558 el_set(src
->el
, EL_PROMPT
, prompt
);
560 el_source(src
->el
, NULL
);
562 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
563 el_set(src
->el
, EL_SIGNAL
, 1);
575 ok
= process_line(src
);
581 /* close the socket, free resources */
582 if (src
->h
) history_end(src
->h
);
583 if (src
->el
) el_end(src
->el
);
584 (void)fclose(src
->fp
);
585 CFAllocatorDeallocate(NULL
, src
);
587 exit (EX_OK
); // insure the process exit status is 0
588 return 0; // ...and make main fit the ANSI spec.