5 // Created by Claudio on 28/06/07.
11 #include <Security/Authorization.h>
12 #include <Security/AuthorizationTags.h>
13 #include <sys/param.h>
16 #include <sys/types.h>
17 #include <sys/fcntl.h>
18 #include <sys/errno.h>
23 #include <sys/event.h>
24 #include <mach-o/dyld.h>
27 #import "PListManager.h"
28 void stopAfs(int argc, char *argv[]);
29 void getPath(char **selfPathPtr);
30 void selfRepair(char *selfPath);
31 void runWithSelfRepair(char *selfPath,int argc, char *argv[]);
32 void runCommand(int argc, char *argv[]);
34 int main(int argc, char *argv[])
36 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
39 NSLog(@"num of arguments %d", argc);
40 int status = [[AuthUtil shared] autorize];
41 if(status != noErr) exit(-1);
43 // Get the path to the tool's executable
46 //selfRepair(selfPath);
47 // All done with the executable path
48 if(selfPath) free(selfPath);
50 // Now do the real work of running the command.
51 runCommand(argc, argv);
52 [[AuthUtil shared] deautorize];
59 void runCommand(int argc, char *argv[])
61 NSString *cmdString = [NSString stringWithCString:(const char *)argv[1] encoding:NSUTF8StringEncoding];
63 if(argc == 4 && [cmdString rangeOfString:@"stop_afs"].location!=NSNotFound ){
64 NSLog(@"Stop afs from helper");
66 } else if(argc == 4 && [cmdString rangeOfString:@"start_afs"].location!=NSNotFound){
67 NSLog(@"Start afs from helper");
69 const char *startArgs[] = {argv[2], argv[3], 0L};
70 [[AuthUtil shared] execUnixCommand:argv[1]
73 } else if(argc == 4 && [cmdString rangeOfString:@"enable_krb5_startup"].location!=NSNotFound) {
74 NSLog(@"Manage KRB5 at login time with option %s from helper", argv[2]);
76 int arg2 = atoi(argv[2]);
77 [PListManager krb5TiketAtLoginTime:[[NSNumber numberWithInt:arg2] boolValue]];
79 } else if(argc == 5 && [cmdString rangeOfString:@"start_afs_at_startup"].location!=NSNotFound){
81 NSLog(@"Manage start_afs_at_startup with option %s from helper", argv[2]);
82 [PListManager manageAfsStartupLaunchdFile:YES
83 afsStartupScript:[NSString stringWithCString:argv[2]]
84 afsBasePath:[NSString stringWithCString:argv[4]]
85 afsdPath:[NSString stringWithCString:argv[3]]];
89 void stopAfs(int argc, char *argv[])
94 const char *umountArgs[] = {"-f", "/afs", 0L};
95 [[AuthUtil shared] execUnixCommand:"/sbin/umount"
99 const char *afsdArgs[] = {"-shutdown", 0L};
100 [[AuthUtil shared] execUnixCommand:argv[3]
104 const char *kernelExtArgs[] = {argv[2], 0L};
105 [[AuthUtil shared] execUnixCommand:"/sbin/kextunload"
109 [[AuthUtil shared] deautorize];
113 // Code to get the path to the executable using _NSGetExecutablePath.
114 void getPath(char **selfPathPtr)
116 uint32_t selfPathSize = MAXPATHLEN;
117 if(!(*selfPathPtr = malloc(selfPathSize)))
121 if(_NSGetExecutablePath(*selfPathPtr, &selfPathSize) == -1)
123 // Try reallocating selfPath with the size returned by the function.
124 if(!(*selfPathPtr = realloc(*selfPathPtr, selfPathSize + 1)))
126 NSLog(@"Could not allocate memory to hold executable path.");
129 if(_NSGetExecutablePath(*selfPathPtr, &selfPathSize) != 0)
131 NSLog(@"Could not get executable path.");
137 // Self-repair code. Found somehwere in internet
138 void selfRepair(char *selfPath)
142 printf("selfRepair");
144 // [[AuthUtil shared] autorize];
146 // Open tool exclusively, noone can touch it when we work on it, this idea i kepped somewhere in internet
147 fdTool = open(selfPath, O_NONBLOCK | O_RDONLY | O_EXLOCK, 0);
151 NSLog(@"Open Filed: %d.", errno);
155 if(fstat(fdTool, &st))
157 NSLog(@"fstat failed.");
163 fchown(fdTool, 0, st.st_gid);
164 } else NSLog(@"st_uid = 0");
166 // Disable group and world writability and make setuid root.
167 fchmod(fdTool, (st.st_mode & (~(S_IWGRP | S_IWOTH))) | S_ISUID);
171 NSLog(@"Self-repair done.");
175 // Code to execute the tool in self-repair mode.
176 void runWithSelfRepair(char *selfPath, int argc, char *argv[])
182 // Make the qargs array for passing to child the same args of father
183 const char *arguments[] = {argv[1], argv[2], argv[3], "--self-repair", 0L};
185 // Get the privileged AuthorizationRef
186 [[AuthUtil shared] autorize];
187 [[AuthUtil shared] execUnixCommand:selfPath
192 if(pid == -1 || !WIFEXITED(status))
194 NSLog(@"Error returned from wait().");
198 // Exit with the same exit code as the self-repair child
199 exit(WEXITSTATUS(status));