]> gitweb.factorcode.org Git - factor.git/blob - basis/io/backend/windows/nt/privileges/privileges.factor
Updating code to use with-out-parameters
[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 }
17     { PHANDLE }
18     [ OpenProcessToken win32-error=0/f ] [ ]
19     with-out-parameters ;
20
21 : open-process-token ( -- handle )
22     #! remember to CloseHandle
23     GetCurrentProcess (open-process-token) ;
24
25 : with-process-token ( quot -- )
26     #! quot: ( token-handle -- token-handle )
27     [ open-process-token ] dip
28     [ keep ] curry
29     [ CloseHandle drop ] [ ] cleanup ; inline
30
31 : lookup-privilege ( string -- luid )
32     [ f ] dip LUID <struct>
33     [ LookupPrivilegeValue win32-error=0/f ] keep ;
34
35 :: make-token-privileges ( name enabled? -- obj )
36     TOKEN_PRIVILEGES <struct>
37         1 >>PrivilegeCount
38         LUID_AND_ATTRIBUTES malloc-struct &free
39             enabled? [ SE_PRIVILEGE_ENABLED >>Attributes ] when
40             name lookup-privilege >>Luid
41         >>Privileges ;
42
43 M: winnt set-privilege ( name ? -- )
44     '[
45         0
46         _ _ make-token-privileges
47         dup byte-length
48         f
49         f
50         AdjustTokenPrivileges win32-error=0/f
51     ] with-process-token ;