summaryrefslogtreecommitdiffstats
path: root/.vim/bundle/SudoEdit/autoload/sudo.cmd
blob: 8f919cbd50b15c2d75ea2c7799e8eed44e2ae98c (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
@echo off
cls

setlocal DisableDelayedExpansion
set "batchPath=%~0"
setlocal EnableDelayedExpansion

:: File to write to
set dummy=%1
set mode=%2
set myfile=%3
set newcontent=%4
set sudo=%5
shift
shift
shift
shift
shift

if '%sudo%' == 'uac' goto checkPrivileges
if '%dummy%' == 'ELEV' goto gotPrivileges

:: Use runas or something alike to elevate priviliges, but
:: first parse parameter for runas
:: Windows cmd.exe is very clumsy to use ;(
set params=%1
:loop
shift
if [%1]==[] goto afterloop
set params=%params% %1
goto loop

:afterloop

:: Use runas or so to elevate rights
echo.
echo ***************************************
echo Calling %sudo% for Privilege Escalation
echo ***************************************
if '%mode%' == 'write' (
    %sudo% %params% "cmd.exe /c type %newcontent% >%myfile%"
    ) else (
    %sudo% %params% "cmd.exe /c type %myfile% >%newcontent%"
    )
goto end

:: Use UAC to elevate rights, idea taken from:
:: http://stackoverflow.com/questions/7044985/how-can-i-auto-elevate-my-batch-file-so-that-it-requests-from-uac-admin-rights
:checkPrivileges
::NET FILE 1>NUL 2>NUL
set vbs="%temp%\GetPrivileges.vbs"

:: Check if we already have system priviliges
>NUL 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
if '%errorlevel%' EQU '0' (goto gotPrivileges) else (goto getPrivileges)

:getPrivileges
echo.
echo **************************************
echo Invoking UAC for Privilege Escalation 
echo **************************************

echo Set UAC = CreateObject^("Shell.Application"^) > %vbs%
echo UAC.ShellExecute "!batchPath!", "ELEV !mode! "!myfile!" "!newcontent!""   , "", "runas", 1 >> %vbs%
:: Run VBS script
%vbs%
exit /B 

:gotPrivileges
::setlocal & pushd .
:: Doesn't work?
if exist %vbs% (del %vbs%)
pushd "%CD%"
cd /d "%~dp0"

if '%mode%' == 'write' (
    cmd.exe /c type %newcontent% > %myfile%
) else (
    cmd.exe /c type %myfile% > %newcontent%
)

if '%errorlevel%' NEQ 0 echo "An error occured"

:end