1 USING: accessors arrays assocs command-line environment
2 formatting fry io.launcher kernel ui locals math namespaces
3 sequences splitting strings system unix.ffi unix.process ;
7 ERROR: elevated-failed path ;
8 ERROR: lowered-failed ;
10 CONSTANT: apple-script-charmap H{
18 : quote-apple-script ( str -- str' )
19 [ 1string [ apple-script-charmap at ] [ ] bi or ] { } map-as
20 "" join "\"" dup surround ;
22 : run-apple-script ( str -- ) drop ;
24 : apple-script-elevated ( command -- )
26 "do shell script %s with administrator privileges without altering line endings"
27 sprintf run-apple-script ;
29 : posix-replace-process ( command-list -- code )
30 [ first ] [ rest ] bi exec-with-path ;
32 GENERIC: glue-command ( prefix command -- glued )
37 M: string glue-command
40 GENERIC: failed-process? ( process -- ? )
41 M: f failed-process? not ;
42 M: fixnum failed-process? -1 = ;
43 M: process failed-process? status>> zero? not ;
47 HOOK: elevated os ( command replace? win-console? posix-graphical? -- process )
54 M:: macosx elevated ( command replace? win-console? posix-graphical? -- process )
55 posix-graphical? [ ! graphical (through applescript)
56 command apple-script-elevated
58 command replace? win-console? posix-graphical?
59 linux os [ elevated ] with-variable ;
61 M:: linux elevated ( command replace? win-console? posix-graphical? -- process )
63 <process> command >>command ! we are already root: just give a process
66 posix-graphical? ui-running? or "DISPLAY" os-env and
67 { "gksudo" "kdesudo" "sudo" } { "sudo" } ?
69 command '[ _ glue-command ] map [
71 " " split posix-replace-process
74 ! if they all failed, then it failed, but if one passed, that's normal (success)
75 [ [ failed-process? ] all? [ command elevated-failed ] [ ] if ] keep
78 : elevate ( win-console? posix-graphical? -- ) [ (command-line) t ] 2dip elevated drop ;
80 HOOK: lowered os ( -- )
82 ! https://wiki.sei.cmu.edu/confluence/display/c/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges
83 ! group ID must be lowered before user ID otherwise program may re-gain root!
85 getgid setgid failed-process? [ lowered-failed ] [ ] if
86 getuid setuid failed-process? [ lowered-failed ] [ ] if ;
89 linux os [ lowered ] with-variable ;