657 {
658 bool proceed = true;
659
662
664
665 uid_t euid = 0;
666 const struct passwd* pw = nullptr;
667 const struct group* gr = nullptr;
668
669 if ((pw = getpwuid(getuid())) == nullptr) {
670 proceed = false;
671 } else if ((gr = getgrgid(pw->pw_gid)) == nullptr) {
672 proceed = false;
673 } else if ((euid = geteuid()) == 0) {
677 } else {
678 const char* homedir = nullptr;
679 if ((homedir = std::getenv("XDG_CONFIG_HOME")) == nullptr) {
680 if ((homedir = std::getenv("HOME")) == nullptr) {
681 homedir = pw->pw_dir;
682 }
683 }
684
685 if (homedir != nullptr) {
687 logDirectory = std::string(homedir) +
"/.local/log/snode.c";
688 pidDirectory = std::string(homedir) +
"/.local/run/snode.c";
689 } else {
690 proceed = false;
691 }
692 }
693
696 std::filesystem::permissions(
698 (std::filesystem::perms::owner_all | std::filesystem::perms::group_read | std::filesystem::perms::group_exec) &
699 ~std::filesystem::perms::others_all);
700 if (geteuid() == 0) {
701 const struct group* gr = nullptr;
702 if ((gr = getgrnam(
XSTR(GROUP_NAME))) !=
nullptr) {
704 std::cout <<
"Warning: Can not set group ownership of '" <<
configDirectory
705 << "' to 'snodec':" << strerror(errno) << std::endl;
706 }
707 } else {
708 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
709 std::cout << " and add the current user to this group." << std::endl;
711 proceed = false;
712 }
713 }
714 } else {
715 std::cout <<
"Error: Can not create directory '" <<
configDirectory <<
"'" << std::endl;
716 proceed = false;
717 }
718 }
719
720 if (proceed && !std::filesystem::exists(
logDirectory)) {
721 if (std::filesystem::create_directories(
logDirectory)) {
723 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
724 ~std::filesystem::perms::others_all);
725 if (geteuid() == 0) {
726 const struct group* gr = nullptr;
727 if ((gr = getgrnam(
XSTR(GROUP_NAME))) !=
nullptr) {
728 if (chown(
logDirectory.c_str(), euid, gr->gr_gid) < 0) {
729 std::cout <<
"Warning: Can not set group ownership of '" <<
logDirectory <<
"' to 'snodec':" << strerror(errno)
730 << std::endl;
731 }
732 } else {
733 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup" << std::endl;
734 std::cout << " and add the current user to this group." << std::endl;
736 proceed = false;
737 }
738 }
739 } else {
740 std::cout <<
"Error: Can not create directory '" <<
logDirectory <<
"'" << std::endl;
741 proceed = false;
742 }
743 }
744
745 if (proceed && !std::filesystem::exists(
pidDirectory)) {
746 if (std::filesystem::create_directories(
pidDirectory)) {
748 (std::filesystem::perms::owner_all | std::filesystem::perms::group_all) &
749 ~std::filesystem::perms::others_all);
750 if (geteuid() == 0) {
751 const struct group* gr = nullptr;
752 if ((gr = getgrnam(
XSTR(GROUP_NAME))) !=
nullptr) {
753 if (chown(
pidDirectory.c_str(), euid, gr->gr_gid) < 0) {
754 std::cout <<
"Warning: Can not set group ownership of '" <<
pidDirectory <<
"' to 'snodec':" << strerror(errno)
755 << std::endl;
756 }
757 } else {
758 std::cout << "Error: Can not find group 'snodec'. Add it using groupadd or addgroup." << std::endl;
759 std::cout << " and add the current user to this group." << std::endl;
761 proceed = false;
762 }
763 }
764 } else {
765 std::cout <<
"Error: Can not create directory '" <<
pidDirectory <<
"'" << std::endl;
766 proceed = false;
767 }
768 }
769
770 if (proceed) {
772
774 }
775
776 return proceed;
777 }
static std::string configDirectory
static std::string pidDirectory
static std::string logDirectory