| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | ! Copyright (C) 2008 Doug Coleman. | 
					
						
							|  |  |  | ! See http://factorcode.org/license.txt for BSD license. | 
					
						
							| 
									
										
										
										
											2010-06-23 11:27:17 -04:00
										 |  |  | USING: accessors alien alien.c-types alien.strings assocs | 
					
						
							| 
									
										
										
										
											2011-09-02 01:28:38 -04:00
										 |  |  | byte-arrays classes.struct combinators combinators.short-circuit | 
					
						
							|  |  |  | continuations fry grouping io.encodings.utf8 kernel math | 
					
						
							|  |  |  | math.parser namespaces sequences splitting strings system unix | 
					
						
							| 
									
										
										
										
											2011-11-02 14:23:41 -04:00
										 |  |  | unix.ffi vocabs ;
 | 
					
						
							| 
									
										
										
										
											2010-01-20 23:42:07 -05:00
										 |  |  | QUALIFIED: unix.ffi | 
					
						
							| 
									
										
										
										
											2010-06-23 11:27:17 -04:00
										 |  |  | IN: unix.users | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-08-31 00:07:46 -04:00
										 |  |  |         [ pw_name>> >>user-name ] | 
					
						
							|  |  |  |         [ pw_passwd>> >>password ] | 
					
						
							|  |  |  |         [ pw_uid>> >>uid ] | 
					
						
							|  |  |  |         [ pw_gid>> >>gid ] | 
					
						
							|  |  |  |         [ pw_gecos>> >>gecos ] | 
					
						
							|  |  |  |         [ pw_dir>> >>dir ] | 
					
						
							|  |  |  |         [ pw_shell>> >>shell ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     } cleave ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : with-pwent ( quot -- )
 | 
					
						
							| 
									
										
										
										
											2010-06-23 11:25:08 -04:00
										 |  |  |     setpwent | 
					
						
							| 
									
										
										
										
											2010-01-20 23:42:07 -05:00
										 |  |  |     [ unix.ffi:endpwent ] [ ] cleanup ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : all-users ( -- seq )
 | 
					
						
							|  |  |  |     [ | 
					
						
							| 
									
										
										
										
											2010-02-22 15:22:29 -05:00
										 |  |  |         [ unix.ffi:getpwent dup ] [ passwd>new-passwd ] produce nip
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     ] with-pwent ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 13:07:56 -04:00
										 |  |  | : all-user-names ( -- seq )
 | 
					
						
							|  |  |  |     all-users [ user-name>> ] map ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:22:29 -05:00
										 |  |  |     [ at ] [ unix.ffi:getpwuid [ passwd>new-passwd ] [ f ] if* ] if* ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | M: string user-passwd ( string -- passwd/f )
 | 
					
						
							| 
									
										
										
										
											2010-02-22 15:22:29 -05:00
										 |  |  |     unix.ffi:getpwnam dup [ passwd>new-passwd ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-02-04 22:50:33 -05:00
										 |  |  | : user-id ( string -- id/f )
 | 
					
						
							|  |  |  |     user-passwd dup [ uid>> ] when ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 13:59:19 -04:00
										 |  |  | ERROR: no-user string ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : ?user-id ( string -- id/f )
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     dup user-passwd [ nip uid>> ] [ no-user ] if* ;
 | 
					
						
							| 
									
										
										
										
											2010-06-17 13:59:19 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : real-user-id ( -- id )
 | 
					
						
							| 
									
										
										
										
											2010-01-20 23:42:07 -05:00
										 |  |  |     unix.ffi:getuid ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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 )
 | 
					
						
							| 
									
										
										
										
											2010-01-20 23:42:07 -05:00
										 |  |  |     unix.ffi: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-21 13:07:56 -04:00
										 |  |  | : user-exists? ( name/id -- ? ) user-id >boolean ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 14:20:51 -04:00
										 |  |  | : (with-real-user) ( string/id quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  |     '[ _ set-real-user @ ] | 
					
						
							|  |  |  |     real-user-id '[ _ set-real-user ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     [ ] cleanup ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 14:20:51 -04:00
										 |  |  | : with-real-user ( string/id/f quot -- )
 | 
					
						
							|  |  |  |     over [ (with-real-user) ] [ nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : (with-effective-user) ( string/id quot -- )
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  |     '[ _ set-effective-user @ ] | 
					
						
							|  |  |  |     effective-user-id '[ _ set-effective-user ] | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  |     [ ] cleanup ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 14:20:51 -04:00
										 |  |  | : with-effective-user ( string/id/f quot -- )
 | 
					
						
							|  |  |  |     over [ (with-effective-user) ] [ nip call ] if ; inline
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | <PRIVATE
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : (set-real-user) ( id -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-23 10:07:35 -05:00
										 |  |  |     [ unix.ffi:setuid ] unix-system-call drop ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | : (set-effective-user) ( id -- )
 | 
					
						
							| 
									
										
										
										
											2010-01-23 10:07:35 -05:00
										 |  |  |     [ unix.ffi:seteuid ] unix-system-call drop ; inline
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | PRIVATE>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 13:59:19 -04:00
										 |  |  | M: string set-real-user ( string -- )
 | 
					
						
							|  |  |  |     ?user-id (set-real-user) ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-10-08 14:22:53 -04:00
										 |  |  | M: integer set-effective-user ( id -- )
 | 
					
						
							| 
									
										
										
										
											2015-06-29 19:43:15 -04:00
										 |  |  |     (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 -- )
 | 
					
						
							| 
									
										
										
										
											2010-06-17 13:59:19 -04:00
										 |  |  |     ?user-id (set-effective-user) ;
 | 
					
						
							| 
									
										
										
										
											2008-10-07 20:25:05 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-19 12:27:19 -05:00
										 |  |  | ERROR: no-such-user obj ;
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | : user-home ( name/uid -- path )
 | 
					
						
							| 
									
										
										
										
											2015-08-13 19:13:05 -04:00
										 |  |  |     dup user-passwd [ nip dir>> ] [ no-such-user ] if* ;
 | 
					
						
							| 
									
										
										
										
											2013-11-19 12:27:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-15 17:25:12 -05:00
										 |  |  | os macosx? [ "unix.users.macosx" require ] when
 |