ec5b95dbb9be712cb89afb05b761632065c46138
Stefan Schuermans add copyright & license

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   1) /**
libpermissioner/src/Tree.cpp   2)  * Permissioner: set file ownerships and permissions
libpermissioner/src/Tree.cpp   3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
libpermissioner/src/Tree.cpp   4)  * Copyleft: GNU GENERAL PUBLIC LICENSE version 3 (see LICENSE)
libpermissioner/src/Tree.cpp   5)  */
libpermissioner/src/Tree.cpp   6) 
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   7) #include <permissioner/Tree.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp     8) 
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp   9) #include <permissioner/Group.h>
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  10) #include <permissioner/Permissions.h>
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  11) #include <permissioner/User.h>
Stefan Schuermans move most sources into libp...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  12) #include <permissioner/StringUtils.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    13) 
permissionerd/src/Tree.cpp    14) #include <boost/filesystem.hpp>
permissionerd/src/Tree.cpp    15) #include <sstream>
permissionerd/src/Tree.cpp    16) #include <stdexcept>
permissionerd/src/Tree.cpp    17) #include <string>
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  18) #include <unistd.h>
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    19) #include <vector>
permissionerd/src/Tree.cpp    20) 
permissionerd/src/Tree.cpp    21) void Tree::parseParams(std::string const &paramStr) {
permissionerd/src/Tree.cpp    22)   // format of paramStr is: <user> " " <group> " " <permissions> " " <root>
permissionerd/src/Tree.cpp    23)   // user, group, permissions may be - for no change
permissionerd/src/Tree.cpp    24)   std::string userStr, groupStr, permissionsStr, rootStr;
permissionerd/src/Tree.cpp    25) 
permissionerd/src/Tree.cpp    26)   std::string::size_type pos = 0;
permissionerd/src/Tree.cpp    27)   StringUtils::getNextField(paramStr, pos, userStr, "user");
permissionerd/src/Tree.cpp    28)   StringUtils::getNextField(paramStr, pos, groupStr, "group");
permissionerd/src/Tree.cpp    29)   StringUtils::getNextField(paramStr, pos, permissionsStr, "permissions");
permissionerd/src/Tree.cpp    30)   if (pos >= paramStr.length()) {
permissionerd/src/Tree.cpp    31)     std::stringstream msg;
permissionerd/src/Tree.cpp    32)     msg << "<root> field missing in \"" << paramStr << "\"";
permissionerd/src/Tree.cpp    33)     throw std::runtime_error(msg.str());
permissionerd/src/Tree.cpp    34)   }
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  35)   rootStr = paramStr.substr(pos);
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    36) 
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  37)   try {
libpermissioner/src/Tree.cpp  38)     user.parseUserName(userStr);
libpermissioner/src/Tree.cpp  39)   } catch (std::exception const & e) {
libpermissioner/src/Tree.cpp  40)     std::stringstream msg;
libpermissioner/src/Tree.cpp  41)     msg << "invalid <user> field \"" << userStr << "\" in \""
libpermissioner/src/Tree.cpp  42)         << paramStr << "\": " << e.what();
libpermissioner/src/Tree.cpp  43)     throw std::runtime_error(msg.str());
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    44)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  45) 
libpermissioner/src/Tree.cpp  46)   try {
libpermissioner/src/Tree.cpp  47)     group.parseGroupName(groupStr);
libpermissioner/src/Tree.cpp  48)   } catch (std::exception const & e) {
libpermissioner/src/Tree.cpp  49)     std::stringstream msg;
libpermissioner/src/Tree.cpp  50)     msg << "invalid <group> field \"" << groupStr << "\" in \""
libpermissioner/src/Tree.cpp  51)         << paramStr << "\": " << e.what();
libpermissioner/src/Tree.cpp  52)     throw std::runtime_error(msg.str());
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    53)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  54) 
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    55)   try {
permissionerd/src/Tree.cpp    56)     permissions.parseParams(permissionsStr);
permissionerd/src/Tree.cpp    57)   } catch (std::exception const & e) {
permissionerd/src/Tree.cpp    58)     std::stringstream msg;
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  59)     msg << "invalid <permissions> field \"" << permissionsStr << "\" in \""
libpermissioner/src/Tree.cpp  60)         << paramStr << "\": " << e.what();
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    61)     throw std::runtime_error(msg.str());
permissionerd/src/Tree.cpp    62)   }
Stefan Schuermans user/group lookup

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  63) 
Stefan Schuermans canonicalize root paths

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  64)   try {
libpermissioner/src/Tree.cpp  65)     root = boost::filesystem::canonical(rootStr);
libpermissioner/src/Tree.cpp  66)   } catch (std::exception const & e) {
libpermissioner/src/Tree.cpp  67)     std::stringstream msg;
libpermissioner/src/Tree.cpp  68)     msg << "invalid <root> field \"" << rootStr << "\" in \""
libpermissioner/src/Tree.cpp  69)         << paramStr << "\": " << e.what();
libpermissioner/src/Tree.cpp  70)     throw std::runtime_error(msg.str());
libpermissioner/src/Tree.cpp  71)   }
Stefan Schuermans begin of permissioner confi...

Stefan Schuermans authored 4 years ago

permissionerd/src/Tree.cpp    72) }
Stefan Schuermans add config getters

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  73) 
libpermissioner/src/Tree.cpp  74) User const & Tree::getUser() const {
libpermissioner/src/Tree.cpp  75)   return user;
libpermissioner/src/Tree.cpp  76) }
libpermissioner/src/Tree.cpp  77) 
libpermissioner/src/Tree.cpp  78) Group const & Tree::getGroup() const {
libpermissioner/src/Tree.cpp  79)   return group;
libpermissioner/src/Tree.cpp  80) }
libpermissioner/src/Tree.cpp  81) 
libpermissioner/src/Tree.cpp  82) Permissions const & Tree::getPermissions() const {
libpermissioner/src/Tree.cpp  83)   return permissions;
libpermissioner/src/Tree.cpp  84) }
libpermissioner/src/Tree.cpp  85) 
libpermissioner/src/Tree.cpp  86) boost::filesystem::path const & Tree::getRoot() const {
libpermissioner/src/Tree.cpp  87)   return root;
libpermissioner/src/Tree.cpp  88) }
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp  89) 
libpermissioner/src/Tree.cpp  90) void Tree::setPermissions(TreeMap const &exclude) const {
libpermissioner/src/Tree.cpp  91)   setPermissionsInternal(root, exclude);
libpermissioner/src/Tree.cpp  92) }
libpermissioner/src/Tree.cpp  93) 
libpermissioner/src/Tree.cpp  94) void Tree::setPermissionsInternal(boost::filesystem::path const &path,
libpermissioner/src/Tree.cpp  95)                                   TreeMap const &exclude) const {
libpermissioner/src/Tree.cpp  96)   try {
libpermissioner/src/Tree.cpp  97)     if (boost::filesystem::is_regular_file(path)) {
libpermissioner/src/Tree.cpp  98)       setPermissionsOne(path);
libpermissioner/src/Tree.cpp  99)     } else if (boost::filesystem::is_directory(path)) {
libpermissioner/src/Tree.cpp 100)       setPermissionsOne(path);
libpermissioner/src/Tree.cpp 101)       for (boost::filesystem::directory_entry entry :
libpermissioner/src/Tree.cpp 102)            boost::filesystem::directory_iterator(path)) {
libpermissioner/src/Tree.cpp 103)         if (exclude.find(entry) != exclude.end()) {
libpermissioner/src/Tree.cpp 104)           continue; // other tree -> skip here
libpermissioner/src/Tree.cpp 105)         }
libpermissioner/src/Tree.cpp 106)         setPermissionsInternal(entry, exclude); // recurse
libpermissioner/src/Tree.cpp 107)       }
libpermissioner/src/Tree.cpp 108)     }
libpermissioner/src/Tree.cpp 109)   } catch (boost::filesystem::filesystem_error const & e) {
libpermissioner/src/Tree.cpp 110)     // ignore filesystem errors for now, as this runs in a daemon in background
libpermissioner/src/Tree.cpp 111)     (void)e;
libpermissioner/src/Tree.cpp 112)   }
libpermissioner/src/Tree.cpp 113) }
libpermissioner/src/Tree.cpp 114) 
libpermissioner/src/Tree.cpp 115) void Tree::setPermissionsOne(boost::filesystem::path const &path) const {
libpermissioner/src/Tree.cpp 116)   // change permissions
libpermissioner/src/Tree.cpp 117)   try {
libpermissioner/src/Tree.cpp 118)     permissions.apply(path);
libpermissioner/src/Tree.cpp 119)   } catch (boost::filesystem::filesystem_error const & e) {
libpermissioner/src/Tree.cpp 120)     // ignore filesystem errors for now, as this runs in a daemon in background
libpermissioner/src/Tree.cpp 121)     (void)e;
libpermissioner/src/Tree.cpp 122)   }
libpermissioner/src/Tree.cpp 123) 
libpermissioner/src/Tree.cpp 124)   // change owner/group
Stefan Schuermans ignore return value of lcho...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 125)   int ret = lchown(path.string().c_str(), user.getUid(), group.getGid());
Stefan Schuermans implement chown/chmod

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 126)   // ignore error for now, as this runs in a daemon in background
Stefan Schuermans ignore return value of lcho...

Stefan Schuermans authored 4 years ago

libpermissioner/src/Tree.cpp 127)   (void)ret;