OSXPreferencePane
[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 <Kerberos/Kerberos.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 -(void) 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         NSData *fileHData = [fileH readDataToEndOfFile];
363         NSString *cacheInfoStrData = [[NSString alloc] initWithData:fileHData
364                                                                                                            encoding:NSASCIIStringEncoding];
365         NSScanner *cacheInfoS = [NSScanner  scannerWithString:cacheInfoStrData];
366         
367         @try{
368                 do {
369                         cicle++;
370                         switch(cicle){
371                                 case 1:
372                                         // afs mount path
373                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
374                                         [self setAfsRootMountPoint:tmpString];
375                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
376                                         break;
377                                         
378                                 case 2:
379                                         //cache path default '/var/db/openafs/cache'
380                                         [cacheInfoS scanUpToCharactersFromSet:fullStopCS intoString:&tmpString];
381                                         [cacheInfoS scanUpToCharactersFromSet:chunkStartCS intoString:&tmpString];
382                                         break;
383                                 
384                                 case 3:
385                                         // cache dimension
386                                         [cacheInfoS scanUpToCharactersFromSet:returnCS intoString:&tmpString];
387                                         [self setCacheDimension:[tmpString intValue]];
388                                         break;
389                         }
390                 }while(cicle < 3 && ![cacheInfoS isAtEnd]);
391         }@catch(NSException *e){
392                 @throw e;
393         }@finally{
394                 //if(cacheInfoStrData) [cacheInfoStrData release];
395                 if(chunkStartCS) [chunkStartCS release];
396         }
397         
398 }
399
400
401 // -------------------------------------------------------------------------------
402 //  writeCacheInfo:
403 // -------------------------------------------------------------------------------
404 -(int) writeCacheInfo:(NSString*)filePath
405 {
406         NSNumber *tmpNum = nil;
407         NSMutableString *cacheInfoContent = [[NSMutableString alloc] init];
408         if(!cacheInfoContent) return -1;
409         
410         //Afs root mount point
411         if([[self afsRootMountPoint] rangeOfString:@"/"].location == NSNotFound || [[self afsRootMountPoint] length] == 0) 
412                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
413                                                                            reason:@"Bad afs path" 
414                                                                          userInfo:nil];
415         [cacheInfoContent appendString:[self afsRootMountPoint]];
416         
417         //standard cache path
418         
419         [cacheInfoContent appendString:@":"]; [cacheInfoContent appendString:@AFS_CACHE_PATH]; [cacheInfoContent appendString:@":"];
420         
421         //cache dimension
422         tmpNum = [NSNumber numberWithInt:[self cacheDimension]];
423         if([tmpNum intValue] == 0)
424                 @throw [NSException exceptionWithName:@"AFSPropertyManager:writeCacheInfo"  
425                                                                            reason:@"Bad cache dimension" 
426                                                                          userInfo:nil];
427         [cacheInfoContent appendString:[tmpNum stringValue]];
428         
429         [cacheInfoContent writeToFile: [filePath stringByExpandingTildeInPath]
430                                            atomically: YES 
431                                                  encoding: NSASCIIStringEncoding 
432                                                         error: nil];
433         
434         [cacheInfoContent release];
435         return noErr;
436 }
437
438 // -------------------------------------------------------------------------------
439 //  readCacheInfo:
440 //  file template "/afs:/var/db/openafs/cache:30000"
441 // -------------------------------------------------------------------------------
442 -(void) readAfsdOption:(NSString*)filePath
443 {
444         @try{
445                 if(useAfsdConfVersion) {
446                         [self readNewAfsdOption:filePath];
447                 } else {
448                         [self readOldAfsdOption:filePath];
449                 }
450         
451         }@catch(NSException *e){
452                 @throw e;
453         }@finally{
454                 
455         }
456         
457 }
458
459 // -------------------------------------------------------------------------------
460 //  readOldAfsdOption:
461 // -------------------------------------------------------------------------------
462 -(void) readOldAfsdOption:(NSString*)filePath
463 {
464
465         /*NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:filePath];
466         NSData *fileHData = [fileH readDataToEndOfFile];
467         NSString *afsdOptionStrData = [[NSString alloc] initWithData:fileHData
468                                                                                                                 encoding:NSASCIIStringEncoding];*/
469         if(!filePath) return;
470         [self readAFSDParamLineContent:[[NSString stringWithContentsOfFile:filePath 
471                                                                                                                           encoding:NSUTF8StringEncoding 
472                                                                                                                                  error:nil] stringByStandardizingPath]];
473 }
474
475 // -------------------------------------------------------------------------------
476 //  readAFSDParamLineContent:
477 // -------------------------------------------------------------------------------
478 -(void) readAFSDParamLineContent:(NSString*) paramLine{
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         
532 }
533
534 // -------------------------------------------------------------------------------
535 //  readNewAfsdOption:
536 // -------------------------------------------------------------------------------
537 -(void) readNewAfsdOption:(NSString*)filePath
538 {
539         if(!filePath) return;
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         
549         //get lines in array
550         NSArray *confLines = [newAFSDConfContent componentsSeparatedByString:@"\n"];
551         
552         //Get the lines enumerator
553         NSEnumerator *lineEnumerator = [confLines objectEnumerator];
554         
555         //scann all lines
556         while(currentLines = [lineEnumerator nextObject]) {
557                 if([currentLines rangeOfString:@"#"].location != NSNotFound) continue;
558                         
559                 if([currentLines rangeOfString:AFSD_CONF_OPTION].location != NSNotFound) {
560                         lineScanner = [NSScanner scannerWithString:currentLines];
561                         if(!lineScanner) continue;
562                         
563                         //scann the line
564                         
565                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];[lineScanner scanUpToString:@"-" intoString:&paramValue];
566                         [lineScanner scanUpToString:@"\"" intoString:&paramValue];
567                         
568                         // read the asfd option param line
569                         [self readAFSDParamLineContent:paramValue];
570                 } else if([currentLines rangeOfString:AFSD_CONF_SYSNAME].location != NSNotFound) {
571                         
572                 } else if([currentLines rangeOfString:AFSD_CONF_POST_INI].location != NSNotFound) {
573                         
574                 } else if([currentLines rangeOfString:AFSD_CONF_PRE_SHUTDOWN].location != NSNotFound) {
575                         
576                 }
577         }
578 }
579
580
581 // -------------------------------------------------------------------------------
582 //  writeCacheInfo:
583 // -------------------------------------------------------------------------------
584 -(int) writeAfsdOption:(NSString*)filePath
585 {
586         int result = 0;
587         @try{
588                 if(useAfsdConfVersion) {
589                         result = [self writeNewAfsdOption:filePath];                    
590                 } else {
591                         result = [self writeOldAfsdOption:filePath];
592                 }
593                 
594         }@catch(NSException *e){
595                 @throw e;
596         }@finally{
597                 
598         }
599         return result;
600 }
601
602 // -------------------------------------------------------------------------------
603 //  writeOldAfsdOption:
604 // -------------------------------------------------------------------------------
605 -(int) writeOldAfsdOption:(NSString*)filePath;
606 {
607         NSMutableString *oldConfFileContent = [[[NSMutableString alloc] init] autorelease];
608         //add afsd param
609         [oldConfFileContent appendString:[self makeChaceParamString]];
610         
611         //add cariage return at end of file
612         [oldConfFileContent appendString:@"\n"];
613         
614         //write content on file
615         [oldConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
616                                                  atomically: YES 
617                                                    encoding: NSASCIIStringEncoding 
618                                                           error: nil];
619         return noErr;
620         
621 }
622
623 // -------------------------------------------------------------------------------
624 //  writeNewAfsdOption:
625 //  OPTIONS=
626 //  AFS_SYSNAME=
627 //  AFS_POST_INIT=afs_server_prefs
628 //  AFS_PRE_SHUTDOWN=
629 // -------------------------------------------------------------------------------
630 -(int) writeNewAfsdOption:(NSString*)filePath
631 {
632         NSMutableString *newConfFileContent = [[[NSMutableString alloc] init] autorelease];
633         
634         //Verbose
635         [newConfFileContent appendString:AFSD_CONF_VERBOSE]; [newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
636         
637         //AFSD Option
638         [newConfFileContent appendString:AFSD_CONF_OPTION];[newConfFileContent appendString:@"=\""]; [newConfFileContent appendString:[self makeChaceParamString]]; [newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"]; 
639         
640         //AFS_SYSNAME
641         [newConfFileContent appendString:AFSD_CONF_SYSNAME];[newConfFileContent appendString:@"=\""];[newConfFileContent appendString:@"\""]; [newConfFileContent appendString:@"\n"];
642         
643         //AFS_POST_INIT
644         [newConfFileContent appendString:AFSD_CONF_POST_INI];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"]; 
645         
646         //AFS_PRE_SHUTDOWN
647         [newConfFileContent appendString:AFSD_CONF_PRE_SHUTDOWN];[newConfFileContent appendString:@"="]; [newConfFileContent appendString:@"\n"];
648         
649         //add content on file
650         [newConfFileContent appendString:@"\n"];
651         
652         //Write to file
653         [newConfFileContent writeToFile: [filePath stringByExpandingTildeInPath]
654                                                  atomically: YES 
655                                                    encoding: NSASCIIStringEncoding 
656                                                           error: nil];
657         return noErr;
658 }
659
660 // -------------------------------------------------------------------------------
661 //  getAfsVersion:
662 // -------------------------------------------------------------------------------
663 -(NSString*) getAfsVersion
664 {
665         NSString *tmpString = nil;
666         NSString *result = [TaskUtil executeTaskSearchingPath:@"fs" args:[NSArray arrayWithObjects:@"-version", nil]];
667         NSCharacterSet *endVersionCS = [NSCharacterSet characterSetWithCharactersInString:@"qwertyuiopasdfghjklzxcvbnmMNBVCXZLKJHGFDSAPOIUYTREWQ"];
668         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" "];
669         NSScanner *versionS = [NSScanner  scannerWithString:result];
670         //go to  start of version
671         [versionS scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
672         
673         //get the total version string
674         [versionS scanUpToCharactersFromSet:endVersionCS intoString:&tmpString];
675
676         return tmpString;
677 }
678
679 // -------------------------------------------------------------------------------
680 //  getAfsMajorVersionVersion:
681 // -------------------------------------------------------------------------------
682 -(int) getAfsMajorVersionVersion
683 {
684         NSString *tmpString = nil;
685         NSString *totalVersion = [self getAfsVersion];
686         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
687         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
688         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
689         return [tmpString intValue];
690 }
691
692 // -------------------------------------------------------------------------------
693 //  getAfsMinorVersionVersion:
694 // -------------------------------------------------------------------------------
695 -(int) getAfsMinorVersionVersion
696 {
697         NSString *tmpString = nil;
698         NSString *totalVersion = [self getAfsVersion];
699         NSCharacterSet *numCS = [NSCharacterSet characterSetWithCharactersInString:@"1234567890"];
700         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
701         NSScanner *versionMS = [NSScanner  scannerWithString:totalVersion];
702         [versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:numCS intoString:&tmpString];[versionMS scanUpToCharactersFromSet:pointCS intoString:&tmpString];
703         return [tmpString intValue];
704 }
705
706 // -------------------------------------------------------------------------------
707 //  getAfsPatchVersionVersion:
708 // -------------------------------------------------------------------------------
709 -(int) getAfsPatchVersionVersion
710 {
711         NSString *totalVersion = [self getAfsVersion];
712         NSCharacterSet *pointCS = [NSCharacterSet characterSetWithCharactersInString:@"."];
713         int lastPointIndex = [totalVersion rangeOfCharacterFromSet:pointCS 
714                                                                                                            options:NSBackwardsSearch].location;
715         int patchVersion = [[totalVersion substringFromIndex:lastPointIndex+1] intValue];
716         return patchVersion;
717 }
718
719
720 // -------------------------------------------------------------------------------
721 //  clearConfiguration:
722 // -------------------------------------------------------------------------------
723 - (void) clearConfiguration{
724         
725         // clear list of cell
726         [cellList removeAllObjects];
727         
728         userDefaultCellArray= nil;
729         
730         // remove the old cell name
731         if(cellName) {
732                 [cellName release];
733                 cellName = nil;
734         }
735 }
736
737 // -------------------------------------------------------------------------------
738 //  getCellList:
739 // -------------------------------------------------------------------------------
740 -(NSMutableArray*) getCellList
741 {
742         return cellList;
743 }
744
745
746
747 // -------------------------------------------------------------------------------
748 //  getAllCellName:
749 // -------------------------------------------------------------------------------
750 -(NSArray*) getAllCellsName {
751         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
752         for(int idx = 0; idx < [cellList count]; idx++){
753                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
754                 [allCelListName addObject:[elemnt getCellName]];
755         }
756         return allCelListName;
757 }
758
759 // -------------------------------------------------------------------------------
760 //  getCellList:
761 // -------------------------------------------------------------------------------
762 -(NSArray*) getUserDefaultForTokenCells {
763         return userDefaultCellArray;
764 }
765
766 // -------------------------------------------------------------------------------
767 //  getDefaultCellName:
768 // -------------------------------------------------------------------------------
769 -(NSArray*) getDefaultForTokenCellsName {
770         NSMutableArray *allCelListName = [[[NSMutableArray alloc] init] autorelease];
771         for(int idx = 0; idx < [cellList count]; idx++){
772                 DBCellElement *elemnt = [cellList objectAtIndex:idx];
773                 if([elemnt userDefaultForToken]) [allCelListName addObject:[[elemnt getCellName] retain]];
774         }
775         return allCelListName;
776 }
777
778 // -------------------------------------------------------------------------------
779 //  getCellName:
780 // -------------------------------------------------------------------------------
781 -(NSString*) getDefaultCellName
782 {
783         return cellName;
784 }
785
786 // -------------------------------------------------------------------------------
787 //  setDefaultCellByName:
788 // -------------------------------------------------------------------------------
789 -(void) setDefaultCellByName:(NSString*)name
790 {
791         DBCellElement *elementCell = nil;
792         BOOL cellFound = false;
793         if(!name) return;
794         
795         for(int idx = 0; idx < [cellList count]; idx++) {
796                 // check every cell for delete as old user default cell or selected as neww cell
797                 elementCell = [cellList objectAtIndex:idx];
798                 cellFound = [name  compare:[elementCell getCellName]] == NSOrderedSame;
799                 [elementCell setUserDefaultCell:cellFound];
800                 if(cellFound) {
801                         [elementCell setUserDefaultForToken:YES];
802                         if(cellName)[cellName release];
803                         cellName = [name retain];
804                  }
805         }
806         
807 }
808
809 // -------------------------------------------------------------------------------
810 //  setCellName:
811 // -------------------------------------------------------------------------------
812 - (void) setCellName:(NSString*)cell {
813         [self setDefaultCellByName:cell];
814 }
815
816 // -------------------------------------------------------------------------------
817 //  readCellInfo:
818 // -------------------------------------------------------------------------------
819 -(void) readCellInfo:(NSString*) configFile {
820         NSError *error = nil;
821         NSString *tmpStr = nil;
822         NSString * result = [NSString stringWithContentsOfFile:configFile
823                                                                                                   encoding:NSASCIIStringEncoding
824                                                                                                          error:&error];
825         if(!result && error){
826                 if([error domain] )
827                 @throw [NSException exceptionWithName:@"readCellInfo" 
828                                                                            reason:kConfFileNotExits
829                                                                          userInfo:nil];
830         }
831         NSScanner *scanner = [NSScanner scannerWithString:result];
832         [scanner scanUpToString:@"\n" 
833                                  intoString:&tmpStr];
834         
835         // make a copy of self created string
836         cellName = [tmpStr retain];
837 }
838
839 // -------------------------------------------------------------------------------
840 //  readCellDB:
841 // -------------------------------------------------------------------------------
842 -(void) readCellDB:(NSString*) configFile {     
843         NSString *tmpString = nil;
844         BOOL isInCellDefaultArray = NO; // the cell belong
845         BOOL isDefaultCell = NO;
846         DBCellElement *afsCellDBElement = nil;
847         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
848         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
849         
850         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
851         NSData *dbCellData = [fileH readDataToEndOfFile];
852         NSString *strData = [[NSString alloc] initWithData:dbCellData
853                                                                                           encoding:NSASCIIStringEncoding];
854         NSScanner *cellDBScanner = [NSScanner  scannerWithString:strData];
855         
856         @try{
857         [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
858         while([cellDBScanner isAtEnd] == NO) {
859                 // make new cell
860                 afsCellDBElement = [[DBCellElement alloc] init];
861                 
862                 // get the name of cell
863                 [cellDBScanner scanUpToCharactersFromSet:spaceCS intoString:&tmpString];
864                 [afsCellDBElement setCellName: tmpString];
865                 
866                 //check if this cells is one of user has selected to get token
867                 isInCellDefaultArray = [userDefaultCellArray containsObject:tmpString];
868                 //check if this cell is also the default cell
869                 isDefaultCell = [cellName compare:tmpString]==NSOrderedSame;
870                 
871                 [afsCellDBElement setUserDefaultForToken:isInCellDefaultArray||isDefaultCell];
872                 [afsCellDBElement setUserDefaultCell:isDefaultCell];
873                 
874                 
875         
876                 
877                 
878                 // get the cell comment
879                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
880                 [cellDBScanner scanUpToCharactersFromSet:returnCS intoString:&tmpString];
881                 [afsCellDBElement setCellComment: tmpString];
882
883                 // get all ip
884                 [cellDBScanner scanUpToString:@">" intoString:&tmpString];
885                 // scann all ip in list
886                 [self scanIpForCell:afsCellDBElement allIP:tmpString];  
887                 
888                 // add cell to list
889                 [cellList addObject:afsCellDBElement];
890                 // release the object becasuse NSMutableArray make a retain on object
891                 [afsCellDBElement release];
892                 //repeat
893                 [cellDBScanner scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:&tmpString];
894         }
895         }@catch(NSException *e){
896                 @throw e;
897         }@finally{
898                 //if(strData) [strData release];
899         }
900 }
901
902 // -------------------------------------------------------------------------------
903 //  scanIpForCell:
904 // -------------------------------------------------------------------------------
905 -(void) scanIpForCell:(DBCellElement*) cellElement allIP:(NSString*)allIP {
906         NSScanner *ipScann = [NSScanner scannerWithString:allIP];
907         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
908         NSCharacterSet *spaceCS = [NSCharacterSet characterSetWithCharactersInString:@" \t"];
909         NSCharacterSet *startCommentCS = [NSCharacterSet characterSetWithCharactersInString:@"#"];
910         NSString *tmpString = nil;
911         while([ipScann isAtEnd] == NO){
912                 CellIp *cellIpDesc = [[CellIp alloc] init];
913                 
914                 //ip string
915                 [ipScann scanUpToCharactersFromSet:spaceCS 
916                                                                 intoString:&tmpString];
917                 
918                 [cellIpDesc setCellIp:tmpString];
919                 //[tmpString release];
920                 
921                 // go to comment
922                 [ipScann scanUpToCharactersFromSet:startCommentCS intoString:nil];
923                 // skip comment symbol
924                 [ipScann scanUpToCharactersFromSet:[NSCharacterSet alphanumericCharacterSet] intoString:nil];
925                 // get comment
926                 [ipScann scanUpToCharactersFromSet:returnCS intoString:&tmpString];
927                 [cellIpDesc setCellComment:tmpString];
928                 //[tmpString release];
929                 
930                 [cellElement addIpToCell:cellIpDesc];
931                 // release the object becasuse NSMutableArray make a retain on object
932                 [cellIpDesc release];
933         }
934 }
935
936
937 // -------------------------------------------------------------------------------
938 //  scanIpForCell:
939 // -------------------------------------------------------------------------------
940 -(NSArray*) readTheseCell:(NSString*) configFile {
941         
942         NSFileHandle *fileH = [NSFileHandle fileHandleForReadingAtPath:configFile];
943         NSData *dbCellData = [fileH readDataToEndOfFile];
944         NSString *strData = [[NSString alloc] initWithData:dbCellData
945                                                                                           encoding:NSASCIIStringEncoding];
946         
947         return [strData componentsSeparatedByString:@"\n"];
948 }
949
950 // -------------------------------------------------------------------------------
951 //  -(void) getTokenList
952 // -------------------------------------------------------------------------------
953 -(NSArray*) getTokenList
954 {
955         int line = 0;
956         NSString *tokenLine = nil;
957         //NSString *tmpValue = nil;
958         NSMutableArray *tokenList = [[NSMutableArray alloc] init];
959         NSString *tokensOutput = [TaskUtil executeTaskSearchingPath:@"tokens" args:[NSArray arrayWithObjects:nil]];
960         
961         // scann the tokens
962         NSScanner *tokenScan = [NSScanner scannerWithString:tokensOutput];
963         NSCharacterSet *returnCS = [NSCharacterSet characterSetWithCharactersInString:@"\n"];
964         
965         while([tokenScan isAtEnd] == NO){
966                 line++;
967                 // get the next line
968                 [tokenScan scanUpToCharactersFromSet:returnCS 
969                                                                 intoString:&tokenLine];
970                 
971                 // check for tokens end
972                 if([tokenLine rangeOfString:@"--End of list--"].location != NSNotFound) break;
973                 
974                 
975                 if(line >= 2){
976                         // add enteir row to result
977                         [tokenList addObject:tokenLine];
978                         // create the line scanner for all the row that contains token info
979                 }
980         }
981         //
982         return tokenList;
983 }
984
985 // -------------------------------------------------------------------------------
986 //  +(void) klog:(NSString*)uName uPwd:(NSString*)uPwd
987 // -------------------------------------------------------------------------------
988 -(void) klog:(NSString*)uName uPwd:(NSString*)uPwd cell:(NSString*)theCell
989 {
990         if(uName == @"" ||  uPwd == @"") return;
991         
992         [TaskUtil executeTaskSearchingPath:@"klog" 
993                                                                   args:(theCell==nil?[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, nil]:[NSArray arrayWithObjects:@"-principal", uName, @"-password", uPwd, @"-c", theCell, nil])];
994         
995         
996 }
997
998
999 // -------------------------------------------------------------------------------
1000 //  +(void) aklog
1001 // -------------------------------------------------------------------------------
1002 -(void) aklog:(NSString*)theCell noKerberosCall:(BOOL)krb5CallEnable {
1003         KLPrincipal             princ = nil;
1004         KLStatus                kstatus = noErr;
1005         char                    *princName = 0L;
1006         KLBoolean       outFoundValidTickets = false;
1007         @try {
1008                 // trying to ket kerberos ticket
1009                 if(krb5CallEnable) {
1010                         kstatus = KLCacheHasValidTickets(nil, nil, &outFoundValidTickets, nil, nil);
1011                         //kstatus =  KLAcquireInitialTickets (0L, 0L, &princ,  &princName);
1012                         if(!outFoundValidTickets) {
1013                                 kstatus = KLAcquireNewInitialTickets(nil, nil, &princ, &princName);
1014                                 if(kstatus != noErr && kstatus != klUserCanceledErr) @throw [NSException exceptionWithName:@"aklog" 
1015                                                                                                                                                                                                         reason:@"KLAcquireInitialTickets" 
1016                                                                                                                                                                                                   userInfo:nil];
1017                         }
1018                 } else kstatus = klNoErr;
1019                 
1020                 
1021                 //ok to launch aklog
1022                 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
1023                                                                                                                          args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1024                 
1025         }
1026         @catch (NSException * e) {
1027                 @throw e;
1028         }
1029         @finally {
1030                 // destory the kerberos va
1031                 if (kstatus == klNoErr) {
1032                         KLDisposeString (princName);
1033                         KLDisposePrincipal (princ);
1034                 }
1035         }
1036                 
1037 }
1038
1039
1040 // -------------------------------------------------------------------------------
1041 //  getTokens:
1042 // -------------------------------------------------------------------------------
1043 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1044         
1045         NSString *celStrName = nil;
1046         NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1047         if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1048                 //there are other cell to autenticate
1049                 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1050                         celStrName = [tmpDefaultArray objectAtIndex:idx];
1051                         if(klogAklogFlag)
1052                                 [self klog:usr 
1053                                           uPwd:pwd  
1054                                           cell:celStrName];
1055                         else
1056                                 [self aklog:celStrName noKerberosCall:YES];     
1057                 }
1058                 
1059         } else {
1060                 //there is only default cell to autenticate
1061                 if(klogAklogFlag)
1062                         [self klog:usr 
1063                                   uPwd:pwd 
1064                                   cell:nil];
1065                 else
1066                         [self aklog:nil noKerberosCall:YES];
1067         }
1068         
1069 }
1070
1071 // -------------------------------------------------------------------------------
1072 //  +(void) unlog
1073 // -------------------------------------------------------------------------------
1074 -(void) unlog:(NSString*)cell
1075 {
1076         [TaskUtil executeTaskSearchingPath:@"unlog" 
1077                                                                   args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1078 }
1079
1080 // -------------------------------------------------------------------------------
1081 //  -(void) shutdown
1082 // -------------------------------------------------------------------------------
1083 -(void) shutdown
1084 {
1085         @try {
1086                 const char *stopArgs[] = {"stop", 0L};
1087                 if([[AuthUtil shared] autorize] == noErr) {
1088                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1089                                                                                   args:stopArgs
1090                                                                                 output:nil];
1091                 }
1092
1093         }
1094         @catch (NSException * e) {
1095                 @throw e;
1096         }
1097         @finally {
1098         
1099         }
1100         
1101         
1102 }
1103
1104
1105 // -------------------------------------------------------------------------------
1106 //  -(void) shutdown
1107 // -------------------------------------------------------------------------------
1108 -(void) startup
1109 {
1110         @try {
1111                 const char *startArgs[] = {"start", 0L};
1112                 if([[AuthUtil shared] autorize] == noErr) {
1113                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1114                                                                                   args:startArgs
1115                                                                                 output:nil];
1116                 }
1117
1118         }
1119         @catch (NSException * e) {
1120                 @throw e;
1121         }
1122         @finally {
1123
1124         }
1125
1126
1127 }
1128 // -------------------------------------------------------------------------------
1129 //  -(void) saveConfigurationFiles
1130 // -------------------------------------------------------------------------------
1131 -(void) saveConfigurationFiles:(BOOL) makeBackup
1132 {
1133         NSError *err;
1134         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1135         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1136         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1137         DBCellElement *cellElement = nil;
1138         
1139         // save the configuration file
1140         @try{
1141                 [self exceptionOnInvalidPath];
1142
1143                 // ThisCell
1144                 [filePath setString: @"/tmp/ThisCell"];
1145                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1146                                    atomically:YES 
1147                                          encoding: NSASCIIStringEncoding 
1148                                                 error:nil];
1149                 // CellServDB
1150                 
1151                 for(int idx = 0; idx < [cellList count]; idx++){
1152                         cellElement = [cellList objectAtIndex:idx];
1153                         [cellServDBString appendString:[cellElement description]];
1154                         if([cellElement userDefaultForToken]) {
1155                                 [theseCellString  appendString:[cellElement getCellName]];
1156                                 [theseCellString  appendString:@"\n"];
1157                         }
1158                 }
1159                 
1160                 
1161                 [filePath setString: @"/tmp/CellServDB"];
1162                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1163                                                    atomically:YES 
1164                                                          encoding:  NSUTF8StringEncoding 
1165                                                                 error:&err];
1166                 
1167                 [filePath setString: @"/tmp/TheseCells"];
1168                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1169                                                    atomically:YES 
1170                                                          encoding:  NSUTF8StringEncoding 
1171                                                                 error:&err];
1172                 
1173                 // backup original file
1174                 if([futil startAutorization] != noErr){
1175                         @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
1176                                                                                    reason:kUserNotAuth
1177                                                                                  userInfo:nil];
1178                 }
1179
1180                 if(makeBackup) [self backupConfigurationFiles];
1181
1182                 // install ThisCell
1183                 [filePath setString: installationPath];
1184                 [filePath appendString: @"/etc/ThisCell"];
1185                 [self installConfigurationFile:@"/tmp/ThisCell" 
1186                                                           destPath:filePath];           
1187                 
1188                 // install CellServDB
1189                 [filePath setString: installationPath];
1190                 [filePath appendString: @"/etc/CellServDB"];
1191                 [self installConfigurationFile:@"/tmp/CellServDB" 
1192                                                           destPath:filePath];
1193                 
1194                 // install CellServDB
1195                 [filePath setString: installationPath];
1196                 [filePath appendString: @"/etc/TheseCells"];
1197                 [self installConfigurationFile:@"/tmp/TheseCells" 
1198                                                           destPath:filePath];
1199                 
1200         } @catch (NSException *e) {
1201                 @throw e;
1202         }@finally {
1203                 // dispose all variable used
1204                 if(filePath) [filePath release];
1205                 if(cellServDBString) [cellServDBString release];
1206         }
1207         
1208 }
1209
1210 // -------------------------------------------------------------------------------
1211 //  -(void) saveCacheConfigurationFiles
1212 // -------------------------------------------------------------------------------
1213 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1214 {
1215         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1216         // save the configuration file
1217         @try{
1218                 [self exceptionOnInvalidPath];
1219                 
1220                 // cacheinfo file creation
1221                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1222                 
1223                 //afsd.option or afs.conf file creation
1224                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1225                 
1226                 // backup original file
1227                 if([futil startAutorization] != noErr){
1228                         @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
1229                                                                                    reason:kUserNotAuth
1230                                                                                  userInfo:nil];
1231                 }
1232                 
1233                 if(makeBackup) {
1234                         //cacheinfo
1235                         [self backupFile:@"/etc/cacheinfo"];
1236                         
1237                         //afsd.options
1238                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1239                 }
1240                 
1241                 // install cacheinfo
1242                 [filePath setString:installationPath];
1243                 [filePath appendString: @"/etc/cacheinfo"];
1244                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1245                                                           destPath:filePath];           
1246                 
1247                 // install afsd.conf or afs.conf configuration file 
1248                 [filePath setString: installationPath];
1249                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1250                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1251                                                           destPath:filePath];
1252                 
1253         } @catch (NSException *e) {
1254                 @throw e;
1255         }@finally {
1256                 if(filePath) [filePath release];
1257         }
1258         
1259         
1260 }
1261
1262 // -------------------------------------------------------------------------------
1263 //  -(void) installConfigurationFile
1264 // -------------------------------------------------------------------------------
1265 -(void) installConfigurationFile:(NSString*)srcConfFile 
1266                                                 destPath:(NSString*)destPath
1267 {
1268         // delete the file original file
1269
1270         if([futil autorizedDelete:destPath] != noErr){
1271                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1272                                                                            reason:destPath
1273                                                                          userInfo:nil];
1274         }
1275         
1276         // move the file
1277         if([futil autorizedMoveFile:srcConfFile
1278                                                  toPath:destPath] != noErr) {
1279                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1280                                                                            reason:srcConfFile
1281                                                                          userInfo:nil];
1282         }
1283         
1284         
1285         if([futil autorizedChown:destPath 
1286                                            owner:@"root"
1287                                            group:@"wheel"]!= noErr) {
1288                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1289                                                                            reason:destPath
1290                                                                          userInfo:nil];
1291         }
1292 }
1293
1294 // -------------------------------------------------------------------------------
1295 //  -(void) backupConfigurationFiles
1296 // -------------------------------------------------------------------------------
1297 -(void) backupConfigurationFiles
1298 {       
1299
1300         @try{
1301                 if([futil startAutorization] != noErr){
1302                         @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
1303                                                                                    reason:kUserNotAuth
1304                                                                                  userInfo:nil];
1305                 }
1306                 //This cell
1307                 [self backupFile:@"/etc/ThisCell"];
1308         
1309                 //CellServDB
1310                 [self backupFile:@"/etc/CellServDB"];
1311                 
1312                 //TheseCell
1313                 [self backupFile:@"/etc/TheseCells"];
1314                 
1315                 //[futil endAutorization];
1316         } @catch (NSException *e) {
1317                 @throw e;
1318         } @finally {
1319         }
1320 }
1321
1322 // -------------------------------------------------------------------------------
1323 //  -(void) backupFile:(NSString*)localAfsFilePath
1324 // -------------------------------------------------------------------------------
1325 -(void) backupFile:(NSString*)localAfsFilePath
1326 {
1327         NSString *srcString = nil;
1328         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1329         OSStatus err = noErr;
1330         @try{
1331                 [filePath setString: installationPath];
1332                 [filePath appendString: localAfsFilePath];
1333                 
1334                 //Check if the file at path exist
1335                 NSFileManager *fileManager = [NSFileManager defaultManager];
1336                 
1337                 //check if th efile exist
1338                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1339                 
1340                 // store the source path
1341                 srcString  = [filePath stringByExpandingTildeInPath];
1342                 [filePath appendString: @".afscommander_bk"];
1343                 
1344                 // check for presence of bk file
1345                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1346                         // backup the file
1347                         err = [futil autorizedCopy:srcString 
1348                                                   toPath:[filePath stringByExpandingTildeInPath]];
1349                 }
1350         } @catch (NSException *e) {
1351                 @throw e;
1352         } @finally {
1353                 if(filePath) [filePath release];
1354         }
1355 }
1356
1357 // -------------------------------------------------------------------------------
1358 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1359 // -------------------------------------------------------------------------------
1360 -(BOOL) checkAfsStatus
1361 {
1362         BOOL result = NO;
1363         NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1364         result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1365         return result;  
1366 }
1367
1368 // -------------------------------------------------------------------------------
1369 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1370 // -------------------------------------------------------------------------------
1371 -(BOOL) checkAfsStatusForStartup {
1372         BOOL result = NO;
1373                 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1374                 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1375         return result;
1376 }
1377
1378 // -------------------------------------------------------------------------------
1379 //  makeChaceParamString
1380 // -------------------------------------------------------------------------------
1381 -(NSString*) makeChaceParamString
1382 {
1383         NSNumber *tmpNum = nil;
1384         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1385         if(!afsdOption) return @"";
1386         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1387         //afsdb
1388         //dynRoot
1389         if([self afsDB]) {
1390                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1391         }
1392         
1393         //Verbose
1394         if([self verbose]) {
1395                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1396         }
1397         
1398         //stat entry
1399         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1400         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1401         
1402         //dcace
1403         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1404         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1405         
1406         //daemons
1407         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1408         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1409         
1410         //volumes
1411         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1412         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1413         
1414         //dynRoot
1415         if([self dynRoot]) {
1416                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1417         }
1418         
1419         //fakestat-all
1420         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1421         
1422         return [afsdOption autorelease];
1423 }
1424
1425 @end
1426