]> git.saurik.com Git - cydia.git/blob - cydo.cpp
I am trying to get the habit of always using auto.
[cydia.git] / cydo.cpp
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
22 #include <cstdio>
23 #include <cstdlib>
24
25 #include <errno.h>
26 #include <sysexits.h>
27 #include <unistd.h>
28
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
41 int main(int argc, char *argv[]) {
42 auto log(fopen("/tmp/cydia.log", "a+"));
43 fprintf(log, "cydo:");
44 for (int arg(1); arg < argc; ++arg)
45 fprintf(log, " %s", argv[arg]);
46 fprintf(log, "\n");
47
48 auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS));
49 auto response(launch_msg(request));
50 launch_data_free(request);
51
52 _assert(response != NULL);
53 _assert(launch_data_get_type(response) == LAUNCH_DATA_DICTIONARY);
54
55 auto parent(getppid());
56
57 auto cydia(false);
58
59 launch_data_dict_iterate(response, [=, &cydia](const char *name, launch_data_t value) {
60 if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY)
61 return;
62
63 auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID));
64 if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER)
65 return;
66
67 auto pid(launch_data_get_integer(integer));
68 if (pid != parent)
69 return;
70
71 auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAM));
72 if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING) {
73 auto array(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PROGRAMARGUMENTS));
74 if (array == NULL || launch_data_get_type(array) != LAUNCH_DATA_ARRAY)
75 return;
76 if (launch_data_array_get_count(array) == 0)
77 return;
78
79 string = launch_data_array_get_index(array, 0);
80 if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING)
81 return;
82 }
83
84 auto program(launch_data_get_string(string));
85 if (program == NULL)
86 return;
87
88 fprintf(log, "%lld %s\n", pid, program);
89 if (strcmp(program, "/Applications/Cydia.app/Cydia") == 0)
90 cydia = true;
91 });
92
93 if (!cydia) {
94 fprintf(stderr, "thou shalt not pass\n");
95 return EX_NOPERM;
96 }
97
98 fflush(log);
99 fclose(log);
100
101 setuid(0);
102 setgid(0);
103
104 if (argc < 2 || argv[1][0] != '/')
105 argv[0] = "/usr/bin/dpkg";
106 else {
107 --argc;
108 ++argv;
109 }
110
111 execv(argv[0], argv);
112 return EX_UNAVAILABLE;
113 }