1 /*
2    BAREOS® - Backup Archiving REcovery Open Sourced
3 
4    Copyright (C) 2003-2009 Free Software Foundation Europe e.V.
5    Copyright (C) 2016-2018 Bareos GmbH & Co. KG
6 
7    This program is Free Software; you can redistribute it and/or
8    modify it under the terms of version three of the GNU Affero General Public
9    License as published by the Free Software Foundation and included
10    in the file LICENSE.
11 
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15    Affero General Public License for more details.
16 
17    You should have received a copy of the GNU Affero General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20    02110-1301, USA.
21 */
22 /*
23  * Kern Sibbald, May MMIII
24  */
25 /**
26  * @file
27  * Enable backup privileges for Win32 systems.
28  */
29 
30 #include "include/bareos.h"
31 #include "find.h"
32 #include "include/jcr.h"
33 #include "findlib/enable_priv.h"
34 
35 /*=============================================================*/
36 /*                                                             */
37 /*                 * * *  U n i x * * * *                      */
38 /*                                                             */
39 /*=============================================================*/
40 
41 #if !defined(HAVE_WIN32)
EnableBackupPrivileges(JobControlRecord * jcr,int ignore_errors)42 int EnableBackupPrivileges(JobControlRecord* jcr, int ignore_errors)
43 {
44   return 0;
45 }
46 #endif
47 
48 /*=============================================================*/
49 /*                                                             */
50 /*                 * * *  W i n 3 2 * * * *                    */
51 /*                                                             */
52 /*=============================================================*/
53 
54 #if defined(HAVE_WIN32)
55 void WinError(JobControlRecord* jcr, const char* prefix, DWORD lerror);
56 
enable_priv(JobControlRecord * jcr,HANDLE hToken,const char * name,int ignore_errors)57 static int enable_priv(JobControlRecord* jcr,
58                        HANDLE hToken,
59                        const char* name,
60                        int ignore_errors)
61 {
62   TOKEN_PRIVILEGES tkp;
63   DWORD lerror;
64 
65   if (!(p_LookupPrivilegeValue && p_AdjustTokenPrivileges)) {
66     return 0; /* not avail on this OS */
67   }
68 
69   // Get the LUID for the security privilege.
70   if (!p_LookupPrivilegeValue(NULL, name, &tkp.Privileges[0].Luid)) {
71     WinError(jcr, "LookupPrivilegeValue", GetLastError());
72     return 0;
73   }
74 
75   /* Set the security privilege for this process. */
76   tkp.PrivilegeCount = 1;
77   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
78   p_AdjustTokenPrivileges(hToken, FALSE, &tkp, sizeof(TOKEN_PRIVILEGES), NULL,
79                           NULL);
80   lerror = GetLastError();
81   if (lerror != ERROR_SUCCESS) {
82     if (!ignore_errors) {
83       char buf[200];
84       strcpy(buf, _("AdjustTokenPrivileges set "));
85       bstrncat(buf, name, sizeof(buf));
86       WinError(jcr, buf, lerror);
87     }
88     return 0;
89   }
90   return 1;
91 }
92 
93 /**
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.
97  */
EnableBackupPrivileges(JobControlRecord * jcr,int ignore_errors)98 int EnableBackupPrivileges(JobControlRecord* jcr, int ignore_errors)
99 {
100   HANDLE hToken, hProcess;
101   int status = 0;
102 
103   if (!p_OpenProcessToken) { return 0; /* No avail on this OS */ }
104 
105   hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
106 
107   // Get a token for this process.
108   if (!p_OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
109                           &hToken)) {
110     if (!ignore_errors) { WinError(jcr, "OpenProcessToken", GetLastError()); }
111     /* Forge on anyway */
112   }
113 
114   /* Return a bit map of permissions set. */
115   if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
116     status |= 1 << 1;
117   }
118   if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
119     status |= 1 << 2;
120   }
121   if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
122     status |= 1 << 0;
123   }
124   if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
125     status |= 1 << 3;
126   }
127   if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
128     status |= 1 << 4;
129   }
130   if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
131     status |= 1 << 5;
132   }
133   if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
134     status |= 1 << 6;
135   }
136   if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
137     status |= 1 << 7;
138   }
139   if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
140     status |= 1 << 8;
141   }
142   if (enable_priv(jcr, hToken, SE_CREATE_PERMANENT_NAME, ignore_errors)) {
143     status |= 1 << 10;
144   }
145 
146   if (status) { status |= 1 << 9; }
147 
148   CloseHandle(hToken);
149   CloseHandle(hProcess);
150   return status;
151 }
152 
153 #endif /* HAVE_WIN32 */
154