4 76920906 2020-09-07 xhr if [ -z "$1" ]; then
5 97695aca 2020-06-29 xhr echo "Missing arguments" >&2
6 97695aca 2020-06-29 xhr display_usage >&2
8 76920906 2020-09-07 xhr elif [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
10 76920906 2020-09-07 xhr elif [ "$1" = "--setup" ]; then
22 76920906 2020-09-07 xhr while [ -n "$1" ]; do
23 76920906 2020-09-07 xhr if [ "$1" = "--as" ]; then
24 76920906 2020-09-07 xhr if [ -z "$2" ]; then
25 76920906 2020-09-07 xhr echo "--as user not specified" >&2
26 76920906 2020-09-07 xhr display_usage >&2
28 76920906 2020-09-07 xhr elif [ -n "$asuser" ]; then
29 76920906 2020-09-07 xhr echo "--as user specified twice" >&2
30 76920906 2020-09-07 xhr display_usage >&2
37 76920906 2020-09-07 xhr elif [ -z "$command" ]; then
40 76920906 2020-09-07 xhr elif [ "$1" = "--" ]; then
48 76920906 2020-09-07 xhr if [ -z "$command" ]; then
49 76920906 2020-09-07 xhr echo "Command not specified" >&2
53 76920906 2020-09-07 xhr cmdfullpath="`which -- $command`"
54 76920906 2020-09-07 xhr if [ -z "$cmdfullpath" ]; then
55 76920906 2020-09-07 xhr echo "Command not found: $command" >&2
59 76920906 2020-09-07 xhr if [ -z "$asuser" ]; then
60 76920906 2020-09-07 xhr basename="`basename $cmdfullpath`"
61 76920906 2020-09-07 xhr asuser="$USER-$basename"
64 76920906 2020-09-07 xhr otherhome="`grep ^$asuser: /etc/passwd | head -n1 | cut -d: -f6`"
65 76920906 2020-09-07 xhr if [ -z "$otherhome" ]; then
66 76920906 2020-09-07 xhr echo "Could not find home of $asuser" >&2
70 76920906 2020-09-07 xhr authfile="$otherhome/.Xauthority"
73 97695aca 2020-06-29 xhr touch $authfile
74 97695aca 2020-06-29 xhr xauth -f $authfile generate $DISPLAY . trusted
75 97695aca 2020-06-29 xhr chgrp $asuser $authfile
76 97695aca 2020-06-29 xhr chmod g+r $authfile
78 97695aca 2020-06-29 xhr cd $otherhome
80 76920906 2020-09-07 xhr # Closes IO handlers to avoid tty manipulation
81 76920906 2020-09-07 xhr # See: https://github.com/garotosopa/xodo/issues/1
83 76920906 2020-09-07 xhr XAUTHORITY="$authfile" exec perl -e "
85 76920906 2020-09-07 xhr use warnings;
86 76920906 2020-09-07 xhr use POSIX qw(setsid uname);
87 76920906 2020-09-07 xhr my (\$sysname) = uname();
88 76920906 2020-09-07 xhr close STDIN; close STDOUT; close STDERR;
89 76920906 2020-09-07 xhr fork and exit;
91 76920906 2020-09-07 xhr if (\$sysname eq \"OpenBSD\") {
92 76920906 2020-09-07 xhr exec 'doas', '-u', \$ARGV[0], \$ARGV[1], @ARGV[2 .. @ARGV-1];
94 76920906 2020-09-07 xhr exec 'sudo', '-u', \$ARGV[0], \$ARGV[1], (defined \$ARGV[2] ? @ARGV[2 .. @ARGV-1] : ());
96 76920906 2020-09-07 xhr " -- "$asuser" "$cmdfullpath" "$@"
100 76920906 2020-09-07 xhr if [ -z "$1" ]; then
101 97695aca 2020-06-29 xhr echo "Command not specified" >&2
102 97695aca 2020-06-29 xhr display_usage >&2
106 76920906 2020-09-07 xhr command="`which -- $1`"
108 76920906 2020-09-07 xhr if [ -z "$command" ]; then
109 76920906 2020-09-07 xhr echo "Command not found: $command" >&2
113 76920906 2020-09-07 xhr basename="`basename $command`"
117 76920906 2020-09-07 xhr while [ -n "$1" ]; do
118 76920906 2020-09-07 xhr if [ "$1" = "--as" ]; then
119 76920906 2020-09-07 xhr if [ -z "$2" ]; then
120 97695aca 2020-06-29 xhr echo "--as user not specified" >&2
121 97695aca 2020-06-29 xhr display_usage >&2
123 76920906 2020-09-07 xhr elif [ -n "$asuser" ]; then
124 97695aca 2020-06-29 xhr echo "--as user specified twice" >&2
125 97695aca 2020-06-29 xhr display_usage >&2
132 76920906 2020-09-07 xhr elif [ "$1" = "--for" ]; then
133 76920906 2020-09-07 xhr if [ -z "$2" ]; then
134 97695aca 2020-06-29 xhr echo "--for user not specified" >&2
135 97695aca 2020-06-29 xhr display_usage >&2
137 76920906 2020-09-07 xhr elif [ -n "$foruser" ]; then
138 97695aca 2020-06-29 xhr echo "--for user specified twice" >&2
139 97695aca 2020-06-29 xhr display_usage >&2
143 76920906 2020-09-07 xhr foruser="$2"
147 97695aca 2020-06-29 xhr echo "Invalid argument: $1" >&2
148 97695aca 2020-06-29 xhr display_usage
153 76920906 2020-09-07 xhr foruser="${foruser-$USER}"
154 76920906 2020-09-07 xhr asuser="${asuser-$foruser-$basename}"
156 76920906 2020-09-07 xhr case "`uname`" in
158 76920906 2020-09-07 xhr setup_function=setup_linux
159 76920906 2020-09-07 xhr priv_file=/etc/sudoers.d/xodo
162 76920906 2020-09-07 xhr setup_function=setup_openbsd
163 76920906 2020-09-07 xhr priv_file=/etc/doas.conf
166 76920906 2020-09-07 xhr echo "Unsupported platform: `uname`" >&2
171 76920906 2020-09-07 xhr echo "The following steps will be executed:"
172 76920906 2020-09-07 xhr echo " - add user $asuser if it doesn't exist;"
173 76920906 2020-09-07 xhr echo " - make $asuser's home readable and writable by its group;"
174 76920906 2020-09-07 xhr echo " - add group $asuser to user $foruser;"
175 76920906 2020-09-07 xhr echo " - add an entry to $priv_file"
176 76920906 2020-09-07 xhr echo " allowing existing user $foruser"
177 76920906 2020-09-07 xhr echo " to execute $command"
178 76920906 2020-09-07 xhr echo " as new user $asuser with no password."
180 76920906 2020-09-07 xhr echo -n "Proceed? [y/n] "
181 76920906 2020-09-07 xhr read proceed
182 76920906 2020-09-07 xhr if [ "$proceed" != "y" ] && [ "$proceed" != "Y" ]; then
183 76920906 2020-09-07 xhr echo "Aborted."
189 76920906 2020-09-07 xhr $setup_function "$command" "$asuser" "$foruser"
191 76920906 2020-09-07 xhr echo "Done."
192 76920906 2020-09-07 xhr 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."
195 76920906 2020-09-07 xhr setup_linux() {
196 76920906 2020-09-07 xhr command="$1"
198 76920906 2020-09-07 xhr foruser="$3"
200 76920906 2020-09-07 xhr if [ -z "`grep "^$asuser:" /etc/passwd`" ]; then
201 76920906 2020-09-07 xhr useradd --create-home --skel /dev/null --user-group --key UMASK=002 "$asuser"
204 76920906 2020-09-07 xhr usermod -aG "$asuser" "$foruser"
205 76920906 2020-09-07 xhr echo "$foruser ALL = ($asuser) NOPASSWD: $command" >> /etc/sudoers.d/xodo
208 76920906 2020-09-07 xhr setup_openbsd() {
209 76920906 2020-09-07 xhr command="$1"
211 76920906 2020-09-07 xhr foruser="$3"
213 76920906 2020-09-07 xhr if [ -z "`grep "^$asuser:" /etc/passwd`" ]; then
214 76920906 2020-09-07 xhr useradd -m -k "" "$asuser"
217 76920906 2020-09-07 xhr usermod -G "$asuser" "$foruser"
219 76920906 2020-09-07 xhr otherhome="`grep "^$asuser:" /etc/passwd | head -n1 | cut -d: -f6`"
220 76920906 2020-09-07 xhr if [ -z "$otherhome" ]; then
221 76920906 2020-09-07 xhr echo "Could not find home of $asuser" >&2
225 76920906 2020-09-07 xhr chmod g+rwX "$otherhome"
227 76920906 2020-09-07 xhr echo "\npermit nopass setenv { DISPLAY HOME=$otherhome USER=$asuser } $foruser as $asuser cmd $command" >> /etc/doas.conf
230 97695aca 2020-06-29 xhr display_usage() {
231 76920906 2020-09-07 xhr echo "usage: xodo <command> [--as <user>] [--] [args...]"
232 97695aca 2020-06-29 xhr echo " xodo --setup <command> [--as <user>] [--for <user>]"
233 97695aca 2020-06-29 xhr echo " xodo --help"