List shared folders in Windows with low privileges

Using C ++ (VS2008) I need to be able to list all the shared folders on the current computer and get or create local and remote names.

We have used NetShareEnum

it quite successfully for this, but ran into a problem where we need to run a low privilege user account.

To get the local path using NetShareEnum

, we need to restore at least SHARE_INFO_2

, but this requires "Administrator, Power User, Print Operator or Group Operator".

I tried to use WNetOpenEnum

and WNetEnumResource

, but I don't seem to get a local name for this. AFAICS only lists shares from an external point of view.

So, I would like to be helped where I am wrong in WNetEnumResource

, or about another way of doing this. Any suggestions are greatly appreciated.

+2


a source to share


3 answers


It looks like you are specifying RESOURCE_GLOBALNET

for scope, but it's hard to tell what's wrong without seeing your code.



It's hard to help without knowing what you tried and what you got back. For example, what local name do you expect and what is returned? Have you tried NetShareEnum with SHARE_INFO_503?

0


a source


in this link you can find an example http://msdn.microsoft.com/en-us/library/aa385334(v=VS.85).aspx



0


a source


Once you have an idea of ​​what the local path will be like, you can force-test it: (TODO: make everything Unicode)

// Helper function to make a name unique.
std::string computername()
{
    static std::string name = []() {
        WCHAR buf[MAX_COMPUTERNAME_LENGTH];
        DWORD len = MAX_COMPUTERNAME_LENGTH;
        GetComputerNameEx(ComputerNameNetBIOS, buf, &len);
        return std::string(buf, buf + len);
    }();
    return name;
}

int main()
{
    std::string dir = "C:\\FindThisShare\\";
    // First, create marker
    std::string testfile = computername() + " 038D2B86-7459-417B-9179-90CEECC6EC9D.txt";
    std::ofstream test(dir + testfile);
    test << "This directory holds files from " << computername() 
         << std::endl;
    test.close();

    // Next, find the UNC path holding it.
    BYTE* buf;
    DWORD numEntries;
    NetShareEnum(nullptr, 1, &buf, MAX_PREFERRED_LENGTH, &numEntries,
                 &numEntries, nullptr);
    auto data = reinterpret_cast<SHARE_INFO_1*>(buf);
    std::string retval;
    for (int i = 0; i != numEntries; ++i)
    {
        auto const& entry = data[i];
        std::wstring tmp(entry.shi1_netname);
        std::string share(tmp.begin(), tmp.end());
        std::string uncdir = "\\\\" + computername() + "\\" + share + "\\";
        if (PathFileExistsA((uncdir + testfile).c_str()))
        {
            printf("Exists");
        }
    }
    remove((dir + testfile).c_str());
    NetApiBufferFree(buf);
}

      

0


a source







All Articles