summaryrefslogtreecommitdiff
path: root/shared/privilege.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'shared/privilege.cpp')
-rw-r--r--shared/privilege.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/shared/privilege.cpp b/shared/privilege.cpp
new file mode 100644
index 00000000..16f9d385
--- /dev/null
+++ b/shared/privilege.cpp
@@ -0,0 +1,102 @@
+#include "privilege.h"
+#include <wx/intl.h>
+#include "system_func.h"
+#include <boost/shared_ptr.hpp>
+
+using namespace ffs3;
+
+
+Privileges& Privileges::getInstance()
+{
+ static Privileges instance;
+ return instance;
+}
+
+
+bool Privileges::privilegeIsActive(LPCTSTR privilege) //throw FileError()
+{
+ HANDLE hToken = NULL;
+ if (!::OpenProcessToken(::GetCurrentProcess(), //__in HANDLE ProcessHandle,
+ TOKEN_QUERY, //__in DWORD DesiredAccess,
+ &hToken)) //__out PHANDLE TokenHandle
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+ boost::shared_ptr<void> dummy(hToken, ::CloseHandle);
+
+ LUID luid = {0};
+ if (!::LookupPrivilegeValue(
+ NULL, //__in_opt LPCTSTR lpSystemName,
+ privilege, //__in LPCTSTR lpName,
+ &luid )) //__out PLUID lpLuid
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+
+ PRIVILEGE_SET priv = {0};
+ priv.PrivilegeCount = 1;
+ priv.Control = PRIVILEGE_SET_ALL_NECESSARY;
+ priv.Privilege[0].Luid = luid;
+ priv.Privilege[0].Attributes = SE_PRIVILEGE_ENABLED;
+
+ BOOL alreadyGranted = FALSE;
+ if (!::PrivilegeCheck(
+ hToken, //__in HANDLE ClientToken,
+ &priv, //__inout PPRIVILEGE_SET RequiredPrivileges,
+ &alreadyGranted)) //__out LPBOOL pfResult
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+
+ return alreadyGranted == TRUE;
+}
+
+
+void Privileges::setPrivilege(LPCTSTR privilege, bool enable) //throw FileError()
+{
+ HANDLE hToken = NULL;
+ if (!::OpenProcessToken(::GetCurrentProcess(), //__in HANDLE ProcessHandle,
+ TOKEN_ADJUST_PRIVILEGES, //__in DWORD DesiredAccess,
+ &hToken)) //__out PHANDLE TokenHandle
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+ boost::shared_ptr<void> dummy(hToken, ::CloseHandle);
+
+ LUID luid = {0};
+ if (!::LookupPrivilegeValue(
+ NULL, //__in_opt LPCTSTR lpSystemName,
+ privilege, //__in LPCTSTR lpName,
+ &luid )) //__out PLUID lpLuid
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+
+ TOKEN_PRIVILEGES tp = {0};
+ tp.PrivilegeCount = 1;
+ tp.Privileges[0].Luid = luid;
+ tp.Privileges[0].Attributes = enable ? SE_PRIVILEGE_ENABLED : 0;
+
+ if (!::AdjustTokenPrivileges(
+ hToken, //__in HANDLE TokenHandle,
+ false, //__in BOOL DisableAllPrivileges,
+ &tp, //__in_opt PTOKEN_PRIVILEGES NewState,
+ 0, //__in DWORD BufferLength,
+ NULL, //__out_opt PTOKEN_PRIVILEGES PreviousState,
+ NULL)) //__out_opt PDWORD ReturnLength
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+
+ if (::GetLastError() == ERROR_NOT_ALL_ASSIGNED) //check although previous function returned with success!
+ {
+ const wxString errorMessage = wxString(_("Error setting privilege:")) + wxT(" \"") + privilege + wxT("\"") + wxT("\n\n");
+ throw FileError(errorMessage + ffs3::getLastErrorFormatted());
+ }
+}
bgstack15