Develop Kerberos renew system for ticket
[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 -(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         KLStatus                kstatus = noErr;
1004         @try {
1005                 // trying to ket kerberos ticket
1006                 if(krb5CallEnable) {
1007                         kstatus = [Krb5Util getNewTicketIfNotPresent];
1008                 } else kstatus = klNoErr;
1009                 
1010                 //ok to launch aklog
1011                 if(kstatus == klNoErr) [TaskUtil executeTaskSearchingPath:@"aklog" 
1012                                                                                                                          args:(theCell==nil?[NSArray arrayWithObjects:nil]:[NSArray arrayWithObjects:@"-c", theCell, nil])];
1013                 
1014         }
1015         @catch (NSException * e) {
1016                 @throw e;
1017         }
1018         @finally {
1019
1020         }
1021                 
1022 }
1023
1024
1025 // -------------------------------------------------------------------------------
1026 //  getTokens:
1027 // -------------------------------------------------------------------------------
1028 - (void) getTokens:(BOOL)klogAklogFlag usr:(NSString*)usr pwd:(NSString*)pwd {
1029         
1030         NSString *celStrName = nil;
1031         NSArray *tmpDefaultArray = [self getDefaultForTokenCellsName];
1032         if(tmpDefaultArray && [tmpDefaultArray count] > 1) {
1033                 //there are other cell to autenticate
1034                 for(int idx=0; idx < [tmpDefaultArray count]; idx++){
1035                         celStrName = [tmpDefaultArray objectAtIndex:idx];
1036                         if(klogAklogFlag)
1037                                 [self klog:usr 
1038                                           uPwd:pwd  
1039                                           cell:celStrName];
1040                         else
1041                                 [self aklog:celStrName noKerberosCall:YES];     
1042                 }
1043                 
1044         } else {
1045                 //there is only default cell to autenticate
1046                 if(klogAklogFlag)
1047                         [self klog:usr 
1048                                   uPwd:pwd 
1049                                   cell:nil];
1050                 else
1051                         [self aklog:nil noKerberosCall:YES];
1052         }
1053         
1054 }
1055
1056 // -------------------------------------------------------------------------------
1057 //  +(void) unlog
1058 // -------------------------------------------------------------------------------
1059 -(void) unlog:(NSString*)cell
1060 {
1061         [TaskUtil executeTaskSearchingPath:@"unlog" 
1062                                                                   args:(cell?[NSArray arrayWithObjects:@"-c",cell,nil]:[NSArray arrayWithObjects:nil])];
1063 }
1064
1065 // -------------------------------------------------------------------------------
1066 //  -(void) shutdown
1067 // -------------------------------------------------------------------------------
1068 -(void) shutdown
1069 {
1070         @try {
1071                 const char *stopArgs[] = {"stop", 0L};
1072                 if([[AuthUtil shared] autorize] == noErr) {
1073                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1074                                                                                   args:stopArgs
1075                                                                                 output:nil];
1076                 }
1077
1078         }
1079         @catch (NSException * e) {
1080                 @throw e;
1081         }
1082         @finally {
1083         
1084         }
1085         
1086         
1087 }
1088
1089
1090 // -------------------------------------------------------------------------------
1091 //  -(void) shutdown
1092 // -------------------------------------------------------------------------------
1093 -(void) startup
1094 {
1095         @try {
1096                 const char *startArgs[] = {"start", 0L};
1097                 if([[AuthUtil shared] autorize] == noErr) {
1098                         [[AuthUtil shared] execUnixCommand:AFS_DAEMON_STARTUPSCRIPT
1099                                                                                   args:startArgs
1100                                                                                 output:nil];
1101                 }
1102
1103         }
1104         @catch (NSException * e) {
1105                 @throw e;
1106         }
1107         @finally {
1108
1109         }
1110
1111
1112 }
1113 // -------------------------------------------------------------------------------
1114 //  -(void) saveConfigurationFiles
1115 // -------------------------------------------------------------------------------
1116 -(void) saveConfigurationFiles:(BOOL) makeBackup
1117 {
1118         NSError *err;
1119         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1120         NSMutableString *cellServDBString = [[NSMutableString alloc] initWithCapacity:256];
1121         NSMutableString *theseCellString = [[NSMutableString alloc] initWithCapacity:256];
1122         DBCellElement *cellElement = nil;
1123         
1124         // save the configuration file
1125         @try{
1126                 [self exceptionOnInvalidPath];
1127
1128                 // ThisCell
1129                 [filePath setString: @"/tmp/ThisCell"];
1130                 [cellName writeToFile: [filePath stringByExpandingTildeInPath]
1131                                    atomically:YES 
1132                                          encoding: NSASCIIStringEncoding 
1133                                                 error:nil];
1134                 // CellServDB
1135                 
1136                 for(int idx = 0; idx < [cellList count]; idx++){
1137                         cellElement = [cellList objectAtIndex:idx];
1138                         [cellServDBString appendString:[cellElement description]];
1139                         if([cellElement userDefaultForToken]) {
1140                                 [theseCellString  appendString:[cellElement getCellName]];
1141                                 [theseCellString  appendString:@"\n"];
1142                         }
1143                 }
1144                 
1145                 
1146                 [filePath setString: @"/tmp/CellServDB"];
1147                 [cellServDBString writeToFile: [filePath stringByExpandingTildeInPath]
1148                                                    atomically:YES 
1149                                                          encoding:  NSUTF8StringEncoding 
1150                                                                 error:&err];
1151                 
1152                 [filePath setString: @"/tmp/TheseCells"];
1153                 [theseCellString writeToFile: [filePath stringByExpandingTildeInPath]
1154                                                    atomically:YES 
1155                                                          encoding:  NSUTF8StringEncoding 
1156                                                                 error:&err];
1157                 
1158                 // backup original file
1159                 if([futil startAutorization] != noErr){
1160                         @throw [NSException exceptionWithName:@"saveConfigurationFiles:startAutorization" 
1161                                                                                    reason:kUserNotAuth
1162                                                                                  userInfo:nil];
1163                 }
1164
1165                 if(makeBackup) [self backupConfigurationFiles];
1166
1167                 // install ThisCell
1168                 [filePath setString: installationPath];
1169                 [filePath appendString: @"/etc/ThisCell"];
1170                 [self installConfigurationFile:@"/tmp/ThisCell" 
1171                                                           destPath:filePath];           
1172                 
1173                 // install CellServDB
1174                 [filePath setString: installationPath];
1175                 [filePath appendString: @"/etc/CellServDB"];
1176                 [self installConfigurationFile:@"/tmp/CellServDB" 
1177                                                           destPath:filePath];
1178                 
1179                 // install CellServDB
1180                 [filePath setString: installationPath];
1181                 [filePath appendString: @"/etc/TheseCells"];
1182                 [self installConfigurationFile:@"/tmp/TheseCells" 
1183                                                           destPath:filePath];
1184                 
1185         } @catch (NSException *e) {
1186                 @throw e;
1187         }@finally {
1188                 // dispose all variable used
1189                 if(filePath) [filePath release];
1190                 if(cellServDBString) [cellServDBString release];
1191         }
1192         
1193 }
1194
1195 // -------------------------------------------------------------------------------
1196 //  -(void) saveCacheConfigurationFiles
1197 // -------------------------------------------------------------------------------
1198 -(void) saveCacheConfigurationFiles:(BOOL)makeBackup
1199 {
1200         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];     
1201         // save the configuration file
1202         @try{
1203                 [self exceptionOnInvalidPath];
1204                 
1205                 // cacheinfo file creation
1206                 [self writeCacheInfo:@"/tmp/cacheinfo"];
1207                 
1208                 //afsd.option or afs.conf file creation
1209                 [self writeAfsdOption:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE];
1210                 
1211                 // backup original file
1212                 if([futil startAutorization] != noErr){
1213                         @throw [NSException exceptionWithName:@"AFSPropertyManager:saveCacheConfigurationFiles:startAutorization" 
1214                                                                                    reason:kUserNotAuth
1215                                                                                  userInfo:nil];
1216                 }
1217                 
1218                 if(makeBackup) {
1219                         //cacheinfo
1220                         [self backupFile:@"/etc/cacheinfo"];
1221                         
1222                         //afsd.options
1223                         [self backupFile:useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE]; 
1224                 }
1225                 
1226                 // install cacheinfo
1227                 [filePath setString:installationPath];
1228                 [filePath appendString: @"/etc/cacheinfo"];
1229                 [self installConfigurationFile:@"/tmp/cacheinfo" 
1230                                                           destPath:filePath];           
1231                 
1232                 // install afsd.conf or afs.conf configuration file 
1233                 [filePath setString: installationPath];
1234                 [filePath appendString: useAfsdConfVersion?AFSD_NEW_PREFERENCE_FILE:AFSD_OLD_PREFERENCE_FILE];
1235                 [self installConfigurationFile:useAfsdConfVersion?AFSD_TMP_NEW_PREFERENCE_FILE:AFSD_TMP_OLD_PREFERENCE_FILE
1236                                                           destPath:filePath];
1237                 
1238         } @catch (NSException *e) {
1239                 @throw e;
1240         }@finally {
1241                 if(filePath) [filePath release];
1242         }
1243         
1244         
1245 }
1246
1247 // -------------------------------------------------------------------------------
1248 //  -(void) installConfigurationFile
1249 // -------------------------------------------------------------------------------
1250 -(void) installConfigurationFile:(NSString*)srcConfFile 
1251                                                 destPath:(NSString*)destPath
1252 {
1253         // delete the file original file
1254
1255         if([futil autorizedDelete:destPath] != noErr){
1256                 @throw [NSException exceptionWithName:@"installConfigurationFile:autorizedDelete" 
1257                                                                            reason:destPath
1258                                                                          userInfo:nil];
1259         }
1260         
1261         // move the file
1262         if([futil autorizedMoveFile:srcConfFile
1263                                                  toPath:destPath] != noErr) {
1264                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedMoveFile" 
1265                                                                            reason:srcConfFile
1266                                                                          userInfo:nil];
1267         }
1268         
1269         
1270         if([futil autorizedChown:destPath 
1271                                            owner:@"root"
1272                                            group:@"wheel"]!= noErr) {
1273                 @throw [NSException exceptionWithName:@"saveConfigurationFiles:autorizedChown" 
1274                                                                            reason:destPath
1275                                                                          userInfo:nil];
1276         }
1277 }
1278
1279 // -------------------------------------------------------------------------------
1280 //  -(void) backupConfigurationFiles
1281 // -------------------------------------------------------------------------------
1282 -(void) backupConfigurationFiles
1283 {       
1284
1285         @try{
1286                 if([futil startAutorization] != noErr){
1287                         @throw [NSException exceptionWithName:@"backupConfigurationFiles:startAutorization" 
1288                                                                                    reason:kUserNotAuth
1289                                                                                  userInfo:nil];
1290                 }
1291                 //This cell
1292                 [self backupFile:@"/etc/ThisCell"];
1293         
1294                 //CellServDB
1295                 [self backupFile:@"/etc/CellServDB"];
1296                 
1297                 //TheseCell
1298                 [self backupFile:@"/etc/TheseCells"];
1299                 
1300                 //[futil endAutorization];
1301         } @catch (NSException *e) {
1302                 @throw e;
1303         } @finally {
1304         }
1305 }
1306
1307 // -------------------------------------------------------------------------------
1308 //  -(void) backupFile:(NSString*)localAfsFilePath
1309 // -------------------------------------------------------------------------------
1310 -(void) backupFile:(NSString*)localAfsFilePath
1311 {
1312         NSString *srcString = nil;
1313         NSMutableString *filePath = [[NSMutableString alloc] initWithCapacity:256];
1314         OSStatus err = noErr;
1315         @try{
1316                 [filePath setString: installationPath];
1317                 [filePath appendString: localAfsFilePath];
1318                 
1319                 //Check if the file at path exist
1320                 NSFileManager *fileManager = [NSFileManager defaultManager];
1321                 
1322                 //check if th efile exist
1323                 if(![fileManager fileExistsAtPath:[filePath stringByExpandingTildeInPath]]) return;
1324                 
1325                 // store the source path
1326                 srcString  = [filePath stringByExpandingTildeInPath];
1327                 [filePath appendString: @".afscommander_bk"];
1328                 
1329                 // check for presence of bk file
1330                 if(![[NSFileManager defaultManager] fileExistsAtPath:[filePath stringByExpandingTildeInPath]]){
1331                         // backup the file
1332                         err = [futil autorizedCopy:srcString 
1333                                                   toPath:[filePath stringByExpandingTildeInPath]];
1334                 }
1335         } @catch (NSException *e) {
1336                 @throw e;
1337         } @finally {
1338                 if(filePath) [filePath release];
1339         }
1340 }
1341
1342 // -------------------------------------------------------------------------------
1343 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1344 // -------------------------------------------------------------------------------
1345 -(BOOL) checkAfsStatus
1346 {
1347         BOOL result = NO;
1348         NSString *dfResult = [TaskUtil executeTaskSearchingPath:@"/bin/df" args:[NSArray arrayWithObjects:nil]];
1349         result = (dfResult?([dfResult rangeOfString:@AFS_FS_MOUNT].location != NSNotFound):NO);
1350         return result;  
1351 }
1352
1353 // -------------------------------------------------------------------------------
1354 //  checkAfsStatus:[NSArray arrayWithObjects:@"checkserver", nil];
1355 // -------------------------------------------------------------------------------
1356 -(BOOL) checkAfsStatusForStartup {
1357         BOOL result = NO;
1358                 //NSString *fsResult = [TaskUtil executeTaskSearchingPath:@"launchctl" args:[NSArray arrayWithObjects: @"list", nil]];
1359                 //result = (fsResult?([fsResult rangeOfString:@AFS_LAUNCHCTL_GREP_STR].location != NSNotFound):NO);
1360         return result;
1361 }
1362
1363 // -------------------------------------------------------------------------------
1364 //  makeChaceParamString
1365 // -------------------------------------------------------------------------------
1366 -(NSString*) makeChaceParamString
1367 {
1368         NSNumber *tmpNum = nil;
1369         NSMutableString *afsdOption = [[NSMutableString alloc] init];
1370         if(!afsdOption) return @"";
1371         //write the data for afsd config file '-afsdb -stat x -dcache x -daemons x -volumes x -dynroot -fakestat-all'
1372         //afsdb
1373         //dynRoot
1374         if([self afsDB]) {
1375                 [afsdOption appendString:@AFSD_OPTION_AFSDB_KEY];[afsdOption appendString:@" "];
1376         }
1377         
1378         //Verbose
1379         if([self verbose]) {
1380                 [afsdOption appendString:@AFSD_OPTION_VERBOSE_KEY];[afsdOption appendString:@" "];
1381         }
1382         
1383         //stat entry
1384         tmpNum = [NSNumber numberWithInt:[self statCacheEntry]];
1385         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_STAT_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1386         
1387         //dcace
1388         tmpNum = [NSNumber numberWithInt:[self dCacheDim]];
1389         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DCACHE_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1390         
1391         //daemons
1392         tmpNum = [NSNumber numberWithInt:[self daemonNumber]];
1393         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_DAEMONS_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1394         
1395         //volumes
1396         tmpNum = [NSNumber numberWithInt:[self nVolEntry]];
1397         if([tmpNum  intValue]) {[afsdOption appendString:@AFSD_OPTION_VOLUMES_KEY];[afsdOption appendString:@" "];[afsdOption appendString:[tmpNum stringValue]];[afsdOption appendString:@" "];}
1398         
1399         //dynRoot
1400         if([self dynRoot]) {
1401                 [afsdOption appendString:@AFSD_OPTION_DYNROOT_KEY];[afsdOption appendString:@" "];
1402         }
1403         
1404         //fakestat-all
1405         [afsdOption appendString:@AFSD_OPTION_FKESTAT_ALL];[afsdOption appendString:@" "];
1406         
1407         return [afsdOption autorelease];
1408 }
1409
1410 @end
1411