]> Dogcows Code - chaz/thecheat/blob - main.m
The Cheat 1.2.4
[chaz/thecheat] / main.m
1
2 // **********************************************************************
3 // The Cheat - A universal game cheater for Mac OS X
4 // (C) 2003-2005 Chaz McGarvey (BrokenZipper)
5 //
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 1, or (at your option)
9 // any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 //
20
21 #import <Cocoa/Cocoa.h>
22 #import <Foundation/foundation.h>
23 #import <SecurityFoundation/SFAuthorization.h>
24 #import <Security/AuthorizationTags.h>
25 #include "ChazLog.h"
26 #include <sys/types.h>
27 #include <sys/uio.h>
28 #include <unistd.h>
29
30 void authMe(char * FullPathToMe)
31 {
32 // get authorization as root
33
34 OSStatus myStatus;
35
36 // set up Authorization Item
37 AuthorizationItem myItems[1];
38 myItems[0].name = kAuthorizationRightExecute;
39 myItems[0].valueLength = 0;
40 myItems[0].value = NULL;
41 myItems[0].flags = 0;
42
43 // Set up Authorization Rights
44 AuthorizationRights myRights;
45 myRights.count = sizeof (myItems) / sizeof (myItems[0]);
46 myRights.items = myItems;
47
48 // set up Authorization Flags
49 AuthorizationFlags myFlags;
50 myFlags =
51 kAuthorizationFlagDefaults |
52 kAuthorizationFlagInteractionAllowed |
53 kAuthorizationFlagExtendRights;
54
55 // Create an Authorization Ref using Objects above. NOTE: Login bod comes up with this call.
56 AuthorizationRef myAuthorizationRef;
57 myStatus = AuthorizationCreate (&myRights, kAuthorizationEmptyEnvironment, myFlags, &myAuthorizationRef);
58
59 if (myStatus == errAuthorizationSuccess)
60 {
61 // prepare communication path - used to signal that process is loaded
62 FILE *myCommunicationsPipe = NULL;
63 char myReadBuffer[] = " ";
64
65 // run this app in GOD mode by passing authorization ref and comm pipe (asynchoronous call to external application)
66 myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef,FullPathToMe,kAuthorizationFlagDefaults,nil,&myCommunicationsPipe);
67
68 // external app is running asynchronously - it will send to stdout when loaded
69 if (myStatus == errAuthorizationSuccess)
70 {
71 read (fileno (myCommunicationsPipe), myReadBuffer, sizeof (myReadBuffer));
72 fclose(myCommunicationsPipe);
73 }
74
75 // release authorization reference
76 myStatus = AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDestroyRights);
77 }
78 }
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 bool amIWorthy(void)
89 {
90 // running as root?
91 AuthorizationRef myAuthRef;
92 OSStatus stat = AuthorizationCopyPrivilegedReference(&myAuthRef,kAuthorizationFlagDefaults);
93
94 return stat == errAuthorizationSuccess || checkExecutablePermissions();
95 }
96
97 int main( int argc, char *argv[] )
98 {
99 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
100
101 ChazLogDisable();
102
103 ChazDebugSetup();
104 ChazMapLogToDebug();
105
106 #ifdef __ppc__
107 // PPC machines whose operating system is below leopard do not need authorization
108 SInt32 osxMajorVersion;
109 Gestalt(gestaltSystemVersionMinor, &osxMajorVersion);
110 if (osxMajorVersion < 5)
111 {
112 [pool release];
113 return NSApplicationMain(argc, (const char **) argv);
114 }
115 #endif
116
117 if (amIWorthy())
118 {
119 #ifndef _DEBUG
120 printf("Don't forget to flush! ;-) "); // signal back to close caller
121 #endif
122 fflush(stdout);
123
124 [pool release];
125 return NSApplicationMain(argc, (const char **) argv);
126 }
127 else
128 {
129 authMe(argv[0]);
130 [pool release];
131 return 0;
132 }
133
134 ChazDebugCleanup();
135 }
This page took 0.033434 seconds and 4 git commands to generate.