]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/enable_priv.c
34539eb8ebd57312c72fc525b4f830abc27ea3bf
[bacula/bacula] / bacula / src / findlib / enable_priv.c
1 /*
2  *  Enable backup privileges for Win32 systems.
3  *
4  *    Kern Sibbald, May MMIII
5  *
6  *   Version $Id$
7  *
8  */
9 /*
10    Copyright (C) 2003-2004 Kern Sibbald and John Walker
11
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.
16
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.
21
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,
25    MA 02111-1307, USA.
26
27  */
28
29 #include "bacula.h"
30 #include "find.h"
31 #include "jcr.h"
32
33
34 /*=============================================================*/
35 /*                                                             */
36 /*                 * * *  U n i x * * * *                      */
37 /*                                                             */
38 /*=============================================================*/
39
40 #if !defined(HAVE_WIN32)
41
42 int enable_backup_privileges(JCR *jcr, int ignore_errors)
43  { return 0; }
44
45
46 #endif
47
48
49
50 /*=============================================================*/
51 /*                                                             */
52 /*                 * * *  W i n 3 2 * * * *                    */
53 /*                                                             */
54 /*=============================================================*/
55
56 #if defined(HAVE_WIN32)
57
58 void win_error(JCR *jcr, char *prefix, DWORD lerror);
59
60 static int
61 enable_priv(JCR *jcr, HANDLE hToken, char *name, int ignore_errors)
62 {
63     TOKEN_PRIVILEGES tkp;
64     DWORD lerror;
65
66     if (!(p_LookupPrivilegeValue && p_AdjustTokenPrivileges)) {
67        return 0;                      /* not avail on this OS */
68     }
69
70     // Get the LUID for the security privilege.
71     if (!p_LookupPrivilegeValue(NULL, name,  &tkp.Privileges[0].Luid)) {
72        win_error(jcr, "LookupPrivilegeValue", GetLastError());
73        return 0;
74     }
75
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) {
82        if (!ignore_errors) {
83           char buf[200];
84           strcpy(buf, _("AdjustTokenPrivileges set "));
85           bstrncat(buf, name, sizeof(buf));
86           win_error(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  */
98 int enable_backup_privileges(JCR *jcr, int ignore_errors)
99 {
100     HANDLE hToken, hProcess;
101     int stat = 0;
102
103     if (!p_OpenProcessToken) {
104        return 0;                      /* No avail on this OS */
105     }
106
107     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
108
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());
114        }
115        /* Forge on anyway */
116     }
117
118     /* Return a bit map of permissions set. */
119     if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
120        stat |= 1<<0;
121     }
122     if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
123        stat |= 1<<1;
124     }
125     if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
126        stat |= 1<<2;
127     }
128     if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
129        stat |= 1<<3;
130     }
131     if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
132        stat |= 1<<4;
133     }
134     if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
135        stat |= 1<<5;
136     }
137     if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
138        stat |= 1<<6;
139     }
140     if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
141        stat |= 1<<7;
142     }
143     if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
144        stat |= 1<<8;
145     }
146     if (enable_priv(jcr, hToken, SE_CREATE_PERMANENT_NAME, ignore_errors)) {
147        stat |= 1<<10;
148     }
149     if (stat) {
150        stat |= 1<<9;
151     }
152
153     CloseHandle(hToken);
154     CloseHandle(hProcess);
155     return stat;
156 }
157
158 #endif  /* HAVE_WIN32 */