1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
4 USING: accessors alien.c-types alien.data alien.utilities assocs
5 byte-arrays classes.struct combinators continuations grouping
6 io.encodings.utf8 kernel math math.parser namespaces sequences
7 strings unix unix.ffi unix.users ;
11 TUPLE: group id name passwd members ;
15 GENERIC: group-struct ( obj -- group/f )
19 : group-members ( group-struct -- seq )
20 gr_mem>> utf8 alien>strings ;
22 : (group-struct) ( id -- group-struct id group-struct byte-array length void* )
23 [ unix.ffi:group new ] dip over 4096
24 [ <byte-array> ] keep f void* <ref> ;
26 : check-group-struct ( group-struct ptr -- group-struct/f )
27 void* deref [ drop f ] unless ;
29 M: integer group-struct
31 [ [ unix.ffi:getgrgid_r ] unix-system-call drop ] keep
34 M: string group-struct
36 [ [ unix.ffi:getgrnam_r ] unix-system-call drop ] keep
39 : group-struct>group ( group-struct -- group )
43 [ gr_passwd>> >>passwd ]
45 [ group-members >>members ]
50 : group-name ( id -- string )
52 ?at [ name>> ] [ number>string ] if
54 group-struct [ gr_name>> ] [ f ] if*
56 [ ] [ number>string ] ?if ;
58 : group-id ( string -- id/f )
59 group-struct dup [ gr_gid>> ] when ;
61 ERROR: no-group string ;
63 : ?group-id ( string -- id )
64 dup group-struct [ nip gr_gid>> ] [ no-group ] if* ;
68 : >groups ( byte-array n -- groups )
69 [ 4 grouping:group ] dip head-slice [ uint deref group-name ] map ;
71 : (user-groups) ( string -- seq )
73 gid>> 64 [ 4 * <byte-array> ] keep
74 int <ref> [ [ unix.ffi:getgrouplist ] unix-system-call drop ] 2keep
82 GENERIC: user-groups ( string/id -- seq )
87 M: integer user-groups
88 user-name (user-groups) ;
90 : all-groups ( -- seq )
91 [ unix.ffi:getgrent dup ] [ group-struct>group ] produce nip
94 : all-group-names ( -- seq )
95 all-groups [ name>> ] map ;
97 : <group-cache> ( -- assoc )
98 all-groups [ [ id>> ] keep ] H{ } map>assoc ;
100 : with-group-cache ( quot -- )
101 [ <group-cache> group-cache ] dip with-variable ; inline
103 : real-group-id ( -- id ) unix.ffi:getgid ; inline
105 : real-group-name ( -- string ) real-group-id group-name ; inline
107 : effective-group-id ( -- string ) unix.ffi:getegid ; inline
109 : effective-group-name ( -- string )
110 effective-group-id group-name ; inline
112 : group-exists? ( name/id -- ? ) group-id >boolean ;
114 GENERIC: set-real-group ( obj -- )
116 GENERIC: set-effective-group ( obj -- )
118 : (with-real-group) ( string/id quot -- )
119 '[ _ set-real-group @ ]
120 real-group-id '[ _ set-real-group ] finally ; inline
122 : with-real-group ( string/id/f quot -- )
123 over [ (with-real-group) ] [ nip call ] if ; inline
125 : (with-effective-group) ( string/id quot -- )
126 '[ _ set-effective-group @ ]
127 effective-group-id '[ _ set-effective-group ] finally ; inline
129 : with-effective-group ( string/id/f quot -- )
130 over [ (with-effective-group) ] [ nip call ] if ; inline
134 : (set-real-group) ( id -- )
135 [ unix.ffi:setgid ] unix-system-call drop ; inline
137 : (set-effective-group) ( id -- )
138 [ unix.ffi:setegid ] unix-system-call drop ; inline
142 M: integer set-real-group
145 M: string set-real-group
146 ?group-id (set-real-group) ;
148 M: integer set-effective-group
149 (set-effective-group) ;
151 M: string set-effective-group
152 ?group-id (set-effective-group) ;