1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien alien.c-types alien.strings assocs
4 byte-arrays classes.struct combinators
5 combinators.short-circuit continuations fry io.backend.unix
6 io.encodings.utf8 kernel math math.parser namespaces sequences
7 splitting strings unix unix.ffi unix.users unix.utilities ;
12 TUPLE: group id name passwd members ;
16 GENERIC: group-struct ( obj -- group/f )
20 : group-members ( group-struct -- seq )
21 gr_mem>> utf8 alien>strings ;
23 : (group-struct) ( id -- group-struct id group-struct byte-array length void* )
24 [ \ unix.ffi:group <struct> ] dip over 4096
25 [ <byte-array> ] keep f void* <ref> ;
27 : check-group-struct ( group-struct ptr -- group-struct/f )
28 void* deref [ drop f ] unless ;
30 M: integer group-struct ( id -- group/f )
32 [ [ unix.ffi:getgrgid_r ] unix-system-call drop ] keep
35 M: string group-struct ( string -- group/f )
37 [ [ unix.ffi:getgrnam_r ] unix-system-call drop ] keep
40 : group-struct>group ( group-struct -- group )
44 [ gr_passwd>> >>passwd ]
46 [ group-members >>members ]
51 : group-name ( id -- string )
53 ?at [ name>> ] [ number>string ] if
55 group-struct [ gr_name>> ] [ f ] if*
57 [ nip ] [ number>string ] if* ;
59 : group-id ( string -- id/f )
60 group-struct dup [ gr_gid>> ] when ;
62 ERROR: no-group string ;
64 : ?group-id ( string -- id )
65 dup group-struct [ nip gr_gid>> ] [ no-group ] if* ;
69 : >groups ( byte-array n -- groups )
70 [ 4 grouping:group ] dip head-slice [ uint deref group-name ] map ;
72 : (user-groups) ( string -- seq )
73 #! first group is -1337, legacy unix code
74 -1337 unix.ffi:NGROUPS_MAX [ 4 * <byte-array> ] keep
75 int <ref> [ [ unix.ffi:getgrouplist ] unix-system-call drop ] 2keep
76 [ 4 tail-slice ] [ int deref 1 - ] bi* >groups ;
80 GENERIC: user-groups ( string/id -- seq )
82 M: string user-groups ( string -- seq )
85 M: integer user-groups ( id -- seq )
86 user-name (user-groups) ;
88 : all-groups ( -- seq )
89 [ unix.ffi:getgrent dup ] [ group-struct>group ] produce nip
92 : all-group-names ( -- seq )
93 all-groups [ name>> ] map ;
95 : <group-cache> ( -- assoc )
96 all-groups [ [ id>> ] keep ] H{ } map>assoc ;
98 : with-group-cache ( quot -- )
99 [ <group-cache> group-cache ] dip with-variable ; inline
101 : real-group-id ( -- id ) unix.ffi:getgid ; inline
103 : real-group-name ( -- string ) real-group-id group-name ; inline
105 : effective-group-id ( -- string ) unix.ffi:getegid ; inline
107 : effective-group-name ( -- string )
108 effective-group-id group-name ; inline
110 : group-exists? ( name/id -- ? ) group-id >boolean ;
112 GENERIC: set-real-group ( obj -- )
114 GENERIC: set-effective-group ( obj -- )
116 : (with-real-group) ( string/id quot -- )
117 '[ _ set-real-group @ ]
118 real-group-id '[ _ set-real-group ] [ ] cleanup ; inline
120 : with-real-group ( string/id/f quot -- )
121 over [ (with-real-group) ] [ nip call ] if ; inline
123 : (with-effective-group) ( string/id quot -- )
124 '[ _ set-effective-group @ ]
125 effective-group-id '[ _ set-effective-group ] [ ] cleanup ; inline
127 : with-effective-group ( string/id/f quot -- )
128 over [ (with-effective-group) ] [ nip call ] if ; inline
132 : (set-real-group) ( id -- )
133 [ unix.ffi:setgid ] unix-system-call drop ; inline
135 : (set-effective-group) ( id -- )
136 [ unix.ffi:setegid ] unix-system-call drop ; inline
140 M: integer set-real-group ( id -- )
143 M: string set-real-group ( string -- )
144 ?group-id (set-real-group) ;
146 M: integer set-effective-group ( id -- )
147 (set-effective-group) ;
149 M: string set-effective-group ( string -- )
150 ?group-id (set-effective-group) ;