Setting the Backup and Restore Privileges

topic: 

The backup and restore privileges are required of all backup and restore applications. These privileges can be programmatically set, and the following example can be used to set these privileges. Based on Setting the Backup and Restore Privileges

$SAMPLECODE$

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49


#DEFINE TOKEN_ADJUST_PRIVILEGES 0x0020
#DEFINE TOKEN_QUERY 0x0008
#DEFINE SE_PRIVILEGE_ENABLED 0x0002

#DEFINE SE_RESTORE_NAME "SeRestorePrivilege"
#DEFINE SE_BACKUP_NAME "SeBackupPrivilege"

DECLARE INTEGER GetLastError IN kernel32

DECLARE Long GetCurrentProcess IN WIN32API
DECLARE LONG OpenProcessToken IN Advapi32.dll Long ProcessHandle, Long DesiredAccess, long @ TokenHandle 
DECLARE LONG LookupPrivilegeValue IN Advapi32.dll String lpSystemName, String lpName, String @lpLuid

DECLARE LONG AdjustTokenPrivileges IN Advapi32.dll long TokenHandle, Long DisableAllPrivileges, ;
	String @ NewState, Long BufferSize, Long PreviousState, Long ReturnLength

DECLARE LONG CloseHandle IN WIN32API Long Handle 

lcPrivilege2set = SE_BACKUP_NAME

lnToken  = 0
llOK =  (OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, @lnToken ) > 0)
IF NOT llOK 
	? "Failed OpenProcessToken"
	RETURN
ENDIF

lcLuid = REPLICATE(CHR(0), 8)
llOK = (LookupPrivilegeValue("", lcPrivilege2set, @lcLuid) > 0)
IF llOk
	lcTokenPrivileges = BINTOC(1, "RS") + lcLuid + BINTOC(SE_PRIVILEGE_ENABLED, "RS")

	llOK =  (AdjustTokenPrivileges(lnToken, 0, lcTokenPrivileges, 0,0,0) > 0)
	IF NOT llOk
		? "Failed AdjustTokenPrivileges, code " + TRANSFORM(GetLastError())
	ENDIF
ELSE
	? "Failed LookupPrivilegeValue"	
ENDIF

= CloseHandle(lnToken)

IF llOk
	? "Privilege " + lcPrivilege2set  + " set" 
ENDIF	

Comments