/************************************************************************************************
* ShareIt - Network sharing rights transfer application *
* *
* Usage : ShareIt <machine1> <machine2> *
* Author : M. Farooque A. Khan *
* Created: December 8, 2000 *
* *
* Modifications: *
* *
************************************************************************************************/
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <lm.h>
#include <aclapi.h>
#include <conio.h>
#define SHOWPATH 0
#define SHOWNETNAME 1
void wmain( int argc, TCHAR *lpszArgv[ ])
{
int iShow = SHOWPATH;
char szBuffer[MAX_PATH];
BOOL bSuccess = TRUE;
DWORD er = 0;
DWORD tr = 0;
DWORD resume = 0;
DWORD i;
PSHARE_INFO_502 BufPtr;
PSHARE_INFO_502 p;
NET_API_STATUS res;
NET_API_STATUS res2;
LPTSTR lpszMachine1;
LPTSTR lpszMachine2;
printf("\n-------------------------------------------------\n");
printf("ShareIt - Network Sharing Rights Transfer Utility\n");
printf("Version 0.9\n");
printf("\n");
printf("Concretio India Pvt. Ltd.\n");
printf("www.concretioindia.com\n");
printf("-------------------------------------------------\n\n");
if(argc < 3)
{
printf("Usage: ShareIt [\\p][\\s] <machine1> <machine2> \n\n\tmachine1 - Computer from which to take the sharing rights\n\tmachine2 - Computer on which to transfer the sharing rights\n");
printf("\n\t\\p - Shows PATH of shared resource while transferring (default)");
printf("\n\t\\s - Shows SHARED NAME of shared resource while transferring");
printf("\n\t\\m - Sets the maximum use value of shared resource to a value given by user");
printf("\n\neg: ShareIt \\p \\\\MAINSERVER \\\\BACKSERVER\n");
return ;
}
// Parse command line
if(wcscmp((LPWSTR)lpszArgv[1], L"\\p") == 0 || wcscmp((LPWSTR)lpszArgv[1], L"-p") == 0)
{
iShow = SHOWPATH;
lpszMachine1 = lpszArgv[2];
lpszMachine2 = lpszArgv[3];
}
else if(wcscmp((LPWSTR)lpszArgv[1], L"\\s") == 0 || wcscmp((LPWSTR)lpszArgv[1], L"-s") == 0)
{
iShow = SHOWNETNAME;
lpszMachine1 = lpszArgv[2];
lpszMachine2 = lpszArgv[3];
}
else
{
lpszMachine1 = lpszArgv[1];
lpszMachine2 = lpszArgv[2];
}
wprintf(L"Transferring sharing rights from %s to %s:\n\n", lpszMachine1, lpszMachine2);
do
{
res = NetShareEnum ( lpszMachine1, 502, (LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
{
// save the share info structure
p = BufPtr;
for(i=1; i <= er; i++)
{
// Check if this is a valid security descriptor. Some are not!
if( IsValidSecurityDescriptor(p->shi502_security_descriptor) != 0 )
{
if(iShow == SHOWNETNAME)
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)p->shi502_netname, MAX_PATH, szBuffer, MAX_PATH, NULL, NULL);
else
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)p->shi502_path, MAX_PATH, szBuffer, MAX_PATH, NULL, NULL);
printf("%s ", szBuffer);
for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
printf(".");
// Try to add sharing
res = NetShareAdd(lpszMachine2, 502, (LPBYTE)p, NULL);
switch(res)
{
case NERR_Success:
printf(" SUCCESS\n");
break;
case NERR_DuplicateShare:
res2 = NetShareSetInfo(lpszMachine2, p->shi502_netname, 502, (LPBYTE)p, NULL);
if(NERR_Success != res2)
{
printf(" FAILED(#%d)\n", res2);
bSuccess = FALSE;
}
else
printf(" SUCCESS\n");
break;
default:
printf(" FAILED(#%d)\n", res);
bSuccess = FALSE;
}
}
else
{
if(iShow == SHOWPATH)
{
if(lstrlen(p->shi502_path) > 0)
{
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)p->shi502_path, MAX_PATH, szBuffer, MAX_PATH, NULL, NULL);
printf("%s ", szBuffer);
for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
printf(".");
printf(" INVALID SD\n");
}
}
else
{
if(lstrlen(p->shi502_netname) > 0)
{
WideCharToMultiByte(CP_ACP, 0, (LPWSTR)p->shi502_netname, MAX_PATH, szBuffer, MAX_PATH, NULL, NULL);
printf("%s ", szBuffer);
for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
printf(".");
printf(" INVALID SD\n");
}
}
}
p++;
}
if(NetApiBufferFree(BufPtr) != NERR_Success)
printf("Unable to do cleanup.\n");
}
else
{
wprintf(L"(#%ld) No shared devices\\directories found on computer %s, or access denied.\n", res, lpszMachine1);
bSuccess = FALSE;
}
}
while (res==ERROR_MORE_DATA);
if(bSuccess == TRUE)
printf("\nAll sharing rights successfully transfered.\n");
else
printf("\nNot all sharing rights could be transfered.\n");
return ;
}