]> git.saurik.com Git - cydia.git/blob - setnsfpn.cpp
Carefully set NSFileProtectionNone inside of /var.
[cydia.git] / setnsfpn.cpp
1 /* Cydia - iPhone UIKit Front-End for Debian APT
2 * Copyright (C) 2008-2014 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 <fcntl.h>
23 #include <dirent.h>
24 #include <unistd.h>
25 #include <stdio.h>
26 #include <errno.h>
27 #include <string.h>
28 #include <sys/stat.h>
29
30 #define _syscall(expr) ({ typeof(expr) _value; for (;;) { \
31 _value = (expr); \
32 if ((long) _value != -1) \
33 break; \
34 if (errno != EINTR) { \
35 perror(#expr); \
36 return -1; \
37 } \
38 } _value; })
39
40 int $getdirentries(int, char *, int, long *) __asm("_getdirentries");
41
42 struct $dirent {
43 __uint32_t d_ino;
44 __uint16_t d_reclen;
45 __uint8_t d_type;
46 __uint8_t d_namlen;
47 char d_name[__DARWIN_MAXNAMLEN + 1];
48 };
49
50 #define getdirentries $getdirentries
51 #define dirent $dirent
52
53 enum Recurse {
54 RecurseYes,
55 RecurseNo,
56 RecurseMaybe,
57 };
58
59 struct File {
60 int fd_;
61
62 File(int fd);
63 ~File();
64
65 operator int() const;
66 };
67
68 File::File(int fd) :
69 fd_(fd)
70 {
71 }
72
73 File::~File() {
74 close(fd_);
75 }
76
77 File::operator int() const {
78 return fd_;
79 }
80
81 static int setnsfpn(const char *path, size_t before, Recurse recurse) {
82 File fd(_syscall(open_dprotected_np(path, O_RDONLY | O_SYMLINK, 0, O_DP_GETRAWENCRYPTED)));
83
84 if (recurse == RecurseMaybe) {
85 struct stat stat;
86 _syscall(fstat(fd, &stat));
87 switch (stat.st_mode & S_IFMT) {
88 case S_IFLNK:
89 return 0;
90 default:
91 return -1;
92
93 case S_IFDIR:
94 recurse = RecurseYes;
95 break;
96 case S_IFREG:
97 recurse = RecurseNo;
98 break;
99 }
100 }
101
102 int mode(_syscall(fcntl(fd, F_GETPROTECTIONCLASS)));
103 if (mode != -1 && mode != 4) {
104 if (recurse == RecurseYes) {
105 long address(0);
106
107 for (;;) {
108 char buffer[4096];
109 int size(_syscall(getdirentries(fd, buffer, sizeof(buffer), &address)));
110 if (size == 0)
111 break;
112
113 const char *next(buffer), *stop(next + size);
114 while (next != stop) {
115 const dirent *dir(reinterpret_cast<const dirent *>(next));
116 const char *name(dir->d_name);
117 size_t after(strlen(name));
118
119 if (false);
120 else if (after == 1 && name[0] == '.');
121 else if (after == 2 && name[0] == '.' && name[1] == '.');
122 else {
123 size_t both(before + 1 + after);
124 char sub[both + 1];
125 memcpy(sub, path, before);
126 sub[before] = '/';
127 memcpy(sub + before + 1, name, after);
128 sub[both] = '\0';
129
130 switch (dir->d_type) {
131 case DT_LNK:
132 break;
133 default:
134 return -1;
135
136 case DT_DIR:
137 setnsfpn(sub, both, RecurseYes);
138 break;
139 case DT_REG:
140 setnsfpn(sub, both, RecurseNo);
141 break;
142 }
143 }
144
145 next += dir->d_reclen;
146 }
147 }
148 }
149
150 _syscall(fcntl(fd, F_SETPROTECTIONCLASS, 4));
151 }
152
153 return 0;
154 }
155
156 int main(int argc, const char *argv[]) {
157 return setnsfpn(argv[1], strlen(argv[1]), RecurseMaybe);
158 }