-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfix.cpp
168 lines (154 loc) · 4.3 KB
/
fix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#include <acl/libacl.h>
#include <cerrno>
#include <cstdlib>
#include <cstring>
#include <filesystem>
#include <grp.h>
#include <iostream>
#include <pwd.h>
#include <string>
#include <sys/acl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
inline bool match_perms(const mode_t target, acl_entry_t entry) {
bool diff = false;
acl_permset_t permset;
acl_get_permset(entry, &permset);
if (acl_get_perm(permset, ACL_READ) != (target & S_IRGRP)) {
diff = true;
if(target & S_IRGRP) {
acl_add_perm(permset, ACL_READ);
} else {
acl_delete_perm(permset, ACL_READ);
}
}
if (acl_get_perm(permset, ACL_WRITE) != (target & S_IWGRP)) {
diff = true;
if(target & S_IWGRP) {
acl_add_perm(permset, ACL_WRITE);
} else {
acl_delete_perm(permset, ACL_WRITE);
}
}
if (acl_get_perm(permset, ACL_EXECUTE) != (target & S_IXGRP)) {
diff = true;
if(target & S_IXGRP) {
acl_add_perm(permset, ACL_EXECUTE);
} else {
acl_delete_perm(permset, ACL_EXECUTE);
}
}
if(diff) {
acl_set_permset(entry, permset);
}
return diff;
}
inline bool set_permset(const mode_t target, acl_entry_t entry) {
bool diff = false;
acl_permset_t permset;
if (acl_get_permset(entry, &permset) == -1){
std::cerr << "Failed to get permset" << std::endl;
return false;
}
if(target & S_IRGRP) {
diff = true;
acl_add_perm(permset, ACL_READ);
}
if(target & S_IWGRP) {
diff = true;
acl_add_perm(permset, ACL_WRITE);
}
if(target & S_IXGRP) {
diff = true;
acl_add_perm(permset, ACL_EXECUTE);
}
acl_set_permset(entry, permset);
return diff;
}
bool set_permset_for_uid(const std::string path, const int sudo_uid, const mode_t mode) {
acl_t acl = acl_get_file(path.c_str(), ACL_TYPE_ACCESS);
if (acl == nullptr) {
std::cerr << "Failed to get ACL from file: " << path << std::endl;
return false;
}
acl_entry_t entry;
int entryId = ACL_FIRST_ENTRY;
bool found = false;
bool modified = false;
while (acl_get_entry(acl, entryId, &entry) == 1) {
entryId = ACL_NEXT_ENTRY;
acl_tag_t tagType;
acl_get_tag_type(entry, &tagType);
if (tagType == ACL_USER) {
uid_t* entryUid = static_cast<uid_t*>(acl_get_qualifier(entry));
if (*entryUid == sudo_uid) {
found = true;
modified = match_perms(mode, entry);
acl_free(entryUid);
break;
}
acl_free(entryUid);
}
}
if(acl_valid(acl) == -1) {
std::cerr << "PRE_Invalid ACL" << std::endl;
acl_free(acl);
return false;
}
if (!found) {
acl_entry_t entry;
if(acl_create_entry(&acl, &entry) == -1) {
std::cerr << "Failed to create ACL entry for user: " << sudo_uid << std::endl;
acl_free(acl);
return false;
}
acl_set_tag_type(entry, ACL_USER);
id_t id = sudo_uid;
acl_set_qualifier(entry, &id);
acl_permset_t permset;
modified = set_permset(mode, entry);
}
if(modified) {
acl_calc_mask(&acl);
if(acl_valid(acl) == -1) {
std::cerr << "Invalid ACL" << std::endl;
acl_free(acl);
return false;
}
if(acl_set_file(path.c_str(), ACL_TYPE_ACCESS, acl) == -1) {
std::cerr << "Error setting ACL on " << path << ": "
<< strerror(errno) << std::endl;
acl_free(acl);
return false;
}
}
acl_free(acl);
return true;
}
int main() {
std::string path;
struct stat buf;
int sudo_uid = std::stoi(getenv("SUDO_UID"));
for (std::filesystem::recursive_directory_iterator
i("."), end;
i != end; ++i) {
path = i->path().string().substr(2);
if(lstat(path.c_str(), &buf) == -1) {
std::cerr << "Error getting path status on " << path << ": "
<< strerror(errno) << std::endl;
} else {
if(S_ISLNK(buf.st_mode)) {
continue;
}
}
mode_t mode = S_IRGRP;
if(S_ISDIR(buf.st_mode)){
mode |= S_IXGRP;
}
if(!set_permset_for_uid(path, sudo_uid, mode)) {
std::cerr << "Error setting ACL for user " << sudo_uid
<< " on " << path << std::endl;
}
}
}