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