]> gitweb.factorcode.org Git - factor.git/blob - basis/io/backend/windows/nt/privileges/privileges.factor
bb075233bd462f3ff8cfdf3492d053a8ad005769
[factor.git] / basis / io / backend / windows / nt / privileges / privileges.factor
1 USING: alien alien.c-types alien.data alien.syntax arrays continuations
2 destructors generic io.mmap io.ports io.backend.windows io.files.windows
3 kernel libc math math.bitwise namespaces quotations sequences windows
4 windows.advapi32 windows.kernel32 windows.types io.backend system accessors
5 io.backend.windows.privileges classes.struct windows.errors ;
6 IN: io.backend.windows.nt.privileges
7
8 TYPEDEF: TOKEN_PRIVILEGES* PTOKEN_PRIVILEGES
9
10 ! Security tokens
11 !  http://msdn.microsoft.com/msdnmag/issues/05/03/TokenPrivileges/
12
13 : (open-process-token) ( handle -- handle )
14     { TOKEN_ADJUST_PRIVILEGES TOKEN_QUERY } flags PHANDLE <c-object>
15     [ OpenProcessToken win32-error=0/f ] keep *void* ;
16
17 : open-process-token ( -- handle )
18     #! remember to CloseHandle
19     GetCurrentProcess (open-process-token) ;
20
21 : with-process-token ( quot -- )
22     #! quot: ( token-handle -- token-handle )
23     [ open-process-token ] dip
24     [ keep ] curry
25     [ CloseHandle drop ] [ ] cleanup ; inline
26
27 : lookup-privilege ( string -- luid )
28     [ f ] dip LUID <struct>
29     [ LookupPrivilegeValue win32-error=0/f ] keep ;
30
31 : make-token-privileges ( name ? -- obj )
32     TOKEN_PRIVILEGES <struct>
33         1 >>PrivilegeCount
34         LUID_AND_ATTRIBUTES malloc-struct &free
35             swap [ SE_PRIVILEGE_ENABLED >>Attributes ] when
36         >>Privileges
37     [ lookup-privilege ] dip
38     [ Privileges>> (>>Luid) ] keep ;
39
40 M: winnt set-privilege ( name ? -- )
41     [
42         -rot 0 -rot make-token-privileges
43         dup length f f AdjustTokenPrivileges win32-error=0/f
44     ] with-process-token ;