2 // AFSPropertyManager.m
5 // Created by Claudio Bisegni on 21/05/07.
6 // Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
9 #import <Kerberos/Kerberos.h>
10 #import "AFSPropertyManager.h"
13 #define kKerberosAuthError NSLocalizedStringFromTableInBundle(@"KerberosAuthError",nil,[NSBundle bundleForClass:[self class]],@"KerberosAuthError")
14 #define kPathNotEmpty NSLocalizedStringFromTableInBundle(@"PathNotEmpty",nil,[NSBundle bundleForClass:[self class]],@"PathNotEmpty")
15 #define kPathDontContainAfsInstallation NSLocalizedStringFromTableInBundle(@"PathDontContainAfsInstallation",nil,[NSBundle bundleForClass:[self class]],@"PathDontContainAfsInstallation")
16 #define kThisCellFOError NSLocalizedStringFromTableInBundle(@"ThisCellFOError",nil,[NSBundle bundleForClass:[self class]],@"ThisCellFOError")
17 #define kConfFileNotExits NSLocalizedStringFromTableInBundle(@"ConfFileNotExits",nil,[NSBundle bundleForClass:[self class]],@"ConfFileNotExits")
18 #define kUserNotAuth NSLocalizedStringFromTableInBundle(@"UserNotAuth",nil,[NSBundle bundleForClass:[self class]],@"UserNotAuth")
19 #define kBadAfsPath NSLocalizedStringFromTableInBundle(@"BadAfsPath",nil,[NSBundle bundleForClass:[self class]],@"BadAfsPath")
21 #define AFS_TOOLS_DIRECTORY /Library/Openafs/
22 #define AFS_TOOLS_BIN(x) "/Library/Openafs/bin/"#x
23 #define AFS_CACHE_PATH "/var/db/openafs/cache"
25 #define AFSD_TMP_OLD_PREFERENCE_FILE @"/tmp/afsd.options"
26 #define AFSD_TMP_NEW_PREFERENCE_FILE @"/tmp/afs.conf"
27 #define AFSD_OLD_PREFERENCE_FILE @"/etc/config/afsd.options"
28 #define AFSD_NEW_PREFERENCE_FILE @"/etc/config/afs.conf"
31 //-afsdb -stat 2000 -dcache 800 -daemons 3 -volumes 70 -dynroot -fakestat-all
32 #define AFSD_OPTION_AFSDB_KEY "-afsdb"
33 #define AFSD_OPTION_VERBOSE_KEY "-verbose"
34 #define AFSD_OPTION_STAT_KEY "-stat"
35 #define AFSD_OPTION_DCACHE_KEY "-dcache"
36 #define AFSD_OPTION_DAEMONS_KEY "-daemons"
37 #define AFSD_OPTION_VOLUMES_KEY "-volumes"
38 #define AFSD_OPTION_DYNROOT_KEY "-dynroot"
39 #define AFSD_OPTION_FKESTAT_ALL "-fakestat-all"
41 #define AFSD_CONF_VERBOSE @"VERBOSE"
42 #define AFSD_CONF_OPTION @"OPTIONS"
43 #define AFSD_CONF_SYSNAME @"AFS_SYSNAME"
44 #define AFSD_CONF_POST_INI @"AFS_POST_INIT"
45 #define AFSD_CONF_PRE_SHUTDOWN @"AFS_PRE_SHUTDOWN"
47 #define AFS_CONF_CHANGE_FROM_Mj_VERSION 1
48 #define AFS_CONF_CHANGE_FROM_Mi_VERSION 4
49 #define AFS_DEV_CONF_CHANGE_FROM_Mi_VERSION 5
50 #define AFS_CONF_CHANGE_FROM_Pa_VERSION 7
51 #define AFS_DEV_CONF_CHANGE_FROM_Pa_VERSION 31
53 @implementation AFSPropertyManager
56 // -------------------------------------------------------------------------------
58 // -------------------------------------------------------------------------------
62 cellList = [[NSMutableArray alloc] init];
64 userDefaultCellArray = nil;
65 useAfsdConfVersion = NO;
69 futil = [[FileUtil alloc] init];
73 // -------------------------------------------------------------------------------
75 // -------------------------------------------------------------------------------
76 - (id)initWithAfsPath:(NSString*)path
78 // avvio la creazione standard dell'oggetto
86 // -------------------------------------------------------------------------------
88 // -------------------------------------------------------------------------------
91 if(installationPath){ [installationPath release]; NSLog(@"Released installationPath"); }
92 if(cellList) { NSLog(@"Released cellList");[cellList removeAllObjects];[cellList release];}
93 if(cellName) { NSLog(@"Released cellName");[cellName release];}
95 [futil endAutorization];
102 // -------------------------------------------------------------------------------
104 // -------------------------------------------------------------------------------
105 - (void) setPath:(NSString*)path {
106 if(installationPath)[installationPath release];
109 installationPath = [path retain];
112 // -------------------------------------------------------------------------------
114 // -------------------------------------------------------------------------------
116 return installationPath;
119 // -------------------------------------------------------------------------------
121 // -------------------------------------------------------------------------------
122 -(int) statCacheEntry
124 return statCacheEntry;
127 // -------------------------------------------------------------------------------
128 // setStatCacheEntry:
129 // -------------------------------------------------------------------------------
130 -(void) setStatCacheEntry:(int)statEntry
132 statCacheEntry = statEntry;
136 // -------------------------------------------------------------------------------
138 // -------------------------------------------------------------------------------
144 // -------------------------------------------------------------------------------
146 // -------------------------------------------------------------------------------
147 -(void) setDCacheDim:(int)dcDim
152 // -------------------------------------------------------------------------------
154 // -------------------------------------------------------------------------------
155 -(int) cacheDimension
157 return cacheDimension;
160 // -------------------------------------------------------------------------------
161 // setCacheDimension:
162 // -------------------------------------------------------------------------------
163 -(void) setCacheDimension:(int)cacheDim
165 cacheDimension = cacheDim;
168 // -------------------------------------------------------------------------------
170 // -------------------------------------------------------------------------------
176 // -------------------------------------------------------------------------------
178 // -------------------------------------------------------------------------------
179 -(void) setDaemonNumber:(int)dNumber
181 daemonNumber = dNumber;
184 // -------------------------------------------------------------------------------
185 // afsRootMountPoint:
186 // -------------------------------------------------------------------------------
187 -(NSString*) afsRootMountPoint
189 return afsRootMountPoint;
192 // -------------------------------------------------------------------------------
193 // setAfsRootMountPoint:
194 // -------------------------------------------------------------------------------
195 -(void) setAfsRootMountPoint:(NSString*)mountPoint
197 if(afsRootMountPoint)[afsRootMountPoint release];
200 afsRootMountPoint = [mountPoint retain];
203 // -------------------------------------------------------------------------------
205 // -------------------------------------------------------------------------------
211 // -------------------------------------------------------------------------------
213 // -------------------------------------------------------------------------------
214 -(void) setNVolEntry:(int)entry
219 // -------------------------------------------------------------------------------
221 // -------------------------------------------------------------------------------
227 // -------------------------------------------------------------------------------
229 // -------------------------------------------------------------------------------
230 -(void) setDynRoot:(bool)flag
235 // -------------------------------------------------------------------------------
237 // -------------------------------------------------------------------------------
243 // -------------------------------------------------------------------------------
245 // -------------------------------------------------------------------------------
246 -(void) setAfsDB:(bool)flag
251 // -------------------------------------------------------------------------------
253 // -------------------------------------------------------------------------------
259 // -------------------------------------------------------------------------------
261 // -------------------------------------------------------------------------------
262 -(void) setVerbose:(bool)flag
267 // -------------------------------------------------------------------------------
268 // useAfsdConfVersion:
269 // -------------------------------------------------------------------------------
270 -(BOOL) useAfsdConfConfigFile
272 return useAfsdConfVersion;
275 // -------------------------------------------------------------------------------
276 // exceptionOnInvalidPath:
277 // -------------------------------------------------------------------------------
278 - (void) exceptionOnInvalidPath
280 if(!installationPath || ([installationPath length] == 0)) {
281 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath"
285 if(![[NSFileManager defaultManager] fileExistsAtPath:installationPath]){
286 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath"
287 reason:kPathDontContainAfsInstallation
293 // -------------------------------------------------------------------------------
294 // loadConfiguration:
295 // -------------------------------------------------------------------------------
296 - (void) loadConfiguration {
297 int mjVersion = [self getAfsMajorVersionVersion];
298 int miVersion = [self getAfsMinorVersionVersion];
299 int paVersion = [self getAfsPatchVersionVersion];
301 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
304 [self exceptionOnInvalidPath];
305 [self clearConfiguration];
307 //chech the afs version for chioce wich afsd conf file usage
308 useAfsdConfVersion = mjVersion >= 1 && miVersion>=4 && paVersion>=7;
309 useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6 && paVersion>=31);
311 // read thiscell config file
312 [filePath setString:installationPath];
313 [filePath appendString: @"/etc/ThisCell"];
314 NSLog(@"Search for cell name.");
315 [self readCellInfo:filePath];
317 @throw [NSException exceptionWithName:@"readCellInfo"
318 reason:kThisCellFOError
321 NSLog(@"Cell found: %@", cellName);
323 //read TheseCell file
324 [filePath setString: installationPath];
325 [filePath appendString: @"/etc/TheseCells"];
326 userDefaultCellArray = [self readTheseCell:filePath];
329 NSLog(@"Scan for cell db");
330 [filePath setString: installationPath];
331 [filePath appendString: @"/etc/CellServDB"];
332 [self readCellDB:filePath];
333 NSLog(@"Server found: %d", [cellList count]);
338 NSLog(@"Scan cacheinfo file");
339 [filePath setString: installationPath];
340 [filePath appendString: @"/etc/cacheinfo"];
341 [self readCacheInfo:filePath];
342 NSLog(@"End scan cacheinfo file");
344 //Read config/afsd.options
345 [filePath setString: installationPath];
346 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
347 [self readAfsdOption:filePath];
349 } @catch(NSException * e){
356 // -------------------------------------------------------------------------------
358 // file template "/afs:/var/db/openafs/cache:30000"
359 // -------------------------------------------------------------------------------
360 -(void) readCacheInfo:(NSString*)filePath
363 NSString *tmpString = nil;
364 NSCharacterSet *fullStopCS = [NSCharacterSet characterSetWithCharactersInString:@":"];
365 NSMutableCharacterSet *chunkStartCS = [[NSMutableCharacterSet alloc] init];
366 [chunkStartCS formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
367 [chunkStartCS formUnionWithCharacterSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"/"]];
369 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
370 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
371 NSData *fileHData = [fileH readDataToEndOfFile];
372 NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
373 encoding:NSASCIIStringEncoding];
374 NSScanner *cacheInfoS = [NSScanner scannerWithString:cacheInfoStrData];
382 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
383 [self setAfsRootMountPoint:tmpString];
384 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
388 //cache path default '/var/db/openafs/cache'
389 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
390 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
395 [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
396 [self setCacheDimension:[tmpString intValue]];
399 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
400 }@catch(NSException *e){
403 //if(cacheInfoStrData) [cacheInfoStrData release];
404 if(chunkStartCS) [chunkStartCS release];
410 // -------------------------------------------------------------------------------
412 // -------------------------------------------------------------------------------
413 -(int) writeCacheInfo:(NSString*)filePath
415 NSNumber *tmpNum = nil;
416 NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
417 if(!cacheInfoContent) return -1;
419 //Afs root mount point
420 if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0)
421 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
422 reason:@"Bad afs path"
424 [cacheInfoContent appendString:[self afsRootMountPoint]];
426 //standard cache path
428 [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
431 tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
432 if([tmpNum intValue] == 0)
433 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
434 reason:@"Bad cache dimension"
436 [cacheInfoContent appendString:[tmpNum stringValue]];
438 [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
440 encoding: NSASCIIStringEncoding
443 [cacheInfoContent release];
447 // -------------------------------------------------------------------------------
449 // file template "/afs:/var/db/openafs/cache:30000"
450 // -------------------------------------------------------------------------------
451 -(void) readAfsdOption:(NSString*)filePath
454 if(useAfsdConfVersion) {
455 [self readNewAfsdOption:filePath];
457 [self readOldAfsdOption:filePath];
460 }@catch(NSException *e){
468 // -------------------------------------------------------------------------------
469 // readOldAfsdOption:
470 // -------------------------------------------------------------------------------
471 -(void) readOldAfsdOption:(NSString*)filePath
474 /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
475 NSData *fileHData = [fileH readDataToEndOfFile];
476 NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
477 encoding:NSASCIIStringEncoding];*/
478 if(!filePath) return;
479 [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath] stringByStandardizingPath]];
482 // -------------------------------------------------------------------------------
483 // readAFSDParamLineContent:
484 // -------------------------------------------------------------------------------
485 -(void) readAFSDParamLineContent:(NSString*) paramLine{
486 NSString *tmpString = nil;
487 NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
488 NSScanner *afsdOptionS = [NSScanner scannerWithString:paramLine];
491 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
492 if(!tmpString) continue;
495 //check parameter type
496 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
498 // get number of daemon
499 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
500 [self setDaemonNumber:[tmpString intValue]];
502 //check parameter type
503 if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
505 //get dcache dimension
506 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
507 [self setDCacheDim:[tmpString intValue]];
509 //check parameter type
510 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
512 [self setDynRoot:true];
514 if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
516 [self setVerbose:true];
517 } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
519 [self setAfsDB:true];
521 //check parameter type
522 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
524 // get fstat entry num
525 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
526 [self setStatCacheEntry:[tmpString intValue]];
529 //check parameter type
530 if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
532 // get fstat entry num
533 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
534 [self setNVolEntry:[tmpString intValue]];
539 }while(![afsdOptionS isAtEnd]);
543 // -------------------------------------------------------------------------------
544 // readNewAfsdOption:
545 // -------------------------------------------------------------------------------
546 -(void) readNewAfsdOption:(NSString*)filePath
548 if(!filePath) return;
549 NSString *currentLines = nil;
550 NSString *paramValue = nil;
551 NSScanner *lineScanner = nil;
554 NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath];
557 NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
559 //Get the lines enumerator
560 NSEnumerator *lineEnumerator = [confLines objectEnumerator];
563 while(currentLines = [lineEnumerator nextObject]) {
564 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
566 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
567 lineScanner = [NSScanner scannerWithString:currentLines];
568 if(!lineScanner) continue;
572 [lineScanner scanUpToString:@"\"" intoString:¶mValue]; NSLog(paramValue);[lineScanner scanUpToString:@"-" intoString:¶mValue];
573 [lineScanner scanUpToString:@"\"" intoString:¶mValue]; NSLog(paramValue);
575 // read the asfd option param line
576 [self readAFSDParamLineContent:paramValue];
577 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
579 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
581 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
588 // -------------------------------------------------------------------------------
590 // -------------------------------------------------------------------------------
591 -(int) writeAfsdOption:(NSString*)filePath
595 if(useAfsdConfVersion) {
596 result = [self writeNewAfsdOption:filePath];
598 result = [self writeOldAfsdOption:filePath];
601 }@catch(NSException *e){
609 // -------------------------------------------------------------------------------
610 // writeOldAfsdOption:
611 // -------------------------------------------------------------------------------
612 -(int) writeOldAfsdOption:(NSString*)filePath;
614 NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
616 [oldConfFileContent appendString:[self makeChaceParamString]];
618 //add cariage return at end of file
619 [oldConfFileContent appendString:@"\n"];
621 //write content on file
622 [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
624 encoding: NSASCIIStringEncoding
630 // -------------------------------------------------------------------------------
631 // writeNewAfsdOption:
634 // AFS_POST_INIT=afs_server_prefs
636 // -------------------------------------------------------------------------------
637 -(int) writeNewAfsdOption:(NSString*)filePath
639 NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
642 [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
645 [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
648 [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
651 [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
654 [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
656 //add content on file
657 [newConfFileContent appendString:@"\n"];
660 [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
662 encoding: NSASCIIStringEncoding
667 // -------------------------------------------------------------------------------
669 // -------------------------------------------------------------------------------
670 -(NSString*) getAfsVersion
672 NSString *tmpString = nil;
673 NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
674 if(result) NSLog(@"fs -version return: %@", result);
677 NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
679 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
680 NSScanner *versionS = [NSScanner scannerWithString:result];
681 //go to start of version
682 [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
684 //get the total version string
685 [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
690 // -------------------------------------------------------------------------------
691 // getAfsMajorVersionVersion:
692 // -------------------------------------------------------------------------------
693 -(int) getAfsMajorVersionVersion
695 NSString *tmpString = nil;
696 NSString *totalVersion = [self getAfsVersion];
697 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
698 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
699 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
700 return [tmpString intValue];
703 // -------------------------------------------------------------------------------
704 // getAfsMinorVersionVersion:
705 // -------------------------------------------------------------------------------
706 -(int) getAfsMinorVersionVersion
708 NSString *tmpString = nil;
709 NSString *totalVersion = [self getAfsVersion];
710 NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
711 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
712 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
713 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
714 return [tmpString intValue];
717 // -------------------------------------------------------------------------------
718 // getAfsPatchVersionVersion:
719 // -------------------------------------------------------------------------------
720 -(int) getAfsPatchVersionVersion
722 NSString *totalVersion = [self getAfsVersion];
723 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
724 int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS
725 options:NSBackwardsSearch].location;
726 int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
731 // -------------------------------------------------------------------------------
732 // clearConfiguration:
733 // -------------------------------------------------------------------------------
734 - (void) clearConfiguration{
736 // clear list of cell
737 [cellList removeAllObjects];
739 userDefaultCellArray= nil;
741 // remove the old cell name
748 // -------------------------------------------------------------------------------
750 // -------------------------------------------------------------------------------
751 -(NSMutableArray*) getCellList
758 // -------------------------------------------------------------------------------
760 // -------------------------------------------------------------------------------
761 -(NSArray*) getAllCellsName {
762 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
763 for(int idx = 0; idx < [cellList count]; idx++){
764 DBCellElement *elemnt = [cellList objectAtIndex:idx];
765 [allCelListName addObject:[elemnt getCellName]];
767 return allCelListName;
770 // -------------------------------------------------------------------------------
772 // -------------------------------------------------------------------------------
773 -(NSArray*) getUserDefaultForTokenCells {
774 return userDefaultCellArray;
777 // -------------------------------------------------------------------------------
778 // getDefaultCellName:
779 // -------------------------------------------------------------------------------
780 -(NSArray*) getDefaultForTokenCellsName {
781 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
782 for(int idx = 0; idx < [cellList count]; idx++){
783 DBCellElement *elemnt = [cellList objectAtIndex:idx];
784 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
786 return allCelListName;
789 // -------------------------------------------------------------------------------
791 // -------------------------------------------------------------------------------
792 -(NSString*) getDefaultCellName
797 // -------------------------------------------------------------------------------
798 // setDefaultCellByName:
799 // -------------------------------------------------------------------------------
800 -(void) setDefaultCellByName:(NSString*)name
802 DBCellElement *elementCell = nil;
803 BOOL cellFound = false;
806 for(int idx = 0; idx < [cellList count]; idx++) {
807 // check every cell for delete as old user default cell or selected as neww cell
808 elementCell = [cellList objectAtIndex:idx];
809 cellFound = [name compare:[elementCell getCellName]] == NSOrderedSame;
810 [elementCell setUserDefaultCell:cellFound];
812 [elementCell setUserDefaultForToken:YES];
813 if(cellName)[cellName release];
814 cellName = [name retain];
820 // -------------------------------------------------------------------------------
822 // -------------------------------------------------------------------------------
823 - (void) setCellName:(NSString*)cell {
824 [self setDefaultCellByName:cell];
827 // -------------------------------------------------------------------------------
829 // -------------------------------------------------------------------------------
830 -(void) readCellInfo:(NSString*) configFile {
833 NSError *error = nil;
834 NSString *tmpStr = nil;
835 NSLog(@"Try to opening file: %@",configFile);
836 NSString * result = [NSString stringWithContentsOfFile:configFile
837 encoding:NSASCIIStringEncoding
839 if(!result && error){
841 @throw [NSException exceptionWithName:@"readCellInfo"
842 reason:kConfFileNotExits
845 NSScanner *scanner = [NSScanner scannerWithString:result];
847 [scanner scanUpToString:@"\n"
850 // make a copy of self created string
851 cellName = [tmpStr retain];
854 // -------------------------------------------------------------------------------
856 // -------------------------------------------------------------------------------
857 -(void) readCellDB:(NSString*) configFile {
858 NSString *tmpString = nil;
859 BOOL isInCellDefaultArray = NO; // the cell belong
860 BOOL isDefaultCell = NO;
861 DBCellElement *afsCellDBElement = nil;
862 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
863 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
865 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
866 NSData *dbCellData = [fileH readDataToEndOfFile];
867 NSString *strData = [[NSString alloc] initWithData:dbCellData
868 encoding:NSASCIIStringEncoding];
869 NSScanner *cellDBScanner = [NSScanner scannerWithString:strData];
872 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
873 while([cellDBScanner isAtEnd] == NO) {
875 afsCellDBElement = [[DBCellElement alloc] init];
877 // get the name of cell
878 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
879 [afsCellDBElement setCellName: tmpString];
881 //check if this cells is one of user has selected to get token
882 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
883 //check if this cell is also the default cell
884 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
886 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
887 [afsCellDBElement setUserDefaultCell:isDefaultCell];
893 // get the cell comment
894 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
895 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
896 [afsCellDBElement setCellComment: tmpString];
899 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
900 // scann all ip in list
901 [self scanIpForCell:afsCellDBElement allIP:tmpString];
904 [cellList addObject:afsCellDBElement];
905 // release the object becasuse NSMutableArray make a retain on object
906 [afsCellDBElement release];
908 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
910 }@catch(NSException *e){
913 //if(strData) [strData release];
917 // -------------------------------------------------------------------------------
919 // -------------------------------------------------------------------------------
920 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
921 NSScanner *ipScann = [NSScanner scannerWithString:allIP];
922 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
923 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
924 NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
925 NSString *tmpString = nil;
926 while([ipScann isAtEnd] == NO){
927 CellIp *cellIpDesc = [[CellIp alloc] init];
930 [ipScann scanUpToCharactersFromSet:spaceCS
931 intoString:&tmpString];
933 [cellIpDesc setCellIp:tmpString];
934 //[tmpString release];
937 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
938 // skip comment symbol
939 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
941 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
942 [cellIpDesc setCellComment:tmpString];
943 //[tmpString release];
945 [cellElement addIpToCell:cellIpDesc];
946 // release the object becasuse NSMutableArray make a retain on object
947 [cellIpDesc release];
952 // -------------------------------------------------------------------------------
954 // -------------------------------------------------------------------------------
955 -(NSArray*) readTheseCell:(NSString*) configFile {
957 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
958 NSData *dbCellData = [fileH readDataToEndOfFile];
959 NSString *strData = [[NSString alloc] initWithData:dbCellData
960 encoding:NSASCIIStringEncoding];
962 return [strData componentsSeparatedByString:@"\n"];
965 // -------------------------------------------------------------------------------
966 // -(void) getTokenList
967 // -------------------------------------------------------------------------------
968 -(NSArray*) getTokenList
971 NSString *tokenLine = nil;
972 //NSString *tmpValue = nil;
973 NSMutableArray *tokenList = [[NSMutableArray alloc] init];
974 NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
977 NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
978 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
980 while([tokenScan isAtEnd] == NO){
983 [tokenScan scanUpToCharactersFromSet:returnCS
984 intoString:&tokenLine];
986 // check for tokens end
987 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
991 NSLog(@"Token found %s", [tokenLine UTF8String]);
992 // add enteir row to result
993 [tokenList addObject:tokenLine];
994 // create the line scanner for all the row that contains token info
1001 // -------------------------------------------------------------------------------
1002 // +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
1003 // -------------------------------------------------------------------------------
1004 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
1006 if(uName == @"" || uPwd == @"") return;
1008 [TaskUtil executeTaskSearchingPath:@"klog"
1009 args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
1015 // -------------------------------------------------------------------------------
1017 // -------------------------------------------------------------------------------
1018 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1019 KLPrincipal princ = nil;
1020 KLStatus kstatus = noErr;
1021 char *princName = malloc(255);
1023 // trying to ket kerberos ticket
1024 if(krb5CallEnable) {
1025 kstatus = KLAcquireInitialTickets (0L, 0L, &princ, &princName);
1026 if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"aklog"
1027 reason:kPathNotEmpty
1029 } else kstatus = klNoErr;
1031 //ok to launch aklog
1032 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog"
1033 args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1036 @catch (NSException * e) {
1040 // destory the kerberos va
1041 if(princName && princ != nil)
1042 KLDisposeString(princName);
1043 else if(princName) free(princName);
1049 // -------------------------------------------------------------------------------
1051 // -------------------------------------------------------------------------------
1052 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1054 NSString *celStrName = nil;
1055 NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1056 if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1057 //there are other cell to autenticate
1058 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1059 celStrName = [tmpDefaultArray objectAtIndex:idx];
1065 [self aklog:celStrName noKerberosCall:YES];
1069 //there is only default cell to autenticate
1075 [self aklog:nil noKerberosCall:YES];
1080 // -------------------------------------------------------------------------------
1082 // -------------------------------------------------------------------------------
1083 -(void) unlog:(NSString*)cell
1085 [TaskUtil executeTaskSearchingPath:@"unlog"
1086 args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1089 // -------------------------------------------------------------------------------
1091 // -------------------------------------------------------------------------------
1094 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1096 if([[AuthUtil shared] autorize] != noErr)
1099 /*const char *args0[] = {"stop", 0L};
1100 [[AuthUtil shared] execUnixCommand:"/Library/StartupItems/OpenAFS/OpenAFS_stop"
1105 const char *args1[] = {"-f", "/afs", 0L};
1106 [[AuthUtil shared] execUnixCommand:"/sbin/umount"
1110 const char *args2[] = {"-shutdown", 0L};
1111 [[AuthUtil shared] execUnixCommand:"/usr/sbin/afsd"
1115 const char *args3[] = {[filePath UTF8String], 0L};
1116 [[AuthUtil shared] execUnixCommand:"/sbin/kextunload"
1121 @catch (NSException * e) {
1131 // -------------------------------------------------------------------------------
1132 // -(void) saveConfigurationFiles
1133 // -------------------------------------------------------------------------------
1134 -(void) saveConfigurationFiles:(BOOL) makeBackup
1137 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1138 NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1139 NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1140 DBCellElement *cellElement = nil;
1142 // save the configuration file
1144 [self exceptionOnInvalidPath];
1147 [filePath setString: @"/tmp/ThisCell"];
1148 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1150 encoding: NSASCIIStringEncoding
1154 for(int idx = 0; idx < [cellList count]; idx++){
1155 cellElement = [cellList objectAtIndex:idx];
1156 [cellServDBString appendString:[cellElement description]];
1157 if([cellElement userDefaultForToken]) {
1158 [theseCellString appendString:[cellElement getCellName]];
1159 [theseCellString appendString:@"\n"];
1164 [filePath setString: @"/tmp/CellServDB"];
1165 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1167 encoding: NSUTF8StringEncoding
1170 [filePath setString: @"/tmp/TheseCells"];
1171 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1173 encoding: NSUTF8StringEncoding
1176 // backup original file
1177 if([futil startAutorization] != noErr){
1178 @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization"
1183 if(makeBackup) [self backupConfigurationFiles];
1186 [filePath setString: installationPath];
1187 [filePath appendString: @"/etc/ThisCell"];
1188 [self installConfigurationFile:@"/tmp/ThisCell"
1191 // install CellServDB
1192 [filePath setString: installationPath];
1193 [filePath appendString: @"/etc/CellServDB"];
1194 [self installConfigurationFile:@"/tmp/CellServDB"
1197 // install CellServDB
1198 [filePath setString: installationPath];
1199 [filePath appendString: @"/etc/TheseCells"];
1200 [self installConfigurationFile:@"/tmp/TheseCells"
1203 } @catch (NSException *e) {
1206 // dispose all variable used
1207 if(filePath) [filePath release];
1208 if(cellServDBString) [cellServDBString release];
1213 // -------------------------------------------------------------------------------
1214 // -(void) saveCacheConfigurationFiles
1215 // -------------------------------------------------------------------------------
1216 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1218 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1219 // save the configuration file
1221 [self exceptionOnInvalidPath];
1223 // cacheinfo file creation
1224 [self writeCacheInfo:@"/tmp/cacheinfo"];
1226 //afsd.option or afs.conf file creation
1227 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1229 // backup original file
1230 if([futil startAutorization] != noErr){
1231 @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization"
1238 [self backupFile:@"/etc/cacheinfo"];
1241 [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1244 // install cacheinfo
1245 [filePath setString:installationPath];
1246 [filePath appendString: @"/etc/cacheinfo"];
1247 [self installConfigurationFile:@"/tmp/cacheinfo"
1250 // install afsd.conf or afs.conf configuration file
1251 [filePath setString: installationPath];
1252 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1253 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1256 } @catch (NSException *e) {
1259 if(filePath) [filePath release];
1265 // -------------------------------------------------------------------------------
1266 // -(void) installConfigurationFile
1267 // -------------------------------------------------------------------------------
1268 -(void) installConfigurationFile:(NSString*)srcConfFile
1269 destPath:(NSString*)destPath
1271 // delete the file original file
1273 if([futil autorizedDelete:destPath] != noErr){
1274 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete"
1280 if([futil autorizedMoveFile:srcConfFile
1281 toPath:destPath] != noErr) {
1282 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile"
1288 if([futil autorizedChown:destPath
1290 group:@"wheel"]!= noErr) {
1291 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown"
1297 // -------------------------------------------------------------------------------
1298 // -(void) backupConfigurationFiles
1299 // -------------------------------------------------------------------------------
1300 -(void) backupConfigurationFiles
1304 if([futil startAutorization] != noErr){
1305 @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization"
1310 [self backupFile:@"/etc/ThisCell"];
1313 [self backupFile:@"/etc/CellServDB"];
1316 [self backupFile:@"/etc/TheseCells"];
1318 //[futil endAutorization];
1319 } @catch (NSException *e) {
1325 // -------------------------------------------------------------------------------
1326 // -(void) backupFile:(NSString*)localAfsFilePath
1327 // -------------------------------------------------------------------------------
1328 -(void) backupFile:(NSString*)localAfsFilePath
1330 NSString *srcString = nil;
1331 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1332 OSStatus err = noErr;
1334 [filePath setString: installationPath];
1335 [filePath appendString: localAfsFilePath];
1337 //Check if the file at path exist
1338 NSFileManager *fileManager = [NSFileManager defaultManager];
1340 //check if th efile exist
1341 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1343 // store the source path
1344 srcString = [filePath stringByExpandingTildeInPath];
1345 [filePath appendString: @".afscommander_bk"];
1347 // check for presence of bk file
1348 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1350 err = [futil autorizedCopy:srcString
1351 toPath:[filePath stringByExpandingTildeInPath]];
1353 } @catch (NSException *e) {
1356 if(filePath) [filePath release];
1359 // -------------------------------------------------------------------------------
1360 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1361 // -------------------------------------------------------------------------------
1362 -(BOOL) checkAfsStatus
1365 NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"checkserver", nil]];
1366 result = (fsResult?([fsResult rangeOfString:@"All servers are running."].location != NSNotFound):NO);
1370 // -------------------------------------------------------------------------------
1371 // makeChaceParamString
1372 // -------------------------------------------------------------------------------
1373 -(NSString*) makeChaceParamString
1375 NSNumber *tmpNum = nil;
1376 NSMutableString *afsdOption = [[NSMutableString alloc] init];
1377 if(!afsdOption) return @"";
1378 //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1382 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1386 if([self verbose]) {
1387 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1391 tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1392 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1395 tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1396 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1399 tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1400 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1403 tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1404 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1407 if([self dynRoot]) {
1408 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1412 [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1414 return [afsdOption autorelease];