📄 driver.cpp
字号:
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// Use of this source code is subject to the terms of the Microsoft shared
// source or premium shared source license agreement under which you licensed
// this source code. If you did not accept the terms of the license agreement,
// you are not authorized to use this source code. For the terms of the license,
// please see the license agreement between you and Microsoft or, if applicable,
// see the SOURCE.RTF on your install media or the root of your tools installation.
// THE SOURCE CODE IS PROVIDED "AS IS", WITH NO WARRANTIES.
//
//
// Windows CE SMB file server driver componet
//
#include <CReg.hxx>
#include <String.hxx>
#include <service.h>
#include <GUIDGen.h>
#include <svsutil.hxx>
#include <strsafe.h>
#include "SMB_Globals.h"
#include "utils.h"
#include "NetbiosTransport.h"
#include "TCPTransport.h"
#include "ShareInfo.h"
#include "PrintQueue.h"
#include "Cracker.h"
#include "FileServer.h"
#include "ConnectionManager.h"
#include "SMBIOCTL.h"
#include <psl_marshaler.hxx>
class InitClass {
public:
InitClass() {
static BOOL fInited = FALSE;
if(FALSE == fInited) {
#ifdef DEBUG
SMB_Globals::g_lMemoryCurrentlyUsed = 0;
#endif
svsutil_Initialize();
fInited = TRUE;
} else {
ASSERT(FALSE);
}
}
~InitClass() {
svsutil_DeInitialize();
}
};
// Stream interface functions
#define SMB_CONTEXT 0xABCDEF01
//
// Global that will call into SVS Utils init etc (any construction that must happen first)
InitClass g_InitClass;
ThreadSafePool<10, SMB_PACKET> SMB_Globals::g_SMB_Pool;
BOOL g_fServerRunning = FALSE;
CRITICAL_SECTION g_csDriverLock;
extern HRESULT StartListenOnNameChange();
extern VOID SMB_RestartServer();
HRESULT CreateShare(const WCHAR *pName,
DWORD dwType,
const WCHAR *pPath,
const WCHAR *pACL,
const WCHAR *pROACL,
const WCHAR *pDriver,
const WCHAR *pComment);
#define TOASCII(x, dest) \
{ \
dest = NULL; \
UINT uiSize; \
if(0 != (uiSize = WideCharToMultiByte(CP_ACP, 0, x, -1, NULL, 0,NULL,NULL))) \
{ \
__try { \
dest = (char *)_alloca(uiSize); \
\
WideCharToMultiByte(CP_ACP, 0, x, -1, dest, uiSize, NULL, NULL); \
} __except(1) { \
dest = NULL; \
} \
} \
}
HRESULT AddFileShare(const WCHAR *_pName,
const WCHAR *_pPath,
const WCHAR *_pACL,
const WCHAR *_pROACL)
{
ASSERT(TRUE == g_fFileServer);
HRESULT hr = E_FAIL;
Share *pFileShare = NULL;
//
// Verify incoming params
if(NULL == _pName ||
NULL == _pPath) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - Invalid Params")));
ASSERT(FALSE);
goto Done;
}
//add file as a share
if(NULL == (pFileShare = new Share())) {
hr = E_OUTOFMEMORY;
goto Done;
}
pFileShare->SetDirectoryName(_pPath);
pFileShare->SetShareName(_pName);
pFileShare->SetShareType(STYPE_DISKTREE);
pFileShare->SetServiceName(L"A:");
pFileShare->SetACL(_pACL, _pROACL);
pFileShare->SetRequireAuth(TRUE);
if(FAILED(hr = SMB_Globals::g_pShareManager->AddShare(pFileShare))) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- cant add default share!")));
delete pFileShare;
goto Done;
}
hr = S_OK;
Done:
return hr;
}
HRESULT AddPrintSpool(const WCHAR *_pName,
const WCHAR *_pDriverName,
const WCHAR *_pProc,
const WCHAR *_pComment,
const WCHAR *_pDirectoryName,
const WCHAR *_pShareName,
const WCHAR *_pServiceName,
const WCHAR *_pRemark,
const WCHAR *_pPortName,
const WCHAR *_pACL)
{
SMBPrintQueue *pPrintQueue;
Share *pPrinterShare;
HRESULT hr = E_FAIL;
CHAR *pName;
CHAR *pDriverName;
CHAR *pProc;
CHAR *pComment;
CHAR *pRemark;
TOASCII(_pName, pName);
TOASCII(_pDriverName, pDriverName);
TOASCII(_pProc, pProc);
TOASCII(_pComment, pComment);
TOASCII(_pRemark, pRemark);
if(NULL == _pName ||
NULL == _pDriverName ||
NULL == _pProc ||
NULL == _pComment ||
NULL == _pRemark) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - Error doing ASCII conversion")));
goto Done;
}
//
if(NULL == (pPrintQueue = new SMBPrintQueue())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- couldnt create printqueue")));
hr = E_OUTOFMEMORY;
goto Done;
}
//
// Add all printers connected to the mix
pPrintQueue->SetStartTime(SecSinceJan1970_0_0_0());
pPrintQueue->SetName(pName);
pPrintQueue->SetDriverName(pDriverName);
pPrintQueue->SetPrProc(pProc);
pPrintQueue->SetSepFile("");
pPrintQueue->SetParams("");
pPrintQueue->SetComment(pComment);
pPrintQueue->SetPriority(1);
pPrintQueue->SetPortName(_pPortName);
//add \printer as a share
if(NULL == (pPrinterShare = new Share())) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- couldnt create shares")));
hr = E_OUTOFMEMORY;
goto Done;
}
pPrinterShare->SetDirectoryName(_pDirectoryName);
pPrinterShare->SetShareName(_pShareName);
pPrinterShare->SetServiceName(_pServiceName);
pPrinterShare->SetRemark(pRemark);
pPrinterShare->SetShareType(STYPE_PRINTQ);
pPrinterShare->SetPrintQueue(pPrintQueue);
pPrinterShare->SetRequireAuth(TRUE);
pPrinterShare->SetACL(_pACL, L"");
if(FAILED(hr = SMB_Globals::g_pShareManager->AddShare(pPrinterShare))) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- cant add default share!")));
}
hr = S_OK;
Done:
return hr;
}
HRESULT AddShare(const WCHAR *ShareName)
{
HRESULT hr = E_FAIL;
CReg ShareKey;
WCHAR Path[MAX_PATH];
TRACEMSG(ZONE_INIT, (L"Loading Share: %s", ShareName));
WCHAR RegPath[MAX_PATH];
WCHAR RegDriver[MAX_PATH];
WCHAR RegComment[MAX_PATH];
WCHAR ACL[MAX_PATH];
WCHAR ROACL[MAX_PATH];
DWORD dwType;
DWORD dwRegType;
DWORD dwLen = sizeof(DWORD);
if(!ShareName) {
RETAILMSG(1, (L" -- Share Name NULL\n"));
goto Done;
}
hr = StringCbPrintf(Path, MAX_PATH*sizeof(WCHAR), L"Services\\SMBServer\\Shares\\%s", ShareName);
if(FAILED(hr)) {
RETAILMSG(1, (L" -- Failed to combine %s into Services\\SMBServer\\Shares\\%s", ShareName, ShareName));
goto Done;
}
RETAILMSG(1, (L"SMB_PRINT: Loading Registry: %s\n", Path));
if(FALSE == ShareKey.Open(HKEY_LOCAL_MACHINE, Path)) {
RETAILMSG(1, (L" -- Cant Open KEY %s\n", Path));
goto Done;
}
//
// Get the type
if(ERROR_SUCCESS != RegQueryValueEx(ShareKey, L"Type", 0, &dwRegType, (LPBYTE)&dwType, &dwLen) ||
REG_DWORD != dwRegType ||
sizeof(DWORD) != dwLen) {
RETAILMSG(1, (L" -- Cant Open TYPE on %s\n", Path));
goto Done;
}
RETAILMSG(1, (L" -- Type: %d\n", dwType));
//
// Only let file shares in if we are linked up with the file server
// code -- otherwise just do print
if(FALSE == (g_fFileServer && dwType == STYPE_DISKTREE) &&
dwType != STYPE_PRINTQ) {
RETAILMSG(1, (L" -- unknown type!! -- we only support printers"));
goto Done;
}
//
// Get the Path
if(FALSE == ShareKey.ValueSZ(L"Path", RegPath, MAX_PATH/sizeof(WCHAR))) {
RETAILMSG(1, (L" -- Cant get PATH\n"));
goto Done;
}
RETAILMSG(1, (L" -- Path: %s\n", RegPath));
//
// Get the ACL
if(FALSE == ShareKey.ValueSZ(L"UserList", ACL, MAX_PATH/sizeof(WCHAR))) {
RETAILMSG(1, (L" -- NOT USING ACL's!\n"));
ACL[0] = NULL;
}
RETAILMSG(1, (L" -- ACL: %s\n", ACL));
if(FALSE == ShareKey.ValueSZ(L"ROUserList", ROACL, MAX_PATH/sizeof(WCHAR))) {
RETAILMSG(1, (L" -- NOT USING READ-ONLY ACL's!\n"));
ROACL[0] = NULL;
}
RETAILMSG(1, (L" -- Read-ONLYACL: %s\n", ROACL));
//
// Get printer specific information
if(dwType == STYPE_PRINTQ)
{
//
// Get the Driver (printer specific)
if(FALSE == ShareKey.ValueSZ(L"Driver", RegDriver, MAX_PATH/sizeof(WCHAR))) {
RETAILMSG(1, (L" -- Cant get Driver\n"));
RegDriver[0] = NULL;
}
RETAILMSG(1, (L" -- Driver: %s\n", RegDriver));
//
// Get the Comment (printer specific)
if(FALSE == ShareKey.ValueSZ(L"Comment", RegComment, MAX_PATH/sizeof(WCHAR))) {
RETAILMSG(1, (L" -- Cant get Comment\n"));
RegComment[0] = 0;
}
RETAILMSG(1, (L" -- Comment: %s\n", RegComment));
//
// Load up the driver
AddPrintSpool(ShareName,
RegDriver,
L"WinPrint",
RegComment,
L"PRINTER_DIR",
ShareName,
L"LPT1:",
RegComment,
RegPath,
ACL);
}else if (dwType == STYPE_DISKTREE) {
//
// Add the fileshare
ASSERT(TRUE == g_fFileServer);
AddFileShare(ShareName, RegPath, ACL, ROACL);
}
hr = S_OK;
Done:
return hr;
}
HRESULT AddShares()
{
CReg RegShares;
WCHAR ShareName[MAX_PATH];
BOOL fFoundSomething = FALSE;
Share *pIPCShare = NULL;
//
// Add IPC Share
if(NULL == (pIPCShare = new Share())) {
return E_OUTOFMEMORY;
}
pIPCShare->SetDirectoryName(L"IPC$");
pIPCShare->SetShareName(L"IPC$");
pIPCShare->SetServiceName(L"IPC");
pIPCShare->SetRequireAuth(FALSE); //let anyone in
pIPCShare->SetRemark("Remote Inter Process Communication");
pIPCShare->SetShareType(STYPE_IPC);
pIPCShare->SetPrintQueue(NULL);
if(FAILED(SMB_Globals::g_pShareManager->AddShare(pIPCShare))) {
TRACEMSG(ZONE_ERROR, (TEXT("-SMB_Init - StartServer failed -- cant add IPC$ share!")));
goto Done;
}
//
// Load the CName from the registry
if(FALSE == RegShares.Open(HKEY_LOCAL_MACHINE, L"Services\\SMBServer\\Shares")) {
RETAILMSG(1, (L"No registy key under Services\\SMBServer\\Shares"));
}
while(TRUE == RegShares.EnumKey(ShareName, MAX_PATH)) {
if(SUCCEEDED(AddShare(ShareName))) {
fFoundSomething = TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -