]> git.saurik.com Git - cydia.git/blame - cydo.cpp
Finally found the crazy APT memory corruption bug.
[cydia.git] / cydo.cpp
CommitLineData
54ab55b8
JF
1/* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2015 Jay Freeman (saurik)
3*/
4
5/* GNU General Public License, Version 3 {{{ */
6/*
7 * Cydia is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
11 *
12 * Cydia is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with Cydia. If not, see <http://www.gnu.org/licenses/>.
19**/
20/* }}} */
21
195c97b3
JF
22#include <cstdio>
23#include <cstdlib>
24
25#include <errno.h>
029bbf6f 26#include <sysexits.h>
232b396b
JF
27#include <unistd.h>
28
195c97b3
JF
29#include <launch.h>
30
af7a541a
JF
31#include <sys/stat.h>
32
195c97b3
JF
33#include <Menes/Function.h>
34
35typedef Function<void, const char *, launch_data_t> LaunchDataIterator;
36
37void launch_data_dict_iterate(launch_data_t data, LaunchDataIterator code) {
38 launch_data_dict_iterate(data, [](launch_data_t value, const char *name, void *baton) {
39 (*static_cast<LaunchDataIterator *>(baton))(name, value);
40 }, &code);
41}
42
232b396b 43int main(int argc, char *argv[]) {
195c97b3
JF
44 auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS));
45 auto response(launch_msg(request));
46 launch_data_free(request);
47
48 _assert(response != NULL);
49 _assert(launch_data_get_type(response) == LAUNCH_DATA_DICTIONARY);
50
28398e34 51 auto parent(getppid());
195c97b3 52
28398e34 53 auto cydia(false);
195c97b3 54
af7a541a
JF
55 struct stat correct;
56 if (lstat("/Applications/Cydia.app/Cydia", &correct) == -1) {
57 fprintf(stderr, "you have no arms left");
58 return EX_NOPERM;
59 }
60
195c97b3 61 launch_data_dict_iterate(response, [=, &cydia](const char *name, launch_data_t value) {
e0c35524 62 if (launch_data_get_type(value) != LAUNCH_DATA_DICTIONARY)
195c97b3
JF
63 return;
64
65 auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID));
66 if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER)
67 return;
68
69 auto pid(launch_data_get_integer(integer));
70 if (pid != parent)
71 return;
72
cf769c11
JF
73 auto variables(launch_data_dict_lookup(value, LAUNCH_JOBKEY_ENVIRONMENTVARIABLES));
74 if (variables != NULL && launch_data_get_type(variables) == LAUNCH_DATA_DICTIONARY) {
688d4976 75 auto dyld(false);
cf769c11
JF
76
77 launch_data_dict_iterate(variables, [&dyld](const char *name, launch_data_t value) {
78 if (strncmp(name, "DYLD_", 5) == 0)
79 dyld = true;
80 });
81
82 if (dyld)
83 return;
84 }
85
195c97b3 86 auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAM));
ca0a920d
JF
87 if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING) {
88 auto array(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAMARGUMENTS));
89 if (array == NULL || launch_data_get_type(array) != LAUNCH_DATA_ARRAY)
90 return;
91 if (launch_data_array_get_count(array) == 0)
92 return;
93
94 string = launch_data_array_get_index(array, 0);
95 if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING)
96 return;
97 }
195c97b3
JF
98
99 auto program(launch_data_get_string(string));
100 if (program == NULL)
101 return;
102
af7a541a
JF
103 struct stat check;
104 if (lstat(program, &check) == -1)
105 return;
106
107 if (correct.st_dev == check.st_dev && correct.st_ino == check.st_ino)
195c97b3
JF
108 cydia = true;
109 });
110
111 if (!cydia) {
af7a541a 112 fprintf(stderr, "none shall pass\n");
195c97b3
JF
113 return EX_NOPERM;
114 }
115
232b396b
JF
116 setuid(0);
117 setgid(0);
0c0a966b
JF
118
119 if (argc < 2 || argv[1][0] != '/')
120 argv[0] = "/usr/bin/dpkg";
121 else {
122 --argc;
123 ++argv;
124 }
125
232b396b 126 execv(argv[0], argv);
029bbf6f 127 return EX_UNAVAILABLE;
232b396b 128}