Skip to content

Commit

Permalink
Hider API: add ability to delete all persistent rules
Browse files Browse the repository at this point in the history
The application that uses Hider API does not have an ability
to query which persistent rules exist. So in order to use
UsbDk_DeletePersistentHideRule the application shall guess
which rule it needs to delete. Current commit adds ability to
delete all existing the persistent rules. This API requires
administrative privileges as all APIs related to persistent
hide rules.

Signed-off-by: Yuri Benditovich <yuri.benditovich@daynix.com>
  • Loading branch information
ybendito authored and YanVugenfirer committed Apr 15, 2019
1 parent a07a062 commit b7acf79
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
14 changes: 14 additions & 0 deletions UsbDkController/UsbDkController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ static void ShowUsage()
tcout << TEXT(" UsbDkController -H TYPE VID PID BCD Class Hide - add dynamic hide rule") << endl;
tcout << TEXT(" UsbDkController -P TYPE VID PID BCD Class Hide - add persistent hide rule") << endl;
tcout << TEXT(" UsbDkController -D TYPE VID PID BCD Class Hide - delete persistent hide rule") << endl;
tcout << TEXT(" UsbDkController -Z - delete all persistent hide rules") << endl;
tcout << endl;
tcout << TEXT(" <VID PID BCD Class> May be specific value or -1 to match all") << endl;
tcout << TEXT(" <Hide> Should be 0 or 1, if 0, the rule is terminal") << endl;
Expand Down Expand Up @@ -319,6 +320,15 @@ static void Controller_HideDevice(TCHAR *Type, TCHAR *VID, TCHAR *PID, TCHAR *BC
UsbDk_CloseHiderHandle(hiderHandle);
}

static int Controller_DeleteAllPersistentHideRules()
{
ULONG done = 0, notDone = 0;
auto res = UsbDk_DeleteAllPersistentRules(&done, &notDone);
tcout << TEXT("Cleaning persistent rules: done ") << dec << done
<< TEXT(", not done ") << notDone << endl;
return Controller_AnalyzeInstallResult(res, TEXT("Clean persistent hide rules"));
}

static bool Controller_ChdirToPackageFolder()
{
TCHAR PackagePath[MAX_PATH];
Expand Down Expand Up @@ -410,6 +420,10 @@ int __cdecl _tmain(int argc, TCHAR* argv[])
}
return Controller_DeletePersistentHideRule(argv[2], argv[3], argv[4], argv[5], argv[6], argv[7]);
}
else if (_tcscmp(L"-Z", argv[1]) == 0)
{
return Controller_DeleteAllPersistentHideRules();
}
else
{
ShowUsage();
Expand Down
17 changes: 17 additions & 0 deletions UsbDkHelper/RuleManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,20 @@ void CRulesManager::DeleteRule(const USB_DK_HIDE_RULE &Rule)
}
}
}

ULONG CRulesManager::DeleteAllRules(ULONG& notDeleted)
{
ULONG deleted = 0;
notDeleted = 0;
vector<wstring> subkeys;

for (const auto &SubKey : m_RegAccess)
subkeys.push_back(SubKey);

while (subkeys.size())
{
m_RegAccess.DeleteKey(subkeys.front().c_str()) ? deleted++ : notDeleted++;
subkeys.erase(subkeys.begin());
}
return deleted;
}
1 change: 1 addition & 0 deletions UsbDkHelper/RuleManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class CRulesManager

void AddRule(const USB_DK_HIDE_RULE &Rule);
void DeleteRule(const USB_DK_HIDE_RULE &Rule);
ULONG DeleteAllRules(ULONG& notDeleted);
private:
template <typename TFunctor>
bool FindRule(const USB_DK_HIDE_RULE &Rule, TFunctor Functor);
Expand Down
26 changes: 26 additions & 0 deletions UsbDkHelper/UsbDkHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -401,3 +401,29 @@ DLL InstallResult UsbDk_DeletePersistentHideRule(PUSB_DK_HIDE_RULE_PUBLIC Public
{
return UsbDk_DeleteExtendedPersistentHideRule(PublicRule, USBDK_HIDER_RULE_DEFAULT);
}

DLL InstallResult UsbDk_DeleteAllPersistentRules(OUT PULONG pDeleted, OUT PULONG pNotDeleted)
{
try
{
CRulesManager Manager;

*pDeleted = *pNotDeleted = 0;
*pDeleted = Manager.DeleteAllRules(*pNotDeleted);

UsbDkDriverAccess driver;
driver.UpdateRegistryParameters();

return *pNotDeleted ? InstallFailure : InstallSuccess;
}
catch (const UsbDkDriverFileException &e)
{
printExceptionString(e.what());
return InstallSuccessNeedReboot;
}
catch (const exception &e)
{
printExceptionString(e.what());
return InstallFailure;
}
}
22 changes: 20 additions & 2 deletions UsbDkHelper/UsbDkHelperHider.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ extern "C" {
*
* @note
* 1. Persistent rule stays until explicitly deleted by
* UsbDk_DeletePersistentHideRule()
* UsbDk_DeletePersistentHideRule or UsbDk_DeleteAllPersistentRules
* 2. This API requires administrative privileges
* 3. For already attached devices the rule will be applied after
* device re-plug or system reboot.
Expand All @@ -167,7 +167,7 @@ extern "C" {
*
* @note
* 1. Persistent rule stays until explicitly deleted by
* UsbDk_DeletePersistentHideRule()
* UsbDk_DeleteExtendedPersistentHideRule() or UsbDk_DeleteAllPersistentRules
* 2. This API requires administrative privileges
* 3. For already attached devices the rule will be applied after
* device re-plug or system reboot.
Expand Down Expand Up @@ -211,6 +211,24 @@ extern "C" {
*/
DLL InstallResult UsbDk_DeleteExtendedPersistentHideRule(PUSB_DK_HIDE_RULE_PUBLIC Rule, ULONG Type);

/* Delete all persistent hide rules
*
* @params
* IN - None
* OUT - PULONG pDeleted - number of deleted rules
* PULONG pNotDeleted - number of not deleted rules
*
* @return
* Rule removal status
*
* @note
* 1. This API requires administrative privileges
* 2. For already attached devices the rules become inactive after
* device re-plug or system reboot.
*
*/
DLL InstallResult UsbDk_DeleteAllPersistentRules(OUT PULONG pDeleted, OUT PULONG pNotDeleted);

#ifdef __cplusplus
}
#endif

0 comments on commit b7acf79

Please sign in to comment.