5 // Created by Claudio Bisegni on
25/04/08.
6 // Copyright
2008 Infn.
All rights reserved.
9 #import "portability.h"
10 #import "PListManager.h"
14 #define BACKGROUNDER_AGENT_NAME @"AFSBackgrounder.app
/Contents
/MacOS
/AFSBackgrounder"
15 #define AD_CONF_FILE @"
/Library
/Preferences
/DirectoryService
/ActiveDirectory.plist"
16 @implementation PListManager
17 // -------------------------------------------------------------------------------
18 // krb5TiketAtLoginTime
:
19 // -------------------------------------------------------------------------------
20 +(void
) krb5TiketAtLoginTime
:(BOOL
)enable helper
:(NSString
*)helper
22 NSData
*plistData
= nil
;
23 NSString
*error = nil
;
24 NSString
*toRemove
= nil
;
25 NSString
*toAdd
= nil
;
26 NSPropertyListFormat
format;
27 NSMutableDictionary
*plist
= nil
;
30 FileUtil
*futil
= nil
;
31 SInt32 object_index
= 0;
34 if (Gestalt
(gestaltSystemVersionMajor
, &osxMJVers
) != noErr || Gestalt
(gestaltSystemVersionMinor
, &osxMnVers
) != noErr
) @throw
[NSException exceptionWithName
:@"PListManager
:krb5TiketAtLoginTime" reason
:@"
Error getting system
version" userInfo
:nil
];
36 // are we eligible to run?
37 plistData
= [NSData dataWithContentsOfFile
:AD_CONF_FILE
];
39 // Get plist
for updating with NSPropertyListMutableContainersAndLeaves
40 plist
= [NSPropertyListSerialization propertyListFromData
:plistData mutabilityOption
:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription
:&error];
43 // Get "AD Advanced Options" dic
44 NSMutableDictionary
*rightsDic
= [plist objectForKey
:@"AD Advanced Options"
];
45 if ([[rightsDic objectForKey
:@"AD Generate AuthAuthority"
] boolValue
])
49 // get auth plist file
50 plistData
= [NSData dataWithContentsOfFile
:AUTH_FILE
];
52 // Get plist
for updating with NSPropertyListMutableContainersAndLeaves
53 plist
= [NSPropertyListSerialization propertyListFromData
:plistData mutabilityOption
:NSPropertyListMutableContainersAndLeaves
format:&format errorDescription
:&error];
56 @throw
[NSException exceptionWithName
:@"PListManager
:krb5TiketAtLoginTime" reason
:error userInfo
:nil
];
60 NSMutableDictionary
*rightsDic
= [plist objectForKey
:@"rights"
];
62 //Get "system.login.console" dic
63 NSMutableDictionary
*loginConsoleDic
= [rightsDic objectForKey
:@"system.login.console"
];
65 //Get "mechanisms" dic
66 NSMutableArray
*mechanismsArray
= [loginConsoleDic objectForKey
:@"mechanisms"
];
71 toRemove
= DELETE_IN_10_4
;
74 toRemove
= ADD_IN_PLIST
;
75 toAdd
= DELETE_IN_10_4
;
83 toRemove
= DELETE_IN_10_5
;
86 toRemove
= ADD_IN_PLIST
;
87 toAdd
= DELETE_IN_10_5
;
94 object_index
= [mechanismsArray indexOfObject
: toRemove
];
95 [mechanismsArray replaceObjectAtIndex
:object_index withObject
:toAdd
];
98 plistData
= [NSPropertyListSerialization dataFromPropertyList
:plist
99 format:NSPropertyListXMLFormat_v1_0
100 errorDescription
:&error];
102 @throw
[NSException exceptionWithName
:@"PListManager
:krb5TiketAtLoginTime"
107 if(![plistData writeToFile
:TMP_FILE atomically
:NO
]) {
108 @throw
[NSException exceptionWithName
:@"PListManager
:krb5TiketAtLoginTime"
109 reason
:@"Temp file write
error"
114 //now we can move the file
115 futil
= [[FileUtil alloc
] init
];
116 if(![[NSFileManager defaultManager
] fileExistsAtPath
:AUTH_FILE_BK
]) {
117 //bk file doesn
't exist so make it
118 [futil autorizedCopy:AUTH_FILE toPath:AUTH_FILE_BK];
121 [futil autorizedChown:TMP_FILE owner:@"root" group:@"wheel"];
123 [futil autorizedMoveFile:TMP_FILE toPath:AUTH_FILE_DIR];
128 // -------------------------------------------------------------------------------
129 // checkAklogAtLoginTimeLaunchdEnable:
130 // -------------------------------------------------------------------------------
131 +(BOOL) checkKrb5AtLoginTimeLaunchdEnable {
133 NSString *authFileContent = nil;
134 authFileContent = [NSString stringWithContentsOfFile:AUTH_FILE
135 encoding:NSUTF8StringEncoding
137 if(authFileContent) {
138 result = [authFileContent rangeOfString:ADD_IN_PLIST].location != NSNotFound;
144 // -------------------------------------------------------------------------------
145 // installLaunchdFile:
146 // -------------------------------------------------------------------------------
147 +(void) installBackgrounderLaunchdFile:(BOOL)install resourcePath:(NSString*) rsrcPath {
148 NSData *plistData = nil;
149 NSMutableDictionary *launchdDic = nil;
150 NSMutableDictionary *keepAliveDic = nil;
151 NSString *error = nil;
152 NSString *backgrounderPath = [[rsrcPath stringByAppendingString:@"/"] stringByAppendingString:BACKGROUNDER_AGENT_NAME];
155 if(![[NSFileManager defaultManager] fileExistsAtPath:[HOME_LAUNCHD_AGENT_FOLDER stringByExpandingTildeInPath]]) {
156 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
157 reason:@"The folder ~/Library/LaunchAgent doesn't
exist!"
158 userInfo
:[NSDictionary dictionaryWithObject
:[NSNumber numberWithBool
:YES
]
159 forKey
:@"agent_folder_error"
]];
163 if(![[NSFileManager defaultManager
] fileExistsAtPath
:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath
]]) {
164 launchdDic
= [[[NSMutableDictionary alloc
] init
] autorelease
];
165 keepAliveDic
= [[[NSMutableDictionary alloc
] init
] autorelease
];
167 [launchdDic setObject
:@"it.infn.lnf.network.AFSBackgrounder"
170 [keepAliveDic setObject
:[NSNumber numberWithBool
:NO
]
171 forKey
:@"SuccessfulExit"
];
173 [launchdDic setObject
:keepAliveDic
174 forKey
:@"KeepAlive"
];
176 [launchdDic setObject
:@"Aqua"
177 forKey
:@"LimitLoadToSessionType"
];
179 /*[launchdDic setObject:[NSArray arrayWithObject:backgrounderPath]
180 forKey:@"ProgramArguments"];*/
181 [launchdDic setObject
:backgrounderPath
183 [launchdDic setObject
:[NSNumber numberWithBool
:YES
]
184 forKey
:@"RunAtLoad"
];
186 plistData
= [NSPropertyListSerialization dataFromPropertyList
:launchdDic
187 format:NSPropertyListXMLFormat_v1_0
188 errorDescription
:&error];
191 @throw
[NSException exceptionWithName
:@"PListManager
:installLaunchdFile"
197 if(![plistData writeToFile
:BACKGROUNDER_LAUNCHD_TMP_CONTROL_FILE atomically
:NO
]) {
198 @throw
[NSException exceptionWithName
:@"PListManager
:installLaunchdFile"
199 reason
:@"Temp file write
error"
204 //now we can move the file
205 [TaskUtil executeTaskSearchingPath
:@"mv" args
:[NSArray arrayWithObjects
:BACKGROUNDER_LAUNCHD_TMP_CONTROL_FILE
, [BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath
], nil
]];
208 // delete launchd configuration file
209 [TaskUtil executeTaskSearchingPath
:@"rm" args
:[NSArray arrayWithObjects
:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath
], nil
]];
214 // -------------------------------------------------------------------------------
215 // checkAklogAtLoginTimeLaunchdEnable
:
216 // -------------------------------------------------------------------------------
217 +(BOOL
) checkLoginTimeLaunchdBackgrounder
{
218 BOOL result
= [[NSFileManager defaultManager
] fileExistsAtPath
:[BACKGROUNDER_LAUNCHD_CONTROL_FILE stringByExpandingTildeInPath
]];
222 // -------------------------------------------------------------------------------
223 // installAfsStartupLaunchdFile
:
224 // -------------------------------------------------------------------------------
225 +(void
) manageAfsStartupLaunchdFile
:(BOOL
)enable
226 afsStartupScript
:(NSString
*)afsStartupScript
227 afsBasePath
:(NSString
*)afsBasePath
228 afsdPath
:(NSString
*)afsdPath
{
229 NSData
*plistData
= nil
;
230 NSMutableDictionary
*launchdDic
= nil
;
231 NSString
*error = nil
;
232 OSErr status
= noErr
;
235 if(![[NSFileManager defaultManager
] fileExistsAtPath
:[LAUNCHD_DAEMON_FOLDER stringByExpandingTildeInPath
]]) {
236 @throw
[NSException exceptionWithName
:@"PListManager
:installAfsStartupLaunchdFile"
237 reason
:@"The folder
/Library
/LaunchDaemon doesn
't exist!"
241 status = [[AuthUtil shared] autorize];
242 if(status != noErr)@throw [NSException exceptionWithName:@"PListManager:installAfsStartupLaunchdFile"
243 reason:@"Autorization Error"
247 //Check first if the launchd configuration file for startup is present
248 if(![[NSFileManager defaultManager] fileExistsAtPath:[AFS_STARTUP_CONTROL_FILE stringByExpandingTildeInPath]]) {
249 launchdDic = [[[NSMutableDictionary alloc] init] autorelease];
250 //argDic = [[NSMutableArray alloc] init];
252 [launchdDic setObject:@"it.infn.lnf.afsstartup"
256 [launchdDic setObject:[NSArray arrayWithObjects:afsStartupScript, afsBasePath, afsdPath, nil]
257 forKey:@"ProgramArguments"];
259 [launchdDic setObject:[NSNumber numberWithBool:YES]
260 forKey:@"RunAtLoad"];
262 plistData = [NSPropertyListSerialization dataFromPropertyList:launchdDic
263 format:NSPropertyListXMLFormat_v1_0
264 errorDescription:&error];
267 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
273 if(![plistData writeToFile:AFS_STARTUP_TMP_CONTROL_FILE atomically:NO]) {
274 @throw [NSException exceptionWithName:@"PListManager:installLaunchdFile"
275 reason:@"Temp file write error"
280 //now we can move the file
281 [TaskUtil executeTaskSearchingPath:@"mv" args:[NSArray arrayWithObjects:AFS_STARTUP_TMP_CONTROL_FILE, [LAUNCHD_DAEMON_FOLDER stringByExpandingTildeInPath], nil]];
287 // -------------------------------------------------------------------------------
289 // -------------------------------------------------------------------------------
290 +(void) launchctlCommand:(BOOL)enable
291 userDomain:(BOOL)userDomain
292 option:(NSArray*)option
293 plistName:(NSString*)plistName
295 NSMutableArray *argument = [NSMutableArray array];
296 NSMutableString *commandPath = [NSMutableString stringWithCapacity:0];
297 NSUInteger searchDomain = userDomain?NSUserDomainMask:NSSystemDomainMask;
299 NSArray *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, searchDomain, YES);
300 //set the load unload
301 [argument addObject:enable?@"load":@"unload"];
303 //if there are load the user custo option
304 if(option) [argument addObjectsFromArray:option];
307 [commandPath appendString:[libraryPath objectAtIndex:0]];
308 [commandPath appendFormat:@"/LaunchAgents/%@", plistName];
310 [argument addObject:commandPath];
313 [TaskUtil executeTask:@"/bin/launchctl"
317 // -------------------------------------------------------------------------------
319 // -------------------------------------------------------------------------------
320 +(void) launchctlStringCommandAuth:(NSString *)operation
321 option:(NSArray *)option
322 plistName:(NSString *)plistName
323 helper:(NSString *)helper
324 withAuthRef:(AuthorizationRef)authRef
326 NSMutableArray *argument = [NSMutableArray array];
328 //set the load unload
329 [argument addObject:operation];
331 //if there are load the user custom option
332 if(option) [argument addObjectsFromArray:option];
335 [argument addObject: plistName];
338 [TaskUtil executeTaskWithAuth:@"/bin/launchctl"
339 arguments:argument helper:helper withAuthRef:authRef];
342 // -------------------------------------------------------------------------------
344 // -------------------------------------------------------------------------------
345 +(BOOL) launchdJobState:(NSString*)jobName {
346 NSMutableArray *argument = [NSMutableArray array];
348 //set the load unload
349 [argument addObject:@"list"];
350 [argument addObject:jobName];
352 NSString *taskResult =[TaskUtil executeTaskSearchingPath:@"launchctl"
354 return taskResult && [taskResult rangeOfString:jobName].location != NSNotFound;