355a11314d16d31e243965227820735062b2a680
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 implement chown/chmod

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

27) 
28) // mock version of lchown, to see if right files get right owners
29) 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

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

73) 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

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

108) int main(int argc, char const **argv) {
109)   (void)argc;
110)   Config config;
111)   config.parseFile(argv[1]);
112)   config.setPermissions();
Stefan Schuermans finish tree traversal test

Stefan Schuermans authored 4 years ago

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