]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/enable_priv.c
b13a9ea9518cafec6968ec9c927ef839e249c767
[bacula/bacula] / bacula / src / findlib / enable_priv.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2003-2009 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of Kern Sibbald.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  *  Enable backup privileges for Win32 systems.
30  *
31  *    Kern Sibbald, May MMIII
32  *
33  *   Version $Id$
34  *
35  */
36
37 #include "bacula.h"
38 #include "find.h"
39 #include "jcr.h"
40
41
42 /*=============================================================*/
43 /*                                                             */
44 /*                 * * *  U n i x * * * *                      */
45 /*                                                             */
46 /*=============================================================*/
47
48 #if !defined(HAVE_WIN32)
49
50 int enable_backup_privileges(JCR *jcr, int ignore_errors)
51  { return 0; }
52
53
54 #endif
55
56
57
58 /*=============================================================*/
59 /*                                                             */
60 /*                 * * *  W i n 3 2 * * * *                    */
61 /*                                                             */
62 /*=============================================================*/
63
64 #if defined(HAVE_WIN32)
65
66 void win_error(JCR *jcr, const char *prefix, DWORD lerror);
67
68 static int
69 enable_priv(JCR *jcr, HANDLE hToken, const char *name, int ignore_errors)
70 {
71     TOKEN_PRIVILEGES tkp;
72     DWORD lerror;
73
74     if (!(p_LookupPrivilegeValue && p_AdjustTokenPrivileges)) {
75        return 0;                      /* not avail on this OS */
76     }
77
78     // Get the LUID for the security privilege.
79     if (!p_LookupPrivilegeValue(NULL, name,  &tkp.Privileges[0].Luid)) {
80        win_error(jcr, "LookupPrivilegeValue", GetLastError());
81        return 0;
82     }
83
84     /* Set the security privilege for this process. */
85     tkp.PrivilegeCount = 1;
86     tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
87     p_AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, NULL, NULL);
88     lerror = GetLastError();
89     if (lerror != ERROR_SUCCESS) {
90        if (!ignore_errors) {
91           char buf[200];
92           strcpy(buf, _("AdjustTokenPrivileges set "));
93           bstrncat(buf, name, sizeof(buf));
94           win_error(jcr, buf, lerror);
95        }
96        return 0;
97     }
98     return 1;
99 }
100
101 /*
102  * Setup privileges we think we will need.  We probably do not need
103  *  the SE_SECURITY_NAME, but since nothing seems to be working,
104  *  we get it hoping to fix the problems.
105  */
106 int enable_backup_privileges(JCR *jcr, int ignore_errors)
107 {
108     HANDLE hToken, hProcess;
109     int stat = 0;
110
111     if (!p_OpenProcessToken) {
112        return 0;                      /* No avail on this OS */
113     }
114
115     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
116
117     // Get a token for this process.
118     if (!p_OpenProcessToken(hProcess,
119             TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) {
120        if (!ignore_errors) {
121           win_error(jcr, "OpenProcessToken", GetLastError());
122        }
123        /* Forge on anyway */
124     }
125
126     /* Return a bit map of permissions set. */
127     if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
128        stat |= 1<<1;
129     }
130     if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
131        stat |= 1<<2;
132     }
133 #if 0
134     if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
135        stat |= 1<<0;
136     }
137     if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
138        stat |= 1<<3;
139     }
140     if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
141        stat |= 1<<4;
142     }
143     if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
144        stat |= 1<<5;
145     }
146     if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
147        stat |= 1<<6;
148     }
149     if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
150        stat |= 1<<7;
151     }
152     if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
153        stat |= 1<<8;
154     }
155     if (enable_priv(jcr, hToken, SE_CREATE_PERMANENT_NAME, ignore_errors)) {
156        stat |= 1<<10;
157     }
158 #endif
159     if (stat) {
160        stat |= 1<<9;
161     }
162
163     CloseHandle(hToken);
164     CloseHandle(hProcess);
165     return stat;
166 }
167
168 #endif  /* HAVE_WIN32 */