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>
24 @implementation AFSBackgrounderDelegate
25 #pragma mark NSApp Delegate
26 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
27 afsMngr = [[AFSPropertyManager alloc] initWithAfsPath:afsSysPath];
28 // allocate the lock for concurent afs check state
29 tokensLock = [[NSLock alloc] init];
31 //remove the auto eanble on menu item
32 [backgrounderMenu setAutoenablesItems:NO];
33 //Sets the images in our NSStatusItem
37 // Get the imge for menu
38 //Load image for menu rappresentation
39 hasTokenImage = [self getImageFromBundle:@"hasToken"
42 noTokenImage = [self getImageFromBundle:@"noToken"
44 //get the sazi of the menu icon
45 menuSize = [hasTokenImage size];
46 //Start to read the afs path
47 [self readPreferenceFile:nil];
52 // Register for preference user change
53 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(readPreferenceFile:)
54 name:kAFSMenuExtraID object:kPrefChangeNotification];
56 // Register for afs state change
57 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(afsVolumeMountChange:)
58 name:kAFSMenuExtraID object:kMExtraAFSStateChange];
60 // Register for menu state change
61 [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(chageMenuVisibility:)
62 name:kAFSMenuExtraID object:kMExtraAFSMenuChangeState];
64 //Register for mount/unmount afs volume
65 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
66 selector:@selector(afsVolumeMountChange:)
67 name:NSWorkspaceDidMountNotification object:nil];
69 [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self
70 selector:@selector(afsVolumeMountChange:)
71 name:NSWorkspaceDidUnmountNotification object:nil];
73 //try to see if we need tho show the menu at startup
75 [self setStatusItem:[showStatusMenu boolValue]];
77 NSLog(@"Check if we need to get token at login time %d", [aklogTokenAtLogin intValue]);
78 if([aklogTokenAtLogin boolValue] && afsState && !gotToken) {
79 NSLog(@"Proceed to get token");
80 //check if we must get the aklog at logint(first run of backgrounder
85 - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender {
86 if(afsSysPath) [afsSysPath release];
91 if(hasTokenImage) [hasTokenImage release];
92 if(noTokenImage) [noTokenImage release];
94 // Unregister for preference change
95 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kPrefChangeNotification];
96 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kMExtraAFSStateChange];
97 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kMExtraAFSMenuChangeState];
98 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceDidMountNotification object:nil];
99 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self name:NSWorkspaceDidUnmountNotification object:nil];
102 // send notify that menuextra has closed
103 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kPrefChangeNotification];
105 if(tokensLock) [tokensLock release];
106 if(afsMngr) [afsMngr release];
107 return NSTerminateNow;
109 // -------------------------------------------------------------------------------
110 // -(void) readPreferenceFile
111 // -------------------------------------------------------------------------------
112 - (void) readPreferenceFile:(NSNotification *)notification
115 [afsSysPath release];
118 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesAnyUser, kCFPreferencesAnyHost);
119 CFPreferencesSynchronize((CFStringRef)kAfsCommanderID, kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
121 afsSysPath = PREFERENCE_AFS_SYS_PAT_STATIC;
123 // read the preference for aklog use
124 useAklogPrefValue = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_USE_AKLOG,
125 (CFStringRef)kAfsCommanderID,
126 kCFPreferencesCurrentUser,
127 kCFPreferencesAnyHost);
129 showStatusMenu = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_SHOW_STATUS_MENU,
130 (CFStringRef)kAfsCommanderID,
131 kCFPreferencesCurrentUser,
132 kCFPreferencesAnyHost);
134 aklogTokenAtLogin = (NSNumber*)CFPreferencesCopyValue((CFStringRef)PREFERENCE_AKLOG_TOKEN_AT_LOGIN, (CFStringRef)kAfsCommanderID,
135 kCFPreferencesCurrentUser, kCFPreferencesAnyHost);
138 [self updateAfsStatus:nil];
141 // -------------------------------------------------------------------------------
142 // - (void)chageMenuVisibility:(NSNotification *)notification
143 // -------------------------------------------------------------------------------
145 - (void)chageMenuVisibility:(NSNotification *)notification {
146 [self readPreferenceFile:nil];
147 [self setStatusItem:[showStatusMenu boolValue]];
150 // -------------------------------------------------------------------------------
151 // - (void)startStopAfs:(id)sender
152 // -------------------------------------------------------------------------------
153 - (void)startStopAfs:(id)sender
155 if(!afsSysPath) return;
157 OSStatus status = noErr;
158 NSString *afsdPath = [TaskUtil searchExecutablePath:@"afsd"];
159 NSString *rootHelperApp = nil;
160 BOOL currentAfsState = NO;
163 if(afsdPath == nil) return;
164 currentAfsState = [afsMngr checkAfsStatus];
165 rootHelperApp = [[NSBundle mainBundle] pathForResource:@"afshlp" ofType:@""];
168 [self repairHelperTool];
170 // make the parameter to call the root helper app
171 status = [[AuthUtil shared] autorize];
175 NSMutableString *afsKextPath = [[NSMutableString alloc] initWithCapacity:256];
176 [afsKextPath setString:afsSysPath];
177 [afsKextPath appendString:@"/etc/afs.kext"];
179 const char *stopAfsArgs[] = {"stop_afs", [afsKextPath UTF8String], [afsdPath UTF8String], 0L};
180 [[AuthUtil shared] execUnixCommand:[rootHelperApp UTF8String]
184 const char *startAfsArgs[] = {[[ [NSBundle mainBundle] pathForResource:@"start_afs" ofType:@"sh"] UTF8String], [afsSysPath UTF8String], [afsdPath UTF8String], 0L};
185 [[AuthUtil shared] execUnixCommand:[rootHelperApp UTF8String]
191 @catch (NSException * e) {
195 [[AuthUtil shared] deautorize];
196 [self updateAfsStatus:nil];
197 //Send notification to preferencepane
198 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kMenuExtraEventOccured];
203 // -------------------------------------------------------------------------------
205 // -------------------------------------------------------------------------------
206 - (void)getToken:(id)sender
210 globalRect.origin = [[[statusItem view] window] convertBaseToScreen:[[statusItem view] frame].origin];
211 globalRect.size = [[statusItem view] frame].size;
212 AFSPropertyManager *afsPropMngr = [[AFSPropertyManager alloc] initWithAfsPath:afsSysPath ];
213 [afsPropMngr loadConfiguration];
215 if([useAklogPrefValue boolValue]) {
216 [afsPropMngr getTokens:false
219 [self klogUserEven:nil];
221 // register for user event
222 [[NSDistributedNotificationCenter defaultCenter] addObserver:self
223 selector:@selector(klogUserEven:)
225 object:kLogWindowClosed];
227 credentialMenuController = [[AFSMenuCredentialContoller alloc] initWhitRec:globalRect
228 afsPropManager:afsPropMngr];
229 [credentialMenuController showWindow];
232 //Dispose afs manager
233 [afsPropMngr release];
236 // -------------------------------------------------------------------------------
237 // -(void) releaseToken
238 // -------------------------------------------------------------------------------
239 - (void)releaseToken:(id)sender
242 [self updateAfsStatus:nil];
246 // -------------------------------------------------------------------------------
247 // -(void) afsVolumeMountChange - Track for mount unmount afs volume
248 // -------------------------------------------------------------------------------
249 - (void) afsVolumeMountChange:(NSNotification *)notification{
250 [self updateAfsStatus:nil];
253 // -------------------------------------------------------------------------------
254 // -(void) updateAfsStatus
255 // -------------------------------------------------------------------------------
256 - (void)updateAfsStatus:(NSTimer*)timer
259 if(![tokensLock tryLock]) return;
261 // check the afs state in esclusive mode
262 afsState = [afsMngr checkAfsStatus];
264 NSArray *tokens = [afsMngr getTokenList];
265 gotToken = [tokens count] > 0;
268 //update the menu icon
269 [[statusItem view] setNeedsDisplay:YES];
274 // -------------------------------------------------------------------------------
275 // -(void) klogUserEven
276 // -------------------------------------------------------------------------------
277 -(void) klogUserEven:(NSNotification *)notification
279 if(credentialMenuController) {
280 [[NSDistributedNotificationCenter defaultCenter] removeObserver:self name:kAFSMenuExtraID object:kLogWindowClosed];
281 [credentialMenuController closeWindow];
282 [credentialMenuController release];
283 credentialMenuController = nil;
285 //Send notification to PreferencePane
286 [[NSDistributedNotificationCenter defaultCenter] postNotificationName:kAfsCommanderID object:kMenuExtraEventOccured];
288 [self updateAfsStatus:nil];
290 #pragma mark Operational Function
291 // -------------------------------------------------------------------------------
293 // -------------------------------------------------------------------------------
295 //start the time for check tokens validity
296 if(timerForCheckTokensList) return;
297 timerForCheckTokensList = [NSTimer scheduledTimerWithTimeInterval:TOKENS_REFRESH_TIME_IN_SEC
299 selector:@selector(updateAfsStatus:)
302 [timerForCheckTokensList fire];
305 // -------------------------------------------------------------------------------
307 // -------------------------------------------------------------------------------
309 if(!timerForCheckTokensList) return;
310 [timerForCheckTokensList invalidate];
311 timerForCheckTokensList = nil;
313 // -------------------------------------------------------------------------------
314 // -(void) getImageFromBundle
315 // -------------------------------------------------------------------------------
316 - (NSImage*)getImageFromBundle:(NSString*)fileName fileExt:(NSString*)ext
318 return [[NSImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:fileName
323 // -------------------------------------------------------------------------------
324 // - (void)menuNeedsUpdate:(NSMenu *)menu
325 // -------------------------------------------------------------------------------
326 - (void)menuNeedsUpdate:(NSMenu *)menu {
327 if (menu == backgrounderMenu)
329 [startStopMenuItem setTitle:afsState?@"Shutdown AFS":@"Startup AFS"];
330 [getReleaseTokenMenuItem setTitle:gotToken?@"Release token":@"Get New Token"];
331 [getReleaseTokenMenuItem setEnabled:afsState];
332 [[statusItem view] setNeedsDisplay:YES];
338 // -------------------------------------------------------------------------------
339 // -(void) useAklogPrefValue
340 // -------------------------------------------------------------------------------
341 - (BOOL)useAklogPrefValue
343 if(useAklogPrefValue) return [useAklogPrefValue intValue] == NSOnState;
344 else return NSOffState;
347 // -------------------------------------------------------------------------------
349 // -------------------------------------------------------------------------------
350 - (void) repairHelperTool
355 NSString *afshlpPath = [[NSBundle mainBundle] pathForResource:@"afshlp" ofType:nil];
359 // Open tool exclusively, so nobody can change it while we bless it.
360 fdTool = open([afshlpPath UTF8String], O_NONBLOCK | O_RDONLY | O_EXLOCK, 0);
364 NSLog(@"Exclusive open while repairing tool failed: %d.", errno);
368 if(fstat(fdTool, &st))
370 NSLog(@"fstat failed.");
376 status = [[AuthUtil shared] autorize];
378 fchown(fdTool, 0, st.st_gid);
380 // Disable group and world writability and make setuid root.
381 fchmod(fdTool, (st.st_mode & (~(S_IWGRP | S_IWOTH)))/* | S_ISUID*/);
382 const char *args[] = {"root", [afshlpPath UTF8String],0L};
383 [[AuthUtil shared] execUnixCommand:"/usr/sbin/chown"
386 [[AuthUtil shared] deautorize];
388 } else NSLog(@"st_uid = 0");
390 NSLog(@"Self-repair done.");
394 #pragma mark accessor
395 // -------------------------------------------------------------------------------
397 // -------------------------------------------------------------------------------
398 -(NSStatusItem*)statusItem {
402 // -------------------------------------------------------------------------------
404 // -------------------------------------------------------------------------------
405 -(void)setStatusItem:(BOOL)show {
407 if(statusItem) return;
408 statusItem = [[[NSStatusBar systemStatusBar] statusItemWithLength:menuSize.width] retain];
409 [statusItem setView:[[AFSMenuExtraView alloc] initWithFrame:[[statusItem view] frame]
411 menu:backgrounderMenu]];
412 //Tells the NSStatusItem what menu to load
413 [statusItem setMenu:backgrounderMenu];
414 //Sets the tooptip for our item
415 [statusItem setToolTip:@"OpenAFS Preference"];
416 //Enables highlighting
417 [statusItem setHighlightMode:YES];
418 [statusItem setImage:noTokenImage];
420 if(!statusItem) return;
421 [[NSStatusBar systemStatusBar] removeStatusItem:statusItem];
422 [statusItem autorelease];
426 // -------------------------------------------------------------------------------
427 // -(void) imageToRender
428 // -------------------------------------------------------------------------------
429 - (NSImage*)imageToRender
432 return hasTokenImage;
439 -(IBAction) startStopEvent:(id)sender {
440 [self startStopAfs:sender];
443 -(IBAction) getReleaseTokenEvent:(id)sender {
444 [self getToken:sender];