Introduction
The ShareIT is a simple utility which transfers the sharing rights from one machine to another. Specially useful to system administrators who want to restore sharing rights from one machine (backup may be) to another machine (main server may be). The utility demonstrates NetShare APIs. ShareIT enumerates the source machine sharing rights using the NetShareEnum()
API. The security descriptors thus obtained are applied to the target machine's directories using the NetShareAdd()
and NetShareSetInfo()
APIs. Cleanup is done using NetApiBufferFree()
function.
The main logic is shown below:
do
{
res = NetShareEnum ( lpszMachine1, 502,
(LPBYTE *) &BufPtr, -1, &er, &tr, &resume);
if(res == ERROR_SUCCESS || res == ERROR_MORE_DATA)
{
p = BufPtr;
for(i=1; i <= er; i++)
{
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);
if(bPrompt == TRUE)
{
char ch = getch();
if(ch == 'n' || ch == 'N')
{
for(int k=0;
k < abs(60 - strlen(szBuffer)) && k<60; k++)
printf(".");
printf(" SKIPPED\n");
p++;
continue;
}
}
for(int k=0; k < abs(60 - strlen(szBuffer)) && k<60; k++)
printf(".");
if(TRUE == bCreateDir)
{
WideCharToMultiByte(CP_ACP, 0,
(LPWSTR)p->shi502_path, MAX_PATH,
szPath, MAX_PATH, NULL, NULL);
if( _chdir(szPath) != 0)
{
_mkdir(szPath);
}
}
if(bSetMax == TRUE)
p->shi502_max_uses = max_use;
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);