]> git.saurik.com Git - apple/configd.git/blob - scutil.tproj/scutil.c
configd-130.tar.gz
[apple/configd.git] / scutil.tproj / scutil.c
1 /*
2 * Copyright (c) 2000-2004 Apple Computer, 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 * August 4, 2004 Allan Nathanson <ajn@apple.com>
28 * - added network configuration (prefs) support
29 *
30 * September 25, 2002 Allan Nathanson <ajn@apple.com>
31 * - added command line history & editing
32 *
33 * July 9, 2001 Allan Nathanson <ajn@apple.com>
34 * - added "-r" option for checking network reachability
35 * - added "-w" option to check/wait for the presence of a
36 * dynamic store key.
37 *
38 * June 1, 2001 Allan Nathanson <ajn@apple.com>
39 * - public API conversion
40 *
41 * November 9, 2000 Allan Nathanson <ajn@apple.com>
42 * - initial revision
43 */
44
45 #include <ctype.h>
46 #include <getopt.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <string.h>
50 #include <termios.h>
51 #include <unistd.h>
52 #include <sysexits.h>
53
54 #ifdef DEBUG
55 #include <mach/mach.h>
56 #include <mach/mach_error.h>
57 #endif /* DEBUG */
58
59 #include "scutil.h"
60 #include "commands.h"
61 #include "dictionary.h"
62 #include "net.h"
63 #include "prefs.h"
64 #include "session.h"
65 #include "tests.h"
66
67 #include "SCDynamicStoreInternal.h"
68
69
70 #define LINE_LENGTH 256
71
72
73 __private_extern__ InputRef currentInput = NULL;
74 __private_extern__ int nesting = 0;
75 __private_extern__ CFRunLoopRef notifyRl = NULL;
76 __private_extern__ CFRunLoopSourceRef notifyRls = NULL;
77 __private_extern__ SCPreferencesRef prefs = NULL;
78 __private_extern__ SCDynamicStoreRef store = NULL;
79 __private_extern__ CFPropertyListRef value = NULL;
80 __private_extern__ CFMutableArrayRef watchedKeys = NULL;
81 __private_extern__ CFMutableArrayRef watchedPatterns = NULL;
82
83 static const struct option longopts[] = {
84 // { "debug", no_argument, NULL, 'd' },
85 // { "verbose", no_argument, NULL, 'v' },
86 // { "SPI", no_argument, NULL, 'p' },
87 // { "check-reachability", required_argument, NULL, 'r' },
88 // { "timeout", required_argument, NULL, 't' },
89 // { "wait-key", required_argument, NULL, 'w' },
90 { "dns", no_argument, NULL, 0 },
91 { "get", required_argument, NULL, 0 },
92 { "help", no_argument, NULL, '?' },
93 { "net", no_argument, NULL, 0 },
94 { "proxy", no_argument, NULL, 0 },
95 { "set", required_argument, NULL, 0 },
96 { NULL, 0, NULL, 0 }
97 };
98
99
100 static char *
101 getLine(char *buf, int len, InputRef src)
102 {
103 int n;
104
105 if (src->el) {
106 int count;
107 const char *line;
108
109 line = el_gets(src->el, &count);
110 if (line == NULL)
111 return NULL;
112
113 strncpy(buf, line, len);
114 } else {
115 if (fgets(buf, len, src->fp) == NULL)
116 return NULL;
117 }
118
119 n = strlen(buf);
120 if (buf[n-1] == '\n') {
121 /* the entire line fit in the buffer, remove the newline */
122 buf[n-1] = '\0';
123 } else if (!src->el) {
124 /* eat the remainder of the line */
125 do {
126 n = fgetc(src->fp);
127 } while ((n != '\n') && (n != EOF));
128 }
129
130 if (src->h) {
131 HistEvent ev;
132
133 history(src->h, &ev, H_ENTER, buf);
134 }
135
136 return buf;
137 }
138
139
140 static char *
141 getString(char **line)
142 {
143 char *s, *e, c, *string;
144 int i, isQuoted = 0, escaped = 0;
145
146 if (*line == NULL) return NULL;
147 if (**line == '\0') return NULL;
148
149 /* Skip leading white space */
150 while (isspace(**line)) *line += 1;
151
152 /* Grab the next string */
153 s = *line;
154 if (*s == '\0') {
155 return NULL; /* no string available */
156 } else if (*s == '"') {
157 isQuoted = 1; /* it's a quoted string */
158 s++;
159 }
160
161 for (e = s; (c = *e) != '\0'; e++) {
162 if (isQuoted && (c == '"'))
163 break; /* end of quoted string */
164 if (c == '\\') {
165 e++;
166 if (*e == '\0')
167 break; /* if premature end-of-string */
168 if ((*e == '"') || isspace(*e))
169 escaped++; /* if escaped quote or white space */
170 }
171 if (!isQuoted && isspace(c))
172 break; /* end of non-quoted string */
173 }
174
175 string = malloc(e - s - escaped + 1);
176
177 for (i = 0; s < e; s++) {
178 string[i] = *s;
179 if (!((s[0] == '\\') && ((s[1] == '"') || isspace(s[1])))) i++;
180 }
181 string[i] = '\0';
182
183 if (isQuoted)
184 e++; /* move past end of quoted string */
185
186 *line = e;
187 return string;
188 }
189
190
191 __private_extern__
192 Boolean
193 process_line(InputRef src)
194 {
195 char *arg;
196 int argc = 0;
197 char **argv = NULL;
198 int i;
199 char line[LINE_LENGTH];
200 char *s = line;
201
202 // if end-of-file, exit
203 if (getLine(line, sizeof(line), src) == NULL)
204 return FALSE;
205
206 if (nesting > 0) {
207 SCPrint(TRUE, stdout, CFSTR("%d> %s\n"), nesting, line);
208 }
209
210 // break up the input line
211 while ((arg = getString(&s)) != NULL) {
212 if (argc == 0)
213 argv = (char **)malloc(2 * sizeof(char *));
214 else
215 argv = (char **)reallocf(argv, ((argc + 2) * sizeof(char *)));
216 argv[argc++] = arg;
217 }
218
219 if (argc == 0) {
220 return TRUE; // if no arguments
221 }
222
223 /* process the command */
224 if (*argv[0] != '#') {
225 argv[argc] = NULL; // just in case...
226 currentInput = src;
227 do_command(argc, argv);
228 }
229
230 /* free the arguments */
231 for (i = 0; i < argc; i++) {
232 free(argv[i]);
233 }
234 free(argv);
235
236 return !termRequested;
237 }
238
239
240 static void
241 usage(const char *command)
242 {
243 SCPrint(TRUE, stderr, CFSTR("usage: %s\n"), command);
244 SCPrint(TRUE, stderr, CFSTR("\tinteractive access to the dynamic store.\n"));
245 SCPrint(TRUE, stderr, CFSTR("\n"));
246 SCPrint(TRUE, stderr, CFSTR(" or: %s -r nodename\n"), command);
247 SCPrint(TRUE, stderr, CFSTR(" or: %s -r address\n"), command);
248 SCPrint(TRUE, stderr, CFSTR(" or: %s -r local-address remote-address\n"), command);
249 SCPrint(TRUE, stderr, CFSTR("\tcheck reachability of node, address, or address pair.\n"));
250 SCPrint(TRUE, stderr, CFSTR("\n"));
251 SCPrint(TRUE, stderr, CFSTR(" or: %s -w dynamic-store-key [ -t timeout ]\n"), command);
252 SCPrint(TRUE, stderr, CFSTR("\t-w\twait for presense of dynamic store key\n"));
253 SCPrint(TRUE, stderr, CFSTR("\t-t\ttime to wait for key\n"));
254 SCPrint(TRUE, stderr, CFSTR("\n"));
255 SCPrint(TRUE, stderr, CFSTR(" or: %s --get pref\n"), command);
256 SCPrint(TRUE, stderr, CFSTR(" or: %s --set pref [newval]\n"), command);
257 SCPrint(TRUE, stderr, CFSTR("\tpref\tdisplay (or set) the specified preference. Valid preferences\n"));
258 SCPrint(TRUE, stderr, CFSTR("\t\tinclude:\n"));
259 SCPrint(TRUE, stderr, CFSTR("\t\t\tComputerName, LocalHostName\n"));
260 SCPrint(TRUE, stderr, CFSTR("\tnewval\tNew preference value to be set. If not specified,\n"));
261 SCPrint(TRUE, stderr, CFSTR("\t\tthe new value will be read from standard input.\n"));
262 SCPrint(TRUE, stderr, CFSTR("\n"));
263 SCPrint(TRUE, stderr, CFSTR(" or: %s --dns\n"), command);
264 SCPrint(TRUE, stderr, CFSTR("\tshow DNS configuration.\n"));
265 SCPrint(TRUE, stderr, CFSTR("\n"));
266 SCPrint(TRUE, stderr, CFSTR(" or: %s --proxy\n"), command);
267 SCPrint(TRUE, stderr, CFSTR("\tshow \"proxy\" configuration.\n"));
268
269 if (getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
270 SCPrint(TRUE, stderr, CFSTR("\n"));
271 SCPrint(TRUE, stderr, CFSTR(" or: %s --net\n"), command);
272 SCPrint(TRUE, stderr, CFSTR("\tmanage network configuration.\n"));
273 }
274
275 exit (EX_USAGE);
276 }
277
278
279 static char *
280 prompt(EditLine *el)
281 {
282 return "> ";
283 }
284
285
286 int
287 main(int argc, char * const argv[])
288 {
289 Boolean dns = FALSE;
290 char *get = NULL;
291 Boolean net = FALSE;
292 extern int optind;
293 int opt;
294 int opti;
295 const char *prog = argv[0];
296 Boolean proxy = FALSE;
297 Boolean reach = FALSE;
298 char *set = NULL;
299 InputRef src;
300 int timeout = 15; /* default timeout (in seconds) */
301 char *wait = NULL;
302 int xStore = 0; /* non dynamic store command line options */
303
304 /* process any arguments */
305
306 while ((opt = getopt_long(argc, argv, "dvprt:w:", longopts, &opti)) != -1)
307 switch(opt) {
308 case 'd':
309 _sc_debug = TRUE;
310 _sc_log = FALSE; /* enable framework logging */
311 break;
312 case 'v':
313 _sc_verbose = TRUE;
314 _sc_log = FALSE; /* enable framework logging */
315 break;
316 case 'p':
317 enablePrivateAPI = TRUE;
318 break;
319 case 'r':
320 reach = TRUE;
321 xStore++;
322 break;
323 case 't':
324 timeout = atoi(optarg);
325 break;
326 case 'w':
327 wait = optarg;
328 xStore++;
329 break;
330 case 0:
331 if (strcmp(longopts[opti].name, "dns") == 0) {
332 dns = TRUE;
333 xStore++;
334 } else if (strcmp(longopts[opti].name, "get") == 0) {
335 get = optarg;
336 xStore++;
337 } else if (strcmp(longopts[opti].name, "net") == 0) {
338 net = TRUE;
339 xStore++;
340 } else if (strcmp(longopts[opti].name, "proxy") == 0) {
341 proxy = TRUE;
342 xStore++;
343 } else if (strcmp(longopts[opti].name, "set") == 0) {
344 set = optarg;
345 xStore++;
346 }
347 break;
348 case '?':
349 default :
350 usage(prog);
351 }
352 argc -= optind;
353 argv += optind;
354
355 if (xStore > 1) {
356 // if we are attempting to process more than one type of request
357 usage(prog);
358 }
359
360 /* are we checking the reachability of a host/address */
361 if (reach) {
362 if ((argc < 1) || (argc > 2)) {
363 usage(prog);
364 }
365 do_checkReachability(argc, (char **)argv);
366 /* NOT REACHED */
367 }
368
369 /* are we waiting on the presense of a dynamic store key */
370 if (wait) {
371 do_wait(wait, timeout);
372 /* NOT REACHED */
373 }
374
375 /* are we looking up the DNS configuration */
376 if (dns) {
377 do_showDNSConfiguration(argc, (char **)argv);
378 /* NOT REACHED */
379 }
380
381 /* are we looking up a preference value */
382 if (get) {
383 if (findPref(get) < 0) {
384 usage(prog);
385 }
386 do_getPref(get, argc, (char **)argv);
387 /* NOT REACHED */
388 }
389
390 /* are we looking up the proxy configuration */
391 if (proxy) {
392 do_showProxyConfiguration(argc, (char **)argv);
393 /* NOT REACHED */
394 }
395
396 /* are we changing a preference value */
397 if (set) {
398 if (findPref(set) < 0) {
399 usage(prog);
400 }
401 do_setPref(set, argc, (char **)argv);
402 /* NOT REACHED */
403 }
404
405 if (net) {
406 /* if we are going to be managing the network configuration */
407 commands = (cmdInfo *)commands_prefs;
408 nCommands = nCommands_prefs;
409
410 if (!getenv("ENABLE_EXPERIMENTAL_SCUTIL_COMMANDS")) {
411 usage(prog);
412 }
413
414 do_net_init(); /* initialization */
415 do_net_open(0, NULL); /* open default prefs */
416 } else {
417 /* if we are going to be managing the dynamic store */
418 commands = (cmdInfo *)commands_store;
419 nCommands = nCommands_store;
420
421 do_dictInit(0, NULL); /* start with an empty dictionary */
422 do_open(0, NULL); /* open the dynamic store */
423 }
424
425 /* allocate command input stream */
426 src = (InputRef)CFAllocatorAllocate(NULL, sizeof(Input), 0);
427 src->fp = stdin;
428 src->el = NULL;
429 src->h = NULL;
430
431 if (isatty(fileno(src->fp))) {
432 int editmode = 1;
433 HistEvent ev;
434 struct termios t;
435
436 if (tcgetattr(fileno(src->fp), &t) != -1) {
437 if ((t.c_lflag & ECHO) == 0) {
438 editmode = 0;
439 }
440 }
441 src->el = el_init(prog, src->fp, stdout, stderr);
442 src->h = history_init();
443
444 (void)history(src->h, &ev, H_SETSIZE, INT_MAX);
445 el_set(src->el, EL_HIST, history, src->h);
446
447 if (!editmode) {
448 el_set(src->el, EL_EDITMODE, 0);
449 }
450
451 el_set(src->el, EL_EDITOR, "emacs");
452 el_set(src->el, EL_PROMPT, prompt);
453
454 el_source(src->el, NULL);
455
456 if ((el_get(src->el, EL_EDITMODE, &editmode) != -1) && editmode != 0) {
457 el_set(src->el, EL_SIGNAL, 1);
458 } else {
459 history_end(src->h);
460 src->h = NULL;
461 el_end(src->el);
462 src->el = NULL;
463 }
464 }
465
466 while (process_line(src) == TRUE) {
467 /* debug information, diagnostics */
468 __showMachPortStatus();
469 }
470
471 /* close the socket, free resources */
472 if (src->h) history_end(src->h);
473 if (src->el) el_end(src->el);
474 (void)fclose(src->fp);
475 CFAllocatorDeallocate(NULL, src);
476
477 exit (EX_OK); // insure the process exit status is 0
478 return 0; // ...and make main fit the ANSI spec.
479 }