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