bool IsValidMountPoint()

in host/common/unix/portablehelpersmajor.cpp [1795:1970]


bool IsValidMountPoint(const std::string & volume,std::string & errmsg)
{
    bool rv = true;
    LocalConfigurator localConfigurator;
    std::string sMount = volume;
    std::string::size_type len = sMount.length();
    std::string::size_type idx = len;
    if ((len > 1) && ('/' == sMount[len - 1]))
        --idx;
    if (idx < len)
        sMount.erase(idx);
    std::string invalidmntdirfilename = localConfigurator.getNotAllowedMountPointFileName();

    do
    {
        if (sMount.empty())
        {
            errmsg += "Specified mount point is empty.\n";
            rv = true;
            break;
        }
        if (strcmp(sMount.c_str(), "none") == 0)
        {
            errmsg += "The mount point specified as none.\n";
            rv = false;
            break;
        }
        std::string cmpdirname;
        if(sMount.size() >= strlen("/dev/"))
            cmpdirname = "/dev/";
        else
            cmpdirname = "/dev";

        if (strncmp(sMount.c_str(), cmpdirname.c_str(), cmpdirname.size()) == 0) 
        {
            errmsg += "The directory /dev is used for device file\n. So mounting any volume to /dev or its subdirectories is not allowed.\n";
            rv = false;
            break;
        }
        cmpdirname.clear();
        if(sMount.size() >= strlen("/proc/"))
            cmpdirname = "/proc/";
        else
            cmpdirname = "/proc";

        if (strncmp(sMount.c_str(), cmpdirname.c_str(), cmpdirname.size()) == 0) 
        {
            errmsg += "The directory /proc is used by system\n. So mounting any volume to /proc or its subdirectories is not allowed.\n";
            rv = false;
            break;
        }
        if (strcmp(sMount.c_str(), "/") == 0) 
        {
            errmsg += "The directory / is used as root. So mounting any volume to / is not allowed.\n";
            rv = false;
            break;
        }
        std::string cacheDir = localConfigurator.getCacheDirectory();
        std::string mountpath = sMount;
        if(cacheDir.size() > mountpath.size())
            mountpath += "/";
        if((cacheDir.size() >= mountpath.size()) && 
            (strncmp(cacheDir.c_str(),mountpath.c_str(),mountpath.size()) == 0))
        {
            errmsg += "The mount point ";
            errmsg += mountpath;
            errmsg += " is a parent directory of cache location.\n";
            errmsg += "So mounting any volume to ";
            errmsg += cacheDir;
            errmsg += " or its parent directories is not allowed.\n";
            rv = false;
            break;
        }

        std::string installDir = localConfigurator.getInstallPath();
        mountpath = sMount;
        if(installDir.size() > mountpath.size())
            mountpath += "/";
        if((installDir.size() >= mountpath.size()) && 
            (strncmp(installDir.c_str(),mountpath.c_str(),mountpath.size()) == 0))
        {
            errmsg += "The mount point ";
            errmsg += mountpath;
            errmsg += " is a parent directory of Vx install location.\n";
            errmsg += "So mounting any volume to ";
            errmsg += installDir;
            errmsg += " or its parent directories is not allowed.\n";
            rv = false;
            break;
        }
        else
        {
            std::string installbindir = installDir;
            installbindir += "/bin";
            std::string installetcdir = installDir;
            installetcdir += "/etc";
            std::string installbin1dir = installDir;
            installetcdir += "/bin1";
            if((strcmp(sMount.c_str(), installbindir.c_str()) == 0)||
                (strcmp(sMount.c_str(), installetcdir.c_str()) == 0)||
                (strcmp(sMount.c_str(), installbin1dir.c_str()) == 0))
            {
                errmsg += "The directory ";
                errmsg += sMount;
                errmsg += " is used as vx installation directory. So mounting any volume to ";
                errmsg += sMount;
                errmsg += "is not allowed.\n";
                rv = false;
                break;
            }
        }

        int counter = localConfigurator.getVirtualVolumesId();
        while(counter!=0)
        {
            char regData[26];
			inm_sprintf_s(regData, ARRAYSIZE(regData), "VirVolume%d", counter);
            std::string data = localConfigurator.getVirtualVolumesPath(regData);
            std::string sparsefile;
            std::string virtualvolume;
            if(!ParseSparseFileNameAndVolPackName(data, sparsefile, virtualvolume))
            {
                counter--;
                continue;
            }
            std::string sparsefiledir = dirname_r(sparsefile.c_str());
            mountpath = sMount;
            if(sparsefiledir.size() > mountpath.size())
                mountpath += "/";
            if((sparsefiledir.size() >= mountpath.size()) && 
                (strncmp(sparsefiledir.c_str(),mountpath.c_str(),mountpath.size()) == 0))
            {
                errmsg += "The mount point ";
                errmsg += sparsefiledir;
                errmsg += " contains virtual volume data.\n";
                errmsg += "So mounting any volume to ";
                errmsg += sparsefiledir;
                errmsg += " or its parent directories is not allowed.\n";
                rv = false;
                break;
            }
            counter--;
        }
        if(!rv)
            break;

        std::ifstream inFile(invalidmntdirfilename.c_str());
        std::stringstream msg;
        if(!inFile)
        {
            DebugPrintf(SV_LOG_INFO, "Unable to open the invalid mount point diectories file %s\n",invalidmntdirfilename.c_str());
            break;
        }
        msg << inFile.rdbuf();
        inFile.close();
        std::string data = msg.str();
        svector_t dirnames = CDPUtil::split(data, "\n");
        svector_t::iterator iter = dirnames.begin();
        for(;iter != dirnames.end();++iter)
        {
            std::string invalidmntdir = (*iter);
            if(strcmp(sMount.c_str(), invalidmntdir.c_str()) == 0)
            {
                errmsg += "The directory ";
                errmsg += sMount;
                errmsg += " is used as system directory. So mounting any volume to ";
                errmsg += sMount;
                errmsg += " is not allowed.\n";
                rv = false;
                break;
            }
        }

    } while(false);
    return rv;
}