5 // Created by Claudio Bisegni on 25/04/08.
6 // Copyright 2008 Infn. All rights reserved.
9 #import "PListManager.h"
13 #define BACKGROUNDER_AGENT_NAME @"AFSBackgrounder.app/Contents/MacOS/AFSBackgrounder"
15 @implementation PListManager
16 // -------------------------------------------------------------------------------
17 // krb5TiketAtLoginTime:
18 // -------------------------------------------------------------------------------
19 +(void) krb5TiketAtLoginTime:(BOOL)enable{
20 NSData *plistData = nil;
21 NSString *error = nil;
22 NSString *toRemove = nil;
23 NSString *toAdd = nil;
24 NSPropertyListFormat format;
25 NSMutableDictionary *plist = nil;
28 FileUtil *futil = nil;
31 if (Gestalt(gestaltSystemVersionMajor, &osxMJVers) != noErr || Gestalt(gestaltSystemVersionMinor, &osxMnVers) != noErr) @throw [NSException exceptionWithName:@"PListManager:krb5TiketAtLoginTime"
32 reason:@"Error getting system version"
35 plistData = [NSData dataWithContentsOfFile:AUTH_FILE];
37 //Get plist for updating with NSPropertyListMutableContainersAndLeaves
38 plist = [NSPropertyListSerialization propertyListFromData:plistData
39 mutabilityOption:NSPropertyListMutableContainersAndLeaves
41 errorDescription:&error];
43 @throw [NSException exceptionWithName:@"PListManager:krb5TiketAtLoginTime"
50 NSMutableDictionary *rightsDic = [plist objectForKey:@"rights"];
52 //Get "system.login.console" dic
53 NSMutableDictionary *loginConsoleDic = [rightsDic objectForKey:@"system.login.console"];
55 //Get "mechanisms" dic
56 NSMutableArray *mechanismsArray = [loginConsoleDic objectForKey:@"mechanisms"];
61 toRemove = DELETE_IN_10_4;
64 toRemove = ADD_IN_PLIST;
65 toAdd = DELETE_IN_10_4;
72 toRemove = DELETE_IN_10_5;
75 toRemove = ADD_IN_PLIST;
76 toAdd = DELETE_IN_10_5;
83 [mechanismsArray removeObject:toRemove];
84 [mechanismsArray addObject:toAdd];
86 plistData = [NSPropertyListSerialization dataFromPropertyList:plist
87 format:NSPropertyListXMLFormat_v1_0
88 errorDescription:&error];
90 @throw [NSException exceptionWithName:@"PListManager:krb5TiketAtLoginTime"
95 if(![plistData writeToFile:TMP_FILE atomically:NO]) {
96 @throw [NSException exceptionWithName:@"PListManager:krb5TiketAtLoginTime"
97 reason:@"Temp file write error"
102 //now we can move the file
103 futil = [[FileUtil alloc] init];
104 if([futil startAutorization] == noErr) {
105 if(![[NSFileManager defaultManager] fileExistsAtPath:AUTH_FILE_BK]) {
106 //bk file doesn't exist so make it
107 [futil autorizedCopy:AUTH_FILE toPath:AUTH_FILE_BK];
110 [futil autorizedChown:TMP_FILE owner:@"root" group:@"wheel"];
112 [futil autorizedMoveFile:TMP_FILE toPath:AUTH_FILE_DIR];
117 // -------------------------------------------------------------------------------
118 // checkAklogAtLoginTimeLaunchdEnable:
119 // -------------------------------------------------------------------------------
120 +(BOOL) checkKrb5AtLoginTimeLaunchdEnable {
122 NSString *authFileContent = nil;
123 authFileContent = [NSString stringWithContentsOfFile:AUTH_FILE];
124 if(authFileContent) {
125 result = [authFileContent rangeOfString:ADD_IN_PLIST].location != NSNotFound;
131 // -------------------------------------------------------------------------------
132 // installLaunchdFile:
133 // -------------------------------------------------------------------------------
134 +(void) installBackgrounderLaunchdFile:(BOOL)install resourcePath:(NSString*) rsrcPath {
135 NSData *plistData = nil;
136 NSMutableDictionary *launchdDic = nil;
137 NSMutableDictionary *keepAliveDic = nil;
138 NSString *error = nil;
139 NSString *backgrounderPath = [[rsrcPath stringByAppendingString:@"/"] stringByAppendingString:BACKGROUNDER_AGENT_NAME];
142 if(![[NSFileManager defaultManager] fileExistsAtPath:[HOME_LAUNCHD_AGENT_FOLDER stringByExpandingTildeInPath]]) {
143 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
144 reason:@"The folder ~/Library/LaunchAgent doesn't exist!"
145 userInfo:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES]
146 forKey:@"agent_folder_error"]];
150 if(![[NSFileManager defaultManager] fileExistsAtPath:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath]]) {
151 launchdDic = [[[NSMutableDictionary alloc] init] autorelease];
152 keepAliveDic = [[[NSMutableDictionary alloc] init] autorelease];
154 [launchdDic setObject:@"it.infn.lnf.network.AFSBackgrounder"
157 [keepAliveDic setObject:[NSNumber numberWithBool:NO]
158 forKey:@"SuccessfulExit"];
160 [launchdDic setObject:keepAliveDic
161 forKey:@"KeepAlive"];
163 [launchdDic setObject:@"Aqua"
164 forKey:@"LimitLoadToSessionType"];
166 [launchdDic setObject:[NSArray arrayWithObject:backgrounderPath]
167 forKey:@"ProgramArguments"];
169 [launchdDic setObject:[NSNumber numberWithBool:YES]
170 forKey:@"RunAtLoad"];
172 plistData = [NSPropertyListSerialization dataFromPropertyList:launchdDic
173 format:NSPropertyListXMLFormat_v1_0
174 errorDescription:&error];
177 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
183 if(![plistData writeToFile:BACKGROUNDER_LAUNCHD_TMP_CONTROL_FILE atomically:NO]) {
184 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
185 reason:@"Temp file write error"
190 //now we can move the file
191 [TaskUtil executeTaskSearchingPath:@"mv" args:[NSArray arrayWithObjects:BACKGROUNDER_LAUNCHD_TMP_CONTROL_FILE, [BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath], nil]];
194 // delete launchd configuration file
195 [TaskUtil executeTaskSearchingPath:@"rm" args:[NSArray arrayWithObjects:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath], nil]];
200 // -------------------------------------------------------------------------------
201 // checkAklogAtLoginTimeLaunchdEnable:
202 // -------------------------------------------------------------------------------
203 +(BOOL) checkLoginTimeLaunchdBackgrounder {
204 BOOL result = [[NSFileManager defaultManager] fileExistsAtPath:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath]];
208 // -------------------------------------------------------------------------------
209 // installAfsStartupLaunchdFile:
210 // -------------------------------------------------------------------------------
211 +(void) manageAfsStartupLaunchdFile:(BOOL)enable
212 afsStartupScript:(NSString*)afsStartupScript
213 afsBasePath:(NSString*)afsBasePath
214 afsdPath:(NSString*)afsdPath {
215 NSData *plistData = nil;
216 NSMutableDictionary *launchdDic = nil;
217 NSString *error = nil;
218 OSErr status = noErr;
221 if(![[NSFileManager defaultManager] fileExistsAtPath:[LAUNCHD_DAEMON_FOLDER stringByExpandingTildeInPath]]) {
222 @throw [NSException exceptionWithName:@"PListManager:installAfsStartupLaunchdFile"
223 reason:@"The folder /Library/LaunchDaemon doesn't exist!"
227 status = [[AuthUtil shared] autorize];
228 if(status != noErr)@throw [NSException exceptionWithName:@"PListManager:installAfsStartupLaunchdFile"
229 reason:@"Autorization Error"
233 //Check first if the launchd configuration file for startup is present
234 if(![[NSFileManager defaultManager] fileExistsAtPath:[AFS_STARTUP_CONTROL_FILE stringByExpandingTildeInPath]]) {
235 launchdDic = [[[NSMutableDictionary alloc] init] autorelease];
236 //argDic = [[NSMutableArray alloc] init];
238 [launchdDic setObject:@"it.infn.lnf.afsstartup"
242 [launchdDic setObject:[NSArray arrayWithObjects:afsStartupScript, afsBasePath, afsdPath, nil]
243 forKey:@"ProgramArguments"];
245 [launchdDic setObject:[NSNumber numberWithBool:YES]
246 forKey:@"RunAtLoad"];
248 plistData = [NSPropertyListSerialization dataFromPropertyList:launchdDic
249 format:NSPropertyListXMLFormat_v1_0
250 errorDescription:&error];
253 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
259 if(![plistData writeToFile:AFS_STARTUP_TMP_CONTROL_FILE atomically:NO]) {
260 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
261 reason:@"Temp file write error"
266 //now we can move the file
267 [TaskUtil executeTaskSearchingPath:@"mv" args:[NSArray arrayWithObjects:AFS_STARTUP_TMP_CONTROL_FILE, [LAUNCHD_DAEMON_FOLDER stringByExpandingTildeInPath], nil]];
273 // -------------------------------------------------------------------------------
275 // -------------------------------------------------------------------------------
276 +(void) launchctlCommand:(BOOL)enable
277 userDomain:(BOOL)userDomain
278 option:(NSArray*)option
279 plistName:(NSString*)plistName {
280 NSMutableArray *argument = [NSMutableArray array];
281 NSMutableString *commandPath = [NSMutableString stringWithCapacity:0];
282 NSUInteger searchDomain = userDomain?NSUserDomainMask:NSSystemDomainMask;
284 NSArray *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, searchDomain, YES);
285 //set the load unload
286 [argument addObject:enable?@"load":@"unload"];
288 //if there are load the user custo option
289 if(option) [argument addObjectsFromArray:option];
292 [commandPath appendString:[libraryPath objectAtIndex:0]];
293 [commandPath appendFormat:@"/LaunchAgents/%@", plistName];
295 [argument addObject:commandPath];
297 [TaskUtil executeTaskSearchingPath:@"launchctl"
301 // -------------------------------------------------------------------------------
303 // -------------------------------------------------------------------------------
304 +(BOOL) launchdJobState:(NSString*)jobName {
305 NSMutableArray *argument = [NSMutableArray array];
307 //set the load unload
308 [argument addObject:@"list"];
309 [argument addObject:jobName];
311 NSString *taskResult =[TaskUtil executeTaskSearchingPath:@"launchctl"
313 return taskResult && [taskResult rangeOfString:jobName].location != NSNotFound;