#include <getopt.h>
#include <unistd.h>
#include <sysexits.h>
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dlfcn.h>
#include <grp.h>
#include <SystemConfiguration/SystemConfiguration.h>
#include <SystemConfiguration/SCPrivate.h>
#if !TARGET_OS_IPHONE
#include <Security/Authorization.h>
#endif
static Boolean apply = TRUE;
static const struct option longopts[] = {
{ "help", no_argument, 0, '?' },
{ 0, 0, 0, 0 }
};
static void
usage(const char *command)
{
SCPrint(TRUE, stderr, CFSTR("usage: %s [-n] new-set-name\n"), command);
exit (EX_USAGE);
}
int
main(int argc, char **argv)
{
const char *command = argv[0];
extern int optind;
int opt;
CFStringRef current = NULL;
int currentMatched = 0;
CFStringRef newSet = NULL;
CFStringRef newSetUDN = NULL;
CFStringRef prefix;
SCPreferencesRef prefs;
CFDictionaryRef sets;
CFIndex nSets;
const void **setKeys = NULL;
const void **setVals = NULL;
CFIndex i;
#if !TARGET_OS_IPHONE
AuthorizationRef authorization = NULL;
AuthorizationFlags flags = kAuthorizationFlagDefaults;
CFMutableDictionaryRef options;
OSStatus status;
#endif // !TARGET_OS_IPHONE
while ((opt = getopt_long(argc, argv, "dvn", longopts, NULL)) != -1) {
switch(opt) {
case 'd':
_sc_debug = TRUE;
_sc_log = FALSE;
break;
case 'v':
_sc_verbose = TRUE;
break;
case 'n':
apply = FALSE;
break;
case '?':
default :
usage(command);
}
}
argc -= optind;
argv += optind;
prefix = CFStringCreateWithFormat(NULL, NULL, CFSTR("/%@/"), kSCPrefSets);
if (argc == 1) {
newSet = CFStringCreateWithCString(NULL, argv[0], kCFStringEncodingMacRoman);
if ((CFStringGetLength(newSet) > 0) && CFStringHasPrefix(newSet, prefix)) {
CFRange range;
CFMutableStringRef str;
str = CFStringCreateMutableCopy(NULL, 0, newSet);
CFRelease(newSet);
CFStringDelete(str, CFRangeMake(0, CFStringGetLength(prefix)));
newSet = CFStringCreateCopy(NULL, str);
CFRelease(str);
range = CFStringFind(newSet, CFSTR("/"), 0);
if (range.location != kCFNotFound) {
SCPrint(TRUE, stderr, CFSTR("Set \"%@\" not available\n"), newSet);
exit (1);
}
}
} else {
newSet = CFRetain(CFSTR(""));
}
#if !TARGET_OS_IPHONE
status = AuthorizationCreate(NULL,
kAuthorizationEmptyEnvironment,
flags,
&authorization);
if (status != errAuthorizationSuccess) {
SCPrint(TRUE,
stderr,
CFSTR("AuthorizationCreate() failed: status = %d\n"),
(int)status);
exit (1);
}
options = CFDictionaryCreateMutable(NULL,
0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(options, kSCPreferencesOptionChangeNetworkSet, kCFBooleanTrue);
prefs = SCPreferencesCreateWithOptions(NULL, CFSTR("scselect"), NULL, authorization, options);
CFRelease(options);
if (prefs == NULL) {
SCPrint(TRUE, stderr, CFSTR("SCPreferencesCreate() failed\n"));
exit (1);
}
#else // !TARGET_OS_IPHONE
prefs = SCPreferencesCreate(NULL, CFSTR("scselect"), NULL);
if (prefs == NULL) {
SCPrint(TRUE, stderr, CFSTR("SCPreferencesCreate() failed\n"));
exit (1);
}
#endif // !TARGET_OS_IPHONE
sets = SCPreferencesGetValue(prefs, kSCPrefSets);
if (sets == NULL) {
SCPrint(TRUE, stderr, CFSTR("No network sets defined.\n"));
exit (1);
}
current = SCPreferencesGetValue(prefs, kSCPrefCurrentSet);
if (current != NULL) {
if (CFStringHasPrefix(current, prefix)) {
CFMutableStringRef tmp;
tmp = CFStringCreateMutableCopy(NULL, 0, current);
CFStringDelete(tmp, CFRangeMake(0, CFStringGetLength(prefix)));
current = tmp;
} else {
CFRetain(current);
currentMatched = -1;
}
} else {
current = CFRetain(CFSTR(""));
currentMatched = -2;
}
nSets = CFDictionaryGetCount(sets);
if (nSets > 0) {
setKeys = CFAllocatorAllocate(NULL, nSets * sizeof(CFStringRef), 0);
setVals = CFAllocatorAllocate(NULL, nSets * sizeof(CFDictionaryRef), 0);
CFDictionaryGetKeysAndValues(sets, setKeys, setVals);
}
for (i = 0; i < nSets; i++) {
CFStringRef key = (CFStringRef) setKeys[i];
CFDictionaryRef dict = (CFDictionaryRef)setVals[i];
if ((currentMatched >= 0) && CFEqual(key, current)) {
currentMatched++;
}
if (CFEqual(newSet, key)) {
newSetUDN = CFDictionaryGetValue(dict, kSCPropUserDefinedName);
if (newSetUDN != NULL) CFRetain(newSetUDN);
goto found;
}
}
for (i = 0; i < nSets; i++) {
CFStringRef key = (CFStringRef) setKeys[i];
CFDictionaryRef dict = (CFDictionaryRef)setVals[i];
newSetUDN = CFDictionaryGetValue(dict, kSCPropUserDefinedName);
if ((newSetUDN != NULL) && CFEqual(newSet, newSetUDN)) {
CFRelease(newSet);
newSet = CFRetain(key);
CFRetain(newSetUDN);
goto found;
}
}
if (argc == 1) {
SCPrint(TRUE, stderr, CFSTR("Set \"%@\" not available.\n"), newSet);
exit(1);
}
SCPrint(TRUE, stdout,
CFSTR("Defined sets include:%s\n"),
(currentMatched > 0) ? " (* == current set)" : "");
for (i = 0; i < nSets; i++) {
CFStringRef key = (CFStringRef) setKeys[i];
CFDictionaryRef dict = (CFDictionaryRef)setVals[i];
CFStringRef udn = CFDictionaryGetValue(dict, kSCPropUserDefinedName);
SCPrint(TRUE, stdout,
CFSTR(" %s %@\t(%@)\n"),
((currentMatched > 0) && CFEqual(key, current)) ? "*" : " ",
key,
udn ? udn : CFSTR(""));
}
switch (currentMatched) {
case -2 :
SCPrint(TRUE, stdout, CFSTR("\nCurrent set not defined.\n"));
break;
case -1 :
SCPrint(TRUE, stdout, CFSTR("\nCurrent set \"%@\" may not be valid\n"), current);
break;
case 0 :
SCPrint(TRUE, stdout, CFSTR("\nCurrent set \"%@\" not valid\n"), current);
break;
default :
break;
}
CFRelease(prefix);
exit (0);
found :
CFRelease(current);
current = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@%@"), prefix, newSet);
SCPreferencesSetValue(prefs, kSCPrefCurrentSet, current);
if (!SCPreferencesCommitChanges(prefs)) {
int sc_status = SCError();
if (sc_status == kSCStatusAccessError) {
SCPrint(TRUE, stderr,
CFSTR("Only local console users and administrators can change locations\n"));
exit (EX_NOPERM);
} else {
SCPrint(TRUE, stderr,
CFSTR("SCPreferencesCommitChanges() failed: %s\n"),
SCErrorString(sc_status));
exit (1);
}
}
if (apply) {
if (!SCPreferencesApplyChanges(prefs)) {
SCPrint(TRUE, stderr,
CFSTR("SCPreferencesApplyChanges() failed %s\n"),
SCErrorString(SCError()));
exit (1);
}
}
SCPrint(TRUE, stdout,
CFSTR("%@ updated to %@ (%@)\n"),
kSCPrefCurrentSet,
newSet,
newSetUDN ? newSetUDN : CFSTR(""));
CFRelease(current);
CFRelease(newSet);
if (newSetUDN != NULL) CFRelease(newSetUDN);
CFRelease(prefix);
CFRelease(prefs);
#if !TARGET_OS_IPHONE
AuthorizationFree(authorization, kAuthorizationFlagDefaults);
#endif
exit (0);
return 0;
}