]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/findlib/enable_priv.c
Add bsock timers to authentication
[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) 2000-2003 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 #ifndef HAVE_CYGWIN
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 #ifdef HAVE_CYGWIN
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        if (!ignore_errors) {  
73           win_error(jcr, "LookupPrivilegeValue", GetLastError());
74        }
75     }
76
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) {
83        if (!ignore_errors) {
84           char buf[200];
85           strcpy(buf, "AdjustTokenPrivileges set ");
86           bstrncat(buf, name, sizeof(buf));
87           win_error(jcr, buf, lerror);
88        }
89        return 0;
90     } 
91     return 1;
92 }
93
94 /*
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.
98  */
99 int enable_backup_privileges(JCR *jcr, int ignore_errors)
100 {
101     HANDLE hToken, hProcess;
102     int stat = 0;
103
104     if (!p_OpenProcessToken) {
105        return 0;                      /* No avail on this OS */
106     }
107
108     hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, GetCurrentProcessId());
109
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());
115        }
116        /* Forge on anyway */
117     } 
118
119     /* Return a bit map of permissions set. */
120     if (enable_priv(jcr, hToken, SE_SECURITY_NAME, ignore_errors)) {
121        stat |= 1<<0;
122     }
123     if (enable_priv(jcr, hToken, SE_BACKUP_NAME, ignore_errors)) {
124        stat |= 1<<1;
125     }
126     if (enable_priv(jcr, hToken, SE_RESTORE_NAME, ignore_errors)) {
127        stat |= 1<<2;
128     }
129     if (enable_priv(jcr, hToken, SE_TAKE_OWNERSHIP_NAME, ignore_errors)) {
130        stat |= 1<<3;
131     }
132     if (enable_priv(jcr, hToken, SE_ASSIGNPRIMARYTOKEN_NAME, ignore_errors)) {
133        stat |= 1<<4;
134     }
135     if (enable_priv(jcr, hToken, SE_SYSTEM_ENVIRONMENT_NAME, ignore_errors)) {
136        stat |= 1<<5;
137     }
138     if (enable_priv(jcr, hToken, SE_CREATE_TOKEN_NAME, ignore_errors)) {
139        stat |= 1<<6;
140     }
141     if (enable_priv(jcr, hToken, SE_MACHINE_ACCOUNT_NAME, ignore_errors)) {
142        stat |= 1<<7;
143     }
144     if (enable_priv(jcr, hToken, SE_TCB_NAME, ignore_errors)) {
145        stat |= 1<<8;
146     }
147     if (stat) {
148        stat |= 1<<9;
149     }
150
151     CloseHandle(hToken);
152     CloseHandle(hProcess);
153     return stat;
154 }
155
156 #endif  /* HAVE_CYGWIN */