📄 driver.cpp
字号:
}
Done:
if(fFoundSomething)
return S_OK;
else
return E_FAIL;
}
DWORD
SMB_Init(
DWORD dwContext
)
{
DWORD dwRet = 0;
CReg CRCName;
CReg CRSettings;
CReg RegAllowAll;
DWORD dwType;
DWORD dwLen;
WCHAR MyCName[16];
WCHAR OrigName[16];
WCHAR wszWorkGroup[MAX_PATH];
BOOL fAllowAll = FALSE;
TRACEMSG(ZONE_INIT,(TEXT("+SMB_Init\n")));
UINT i;
//
// Print a RETAIL message if passwords are disabled
if(TRUE == RegAllowAll.Open(HKEY_LOCAL_MACHINE, L"Services\\SMBServer\\Shares")) {
fAllowAll = !(RegAllowAll.ValueDW(L"UseAuthentication", TRUE));
if(TRUE == fAllowAll) {
RETAILMSG(1, (L"SMB_SRV: **PASSWORDS DISABLED**"));
}
}
//
// Initialize any globals
//
// First get the server GUID from registry (if it doesnt exist, create it)
if(FALSE == CRSettings.OpenOrCreateRegKey(HKEY_LOCAL_MACHINE,
L"Services\\SMBServer",0)) {
RETAILMSG(1, (L"SMB_SRVR: Error opening up SMBPrint reg key!\n"));
dwRet = 0;
goto Done;
}
if(FALSE == CRSettings.ValueSZ(L"AdapterList", SMB_Globals::g_AdapterAllowList, MAX_PATH / sizeof(WCHAR))) {
RETAILMSG(1, (L"Name value under HKLM\\Services\\AdapterList is not there\n"));
SMB_Globals::g_AdapterAllowList[0] = NULL;
}
dwLen = sizeof(SMB_Globals::g_ServerGUID);
if(ERROR_SUCCESS != RegQueryValueEx(CRSettings,
L"GUID",
NULL,
&dwType,
SMB_Globals::g_ServerGUID,
&dwLen) || (16 != dwLen) || (REG_BINARY != dwType))
{
GUID srvrGUID;
TRACEMSG(ZONE_INIT, (L"SMB_SRV: Server GUID not specified in registry -- creating a new one"));
GenerateGUID(&srvrGUID);
memcpy(&(SMB_Globals::g_ServerGUID[0]), (BYTE *)(&srvrGUID.Data1), 4);
memcpy(&(SMB_Globals::g_ServerGUID[4]), (BYTE *)(&srvrGUID.Data2), 2);
memcpy(&(SMB_Globals::g_ServerGUID[6]), (BYTE *)(&srvrGUID.Data3), 2);
memcpy(&(SMB_Globals::g_ServerGUID[8]), srvrGUID.Data4, 8);
if(TRUE != CRSettings.SetBinary(L"GUID", SMB_Globals::g_ServerGUID, sizeof(SMB_Globals::g_ServerGUID))) {
TRACEMSG(ZONE_ERROR, (L"SMB_SRV: Cant write GUID to registry -- we will continue, but this isnt good"));
ASSERT(FALSE);
}
}
if(FALSE == CRSettings.ValueSZ(L"workgroup", wszWorkGroup, sizeof(wszWorkGroup)/sizeof(WCHAR))) {
RETAILMSG(1, (L"SMB_SRV: Workgroup reg value not set -- using WORKGROUP\n"));
sprintf(SMB_Globals::g_szWorkGroup, "WORKGROUP");
} else {
BYTE *pWorkGroup = NULL;
UINT uiSize;
StringConverter SCWorkGroup;
SCWorkGroup.append(wszWorkGroup);
pWorkGroup = SCWorkGroup.NewSTRING(&uiSize, FALSE);
if(NULL == pWorkGroup) {
dwRet = 0;
ASSERT(FALSE);
goto Done;
}
strcpy(SMB_Globals::g_szWorkGroup, (CHAR *)pWorkGroup);
LocalFree(pWorkGroup);
}
//
// Set MaxConnections
SMB_Globals::g_uiMaxConnections = CRSettings.ValueDW(L"MaxConnections", 5);
#ifdef DEBUG
SMB_Globals::g_PacketID = 0;
#endif
//
// Make sure the netbios transport is initialized (because we may start listening for name changes etc)
InitNetbiosTransport();
//
// Load the CName from the registry
if(FALSE == CRCName.Open(HKEY_LOCAL_MACHINE, L"Ident")) {
RETAILMSG(1, (L"No registy key under HKLM\\Ident"));
dwRet = SMB_CONTEXT;
goto Done;
}
if(FALSE == CRCName.ValueSZ(L"Name", MyCName, sizeof(MyCName)/sizeof(WCHAR))) {
RETAILMSG(1, (L"Name value under HKLM\\Ident is either too large (15 chars) or isnt present -- stopping server"));
dwRet = SMB_CONTEXT;
goto Done;
}
if(FALSE == CRCName.ValueSZ(L"OrigName", OrigName, sizeof(OrigName)/sizeof(WCHAR))) {
RETAILMSG(1, (L"OrigName value under HKLM\\Ident is either too large (15 chars) or isnt present -- stopping server"));
dwRet = SMB_CONTEXT;
goto Done;
}
if(0 == wcscmp(MyCName, OrigName)) {
RETAILMSG(1, (L"OrigName = Name value under HKLM\\Ident -- stopping server to prevent multiple machine with the same name"));
dwRet = SMB_CONTEXT;
//
// In the case where names arent the same start up the name listener so if they become okay we will start again
StartListenOnNameChange();
goto Done;
}
if(0 == WideCharToMultiByte(CP_ACP, 0, MyCName, -1, SMB_Globals::CName, sizeof(SMB_Globals::CName),NULL,NULL)) {
TRACEMSG(ZONE_ERROR, (L"Conversion of CName (%s) failed!!!", MyCName));
ASSERT(FALSE);
dwRet = 0;
goto Done;
}
//make sure its upper case
for(i=0; i<sizeof(SMB_Globals::CName); i++) {
SMB_Globals::CName[i] = toupper(SMB_Globals::CName[i]);
}
RETAILMSG(1, (L"SMB_SRV: Registering CName %s", MyCName));
//
// Create a abstract filesystem (since CE doesnt have locking)
SMB_Globals::g_pAbstractFileSystem = new AbstractFileSystem();
if(NULL == SMB_Globals::g_pAbstractFileSystem) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- couldnt create abstract file system")));
dwRet = 0;
goto Done;
}
//
// Start the master share list
SMB_Globals::g_pShareManager = new ShareManager();
if(NULL == SMB_Globals::g_pShareManager) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- couldnt create shares")));
dwRet = 0;
goto Done;
}
//
// Start the TID Manager
SMB_Globals::g_pConnectionManager = new ConnectionManager();
if(NULL == SMB_Globals::g_pConnectionManager) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- couldnt create connection manager")));
dwRet = 0;
goto Done;
}
//
// Load (and add) all shares from the registry
if(FAILED(AddShares())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - adding shares failed!")));
dwRet = SMB_CONTEXT;
goto NoShares;
}
//
// Start up the packet cracker
if(FAILED(StartCracker())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed")));
dwRet = 0;
goto Done;
}
//
// Start up the print spool
if(FAILED(StartPrintSpool())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartPrintSpool failed")));
dwRet = 0;
goto Done;
}
//
// Start up protocol transports
if(FAILED(StartTCPTransport())) { //TCP must be started BEFORE Netbios!
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer on TCP failed")));
ASSERT(FALSE);
dwRet = 0;
goto Done;
}
if(FAILED(StartNetbiosTransport())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer on NETBsdIOS failed")));
ASSERT(FALSE);
dwRet = 0;
goto Done;
}
dwRet = SMB_CONTEXT;
g_fServerRunning = TRUE;
Done:
if(0 == dwRet) {
NoShares:
TRACEMSG(ZONE_ERROR, (TEXT("SMB_Init -- an error occured during init... cleaning up")));
//
// Stop transports (NOTE: this is okay to do even if they were not inited!!
if(FAILED(StopTCPTransport())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init -- couldnt stop TCP transport!!")));
ASSERT(FALSE);
}
if(FAILED(StopNetbiosTransport())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init -- couldnt stop NETBIOS transport!!")));
ASSERT(FALSE);
}
//
// Stop print spool
if(FAILED(StopPrintSpool())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init -- couldnt stop print spool!!")));
ASSERT(FALSE);
}
//
// Stop the cracker
if(FAILED(StopCracker())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init -- couldnt stop cracker!!")));
ASSERT(FALSE);
}
//
// Clean up memory
if(NULL != SMB_Globals::g_pShareManager) {
delete SMB_Globals::g_pShareManager;
SMB_Globals::g_pShareManager = NULL;
}
if(NULL != SMB_Globals::g_pConnectionManager) {
delete SMB_Globals::g_pConnectionManager;
SMB_Globals::g_pConnectionManager = NULL;
}
if(NULL != SMB_Globals::g_pWakeUpOnEvent) {
delete SMB_Globals::g_pWakeUpOnEvent;
SMB_Globals::g_pWakeUpOnEvent = NULL;
}
if(NULL != SMB_Globals::g_pAbstractFileSystem) {
delete SMB_Globals::g_pAbstractFileSystem;
SMB_Globals::g_pAbstractFileSystem = NULL;
}
}
TRACEMSG(ZONE_INIT,(TEXT("-SMB_Init")));
return dwRet;
}
DWORD
SMB_Deinit(
DWORD dwClientContext
)
{
TRACEMSG(ZONE_INIT,(TEXT("+SMB_Deinit")));
BOOL fSuccess = TRUE;
//
// Halt all existing TCP connections (and dont allow for new ones)
if(FAILED(HaltTCPIncomingConnections())) {
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Closing TCP transport incoming connections failed")));
fSuccess = FALSE;
ASSERT(FALSE);
}
//
// Halt all existing NETBIOS connections (and dont allow for new ones)
if(FAILED(StopNetbiosTransport())) {
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Closing NETBIOS transport failed")));
fSuccess = FALSE;
ASSERT(FALSE);
}
//
// Stop the cracker -- this will flush all existing packets
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Halt SMB Cracker")));
if(FAILED(StopCracker())) {
TRACEMSG(ZONE_INIT, (L"-SMB_Deinit -- error stopping cracker!!"));
fSuccess = FALSE;
ASSERT(FALSE);
}
//
// Now shut off the transport threads
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Halting TCP transport")));
if(FAILED(StopTCPTransport())) {
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Closing TCP transport failed")));
fSuccess = FALSE;
ASSERT(FALSE);
}
TRACEMSG(ZONE_INIT, (TEXT("-SMB_Deinit - Deleting memory")));
//
// Delete all connection information
// The ShareManager needs to be around still!
if(SMB_Globals::g_pConnectionManager) {
delete SMB_Globals::g_pConnectionManager;
SMB_Globals::g_pConnectionManager = NULL;
}
//
// Stop print spool
if(FAILED(StopPrintSpool())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init -- couldnt stop print spool!!")));
ASSERT(FALSE);
}
//
// Delete all share information (print spool must be down by now
// because the share binds to the print spool and blocks on its termination)
if(SMB_Globals::g_pShareManager) {
delete SMB_Globals::g_pShareManager;
SMB_Globals::g_pShareManager = NULL;
}
//
// Delete wakeup event object iff the namechange notification handle isnt alive
if(SMB_Globals::g_pWakeUpOnEvent && !NETBIOS_TRANSPORT::NameChangeNotification::h) {
delete SMB_Globals::g_pWakeUpOnEvent;
SMB_Globals::g_pWakeUpOnEvent = NULL;
}
//
// Delete the abstract file system
if(NULL != SMB_Globals::g_pAbstractFileSystem) {
delete SMB_Globals::g_pAbstractFileSystem;
SMB_Globals::g_pAbstractFileSystem = NULL;
}
//
// Delete the printers memmapped buffers
if(FAILED(SMB_Globals::g_PrinterMemMapBuffers.Close())) {
TRACEMSG(ZONE_ERROR, (L"SMB_PRINT: Error closing swap buffer!"));
ASSERT(FALSE);
}
//
// Destroy any netbios memory
DestroyNetbiosTransport();
g_fServerRunning = FALSE;
TRACEMSG(ZONE_INIT,(TEXT("-SMB_Deinit")));
return fSuccess;
}
DWORD
SMB_Open(
DWORD dwClientContext,
DWORD dwAccess,
DWORD dwShareMode
)
{
TRACEMSG(ZONE_INIT,(TEXT("+SMB_Open")));
return SMB_CONTEXT;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -