]> Dogcows Code - chaz/thecheat/blob - main.m
Remove support of Mac OS X 10.3 and earlier system, change codes for Mac OS X 10.7.
[chaz/thecheat] / main.m
1
2 /*
3 * The Cheat - The legendary universal game trainer for Mac OS X.
4 * http://www.brokenzipper.com/trac/wiki/TheCheat
5 *
6 * Copyright (c) 2003-2011, Charles McGarvey et al.
7 *
8 * Distributable under the terms and conditions of the 2-clause BSD
9 * license; see the file COPYING for the legal text of the license.
10 */
11
12 #import <Cocoa/Cocoa.h>
13 #import <Foundation/foundation.h>
14 #import <SecurityFoundation/SFAuthorization.h>
15 #import <Security/AuthorizationTags.h>
16 #include "ChazLog.h"
17 #include <sys/types.h>
18 #include <sys/uio.h>
19 #include <unistd.h>
20
21
22 /*
23 * I think this function was ripped from the iHaxGamez project, so it
24 * remains licensed under the GPL.
25 */
26 void authMe(char * FullPathToMe)
27 {
28 // get authorization as root
29
30 OSStatus myStatus;
31
32 // set up Authorization Item
33 AuthorizationItem myItems[1];
34 myItems[0].name = kAuthorizationRightExecute;
35 myItems[0].valueLength = 0;
36 myItems[0].value = NULL;
37 myItems[0].flags = 0;
38
39 // Set up Authorization Rights
40 AuthorizationRights myRights;
41 myRights.count = sizeof (myItems) / sizeof (myItems[0]);
42 myRights.items = myItems;
43
44 // set up Authorization Flags
45 AuthorizationFlags myFlags;
46 myFlags =
47 kAuthorizationFlagDefaults |
48 kAuthorizationFlagInteractionAllowed |
49 kAuthorizationFlagExtendRights;
50
51 // Create an Authorization Ref using Objects above. NOTE: Login bod comes up with this call.
52 AuthorizationRef myAuthorizationRef;
53 myStatus = AuthorizationCreate (&myRights, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
54
55 if (myStatus == errAuthorizationSuccess)
56 {
57 // prepare communication path - used to signal that process is loaded
58 FILE *myCommunicationsPipe = NULL;
59 char myReadBuffer[] = " ";
60
61 // run this app in GOD mode by passing authorization ref and comm pipe (asynchoronous call to external application)
62 myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef,FullPathToMe,kAuthorizationFlagDefaults,nil,&myCommunicationsPipe);
63
64 // external app is running asynchronously - it will send to stdout when loaded
65 if (myStatus == errAuthorizationSuccess)
66 {
67 read (fileno (myCommunicationsPipe), myReadBuffer, sizeof (myReadBuffer));
68 fclose(myCommunicationsPipe);
69 }
70
71 // release authorization reference
72 myStatus = AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDestroyRights);
73 }
74 }
75
76 /*
77 * I think this function was ripped from the iHaxGamez project, so it
78 * remains licensed under the GPL.
79 */
80 bool checkExecutablePermissions(void)
81 {
82 NSDictionary *applicationAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:[[NSBundle mainBundle] executablePath] traverseLink: YES];
83
84 // We expect 2755 as octal (1517 as decimal, -rwxr-sr-x as extended notation)
85 return ([applicationAttributes filePosixPermissions] == 1517 && [[applicationAttributes fileGroupOwnerAccountName] isEqualToString: @"procmod"]);
86 }
87
88 /*
89 * I think this function was ripped from the iHaxGamez project, so it
90 * remains licensed under the GPL.
91 */
92 bool amIWorthy(void)
93 {
94 // running as root?
95 AuthorizationRef myAuthRef;
96 OSStatus stat = AuthorizationCopyPrivilegedReference(&myAuthRef,kAuthorizationFlagDefaults);
97
98 return stat == errAuthorizationSuccess || checkExecutablePermissions();
99 }
100
101 int main( int argc, char *argv[] )
102 {
103 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
104
105 ChazLogDisable();
106
107 ChazDebugSetup();
108 ChazMapLogToDebug();
109
110 if (amIWorthy())
111 {
112 #ifndef _DEBUG
113 printf("Don't forget to flush! ;-) "); // signal back to close caller
114 #endif
115 fflush(stdout);
116
117 [pool release];
118 return NSApplicationMain(argc, (const char **) argv);
119 }
120 else
121 {
122 authMe(argv[0]);
123 [pool release];
124 return 0;
125 }
126
127 ChazDebugCleanup();
128 }
This page took 0.038798 seconds and 4 git commands to generate.