2016-08-02 10:10:25 +00:00
|
|
|
/*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS net command
|
|
|
|
* FILE: base/applications/network/net/cmdUse.c
|
|
|
|
* PURPOSE:
|
|
|
|
*
|
|
|
|
* PROGRAMMERS: Pierre Schweitzer
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "net.h"
|
|
|
|
|
|
|
|
static
|
|
|
|
DWORD
|
|
|
|
EnumerateConnections(LPCWSTR Local)
|
|
|
|
{
|
|
|
|
DWORD dRet;
|
|
|
|
HANDLE hEnum;
|
|
|
|
LPNETRESOURCE lpRes;
|
|
|
|
DWORD dSize = 0x1000;
|
|
|
|
DWORD dCount = -1;
|
|
|
|
LPNETRESOURCE lpCur;
|
|
|
|
|
2016-10-07 22:50:32 +00:00
|
|
|
ConPrintf(StdOut, L"%s\t\t\t%s\t\t\t\t%s\n", L"Local", L"Remote", L"Provider");
|
2016-08-02 10:10:25 +00:00
|
|
|
|
|
|
|
dRet = WNetOpenEnum(RESOURCE_CONNECTED, RESOURCETYPE_DISK, 0, NULL, &hEnum);
|
|
|
|
if (dRet != WN_SUCCESS)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
lpRes = HeapAlloc(GetProcessHeap(), 0, dSize);
|
|
|
|
if (!lpRes)
|
|
|
|
{
|
|
|
|
WNetCloseEnum(hEnum);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
dSize = 0x1000;
|
|
|
|
dCount = -1;
|
|
|
|
|
|
|
|
memset(lpRes, 0, dSize);
|
|
|
|
dRet = WNetEnumResource(hEnum, &dCount, lpRes, &dSize);
|
|
|
|
if (dRet == WN_SUCCESS || dRet == WN_MORE_DATA)
|
|
|
|
{
|
|
|
|
lpCur = lpRes;
|
|
|
|
for (; dCount; dCount--)
|
|
|
|
{
|
|
|
|
if (!Local || wcsicmp(lpCur->lpLocalName, Local) == 0)
|
|
|
|
{
|
2016-10-07 22:50:32 +00:00
|
|
|
ConPrintf(StdOut, L"%s\t\t\t%s\t\t%s\n", lpCur->lpLocalName, lpCur->lpRemoteName, lpCur->lpProvider);
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
lpCur++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (dRet != WN_NO_MORE_ENTRIES);
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, lpRes);
|
|
|
|
WNetCloseEnum(hEnum);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-12-11 10:23:02 +00:00
|
|
|
static
|
|
|
|
VOID
|
|
|
|
PrintError(DWORD Status)
|
|
|
|
{
|
|
|
|
LPWSTR Buffer;
|
|
|
|
|
|
|
|
ConResPrintf(StdErr, IDS_ERROR_SYSTEM_ERROR, Status);
|
|
|
|
|
|
|
|
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, Status, 0, (LPWSTR)&Buffer, 0, NULL))
|
|
|
|
{
|
|
|
|
ConPrintf(StdErr, L"\n%s", Buffer);
|
|
|
|
LocalFree(Buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-02 20:39:48 +00:00
|
|
|
static
|
|
|
|
BOOL
|
|
|
|
ValidateDeviceName(PWSTR DevName)
|
|
|
|
{
|
|
|
|
DWORD Len;
|
|
|
|
|
|
|
|
Len = wcslen(DevName);
|
|
|
|
if (Len != 2)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iswalpha(DevName[0]) || DevName[1] != L':')
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2016-08-02 10:10:25 +00:00
|
|
|
INT
|
|
|
|
cmdUse(
|
|
|
|
INT argc,
|
|
|
|
WCHAR **argv)
|
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
DWORD Status, Len, Delete;
|
2016-08-02 10:10:25 +00:00
|
|
|
|
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
Status = EnumerateConnections(NULL);
|
2016-12-11 10:23:02 +00:00
|
|
|
if (Status == NO_ERROR)
|
|
|
|
ConResPrintf(StdOut, IDS_ERROR_NO_ERROR);
|
|
|
|
else
|
|
|
|
PrintError(Status);
|
|
|
|
|
2016-08-02 10:10:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (argc == 3)
|
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
if (!ValidateDeviceName(argv[2]))
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2016-10-07 22:50:32 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
|
2016-08-02 10:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status = EnumerateConnections(argv[2]);
|
2016-12-11 10:23:02 +00:00
|
|
|
if (Status == NO_ERROR)
|
|
|
|
ConResPrintf(StdOut, IDS_ERROR_NO_ERROR);
|
|
|
|
else
|
|
|
|
PrintError(Status);
|
|
|
|
|
2016-08-02 10:10:25 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-02 20:39:48 +00:00
|
|
|
Delete = 0;
|
|
|
|
if (wcsicmp(argv[2], L"/DELETE") == 0)
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
Delete = 3;
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
2017-07-02 20:39:48 +00:00
|
|
|
else
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
if ((argv[2][0] != '*' && argv[2][1] != 0) &&
|
|
|
|
!ValidateDeviceName(argv[2]))
|
|
|
|
{
|
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
|
|
|
|
return 1;
|
|
|
|
}
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 20:39:48 +00:00
|
|
|
if (wcsicmp(argv[3], L"/DELETE") == 0)
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
Delete = 2;
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
|
|
|
|
2017-07-02 20:39:48 +00:00
|
|
|
if (Delete != 0)
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2017-07-02 20:39:48 +00:00
|
|
|
if (!ValidateDeviceName(argv[Delete]) || argv[Delete][0] == L'*')
|
2016-08-02 10:10:25 +00:00
|
|
|
{
|
2016-10-07 22:50:32 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"DeviceName");
|
2016-08-02 10:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-07-02 20:39:48 +00:00
|
|
|
Status = WNetCancelConnection2(argv[Delete], CONNECT_UPDATE_PROFILE, FALSE);
|
|
|
|
if (Status != NO_ERROR)
|
|
|
|
PrintError(Status);
|
|
|
|
|
|
|
|
return Status;
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BOOL Persist = FALSE;
|
|
|
|
NETRESOURCE lpNet;
|
2016-08-03 14:42:49 +00:00
|
|
|
WCHAR Access[256];
|
2016-12-11 09:15:16 +00:00
|
|
|
DWORD OutFlags = 0, Size = ARRAYSIZE(Access);
|
2016-08-02 10:10:25 +00:00
|
|
|
|
|
|
|
Len = wcslen(argv[3]);
|
|
|
|
if (Len < 4)
|
|
|
|
{
|
2016-10-07 22:50:32 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
|
2016-08-02 10:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[3][0] != L'\\' || argv[3][1] != L'\\')
|
|
|
|
{
|
2016-10-07 22:50:32 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Name");
|
2016-08-02 10:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argc > 4)
|
|
|
|
{
|
|
|
|
LPWSTR Cpy;
|
|
|
|
Len = wcslen(argv[4]);
|
|
|
|
if (Len > 12)
|
|
|
|
{
|
|
|
|
Cpy = HeapAlloc(GetProcessHeap(), 0, (Len + 1) * sizeof(WCHAR));
|
|
|
|
if (Cpy)
|
|
|
|
{
|
|
|
|
INT i;
|
|
|
|
for (i = 0; i < Len; ++i)
|
|
|
|
Cpy[i] = towupper(argv[4][i]);
|
|
|
|
|
|
|
|
if (wcsstr(Cpy, L"/PERSISTENT:") == Cpy)
|
|
|
|
{
|
|
|
|
LPWSTR Arg = Cpy + 12;
|
|
|
|
if (Len == 14 && Arg[0] == 'N' && Arg[1] == 'O')
|
|
|
|
{
|
|
|
|
Persist = FALSE;
|
|
|
|
}
|
|
|
|
else if (Len == 15 && Arg[0] == 'Y' && Arg[1] == 'E' && Arg[2] == 'S')
|
|
|
|
{
|
|
|
|
Persist = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, Cpy);
|
2016-10-07 22:50:32 +00:00
|
|
|
ConResPrintf(StdErr, IDS_ERROR_INVALID_OPTION_VALUE, L"Persistent");
|
2016-08-02 10:10:25 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
HeapFree(GetProcessHeap(), 0, Cpy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
lpNet.dwType = RESOURCETYPE_DISK;
|
|
|
|
lpNet.lpLocalName = (argv[2][0] != L'*') ? argv[2] : NULL;
|
|
|
|
lpNet.lpRemoteName = argv[3];
|
|
|
|
lpNet.lpProvider = NULL;
|
|
|
|
|
2016-08-03 14:42:49 +00:00
|
|
|
Status = WNetUseConnection(NULL, &lpNet, NULL, NULL, CONNECT_REDIRECT | (Persist ? CONNECT_UPDATE_PROFILE : 0), Access, &Size, &OutFlags);
|
|
|
|
if (argv[2][0] == L'*' && Status == NO_ERROR && OutFlags == CONNECT_LOCALDRIVE)
|
2016-12-11 09:01:22 +00:00
|
|
|
ConResPrintf(StdOut, IDS_USE_NOW_CONNECTED, argv[3], Access);
|
2016-12-10 22:12:53 +00:00
|
|
|
else if (Status != NO_ERROR)
|
2016-12-11 10:23:02 +00:00
|
|
|
PrintError(Status);
|
2016-08-03 14:42:49 +00:00
|
|
|
|
|
|
|
return Status;
|
2016-08-02 10:10:25 +00:00
|
|
|
}
|
|
|
|
}
|