2 // AFSPropertyManager.m
5 // Created by Claudio Bisegni on 21/05/07.
6 // Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
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];}
92 if(cellList) {[cellList removeAllObjects];[cellList release];}
93 if(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"];
315 [self readCellInfo:filePath];
317 @throw [NSException exceptionWithName:@"readCellInfo"
318 reason:kThisCellFOError
321 //read TheseCell file
322 [filePath setString: installationPath];
323 [filePath appendString: @"/etc/TheseCells"];
324 userDefaultCellArray = [self readTheseCell:filePath];
327 [filePath setString: installationPath];
328 [filePath appendString: @"/etc/CellServDB"];
329 [self readCellDB:filePath];
332 [filePath setString: installationPath];
333 [filePath appendString: @"/etc/cacheinfo"];
334 [self readCacheInfo:filePath];
336 //Read config/afsd.options
337 [filePath setString: installationPath];
338 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
339 [self readAfsdOption:filePath];
340 } @catch(NSException * e){
347 // -------------------------------------------------------------------------------
349 // file template "/afs:/var/db/openafs/cache:30000"
350 // -------------------------------------------------------------------------------
351 -(int) readCacheInfo:(NSString*)filePath
354 NSString *tmpString = nil;
355 NSCharacterSet *fullStopCS = [NSCharacterSet characterSetWithCharactersInString:@":"];
356 NSMutableCharacterSet *chunkStartCS = [[NSMutableCharacterSet alloc] init];
357 [chunkStartCS formUnionWithCharacterSet:[NSCharacterSet alphanumericCharacterSet]];
358 [chunkStartCS formUnionWithCharacterSet:[NSMutableCharacterSet characterSetWithCharactersInString:@"/"]];
360 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
361 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
362 if (!fileH) return nil;
363 NSData *fileHData = [fileH readDataToEndOfFile];
364 NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
365 encoding:NSASCIIStringEncoding];
366 NSScanner *cacheInfoS = [NSScanner scannerWithString:cacheInfoStrData];
374 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
375 [self setAfsRootMountPoint:tmpString];
376 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
380 //cache path default '/var/db/openafs/cache'
381 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
382 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
387 [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
388 [self setCacheDimension:[tmpString intValue]];
391 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
392 }@catch(NSException *e){
395 //if(cacheInfoStrData) [cacheInfoStrData release];
396 if(chunkStartCS) [chunkStartCS release];
402 // -------------------------------------------------------------------------------
404 // -------------------------------------------------------------------------------
405 -(int) writeCacheInfo:(NSString*)filePath
407 NSNumber *tmpNum = nil;
408 NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
409 if(!cacheInfoContent) return -1;
411 //Afs root mount point
412 if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0)
413 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
414 reason:@"Bad afs path"
416 [cacheInfoContent appendString:[self afsRootMountPoint]];
418 //standard cache path
420 [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
423 tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
424 if([tmpNum intValue] == 0)
425 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
426 reason:@"Bad cache dimension"
428 [cacheInfoContent appendString:[tmpNum stringValue]];
430 [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
432 encoding: NSASCIIStringEncoding
435 [cacheInfoContent release];
439 // -------------------------------------------------------------------------------
441 // file template "/afs:/var/db/openafs/cache:30000"
442 // -------------------------------------------------------------------------------
443 -(void) readAfsdOption:(NSString*)filePath
446 if(useAfsdConfVersion) {
447 if ([self readNewAfsdOption:filePath] != noErr)
448 [self readOldAfsdOption:filePath];
450 if ([self readOldAfsdOption:filePath] != noErr)
451 [self readNewAfsdOption:filePath];
454 }@catch(NSException *e){
462 // -------------------------------------------------------------------------------
463 // readOldAfsdOption:
464 // -------------------------------------------------------------------------------
465 -(int) readOldAfsdOption:(NSString*)filePath
467 if(!filePath) return nil;
468 return [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath
469 encoding:NSUTF8StringEncoding
470 error:nil] stringByStandardizingPath]];
473 // -------------------------------------------------------------------------------
474 // readAFSDParamLineContent:
475 // -------------------------------------------------------------------------------
476 -(int) readAFSDParamLineContent:(NSString*) paramLine{
477 if (!paramLine) return nil;
479 NSString *tmpString = nil;
480 NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
481 NSScanner *afsdOptionS = [NSScanner scannerWithString:paramLine];
484 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
485 if(!tmpString) continue;
486 //check parameter type
487 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
489 // get number of daemon
490 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
491 [self setDaemonNumber:[tmpString intValue]];
493 //check parameter type
494 if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
496 //get dcache dimension
497 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
498 [self setDCacheDim:[tmpString intValue]];
500 //check parameter type
501 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
503 [self setDynRoot:true];
505 if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
507 [self setVerbose:true];
508 } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
510 [self setAfsDB:true];
512 //check parameter type
513 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
515 // get fstat entry num
516 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
517 [self setStatCacheEntry:[tmpString intValue]];
520 //check parameter type
521 if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
523 // get fstat entry num
524 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
525 [self setNVolEntry:[tmpString intValue]];
530 }while(![afsdOptionS isAtEnd]);
534 // -------------------------------------------------------------------------------
535 // readNewAfsdOption:
536 // -------------------------------------------------------------------------------
537 -(int) readNewAfsdOption:(NSString*)filePath
539 if(!filePath) return nil;
540 NSString *currentLines = nil;
541 NSString *paramValue = nil;
542 NSScanner *lineScanner = nil;
545 NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath
546 encoding:NSUTF8StringEncoding
548 if (!newAFSDConfContent) return nil;
551 NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
553 //Get the lines enumerator
554 NSEnumerator *lineEnumerator = [confLines objectEnumerator];
557 while(currentLines = [lineEnumerator nextObject]) {
558 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
560 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
561 lineScanner = [NSScanner scannerWithString:currentLines];
562 if(!lineScanner) continue;
566 [lineScanner scanUpToString:@"\"" intoString:¶mValue];[lineScanner scanUpToString:@"-" intoString:¶mValue];
567 [lineScanner scanUpToString:@"\"" intoString:¶mValue];
569 // read the asfd option param line
570 [self readAFSDParamLineContent:paramValue];
571 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
573 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
575 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
583 // -------------------------------------------------------------------------------
585 // -------------------------------------------------------------------------------
586 -(int) writeAfsdOption:(NSString*)filePath
590 if(useAfsdConfVersion) {
591 result = [self writeNewAfsdOption:filePath];
593 result = [self writeOldAfsdOption:filePath];
596 }@catch(NSException *e){
604 // -------------------------------------------------------------------------------
605 // writeOldAfsdOption:
606 // -------------------------------------------------------------------------------
607 -(int) writeOldAfsdOption:(NSString*)filePath;
609 NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
611 [oldConfFileContent appendString:[self makeChaceParamString]];
613 //add cariage return at end of file
614 [oldConfFileContent appendString:@"\n"];
616 //write content on file
617 [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
619 encoding: NSASCIIStringEncoding
625 // -------------------------------------------------------------------------------
626 // writeNewAfsdOption:
629 // AFS_POST_INIT=afs_server_prefs
631 // -------------------------------------------------------------------------------
632 -(int) writeNewAfsdOption:(NSString*)filePath
634 NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
637 [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
640 [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
643 [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
646 [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
649 [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
651 //add content on file
652 [newConfFileContent appendString:@"\n"];
655 [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
657 encoding: NSASCIIStringEncoding
662 // -------------------------------------------------------------------------------
664 // -------------------------------------------------------------------------------
665 -(NSString*) getAfsVersion
667 NSString *tmpString = nil;
668 NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
669 if (!result) return nil;
670 NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
671 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
672 NSScanner *versionS = [NSScanner scannerWithString:result];
673 //go to start of version
674 [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
676 //get the total version string
677 [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
682 // -------------------------------------------------------------------------------
683 // getAfsMajorVersionVersion:
684 // -------------------------------------------------------------------------------
685 -(int) getAfsMajorVersionVersion
687 NSString *tmpString = nil;
688 NSString *totalVersion = [self getAfsVersion];
689 if (!totalVersion) return 0;
690 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
691 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
692 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
693 return [tmpString intValue];
696 // -------------------------------------------------------------------------------
697 // getAfsMinorVersionVersion:
698 // -------------------------------------------------------------------------------
699 -(int) getAfsMinorVersionVersion
701 NSString *tmpString = nil;
702 NSString *totalVersion = [self getAfsVersion];
703 if (!totalVersion) return 0;
704 NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
705 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
706 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
707 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
708 return [tmpString intValue];
711 // -------------------------------------------------------------------------------
712 // getAfsPatchVersionVersion:
713 // -------------------------------------------------------------------------------
714 -(int) getAfsPatchVersionVersion
716 NSString *totalVersion = [self getAfsVersion];
717 if (!totalVersion) return 0;
718 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
719 int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS
720 options:NSBackwardsSearch].location;
721 int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
726 // -------------------------------------------------------------------------------
727 // clearConfiguration:
728 // -------------------------------------------------------------------------------
729 - (void) clearConfiguration{
731 // clear list of cell
732 [cellList removeAllObjects];
734 userDefaultCellArray= nil;
736 // remove the old cell name
743 // -------------------------------------------------------------------------------
745 // -------------------------------------------------------------------------------
746 -(NSMutableArray*) getCellList
753 // -------------------------------------------------------------------------------
755 // -------------------------------------------------------------------------------
756 -(NSArray*) getAllCellsName {
757 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
758 for(int idx = 0; idx < [cellList count]; idx++){
759 DBCellElement *elemnt = [cellList objectAtIndex:idx];
760 [allCelListName addObject:[elemnt getCellName]];
762 return allCelListName;
765 // -------------------------------------------------------------------------------
767 // -------------------------------------------------------------------------------
768 -(NSArray*) getUserDefaultForTokenCells {
769 return userDefaultCellArray;
772 // -------------------------------------------------------------------------------
773 // getDefaultCellName:
774 // -------------------------------------------------------------------------------
775 -(NSArray*) getDefaultForTokenCellsName {
776 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
777 for(int idx = 0; idx < [cellList count]; idx++){
778 DBCellElement *elemnt = [cellList objectAtIndex:idx];
779 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
781 return allCelListName;
784 // -------------------------------------------------------------------------------
786 // -------------------------------------------------------------------------------
787 -(NSString*) getDefaultCellName
792 // -------------------------------------------------------------------------------
793 // setDefaultCellByName:
794 // -------------------------------------------------------------------------------
795 -(void) setDefaultCellByName:(NSString*)name
797 DBCellElement *elementCell = nil;
798 BOOL cellFound = false;
801 for(int idx = 0; idx < [cellList count]; idx++) {
802 // check every cell for delete as old user default cell or selected as neww cell
803 elementCell = [cellList objectAtIndex:idx];
804 cellFound = [name compare:[elementCell getCellName]] == NSOrderedSame;
805 [elementCell setUserDefaultCell:cellFound];
807 [elementCell setUserDefaultForToken:YES];
808 if(cellName)[cellName release];
809 cellName = [name retain];
815 // -------------------------------------------------------------------------------
817 // -------------------------------------------------------------------------------
818 - (void) setCellName:(NSString*)cell {
819 [self setDefaultCellByName:cell];
822 // -------------------------------------------------------------------------------
824 // -------------------------------------------------------------------------------
825 -(void) readCellInfo:(NSString*) configFile {
826 NSError *error = nil;
827 NSString *tmpStr = nil;
828 NSString * result = [NSString stringWithContentsOfFile:configFile
829 encoding:NSASCIIStringEncoding
831 if(!result && error){
833 @throw [NSException exceptionWithName:@"readCellInfo"
834 reason:kConfFileNotExits
838 NSScanner *scanner = [NSScanner scannerWithString:result];
839 [scanner scanUpToString:@"\n"
842 // make a copy of self created string
843 cellName = [tmpStr retain];
846 // -------------------------------------------------------------------------------
848 // -------------------------------------------------------------------------------
849 -(void) readCellDB:(NSString*) configFile {
850 NSString *tmpString = nil;
851 BOOL isInCellDefaultArray = NO; // the cell belong
852 BOOL isDefaultCell = NO;
853 DBCellElement *afsCellDBElement = nil;
854 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
855 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
857 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
859 NSData *dbCellData = [fileH readDataToEndOfFile];
860 NSString *strData = [[NSString alloc] initWithData:dbCellData
861 encoding:NSASCIIStringEncoding];
862 NSScanner *cellDBScanner = [NSScanner scannerWithString:strData];
865 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
866 while([cellDBScanner isAtEnd] == NO) {
868 afsCellDBElement = [[DBCellElement alloc] init];
870 // get the name of cell
871 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
872 [afsCellDBElement setCellName: tmpString];
874 //check if this cells is one of user has selected to get token
875 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
876 //check if this cell is also the default cell
877 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
879 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
880 [afsCellDBElement setUserDefaultCell:isDefaultCell];
886 // get the cell comment
887 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
888 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
889 [afsCellDBElement setCellComment: tmpString];
892 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
893 // scann all ip in list
894 [self scanIpForCell:afsCellDBElement allIP:tmpString];
897 [cellList addObject:afsCellDBElement];
898 // release the object becasuse NSMutableArray make a retain on object
899 [afsCellDBElement release];
901 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
903 }@catch(NSException *e){
906 //if(strData) [strData release];
910 // -------------------------------------------------------------------------------
912 // -------------------------------------------------------------------------------
913 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
915 NSScanner *ipScann = [NSScanner scannerWithString:allIP];
916 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
917 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
918 NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
919 NSString *tmpString = nil;
920 while([ipScann isAtEnd] == NO){
921 CellIp *cellIpDesc = [[CellIp alloc] init];
924 [ipScann scanUpToCharactersFromSet:spaceCS
925 intoString:&tmpString];
927 [cellIpDesc setCellIp:tmpString];
928 //[tmpString release];
931 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
932 // skip comment symbol
933 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
935 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
936 [cellIpDesc setCellComment:tmpString];
937 //[tmpString release];
939 [cellElement addIpToCell:cellIpDesc];
940 // release the object becasuse NSMutableArray make a retain on object
941 [cellIpDesc release];
946 // -------------------------------------------------------------------------------
948 // -------------------------------------------------------------------------------
949 -(NSArray*) readTheseCell:(NSString*) configFile {
951 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
952 NSData *dbCellData = [fileH readDataToEndOfFile];
953 NSString *strData = [[NSString alloc] initWithData:dbCellData
954 encoding:NSASCIIStringEncoding];
956 return [strData componentsSeparatedByString:@"\n"];
959 // -------------------------------------------------------------------------------
960 // -(void) getTokenList
961 // -------------------------------------------------------------------------------
962 -(NSArray*) getTokenList
965 NSString *tokenLine = nil;
966 //NSString *tmpValue = nil;
967 NSMutableArray *tokenList = [[NSMutableArray alloc] init];
968 NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
970 if (!tokensOutput) return tokenList;
972 NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
973 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
975 while([tokenScan isAtEnd] == NO){
978 [tokenScan scanUpToCharactersFromSet:returnCS
979 intoString:&tokenLine];
981 // check for tokens end
982 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
986 // add enteir row to result
987 [tokenList addObject:tokenLine];
988 // create the line scanner for all the row that contains token info
995 // -------------------------------------------------------------------------------
996 // +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
997 // -------------------------------------------------------------------------------
998 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
1000 if(uName == @"" || uPwd == @"") return;
1002 [TaskUtil executeTaskSearchingPath:@"klog"
1003 args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
1009 // -------------------------------------------------------------------------------
1011 // -------------------------------------------------------------------------------
1012 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1013 KLStatus kstatus = noErr;
1015 // trying to ket kerberos ticket
1016 if(krb5CallEnable) {
1017 kstatus = [Krb5Util getNewTicketIfNotPresent];
1018 } else kstatus = klNoErr;
1020 //ok to launch aklog
1021 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog"
1022 args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1025 @catch (NSException * e) {
1035 // -------------------------------------------------------------------------------
1037 // -------------------------------------------------------------------------------
1038 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1040 NSString *celStrName = nil;
1041 NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1042 if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1043 //there are other cell to autenticate
1044 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1045 celStrName = [tmpDefaultArray objectAtIndex:idx];
1051 [self aklog:celStrName noKerberosCall:YES];
1055 //there is only default cell to autenticate
1061 [self aklog:nil noKerberosCall:YES];
1066 // -------------------------------------------------------------------------------
1068 // -------------------------------------------------------------------------------
1069 -(void) unlog:(NSString*)cell
1071 [TaskUtil executeTaskSearchingPath:@"unlog"
1072 args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1075 // -------------------------------------------------------------------------------
1077 // -------------------------------------------------------------------------------
1081 const char *stopArgs[] = {"stop", 0L};
1082 if([[AuthUtil shared] autorize] == noErr) {
1083 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1089 @catch (NSException * e) {
1100 // -------------------------------------------------------------------------------
1102 // -------------------------------------------------------------------------------
1106 const char *startArgs[] = {"start", 0L};
1107 if([[AuthUtil shared] autorize] == noErr) {
1108 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1114 @catch (NSException * e) {
1123 // -------------------------------------------------------------------------------
1124 // -(void) saveConfigurationFiles
1125 // -------------------------------------------------------------------------------
1126 -(void) saveConfigurationFiles:(BOOL) makeBackup
1129 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1130 NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1131 NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1132 DBCellElement *cellElement = nil;
1134 // save the configuration file
1136 [self exceptionOnInvalidPath];
1139 [filePath setString: @"/tmp/ThisCell"];
1140 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1142 encoding: NSASCIIStringEncoding
1146 for(int idx = 0; idx < [cellList count]; idx++){
1147 cellElement = [cellList objectAtIndex:idx];
1148 [cellServDBString appendString:[cellElement description]];
1149 if([cellElement userDefaultForToken]) {
1150 [theseCellString appendString:[cellElement getCellName]];
1151 [theseCellString appendString:@"\n"];
1156 [filePath setString: @"/tmp/CellServDB"];
1157 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1159 encoding: NSUTF8StringEncoding
1162 [filePath setString: @"/tmp/TheseCells"];
1163 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1165 encoding: NSUTF8StringEncoding
1168 // backup original file
1169 if([futil startAutorization] != noErr){
1170 @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization"
1175 if(makeBackup) [self backupConfigurationFiles];
1178 [filePath setString: installationPath];
1179 [filePath appendString: @"/etc/ThisCell"];
1180 [self installConfigurationFile:@"/tmp/ThisCell"
1183 // install CellServDB
1184 [filePath setString: installationPath];
1185 [filePath appendString: @"/etc/CellServDB"];
1186 [self installConfigurationFile:@"/tmp/CellServDB"
1189 // install CellServDB
1190 [filePath setString: installationPath];
1191 [filePath appendString: @"/etc/TheseCells"];
1192 [self installConfigurationFile:@"/tmp/TheseCells"
1195 } @catch (NSException *e) {
1198 // dispose all variable used
1199 if(filePath) [filePath release];
1200 if(cellServDBString) [cellServDBString release];
1205 // -------------------------------------------------------------------------------
1206 // -(void) saveCacheConfigurationFiles
1207 // -------------------------------------------------------------------------------
1208 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1210 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1211 // save the configuration file
1213 [self exceptionOnInvalidPath];
1215 // cacheinfo file creation
1216 [self writeCacheInfo:@"/tmp/cacheinfo"];
1218 //afsd.option or afs.conf file creation
1219 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1221 // backup original file
1222 if([futil startAutorization] != noErr){
1223 @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization"
1230 [self backupFile:@"/etc/cacheinfo"];
1233 [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1236 // install cacheinfo
1237 [filePath setString:installationPath];
1238 [filePath appendString: @"/etc/cacheinfo"];
1239 [self installConfigurationFile:@"/tmp/cacheinfo"
1242 // install afsd.conf or afs.conf configuration file
1243 [filePath setString: installationPath];
1244 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1245 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1248 } @catch (NSException *e) {
1251 if(filePath) [filePath release];
1257 // -------------------------------------------------------------------------------
1258 // -(void) installConfigurationFile
1259 // -------------------------------------------------------------------------------
1260 -(void) installConfigurationFile:(NSString*)srcConfFile
1261 destPath:(NSString*)destPath
1263 // delete the file original file
1265 if([futil autorizedDelete:destPath] != noErr){
1266 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete"
1272 if([futil autorizedMoveFile:srcConfFile
1273 toPath:destPath] != noErr) {
1274 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile"
1280 if([futil autorizedChown:destPath
1282 group:@"wheel"]!= noErr) {
1283 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown"
1289 // -------------------------------------------------------------------------------
1290 // -(void) backupConfigurationFiles
1291 // -------------------------------------------------------------------------------
1292 -(void) backupConfigurationFiles
1296 if([futil startAutorization] != noErr){
1297 @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization"
1302 [self backupFile:@"/etc/ThisCell"];
1305 [self backupFile:@"/etc/CellServDB"];
1308 [self backupFile:@"/etc/TheseCells"];
1310 //[futil endAutorization];
1311 } @catch (NSException *e) {
1317 // -------------------------------------------------------------------------------
1318 // -(void) backupFile:(NSString*)localAfsFilePath
1319 // -------------------------------------------------------------------------------
1320 -(void) backupFile:(NSString*)localAfsFilePath
1322 NSString *srcString = nil;
1323 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1324 OSStatus err = noErr;
1326 [filePath setString: installationPath];
1327 [filePath appendString: localAfsFilePath];
1329 //Check if the file at path exist
1330 NSFileManager *fileManager = [NSFileManager defaultManager];
1332 //check if th efile exist
1333 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1335 // store the source path
1336 srcString = [filePath stringByExpandingTildeInPath];
1337 [filePath appendString: @".afscommander_bk"];
1339 // check for presence of bk file
1340 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1342 err = [futil autorizedCopy:srcString
1343 toPath:[filePath stringByExpandingTildeInPath]];
1345 } @catch (NSException *e) {
1348 if(filePath) [filePath release];
1352 // -------------------------------------------------------------------------------
1353 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1354 // -------------------------------------------------------------------------------
1355 -(BOOL) checkAfsStatus
1358 NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1359 result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1363 // -------------------------------------------------------------------------------
1364 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1365 // -------------------------------------------------------------------------------
1366 -(BOOL) checkAfsStatusForStartup {
1368 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1369 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1373 // -------------------------------------------------------------------------------
1374 // makeChaceParamString
1375 // -------------------------------------------------------------------------------
1376 -(NSString*) makeChaceParamString
1378 NSNumber *tmpNum = nil;
1379 NSMutableString *afsdOption = [[NSMutableString alloc] init];
1380 if(!afsdOption) return @"";
1381 //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1385 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1389 if([self verbose]) {
1390 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1394 tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1395 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1398 tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1399 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1402 tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1403 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1406 tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1407 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1410 if([self dynRoot]) {
1411 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1415 [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1417 return [afsdOption autorelease];