| 134 | } |
| 135 | |
| 136 | int Daemon::checkFilePermissions(const std::string& path, |
| 137 | const mode_t permissions) |
| 138 | { |
| 139 | struct stat file_stat; |
| 140 | // from all enabled permissions we subtract the permissions we want to check |
| 141 | // after this operation variable permission_bad contains the complement of |
| 142 | // permissions we want to check. |
| 143 | mode_t permission_bad { (S_IRWXU | S_IRWXG | S_IRWXO ) - permissions }; |
| 144 | |
| 145 | if (!stat(path.c_str(), &file_stat)) { |
| 146 | if (S_ISREG(file_stat.st_mode)) { |
| 147 | // this comparison inspect if file has the wanted permissions and if |
| 148 | // the file does not contain the unwanted permissions. |
| 149 | if (!(file_stat.st_mode & permissions ) || |
| 150 | (file_stat.st_mode & permission_bad) |
| 151 | ) { |
| 152 | std::ostringstream strm ; |
| 153 | strm.width(4) ; |
| 154 | strm.fill('0') ; |
| 155 | strm << std::oct << permissions ; |
| 156 | USBGUARD_LOG(Error) << "Permissions for " << path << " should be " << strm.str() ; |
| 157 | throw Exception("Check permissions", path, "Policy may be readable"); |
| 158 | } |
| 159 | else { |
| 160 | USBGUARD_LOG(Info) << "File has correct permissions."; |
| 161 | } |
| 162 | } |
| 163 | else { |
| 164 | USBGUARD_LOG(Error) << "ERROR: File is not a regular file."; |
| 165 | throw Exception("Check permissions", path, "Path is not a file"); |
| 166 | } |
| 167 | } |
| 168 | else { |
| 169 | USBGUARD_LOG(Error) << "ERROR: obtaining file permissions! Errno: " << errno; |
| 170 | throw ErrnoException("Check permissions", path, errno); |
| 171 | } |
| 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
| 176 | void Daemon::checkFolderPermissions(const std::string& path, |
| 177 | const mode_t permissions) |
nothing calls this directly
no test coverage detected