| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							|  |  |  | USING: alien alien.c-types alien.strings io.encodings.utf8 | 
					
						
							| 
									
										
										
										
											2008-12-14 21:03:00 -05:00
										 |  |  | io.backend.unix kernel math sequences splitting unix strings | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | combinators.short-circuit grouping byte-arrays combinators | 
					
						
							|  |  |  | accessors math.parser fry assocs namespaces continuations | 
					
						
							|  |  |  | vocabs.loader system ;
 | 
					
						
							|  |  |  | IN: unix.users | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  | TUPLE: passwd user-name password uid gid gecos dir shell ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | HOOK: new-passwd os ( -- passwd )
 | 
					
						
							|  |  |  | HOOK: passwd>new-passwd os ( passwd -- new-passwd )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix new-passwd ( -- passwd )
 | 
					
						
							|  |  |  |     passwd new ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | M: unix passwd>new-passwd ( passwd -- seq )
 | 
					
						
							|  |  |  |     [ new-passwd ] dip
 | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  |         [ passwd-pw_name >>user-name ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |         [ passwd-pw_passwd >>password ] | 
					
						
							|  |  |  |         [ passwd-pw_uid >>uid ] | 
					
						
							|  |  |  |         [ passwd-pw_gid >>gid ] | 
					
						
							|  |  |  |         [ passwd-pw_gecos >>gecos ] | 
					
						
							|  |  |  |         [ passwd-pw_dir >>dir ] | 
					
						
							|  |  |  |         [ passwd-pw_shell >>shell ] | 
					
						
							|  |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-pwent ( quot -- )
 | 
					
						
							|  |  |  |     [ endpwent ] [ ] cleanup ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-users ( -- seq )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2009-02-28 16:31:34 -05:00
										 |  |  |         [ getpwent dup ] [ passwd>new-passwd ] produce nip
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     ] with-pwent ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 14:05:04 -05:00
										 |  |  | SYMBOL: user-cache | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-18 12:45:55 -05:00
										 |  |  | : <user-cache> ( -- assoc )
 | 
					
						
							|  |  |  |     all-users [ [ uid>> ] keep ] H{ } map>assoc ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-11 14:05:04 -05:00
										 |  |  | : with-user-cache ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-11-18 12:45:55 -05:00
										 |  |  |     [ <user-cache> user-cache ] dip with-variable ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-04 12:44:49 -05:00
										 |  |  | GENERIC: user-passwd ( obj -- passwd/f )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: integer user-passwd ( id -- passwd/f )
 | 
					
						
							| 
									
										
										
										
											2008-11-11 14:05:04 -05:00
										 |  |  |     user-cache get
 | 
					
						
							| 
									
										
										
										
											2009-01-04 12:44:49 -05:00
										 |  |  |     [ at ] [ getpwuid [ passwd>new-passwd ] [ f ] if* ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: string user-passwd ( string -- passwd/f )
 | 
					
						
							|  |  |  |     getpwnam dup [ passwd>new-passwd ] when ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  | : user-name ( id -- string )
 | 
					
						
							| 
									
										
										
										
											2009-01-04 12:44:49 -05:00
										 |  |  |     dup user-passwd | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  |     [ nip user-name>> ] [ number>string ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : user-id ( string -- id )
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:13:25 -04:00
										 |  |  |     user-passwd uid>> ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : real-user-id ( -- id )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     getuid ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  | : real-user-name ( -- string )
 | 
					
						
							|  |  |  |     real-user-id user-name ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : effective-user-id ( -- id )
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:13:25 -04:00
										 |  |  |     geteuid ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-07 15:53:43 -05:00
										 |  |  | : effective-user-name ( -- string )
 | 
					
						
							|  |  |  |     effective-user-id user-name ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | GENERIC: set-real-user ( string/id -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | GENERIC: set-effective-user ( string/id -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : with-real-user ( string/id quot -- )
 | 
					
						
							|  |  |  |     '[ _ set-real-user @ ] | 
					
						
							|  |  |  |     real-user-id '[ _ set-real-user ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     [ ] cleanup ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : with-effective-user ( string/id quot -- )
 | 
					
						
							|  |  |  |     '[ _ set-effective-user @ ] | 
					
						
							|  |  |  |     effective-user-id '[ _ set-effective-user ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     [ ] cleanup ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : (set-real-user) ( id -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     setuid io-error ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : (set-effective-user) ( id -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     seteuid io-error ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | M: string set-real-user ( string -- )
 | 
					
						
							|  |  |  |     user-id (set-real-user) ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | M: integer set-real-user ( id -- )
 | 
					
						
							|  |  |  |     (set-real-user) ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | M: integer set-effective-user ( id -- )
 | 
					
						
							|  |  |  |     (set-effective-user) ;  | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | M: string set-effective-user ( string -- )
 | 
					
						
							|  |  |  |     user-id (set-effective-user) ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | os { | 
					
						
							|  |  |  |     { [ dup bsd? ] [ drop "unix.users.bsd" require ] } | 
					
						
							|  |  |  |     { [ dup linux? ] [ drop ] } | 
					
						
							|  |  |  | } cond
 |