Skip to content

Commit

Permalink
ask credentials for network paths when needed
Browse files Browse the repository at this point in the history
  • Loading branch information
alabuzhev committed Mar 24, 2016
1 parent 9de1673 commit e8ce0cb
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 22 deletions.
6 changes: 5 additions & 1 deletion far/changelog
@@ -1,4 +1,8 @@
w17 24.03.2016 20:32:59 +0300 - build 4597
drkns 24.03.2016 23:04:54 +0200 - build 4598

1. Спрашиваем имя & пароль при cd \\server\share

w17 24.03.2016 20:32:59 +0300 - build 4597

1. Уточнение 4596. Вернём переключение когда Info панель неактивна.

Expand Down
16 changes: 16 additions & 0 deletions far/dirmix.cpp
Expand Up @@ -44,6 +44,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "strmix.hpp"
#include "interf.hpp"
#include "elevation.hpp"
#include "network.hpp"

BOOL FarChDir(const string& NewDir, BOOL ChangeDir)
{
Expand All @@ -54,6 +55,8 @@ BOOL FarChDir(const string& NewDir, BOOL ChangeDir)
string Drive(L"=A:");
string strCurDir;

bool IsNetworkDrive = false;

// если указана только буква диска, то путь возьмем из переменной
if (NewDir.size() == 2 && NewDir[1]==L':')
{
Expand All @@ -70,6 +73,13 @@ BOOL FarChDir(const string& NewDir, BOOL ChangeDir)
{
rc=os::SetCurrentDirectory(strCurDir);
}

if (!rc && GetLastError() == ERROR_PATH_NOT_FOUND)
{
os::drives_set NetworkDrives;
AddSavedNetworkDisks(NetworkDrives);
IsNetworkDrive = NetworkDrives[Drive[1] - L'A'];
}
}
else
{
Expand All @@ -87,6 +97,12 @@ BOOL FarChDir(const string& NewDir, BOOL ChangeDir)
}
}

if (!rc && (IsNetworkDrive || GetLastError() == ERROR_LOGON_FAILURE))
{
ConnectToNetworkResource(strCurDir);
rc = os::SetCurrentDirectory(strCurDir);
}

if (rc || !ChangeDir)
{
if (ChangeDir)
Expand Down
6 changes: 0 additions & 6 deletions far/diskmenu.cpp
Expand Up @@ -1146,12 +1146,6 @@ static int ChangeDiskMenu(panel_ptr Owner, int Pos, bool FirstCall)
{
wchar_t NewDir[] = { mitem->cDrive, L':', 0, 0 };

// In general, mitem->cDrive can contain any unicode character
if (mitem->cDrive >= L'A' && mitem->cDrive <= L'Z' && NetworkMask[mitem->cDrive - L'A'])
{
ConnectToNetworkDrive(NewDir);
}

if (FarChDir(NewDir))
{
break;
Expand Down
44 changes: 31 additions & 13 deletions far/network.cpp
Expand Up @@ -43,13 +43,14 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "pathmix.hpp"
#include "strmix.hpp"

void GetStoredUserName(wchar_t cDrive, string &strUserName)
static bool GetStoredUserName(wchar_t Drive, string &strUserName)
{
//Тут может быть надо заюзать WNetGetUser
strUserName.clear();
const wchar_t KeyName[]={L'N',L'e',L't',L'w',L'o',L'r',L'k',L'\\',cDrive,L'\0'};
wchar_t KeyName[] = L"Network\?";
*std::prev(std::end(KeyName)) = Drive;

os::reg::GetValue(HKEY_CURRENT_USER, KeyName, L"UserName", strUserName);
return os::reg::GetValue(HKEY_CURRENT_USER, KeyName, L"UserName", strUserName);
}

os::drives_set AddSavedNetworkDisks(os::drives_set& Mask)
Expand Down Expand Up @@ -97,32 +98,48 @@ os::drives_set AddSavedNetworkDisks(os::drives_set& Mask)
return Result;
}

void ConnectToNetworkDrive(const string& NewDir)
bool ConnectToNetworkResource(const string& NewDir)
{
string strRemoteName;
DriveLocalToRemoteName(DRIVE_REMOTE_NOT_CONNECTED,NewDir[0],strRemoteName);
string LocalName, RemoteName;

const auto IsDrive = ParsePath(NewDir) == PATH_DRIVELETTER;
if (IsDrive)
{
LocalName = NewDir.substr(0, 2);
DriveLocalToRemoteName(DRIVE_REMOTE_NOT_CONNECTED, NewDir[0], RemoteName);
}
else
{
LocalName = NewDir;
RemoteName = NewDir;
}

string strUserName, strPassword;
GetStoredUserName(NewDir[0], strUserName);
NETRESOURCE netResource;
if (IsDrive)
{
GetStoredUserName(NewDir[0], strUserName);
}

NETRESOURCE netResource {};
netResource.dwType = RESOURCETYPE_DISK;
netResource.lpLocalName = UNSAFE_CSTR(NewDir);
netResource.lpRemoteName = UNSAFE_CSTR(strRemoteName);
netResource.lpLocalName = IsDrive? UNSAFE_CSTR(LocalName) : nullptr;
netResource.lpRemoteName = UNSAFE_CSTR(RemoteName);
netResource.lpProvider = nullptr;
DWORD res = WNetAddConnection2(&netResource, nullptr, EmptyToNull(strUserName.data()), 0);

if (res == ERROR_SESSION_CREDENTIAL_CONFLICT)
res = WNetAddConnection2(&netResource, nullptr, nullptr, 0);

if (res)
if (res != NO_ERROR)
{
for (;;)
{
if (!GetNameAndPassword(strRemoteName, strUserName, strPassword, nullptr, GNP_USELAST))
if (!GetNameAndPassword(RemoteName, strUserName, strPassword, nullptr, GNP_USELAST))
break;

res = WNetAddConnection2(&netResource, strPassword.data(), EmptyToNull(strUserName.data()), 0);

if (!res)
if (res == NO_ERROR)
break;

Global->CatchError();
Expand All @@ -134,6 +151,7 @@ void ConnectToNetworkDrive(const string& NewDir)
}
}
}
return res == NO_ERROR;
}

string ExtractComputerName(const string& CurDir, string* strTail)
Expand Down
2 changes: 1 addition & 1 deletion far/network.hpp
Expand Up @@ -36,7 +36,7 @@ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

os::drives_set AddSavedNetworkDisks(os::drives_set& Mask);

void ConnectToNetworkDrive(const string& NewDir);
bool ConnectToNetworkResource(const string& NewDir);

string ExtractComputerName(const string& CurDir, string* strTail = nullptr);

Expand Down
2 changes: 1 addition & 1 deletion far/vbuild.m4
@@ -1 +1 @@
m4_define(BUILD,4597)m4_dnl
m4_define(BUILD,4598)m4_dnl

0 comments on commit e8ce0cb

Please sign in to comment.