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];}
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 -(void) 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 NSData *fileHData = [fileH readDataToEndOfFile];
363 NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
364 encoding:NSASCIIStringEncoding];
365 NSScanner *cacheInfoS = [NSScanner scannerWithString:cacheInfoStrData];
373 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
374 [self setAfsRootMountPoint:tmpString];
375 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
379 //cache path default '/var/db/openafs/cache'
380 [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
381 [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
386 [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
387 [self setCacheDimension:[tmpString intValue]];
390 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
391 }@catch(NSException *e){
394 //if(cacheInfoStrData) [cacheInfoStrData release];
395 if(chunkStartCS) [chunkStartCS release];
401 // -------------------------------------------------------------------------------
403 // -------------------------------------------------------------------------------
404 -(int) writeCacheInfo:(NSString*)filePath
406 NSNumber *tmpNum = nil;
407 NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
408 if(!cacheInfoContent) return -1;
410 //Afs root mount point
411 if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0)
412 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
413 reason:@"Bad afs path"
415 [cacheInfoContent appendString:[self afsRootMountPoint]];
417 //standard cache path
419 [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
422 tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
423 if([tmpNum intValue] == 0)
424 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"
425 reason:@"Bad cache dimension"
427 [cacheInfoContent appendString:[tmpNum stringValue]];
429 [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
431 encoding: NSASCIIStringEncoding
434 [cacheInfoContent release];
438 // -------------------------------------------------------------------------------
440 // file template "/afs:/var/db/openafs/cache:30000"
441 // -------------------------------------------------------------------------------
442 -(void) readAfsdOption:(NSString*)filePath
445 if(useAfsdConfVersion) {
446 [self readNewAfsdOption:filePath];
448 [self readOldAfsdOption:filePath];
451 }@catch(NSException *e){
459 // -------------------------------------------------------------------------------
460 // readOldAfsdOption:
461 // -------------------------------------------------------------------------------
462 -(void) readOldAfsdOption:(NSString*)filePath
465 /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
466 NSData *fileHData = [fileH readDataToEndOfFile];
467 NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
468 encoding:NSASCIIStringEncoding];*/
469 if(!filePath) return;
470 [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath
471 encoding:NSUTF8StringEncoding
472 error:nil] stringByStandardizingPath]];
475 // -------------------------------------------------------------------------------
476 // readAFSDParamLineContent:
477 // -------------------------------------------------------------------------------
478 -(void) readAFSDParamLineContent:(NSString*) paramLine{
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 -(void) readNewAfsdOption:(NSString*)filePath
539 if(!filePath) return;
540 NSString *currentLines = nil;
541 NSString *paramValue = nil;
542 NSScanner *lineScanner = nil;
545 NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath
546 encoding:NSUTF8StringEncoding
550 NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
552 //Get the lines enumerator
553 NSEnumerator *lineEnumerator = [confLines objectEnumerator];
556 while(currentLines = [lineEnumerator nextObject]) {
557 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
559 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
560 lineScanner = [NSScanner scannerWithString:currentLines];
561 if(!lineScanner) continue;
565 [lineScanner scanUpToString:@"\"" intoString:¶mValue];[lineScanner scanUpToString:@"-" intoString:¶mValue];
566 [lineScanner scanUpToString:@"\"" intoString:¶mValue];
568 // read the asfd option param line
569 [self readAFSDParamLineContent:paramValue];
570 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
572 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
574 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
581 // -------------------------------------------------------------------------------
583 // -------------------------------------------------------------------------------
584 -(int) writeAfsdOption:(NSString*)filePath
588 if(useAfsdConfVersion) {
589 result = [self writeNewAfsdOption:filePath];
591 result = [self writeOldAfsdOption:filePath];
594 }@catch(NSException *e){
602 // -------------------------------------------------------------------------------
603 // writeOldAfsdOption:
604 // -------------------------------------------------------------------------------
605 -(int) writeOldAfsdOption:(NSString*)filePath;
607 NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
609 [oldConfFileContent appendString:[self makeChaceParamString]];
611 //add cariage return at end of file
612 [oldConfFileContent appendString:@"\n"];
614 //write content on file
615 [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
617 encoding: NSASCIIStringEncoding
623 // -------------------------------------------------------------------------------
624 // writeNewAfsdOption:
627 // AFS_POST_INIT=afs_server_prefs
629 // -------------------------------------------------------------------------------
630 -(int) writeNewAfsdOption:(NSString*)filePath
632 NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
635 [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
638 [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
641 [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
644 [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
647 [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
649 //add content on file
650 [newConfFileContent appendString:@"\n"];
653 [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
655 encoding: NSASCIIStringEncoding
660 // -------------------------------------------------------------------------------
662 // -------------------------------------------------------------------------------
663 -(NSString*) getAfsVersion
665 NSString *tmpString = nil;
666 NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
667 NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
668 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
669 NSScanner *versionS = [NSScanner scannerWithString:result];
670 //go to start of version
671 [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
673 //get the total version string
674 [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
679 // -------------------------------------------------------------------------------
680 // getAfsMajorVersionVersion:
681 // -------------------------------------------------------------------------------
682 -(int) getAfsMajorVersionVersion
684 NSString *tmpString = nil;
685 NSString *totalVersion = [self getAfsVersion];
686 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
687 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
688 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
689 return [tmpString intValue];
692 // -------------------------------------------------------------------------------
693 // getAfsMinorVersionVersion:
694 // -------------------------------------------------------------------------------
695 -(int) getAfsMinorVersionVersion
697 NSString *tmpString = nil;
698 NSString *totalVersion = [self getAfsVersion];
699 NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
700 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
701 NSScanner *versionMS = [NSScanner scannerWithString:totalVersion];
702 [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
703 return [tmpString intValue];
706 // -------------------------------------------------------------------------------
707 // getAfsPatchVersionVersion:
708 // -------------------------------------------------------------------------------
709 -(int) getAfsPatchVersionVersion
711 NSString *totalVersion = [self getAfsVersion];
712 NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
713 int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS
714 options:NSBackwardsSearch].location;
715 int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
720 // -------------------------------------------------------------------------------
721 // clearConfiguration:
722 // -------------------------------------------------------------------------------
723 - (void) clearConfiguration{
725 // clear list of cell
726 [cellList removeAllObjects];
728 userDefaultCellArray= nil;
730 // remove the old cell name
737 // -------------------------------------------------------------------------------
739 // -------------------------------------------------------------------------------
740 -(NSMutableArray*) getCellList
747 // -------------------------------------------------------------------------------
749 // -------------------------------------------------------------------------------
750 -(NSArray*) getAllCellsName {
751 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
752 for(int idx = 0; idx < [cellList count]; idx++){
753 DBCellElement *elemnt = [cellList objectAtIndex:idx];
754 [allCelListName addObject:[elemnt getCellName]];
756 return allCelListName;
759 // -------------------------------------------------------------------------------
761 // -------------------------------------------------------------------------------
762 -(NSArray*) getUserDefaultForTokenCells {
763 return userDefaultCellArray;
766 // -------------------------------------------------------------------------------
767 // getDefaultCellName:
768 // -------------------------------------------------------------------------------
769 -(NSArray*) getDefaultForTokenCellsName {
770 NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
771 for(int idx = 0; idx < [cellList count]; idx++){
772 DBCellElement *elemnt = [cellList objectAtIndex:idx];
773 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
775 return allCelListName;
778 // -------------------------------------------------------------------------------
780 // -------------------------------------------------------------------------------
781 -(NSString*) getDefaultCellName
786 // -------------------------------------------------------------------------------
787 // setDefaultCellByName:
788 // -------------------------------------------------------------------------------
789 -(void) setDefaultCellByName:(NSString*)name
791 DBCellElement *elementCell = nil;
792 BOOL cellFound = false;
795 for(int idx = 0; idx < [cellList count]; idx++) {
796 // check every cell for delete as old user default cell or selected as neww cell
797 elementCell = [cellList objectAtIndex:idx];
798 cellFound = [name compare:[elementCell getCellName]] == NSOrderedSame;
799 [elementCell setUserDefaultCell:cellFound];
801 [elementCell setUserDefaultForToken:YES];
802 if(cellName)[cellName release];
803 cellName = [name retain];
809 // -------------------------------------------------------------------------------
811 // -------------------------------------------------------------------------------
812 - (void) setCellName:(NSString*)cell {
813 [self setDefaultCellByName:cell];
816 // -------------------------------------------------------------------------------
818 // -------------------------------------------------------------------------------
819 -(void) readCellInfo:(NSString*) configFile {
820 NSError *error = nil;
821 NSString *tmpStr = nil;
822 NSString * result = [NSString stringWithContentsOfFile:configFile
823 encoding:NSASCIIStringEncoding
825 if(!result && error){
827 @throw [NSException exceptionWithName:@"readCellInfo"
828 reason:kConfFileNotExits
831 NSScanner *scanner = [NSScanner scannerWithString:result];
832 [scanner scanUpToString:@"\n"
835 // make a copy of self created string
836 cellName = [tmpStr retain];
839 // -------------------------------------------------------------------------------
841 // -------------------------------------------------------------------------------
842 -(void) readCellDB:(NSString*) configFile {
843 NSString *tmpString = nil;
844 BOOL isInCellDefaultArray = NO; // the cell belong
845 BOOL isDefaultCell = NO;
846 DBCellElement *afsCellDBElement = nil;
847 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
848 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
850 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
851 NSData *dbCellData = [fileH readDataToEndOfFile];
852 NSString *strData = [[NSString alloc] initWithData:dbCellData
853 encoding:NSASCIIStringEncoding];
854 NSScanner *cellDBScanner = [NSScanner scannerWithString:strData];
857 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
858 while([cellDBScanner isAtEnd] == NO) {
860 afsCellDBElement = [[DBCellElement alloc] init];
862 // get the name of cell
863 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
864 [afsCellDBElement setCellName: tmpString];
866 //check if this cells is one of user has selected to get token
867 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
868 //check if this cell is also the default cell
869 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
871 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
872 [afsCellDBElement setUserDefaultCell:isDefaultCell];
878 // get the cell comment
879 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
880 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
881 [afsCellDBElement setCellComment: tmpString];
884 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
885 // scann all ip in list
886 [self scanIpForCell:afsCellDBElement allIP:tmpString];
889 [cellList addObject:afsCellDBElement];
890 // release the object becasuse NSMutableArray make a retain on object
891 [afsCellDBElement release];
893 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
895 }@catch(NSException *e){
898 //if(strData) [strData release];
902 // -------------------------------------------------------------------------------
904 // -------------------------------------------------------------------------------
905 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
906 NSScanner *ipScann = [NSScanner scannerWithString:allIP];
907 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
908 NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
909 NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
910 NSString *tmpString = nil;
911 while([ipScann isAtEnd] == NO){
912 CellIp *cellIpDesc = [[CellIp alloc] init];
915 [ipScann scanUpToCharactersFromSet:spaceCS
916 intoString:&tmpString];
918 [cellIpDesc setCellIp:tmpString];
919 //[tmpString release];
922 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
923 // skip comment symbol
924 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
926 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
927 [cellIpDesc setCellComment:tmpString];
928 //[tmpString release];
930 [cellElement addIpToCell:cellIpDesc];
931 // release the object becasuse NSMutableArray make a retain on object
932 [cellIpDesc release];
937 // -------------------------------------------------------------------------------
939 // -------------------------------------------------------------------------------
940 -(NSArray*) readTheseCell:(NSString*) configFile {
942 NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
943 NSData *dbCellData = [fileH readDataToEndOfFile];
944 NSString *strData = [[NSString alloc] initWithData:dbCellData
945 encoding:NSASCIIStringEncoding];
947 return [strData componentsSeparatedByString:@"\n"];
950 // -------------------------------------------------------------------------------
951 // -(void) getTokenList
952 // -------------------------------------------------------------------------------
953 -(NSArray*) getTokenList
956 NSString *tokenLine = nil;
957 //NSString *tmpValue = nil;
958 NSMutableArray *tokenList = [[NSMutableArray alloc] init];
959 NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
962 NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
963 NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
965 while([tokenScan isAtEnd] == NO){
968 [tokenScan scanUpToCharactersFromSet:returnCS
969 intoString:&tokenLine];
971 // check for tokens end
972 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
976 // add enteir row to result
977 [tokenList addObject:tokenLine];
978 // create the line scanner for all the row that contains token info
985 // -------------------------------------------------------------------------------
986 // +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
987 // -------------------------------------------------------------------------------
988 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
990 if(uName == @"" || uPwd == @"") return;
992 [TaskUtil executeTaskSearchingPath:@"klog"
993 args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
999 // -------------------------------------------------------------------------------
1001 // -------------------------------------------------------------------------------
1002 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1003 KLPrincipal princ = nil;
1004 KLStatus kstatus = noErr;
1005 char *princName = 0L;
1006 KLBoolean outFoundValidTickets = false;
1008 // trying to ket kerberos ticket
1009 if(krb5CallEnable) {
1010 kstatus = KLCacheHasValidTickets(nil, nil, &outFoundValidTickets, nil, nil);
1011 //kstatus = KLAcquireInitialTickets (0L, 0L, &princ, &princName);
1012 if(!outFoundValidTickets) {
1013 kstatus = KLAcquireNewInitialTickets(nil, nil, &princ, &princName);
1014 if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"aklog"
1015 reason:@"KLAcquireInitialTickets"
1018 } else kstatus = klNoErr;
1021 //ok to launch aklog
1022 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog"
1023 args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1026 @catch (NSException * e) {
1030 // destory the kerberos va
1031 if (kstatus == klNoErr) {
1032 KLDisposeString (princName);
1033 KLDisposePrincipal (princ);
1040 // -------------------------------------------------------------------------------
1042 // -------------------------------------------------------------------------------
1043 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1045 NSString *celStrName = nil;
1046 NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1047 if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1048 //there are other cell to autenticate
1049 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1050 celStrName = [tmpDefaultArray objectAtIndex:idx];
1056 [self aklog:celStrName noKerberosCall:YES];
1060 //there is only default cell to autenticate
1066 [self aklog:nil noKerberosCall:YES];
1071 // -------------------------------------------------------------------------------
1073 // -------------------------------------------------------------------------------
1074 -(void) unlog:(NSString*)cell
1076 [TaskUtil executeTaskSearchingPath:@"unlog"
1077 args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1080 // -------------------------------------------------------------------------------
1082 // -------------------------------------------------------------------------------
1086 const char *stopArgs[] = {"stop", 0L};
1087 if([[AuthUtil shared] autorize] == noErr) {
1088 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1094 @catch (NSException * e) {
1105 // -------------------------------------------------------------------------------
1107 // -------------------------------------------------------------------------------
1111 const char *startArgs[] = {"start", 0L};
1112 if([[AuthUtil shared] autorize] == noErr) {
1113 [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1119 @catch (NSException * e) {
1128 // -------------------------------------------------------------------------------
1129 // -(void) saveConfigurationFiles
1130 // -------------------------------------------------------------------------------
1131 -(void) saveConfigurationFiles:(BOOL) makeBackup
1134 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1135 NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1136 NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1137 DBCellElement *cellElement = nil;
1139 // save the configuration file
1141 [self exceptionOnInvalidPath];
1144 [filePath setString: @"/tmp/ThisCell"];
1145 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1147 encoding: NSASCIIStringEncoding
1151 for(int idx = 0; idx < [cellList count]; idx++){
1152 cellElement = [cellList objectAtIndex:idx];
1153 [cellServDBString appendString:[cellElement description]];
1154 if([cellElement userDefaultForToken]) {
1155 [theseCellString appendString:[cellElement getCellName]];
1156 [theseCellString appendString:@"\n"];
1161 [filePath setString: @"/tmp/CellServDB"];
1162 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1164 encoding: NSUTF8StringEncoding
1167 [filePath setString: @"/tmp/TheseCells"];
1168 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1170 encoding: NSUTF8StringEncoding
1173 // backup original file
1174 if([futil startAutorization] != noErr){
1175 @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization"
1180 if(makeBackup) [self backupConfigurationFiles];
1183 [filePath setString: installationPath];
1184 [filePath appendString: @"/etc/ThisCell"];
1185 [self installConfigurationFile:@"/tmp/ThisCell"
1188 // install CellServDB
1189 [filePath setString: installationPath];
1190 [filePath appendString: @"/etc/CellServDB"];
1191 [self installConfigurationFile:@"/tmp/CellServDB"
1194 // install CellServDB
1195 [filePath setString: installationPath];
1196 [filePath appendString: @"/etc/TheseCells"];
1197 [self installConfigurationFile:@"/tmp/TheseCells"
1200 } @catch (NSException *e) {
1203 // dispose all variable used
1204 if(filePath) [filePath release];
1205 if(cellServDBString) [cellServDBString release];
1210 // -------------------------------------------------------------------------------
1211 // -(void) saveCacheConfigurationFiles
1212 // -------------------------------------------------------------------------------
1213 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1215 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1216 // save the configuration file
1218 [self exceptionOnInvalidPath];
1220 // cacheinfo file creation
1221 [self writeCacheInfo:@"/tmp/cacheinfo"];
1223 //afsd.option or afs.conf file creation
1224 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1226 // backup original file
1227 if([futil startAutorization] != noErr){
1228 @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization"
1235 [self backupFile:@"/etc/cacheinfo"];
1238 [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1241 // install cacheinfo
1242 [filePath setString:installationPath];
1243 [filePath appendString: @"/etc/cacheinfo"];
1244 [self installConfigurationFile:@"/tmp/cacheinfo"
1247 // install afsd.conf or afs.conf configuration file
1248 [filePath setString: installationPath];
1249 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1250 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1253 } @catch (NSException *e) {
1256 if(filePath) [filePath release];
1262 // -------------------------------------------------------------------------------
1263 // -(void) installConfigurationFile
1264 // -------------------------------------------------------------------------------
1265 -(void) installConfigurationFile:(NSString*)srcConfFile
1266 destPath:(NSString*)destPath
1268 // delete the file original file
1270 if([futil autorizedDelete:destPath] != noErr){
1271 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete"
1277 if([futil autorizedMoveFile:srcConfFile
1278 toPath:destPath] != noErr) {
1279 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile"
1285 if([futil autorizedChown:destPath
1287 group:@"wheel"]!= noErr) {
1288 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown"
1294 // -------------------------------------------------------------------------------
1295 // -(void) backupConfigurationFiles
1296 // -------------------------------------------------------------------------------
1297 -(void) backupConfigurationFiles
1301 if([futil startAutorization] != noErr){
1302 @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization"
1307 [self backupFile:@"/etc/ThisCell"];
1310 [self backupFile:@"/etc/CellServDB"];
1313 [self backupFile:@"/etc/TheseCells"];
1315 //[futil endAutorization];
1316 } @catch (NSException *e) {
1322 // -------------------------------------------------------------------------------
1323 // -(void) backupFile:(NSString*)localAfsFilePath
1324 // -------------------------------------------------------------------------------
1325 -(void) backupFile:(NSString*)localAfsFilePath
1327 NSString *srcString = nil;
1328 NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1329 OSStatus err = noErr;
1331 [filePath setString: installationPath];
1332 [filePath appendString: localAfsFilePath];
1334 //Check if the file at path exist
1335 NSFileManager *fileManager = [NSFileManager defaultManager];
1337 //check if th efile exist
1338 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1340 // store the source path
1341 srcString = [filePath stringByExpandingTildeInPath];
1342 [filePath appendString: @".afscommander_bk"];
1344 // check for presence of bk file
1345 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1347 err = [futil autorizedCopy:srcString
1348 toPath:[filePath stringByExpandingTildeInPath]];
1350 } @catch (NSException *e) {
1353 if(filePath) [filePath release];
1357 // -------------------------------------------------------------------------------
1358 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1359 // -------------------------------------------------------------------------------
1360 -(BOOL) checkAfsStatus
1363 NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1364 result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1368 // -------------------------------------------------------------------------------
1369 // checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1370 // -------------------------------------------------------------------------------
1371 -(BOOL) checkAfsStatusForStartup {
1373 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1374 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1378 // -------------------------------------------------------------------------------
1379 // makeChaceParamString
1380 // -------------------------------------------------------------------------------
1381 -(NSString*) makeChaceParamString
1383 NSNumber *tmpNum = nil;
1384 NSMutableString *afsdOption = [[NSMutableString alloc] init];
1385 if(!afsdOption) return @"";
1386 //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1390 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1394 if([self verbose]) {
1395 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1399 tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1400 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1403 tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1404 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1407 tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1408 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1411 tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1412 if([tmpNum intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1415 if([self dynRoot]) {
1416 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1420 [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1422 return [afsdOption autorelease];