bdbf09afe8d2cd07513a908431a50e641cecc28d
Stefan Schuermans add copyright & license

Stefan Schuermans authored 4 years ago

1) /**
2)  * Permissioner: set file ownerships and permissions
3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
4)  * Copyleft: GNU GENERAL PUBLIC LICENSE version 3 (see LICENSE)
5)  */
6) 
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

7) #include <permissioner/Callback.h>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

8) #include <permissioner/Config.h>
9) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

10) #include <boost/filesystem.hpp>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

11) #include <cstdlib>
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

12) #include <grp.h>
13) #include <iomanip>
14) #include <iostream>
15) #include <pwd.h>
Stefan Schuermans complete config text, fix c...

Stefan Schuermans authored 4 years ago

16) #include <string>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

17) #include <sys/stat.h>
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

18) #include <sys/types.h>
19) #include <unistd.h>
20) #include <vector>
21) 
22) struct LogLchown {
23)   std::string pathname;
24)   uid_t owner;
25)   gid_t group;
26) };
27) std::vector<LogLchown> logs_lchown;
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

28) 
29) // mock version of lchown, to see if right files get right owners
30) extern "C" int lchown(const char *pathname, uid_t owner, gid_t group) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

31)   logs_lchown.emplace_back(LogLchown { pathname, owner, group });
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

32)   return 0;
33) }
34) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

35) bool check_lchown(unsigned int idx, std::string const &path,
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

36)                   uid_t owner, gid_t group) {
37)   bool ret = true;
38)   if (idx > logs_lchown.size()) {
39)     std::cerr << "no such lchown call #" << idx << std::endl;
40)     return false;
41)   }
42)   LogLchown const & log_lchown = logs_lchown.at(idx);
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

43)   if (log_lchown.pathname != path) {
44)     std::cerr << "lchown call #" << idx << ": unexpected path \""
45)               << log_lchown.pathname << "\" != ...\"" << path << "\""
46)               << std::endl;
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

47)     ret = false;
48)   }
49)   if (log_lchown.owner != owner) {
50)     std::cerr << "lchown call #" << idx << ": unexpected owner "
51)               << log_lchown.owner << " != " << owner << std::endl;
52)     ret = false;
53)   }
54)   if (log_lchown.group != group) {
55)     std::cerr << "lchown call #" << idx << ": unexpected group "
56)               << log_lchown.group << " != " << group << std::endl;
57)     ret = false;
58)   }
59)   return ret;
60) }
61) 
62) struct LogChmod {
63)   std::string pathname;
64)   mode_t mode;
65) };
66) std::vector<LogChmod> logs_chmod;
67) 
68) // mock version of chmod, to see if right files get right permissions
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

69) extern "C" int chmod(const char *pathname, mode_t mode) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

70)   logs_chmod.emplace_back(LogChmod { pathname, mode });
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

71)   return 0;
72) }
73) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

74) bool check_chmod(unsigned int idx, std::string const &path, mode_t mode) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

75)   bool ret = true;
76)   if (idx > logs_chmod.size()) {
77)     std::cerr << "no such chmod call #" << idx << std::endl;
78)     return false;
79)   }
80)   LogChmod const & log_chmod = logs_chmod.at(idx);
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

81)   if (log_chmod.pathname != path) {
82)     std::cerr << "chmod call #" << idx << ": unexpected path \""
83)               << log_chmod.pathname << "\" != ...\"" << path << "\""
84)               << std::endl;
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

85)     ret = false;
86)   }
87)   if (log_chmod.mode != mode) {
88)     std::cerr << "chmod call #" << idx << ": unexpected mode "
89)               << std::oct << log_chmod.mode << " != " << mode
90)               << std::dec << std::endl;
91)     ret = false;
92)   }
93)   return ret;
94) }
95) 
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

96) bool check(unsigned int idx, std::string const &rel_path,
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

97)            uid_t owner, gid_t group, mode_t mode) {
98)   bool ret = true;
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

99)   std::string path = boost::filesystem::canonical(rel_path).string();
100)   if (! check_lchown(idx, path, owner, group)) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

101)     ret = false;
102)   }
Stefan Schuermans tree traversal test: check...

Stefan Schuermans authored 4 years ago

103)   if (! check_chmod(idx, path, mode)) {
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

104)     ret = false;
105)   }
106)   return ret;
107) }
108) 
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

109) int main(int argc, char const **argv) {
110)   (void)argc;
111)   Config config;
112)   config.parseFile(argv[1]);
Stefan Schuermans fix long shutdown delay aft...

Stefan Schuermans authored 4 years ago

113)   Callback callback;
114)   config.setPermissions(callback);
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

115) 
116)   int ret = EXIT_SUCCESS;
117) 
118)   uid_t nobody = getpwnam("nobody")->pw_uid;
119)   gid_t nogroup = getgrnam("nogroup")->gr_gid;
120) 
121)   if (! check(0, "work", nobody, nogroup, 0775)) {
122)     ret = EXIT_FAILURE;
123)   }
124)   if (! check(1, "work/file", nobody, nogroup, 0664)) {
125)     ret = EXIT_FAILURE;
126)   }
127)   if (! check(2, "work/nested", -1, -1, 0757)) {
128)     ret = EXIT_FAILURE;
129)   }
130)   if (! check(3, "work/nested/other", -1, -1, 0646)) {
131)     ret = EXIT_FAILURE;
132)   }
133)   unsigned int size = 4;
134)   if (logs_lchown.size() != size) {
135)     std::cerr << "unexpected size of logs_lchown: " << logs_lchown.size()
136)               << " != " << size << std::endl;
137)     ret = EXIT_FAILURE;
138)   }
139)   if (logs_chmod.size() != size) {
140)     std::cerr << "unexpected size of logs_chmod: " << logs_chmod.size()
141)               << " != " << size << std::endl;
142)     ret = EXIT_FAILURE;
143)   }
144) 
145)   return ret;