]> gitweb.factorcode.org Git - factor.git/blob - basis/unix/users/users.factor
Merge branch 'master' into experimental (untested!)
[factor.git] / basis / unix / users / users.factor
1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien alien.c-types alien.strings io.encodings.utf8
4 io.unix.backend kernel math sequences splitting unix strings
5 combinators.short-circuit grouping byte-arrays combinators
6 accessors math.parser fry assocs namespaces continuations
7 vocabs.loader system ;
8 IN: unix.users
9
10 TUPLE: passwd username password uid gid gecos dir shell ;
11
12 HOOK: new-passwd os ( -- passwd )
13 HOOK: passwd>new-passwd os ( passwd -- new-passwd )
14
15 <PRIVATE
16
17 M: unix new-passwd ( -- passwd )
18     passwd new ;
19
20 M: unix passwd>new-passwd ( passwd -- seq )
21     [ new-passwd ] dip
22     {
23         [ passwd-pw_name >>username ]
24         [ passwd-pw_passwd >>password ]
25         [ passwd-pw_uid >>uid ]
26         [ passwd-pw_gid >>gid ]
27         [ passwd-pw_gecos >>gecos ]
28         [ passwd-pw_dir >>dir ]
29         [ passwd-pw_shell >>shell ]
30     } cleave ;
31
32 : with-pwent ( quot -- )
33     [ endpwent ] [ ] cleanup ; inline
34
35 PRIVATE>
36
37 : all-users ( -- seq )
38     [
39         [ getpwent dup ] [ passwd>new-passwd ] [ drop ] produce
40     ] with-pwent ;
41
42 SYMBOL: user-cache
43
44 : <user-cache> ( -- assoc )
45     all-users [ [ uid>> ] keep ] H{ } map>assoc ;
46
47 : with-user-cache ( quot -- )
48     [ <user-cache> user-cache ] dip with-variable ; inline
49
50 GENERIC: user-passwd ( obj -- passwd )
51
52 M: integer user-passwd ( id -- passwd/f )
53     user-cache get
54     [ at ] [ getpwuid passwd>new-passwd ] if* ;
55
56 M: string user-passwd ( string -- passwd/f )
57     getpwnam dup [ passwd>new-passwd ] when ;
58
59 : username ( id -- string )
60     user-passwd username>> ;
61
62 : user-id ( string -- id )
63     user-passwd uid>> ;
64
65 : real-user-id ( -- id )
66     getuid ; inline
67
68 : real-username ( -- string )
69     real-user-id username ; inline
70
71 : effective-user-id ( -- id )
72     geteuid ; inline
73
74 : effective-username ( -- string )
75     effective-user-id username ; inline
76
77 GENERIC: set-real-user ( string/id -- )
78
79 GENERIC: set-effective-user ( string/id -- )
80
81 : with-real-user ( string/id quot -- )
82     '[ _ set-real-user @ ]
83     real-user-id '[ _ set-real-user ]
84     [ ] cleanup ; inline
85
86 : with-effective-user ( string/id quot -- )
87     '[ _ set-effective-user @ ]
88     effective-user-id '[ _ set-effective-user ]
89     [ ] cleanup ; inline
90
91 <PRIVATE
92
93 : (set-real-user) ( id -- )
94     setuid io-error ; inline
95
96 : (set-effective-user) ( id -- )
97     seteuid io-error ; inline
98
99 PRIVATE>
100
101 M: string set-real-user ( string -- )
102     user-id (set-real-user) ;
103
104 M: integer set-real-user ( id -- )
105     (set-real-user) ;
106
107 M: integer set-effective-user ( id -- )
108     (set-effective-user) ; 
109
110 M: string set-effective-user ( string -- )
111     user-id (set-effective-user) ;
112
113 os {
114     { [ dup bsd? ] [ drop "unix.users.bsd" require ] }
115     { [ dup linux? ] [ drop ] }
116 } cond