2 * Enable backup privileges for Win32 systems.
4 * Kern Sibbald, May MMIII
10 Copyright (C) 2003-2004 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_WIN32)
42 int enable_backup_privileges(JCR *jcr, int ignore_errors)
50 /*=============================================================*/
52 /* * * * W i n 3 2 * * * * */
54 /*=============================================================*/
56 #if 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)) {
72 win_error(jcr, "LookupPrivilegeValue", GetLastError());
76 /* Set the security privilege for this process. */
77 tkp.PrivilegeCount = 1;
78 tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
79 p_AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);
80 lerror = GetLastError();
81 if (lerror != ERROR_SUCCESS) {
84 strcpy(buf, _("AdjustTokenPrivileges set "));
85 bstrncat(buf, name, sizeof(buf));
86 win_error(jcr, buf, lerror);
94 * Setup privileges we think we will need. We probably do not need
95 * the SE_SECURITY_NAME, but since nothing seems to be working,
96 * we get it hoping to fix the problems.
98 int enable_backup_privileges(JCR *jcr, int ignore_errors)
100 HANDLE hToken, hProcess;
103 if (!p_OpenProcessToken) {
104 return 0; /* No avail on this OS */
107 hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
109 // Get a token for this process.
110 if (!p_OpenProcessToken(hProcess,
111 TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
112 if (!ignore_errors) {
113 win_error(jcr, "OpenProcessToken", GetLastError());
115 /* Forge on anyway */
118 /* Return a bit map of permissions set. */
119 if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
122 if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
125 if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
128 if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
131 if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
134 if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
137 if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
140 if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
143 if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
146 if (enable_priv(jcr, hToken, SE_CREATE_PERMANENT_NAME, ignore_errors)) {
154 CloseHandle(hProcess);
158 #endif /* HAVE_WIN32 */