/
NetworkLocationAction.m
171 lines (135 loc) · 4.09 KB
/
NetworkLocationAction.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//
// NetworkLocationAction.m
// ControlPlane
//
// Created by David Symonds on 4/07/07.
//
#import <SystemConfiguration/SCPreferences.h>
#import <SystemConfiguration/SCSchemaDefinitions.h>
#import "NetworkLocationAction.h"
@implementation NetworkLocationAction
#pragma mark Utility methods
+ (NSDictionary *) getAllSets
{
SCPreferencesRef prefs = SCPreferencesCreate(NULL, CFSTR("ControlPlane"), NULL);
SCPreferencesLock(prefs, true);
CFDictionaryRef cf_dict = (CFDictionaryRef) SCPreferencesGetValue(prefs, kSCPrefSets);
NSDictionary *dict = [NSDictionary dictionaryWithDictionary:(NSDictionary *) cf_dict];
// Clean up
SCPreferencesUnlock(prefs);
CFRelease(prefs);
return dict;
}
+ (NSString *)getCurrentSet
{
SCPreferencesRef prefs = SCPreferencesCreate(NULL, CFSTR("ControlPlane"), NULL);
SCPreferencesLock(prefs, true);
CFStringRef cf_str = (CFStringRef) SCPreferencesGetValue(prefs, kSCPrefCurrentSet);
NSMutableString *str = [NSMutableString stringWithString:(NSString *) cf_str];
[str replaceOccurrencesOfString:[NSString stringWithFormat:@"/%@/", kSCPrefSets]
withString:@""
options:0
range:NSMakeRange(0, [str length])];
// Clean up
SCPreferencesUnlock(prefs);
CFRelease(prefs);
return str;
}
#pragma mark -
- (id)init
{
if (!(self = [super init]))
return nil;
networkLocation = [[NSString alloc] init];
return self;
}
- (id)initWithDictionary:(NSDictionary *)dict
{
if (!(self = [super initWithDictionary:dict]))
return nil;
networkLocation = [[dict valueForKey:@"parameter"] copy];
return self;
}
- (void)dealloc
{
[networkLocation release];
[super dealloc];
}
- (NSMutableDictionary *)dictionary
{
NSMutableDictionary *dict = [super dictionary];
[dict setObject:[[networkLocation copy] autorelease] forKey:@"parameter"];
return dict;
}
- (NSString *)description
{
return [NSString stringWithFormat:NSLocalizedString(@"Changing network location to '%@'.", @""),
networkLocation];
}
- (BOOL)execute:(NSString **)errorString
{
// Using SCPreferences* to change the location requires a setuid binary,
// so we just execute /usr/sbin/scselect to do the heavy lifting.
NSDictionary *all_sets = [[self class] getAllSets];
NSEnumerator *en = [all_sets keyEnumerator];
NSString *key;
NSDictionary *subdict;
while ((key = [en nextObject])) {
subdict = [all_sets valueForKey:key];
if ([networkLocation isEqualToString:[subdict valueForKey:@"UserDefinedName"]])
break;
}
if (!key) {
*errorString = [NSString stringWithFormat:
NSLocalizedString(@"No network location named \"%@\" exists!", @"Action error message"),
networkLocation];
return NO;
}
NSArray *args = [NSArray arrayWithObject:key];
NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/sbin/scselect" arguments:args];
[task waitUntilExit];
if ([task terminationStatus] != 0) {
*errorString = NSLocalizedString(@"Failed changing network location", @"Action error message");
return NO;
}
return YES;
}
+ (NSString *)helpText
{
return NSLocalizedString(@"The parameter for NetworkLocation actions is the name of the "
"network location to select.", @"");
}
+ (NSString *)creationHelpText
{
return NSLocalizedString(@"Changing network location to", @"");
}
+ (NSArray *)limitedOptions
{
NSMutableArray *loc_list = [NSMutableArray array];
NSEnumerator *en = [[[self class] getAllSets] objectEnumerator];
NSDictionary *set;
while ((set = [en nextObject]))
[loc_list addObject:[set valueForKey:@"UserDefinedName"]];
[loc_list sortUsingSelector:@selector(localizedCompare:)];
NSMutableArray *opts = [NSMutableArray arrayWithCapacity:[loc_list count]];
en = [loc_list objectEnumerator];
NSString *loc;
while ((loc = [en nextObject]))
[opts addObject:[NSDictionary dictionaryWithObjectsAndKeys:
loc, @"option", loc, @"description", nil]];
return opts;
}
- (id)initWithOption:(NSString *)option
{
self = [super init];
[networkLocation autorelease];
networkLocation = [option copy];
return self;
}
+ (NSString *) friendlyName {
return NSLocalizedString(@"Network Location", @"");
}
+ (NSString *)menuCategory {
return NSLocalizedString(@"Networking", @"");
}
@end