2 * Enable backup privileges for Win32 systems.
4 * Kern Sibbald, May MMIII
10 Copyright (C) 2000-2003 Kern Sibbald and John Walker
12 This program is free software; you can redistribute it and/or
13 modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation; either version 2 of
15 the License, or (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 General Public License for more details.
22 You should have received a copy of the GNU General Public
23 License along with this program; if not, write to the Free
24 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
34 /*=============================================================*/
36 /* * * * U n i x * * * * */
38 /*=============================================================*/
40 #if !defined(HAVE_CYGWIN) && !defined(HAVE_WIN32)
42 int enable_backup_privileges(JCR *jcr, int ignore_errors)
50 /*=============================================================*/
52 /* * * * W i n 3 2 * * * * */
54 /*=============================================================*/
56 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
58 void win_error(JCR *jcr, char *prefix, DWORD lerror);
61 enable_priv(JCR *jcr, HANDLE hToken, char *name, int ignore_errors)
66 if (!(p_LookupPrivilegeValue && p_AdjustTokenPrivileges)) {
67 return 0; /* not avail on this OS */
70 // Get the LUID for the security privilege.
71 if (!p_LookupPrivilegeValue(NULL, name, &tkp.Privileges[0].Luid)) {
73 win_error(jcr, "LookupPrivilegeValue", GetLastError());
77 /* Set the security privilege for this process. */
78 tkp.PrivilegeCount = 1;
79 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
80 p_AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);
81 lerror = GetLastError();
82 if (lerror != ERROR_SUCCESS) {
85 strcpy(buf, "AdjustTokenPrivileges set ");
86 bstrncat(buf, name, sizeof(buf));
87 win_error(jcr, buf, lerror);
95 * Setup privileges we think we will need. We probably do not need
96 * the SE_SECURITY_NAME, but since nothing seems to be working,
97 * we get it hoping to fix the problems.
99 int enable_backup_privileges(JCR *jcr, int ignore_errors)
101 HANDLE hToken, hProcess;
104 if (!p_OpenProcessToken) {
105 return 0; /* No avail on this OS */
108 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
110 // Get a token for this process.
111 if (!p_OpenProcessToken(hProcess,
112 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
113 if (!ignore_errors) {
114 win_error(jcr, "OpenProcessToken", GetLastError());
116 /* Forge on anyway */
119 /* Return a bit map of permissions set. */
120 if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
123 if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
126 if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
129 if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
132 if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
135 if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
138 if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
141 if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
144 if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
152 CloseHandle(hProcess);
156 #endif /* HAVE_CYGWIN */