]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/scutil.c
c506724c7453e6e546ce23688ba035346392bcf1
[apple/configd.git] / scutil.tproj / scutil.c
1 /*
2 * Copyright (c) 2000-2011 Apple Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24 /*
25 * Modification History
26 *
27 * June 13, 2005 Allan Nathanson <ajn@apple.com>
28 * - added SCPreferences support
29 *
30 * August 4, 2004 Allan Nathanson <ajn@apple.com>
31 * - added network configuration (prefs) support
32 *
33 * September 25, 2002 Allan Nathanson <ajn@apple.com>
34 * - added command line history & editing
35 *
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
39 * dynamic store key.
40 *
41 * June 1, 2001 Allan Nathanson <ajn@apple.com>
42 * - public API conversion
43 *
44 * November 9, 2000 Allan Nathanson <ajn@apple.com>
45 * - initial revision
46 */
47
48 #include <TargetConditionals.h>
49 #include <ctype.h>
50 #include <getopt.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <termios.h>
55 #include <unistd.h>
56 #include <sysexits.h>
57
58 #ifdef DEBUG
59 #include <mach/mach.h>
60 #include <mach/mach_error.h>
61 #endif /* DEBUG */
62
63 #include "scutil.h"
64 #include "commands.h"
65 #include "dictionary.h"
66 #include "net.h"
67 #include "nc.h"
68 #include "prefs.h"
69 #include "session.h"
70 #include "tests.h"
71
72
73 #define LINE_LENGTH 256
74
75
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;
87
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 },
105 { NULL, 0, NULL, 0 }
106 };
107
108
109 __private_extern__
110 CFStringRef
111 _copyStringFromSTDIN()
112 {
113 char buf[1024];
114 size_t len;
115 CFStringRef utf8;
116
117 if (fgets(buf, sizeof(buf), stdin) == NULL) {
118 return NULL;
119 }
120
121 len = strlen(buf);
122 if (buf[len-1] == '\n') {
123 buf[--len] = '\0';
124 }
125
126 utf8 = CFStringCreateWithBytes(NULL, (UInt8 *)buf, len, kCFStringEncodingUTF8, TRUE);
127 return utf8;
128 }
129
130 static char *
131 getLine(char *buf, int len, InputRef src)
132 {
133 int n;
134
135 if (src->el) {
136 int count;
137 const char *line;
138
139 line = el_gets(src->el, &count);
140 if (line == NULL)
141 return NULL;
142
143 strncpy(buf, line, len);
144 } else {
145 if (fgets(buf, len, src->fp) == NULL)
146 return NULL;
147 }
148
149 n = strlen(buf);
150 if (buf[n-1] == '\n') {
151 /* the entire line fit in the buffer, remove the newline */
152 buf[n-1] = '\0';
153 } else if (!src->el) {
154 /* eat the remainder of the line */
155 do {
156 n = fgetc(src->fp);
157 } while ((n != '\n') && (n != EOF));
158 }
159
160 if (src->h && (buf[0] != '\0')) {
161 HistEvent ev;
162
163 history(src->h, &ev, H_ENTER, buf);
164 }
165
166
167 return buf;
168 }
169
170
171 static char *
172 getString(char **line)
173 {
174 char *s, *e, c, *string;
175 int i, isQuoted = 0, escaped = 0;
176
177 if (*line == NULL) return NULL;
178 if (**line == '\0') return NULL;
179
180 /* Skip leading white space */
181 while (isspace(**line)) *line += 1;
182
183 /* Grab the next string */
184 s = *line;
185 if (*s == '\0') {
186 return NULL; /* no string available */
187 } else if (*s == '"') {
188 isQuoted = 1; /* it's a quoted string */
189 s++;
190 }
191
192 for (e = s; (c = *e) != '\0'; e++) {
193 if (isQuoted && (c == '"'))
194 break; /* end of quoted string */
195 if (c == '\\') {
196 e++;
197 if (*e == '\0')
198 break; /* if premature end-of-string */
199 if ((*e == '"') || isspace(*e))
200 escaped++; /* if escaped quote or white space */
201 }
202 if (!isQuoted && isspace(c))
203 break; /* end of non-quoted string */
204 }
205
206 string = malloc(e - s - escaped + 1);
207
208 for (i = 0; s < e; s++) {
209 string[i] = *s;
210 if (!((s[0] == '\\') && ((s[1] == '"') || isspace(s[1])))) i++;
211 }
212 string[i] = '\0';
213
214 if (isQuoted)
215 e++; /* move past end of quoted string */
216
217 *line = e;
218 return string;
219 }
220
221
222 __private_extern__
223 Boolean
224 process_line(InputRef src)
225 {
226 char *arg;
227 int argc = 0;
228 char **argv = NULL;
229 int i;
230 char line[LINE_LENGTH];
231 char *s = line;
232
233 // if end-of-file, exit
234 if (getLine(line, sizeof(line), src) == NULL)
235 return FALSE;
236
237 if (nesting > 0) {
238 SCPrint(TRUE, stdout, CFSTR("%d> %s\n"), nesting, line);
239 }
240
241 // break up the input line
242 while ((arg = getString(&s)) != NULL) {
243 if (argc == 0)
244 argv = (char **)malloc(2 * sizeof(char *));
245 else
246 argv = (char **)reallocf(argv, ((argc + 2) * sizeof(char *)));
247 argv[argc++] = arg;
248 }
249
250 if (argc == 0) {
251 return TRUE; // if no arguments
252 }
253
254 /* process the command */
255 if (*argv[0] != '#') {
256 argv[argc] = NULL; // just in case...
257 currentInput = src;
258 do_command(argc, argv);
259 }
260
261 /* free the arguments */
262 for (i = 0; i < argc; i++) {
263 free(argv[i]);
264 }
265 free(argv);
266
267 return !termRequested;
268 }
269
270
271 static void
272 usage(const char *command)
273 {
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"));
302
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"));
307 }
308
309 exit (EX_USAGE);
310 }
311
312
313 static char *
314 prompt(EditLine *el)
315 {
316 return "> ";
317 }
318
319
320 int
321 main(int argc, char * const argv[])
322 {
323 Boolean doDNS = FALSE;
324 Boolean doNet = FALSE;
325 Boolean doPrefs = FALSE;
326 Boolean doProxy = FALSE;
327 Boolean doReach = FALSE;
328 char *get = NULL;
329 extern int optind;
330 int opt;
331 int opti;
332 const char *prog = argv[0];
333 char *set = NULL;
334 char *nc_cmd = NULL;
335 InputRef src;
336 int timeout = 15; /* default timeout (in seconds) */
337 char *wait = NULL;
338 Boolean watch = FALSE;
339 int xStore = 0; /* non dynamic store command line options */
340
341 /* process any arguments */
342
343 while ((opt = getopt_long(argc, argv, "dDvprt:w:W", longopts, &opti)) != -1)
344 switch(opt) {
345 case 'd':
346 _sc_debug = TRUE;
347 _sc_log = FALSE; /* enable framework logging */
348 break;
349 case 'D':
350 doDispatch = TRUE;
351 break;
352 case 'v':
353 _sc_verbose = TRUE;
354 _sc_log = FALSE; /* enable framework logging */
355 break;
356 case 'p':
357 enablePrivateAPI = TRUE;
358 break;
359 case 'r':
360 doReach = TRUE;
361 xStore++;
362 break;
363 case 't':
364 timeout = atoi(optarg);
365 break;
366 case 'w':
367 wait = optarg;
368 xStore++;
369 break;
370 case 'W':
371 watch = TRUE;
372 break;
373 case 0:
374 if (strcmp(longopts[opti].name, "dns") == 0) {
375 doDNS = TRUE;
376 xStore++;
377 } else if (strcmp(longopts[opti].name, "get") == 0) {
378 get = optarg;
379 xStore++;
380 } else if (strcmp(longopts[opti].name, "net") == 0) {
381 doNet = TRUE;
382 xStore++;
383 } else if (strcmp(longopts[opti].name, "prefs") == 0) {
384 doPrefs = TRUE;
385 xStore++;
386 } else if (strcmp(longopts[opti].name, "proxy") == 0) {
387 doProxy = TRUE;
388 xStore++;
389 } else if (strcmp(longopts[opti].name, "set") == 0) {
390 set = optarg;
391 xStore++;
392 } else if (strcmp(longopts[opti].name, "nc") == 0) {
393 nc_cmd = optarg;
394 xStore++;
395 }
396 break;
397 case '?':
398 default :
399 usage(prog);
400 }
401 argc -= optind;
402 argv += optind;
403
404 if (xStore > 1) {
405 // if we are attempting to process more than one type of request
406 usage(prog);
407 }
408 /* are we checking (or watching) the reachability of a host/address */
409 if (doReach) {
410 if (argc < 1) {
411 usage(prog);
412 }
413 if (watch) {
414 do_watchReachability(argc, (char **)argv);
415 } else {
416 do_checkReachability(argc, (char **)argv);
417 }
418 /* NOT REACHED */
419 }
420
421 /* are we waiting on the presense of a dynamic store key */
422 if (wait) {
423 do_wait(wait, timeout);
424 /* NOT REACHED */
425 }
426
427 /* are we looking up the DNS configuration */
428 if (doDNS) {
429 do_showDNSConfiguration(argc, (char **)argv);
430 /* NOT REACHED */
431 }
432
433 /* are we looking up a preference value */
434 if (get) {
435 if (findPref(get) < 0) {
436 usage(prog);
437 }
438 do_getPref(get, argc, (char **)argv);
439 /* NOT REACHED */
440 }
441
442 /* are we looking up the proxy configuration */
443 if (doProxy) {
444 do_showProxyConfiguration(argc, (char **)argv);
445 /* NOT REACHED */
446 }
447
448 /* are we changing a preference value */
449 if (set) {
450 if (findPref(set) < 0) {
451 usage(prog);
452 }
453 do_setPref(set, argc, (char **)argv);
454 /* NOT REACHED */
455 }
456
457 /* network connection commands */
458 if (nc_cmd) {
459 if (find_nc_cmd(nc_cmd) < 0) {
460 usage(prog);
461 }
462 do_nc_cmd(nc_cmd, argc, (char **)argv, watch);
463 /* NOT REACHED */
464 }
465
466 if (doNet) {
467 /* if we are going to be managing the network configuration */
468 commands = (cmdInfo *)commands_net;
469 nCommands = nCommands_net;
470
471 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
472 usage(prog);
473 }
474
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;
481
482 do_dictInit(0, NULL); /* start with an empty dictionary */
483 do_prefs_init(); /* initialization */
484 do_prefs_open(argc, (char **)argv); /* open prefs */
485 } else {
486 /* if we are going to be managing the dynamic store */
487 commands = (cmdInfo *)commands_store;
488 nCommands = nCommands_store;
489
490 do_dictInit(0, NULL); /* start with an empty dictionary */
491 do_open(0, NULL); /* open the dynamic store */
492 }
493
494 /* allocate command input stream */
495 src = (InputRef)CFAllocatorAllocate(NULL, sizeof(Input), 0);
496 src->fp = stdin;
497 src->el = NULL;
498 src->h = NULL;
499
500 if (isatty(fileno(src->fp))) {
501 int editmode = 1;
502 HistEvent ev;
503 struct termios t;
504
505 if (tcgetattr(fileno(src->fp), &t) != -1) {
506 if ((t.c_lflag & ECHO) == 0) {
507 editmode = 0;
508 }
509 }
510 src->el = el_init(prog, src->fp, stdout, stderr);
511 src->h = history_init();
512
513 (void)history(src->h, &ev, H_SETSIZE, INT_MAX);
514 el_set(src->el, EL_HIST, history, src->h);
515
516 if (!editmode) {
517 el_set(src->el, EL_EDITMODE, 0);
518 }
519
520 el_set(src->el, EL_EDITOR, "emacs");
521 el_set(src->el, EL_PROMPT, prompt);
522
523 el_source(src->el, NULL);
524
525 if ((el_get(src->el, EL_EDITMODE, &editmode) != -1) && editmode != 0) {
526 el_set(src->el, EL_SIGNAL, 1);
527 } else {
528 history_end(src->h);
529 src->h = NULL;
530 el_end(src->el);
531 src->el = NULL;
532 }
533 }
534
535 while (TRUE) {
536 Boolean ok;
537
538 ok = process_line(src);
539 if (!ok) {
540 break;
541 }
542 }
543
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);
549
550 exit (EX_OK); // insure the process exit status is 0
551 return 0; // ...and make main fit the ANSI spec.
552 }