⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 driver.cpp

📁 Windows CE 6.0 Server 源码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
BOOL
SMB_Close(
    DWORD dwOpenContext
    )
{
    TRACEMSG(ZONE_INIT,(TEXT("+SMB_Close")));
    return 0;
}


DWORD
SMB_Read(
    DWORD dwOpenContext,
    LPVOID pBuffer,
    DWORD dwNumBytes
    )
{
    TRACEMSG(ZONE_INIT,(TEXT("+SMB_Read")));
    return 0;
}


DWORD
SMB_Write(
    DWORD dwOpenContext,
    LPCVOID pBuffer,
    DWORD dwNumBytes
    )
{
    TRACEMSG(ZONE_INIT,(TEXT("+SMB_Write")));
    return 0;
}


DWORD
SMB_Seek(
    DWORD dwOpenContext,
    long lDistance,
    DWORD dwMoveMethod
    )
{
    TRACEMSG(ZONE_INIT,(TEXT("+SMB_Seek")));
    return 0;
}

void SMB_PowerUp(void)  {return;}
void SMB_PowerDown(void){return;}

BOOL IOCTL_Change_ACL(ce::marshal_arg<ce::copy_in,PCWSTR> pShareName, ce::marshal_arg<ce::copy_in,PCWSTR> pACL, ce::marshal_arg<ce::copy_in,PCWSTR> pROACL)
{
    CReg ShareKey;
    HRESULT hr = E_FAIL;

    StringConverter ShareName;
    ShareName.append(L"Services\\SMBServer\\Shares\\");
    ShareName.append(pShareName);

    if(FALSE == ShareKey.Open(HKEY_LOCAL_MACHINE, ShareName.GetString())) {
        RETAILMSG(1, (L"SMBSERVER: Error -- cant change ACL because %s isnt a valid share", pShareName));
        goto Done;
    }
    if(pACL && FALSE == ShareKey.SetSZ(L"UserList", pACL, wcslen(pACL))) {
        RETAILMSG(1, (L"SMBSERVER: Error -- cant change ACL because %s userlist cant be set", pShareName));
        goto Done;
    }
    if(pROACL && FALSE == ShareKey.SetSZ(L"ROUserList", pROACL, wcslen(pROACL))) {
        RETAILMSG(1, (L"SMBSERVER: Error -- cant change ACL because %s ROUserList cant be set", pShareName));
        goto Done;
    }

    if(TRUE == g_fServerRunning && NULL != SMB_Globals::g_pShareManager) {
        SMB_Globals::g_pShareManager->ReloadACLS();
    }

    hr = S_OK;
    Done:
    if (FAILED(hr)) {
        SetLastError(hr);
        return FALSE;
    }
    return TRUE;
}

BOOL IOCTL_Add_Share(ce::marshal_arg<ce::copy_in,PCWSTR> pName, DWORD dwType, ce::marshal_arg<ce::copy_in,PCWSTR> pPath, ce::marshal_arg<ce::copy_in,PCWSTR> pACL, ce::marshal_arg<ce::copy_in,PCWSTR> pROACL, ce::marshal_arg<ce::copy_in,PCWSTR> pDriver, ce::marshal_arg<ce::copy_in,PCWSTR> pComment)
{
    HRESULT hr;
    CReg regPaths;

    if(0 != wcsstr(pPath, L"..")) {
        hr = E_FAIL;
        goto Done;
    }

    if(regPaths.OpenOrCreateRegKey(HKEY_LOCAL_MACHINE, L"Services\\SMBServer\\ExcludePaths")) {
        WCHAR wcName[100];
        WCHAR wcPath[500];

        while(regPaths.EnumValue(wcName, sizeof(wcName)/sizeof(WCHAR), wcPath, sizeof(wcPath))) {
            if(0 == _memicmp(pPath, wcPath, wcslen(wcPath))) {
                RETAILMSG(1, (L"SMBSERVER: Error adding share with path name %s because that path is blocked", pPath));
                hr = E_FAIL;
                goto Done;
            }
        }
    }

    if(SUCCEEDED(hr = CreateShare(pName, dwType, pPath, pACL, pROACL, pDriver, pComment))) {
        if(FALSE == g_fServerRunning) {
            SMB_RestartServer();
        } else {
            AddShare(pName);
        }
    }

    Done:
    if (FAILED(hr)) {
        SetLastError(hr);
        return FALSE;
    }
    return TRUE;
}

BOOL IOCTL_Del_Share(ce::marshal_arg<ce::copy_in,PCWSTR> pName)
{
    // NOTE: its safe to search for this share and use it w/o CritSection b/c we are the
    //   only person that ever deletes shares
    Share *pMyShare = NULL;

    if(g_fServerRunning) {
        pMyShare = SMB_Globals::g_pShareManager->SearchForShare((WCHAR*)((const WCHAR*)pName));
    }

    ce::wstring RegPath = L"Services\\SMBServer\\Shares\\";
    RegPath.append((WCHAR *)((const WCHAR*)pName));
    HRESULT hr = E_FAIL;

    if(g_fServerRunning && NULL != pMyShare) {
            if(SUCCEEDED(SMB_Globals::g_pConnectionManager->TerminateTIDsOnShare(pMyShare))) {
               SMB_Globals::g_pShareManager->DeleteShare(pMyShare);
               if(ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, RegPath)) {
                   RETAILMSG(1, (L"SMBSRV:  Share %s doesnt exist in the registry!", (WCHAR *)(RegPath.get_buffer())));
               }
               hr = S_OK;
            }
    } else if(!g_fServerRunning) {
      if(ERROR_SUCCESS != RegDeleteKey(HKEY_LOCAL_MACHINE, RegPath)) {
          RETAILMSG(1, (L"SMBSRV:  Share %s doesnt exist in the registry!", (WCHAR *)(RegPath.get_buffer())));
      } else {
          SMB_RestartServer();
          hr = S_OK;
      }
    }

    if (FAILED(hr)) {
        SetLastError(hr);
        return FALSE;
    }
    return TRUE;
}

BOOL IOCTL_List_Connected_Users(ce::marshal_arg<ce::copy_out, ce::psl_buffer_wrapper<PWSTR> > pBuffer,
                                   ce::marshal_arg<ce::copy_out, PDWORD> puiLen)
{
  ce::wstring WhosOn = L"";

  if(g_fServerRunning && SMB_Globals::g_pConnectionManager) {
      SMB_Globals::g_pConnectionManager->ListConnectedUsers(WhosOn);
  }
  *puiLen = sizeof(WCHAR) * (WhosOn.length()+1);

  if(pBuffer.count() < (WhosOn.length()+1)) {
      SetLastError(E_OUTOFMEMORY);
      return FALSE;
  }
  memcpy(pBuffer.buffer(), WhosOn.get_buffer(),sizeof(WCHAR) * (WhosOn.length()+1));
  return TRUE;
}

BOOL IOCTL_QueryAmountTransfered(ce::marshal_arg<ce::copy_out, LARGE_INTEGER*> pRead,
                                    ce::marshal_arg<ce::copy_out, LARGE_INTEGER*> pWritten)
{
  CCritSection csLock(&SMB_Globals::g_Bookeeping_CS);
  csLock.Lock();
  pRead->QuadPart = SMB_Globals::g_Bookeeping_TotalRead.QuadPart;
  pWritten->QuadPart = SMB_Globals::g_Bookeeping_TotalWritten.QuadPart;
  return TRUE;
}




BOOL
SMB_IOControl(
    DWORD dwOpenContext,
    DWORD dwIoControlCode,
    PBYTE pInBuf,
    DWORD nInBufSize,
    PBYTE pOutBuf,
    DWORD nOutBufSize,
    PDWORD pBytesReturned
    )
{
    BOOL fRet = 1;
    CCritSection csLock(&g_csDriverLock);
    csLock.Lock();


    switch(dwIoControlCode) {
        case IOCTL_SERVICE_REFRESH:
            SMB_RestartServer();
            break;
        case SMB_IOCTL_INVOKE: {
            ce::psl_stub<> stub(pInBuf, nInBufSize);

            switch(stub.function()) {
                case IOCTL_CHANGE_ACL:
                    fRet = stub.call(IOCTL_Change_ACL);
                    break;
                case IOCTL_ADD_SHARE:
                    fRet = stub.call(IOCTL_Add_Share);
                    break;
                case IOCTL_DEL_SHARE:
                    fRet = stub.call(IOCTL_Del_Share);
                    break;
                case IOCTL_LIST_USERS_CONNECTED:
                    fRet = stub.call(IOCTL_List_Connected_Users);
                    break;
                case IOCTL_QUERY_AMOUNT_TRANSFERED:
                    fRet = stub.call(IOCTL_QueryAmountTransfered);
                    break;
            }
            break;
        }
      /*  case IOCTL_SET_MAX_CONNS:
            if(sizeof(UINT) == nInBufSize) {
                CReg reg;
                if(reg.OpenOrCreateRegKey(HKEY_LOCAL_MACHINE, L"Services\\SMBServer")) {
                   SMB_Globals::g_uiMaxConnections = *((UINT *)pInBuf);
                   reg.SetDW(L"MaxConnections", SMB_Globals::g_uiMaxConnections);
                   TRACEMSG(ZONE_INIT, (L"SMB_SRV: Set Max Connections to: %d", SMB_Globals::g_uiMaxConnections));
                } else {
                    TRACEMSG(ZONE_ERROR, (L"SMB_SRV: SMB registry not present!"));
                    ASSERT(FALSE);
                }
            }
            break;
        */

        case IOCTL_SERVICE_STOP:
            TRACEMSG(ZONE_INIT,(L"+SMB_IOControl, STOP IOCTL"));
            SMB_Deinit(0);
            break;
        case IOCTL_SERVICE_START:
            TRACEMSG(ZONE_INIT,(L"+SMB_IOControl, START IOCTL"));
            SMB_Init(0);
            break;
        #ifdef DEBUG
        case IOCTL_DEBUG_PRINT:
            SMB_Globals::g_pConnectionManager->DebugPrint();
            break;
        #endif
        default:
            TRACEMSG(ZONE_INIT,(TEXT("+SMB_IOControl, unknown code 0x%X"),dwIoControlCode));
            break;
    }
    return fRet; //0=failure, nonzero=success
}


DWORD SMBSRVR_RestartServerThread(LPVOID netnum)
{
    CCritSection csLock(&g_csDriverLock);
    csLock.Lock();
    SMB_Deinit(0);
    SMB_Init(0);
    return 0;
}


VOID SMB_RestartServer()
{
    HANDLE h;
    if(NULL == (h = CreateThread(NULL, 0, SMBSRVR_RestartServerThread, 0, 0, NULL))) {
        TRACEMSG(ZONE_ERROR, (L"SMBSRV-CRACKER: cant make restart thread"));
        ASSERT(FALSE);
        return;
    }
    CloseHandle(h);
}

extern "C"
BOOL WINAPI DllMain(IN HANDLE DllHandle,
                    IN ULONG Reason,
                    IN PVOID Context OPTIONAL)
{
    switch(Reason) {

        case DLL_PROCESS_ATTACH:
            TRACEMSG(ZONE_INIT,(TEXT("SMBSRV: DLL_PROCESS_ATTACH, hInst:0x%X"), DllHandle));
            InitializeCriticalSection(&g_csDriverLock);
            DEBUGREGISTER((HINSTANCE)DllHandle);
            SMB_Globals::g_Bookeeping_TotalRead.QuadPart = 0;
            SMB_Globals::g_Bookeeping_TotalWritten.QuadPart = 0;
            InitializeCriticalSection(&SMB_Globals::g_Bookeeping_CS);
            return TRUE;

        case DLL_PROCESS_DETACH:
            TRACEMSG(ZONE_INIT,(TEXT("SMBSRV: DLL_PROCESS_DETACH")));
            DeleteCriticalSection(&g_csDriverLock);
            DeleteCriticalSection(&SMB_Globals::g_Bookeeping_CS);
            return TRUE;

        case DLL_THREAD_ATTACH:    // A new thread has been created
        case DLL_THREAD_DETACH:    // Thread has exited
            // No processing for these
            return TRUE;

        default:
            TRACEMSG(ZONE_ERROR,(TEXT("SMBSRV:DllEntry: Invalid reason #%d"), Reason));
            return FALSE;
            break;
    }
    return TRUE;
}


HRESULT CreateShare(const WCHAR *pName,
                    DWORD dwType,
                    const WCHAR *pPath,
                    const WCHAR *pACL,
                    const WCHAR *pROACL,
                    const WCHAR *pDriver,
                    const WCHAR *pComment)
{
    ce::wstring RegPath = L"Services\\SMBServer\\Shares\\";
    HRESULT hr = E_FAIL;
    CReg reg;

    if(NULL == pName) {
        goto Done;
    }

    RegPath += pName;
    if(FALSE == reg.OpenOrCreateRegKey(HKEY_LOCAL_MACHINE, RegPath)) {
        goto Done;
    }

    //
    // These values are for all share typessd
    if(FALSE == reg.SetDW(L"Type", dwType)) {
        goto Done;
    }
    if(NULL != pPath && FALSE == reg.SetSZ(L"Path", pPath)) {
        goto Done;
    }
    if(NULL != pACL && FALSE == reg.SetSZ(L"UserList", pACL)) {
       goto Done;
    }
    if(NULL != pROACL && FALSE == reg.SetSZ(L"ROUserList", pROACL)) {
       goto Done;
    }

    if(dwType == STYPE_PRINTQ) {
        if(NULL != pDriver && FALSE == reg.SetSZ(L"Driver", pDriver)) {
            goto Done;
        }
        if(NULL != pComment && FALSE == reg.SetSZ(L"Comment", pComment)) {
           goto Done;
        }
    } else if(dwType == STYPE_DISKTREE) {
    } else {
        RETAILMSG(1, (L"SMBSRV: Error Creating Share! unknown type!"));
        ASSERT(FALSE);
        goto Done;
    }

    hr = S_OK;
    Done:
        return hr;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -