Stefan Schuermans commited on 2020-08-24 20:04:04
Showing 3 changed files, with 121 additions and 8 deletions.
| ... | ... |
@@ -1,27 +1,138 @@ |
| 1 | 1 |
#include <permissioner/Config.h> |
| 2 | 2 |
|
| 3 | 3 |
#include <cstdlib> |
| 4 |
-#include <iostream> // DEBUG |
|
| 5 |
-#include <unistd.h> |
|
| 4 |
+#include <grp.h> |
|
| 5 |
+#include <iomanip> |
|
| 6 |
+#include <iostream> |
|
| 7 |
+#include <pwd.h> |
|
| 6 | 8 |
#include <sys/stat.h> |
| 9 |
+#include <sys/types.h> |
|
| 10 |
+#include <unistd.h> |
|
| 11 |
+#include <vector> |
|
| 12 |
+ |
|
| 13 |
+struct LogLchown {
|
|
| 14 |
+ std::string pathname; |
|
| 15 |
+ uid_t owner; |
|
| 16 |
+ gid_t group; |
|
| 17 |
+}; |
|
| 18 |
+std::vector<LogLchown> logs_lchown; |
|
| 7 | 19 |
|
| 8 | 20 |
// mock version of lchown, to see if right files get right owners |
| 9 | 21 |
extern "C" int lchown(const char *pathname, uid_t owner, gid_t group) {
|
| 10 |
- std::cout << "DEBUG lchown " << pathname << " owner " << owner |
|
| 11 |
- << " group " << group << std::endl; |
|
| 22 |
+ logs_lchown.emplace_back(LogLchown { pathname, owner, group });
|
|
| 12 | 23 |
return 0; |
| 13 | 24 |
} |
| 14 | 25 |
|
| 15 |
-// mock version fo chmod, to see if right files get right permissions |
|
| 26 |
+bool check_lchown(unsigned int idx, std::string const &path_suffix, |
|
| 27 |
+ uid_t owner, gid_t group) {
|
|
| 28 |
+ bool ret = true; |
|
| 29 |
+ if (idx > logs_lchown.size()) {
|
|
| 30 |
+ std::cerr << "no such lchown call #" << idx << std::endl; |
|
| 31 |
+ return false; |
|
| 32 |
+ } |
|
| 33 |
+ LogLchown const & log_lchown = logs_lchown.at(idx); |
|
| 34 |
+ std::string pn = log_lchown.pathname; |
|
| 35 |
+ if (pn.length() < path_suffix.length() || |
|
| 36 |
+ pn.substr(pn.length() - path_suffix.length()) != path_suffix) {
|
|
| 37 |
+ std::cerr << "lchown call #" << idx << ": unexpcted path \"" |
|
| 38 |
+ << pn << "\" != ...\"" << path_suffix << "\"" << std::endl; |
|
| 39 |
+ ret = false; |
|
| 40 |
+ } |
|
| 41 |
+ if (log_lchown.owner != owner) {
|
|
| 42 |
+ std::cerr << "lchown call #" << idx << ": unexpected owner " |
|
| 43 |
+ << log_lchown.owner << " != " << owner << std::endl; |
|
| 44 |
+ ret = false; |
|
| 45 |
+ } |
|
| 46 |
+ if (log_lchown.group != group) {
|
|
| 47 |
+ std::cerr << "lchown call #" << idx << ": unexpected group " |
|
| 48 |
+ << log_lchown.group << " != " << group << std::endl; |
|
| 49 |
+ ret = false; |
|
| 50 |
+ } |
|
| 51 |
+ return ret; |
|
| 52 |
+} |
|
| 53 |
+ |
|
| 54 |
+struct LogChmod {
|
|
| 55 |
+ std::string pathname; |
|
| 56 |
+ mode_t mode; |
|
| 57 |
+}; |
|
| 58 |
+std::vector<LogChmod> logs_chmod; |
|
| 59 |
+ |
|
| 60 |
+// mock version of chmod, to see if right files get right permissions |
|
| 16 | 61 |
extern "C" int chmod(const char *pathname, mode_t mode) {
|
| 17 |
- std::cout << "DEBUG chmod " << pathname << " mode " << mode << std::endl; |
|
| 62 |
+ logs_chmod.emplace_back(LogChmod { pathname, mode });
|
|
| 18 | 63 |
return 0; |
| 19 | 64 |
} |
| 20 | 65 |
|
| 66 |
+bool check_chmod(unsigned int idx, std::string const &path_suffix, |
|
| 67 |
+ mode_t mode) {
|
|
| 68 |
+ bool ret = true; |
|
| 69 |
+ if (idx > logs_chmod.size()) {
|
|
| 70 |
+ std::cerr << "no such chmod call #" << idx << std::endl; |
|
| 71 |
+ return false; |
|
| 72 |
+ } |
|
| 73 |
+ LogChmod const & log_chmod = logs_chmod.at(idx); |
|
| 74 |
+ std::string pn = log_chmod.pathname; |
|
| 75 |
+ if (pn.length() < path_suffix.length() || |
|
| 76 |
+ pn.substr(pn.length() - path_suffix.length()) != path_suffix) {
|
|
| 77 |
+ std::cerr << "chmod call #" << idx << ": unexpcted path \"" |
|
| 78 |
+ << pn << "\" != ...\"" << path_suffix << "\"" << std::endl; |
|
| 79 |
+ ret = false; |
|
| 80 |
+ } |
|
| 81 |
+ if (log_chmod.mode != mode) {
|
|
| 82 |
+ std::cerr << "chmod call #" << idx << ": unexpected mode " |
|
| 83 |
+ << std::oct << log_chmod.mode << " != " << mode |
|
| 84 |
+ << std::dec << std::endl; |
|
| 85 |
+ ret = false; |
|
| 86 |
+ } |
|
| 87 |
+ return ret; |
|
| 88 |
+} |
|
| 89 |
+ |
|
| 90 |
+bool check(unsigned int idx, std::string const &path_suffix, |
|
| 91 |
+ uid_t owner, gid_t group, mode_t mode) {
|
|
| 92 |
+ bool ret = true; |
|
| 93 |
+ if (! check_lchown(idx, path_suffix, owner, group)) {
|
|
| 94 |
+ ret = false; |
|
| 95 |
+ } |
|
| 96 |
+ if (! check_chmod(idx, path_suffix, mode)) {
|
|
| 97 |
+ ret = false; |
|
| 98 |
+ } |
|
| 99 |
+ return ret; |
|
| 100 |
+} |
|
| 101 |
+ |
|
| 21 | 102 |
int main(int argc, char const **argv) {
|
| 22 | 103 |
(void)argc; |
| 23 | 104 |
Config config; |
| 24 | 105 |
config.parseFile(argv[1]); |
| 25 | 106 |
config.setPermissions(); |
| 26 |
- return EXIT_SUCCESS; |
|
| 107 |
+ |
|
| 108 |
+ int ret = EXIT_SUCCESS; |
|
| 109 |
+ |
|
| 110 |
+ uid_t nobody = getpwnam("nobody")->pw_uid;
|
|
| 111 |
+ gid_t nogroup = getgrnam("nogroup")->gr_gid;
|
|
| 112 |
+ |
|
| 113 |
+ if (! check(0, "work", nobody, nogroup, 0775)) {
|
|
| 114 |
+ ret = EXIT_FAILURE; |
|
| 115 |
+ } |
|
| 116 |
+ if (! check(1, "work/file", nobody, nogroup, 0664)) {
|
|
| 117 |
+ ret = EXIT_FAILURE; |
|
| 118 |
+ } |
|
| 119 |
+ if (! check(2, "work/nested", -1, -1, 0757)) {
|
|
| 120 |
+ ret = EXIT_FAILURE; |
|
| 121 |
+ } |
|
| 122 |
+ if (! check(3, "work/nested/other", -1, -1, 0646)) {
|
|
| 123 |
+ ret = EXIT_FAILURE; |
|
| 124 |
+ } |
|
| 125 |
+ unsigned int size = 4; |
|
| 126 |
+ if (logs_lchown.size() != size) {
|
|
| 127 |
+ std::cerr << "unexpected size of logs_lchown: " << logs_lchown.size() |
|
| 128 |
+ << " != " << size << std::endl; |
|
| 129 |
+ ret = EXIT_FAILURE; |
|
| 130 |
+ } |
|
| 131 |
+ if (logs_chmod.size() != size) {
|
|
| 132 |
+ std::cerr << "unexpected size of logs_chmod: " << logs_chmod.size() |
|
| 133 |
+ << " != " << size << std::endl; |
|
| 134 |
+ ret = EXIT_FAILURE; |
|
| 135 |
+ } |
|
| 136 |
+ |
|
| 137 |
+ return ret; |
|
| 27 | 138 |
} |