e96fc747b45e181d5bffa1aaa12d4acb5b018b5b
[openafs.git] / src / platform / DARWIN / AFSPreference / AFSPropertyManager.m
1 //
2 //  AFSPropertyManager.m
3 //  AFSCommander
4 //
5 //  Created by Claudio Bisegni on 21/05/07.
6 //  Copyright 2007 INFN - National Institute of Nuclear Physics. All rights reserved.
7 //
8
9 #import "Krb5Util.h"
10 #import "AFSPropertyManager.h"
11 #import "TaskUtil.h"
12
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")
20
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"
24
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"
29
30
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"
40
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"
46
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
52
53 @implementation AFSPropertyManager
54
55
56 // -------------------------------------------------------------------------------
57 //  init:
58 // -------------------------------------------------------------------------------
59 -(id) init
60 {
61         [super init];
62         cellList = [[NSMutableArray alloc] init];
63         cellName = nil;
64         userDefaultCellArray = nil;
65         useAfsdConfVersion = NO;
66         dynRoot = NO;
67         afsDB = NO;
68         verbose = NO;
69         futil = [[FileUtil alloc] init];
70         return self;
71 }
72
73 // -------------------------------------------------------------------------------
74 //  initWithAfsPath:
75 // -------------------------------------------------------------------------------
76 - (id)initWithAfsPath:(NSString*)path
77 {
78         // avvio la creazione standard dell'oggetto
79         if([self init]){
80                 // imposto il path
81                 [self setPath:path];
82         }
83         return self;
84 }
85
86 // -------------------------------------------------------------------------------
87 //  dealloc:
88 // -------------------------------------------------------------------------------
89 -(void) dealloc
90 {
91         if(installationPath){ [installationPath release];}
92         if(cellList) {[cellList removeAllObjects];[cellList release];}
93         if(cellName) {[cellName release];}
94         if(futil) {
95                 [futil release];
96                 futil = nil; 
97         }
98         [super dealloc];
99 }
100
101 // -------------------------------------------------------------------------------
102 //  setPath:
103 // -------------------------------------------------------------------------------
104 - (void) setPath:(NSString*)path {
105         if(installationPath)[installationPath release];
106         
107         if(path)
108                 installationPath = [path retain];
109 }
110
111 // -------------------------------------------------------------------------------
112 //  path:
113 // -------------------------------------------------------------------------------
114 - (NSString*) path{
115         return installationPath;
116 }
117
118 // -------------------------------------------------------------------------------
119 //  statCacheEntry:
120 // -------------------------------------------------------------------------------
121 -(int) statCacheEntry
122 {
123         return statCacheEntry;
124 }
125
126 // -------------------------------------------------------------------------------
127 //  setStatCacheEntry:
128 // -------------------------------------------------------------------------------
129 -(void) setStatCacheEntry:(int)statEntry
130 {
131         statCacheEntry = statEntry;
132 }
133
134
135 // -------------------------------------------------------------------------------
136 //  dCacheDim:
137 // -------------------------------------------------------------------------------
138 -(int) dCacheDim
139 {
140         return dCacheDim;
141 }
142
143 // -------------------------------------------------------------------------------
144 //  setDCacheDim:
145 // -------------------------------------------------------------------------------
146 -(void) setDCacheDim:(int)dcDim
147 {
148         dCacheDim = dcDim;
149 }
150
151 // -------------------------------------------------------------------------------
152 //  cacheDimension:
153 // -------------------------------------------------------------------------------
154 -(int) cacheDimension
155 {
156         return cacheDimension;
157 }
158
159 // -------------------------------------------------------------------------------
160 //  setCacheDimension:
161 // -------------------------------------------------------------------------------
162 -(void) setCacheDimension:(int)cacheDim
163 {
164         cacheDimension = cacheDim;
165 }
166
167 // -------------------------------------------------------------------------------
168 //  daemonNumber:
169 // -------------------------------------------------------------------------------
170 -(int) daemonNumber
171 {
172         return daemonNumber;
173 }
174
175 // -------------------------------------------------------------------------------
176 //  setDaemonNumber:
177 // -------------------------------------------------------------------------------
178 -(void) setDaemonNumber:(int)dNumber
179 {
180         daemonNumber = dNumber;
181 }
182
183 // -------------------------------------------------------------------------------
184 //  afsRootMountPoint:
185 // -------------------------------------------------------------------------------
186 -(NSString*) afsRootMountPoint
187 {
188         return afsRootMountPoint;
189 }
190
191 // -------------------------------------------------------------------------------
192 //  setAfsRootMountPoint:
193 // -------------------------------------------------------------------------------
194 -(void) setAfsRootMountPoint:(NSString*)mountPoint
195 {
196         if(afsRootMountPoint)[afsRootMountPoint release];
197         
198         if(mountPoint)
199                 afsRootMountPoint = [mountPoint retain];
200 }
201
202 // -------------------------------------------------------------------------------
203 //  nVolEntry:
204 // -------------------------------------------------------------------------------
205 -(int) nVolEntry
206 {
207         return nVolEntry;
208 }
209
210 // -------------------------------------------------------------------------------
211 //  setNVolEntry:
212 // -------------------------------------------------------------------------------
213 -(void) setNVolEntry:(int)entry
214 {
215         nVolEntry = entry;
216 }
217
218 // -------------------------------------------------------------------------------
219 //  dynRoot:
220 // -------------------------------------------------------------------------------
221 -(bool) dynRoot
222 {
223         return dynRoot;
224 }
225
226 // -------------------------------------------------------------------------------
227 //  setDynRoot:
228 // -------------------------------------------------------------------------------
229 -(void) setDynRoot:(bool)flag
230 {
231         dynRoot = flag;
232 }
233
234 // -------------------------------------------------------------------------------
235 //  dynRoot:
236 // -------------------------------------------------------------------------------
237 -(bool) afsDB
238 {
239         return afsDB;
240 }
241
242 // -------------------------------------------------------------------------------
243 //  setDynRoot:
244 // -------------------------------------------------------------------------------
245 -(void) setAfsDB:(bool)flag
246 {
247         afsDB = flag;
248 }
249
250 // -------------------------------------------------------------------------------
251 //  setDynRoot:
252 // -------------------------------------------------------------------------------
253 -(bool) verbose
254 {
255         return verbose;
256 }
257
258 // -------------------------------------------------------------------------------
259 //  setDynRoot:
260 // -------------------------------------------------------------------------------
261 -(void) setVerbose:(bool)flag
262 {
263         verbose = flag;
264 }
265
266 // -------------------------------------------------------------------------------
267 //  useAfsdConfVersion:
268 // -------------------------------------------------------------------------------
269 -(BOOL) useAfsdConfConfigFile
270 {
271         return useAfsdConfVersion;
272 }
273
274 // -------------------------------------------------------------------------------
275 //  exceptionOnInvalidPath:
276 // -------------------------------------------------------------------------------
277 - (void) exceptionOnInvalidPath
278 {
279         if(!installationPath || ([installationPath length] == 0)) {
280                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
281                                                                            reason:kPathNotEmpty 
282                                                                          userInfo:nil];                 
283         }
284         if(![[NSFileManager defaultManager] fileExistsAtPath:installationPath]){
285                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
286                                                                            reason:kPathDontContainAfsInstallation
287                                                                          userInfo:nil];                 
288         }
289         
290 }
291
292 // -------------------------------------------------------------------------------
293 //  loadConfiguration:
294 // -------------------------------------------------------------------------------
295 - (void) loadConfiguration {
296         int mjVersion = [self getAfsMajorVersionVersion];
297         int miVersion = [self getAfsMinorVersionVersion];
298         int paVersion = [self getAfsPatchVersionVersion];
299         
300         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
301         @try{
302                 
303                 [self exceptionOnInvalidPath];
304                 [self clearConfiguration];
305                 
306                 //chech the afs version for chioce wich afsd conf file usage
307                 useAfsdConfVersion = mjVersion >= 1 && miVersion==4 && paVersion>=7;
308                 useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion==5 && paVersion>=31);
309                 useAfsdConfVersion = useAfsdConfVersion || (mjVersion >= 1 && miVersion>=6);
310                 
311                 // read thiscell config file
312                 [filePath setString:installationPath];
313                 [filePath appendString: @"/etc/ThisCell"];
314
315                 [self readCellInfo:filePath];
316                 if(!cellName){
317                         @throw [NSException exceptionWithName:@"readCellInfo" 
318                                                                         reason:kThisCellFOError 
319                                                                   userInfo:nil];
320                 }
321                 //read TheseCell file
322                 [filePath setString: installationPath];
323                 [filePath appendString: @"/etc/TheseCells"];
324                 userDefaultCellArray = [self readTheseCell:filePath];
325                 
326                 //read cell serv db
327                 [filePath setString: installationPath];
328                 [filePath appendString: @"/etc/CellServDB"];
329                 [self readCellDB:filePath];
330                 
331                 //Read cacheinfo
332                 [filePath setString: installationPath];
333                 [filePath appendString: @"/etc/cacheinfo"];
334                 [self readCacheInfo:filePath];
335                 
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){
341                 @throw e;
342         } @finally {
343                 [filePath release];
344         }
345 }
346
347 // -------------------------------------------------------------------------------
348 //  readCacheInfo:
349 //  file template "/afs:/var/db/openafs/cache:30000"
350 // -------------------------------------------------------------------------------
351 -(int) readCacheInfo:(NSString*)filePath
352 {
353         int cicle = 0;
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:@"/"]];
359         
360         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
361         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
362         if (!fileH) return 0;
363         NSData *fileHData = [fileH readDataToEndOfFile];
364         NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
365                                                                                                            encoding:NSASCIIStringEncoding];
366         NSScanner *cacheInfoS = [NSScanner  scannerWithString:cacheInfoStrData];
367         
368         @try{
369                 do {
370                         cicle++;
371                         switch(cicle){
372                                 case 1:
373                                         // afs mount path
374                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
375                                         [self setAfsRootMountPoint:tmpString];
376                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
377                                         break;
378                                         
379                                 case 2:
380                                         //cache path default '/var/db/openafs/cache'
381                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
382                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
383                                         break;
384                                 
385                                 case 3:
386                                         // cache dimension
387                                         [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
388                                         [self setCacheDimension:[tmpString intValue]];
389                                         break;
390                         }
391                 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
392         }@catch(NSException *e){
393                 @throw e;
394         }@finally{
395                 //if(cacheInfoStrData) [cacheInfoStrData release];
396                 if(chunkStartCS) [chunkStartCS release];
397         }
398         return noErr;
399 }
400
401
402 // -------------------------------------------------------------------------------
403 //  writeCacheInfo:
404 // -------------------------------------------------------------------------------
405 -(int) writeCacheInfo:(NSString*)filePath
406 {
407         NSNumber *tmpNum = nil;
408         NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
409         if(!cacheInfoContent) return -1;
410         
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" 
415                                                                          userInfo:nil];
416         [cacheInfoContent appendString:[self afsRootMountPoint]];
417         
418         //standard cache path
419         
420         [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
421         
422         //cache dimension
423         tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
424         if([tmpNum intValue] == 0)
425                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
426                                                                            reason:@"Bad cache dimension" 
427                                                                          userInfo:nil];
428         [cacheInfoContent appendString:[tmpNum stringValue]];
429         
430         [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
431                                            atomically: YES 
432                                                  encoding: NSASCIIStringEncoding 
433                                                         error: nil];
434         
435         [cacheInfoContent release];
436         return noErr;
437 }
438
439 // -------------------------------------------------------------------------------
440 //  readCacheInfo:
441 //  file template "/afs:/var/db/openafs/cache:30000"
442 // -------------------------------------------------------------------------------
443 -(void) readAfsdOption:(NSString*)filePath
444 {
445         @try{
446                 if(useAfsdConfVersion) {
447                     if ([self readNewAfsdOption:filePath] != noErr)
448                         [self readOldAfsdOption:filePath];
449                 } else {
450                     if ([self readOldAfsdOption:filePath] != noErr)
451                         [self readNewAfsdOption:filePath];
452                 }
453         
454         }@catch(NSException *e){
455                 @throw e;
456         }@finally{
457                 
458         }
459         
460 }
461
462 // -------------------------------------------------------------------------------
463 //  readOldAfsdOption:
464 // -------------------------------------------------------------------------------
465 -(int) readOldAfsdOption:(NSString*)filePath
466 {
467         if(!filePath) return 0;
468         return [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath 
469                                                                                                                           encoding:NSUTF8StringEncoding 
470                                                                                                                                  error:nil] stringByStandardizingPath]];
471 }
472
473 // -------------------------------------------------------------------------------
474 //  readAFSDParamLineContent:
475 // -------------------------------------------------------------------------------
476 -(int) readAFSDParamLineContent:(NSString*) paramLine{
477         if (!paramLine) return 0;
478
479         NSString *tmpString = nil;
480         NSCharacterSet *space = [NSCharacterSet characterSetWithCharactersInString:@" "];
481         NSScanner *afsdOptionS = [NSScanner  scannerWithString:paramLine];
482         
483         do{
484                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
485                 if(!tmpString) continue;
486                 //check parameter type
487                 if([tmpString isEqualToString:@AFSD_OPTION_DAEMONS_KEY])
488                 {
489                         // get number of daemon
490                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
491                         [self setDaemonNumber:[tmpString intValue]];
492                 } else                          
493                         //check parameter type
494                         if([tmpString isEqualToString:@AFSD_OPTION_DCACHE_KEY])
495                         {
496                                 //get dcache dimension
497                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
498                                 [self setDCacheDim:[tmpString intValue]];
499                         } else 
500                                 //check parameter type
501                                 if([tmpString isEqualToString:@AFSD_OPTION_DYNROOT_KEY])
502                                 {
503                                         [self setDynRoot:true];
504                                 } else 
505                                         if([tmpString isEqualToString:@AFSD_OPTION_VERBOSE_KEY])
506                                         {
507                                                 [self setVerbose:true];
508                                         } else if([tmpString isEqualToString:@AFSD_OPTION_AFSDB_KEY])
509                                         {
510                                                 [self setAfsDB:true];
511                                         } else
512                                                 //check parameter type
513                                                 if([tmpString isEqualToString:@AFSD_OPTION_STAT_KEY])
514                                                 {
515                                                         // get fstat entry num
516                                                         [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
517                                                         [self setStatCacheEntry:[tmpString intValue]];
518                                                 } else
519                                                         
520                                                         //check parameter type
521                                                         if([tmpString isEqualToString:@AFSD_OPTION_VOLUMES_KEY])
522                                                         {
523                                                                 // get fstat entry num
524                                                                 [afsdOptionS scanUpToCharactersFromSet:space intoString:&tmpString];
525                                                                 [self setNVolEntry:[tmpString intValue]];
526                                                         }
527                 
528                 
529                 
530         }while(![afsdOptionS isAtEnd]);
531         return noErr;
532 }
533
534 // -------------------------------------------------------------------------------
535 //  readNewAfsdOption:
536 // -------------------------------------------------------------------------------
537 -(int) readNewAfsdOption:(NSString*)filePath
538 {
539         if(!filePath) return 0;
540         NSString *currentLines = 0;
541         NSString *paramValue = 0;
542         NSScanner *lineScanner = 0;
543         
544         //Get file content
545         NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath 
546                                                                                                                          encoding:NSUTF8StringEncoding 
547                                                                                                                                 error:nil];
548         if (!newAFSDConfContent) return 0;
549         
550         //get lines in array
551         NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
552         
553         //Get the lines enumerator
554         NSEnumerator *lineEnumerator = [confLines objectEnumerator];
555         
556         //scann all lines
557         while(currentLines = [lineEnumerator nextObject]) {
558                 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
559                         
560                 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
561                         lineScanner = [NSScanner scannerWithString:currentLines];
562                         if(!lineScanner) continue;
563                         
564                         //scann the line
565                         
566                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];[lineScanner scanUpToString:@"-" intoString:&paramValue];
567                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];
568                         
569                         // read the asfd option param line
570                         [self readAFSDParamLineContent:paramValue];
571                 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
572                         
573                 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
574                         
575                 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
576                         
577                 }
578         }
579         return noErr;
580 }
581
582
583 // -------------------------------------------------------------------------------
584 //  writeCacheInfo:
585 // -------------------------------------------------------------------------------
586 -(int) writeAfsdOption:(NSString*)filePath
587 {
588         int result = 0;
589         @try{
590                 if(useAfsdConfVersion) {
591                         result = [self writeNewAfsdOption:filePath];                    
592                 } else {
593                         result = [self writeOldAfsdOption:filePath];
594                 }
595                 
596         }@catch(NSException *e){
597                 @throw e;
598         }@finally{
599                 
600         }
601         return result;
602 }
603
604 // -------------------------------------------------------------------------------
605 //  writeOldAfsdOption:
606 // -------------------------------------------------------------------------------
607 -(int) writeOldAfsdOption:(NSString*)filePath;
608 {
609         NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
610         //add afsd param
611         [oldConfFileContent appendString:[self makeChaceParamString]];
612         
613         //add cariage return at end of file
614         [oldConfFileContent appendString:@"\n"];
615         
616         //write content on file
617         [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
618                                                  atomically: YES 
619                                                    encoding: NSASCIIStringEncoding 
620                                                           error: nil];
621         return noErr;
622         
623 }
624
625 // -------------------------------------------------------------------------------
626 //  writeNewAfsdOption:
627 //  OPTIONS=
628 //  AFS_SYSNAME=
629 //  AFS_POST_INIT=afs_server_prefs
630 //  AFS_PRE_SHUTDOWN=
631 // -------------------------------------------------------------------------------
632 -(int) writeNewAfsdOption:(NSString*)filePath
633 {
634         NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
635         
636         //Verbose
637         [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
638         
639         //AFSD Option
640         [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"]; 
641         
642         //AFS_SYSNAME
643         [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
644         
645         //AFS_POST_INIT
646         [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"]; 
647         
648         //AFS_PRE_SHUTDOWN
649         [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
650         
651         //add content on file
652         [newConfFileContent appendString:@"\n"];
653         
654         //Write to file
655         [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
656                                                  atomically: YES 
657                                                    encoding: NSASCIIStringEncoding 
658                                                           error: nil];
659         return noErr;
660 }
661
662 // -------------------------------------------------------------------------------
663 //  getAfsVersion:
664 // -------------------------------------------------------------------------------
665 -(NSString*) getAfsVersion
666 {
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];
675         
676         //get the total version string
677         [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
678
679         return tmpString;
680 }
681
682 // -------------------------------------------------------------------------------
683 //  getAfsMajorVersionVersion:
684 // -------------------------------------------------------------------------------
685 -(int) getAfsMajorVersionVersion
686 {
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];
694 }
695
696 // -------------------------------------------------------------------------------
697 //  getAfsMinorVersionVersion:
698 // -------------------------------------------------------------------------------
699 -(int) getAfsMinorVersionVersion
700 {
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];
709 }
710
711 // -------------------------------------------------------------------------------
712 //  getAfsPatchVersionVersion:
713 // -------------------------------------------------------------------------------
714 -(int) getAfsPatchVersionVersion
715 {
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];
722         return patchVersion;
723 }
724
725
726 // -------------------------------------------------------------------------------
727 //  clearConfiguration:
728 // -------------------------------------------------------------------------------
729 - (void) clearConfiguration{
730         
731         // clear list of cell
732         [cellList removeAllObjects];
733         
734         userDefaultCellArray= nil;
735         
736         // remove the old cell name
737         if(cellName) {
738                 [cellName release];
739                 cellName = nil;
740         }
741 }
742
743 // -------------------------------------------------------------------------------
744 //  getCellList:
745 // -------------------------------------------------------------------------------
746 -(NSMutableArray*) getCellList
747 {
748         return cellList;
749 }
750
751
752
753 // -------------------------------------------------------------------------------
754 //  getAllCellName:
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]];
761         }
762         return allCelListName;
763 }
764
765 // -------------------------------------------------------------------------------
766 //  getCellList:
767 // -------------------------------------------------------------------------------
768 -(NSArray*) getUserDefaultForTokenCells {
769         return userDefaultCellArray;
770 }
771
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]];
780         }
781         return allCelListName;
782 }
783
784 // -------------------------------------------------------------------------------
785 //  getCellName:
786 // -------------------------------------------------------------------------------
787 -(NSString*) getDefaultCellName
788 {
789         return cellName;
790 }
791
792 // -------------------------------------------------------------------------------
793 //  setDefaultCellByName:
794 // -------------------------------------------------------------------------------
795 -(void) setDefaultCellByName:(NSString*)name
796 {
797         DBCellElement *elementCell = nil;
798         BOOL cellFound = false;
799         if(!name) return;
800         
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];
806                 if(cellFound) {
807                         [elementCell setUserDefaultForToken:YES];
808                         if(cellName)[cellName release];
809                         cellName = [name retain];
810                  }
811         }
812         
813 }
814
815 // -------------------------------------------------------------------------------
816 //  setCellName:
817 // -------------------------------------------------------------------------------
818 - (void) setCellName:(NSString*)cell {
819         [self setDefaultCellByName:cell];
820 }
821
822 // -------------------------------------------------------------------------------
823 //  readCellInfo:
824 // -------------------------------------------------------------------------------
825 -(void) readCellInfo:(NSString*) configFile {
826         NSError *error = nil;
827         NSString *tmpStr = nil;
828         NSString * result = [NSString stringWithContentsOfFile:configFile
829                                                                                                   encoding:NSASCIIStringEncoding
830                                                                                                          error:&error];
831         if(!result && error){
832                 if([error domain] )
833                 @throw [NSException exceptionWithName:@"readCellInfo" 
834                                                                            reason:kConfFileNotExits
835                                                                          userInfo:nil];
836         } else if (!result)
837             return;
838         NSScanner *scanner = [NSScanner scannerWithString:result];
839         [scanner scanUpToString:@"\n" 
840                                  intoString:&tmpStr];
841         
842         // make a copy of self created string
843         cellName = [tmpStr retain];
844 }
845
846 // -------------------------------------------------------------------------------
847 //  readCellDB:
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"];
856         
857         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
858         if (!fileH) return;
859         NSData *dbCellData = [fileH readDataToEndOfFile];
860         NSString *strData = [[NSString alloc] initWithData:dbCellData
861                                                                                           encoding:NSASCIIStringEncoding];
862         NSScanner *cellDBScanner = [NSScanner  scannerWithString:strData];
863         
864         @try{
865         [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
866         while([cellDBScanner isAtEnd] == NO) {
867                 // make new cell
868                 afsCellDBElement = [[DBCellElement alloc] init];
869                 
870                 // get the name of cell
871                 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
872                 [afsCellDBElement setCellName: tmpString];
873                 
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;
878                 
879                 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
880                 [afsCellDBElement setUserDefaultCell:isDefaultCell];
881                 
882                 
883         
884                 
885                 
886                 // get the cell comment
887                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
888                 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
889                 [afsCellDBElement setCellComment: tmpString];
890
891                 // get all ip
892                 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
893                 // scann all ip in list
894                 [self scanIpForCell:afsCellDBElement allIP:tmpString];  
895                 
896                 // add cell to list
897                 [cellList addObject:afsCellDBElement];
898                 // release the object becasuse NSMutableArray make a retain on object
899                 [afsCellDBElement release];
900                 //repeat
901                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
902         }
903         }@catch(NSException *e){
904                 @throw e;
905         }@finally{
906                 //if(strData) [strData release];
907         }
908 }
909
910 // -------------------------------------------------------------------------------
911 //  scanIpForCell:
912 // -------------------------------------------------------------------------------
913 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
914     if (!allIP) return;
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];
922                 
923                 //ip string
924                 [ipScann scanUpToCharactersFromSet:spaceCS 
925                                                                 intoString:&tmpString];
926                 
927                 [cellIpDesc setCellIp:tmpString];
928                 //[tmpString release];
929                 
930                 // go to comment
931                 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
932                 // skip comment symbol
933                 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
934                 // get comment
935                 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
936                 [cellIpDesc setCellComment:tmpString];
937                 //[tmpString release];
938                 
939                 [cellElement addIpToCell:cellIpDesc];
940                 // release the object becasuse NSMutableArray make a retain on object
941                 [cellIpDesc release];
942         }
943 }
944
945
946 // -------------------------------------------------------------------------------
947 //  scanIpForCell:
948 // -------------------------------------------------------------------------------
949 -(NSArray*) readTheseCell:(NSString*) configFile {
950         
951         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
952         NSData *dbCellData = [fileH readDataToEndOfFile];
953         NSString *strData = [[NSString alloc] initWithData:dbCellData
954                                                                                           encoding:NSASCIIStringEncoding];
955         
956         return [strData componentsSeparatedByString:@"\n"];
957 }
958
959 // -------------------------------------------------------------------------------
960 //  -(void) getTokenList
961 // -------------------------------------------------------------------------------
962 -(NSArray*) getTokenList
963 {
964         int line = 0;
965         NSString *tokenLine = nil;
966         //NSString *tmpValue = nil;
967         NSMutableArray *tokenList = [[NSMutableArray alloc] init];
968         NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
969         
970         if (!tokensOutput) return tokenList;
971         // scan the tokens
972         NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
973         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
974         
975         while([tokenScan isAtEnd] == NO){
976                 line++;
977                 // get the next line
978                 [tokenScan scanUpToCharactersFromSet:returnCS 
979                                                                 intoString:&tokenLine];
980                 
981                 // check for tokens end
982                 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
983                 
984                 
985                 if(line >= 2){
986                         // add enteir row to result
987                         [tokenList addObject:tokenLine];
988                         // create the line scanner for all the row that contains token info
989                 }
990         }
991         //
992         return tokenList;
993 }
994
995 // -------------------------------------------------------------------------------
996 //  +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
997 // -------------------------------------------------------------------------------
998 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
999 {
1000         if(uName == @"" ||  uPwd == @"") return;
1001         
1002         [TaskUtil executeTaskSearchingPath:@"klog" 
1003                                                                   args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
1004         
1005         
1006 }
1007
1008
1009 // -------------------------------------------------------------------------------
1010 //  +(void) aklog
1011 // -------------------------------------------------------------------------------
1012 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1013         KLStatus                kstatus = noErr;
1014         @try {
1015                 // trying to ket kerberos ticket
1016                 if(krb5CallEnable) {
1017                         kstatus = [Krb5Util getNewTicketIfNotPresent];
1018                 } else kstatus = klNoErr;
1019                 
1020                 //ok to launch aklog
1021                 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
1022                                                                                                                          args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1023                 
1024         }
1025         @catch (NSException * e) {
1026                 @throw e;
1027         }
1028         @finally {
1029
1030         }
1031                 
1032 }
1033
1034
1035 // -------------------------------------------------------------------------------
1036 //  getTokens:
1037 // -------------------------------------------------------------------------------
1038 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1039         
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];
1046                         if(klogAklogFlag)
1047                                 [self klog:usr 
1048                                           uPwd:pwd  
1049                                           cell:celStrName];
1050                         else
1051                                 [self aklog:celStrName noKerberosCall:YES];     
1052                 }
1053                 
1054         } else {
1055                 //there is only default cell to autenticate
1056                 if(klogAklogFlag)
1057                         [self klog:usr 
1058                                   uPwd:pwd 
1059                                   cell:nil];
1060                 else
1061                         [self aklog:nil noKerberosCall:YES];
1062         }
1063         
1064 }
1065
1066 // -------------------------------------------------------------------------------
1067 //  +(void) unlog
1068 // -------------------------------------------------------------------------------
1069 -(void) unlog:(NSString*)cell
1070 {
1071         [TaskUtil executeTaskSearchingPath:@"unlog" 
1072                                                                   args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1073 }
1074
1075 // -------------------------------------------------------------------------------
1076 //  -(void) shutdown
1077 // -------------------------------------------------------------------------------
1078 -(void) shutdown
1079 {
1080   NSString *rootHelperApp = [[NSBundle bundleForClass:[self class]] pathForResource:@"afshlp" ofType:@""];
1081     @try {
1082         const char *stopArgs[] = {AFS_DAEMON_STARTUPSCRIPT, "stop", 0L};
1083         [[AuthUtil shared] execUnixCommand:[rootHelperApp fileSystemRepresentation]
1084                            args:stopArgs
1085                            output:nil];
1086     }
1087     @catch (NSException * e) {
1088         @throw e;
1089     }
1090     @finally {
1091     }
1092 }
1093
1094
1095 // -------------------------------------------------------------------------------
1096 //  -(void) shutdown
1097 // -------------------------------------------------------------------------------
1098 -(void) startup
1099 {
1100   NSString *rootHelperApp = [[NSBundle bundleForClass:[self class]] pathForResource:@"afshlp" ofType:@""];
1101     @try {
1102         const char *startArgs[] = {AFS_DAEMON_STARTUPSCRIPT, "start", 0L};
1103         [[AuthUtil shared] execUnixCommand:[rootHelperApp fileSystemRepresentation]
1104                            args:startArgs
1105                            output:nil];
1106     }
1107     @catch (NSException * e) {
1108         @throw e;
1109     }
1110     @finally {
1111     }
1112 }
1113
1114 // -------------------------------------------------------------------------------
1115 //  -(void) saveConfigurationFiles
1116 // -------------------------------------------------------------------------------
1117 -(void) saveConfigurationFiles:(BOOL) makeBackup
1118 {
1119         NSError *err;
1120         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1121         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1122         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1123         DBCellElement *cellElement = nil;
1124         
1125         // save the configuration file
1126         @try{
1127                 [self exceptionOnInvalidPath];
1128
1129                 // ThisCell
1130                 [filePath setString: @"/tmp/ThisCell"];
1131                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1132                                    atomically:YES 
1133                                          encoding: NSASCIIStringEncoding 
1134                                                 error:nil];
1135                 // CellServDB
1136                 
1137                 for(int idx = 0; idx < [cellList count]; idx++){
1138                         cellElement = [cellList objectAtIndex:idx];
1139                         [cellServDBString appendString:[cellElement description]];
1140                         if([cellElement userDefaultForToken]) {
1141                                 [theseCellString  appendString:[cellElement getCellName]];
1142                                 [theseCellString  appendString:@"\n"];
1143                         }
1144                 }
1145                 
1146                 
1147                 [filePath setString: @"/tmp/CellServDB"];
1148                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1149                                                    atomically:YES 
1150                                                          encoding:  NSUTF8StringEncoding 
1151                                                                 error:&err];
1152                 
1153                 [filePath setString: @"/tmp/TheseCells"];
1154                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1155                                                    atomically:YES 
1156                                                          encoding:  NSUTF8StringEncoding 
1157                                                                 error:&err];
1158                 
1159                 if(makeBackup) [self backupConfigurationFiles];
1160
1161                 // install ThisCell
1162                 [filePath setString: installationPath];
1163                 [filePath appendString: @"/etc/ThisCell"];
1164                 [self installConfigurationFile:@"/tmp/ThisCell" 
1165                                                           destPath:filePath];           
1166                 
1167                 // install CellServDB
1168                 [filePath setString: installationPath];
1169                 [filePath appendString: @"/etc/CellServDB"];
1170                 [self installConfigurationFile:@"/tmp/CellServDB" 
1171                                                           destPath:filePath];
1172                 
1173                 // install CellServDB
1174                 [filePath setString: installationPath];
1175                 [filePath appendString: @"/etc/TheseCells"];
1176                 [self installConfigurationFile:@"/tmp/TheseCells" 
1177                                                           destPath:filePath];
1178                 
1179         } @catch (NSException *e) {
1180                 @throw e;
1181         }@finally {
1182                 // dispose all variable used
1183                 if(filePath) [filePath release];
1184                 if(cellServDBString) [cellServDBString release];
1185         }
1186         
1187 }
1188
1189 // -------------------------------------------------------------------------------
1190 //  -(void) saveCacheConfigurationFiles
1191 // -------------------------------------------------------------------------------
1192 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1193 {
1194         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1195         // save the configuration file
1196         @try{
1197                 [self exceptionOnInvalidPath];
1198                 
1199                 // cacheinfo file creation
1200                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1201                 
1202                 //afsd.option or afs.conf file creation
1203                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1204                 
1205                 // backup original file
1206                 if(makeBackup) {
1207                         //cacheinfo
1208                         [self backupFile:@"/etc/cacheinfo"];
1209                         
1210                         //afsd.options
1211                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1212                 }
1213                 
1214                 // install cacheinfo
1215                 [filePath setString:installationPath];
1216                 [filePath appendString: @"/etc/cacheinfo"];
1217                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1218                                                           destPath:filePath];           
1219                 
1220                 // install afsd.conf or afs.conf configuration file 
1221                 [filePath setString: installationPath];
1222                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1223                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1224                                                           destPath:filePath];
1225                 
1226         } @catch (NSException *e) {
1227                 @throw e;
1228         }@finally {
1229                 if(filePath) [filePath release];
1230         }
1231         
1232         
1233 }
1234
1235 // -------------------------------------------------------------------------------
1236 //  -(void) installConfigurationFile
1237 // -------------------------------------------------------------------------------
1238 -(void) installConfigurationFile:(NSString*)srcConfFile 
1239                                                 destPath:(NSString*)destPath
1240 {
1241         // delete the file original file
1242
1243         if([futil autorizedDelete:destPath] != noErr){
1244                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1245                                                                            reason:destPath
1246                                                                          userInfo:nil];
1247         }
1248         
1249         // move the file
1250         if([futil autorizedMoveFile:srcConfFile
1251                                                  toPath:destPath] != noErr) {
1252                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1253                                                                            reason:srcConfFile
1254                                                                          userInfo:nil];
1255         }
1256         
1257         
1258         if([futil autorizedChown:destPath 
1259                                            owner:@"root"
1260                                            group:@"wheel"]!= noErr) {
1261                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1262                                                                            reason:destPath
1263                                                                          userInfo:nil];
1264         }
1265 }
1266
1267 // -------------------------------------------------------------------------------
1268 //  -(void) backupConfigurationFiles
1269 // -------------------------------------------------------------------------------
1270 -(void) backupConfigurationFiles
1271 {       
1272
1273         @try{
1274                 //This cell
1275                 [self backupFile:@"/etc/ThisCell"];
1276         
1277                 //CellServDB
1278                 [self backupFile:@"/etc/CellServDB"];
1279                 
1280                 //TheseCell
1281                 [self backupFile:@"/etc/TheseCells"];
1282                 
1283         } @catch (NSException *e) {
1284                 @throw e;
1285         } @finally {
1286         }
1287 }
1288
1289 // -------------------------------------------------------------------------------
1290 //  -(void) backupFile:(NSString*)localAfsFilePath
1291 // -------------------------------------------------------------------------------
1292 -(void) backupFile:(NSString*)localAfsFilePath
1293 {
1294         NSString *srcString = nil;
1295         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1296         OSStatus err = noErr;
1297         @try{
1298                 [filePath setString: installationPath];
1299                 [filePath appendString: localAfsFilePath];
1300                 
1301                 //Check if the file at path exist
1302                 NSFileManager *fileManager = [NSFileManager defaultManager];
1303                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1304                 
1305                 // store the source path
1306                 srcString  = [filePath stringByExpandingTildeInPath];
1307                 [filePath appendString: @".afscommander_bk"];
1308                 
1309                 // check for presence of bk file
1310                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1311                         // backup the file
1312                         err = [futil autorizedCopy:srcString 
1313                                                   toPath:[filePath stringByExpandingTildeInPath]];
1314                 }
1315         } @catch (NSException *e) {
1316                 @throw e;
1317         } @finally {
1318                 if(filePath) [filePath release];
1319         }
1320 }
1321
1322 // -------------------------------------------------------------------------------
1323 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1324 // -------------------------------------------------------------------------------
1325 -(BOOL) checkAfsStatus
1326 {
1327         BOOL result = NO;
1328         NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1329         result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1330         return result;  
1331 }
1332
1333 // -------------------------------------------------------------------------------
1334 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1335 // -------------------------------------------------------------------------------
1336 -(BOOL) checkAfsStatusForStartup {
1337         BOOL result = NO;
1338                 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1339                 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1340         return result;
1341 }
1342
1343 // -------------------------------------------------------------------------------
1344 //  makeChaceParamString
1345 // -------------------------------------------------------------------------------
1346 -(NSString*) makeChaceParamString
1347 {
1348         NSNumber *tmpNum = nil;
1349         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1350         if(!afsdOption) return @"";
1351         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1352         //afsdb
1353         //dynRoot
1354         if([self afsDB]) {
1355                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1356         }
1357         
1358         //Verbose
1359         if([self verbose]) {
1360                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1361         }
1362         
1363         //stat entry
1364         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1365         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1366         
1367         //dcace
1368         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1369         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1370         
1371         //daemons
1372         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1373         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1374         
1375         //volumes
1376         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1377         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1378         
1379         //dynRoot
1380         if([self dynRoot]) {
1381                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1382         }
1383         
1384         //fakestat-all
1385         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1386         
1387         return [afsdOption autorelease];
1388 }
1389
1390 @end
1391