5 echo "Missing arguments" >&2
8 elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
10 elif [ "$1" = "--setup" ]; then
23 if [ "$1" = "--as" ]; then
25 echo "--as user not specified" >&2
28 elif [ -n "$asuser" ]; then
29 echo "--as user specified twice" >&2
37 elif [ -z "$command" ]; then
40 elif [ "$1" = "--" ]; then
48 if [ -z "$command" ]; then
49 echo "Command not specified" >&2
53 cmdfullpath="`which -- $command`"
54 if [ -z "$cmdfullpath" ]; then
55 echo "Command not found: $command" >&2
59 if [ -z "$asuser" ]; then
60 basename="`basename $cmdfullpath`"
61 asuser="$USER-$basename"
64 otherhome="`grep ^$asuser: /etc/passwd | head -n1 | cut -d: -f6`"
65 if [ -z "$otherhome" ]; then
66 echo "Could not find home of $asuser" >&2
70 authfile="$otherhome/.Xauthority"
74 xauth -f $authfile generate $DISPLAY . trusted
75 chgrp $asuser $authfile
80 # Closes IO handlers to avoid tty manipulation
81 # See: https://github.com/garotosopa/xodo/issues/1
83 XAUTHORITY="$authfile" exec perl -e "
86 use POSIX qw(setsid uname);
87 my (\$sysname) = uname();
88 close STDIN; close STDOUT; close STDERR;
91 if (\$sysname eq \"OpenBSD\") {
92 exec 'doas', '-u', \$ARGV[0], \$ARGV[1], @ARGV[2 .. @ARGV-1];
94 exec 'sudo', '-u', \$ARGV[0], \$ARGV[1], (defined \$ARGV[2] ? @ARGV[2 .. @ARGV-1] : ());
96 " -- "$asuser" "$cmdfullpath" "$@"
101 echo "Command not specified" >&2
106 command="`which -- $1`"
108 if [ -z "$command" ]; then
109 echo "Command not found: $command" >&2
113 basename="`basename $command`"
117 while [ -n "$1" ]; do
118 if [ "$1" = "--as" ]; then
120 echo "--as user not specified" >&2
123 elif [ -n "$asuser" ]; then
124 echo "--as user specified twice" >&2
132 elif [ "$1" = "--for" ]; then
134 echo "--for user not specified" >&2
137 elif [ -n "$foruser" ]; then
138 echo "--for user specified twice" >&2
147 echo "Invalid argument: $1" >&2
153 foruser="${foruser-$USER}"
154 asuser="${asuser-$foruser-$basename}"
158 setup_function=setup_linux
159 priv_file=/etc/sudoers.d/xodo
162 setup_function=setup_openbsd
163 priv_file=/etc/doas.conf
166 echo "Unsupported platform: `uname`" >&2
171 echo "The following steps will be executed:"
172 echo " - add user $asuser if it doesn't exist;"
173 echo " - make $asuser's home readable and writable by its group;"
174 echo " - add group $asuser to user $foruser;"
175 echo " - add an entry to $priv_file"
176 echo " allowing existing user $foruser"
177 echo " to execute $command"
178 echo " as new user $asuser with no password."
180 echo -n "Proceed? [y/n] "
182 if [ "$proceed" != "y" ] && [ "$proceed" != "Y" ]; then
189 $setup_function "$command" "$asuser" "$foruser"
192 echo "If user $foruser is logged in, it must log out and in again before using xodo as $asuser, so that it gets added to the new group."
200 if [ -z "`grep "^$asuser:" /etc/passwd`" ]; then
201 useradd --create-home --skel /dev/null --user-group --key UMASK=002 "$asuser"
204 usermod -aG "$asuser" "$foruser"
205 echo "$foruser ALL = ($asuser) NOPASSWD: $command" >> /etc/sudoers.d/xodo
213 if [ -z "`grep "^$asuser:" /etc/passwd`" ]; then
214 useradd -m -k "" "$asuser"
217 usermod -G "$asuser" "$foruser"
219 otherhome="`grep "^$asuser:" /etc/passwd | head -n1 | cut -d: -f6`"
220 if [ -z "$otherhome" ]; then
221 echo "Could not find home of $asuser" >&2
225 chmod g+rwX "$otherhome"
227 echo "\npermit nopass setenv { DISPLAY HOME=$otherhome USER=$asuser } $foruser as $asuser cmd $command" >> /etc/doas.conf
231 echo "usage: xodo <command> [--as <user>] [--] [args...]"
232 echo " xodo --setup <command> [--as <user>] [--for <user>]"