macos: update AFS prefs pane
[openafs.git] / src / platform / DARWIN / AFSPreference / AFSPropertyManager.m
index 21e53d5..e96fc74 100644 (file)
@@ -6,7 +6,7 @@
 //  Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
 //
 
-#import <Kerberos/Kerberos.h>
+#import "Krb5Util.h"
 #import "AFSPropertyManager.h"
 #import "TaskUtil.h"
 
 // -------------------------------------------------------------------------------
 -(void) dealloc
 {
-       if(installationPath){ [installationPath release]; NSLog(@"Released installationPath"); }
-       if(cellList) { NSLog(@"Released cellList");[cellList removeAllObjects];[cellList release];}
-       if(cellName) { NSLog(@"Released cellName");[cellName release];}
+       if(installationPath){ [installationPath release];}
+       if(cellList) {[cellList removeAllObjects];[cellList release];}
+       if(cellName) {[cellName release];}
        if(futil) {
-               [futil endAutorization];
                [futil release];
                futil = nil; 
        }
                [self clearConfiguration];
                
                //chech the afs version for chioce wich afsd conf file usage
-               useAfsdConfVersion = mjVersion >= 1 && miVersion>=4 && paVersion>=7;
-               useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6 && paVersion>=31);
+               useAfsdConfVersion = mjVersion >= 1 && miVersion==4 && paVersion>=7;
+               useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion==5 && paVersion>=31);
+               useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6);
                
                // read thiscell config file
                [filePath setString:installationPath];
                [filePath appendString: @"/etc/ThisCell"];
-               NSLog(@"Search for cell name.");
+
                [self readCellInfo:filePath];
                if(!cellName){
                        @throw [NSException exceptionWithName:@"readCellInfo" 
                                                                        reason:kThisCellFOError 
                                                                  userInfo:nil];
                }
-               NSLog(@"Cell found: %@", cellName);
-               
                //read TheseCell file
                [filePath setString: installationPath];
                [filePath appendString: @"/etc/TheseCells"];
                userDefaultCellArray = [self readTheseCell:filePath];
                
                //read cell serv db
-               NSLog(@"Scan for cell db");
                [filePath setString: installationPath];
                [filePath appendString: @"/etc/CellServDB"];
                [self readCellDB:filePath];
-               NSLog(@"Server found: %d", [cellList count]);
-               
-               
                
                //Read cacheinfo
-               NSLog(@"Scan cacheinfo file");
                [filePath setString: installationPath];
                [filePath appendString: @"/etc/cacheinfo"];
                [self readCacheInfo:filePath];
-               NSLog(@"End scan cacheinfo file");
                
                //Read config/afsd.options
                [filePath setString: installationPath];
                [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
                [self readAfsdOption:filePath];
-               
        } @catch(NSException * e){
                @throw e;
        } @finally {
 //  readCacheInfo:
 //  file template "/afs:/var/db/openafs/cache:30000"
 // -------------------------------------------------------------------------------
--(void) readCacheInfo:(NSString*)filePath
+-(int) readCacheInfo:(NSString*)filePath
 {
        int cicle = 0;
        NSString *tmpString = nil;
        
        NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
        NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
+       if (!fileH) return 0;
        NSData *fileHData = [fileH readDataToEndOfFile];
        NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
                                                                                                           encoding:NSASCIIStringEncoding];
                //if(cacheInfoStrData) [cacheInfoStrData release];
                if(chunkStartCS) [chunkStartCS release];
        }
-       
+       return noErr;
 }
 
 
 {
        @try{
                if(useAfsdConfVersion) {
-                       [self readNewAfsdOption:filePath];
-               } else {
+                   if ([self readNewAfsdOption:filePath] != noErr)
                        [self readOldAfsdOption:filePath];
+               } else {
+                   if ([self readOldAfsdOption:filePath] != noErr)
+                       [self readNewAfsdOption:filePath];
                }
        
        }@catch(NSException *e){
 // -------------------------------------------------------------------------------
 //  readOldAfsdOption:
 // -------------------------------------------------------------------------------
--(void) readOldAfsdOption:(NSString*)filePath
+-(int) readOldAfsdOption:(NSString*)filePath
 {
-
-       /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
-       NSData *fileHData = [fileH readDataToEndOfFile];
-       NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
-                                                                                                               encoding:NSASCIIStringEncoding];*/
-       if(!filePath) return;
-       [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath] stringByStandardizingPath]];
+       if(!filePath) return 0;
+       return [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath 
+                                                                                                                         encoding:NSUTF8StringEncoding 
+                                                                                                                                error:nil] stringByStandardizingPath]];
 }
 
 // -------------------------------------------------------------------------------
 //  readAFSDParamLineContent:
 // -------------------------------------------------------------------------------
--(void) readAFSDParamLineContent:(NSString*) paramLine{
+-(int) readAFSDParamLineContent:(NSString*) paramLine{
+       if (!paramLine) return 0;
+
        NSString *tmpString = nil;
        NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
        NSScanner *afsdOptionS = [NSScanner  scannerWithString:paramLine];
        do{
                [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
                if(!tmpString) continue;
-               
-               NSLog(tmpString);
                //check parameter type
                if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
                {
                
                
        }while(![afsdOptionS isAtEnd]);
-       
+       return noErr;
 }
 
 // -------------------------------------------------------------------------------
 //  readNewAfsdOption:
 // -------------------------------------------------------------------------------
--(void) readNewAfsdOption:(NSString*)filePath
+-(int) readNewAfsdOption:(NSString*)filePath
 {
-       if(!filePath) return;
-       NSString *currentLines = nil;
-       NSString *paramValue = nil;
-       NSScanner *lineScanner = nil;
+       if(!filePath) return 0;
+       NSString *currentLines = 0;
+       NSString *paramValue = 0;
+       NSScanner *lineScanner = 0;
        
        //Get file content
-       NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath];
+       NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath 
+                                                                                                                        encoding:NSUTF8StringEncoding 
+                                                                                                                               error:nil];
+       if (!newAFSDConfContent) return 0;
        
        //get lines in array
        NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
                        
                        //scann the line
                        
-                       [lineScanner scanUpToString:@"\"" intoString:&paramValue]; NSLog(paramValue);[lineScanner scanUpToString:@"-" intoString:&paramValue];
-                       [lineScanner scanUpToString:@"\"" intoString:&paramValue]; NSLog(paramValue);
+                       [lineScanner scanUpToString:@"\"" intoString:&paramValue];[lineScanner scanUpToString:@"-" intoString:&paramValue];
+                       [lineScanner scanUpToString:@"\"" intoString:&paramValue];
                        
                        // read the asfd option param line
                        [self readAFSDParamLineContent:paramValue];
                        
                }
        }
+       return noErr;
 }
 
 
 {
        NSString *tmpString = nil;
        NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
-       if(result) NSLog(@"fs -version return: %@", result);
-       
-       
+       if (!result) return nil;
        NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
-
        NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
        NSScanner *versionS = [NSScanner  scannerWithString:result];
        //go to  start of version
 {
        NSString *tmpString = nil;
        NSString *totalVersion = [self getAfsVersion];
+       if (!totalVersion) return 0;
        NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
        NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
        [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
 {
        NSString *tmpString = nil;
        NSString *totalVersion = [self getAfsVersion];
+       if (!totalVersion) return 0;
        NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
        NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
        NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
 -(int) getAfsPatchVersionVersion
 {
        NSString *totalVersion = [self getAfsVersion];
+       if (!totalVersion) return 0;
        NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
        int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS 
                                                                                                           options:NSBackwardsSearch].location;
 //  readCellInfo:
 // -------------------------------------------------------------------------------
 -(void) readCellInfo:(NSString*) configFile {
-
-       
        NSError *error = nil;
        NSString *tmpStr = nil;
-       NSLog(@"Try to opening file: %@",configFile);
        NSString * result = [NSString stringWithContentsOfFile:configFile
                                                                                                  encoding:NSASCIIStringEncoding
                                                                                                         error:&error];
                @throw [NSException exceptionWithName:@"readCellInfo" 
                                                                           reason:kConfFileNotExits
                                                                         userInfo:nil];
-       }
+       } else if (!result)
+           return;
        NSScanner *scanner = [NSScanner scannerWithString:result];
-       
        [scanner scanUpToString:@"\n" 
                                 intoString:&tmpStr];
        
        NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
        
        NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
+       if (!fileH) return;
        NSData *dbCellData = [fileH readDataToEndOfFile];
        NSString *strData = [[NSString alloc] initWithData:dbCellData
                                                                                          encoding:NSASCIIStringEncoding];
 //  scanIpForCell:
 // -------------------------------------------------------------------------------
 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
+    if (!allIP) return;
        NSScanner *ipScann = [NSScanner scannerWithString:allIP];
        NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
        NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
        NSMutableArray *tokenList = [[NSMutableArray alloc] init];
        NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
        
-       // scann the tokens
+       if (!tokensOutput) return tokenList;
+       // scan the tokens
        NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
        NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
        
                
                
                if(line >= 2){
-                       NSLog(@"Token found %s", [tokenLine UTF8String]);
                        // add enteir row to result
                        [tokenList addObject:tokenLine];
                        // create the line scanner for all the row that contains token info
 //  +(void) aklog
 // -------------------------------------------------------------------------------
 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
-       KLPrincipal  princ = nil;
-       KLStatus kstatus = noErr;
-       char *princName = malloc(255);
+       KLStatus                kstatus = noErr;
        @try {
                // trying to ket kerberos ticket
                if(krb5CallEnable) {
-                       kstatus =  KLAcquireInitialTickets (0L, 0L, &princ,  &princName);
-                       if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"aklog" 
-                                                                                                                                                                                               reason:kPathNotEmpty 
-                                                                                                                                                                                         userInfo:nil];
+                       kstatus = [Krb5Util getNewTicketIfNotPresent];
                } else kstatus = klNoErr;
                
-                //ok to launch aklog
+               //ok to launch aklog
                if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
                                                                                                                         args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
                
                @throw e;
        }
        @finally {
-               // destory the kerberos va
-               if(princName && princ != nil) 
-                       KLDisposeString(princName);
-               else if(princName) free(princName);
+
        }
                
 }
 // -------------------------------------------------------------------------------
 -(void) shutdown
 {
-       NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
-       @try {
-               if([[AuthUtil shared] autorize] != noErr)
-                       return;
-               
-               /*const char *args0[] = {"stop", 0L};
-               [[AuthUtil shared] execUnixCommand:"/Library/StartupItems/OpenAFS/OpenAFS_stop"
-                                                                         args:args0 
-                                                                       output:0L];*/
-               
-               // unmount afs
-               const char *args1[] = {"-f", "/afs", 0L};
-               [[AuthUtil shared] execUnixCommand:"/sbin/umount"
-                                                                         args:args1 
-                                                                       output:0L];
-               
-               const char *args2[] = {"-shutdown", 0L};
-               [[AuthUtil shared] execUnixCommand:"/usr/sbin/afsd"
-                                                                         args:args2 
-                                                                       output:0L];
-                               
-               const char *args3[] = {[filePath UTF8String], 0L};
-               [[AuthUtil shared] execUnixCommand:"/sbin/kextunload"
-                                                                         args:args3 
-                                                                       output:0L];
-               
-       }
-       @catch (NSException * e) {
-               @throw e;
-       }
-       @finally {
-       
-       }
-       
-       
+  NSString *rootHelperApp = [[NSBundle bundleForClass:[self class]] pathForResource:@"afshlp" ofType:@""];
+    @try {
+       const char *stopArgs[] = {AFS_DAEMON_STARTUPSCRIPT, "stop", 0L};
+       [[AuthUtil shared] execUnixCommand:[rootHelperApp fileSystemRepresentation]
+                          args:stopArgs
+                          output:nil];
+    }
+    @catch (NSException * e) {
+       @throw e;
+    }
+    @finally {
+    }
+}
+
+
+// -------------------------------------------------------------------------------
+//  -(void) shutdown
+// -------------------------------------------------------------------------------
+-(void) startup
+{
+  NSString *rootHelperApp = [[NSBundle bundleForClass:[self class]] pathForResource:@"afshlp" ofType:@""];
+    @try {
+       const char *startArgs[] = {AFS_DAEMON_STARTUPSCRIPT, "start", 0L};
+       [[AuthUtil shared] execUnixCommand:[rootHelperApp fileSystemRepresentation]
+                          args:startArgs
+                          output:nil];
+    }
+    @catch (NSException * e) {
+       @throw e;
+    }
+    @finally {
+    }
 }
 
 // -------------------------------------------------------------------------------
                                                         encoding:  NSUTF8StringEncoding 
                                                                error:&err];
                
-               // backup original file
-               if([futil startAutorization] != noErr){
-                       @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
-                                                                                  reason:kUserNotAuth
-                                                                                userInfo:nil];
-               }
-
                if(makeBackup) [self backupConfigurationFiles];
 
                // install ThisCell
                [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
                
                // backup original file
-               if([futil startAutorization] != noErr){
-                       @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
-                                                                                  reason:kUserNotAuth
-                                                                                userInfo:nil];
-               }
-               
                if(makeBackup) {
                        //cacheinfo
                        [self backupFile:@"/etc/cacheinfo"];
 {      
 
        @try{
-               if([futil startAutorization] != noErr){
-                       @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
-                                                                                  reason:kUserNotAuth
-                                                                                userInfo:nil];
-               }
                //This cell
                [self backupFile:@"/etc/ThisCell"];
        
                //TheseCell
                [self backupFile:@"/etc/TheseCells"];
                
-               //[futil endAutorization];
        } @catch (NSException *e) {
                @throw e;
        } @finally {
                
                //Check if the file at path exist
                NSFileManager *fileManager = [NSFileManager defaultManager];
-               
-               //check if th efile exist
                if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
                
                // store the source path
                if(filePath) [filePath release];
        }
 }
+
 // -------------------------------------------------------------------------------
 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
 // -------------------------------------------------------------------------------
 -(BOOL) checkAfsStatus
 {
        BOOL result = NO;
-       NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"checkserver", nil]];
-       result = (fsResult?([fsResult rangeOfString:@"All servers are running."].location != NSNotFound):NO);
+       NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
+       result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
        return result;  
 }
 
 // -------------------------------------------------------------------------------
+//  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
+// -------------------------------------------------------------------------------
+-(BOOL) checkAfsStatusForStartup {
+       BOOL result = NO;
+               //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
+               //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
+       return result;
+}
+
+// -------------------------------------------------------------------------------
 //  makeChaceParamString
 // -------------------------------------------------------------------------------
 -(NSString*) makeChaceParamString