]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/scutil.c
c4bff2a6e9e4f98110c34444e01f69a50a68979c
[apple/configd.git] / scutil.tproj / scutil.c
1 /*
2 * Copyright (c) 2000-2013 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 { "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 { "renew", required_argument, NULL, 0 },
106 { "set", required_argument, NULL, 0 },
107 { "snapshot", no_argument, NULL, 0 },
108 { "user", required_argument, NULL, 0 },
109 { "password", required_argument, NULL, 0 },
110 { "secret", required_argument, NULL, 0 },
111 { "log", required_argument, NULL, 0 },
112 { NULL, 0, NULL, 0 }
113 };
114
115
116 __private_extern__
117 CFStringRef
118 _copyStringFromSTDIN(CFStringRef prompt, CFStringRef defaultValue)
119 {
120 char buf[1024];
121 int i;
122 Boolean is_user_prompt = (prompt != NULL && isatty(STDIN_FILENO) && isatty(STDOUT_FILENO));
123 size_t len;
124 char *modbuf;
125 size_t modlen;
126 CFStringRef utf8;
127
128 /* Print out a prompt to user that entry is desired */
129 if (is_user_prompt) {
130 if (defaultValue != NULL) {
131 SCPrint(TRUE, stdout, CFSTR("%@ [%@]: "), prompt, defaultValue);
132 } else {
133 SCPrint(TRUE, stdout, CFSTR("%@: "), prompt);
134 }
135 }
136
137 /* Get user input */
138 if (fgets(buf, sizeof(buf), stdin) == NULL) {
139 return NULL;
140 }
141
142 /* Prepare for trim */
143 len = strlen(buf);
144 modbuf = buf;
145 modlen = len;
146
147 /* Trim new-line */
148 if ((modlen > 0) && (modbuf[modlen - 1] == '\n')) {
149 modbuf[modlen - 1] = '\0';
150 modlen--;
151 }
152
153 /* If nothing was entered at the user prompt, set default */
154 if (is_user_prompt && defaultValue != NULL && modlen == 0) {
155 CFRetain(defaultValue);
156 return defaultValue;
157 }
158
159 /* Trim spaces from front */
160 while (modlen > 0 && isspace(modbuf[0])) {
161 modbuf = &modbuf[1];
162 modlen--;
163 }
164
165 /* Trim spaces from back */
166 for (i = modlen - 1; i >= 0; i--) {
167 if (isspace(buf[i])) {
168 buf[i] = '\0';
169 modlen--;
170 } else {
171 break;
172 }
173 }
174
175 utf8 = CFStringCreateWithBytes(NULL, (UInt8 *)modbuf, modlen, kCFStringEncodingUTF8, TRUE);
176 return utf8;
177 }
178
179 static char *
180 getLine(char *buf, int len, InputRef src)
181 {
182 int n;
183
184 if (src->el) {
185 int count;
186 const char *line;
187
188 line = el_gets(src->el, &count);
189 if (line == NULL)
190 return NULL;
191
192 strncpy(buf, line, len);
193 } else {
194 if (fgets(buf, len, src->fp) == NULL)
195 return NULL;
196 }
197
198 n = strlen(buf);
199 if (buf[n-1] == '\n') {
200 /* the entire line fit in the buffer, remove the newline */
201 buf[n-1] = '\0';
202 } else if (!src->el) {
203 /* eat the remainder of the line */
204 do {
205 n = fgetc(src->fp);
206 } while ((n != '\n') && (n != EOF));
207 }
208
209 if (src->h && (buf[0] != '\0')) {
210 HistEvent ev;
211
212 history(src->h, &ev, H_ENTER, buf);
213 }
214
215
216 return buf;
217 }
218
219
220 static char *
221 getString(char **line)
222 {
223 char *s, *e, c, *string;
224 int i, isQuoted = 0, escaped = 0;
225
226 if (*line == NULL) return NULL;
227 if (**line == '\0') return NULL;
228
229 /* Skip leading white space */
230 while (isspace(**line)) *line += 1;
231
232 /* Grab the next string */
233 s = *line;
234 if (*s == '\0') {
235 return NULL; /* no string available */
236 } else if (*s == '"') {
237 isQuoted = 1; /* it's a quoted string */
238 s++;
239 }
240
241 for (e = s; (c = *e) != '\0'; e++) {
242 if (isQuoted && (c == '"'))
243 break; /* end of quoted string */
244 if (c == '\\') {
245 e++;
246 if (*e == '\0')
247 break; /* if premature end-of-string */
248 if ((*e == '"') || isspace(*e))
249 escaped++; /* if escaped quote or white space */
250 }
251 if (!isQuoted && isspace(c))
252 break; /* end of non-quoted string */
253 }
254
255 string = malloc(e - s - escaped + 1);
256
257 for (i = 0; s < e; s++) {
258 string[i] = *s;
259 if (!((s[0] == '\\') && ((s[1] == '"') || isspace(s[1])))) i++;
260 }
261 string[i] = '\0';
262
263 if (isQuoted)
264 e++; /* move past end of quoted string */
265
266 *line = e;
267 return string;
268 }
269
270
271 __private_extern__
272 Boolean
273 process_line(InputRef src)
274 {
275 char *arg;
276 int argc = 0;
277 char **argv = NULL;
278 int i;
279 char line[LINE_LENGTH];
280 char *s = line;
281
282 // if end-of-file, exit
283 if (getLine(line, sizeof(line), src) == NULL)
284 return FALSE;
285
286 if (nesting > 0) {
287 SCPrint(TRUE, stdout, CFSTR("%d> %s\n"), nesting, line);
288 }
289
290 // break up the input line
291 while ((arg = getString(&s)) != NULL) {
292 if (argc == 0)
293 argv = (char **)malloc(2 * sizeof(char *));
294 else
295 argv = (char **)reallocf(argv, ((argc + 2) * sizeof(char *)));
296 argv[argc++] = arg;
297 }
298
299 if (argc == 0) {
300 return TRUE; // if no arguments
301 }
302
303 /* process the command */
304 if (*argv[0] != '#') {
305 argv[argc] = NULL; // just in case...
306 currentInput = src;
307 do_command(argc, argv);
308 }
309
310 /* free the arguments */
311 for (i = 0; i < argc; i++) {
312 free(argv[i]);
313 }
314 free(argv);
315
316 return !termRequested;
317 }
318
319
320 static void
321 usage(const char *command)
322 {
323 SCPrint(TRUE, stderr, CFSTR("usage: %s\n"), command);
324 SCPrint(TRUE, stderr, CFSTR("\tinteractive access to the dynamic store.\n"));
325 SCPrint(TRUE, stderr, CFSTR("\n"));
326 SCPrint(TRUE, stderr, CFSTR(" or: %s --prefs [preference-file]\n"), command);
327 SCPrint(TRUE, stderr, CFSTR("\tinteractive access to the [raw] stored preferences.\n"));
328 SCPrint(TRUE, stderr, CFSTR("\n"));
329 SCPrint(TRUE, stderr, CFSTR(" or: %s [-W] -r nodename\n"), command);
330 SCPrint(TRUE, stderr, CFSTR(" or: %s [-W] -r address\n"), command);
331 SCPrint(TRUE, stderr, CFSTR(" or: %s [-W] -r local-address remote-address\n"), command);
332 SCPrint(TRUE, stderr, CFSTR("\tcheck reachability of node, address, or address pair (-W to \"watch\").\n"));
333 SCPrint(TRUE, stderr, CFSTR("\n"));
334 SCPrint(TRUE, stderr, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command);
335 SCPrint(TRUE, stderr, CFSTR("\t-w\twait for presense of dynamic store key\n"));
336 SCPrint(TRUE, stderr, CFSTR("\t-t\ttime to wait for key\n"));
337 SCPrint(TRUE, stderr, CFSTR("\n"));
338 SCPrint(TRUE, stderr, CFSTR(" or: %s --get pref\n"), command);
339 SCPrint(TRUE, stderr, CFSTR(" or: %s --set pref [newval]\n"), command);
340 SCPrint(TRUE, stderr, CFSTR(" or: %s --get filename path key \n"), command);
341 SCPrint(TRUE, stderr, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
342 SCPrint(TRUE, stderr, CFSTR("\t\tinclude:\n"));
343 SCPrint(TRUE, stderr, CFSTR("\t\t\tComputerName, LocalHostName, HostName\n"));
344 SCPrint(TRUE, stderr, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
345 SCPrint(TRUE, stderr, CFSTR("\t\tthe new value will be read from standard input.\n"));
346 SCPrint(TRUE, stderr, CFSTR("\n"));
347 SCPrint(TRUE, stderr, CFSTR(" or: %s --dns\n"), command);
348 SCPrint(TRUE, stderr, CFSTR("\tshow DNS configuration.\n"));
349 SCPrint(TRUE, stderr, CFSTR("\n"));
350 SCPrint(TRUE, stderr, CFSTR(" or: %s --proxy\n"), command);
351 SCPrint(TRUE, stderr, CFSTR("\tshow \"proxy\" configuration.\n"));
352 SCPrint(TRUE, stderr, CFSTR("\n"));
353 SCPrint(TRUE, stderr, CFSTR(" or: %s --nwi\n"), command);
354 SCPrint(TRUE, stderr, CFSTR("\tshow network information\n"));
355 SCPrint(TRUE, stderr, CFSTR("\n"));
356 SCPrint(TRUE, stderr, CFSTR(" or: %s --nc\n"), command);
357 SCPrint(TRUE, stderr, CFSTR("\tshow VPN network configuration information. Use --nc help for full command list\n"));
358
359 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
360 SCPrint(TRUE, stderr, CFSTR("\n"));
361 SCPrint(TRUE, stderr, CFSTR(" or: %s --net\n"), command);
362 SCPrint(TRUE, stderr, CFSTR("\tmanage network configuration.\n"));
363 }
364
365 exit (EX_USAGE);
366 }
367
368
369 static char *
370 prompt(EditLine *el)
371 {
372 #if !TARGET_IPHONE_SIMULATOR
373 return "> ";
374 #else // !TARGET_IPHONE_SIMULATOR
375 return "sim> ";
376 #endif // !TARGET_IPHONE_SIMULATOR
377 }
378
379
380 int
381 main(int argc, char * const argv[])
382 {
383 Boolean doDNS = FALSE;
384 Boolean doNet = FALSE;
385 Boolean doNWI = FALSE;
386 Boolean doPrefs = FALSE;
387 Boolean doProxy = FALSE;
388 Boolean doReach = FALSE;
389 Boolean doSnap = FALSE;
390 char *get = NULL;
391 char *log = NULL;
392 extern int optind;
393 int opt;
394 int opti;
395 const char *prog = argv[0];
396 char *renew = NULL;
397 char *set = NULL;
398 char *nc_cmd = NULL;
399 InputRef src;
400 int timeout = 15; /* default timeout (in seconds) */
401 char *wait = NULL;
402 Boolean watch = FALSE;
403 int xStore = 0; /* non dynamic store command line options */
404
405 /* process any arguments */
406
407 while ((opt = getopt_long(argc, argv, "dDvprt:w:W", longopts, &opti)) != -1)
408 switch(opt) {
409 case 'd':
410 _sc_debug = TRUE;
411 _sc_log = FALSE; /* enable framework logging */
412 break;
413 case 'D':
414 doDispatch = TRUE;
415 break;
416 case 'v':
417 _sc_verbose = TRUE;
418 _sc_log = FALSE; /* enable framework logging */
419 break;
420 case 'p':
421 enablePrivateAPI = TRUE;
422 break;
423 case 'r':
424 doReach = TRUE;
425 xStore++;
426 break;
427 case 't':
428 timeout = atoi(optarg);
429 break;
430 case 'w':
431 wait = optarg;
432 xStore++;
433 break;
434 case 'W':
435 watch = TRUE;
436 break;
437 case 0:
438 if (strcmp(longopts[opti].name, "dns") == 0) {
439 doDNS = TRUE;
440 xStore++;
441 } else if (strcmp(longopts[opti].name, "get") == 0) {
442 get = optarg;
443 xStore++;
444 } else if (strcmp(longopts[opti].name, "nc") == 0) {
445 nc_cmd = optarg;
446 xStore++;
447 } else if (strcmp(longopts[opti].name, "net") == 0) {
448 doNet = TRUE;
449 xStore++;
450 } else if (strcmp(longopts[opti].name, "nwi") == 0) {
451 doNWI = TRUE;
452 xStore++;
453 } else if (strcmp(longopts[opti].name, "prefs") == 0) {
454 doPrefs = TRUE;
455 xStore++;
456 } else if (strcmp(longopts[opti].name, "proxy") == 0) {
457 doProxy = TRUE;
458 xStore++;
459 } else if (strcmp(longopts[opti].name, "renew") == 0) {
460 renew = optarg;
461 xStore++;
462 } else if (strcmp(longopts[opti].name, "set") == 0) {
463 set = optarg;
464 xStore++;
465 } else if (strcmp(longopts[opti].name, "snapshot") == 0) {
466 doSnap = TRUE;
467 xStore++;
468 } else if (strcmp(longopts[opti].name, "log") == 0) {
469 log = optarg;
470 xStore++;
471 } else if (strcmp(longopts[opti].name, "user") == 0) {
472 username = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
473 } else if (strcmp(longopts[opti].name, "password") == 0) {
474 password = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
475 } else if (strcmp(longopts[opti].name, "secret") == 0) {
476 sharedsecret = CFStringCreateWithCString(NULL, optarg, kCFStringEncodingUTF8);
477 }
478 break;
479 case '?':
480 default :
481 usage(prog);
482 }
483 argc -= optind;
484 argv += optind;
485
486 if (xStore > 1) {
487 // if we are attempting to process more than one type of request
488 usage(prog);
489 }
490
491 /* are we checking (or watching) the reachability of a host/address */
492 if (doReach) {
493 if (argc < 1) {
494 usage(prog);
495 }
496 if (watch) {
497 do_watchReachability(argc, (char **)argv);
498 } else {
499 do_checkReachability(argc, (char **)argv);
500 }
501 /* NOT REACHED */
502 }
503
504 /* are we waiting on the presense of a dynamic store key */
505 if (wait) {
506 do_wait(wait, timeout);
507 /* NOT REACHED */
508 }
509
510 /* are we looking up the DNS configuration */
511 if (doDNS) {
512 do_showDNSConfiguration(argc, (char **)argv);
513 /* NOT REACHED */
514 }
515
516 if (doNWI) {
517 do_nwi(argc, (char**)argv);
518 /* NOT REACHED */
519 }
520
521 if (doSnap) {
522 if (!enablePrivateAPI
523 #if !TARGET_IPHONE_SIMULATOR
524 || (geteuid() != 0)
525 #endif // !TARGET_IPHONE_SIMULATOR
526 ) {
527 usage(prog);
528 }
529
530 do_open(0, NULL); /* open the dynamic store */
531 do_snapshot(argc, (char**)argv);
532 exit(0);
533 }
534
535 /* are we looking up a preference value */
536 if (get) {
537 if (argc != 2) {
538 if (findPref(get) < 0) {
539 usage(prog);
540 }
541 } else {
542 /* need to go back one argument
543 * for the filename */
544 argc++;
545 argv--;
546 }
547
548 do_getPref(get, argc, (char **)argv);
549 /* NOT REACHED */
550 }
551
552 /* are we looking up the proxy configuration */
553 if (doProxy) {
554 do_showProxyConfiguration(argc, (char **)argv);
555 /* NOT REACHED */
556 }
557
558 /* are we changing a preference value */
559 if (set) {
560 if (findPref(set) < 0) {
561 usage(prog);
562 }
563 do_setPref(set, argc, (char **)argv);
564 /* NOT REACHED */
565 }
566
567 /* verbose log */
568 if (log != NULL) {
569 if (strcasecmp(log, "IPMonitor")) {
570 usage(prog);
571 }
572 do_log(log, argc, (char * *)argv);
573 /* NOT REACHED */
574 }
575 /* network connection commands */
576 if (nc_cmd) {
577 if (find_nc_cmd(nc_cmd) < 0) {
578 usage(prog);
579 }
580 do_nc_cmd(nc_cmd, argc, (char **)argv, watch);
581 /* NOT REACHED */
582 }
583
584 if (doNet) {
585 /* if we are going to be managing the network configuration */
586 commands = (cmdInfo *)commands_net;
587 nCommands = nCommands_net;
588
589 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
590 usage(prog);
591 }
592
593 do_net_init(); /* initialization */
594 do_net_open(argc, (char **)argv); /* open prefs */
595 } else if (doPrefs) {
596 /* if we are going to be managing the network configuration */
597 commands = (cmdInfo *)commands_prefs;
598 nCommands = nCommands_prefs;
599
600 do_dictInit(0, NULL); /* start with an empty dictionary */
601 do_prefs_init(); /* initialization */
602 do_prefs_open(argc, (char **)argv); /* open prefs */
603 } else {
604 /* if we are going to be managing the dynamic store */
605 commands = (cmdInfo *)commands_store;
606 nCommands = nCommands_store;
607
608 do_dictInit(0, NULL); /* start with an empty dictionary */
609 do_open(0, NULL); /* open the dynamic store */
610 }
611
612 /* are we trying to renew a DHCP lease */
613 if (renew != NULL) {
614 do_renew(renew);
615 /* NOT REACHED */
616 }
617
618 /* allocate command input stream */
619 src = (InputRef)CFAllocatorAllocate(NULL, sizeof(Input), 0);
620 src->fp = stdin;
621 src->el = NULL;
622 src->h = NULL;
623
624 if (isatty(fileno(src->fp))) {
625 int editmode = 1;
626 HistEvent ev;
627 struct termios t;
628
629 if (tcgetattr(fileno(src->fp), &t) != -1) {
630 if ((t.c_lflag & ECHO) == 0) {
631 editmode = 0;
632 }
633 }
634 src->el = el_init(prog, src->fp, stdout, stderr);
635 src->h = history_init();
636
637 (void)history(src->h, &ev, H_SETSIZE, INT_MAX);
638 el_set(src->el, EL_HIST, history, src->h);
639
640 if (!editmode) {
641 el_set(src->el, EL_EDITMODE, 0);
642 }
643
644 el_set(src->el, EL_EDITOR, "emacs");
645 el_set(src->el, EL_PROMPT, prompt);
646
647 el_source(src->el, NULL);
648
649 if ((el_get(src->el, EL_EDITMODE, &editmode) != -1) && editmode != 0) {
650 el_set(src->el, EL_SIGNAL, 1);
651 } else {
652 history_end(src->h);
653 src->h = NULL;
654 el_end(src->el);
655 src->el = NULL;
656 }
657 }
658
659 while (TRUE) {
660 Boolean ok;
661
662 ok = process_line(src);
663 if (!ok) {
664 break;
665 }
666 }
667
668 /* close the socket, free resources */
669 if (src->h) history_end(src->h);
670 if (src->el) el_end(src->el);
671 (void)fclose(src->fp);
672 CFAllocatorDeallocate(NULL, src);
673
674 exit (EX_OK); // insure the process exit status is 0
675 return 0; // ...and make main fit the ANSI spec.
676 }