5 // Created by Claudio Bisegni on 29/07/09.
6 // Copyright 2009 Infn. All rights reserved.
9 #import "AFSBackgrounderDelegate.h"
10 #import "AFSMenuExtraView.h"
11 #import "AFSPropertyManager.h"
13 #import "TokenCredentialController.h"
14 #include <sys/param.h>
17 #include <sys/types.h>
18 #include <sys/fcntl.h>
19 #include <sys/errno.h>
26 #define LINK_ICON 'srvr'
28 @implementation AFSBackgrounderDelegate
29 #pragma mark NSApp Delegate
30 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
32 linkCreationLock = [[NSLock alloc] init];
34 afsMngr = [[AFSPropertyManager alloc] initWithAfsPath:PREFERENCE_AFS_SYS_PAT_STATIC];
36 // allocate the lock for concurent afs check state
37 tokensLock = [[NSLock alloc] init];
38 renewTicketLock = [[NSLock alloc] init];
40 //remove the auto eanble on menu item
41 [backgrounderMenu setAutoenablesItems:NO];
42 //Sets the images in our NSStatusItem
46 // Get the imge for menu
47 //Load image for menu rappresentation
48 hasTokenImage = [self getImageFromBundle:@"hasToken"
51 noTokenImage = [self getImageFromBundle:@"noToken"
53 //get the size of the menu icon
54 menuSize = [hasTokenImage size];
56 //inizialize the local link mode status
57 currentLinkActivationStatus = NO;
59 //Start to read the afs path
60 [self readPreferenceFile:nil];
65 // Register for preference user change
66 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
67 selector:@selector(readPreferenceFile:)
68 name:kAFSMenuExtraID object:kPrefChangeNotification];
70 // Register for afs state change
71 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
72 selector:@selector(afsVolumeMountChange:)
73 name:kAFSMenuExtraID object:kMExtraAFSStateChange];
75 // Register for menu state change
76 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
77 selector:@selector(chageMenuVisibility:)
78 name:kAFSMenuExtraID object:kMExtraAFSMenuChangeState];
80 //Register for mount/unmount afs volume
81 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
82 selector:@selector(afsVolumeMountChange:)
83 name:NSWorkspaceDidMountNotification object:nil];
85 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
86 selector:@selector(afsVolumeMountChange:)
87 name:NSWorkspaceDidUnmountNotification object:nil];
89 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
90 selector:@selector(switchHandler:)
91 name:NSWorkspaceSessionDidBecomeActiveNotification
94 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
95 selector:@selector(switchHandler:)
96 name:NSWorkspaceSessionDidResignActiveNotification
99 //try to see if we need tho show the menu at startup
101 [self setStatusItem:[showStatusMenu boolValue]];
103 NSLog(@"Check if we need to get token at login time %d", [aklogTokenAtLogin intValue]);
104 if([aklogTokenAtLogin boolValue] && afsState && !gotToken) {
105 NSLog(@"Proceed to get token");
106 //check if we must get the aklog at logint(first run of backgrounder
111 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
112 if(afsSysPath) [afsSysPath release];
116 [self stopTimerRenewTicket];
118 if(hasTokenImage) [hasTokenImage release];
119 if(noTokenImage) [noTokenImage release];
121 // Unregister for preference change
122 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:NSWorkspaceSessionDidBecomeActiveNotification object:nil];
123 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:NSWorkspaceSessionDidResignActiveNotification object:nil];
124 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kPrefChangeNotification];
125 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kMExtraAFSStateChange];
126 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kMExtraAFSMenuChangeState];
127 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceDidMountNotification object:nil];
128 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceDidUnmountNotification object:nil];
131 // send notify that menuextra has closed
132 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kPrefChangeNotification];
134 if(tokensLock) [tokensLock release];
135 if(afsMngr) [afsMngr release];
136 if(linkCreationLock) [linkCreationLock release];
137 return NSTerminateNow;
139 #pragma mark Notification Handler
140 // -------------------------------------------------------------------------------
141 // -(void) readPreferenceFile
142 // -------------------------------------------------------------------------------
143 - (void) readPreferenceFile:(NSNotification *)notification
146 [afsSysPath release];
149 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
150 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
152 afsSysPath = PREFERENCE_AFS_SYS_PAT_STATIC;
154 // read the preference for aklog use
155 useAklogPrefValue = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_USE_AKLOG,
156 (CFStringRef)kAfsCommanderID,
157 kCFPreferencesCurrentUser,
158 kCFPreferencesAnyHost);
160 showStatusMenu = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_SHOW_STATUS_MENU,
161 (CFStringRef)kAfsCommanderID,
162 kCFPreferencesCurrentUser,
163 kCFPreferencesAnyHost);
165 aklogTokenAtLogin = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_AKLOG_TOKEN_AT_LOGIN, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
167 //get link configuration
168 NSData *prefData = (NSData*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_LINK_CONFIGURATION, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
169 linkConfiguration = (NSMutableDictionary*)[NSPropertyListSerialization propertyListFromData:prefData
170 mutabilityOption:NSPropertyListMutableContainers
172 errorDescription:nil];
174 //get link enabled status
175 NSNumber *linkEnabledStatus = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_USE_LINK, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
176 [self updateLinkModeStatusWithpreferenceStatus:[linkEnabledStatus boolValue]];
178 //check the user preference for manage the renew
179 krb5CheckRenew = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_KRB5_CHECK_ENABLE, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
180 krb5RenewTime = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_KRB5_RENEW_TIME, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
181 krb5RenewCheckTimeInterval = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_KRB5_RENEW_CHECK_TIME_INTERVALL, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
182 krb5SecToExpireTimeForRenew = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_KRB5_SEC_TO_EXPIRE_TIME_FOR_RENEW, (CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
186 [self updateAfsStatus:nil];
188 //stop and start the timer for krb5 renew
189 [self stopTimerRenewTicket];
190 [self startTimerRenewTicket];
193 // -------------------------------------------------------------------------------
194 // - (void) updateLinkModeStatusWithpreferenceStatus:(BOOL)status
195 // -------------------------------------------------------------------------------
196 - (void) updateLinkModeStatusWithpreferenceStatus:(BOOL)status {
197 //exec the link operation on thread
198 [NSThread detachNewThreadSelector:@selector(performLinkOpeartionOnThread:)
200 withObject:[NSNumber numberWithBool:status]];
204 // -------------------------------------------------------------------------------
205 // - (void) performLinkOpenartionOnThread:(id)object
206 // -------------------------------------------------------------------------------
207 - (void) performLinkOpeartionOnThread:(id)object {
208 [linkCreationLock lock];
209 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
210 NSError *error = nil;
212 NSString *linkDstPath = nil;
213 NSString *linkSrcPath = nil;
214 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDesktopDirectory, NSUserDomainMask, YES);
215 NSString *documentFolderPath = [paths objectAtIndex:0];
216 NSNumber *number = (NSNumber*)object;
217 //NSString *fType = NSFileTypeForHFSTypeCode(LINK_ICON);
218 //NSImage *picture = [[NSWorkspace sharedWorkspace] iconForFileType:fType];
220 BOOL linkSourcePathExist = NO;
221 BOOL linkDestinationPathExist = NO;
223 NSLog(@"updateLinkModeStatusWithpreferenceStatus %d", [number boolValue]);
224 NSEnumerator *keys = [linkConfiguration keyEnumerator];
225 while ((key = [keys nextObject])) {
227 linkSrcPath = [documentFolderPath stringByAppendingPathComponent:key];
228 //afs destionation path
229 linkDstPath = [linkConfiguration objectForKey:key];
230 linkSourcePathExist = [[NSFileManager defaultManager] fileExistsAtPath:linkSrcPath];
231 linkDestinationPathExist = [[NSFileManager defaultManager] fileExistsAtPath:linkDstPath];
233 if([number boolValue]) {
234 if(!linkSourcePathExist) {
235 if(linkDestinationPathExist) {
236 NSLog(@"Creating link \"%@\" to point to \"%@\"", linkSrcPath, linkDstPath);
237 [[NSFileManager defaultManager] createSymbolicLinkAtPath:linkSrcPath
238 withDestinationPath:linkDstPath
241 //Link has been created so i can chnge the icon
242 /* [[NSWorkspace sharedWorkspace] setIcon:picture
245 NSLog(@"Link \"%@\" created", linkSrcPath);
247 NSLog(@"Link Creation Error: %@", [error localizedDescription]);
250 NSLog(@"Deleting Link: %@", linkSrcPath);
251 [[NSFileManager defaultManager] removeItemAtPath:linkSrcPath
255 //the lynk already exist check if the dest path is accesible
256 if(!linkSourcePathExist) {
257 NSLog(@"Deleting Link: %@", linkSrcPath);
258 [[NSFileManager defaultManager] removeItemAtPath:linkSrcPath
264 NSLog(@"Deleting Link: %@", linkSrcPath);
265 [[NSFileManager defaultManager] removeItemAtPath:linkSrcPath
272 currentLinkActivationStatus = [number boolValue];
273 //release thread resource
275 [linkCreationLock unlock];
278 // -------------------------------------------------------------------------------
279 // - (void)chageMenuVisibility:(NSNotification *)notification
280 // -------------------------------------------------------------------------------
281 - (void)chageMenuVisibility:(NSNotification *)notification {
282 [self readPreferenceFile:nil];
283 [self setStatusItem:[showStatusMenu boolValue]];
286 // -------------------------------------------------------------------------------
287 // - (void) switchHandler:(NSNotification*) notification
288 // -------------------------------------------------------------------------------
289 - (void) switchHandler:(NSNotification*) notification
291 if ([[notification name] isEqualToString:NSWorkspaceSessionDidResignActiveNotification])
293 // user has switched out
297 // user has switched in
298 NSLog(@"User has switch in again");
299 if([aklogTokenAtLogin boolValue] && afsState && !gotToken) {
300 NSLog(@"Proceed to get token");
301 //check if we must get the aklog at logint(first run of backgrounder
306 // -------------------------------------------------------------------------------
307 // -(void) afsVolumeMountChange - Track for mount unmount afs volume
308 // -------------------------------------------------------------------------------
309 - (void) afsVolumeMountChange:(NSNotification *)notification{
310 [self updateAfsStatus:nil];
311 [self readPreferenceFile:nil];
315 // -------------------------------------------------------------------------------
316 // -(void) klogUserEven
317 // -------------------------------------------------------------------------------
318 -(void) klogUserEven:(NSNotification *)notification
320 if(credentialMenuController) {
321 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kLogWindowClosed];
322 [credentialMenuController closeWindow];
323 [credentialMenuController release];
324 credentialMenuController = nil;
326 //Send notification to PreferencePane
327 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kMenuExtraEventOccured];
329 [self updateAfsStatus:nil];
332 // -------------------------------------------------------------------------------
333 // - (void)startStopAfs:(id)sender
334 // -------------------------------------------------------------------------------
335 - (void)startStopAfs:(id)sender
338 BOOL currentAfsState = NO;
339 OSErr status = [[AuthUtil shared] autorize];
341 currentAfsState = [afsMngr checkAfsStatus];
342 // make the parameter to call the root helper app
345 NSLog(@"Shutting down afs");
349 NSLog(@"Starting up afs");
353 }@catch (NSException * e) {
354 NSLog(@"error %@", [e reason]);
356 [self updateAfsStatus:nil];
357 [[AuthUtil shared] deautorize];
358 //Send notification to preferencepane
359 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kMenuExtraEventOccured];
364 // -------------------------------------------------------------------------------
366 // -------------------------------------------------------------------------------
367 - (void)getToken:(id)sender
370 if([useAklogPrefValue boolValue]) {
371 [afsMngr getTokens:false
374 [self klogUserEven:nil];
376 globalRect.origin = [[[statusItem view] window] convertBaseToScreen:[[statusItem view] frame].origin];
377 globalRect.size = [[statusItem view] frame].size;
379 // register for user event
380 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
381 selector:@selector(klogUserEven:)
383 object:kLogWindowClosed];
385 credentialMenuController = [[AFSMenuCredentialContoller alloc] initWhitRec:globalRect
386 afsPropManager:afsMngr];
387 [credentialMenuController showWindow];
389 //Dispose afs manager
390 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID
391 object:kMExtraTokenOperation];
394 // -------------------------------------------------------------------------------
395 // -(void) releaseToken
396 // -------------------------------------------------------------------------------
397 - (void)releaseToken:(id)sender
400 [self updateAfsStatus:nil];
401 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID
402 object:kMExtraTokenOperation];
406 // -------------------------------------------------------------------------------
407 // -(void) updateAfsStatus
408 // -------------------------------------------------------------------------------
409 - (void)updateAfsStatus:(NSTimer*)timer
412 if(![tokensLock tryLock]) return;
414 //reload configuration
415 [afsMngr loadConfiguration];
417 // check the afs state in esclusive mode
418 afsState = [afsMngr checkAfsStatus];
420 NSArray *tokens = [afsMngr getTokenList];
421 gotToken = [tokens count] > 0;
424 //update the menu icon
425 [[statusItem view] setNeedsDisplay:YES];
430 // -------------------------------------------------------------------------------
432 // -------------------------------------------------------------------------------
434 //start the time for check tokens validity
435 if(timerForCheckTokensList) return;
436 timerForCheckTokensList = [NSTimer scheduledTimerWithTimeInterval:TOKENS_REFRESH_TIME_IN_SEC
438 selector:@selector(updateAfsStatus:)
441 [timerForCheckTokensList fire];
444 // -------------------------------------------------------------------------------
446 // -------------------------------------------------------------------------------
448 if(!timerForCheckTokensList) return;
449 [timerForCheckTokensList invalidate];
450 timerForCheckTokensList = nil;
453 // -------------------------------------------------------------------------------
454 // startTimerRenewTicket:
455 // -------------------------------------------------------------------------------
456 - (void)startTimerRenewTicket {
457 //start the time for check ticket renew
458 if(timerForCheckRenewTicket || !krb5RenewCheckTimeInterval || ![krb5RenewCheckTimeInterval intValue]) return;
459 NSLog(@"startTimerRenewTicket with sec %d", [krb5RenewCheckTimeInterval intValue]);
460 timerForCheckRenewTicket = [NSTimer scheduledTimerWithTimeInterval:(krb5RenewCheckTimeInterval?[krb5RenewCheckTimeInterval intValue]:PREFERENCE_KRB5_RENEW_CHECK_TIME_INTERVALL_DEFAULT_VALUE)
462 selector:@selector(krb5RenewAction:)
465 [timerForCheckRenewTicket fire];
468 // -------------------------------------------------------------------------------
469 // stopTimerRenewTicket:
470 // -------------------------------------------------------------------------------
471 - (void)stopTimerRenewTicket {
472 NSLog(@"stopTimerRenewTicket");
473 if(!timerForCheckRenewTicket) return;
474 [timerForCheckRenewTicket invalidate];
475 timerForCheckRenewTicket = nil;
478 // -------------------------------------------------------------------------------
480 // -------------------------------------------------------------------------------
481 - (void)krb5RenewAction:(NSTimer*)timer {
483 if(![renewTicketLock tryLock]) return;
484 NSLog(@"krb5RenewAction %@", [NSDate date]);
486 [Krb5Util renewTicket:[krb5SecToExpireTimeForRenew intValue]
487 renewTime:[krb5RenewTime intValue]];
489 @catch (NSException * e) {
492 [renewTicketLock unlock];
498 // -------------------------------------------------------------------------------
499 // -(void) getImageFromBundle
500 // -------------------------------------------------------------------------------
501 - (NSImage*)getImageFromBundle:(NSString*)fileName fileExt:(NSString*)ext
503 return [[NSImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName
508 // -------------------------------------------------------------------------------
509 // - (void)menuNeedsUpdate:(NSMenu *)menu
510 // -------------------------------------------------------------------------------
511 - (void)menuNeedsUpdate:(NSMenu *)menu {
512 if (menu == backgrounderMenu)
514 [startStopMenuItem setTitle:afsState?@"Shutdown AFS":@"Startup AFS"];
515 [getReleaseTokenMenuItem setTitle:gotToken?@"Release token":@"Get New Token"];
516 [getReleaseTokenMenuItem setEnabled:afsState];
517 [[statusItem view] setNeedsDisplay:YES];
523 // -------------------------------------------------------------------------------
524 // -(void) useAklogPrefValue
525 // -------------------------------------------------------------------------------
526 - (BOOL)useAklogPrefValue
528 if(useAklogPrefValue) return [useAklogPrefValue intValue] == NSOnState;
529 else return NSOffState;
532 #pragma mark accessor
533 // -------------------------------------------------------------------------------
535 // -------------------------------------------------------------------------------
536 -(NSStatusItem*)statusItem {
540 // -------------------------------------------------------------------------------
542 // -------------------------------------------------------------------------------
543 -(void)setStatusItem:(BOOL)show {
545 if(statusItem) return;
546 statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:menuSize.width] retain];
547 [statusItem setView:[[AFSMenuExtraView alloc] initWithFrame:[[statusItem view] frame]
549 menu:backgrounderMenu]];
550 //Tells the NSStatusItem what menu to load
551 [statusItem setMenu:backgrounderMenu];
552 //Sets the tooptip for our item
553 [statusItem setToolTip:@"OpenAFS Preference"];
554 //Enables highlighting
555 [statusItem setHighlightMode:YES];
556 [statusItem setImage:noTokenImage];
558 if(!statusItem) return;
559 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
560 [statusItem autorelease];
564 // -------------------------------------------------------------------------------
565 // -(void) imageToRender
566 // -------------------------------------------------------------------------------
567 - (NSImage*)imageToRender
570 return hasTokenImage;
577 -(IBAction) startStopEvent:(id)sender {
578 [self startStopAfs:sender];
581 -(IBAction) getReleaseTokenEvent:(id)sender {
583 [self releaseToken:sender];
585 [self getToken:sender];