2 * Copyright (c) 2000-2009 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>
58 #include <mach/mach.h>
59 #include <mach/mach_error.h>
64 #include "dictionary.h"
71 #define LINE_LENGTH 256
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 { "help", no_argument
, NULL
, '?' },
98 { "net", no_argument
, NULL
, 0 },
99 { "prefs", no_argument
, NULL
, 0 },
100 { "proxy", no_argument
, NULL
, 0 },
101 { "set", required_argument
, NULL
, 0 },
107 getLine(char *buf
, int len
, InputRef src
)
115 line
= el_gets(src
->el
, &count
);
119 strncpy(buf
, line
, len
);
121 if (fgets(buf
, len
, src
->fp
) == NULL
)
126 if (buf
[n
-1] == '\n') {
127 /* the entire line fit in the buffer, remove the newline */
129 } else if (!src
->el
) {
130 /* eat the remainder of the line */
133 } while ((n
!= '\n') && (n
!= EOF
));
139 history(src
->h
, &ev
, H_ENTER
, buf
);
148 getString(char **line
)
150 char *s
, *e
, c
, *string
;
151 int i
, isQuoted
= 0, escaped
= 0;
153 if (*line
== NULL
) return NULL
;
154 if (**line
== '\0') return NULL
;
156 /* Skip leading white space */
157 while (isspace(**line
)) *line
+= 1;
159 /* Grab the next string */
162 return NULL
; /* no string available */
163 } else if (*s
== '"') {
164 isQuoted
= 1; /* it's a quoted string */
168 for (e
= s
; (c
= *e
) != '\0'; e
++) {
169 if (isQuoted
&& (c
== '"'))
170 break; /* end of quoted string */
174 break; /* if premature end-of-string */
175 if ((*e
== '"') || isspace(*e
))
176 escaped
++; /* if escaped quote or white space */
178 if (!isQuoted
&& isspace(c
))
179 break; /* end of non-quoted string */
182 string
= malloc(e
- s
- escaped
+ 1);
184 for (i
= 0; s
< e
; s
++) {
186 if (!((s
[0] == '\\') && ((s
[1] == '"') || isspace(s
[1])))) i
++;
191 e
++; /* move past end of quoted string */
200 process_line(InputRef src
)
206 char line
[LINE_LENGTH
];
209 // if end-of-file, exit
210 if (getLine(line
, sizeof(line
), src
) == NULL
)
214 SCPrint(TRUE
, stdout
, CFSTR("%d> %s\n"), nesting
, line
);
217 // break up the input line
218 while ((arg
= getString(&s
)) != NULL
) {
220 argv
= (char **)malloc(2 * sizeof(char *));
222 argv
= (char **)reallocf(argv
, ((argc
+ 2) * sizeof(char *)));
227 return TRUE
; // if no arguments
230 /* process the command */
231 if (*argv
[0] != '#') {
232 argv
[argc
] = NULL
; // just in case...
234 do_command(argc
, argv
);
237 /* free the arguments */
238 for (i
= 0; i
< argc
; i
++) {
243 return !termRequested
;
248 usage(const char *command
)
250 SCPrint(TRUE
, stderr
, CFSTR("usage: %s\n"), command
);
251 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the dynamic store.\n"));
252 SCPrint(TRUE
, stderr
, CFSTR("\n"));
253 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --prefs\n"), command
);
254 SCPrint(TRUE
, stderr
, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
255 SCPrint(TRUE
, stderr
, CFSTR("\n"));
256 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r nodename\n"), command
);
257 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r address\n"), command
);
258 SCPrint(TRUE
, stderr
, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command
);
259 SCPrint(TRUE
, stderr
, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
260 SCPrint(TRUE
, stderr
, CFSTR("\n"));
261 SCPrint(TRUE
, stderr
, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command
);
262 SCPrint(TRUE
, stderr
, CFSTR("\t-w\twait for presense of dynamic store key\n"));
263 SCPrint(TRUE
, stderr
, CFSTR("\t-t\ttime to wait for key\n"));
264 SCPrint(TRUE
, stderr
, CFSTR("\n"));
265 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --get pref\n"), command
);
266 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --set pref [newval]\n"), command
);
267 SCPrint(TRUE
, stderr
, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
268 SCPrint(TRUE
, stderr
, CFSTR("\t\tinclude:\n"));
269 SCPrint(TRUE
, stderr
, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
270 SCPrint(TRUE
, stderr
, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
271 SCPrint(TRUE
, stderr
, CFSTR("\t\tthe new value will be read from standard input.\n"));
272 SCPrint(TRUE
, stderr
, CFSTR("\n"));
273 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --dns\n"), command
);
274 SCPrint(TRUE
, stderr
, CFSTR("\tshow DNS configuration.\n"));
275 SCPrint(TRUE
, stderr
, CFSTR("\n"));
276 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --proxy\n"), command
);
277 SCPrint(TRUE
, stderr
, CFSTR("\tshow \"proxy\" configuration.\n"));
279 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
280 SCPrint(TRUE
, stderr
, CFSTR("\n"));
281 SCPrint(TRUE
, stderr
, CFSTR(" or: %s --net\n"), command
);
282 SCPrint(TRUE
, stderr
, CFSTR("\tmanage network configuration.\n"));
297 main(int argc
, char * const argv
[])
299 Boolean doDNS
= FALSE
;
300 Boolean doNet
= FALSE
;
301 Boolean doPrefs
= FALSE
;
302 Boolean doProxy
= FALSE
;
303 Boolean doReach
= FALSE
;
308 const char *prog
= argv
[0];
311 int timeout
= 15; /* default timeout (in seconds) */
313 Boolean watch
= FALSE
;
314 int xStore
= 0; /* non dynamic store command line options */
316 /* process any arguments */
318 while ((opt
= getopt_long(argc
, argv
, "dDvprt:w:W", longopts
, &opti
)) != -1)
322 _sc_log
= FALSE
; /* enable framework logging */
329 _sc_log
= FALSE
; /* enable framework logging */
332 enablePrivateAPI
= TRUE
;
339 timeout
= atoi(optarg
);
349 if (strcmp(longopts
[opti
].name
, "dns") == 0) {
352 } else if (strcmp(longopts
[opti
].name
, "get") == 0) {
355 } else if (strcmp(longopts
[opti
].name
, "net") == 0) {
358 } else if (strcmp(longopts
[opti
].name
, "prefs") == 0) {
361 } else if (strcmp(longopts
[opti
].name
, "proxy") == 0) {
364 } else if (strcmp(longopts
[opti
].name
, "set") == 0) {
377 // if we are attempting to process more than one type of request
381 /* are we checking (or watching) the reachability of a host/address */
387 do_watchReachability(argc
, (char **)argv
);
389 do_checkReachability(argc
, (char **)argv
);
394 /* are we waiting on the presense of a dynamic store key */
396 do_wait(wait
, timeout
);
400 /* are we looking up the DNS configuration */
402 do_showDNSConfiguration(argc
, (char **)argv
);
406 /* are we looking up a preference value */
408 if (findPref(get
) < 0) {
411 do_getPref(get
, argc
, (char **)argv
);
415 /* are we looking up the proxy configuration */
417 do_showProxyConfiguration(argc
, (char **)argv
);
421 /* are we changing a preference value */
423 if (findPref(set
) < 0) {
426 do_setPref(set
, argc
, (char **)argv
);
431 /* if we are going to be managing the network configuration */
432 commands
= (cmdInfo
*)commands_net
;
433 nCommands
= nCommands_net
;
435 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
439 do_net_init(); /* initialization */
440 do_net_open(0, NULL
); /* open default prefs */
441 } else if (doPrefs
) {
442 /* if we are going to be managing the network configuration */
443 commands
= (cmdInfo
*)commands_prefs
;
444 nCommands
= nCommands_prefs
;
446 do_dictInit(0, NULL
); /* start with an empty dictionary */
447 do_prefs_init(); /* initialization */
448 do_prefs_open(0, NULL
); /* open default prefs */
450 /* if we are going to be managing the dynamic store */
451 commands
= (cmdInfo
*)commands_store
;
452 nCommands
= nCommands_store
;
454 do_dictInit(0, NULL
); /* start with an empty dictionary */
455 do_open(0, NULL
); /* open the dynamic store */
458 /* allocate command input stream */
459 src
= (InputRef
)CFAllocatorAllocate(NULL
, sizeof(Input
), 0);
464 if (isatty(fileno(src
->fp
))) {
469 if (tcgetattr(fileno(src
->fp
), &t
) != -1) {
470 if ((t
.c_lflag
& ECHO
) == 0) {
474 src
->el
= el_init(prog
, src
->fp
, stdout
, stderr
);
475 src
->h
= history_init();
477 (void)history(src
->h
, &ev
, H_SETSIZE
, INT_MAX
);
478 el_set(src
->el
, EL_HIST
, history
, src
->h
);
481 el_set(src
->el
, EL_EDITMODE
, 0);
484 el_set(src
->el
, EL_EDITOR
, "emacs");
485 el_set(src
->el
, EL_PROMPT
, prompt
);
487 el_source(src
->el
, NULL
);
489 if ((el_get(src
->el
, EL_EDITMODE
, &editmode
) != -1) && editmode
!= 0) {
490 el_set(src
->el
, EL_SIGNAL
, 1);
502 ok
= process_line(src
);
508 /* close the socket, free resources */
509 if (src
->h
) history_end(src
->h
);
510 if (src
->el
) el_end(src
->el
);
511 (void)fclose(src
->fp
);
512 CFAllocatorDeallocate(NULL
, src
);
514 exit (EX_OK
); // insure the process exit status is 0
515 return 0; // ...and make main fit the ANSI spec.