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 command { strategies array } ;
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 : already-root? ( -- ? )
33 getuid geteuid [ zero? ] bi@ or ;
35 GENERIC: glue-command ( prefix command -- glued )
40 M: string glue-command
43 GENERIC: failed-process? ( process -- ? )
44 M: f failed-process? not ;
45 M: fixnum failed-process? -1 = ;
46 M: process failed-process? status>> zero? not ;
48 : posix-lowered ( -- )
49 getgid setgid failed-process? [ lowered-failed ] [ ] if
50 getuid setuid failed-process? [ lowered-failed ] [ ] if ;
54 HOOK: elevated os ( command replace? win-console? posix-graphical? -- process )
61 M:: macosx elevated ( command replace? win-console? posix-graphical? -- process )
62 already-root? [ <process> command >>command 1array ] [
63 posix-graphical? [ ! graphical (through applescript)
64 command apple-script-elevated
66 command replace? win-console? posix-graphical?
67 linux os [ elevated ] with-variable
70 M:: linux elevated ( command replace? win-console? posix-graphical? -- process )
72 <process> command >>command 1array ! we are already root: just give a process
75 posix-graphical? ui-running? or "DISPLAY" os-env and
76 { "gksudo" "kdesudo" "sudo" } { "sudo" } ?
78 command '[ _ glue-command ] map :> command-list command-list [
80 " " split posix-replace-process
83 ! if they all failed, then it failed, but if one passed, that's normal (success)
84 [ [ failed-process? ] all? [ command command-list elevated-failed ] [ ] if ] keep
87 : elevate ( win-console? posix-graphical? -- ) [ (command-line) t ] 2dip elevated drop ;
89 HOOK: lowered os ( -- )
91 ! https://wiki.sei.cmu.edu/confluence/display/c/POS36-C.+Observe+correct+revocation+order+while+relinquishing+privileges
92 ! group ID must be lowered before user ID otherwise program may re-gain root!