]>
Commit | Line | Data |
---|---|---|
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 | ||
31 | #include <Menes/Function.h> | |
32 | ||
33 | typedef Function<void, const char *, launch_data_t> LaunchDataIterator; | |
34 | ||
35 | void launch_data_dict_iterate(launch_data_t data, LaunchDataIterator code) { | |
36 | launch_data_dict_iterate(data, [](launch_data_t value, const char *name, void *baton) { | |
37 | (*static_cast<LaunchDataIterator *>(baton))(name, value); | |
38 | }, &code); | |
39 | } | |
40 | ||
232b396b | 41 | int main(int argc, char *argv[]) { |
195c97b3 JF |
42 | auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS)); |
43 | auto response(launch_msg(request)); | |
44 | launch_data_free(request); | |
45 | ||
46 | _assert(response != NULL); | |
47 | _assert(launch_data_get_type(response) == LAUNCH_DATA_DICTIONARY); | |
48 | ||
28398e34 | 49 | auto parent(getppid()); |
195c97b3 | 50 | |
28398e34 | 51 | auto cydia(false); |
195c97b3 JF |
52 | |
53 | launch_data_dict_iterate(response, [=, &cydia](const char *name, launch_data_t value) { | |
54 | if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY) | |
55 | return; | |
56 | ||
57 | auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID)); | |
58 | if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER) | |
59 | return; | |
60 | ||
61 | auto pid(launch_data_get_integer(integer)); | |
62 | if (pid != parent) | |
63 | return; | |
64 | ||
cf769c11 JF |
65 | auto variables(launch_data_dict_lookup(value, LAUNCH_JOBKEY_ENVIRONMENTVARIABLES)); |
66 | if (variables != NULL && launch_data_get_type(variables) == LAUNCH_DATA_DICTIONARY) { | |
688d4976 | 67 | auto dyld(false); |
cf769c11 JF |
68 | |
69 | launch_data_dict_iterate(variables, [&dyld](const char *name, launch_data_t value) { | |
70 | if (strncmp(name, "DYLD_", 5) == 0) | |
71 | dyld = true; | |
72 | }); | |
73 | ||
74 | if (dyld) | |
75 | return; | |
76 | } | |
77 | ||
195c97b3 | 78 | auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAM)); |
ca0a920d JF |
79 | if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING) { |
80 | auto array(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAMARGUMENTS)); | |
81 | if (array == NULL || launch_data_get_type(array) != LAUNCH_DATA_ARRAY) | |
82 | return; | |
83 | if (launch_data_array_get_count(array) == 0) | |
84 | return; | |
85 | ||
86 | string = launch_data_array_get_index(array, 0); | |
87 | if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING) | |
88 | return; | |
89 | } | |
195c97b3 JF |
90 | |
91 | auto program(launch_data_get_string(string)); | |
92 | if (program == NULL) | |
93 | return; | |
94 | ||
95 | if (strcmp(program, "/Applications/Cydia.app/Cydia") == 0) | |
96 | cydia = true; | |
97 | }); | |
98 | ||
99 | if (!cydia) { | |
100 | fprintf(stderr, "thou shalt not pass\n"); | |
101 | return EX_NOPERM; | |
102 | } | |
103 | ||
232b396b JF |
104 | setuid(0); |
105 | setgid(0); | |
0c0a966b JF |
106 | |
107 | if (argc < 2 || argv[1][0] != '/') | |
108 | argv[0] = "/usr/bin/dpkg"; | |
109 | else { | |
110 | --argc; | |
111 | ++argv; | |
112 | } | |
113 | ||
232b396b | 114 | execv(argv[0], argv); |
029bbf6f | 115 | return EX_UNAVAILABLE; |
232b396b | 116 | } |