macos prefs pane improved error handling
[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 endAutorization];
96                 [futil release];
97                 futil = nil; 
98         }
99         [super dealloc];
100 }
101
102 // -------------------------------------------------------------------------------
103 //  setPath:
104 // -------------------------------------------------------------------------------
105 - (void) setPath:(NSString*)path {
106         if(installationPath)[installationPath release];
107         
108         if(path)
109                 installationPath = [path retain];
110 }
111
112 // -------------------------------------------------------------------------------
113 //  path:
114 // -------------------------------------------------------------------------------
115 - (NSString*) path{
116         return installationPath;
117 }
118
119 // -------------------------------------------------------------------------------
120 //  statCacheEntry:
121 // -------------------------------------------------------------------------------
122 -(int) statCacheEntry
123 {
124         return statCacheEntry;
125 }
126
127 // -------------------------------------------------------------------------------
128 //  setStatCacheEntry:
129 // -------------------------------------------------------------------------------
130 -(void) setStatCacheEntry:(int)statEntry
131 {
132         statCacheEntry = statEntry;
133 }
134
135
136 // -------------------------------------------------------------------------------
137 //  dCacheDim:
138 // -------------------------------------------------------------------------------
139 -(int) dCacheDim
140 {
141         return dCacheDim;
142 }
143
144 // -------------------------------------------------------------------------------
145 //  setDCacheDim:
146 // -------------------------------------------------------------------------------
147 -(void) setDCacheDim:(int)dcDim
148 {
149         dCacheDim = dcDim;
150 }
151
152 // -------------------------------------------------------------------------------
153 //  cacheDimension:
154 // -------------------------------------------------------------------------------
155 -(int) cacheDimension
156 {
157         return cacheDimension;
158 }
159
160 // -------------------------------------------------------------------------------
161 //  setCacheDimension:
162 // -------------------------------------------------------------------------------
163 -(void) setCacheDimension:(int)cacheDim
164 {
165         cacheDimension = cacheDim;
166 }
167
168 // -------------------------------------------------------------------------------
169 //  daemonNumber:
170 // -------------------------------------------------------------------------------
171 -(int) daemonNumber
172 {
173         return daemonNumber;
174 }
175
176 // -------------------------------------------------------------------------------
177 //  setDaemonNumber:
178 // -------------------------------------------------------------------------------
179 -(void) setDaemonNumber:(int)dNumber
180 {
181         daemonNumber = dNumber;
182 }
183
184 // -------------------------------------------------------------------------------
185 //  afsRootMountPoint:
186 // -------------------------------------------------------------------------------
187 -(NSString*) afsRootMountPoint
188 {
189         return afsRootMountPoint;
190 }
191
192 // -------------------------------------------------------------------------------
193 //  setAfsRootMountPoint:
194 // -------------------------------------------------------------------------------
195 -(void) setAfsRootMountPoint:(NSString*)mountPoint
196 {
197         if(afsRootMountPoint)[afsRootMountPoint release];
198         
199         if(mountPoint)
200                 afsRootMountPoint = [mountPoint retain];
201 }
202
203 // -------------------------------------------------------------------------------
204 //  nVolEntry:
205 // -------------------------------------------------------------------------------
206 -(int) nVolEntry
207 {
208         return nVolEntry;
209 }
210
211 // -------------------------------------------------------------------------------
212 //  setNVolEntry:
213 // -------------------------------------------------------------------------------
214 -(void) setNVolEntry:(int)entry
215 {
216         nVolEntry = entry;
217 }
218
219 // -------------------------------------------------------------------------------
220 //  dynRoot:
221 // -------------------------------------------------------------------------------
222 -(bool) dynRoot
223 {
224         return dynRoot;
225 }
226
227 // -------------------------------------------------------------------------------
228 //  setDynRoot:
229 // -------------------------------------------------------------------------------
230 -(void) setDynRoot:(bool)flag
231 {
232         dynRoot = flag;
233 }
234
235 // -------------------------------------------------------------------------------
236 //  dynRoot:
237 // -------------------------------------------------------------------------------
238 -(bool) afsDB
239 {
240         return afsDB;
241 }
242
243 // -------------------------------------------------------------------------------
244 //  setDynRoot:
245 // -------------------------------------------------------------------------------
246 -(void) setAfsDB:(bool)flag
247 {
248         afsDB = flag;
249 }
250
251 // -------------------------------------------------------------------------------
252 //  setDynRoot:
253 // -------------------------------------------------------------------------------
254 -(bool) verbose
255 {
256         return verbose;
257 }
258
259 // -------------------------------------------------------------------------------
260 //  setDynRoot:
261 // -------------------------------------------------------------------------------
262 -(void) setVerbose:(bool)flag
263 {
264         verbose = flag;
265 }
266
267 // -------------------------------------------------------------------------------
268 //  useAfsdConfVersion:
269 // -------------------------------------------------------------------------------
270 -(BOOL) useAfsdConfConfigFile
271 {
272         return useAfsdConfVersion;
273 }
274
275 // -------------------------------------------------------------------------------
276 //  exceptionOnInvalidPath:
277 // -------------------------------------------------------------------------------
278 - (void) exceptionOnInvalidPath
279 {
280         if(!installationPath || ([installationPath length] == 0)) {
281                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
282                                                                            reason:kPathNotEmpty 
283                                                                          userInfo:nil];                 
284         }
285         if(![[NSFileManager defaultManager] fileExistsAtPath:installationPath]){
286                 @throw [NSException exceptionWithName:@"AFSPropertyManager:exceptionOnInvalidPath" 
287                                                                            reason:kPathDontContainAfsInstallation
288                                                                          userInfo:nil];                 
289         }
290         
291 }
292
293 // -------------------------------------------------------------------------------
294 //  loadConfiguration:
295 // -------------------------------------------------------------------------------
296 - (void) loadConfiguration {
297         int mjVersion = [self getAfsMajorVersionVersion];
298         int miVersion = [self getAfsMinorVersionVersion];
299         int paVersion = [self getAfsPatchVersionVersion];
300         
301         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
302         @try{
303                 
304                 [self exceptionOnInvalidPath];
305                 [self clearConfiguration];
306                 
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);
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 nil;
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 nil;
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 nil;
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 nil;
540         NSString *currentLines = nil;
541         NSString *paramValue = nil;
542         NSScanner *lineScanner = nil;
543         
544         //Get file content
545         NSString *newAFSDConfContent = [NSString stringWithContentsOfFile:filePath 
546                                                                                                                          encoding:NSUTF8StringEncoding 
547                                                                                                                                 error:nil];
548         if (!newAFSDConfContent) return nil;
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         @try {
1081                 const char *stopArgs[] = {"stop", 0L};
1082                 if([[AuthUtil shared] autorize] == noErr) {
1083                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1084                                                                                   args:stopArgs
1085                                                                                 output:nil];
1086                 }
1087
1088         }
1089         @catch (NSException * e) {
1090                 @throw e;
1091         }
1092         @finally {
1093         
1094         }
1095         
1096         
1097 }
1098
1099
1100 // -------------------------------------------------------------------------------
1101 //  -(void) shutdown
1102 // -------------------------------------------------------------------------------
1103 -(void) startup
1104 {
1105         @try {
1106                 const char *startArgs[] = {"start", 0L};
1107                 if([[AuthUtil shared] autorize] == noErr) {
1108                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1109                                                                                   args:startArgs
1110                                                                                 output:nil];
1111                 }
1112
1113         }
1114         @catch (NSException * e) {
1115                 @throw e;
1116         }
1117         @finally {
1118
1119         }
1120
1121
1122 }
1123 // -------------------------------------------------------------------------------
1124 //  -(void) saveConfigurationFiles
1125 // -------------------------------------------------------------------------------
1126 -(void) saveConfigurationFiles:(BOOL) makeBackup
1127 {
1128         NSError *err;
1129         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1130         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1131         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1132         DBCellElement *cellElement = nil;
1133         
1134         // save the configuration file
1135         @try{
1136                 [self exceptionOnInvalidPath];
1137
1138                 // ThisCell
1139                 [filePath setString: @"/tmp/ThisCell"];
1140                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1141                                    atomically:YES 
1142                                          encoding: NSASCIIStringEncoding 
1143                                                 error:nil];
1144                 // CellServDB
1145                 
1146                 for(int idx = 0; idx < [cellList count]; idx++){
1147                         cellElement = [cellList objectAtIndex:idx];
1148                         [cellServDBString appendString:[cellElement description]];
1149                         if([cellElement userDefaultForToken]) {
1150                                 [theseCellString  appendString:[cellElement getCellName]];
1151                                 [theseCellString  appendString:@"\n"];
1152                         }
1153                 }
1154                 
1155                 
1156                 [filePath setString: @"/tmp/CellServDB"];
1157                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1158                                                    atomically:YES 
1159                                                          encoding:  NSUTF8StringEncoding 
1160                                                                 error:&err];
1161                 
1162                 [filePath setString: @"/tmp/TheseCells"];
1163                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1164                                                    atomically:YES 
1165                                                          encoding:  NSUTF8StringEncoding 
1166                                                                 error:&err];
1167                 
1168                 // backup original file
1169                 if([futil startAutorization] != noErr){
1170                         @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
1171                                                                                    reason:kUserNotAuth
1172                                                                                  userInfo:nil];
1173                 }
1174
1175                 if(makeBackup) [self backupConfigurationFiles];
1176
1177                 // install ThisCell
1178                 [filePath setString: installationPath];
1179                 [filePath appendString: @"/etc/ThisCell"];
1180                 [self installConfigurationFile:@"/tmp/ThisCell" 
1181                                                           destPath:filePath];           
1182                 
1183                 // install CellServDB
1184                 [filePath setString: installationPath];
1185                 [filePath appendString: @"/etc/CellServDB"];
1186                 [self installConfigurationFile:@"/tmp/CellServDB" 
1187                                                           destPath:filePath];
1188                 
1189                 // install CellServDB
1190                 [filePath setString: installationPath];
1191                 [filePath appendString: @"/etc/TheseCells"];
1192                 [self installConfigurationFile:@"/tmp/TheseCells" 
1193                                                           destPath:filePath];
1194                 
1195         } @catch (NSException *e) {
1196                 @throw e;
1197         }@finally {
1198                 // dispose all variable used
1199                 if(filePath) [filePath release];
1200                 if(cellServDBString) [cellServDBString release];
1201         }
1202         
1203 }
1204
1205 // -------------------------------------------------------------------------------
1206 //  -(void) saveCacheConfigurationFiles
1207 // -------------------------------------------------------------------------------
1208 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1209 {
1210         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1211         // save the configuration file
1212         @try{
1213                 [self exceptionOnInvalidPath];
1214                 
1215                 // cacheinfo file creation
1216                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1217                 
1218                 //afsd.option or afs.conf file creation
1219                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1220                 
1221                 // backup original file
1222                 if([futil startAutorization] != noErr){
1223                         @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
1224                                                                                    reason:kUserNotAuth
1225                                                                                  userInfo:nil];
1226                 }
1227                 
1228                 if(makeBackup) {
1229                         //cacheinfo
1230                         [self backupFile:@"/etc/cacheinfo"];
1231                         
1232                         //afsd.options
1233                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1234                 }
1235                 
1236                 // install cacheinfo
1237                 [filePath setString:installationPath];
1238                 [filePath appendString: @"/etc/cacheinfo"];
1239                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1240                                                           destPath:filePath];           
1241                 
1242                 // install afsd.conf or afs.conf configuration file 
1243                 [filePath setString: installationPath];
1244                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1245                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1246                                                           destPath:filePath];
1247                 
1248         } @catch (NSException *e) {
1249                 @throw e;
1250         }@finally {
1251                 if(filePath) [filePath release];
1252         }
1253         
1254         
1255 }
1256
1257 // -------------------------------------------------------------------------------
1258 //  -(void) installConfigurationFile
1259 // -------------------------------------------------------------------------------
1260 -(void) installConfigurationFile:(NSString*)srcConfFile 
1261                                                 destPath:(NSString*)destPath
1262 {
1263         // delete the file original file
1264
1265         if([futil autorizedDelete:destPath] != noErr){
1266                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1267                                                                            reason:destPath
1268                                                                          userInfo:nil];
1269         }
1270         
1271         // move the file
1272         if([futil autorizedMoveFile:srcConfFile
1273                                                  toPath:destPath] != noErr) {
1274                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1275                                                                            reason:srcConfFile
1276                                                                          userInfo:nil];
1277         }
1278         
1279         
1280         if([futil autorizedChown:destPath 
1281                                            owner:@"root"
1282                                            group:@"wheel"]!= noErr) {
1283                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1284                                                                            reason:destPath
1285                                                                          userInfo:nil];
1286         }
1287 }
1288
1289 // -------------------------------------------------------------------------------
1290 //  -(void) backupConfigurationFiles
1291 // -------------------------------------------------------------------------------
1292 -(void) backupConfigurationFiles
1293 {       
1294
1295         @try{
1296                 if([futil startAutorization] != noErr){
1297                         @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
1298                                                                                    reason:kUserNotAuth
1299                                                                                  userInfo:nil];
1300                 }
1301                 //This cell
1302                 [self backupFile:@"/etc/ThisCell"];
1303         
1304                 //CellServDB
1305                 [self backupFile:@"/etc/CellServDB"];
1306                 
1307                 //TheseCell
1308                 [self backupFile:@"/etc/TheseCells"];
1309                 
1310                 //[futil endAutorization];
1311         } @catch (NSException *e) {
1312                 @throw e;
1313         } @finally {
1314         }
1315 }
1316
1317 // -------------------------------------------------------------------------------
1318 //  -(void) backupFile:(NSString*)localAfsFilePath
1319 // -------------------------------------------------------------------------------
1320 -(void) backupFile:(NSString*)localAfsFilePath
1321 {
1322         NSString *srcString = nil;
1323         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1324         OSStatus err = noErr;
1325         @try{
1326                 [filePath setString: installationPath];
1327                 [filePath appendString: localAfsFilePath];
1328                 
1329                 //Check if the file at path exist
1330                 NSFileManager *fileManager = [NSFileManager defaultManager];
1331                 
1332                 //check if th efile exist
1333                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1334                 
1335                 // store the source path
1336                 srcString  = [filePath stringByExpandingTildeInPath];
1337                 [filePath appendString: @".afscommander_bk"];
1338                 
1339                 // check for presence of bk file
1340                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1341                         // backup the file
1342                         err = [futil autorizedCopy:srcString 
1343                                                   toPath:[filePath stringByExpandingTildeInPath]];
1344                 }
1345         } @catch (NSException *e) {
1346                 @throw e;
1347         } @finally {
1348                 if(filePath) [filePath release];
1349         }
1350 }
1351
1352 // -------------------------------------------------------------------------------
1353 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1354 // -------------------------------------------------------------------------------
1355 -(BOOL) checkAfsStatus
1356 {
1357         BOOL result = NO;
1358         NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1359         result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1360         return result;  
1361 }
1362
1363 // -------------------------------------------------------------------------------
1364 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1365 // -------------------------------------------------------------------------------
1366 -(BOOL) checkAfsStatusForStartup {
1367         BOOL result = NO;
1368                 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1369                 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1370         return result;
1371 }
1372
1373 // -------------------------------------------------------------------------------
1374 //  makeChaceParamString
1375 // -------------------------------------------------------------------------------
1376 -(NSString*) makeChaceParamString
1377 {
1378         NSNumber *tmpNum = nil;
1379         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1380         if(!afsdOption) return @"";
1381         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1382         //afsdb
1383         //dynRoot
1384         if([self afsDB]) {
1385                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1386         }
1387         
1388         //Verbose
1389         if([self verbose]) {
1390                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1391         }
1392         
1393         //stat entry
1394         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1395         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1396         
1397         //dcace
1398         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1399         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1400         
1401         //daemons
1402         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1403         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1404         
1405         //volumes
1406         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1407         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1408         
1409         //dynRoot
1410         if([self dynRoot]) {
1411                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1412         }
1413         
1414         //fakestat-all
1415         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1416         
1417         return [afsdOption autorelease];
1418 }
1419
1420 @end
1421