Code:
// driveMap.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <windows.h>
#include <atlstr.h>
#include <stdlib.h>
#include <stdio.h>
#include <locale.h>
#define MAX_NAME 256
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
DWORD i, dwSize = 0, dwResult = 0;
HANDLE hToken;
PTOKEN_GROUPS pGroupInfo;
SID_NAME_USE SidType;
char lpName[MAX_NAME];
char lpDomain[MAX_NAME];
BYTE sidBuffer[100];
PSID pSID = (PSID)&sidBuffer;
string stringGrp;
char UserName[100];
DWORD nUserName = sizeof(UserName);
char szName[256];
//Hide the command prompt window
FreeConsole();
//Map F: Drive
GetUserName((TCHAR*)UserName, &nUserName );
sprintf_s(szName, sizeof( szName ), "net use F: \\\\Earth\\users\\%s /persistent:yes", UserName);
system(szName);
//Map H: Drive
system("net use H: \\\\Earth\\corp /persistent:yes");
// Open a handle to the access token for the calling process, that is, this running program (process). So we get the handle
// to the token for the current user that run this program and/or login to this machine.
// Depend on your task, change the TOKEN_QUERY to others accordingly...
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
{
printf("OpenProcessToken() error %u\n", GetLastError());
return FALSE;
}
// By assuming that we got the handle to the token...
// Call GetTokenInformation() to get the buffer size for storage. This is for Tokengroups, change accordingly for others such
// as TokenUser, TokenOwner, TokenType etc
if(!GetTokenInformation(hToken, TokenGroups, NULL, dwSize, &dwSize))
{
dwResult = GetLastError();
if(dwResult != ERROR_INSUFFICIENT_BUFFER)
{
printf("GetTokenInformation() error %u\n", dwResult);
return FALSE;
}
}
// By assuming that we got the storage, then allocate the buffer.
pGroupInfo = (PTOKEN_GROUPS) GlobalAlloc(GPTR, dwSize);
// Call GetTokenInformation() again to get the group information.
if(!GetTokenInformation(hToken, TokenGroups, pGroupInfo, dwSize, &dwSize))
{
printf("GetTokenInformation() error %u\n", GetLastError());
return FALSE;
}
//****************** Playing with SIDs *****************************
// Create a SID for the BUILTIN\Administrators group...
// You can try other groups also lol as commented out on the following
// codes. Uncomment/comment out the SIDs for testing....
// This is 32 bit RID value. Applications that require longer RID values,
// use CreateWellKnownSid() instead
//*********************** Administrator group ***********************
SID_IDENTIFIER_AUTHORITY SIDAuth = SECURITY_NT_AUTHORITY;
if(!AllocateAndInitializeSid(&SIDAuth, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &pSID))
{
printf("AllocateAndInitializeSid() error %u\n", GetLastError());
return FALSE;
}
// Loop through the group SIDs looking for the created group SID.
for(i=0; i<pGroupInfo->GroupCount; i++)
{
// Lookup the account name and print it.
dwSize = MAX_NAME;
if(!LookupAccountSid(NULL, pGroupInfo->Groups[i].Sid, lpName, &dwSize, lpDomain, &dwSize, &SidType))
{
// If not found or something wrong...
dwResult = GetLastError();
if(dwResult == ERROR_NONE_MAPPED)
strcpy_s(lpName, "NONE_MAPPED");
else
{
printf("LookupAccountSid() error %u\n", GetLastError());
return FALSE;
}
}
// If found...
else
{
stringGrp = lpName;
//Map G drive for users
if(stringGrp.compare("SEC-Clinical Services") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\clin /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Billing") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\fin /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Accounting") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\fin /persistent:yes");
break;
}
else if(stringGrp.compare("Finance") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\fin /persistent:yes");
break;
}
else if(stringGrp.compare("Corporate Planning") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\cp /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Business Development") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\bd /persistent:yes");
break;
}
else if(stringGrp.compare("Community Development Dept") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\bd /persistent:yes");
system("net use S: \\\\Earth\\scans\\development /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Community Development") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\clin /persistent:yes");
system("net use S: \\\\Earth\\scans\\development /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Information Systems") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\is /persistent:yes");
break;
}
else if(stringGrp.compare("SEC-Human Resources") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\hr /persistent:yes");
break;
}
else if(stringGrp.compare("Office Services") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\offserv /persistent:yes");
break;
}
else if(stringGrp.compare("President Office") == 0)
{
//printf("You are part of the %s Group\n", lpName);
system("net use G: \\\\Earth\\legal /persistent:yes");
break;
}
}
}
// Release resources back to system
if(pSID)
FreeSid(pSID);
if(pGroupInfo)
GlobalFree(pGroupInfo);
return TRUE;
}