OLD | NEW |
(Empty) | |
| 1 #!/bin/sh |
| 2 #--------------------------------------------- |
| 3 # xdg-su |
| 4 # |
| 5 # Utility script to run a command as an alternate user, generally |
| 6 # the root user, with a graphical prompt for the root |
| 7 # password if needed |
| 8 # |
| 9 # Refer to the usage() function below for usage. |
| 10 # |
| 11 # Copyright 2006, Jeremy White <jwhite@codeweavers.com> |
| 12 # Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at> |
| 13 # |
| 14 # LICENSE: |
| 15 # |
| 16 # Permission is hereby granted, free of charge, to any person obtaining a |
| 17 # copy of this software and associated documentation files (the "Software"), |
| 18 # to deal in the Software without restriction, including without limitation |
| 19 # the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 20 # and/or sell copies of the Software, and to permit persons to whom the |
| 21 # Software is furnished to do so, subject to the following conditions: |
| 22 # |
| 23 # The above copyright notice and this permission notice shall be included |
| 24 # in all copies or substantial portions of the Software. |
| 25 # |
| 26 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 27 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 28 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 29 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR |
| 30 # OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 31 # ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 32 # OTHER DEALINGS IN THE SOFTWARE. |
| 33 # |
| 34 #--------------------------------------------- |
| 35 |
| 36 manualpage() |
| 37 { |
| 38 cat << _MANUALPAGE |
| 39 Name |
| 40 |
| 41 xdg-su - run a GUI program as root after prompting for the root password |
| 42 |
| 43 Synopsis |
| 44 |
| 45 xdg-su [-u user] -c command |
| 46 |
| 47 xdg-su { --help | --manual | --version } |
| 48 |
| 49 Description |
| 50 |
| 51 xdg-su provides a graphical dialog that prompts the user for a password to run |
| 52 command as user or as root if no user was specified. |
| 53 |
| 54 xdg-su is for use inside a desktop session only. |
| 55 |
| 56 xdg-su discards any stdout and stderr output from command. |
| 57 |
| 58 Options |
| 59 |
| 60 -u user |
| 61 run command as user. The default is to run as root. |
| 62 --help |
| 63 Show command synopsis. |
| 64 --manual |
| 65 Show this manualpage. |
| 66 --version |
| 67 Show the xdg-utils version information. |
| 68 |
| 69 Exit Codes |
| 70 |
| 71 An exit code of 0 indicates success while a non-zero exit code indicates |
| 72 failure. The following failure codes can be returned: |
| 73 |
| 74 1 |
| 75 Error in command line syntax. |
| 76 2 |
| 77 One of the files passed on the command line did not exist. |
| 78 3 |
| 79 A required tool could not be found. |
| 80 4 |
| 81 The action failed. |
| 82 |
| 83 See Also |
| 84 |
| 85 su(1) |
| 86 |
| 87 Examples |
| 88 |
| 89 xdg-su -u root -c "/opt/shinythings/bin/install-GUI --install fast" |
| 90 |
| 91 Runs the /opt/shinythings/bin/install-GUI command with root permissions. |
| 92 |
| 93 _MANUALPAGE |
| 94 } |
| 95 |
| 96 usage() |
| 97 { |
| 98 cat << _USAGE |
| 99 xdg-su - run a GUI program as root after prompting for the root password |
| 100 |
| 101 Synopsis |
| 102 |
| 103 xdg-su [-u user] -c command |
| 104 |
| 105 xdg-su { --help | --manual | --version } |
| 106 |
| 107 _USAGE |
| 108 } |
| 109 |
| 110 #@xdg-utils-common@ |
| 111 |
| 112 #---------------------------------------------------------------------------- |
| 113 # Common utility functions included in all XDG wrapper scripts |
| 114 #---------------------------------------------------------------------------- |
| 115 |
| 116 DEBUG() |
| 117 { |
| 118 [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt $1 ] && return 0; |
| 119 shift |
| 120 echo "$@" >&2 |
| 121 } |
| 122 |
| 123 #------------------------------------------------------------- |
| 124 # Exit script on successfully completing the desired operation |
| 125 |
| 126 exit_success() |
| 127 { |
| 128 if [ $# -gt 0 ]; then |
| 129 echo "$@" |
| 130 echo |
| 131 fi |
| 132 |
| 133 exit 0 |
| 134 } |
| 135 |
| 136 |
| 137 #----------------------------------------- |
| 138 # Exit script on malformed arguments, not enough arguments |
| 139 # or missing required option. |
| 140 # prints usage information |
| 141 |
| 142 exit_failure_syntax() |
| 143 { |
| 144 if [ $# -gt 0 ]; then |
| 145 echo "xdg-su: $@" >&2 |
| 146 echo "Try 'xdg-su --help' for more information." >&2 |
| 147 else |
| 148 usage |
| 149 echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info." |
| 150 fi |
| 151 |
| 152 exit 1 |
| 153 } |
| 154 |
| 155 #------------------------------------------------------------- |
| 156 # Exit script on missing file specified on command line |
| 157 |
| 158 exit_failure_file_missing() |
| 159 { |
| 160 if [ $# -gt 0 ]; then |
| 161 echo "xdg-su: $@" >&2 |
| 162 fi |
| 163 |
| 164 exit 2 |
| 165 } |
| 166 |
| 167 #------------------------------------------------------------- |
| 168 # Exit script on failure to locate necessary tool applications |
| 169 |
| 170 exit_failure_operation_impossible() |
| 171 { |
| 172 if [ $# -gt 0 ]; then |
| 173 echo "xdg-su: $@" >&2 |
| 174 fi |
| 175 |
| 176 exit 3 |
| 177 } |
| 178 |
| 179 #------------------------------------------------------------- |
| 180 # Exit script on failure returned by a tool application |
| 181 |
| 182 exit_failure_operation_failed() |
| 183 { |
| 184 if [ $# -gt 0 ]; then |
| 185 echo "xdg-su: $@" >&2 |
| 186 fi |
| 187 |
| 188 exit 4 |
| 189 } |
| 190 |
| 191 #------------------------------------------------------------ |
| 192 # Exit script on insufficient permission to read a specified file |
| 193 |
| 194 exit_failure_file_permission_read() |
| 195 { |
| 196 if [ $# -gt 0 ]; then |
| 197 echo "xdg-su: $@" >&2 |
| 198 fi |
| 199 |
| 200 exit 5 |
| 201 } |
| 202 |
| 203 #------------------------------------------------------------ |
| 204 # Exit script on insufficient permission to read a specified file |
| 205 |
| 206 exit_failure_file_permission_write() |
| 207 { |
| 208 if [ $# -gt 0 ]; then |
| 209 echo "xdg-su: $@" >&2 |
| 210 fi |
| 211 |
| 212 exit 6 |
| 213 } |
| 214 |
| 215 check_input_file() |
| 216 { |
| 217 if [ ! -e "$1" ]; then |
| 218 exit_failure_file_missing "file '$1' does not exist" |
| 219 fi |
| 220 if [ ! -r "$1" ]; then |
| 221 exit_failure_file_permission_read "no permission to read file '$1'" |
| 222 fi |
| 223 } |
| 224 |
| 225 check_vendor_prefix() |
| 226 { |
| 227 file=`basename "$1"` |
| 228 case "$file" in |
| 229 [a-zA-Z]*-*) |
| 230 return |
| 231 ;; |
| 232 esac |
| 233 |
| 234 echo "xdg-su: filename '$file' does not have a proper vendor prefix" >&2 |
| 235 echo 'A vendor prefix consists of alpha characters ([a-zA-Z]) and is termina
ted' >&2 |
| 236 echo 'with a dash ("-"). An example filename is '"'example-$file'" >&2 |
| 237 echo "Use --novendor to override or 'xdg-su --manual' for additional info."
>&2 |
| 238 exit 1 |
| 239 } |
| 240 |
| 241 check_output_file() |
| 242 { |
| 243 # if the file exists, check if it is writeable |
| 244 # if it does not exists, check if we are allowed to write on the directory |
| 245 if [ -e "$1" ]; then |
| 246 if [ ! -w "$1" ]; then |
| 247 exit_failure_file_permission_write "no permission to write to file '
$1'" |
| 248 fi |
| 249 else |
| 250 DIR=`dirname "$1"` |
| 251 if [ ! -w "$DIR" -o ! -x "$DIR" ]; then |
| 252 exit_failure_file_permission_write "no permission to create file '$1
'" |
| 253 fi |
| 254 fi |
| 255 } |
| 256 |
| 257 #---------------------------------------- |
| 258 # Checks for shared commands, e.g. --help |
| 259 |
| 260 check_common_commands() |
| 261 { |
| 262 while [ $# -gt 0 ] ; do |
| 263 parm="$1" |
| 264 shift |
| 265 |
| 266 case "$parm" in |
| 267 --help) |
| 268 usage |
| 269 echo "Use 'man xdg-su' or 'xdg-su --manual' for additional info." |
| 270 exit_success |
| 271 ;; |
| 272 |
| 273 --manual) |
| 274 manualpage |
| 275 exit_success |
| 276 ;; |
| 277 |
| 278 --version) |
| 279 echo "xdg-su 1.0beta2" |
| 280 exit_success |
| 281 ;; |
| 282 esac |
| 283 done |
| 284 } |
| 285 |
| 286 check_common_commands "$@" |
| 287 if [ ${XDG_UTILS_DEBUG_LEVEL-0} -lt 1 ]; then |
| 288 # Be silent |
| 289 xdg_redirect_output=" > /dev/null 2> /dev/null" |
| 290 else |
| 291 # All output to stderr |
| 292 xdg_redirect_output=" >&2" |
| 293 fi |
| 294 |
| 295 #-------------------------------------- |
| 296 # Checks for known desktop environments |
| 297 # set variable DE to the desktop environments name, lowercase |
| 298 |
| 299 detectDE() |
| 300 { |
| 301 if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde; |
| 302 elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome; |
| 303 elif xprop -root _DT_SAVE_MODE | grep ' = \"xfce4\"$' >/dev/null 2>&1; then
DE=xfce; |
| 304 fi |
| 305 } |
| 306 |
| 307 #---------------------------------------------------------------------------- |
| 308 |
| 309 |
| 310 |
| 311 su_kde() |
| 312 { |
| 313 if [ x"$KDE_SESSION_VERSION" = x"4" ]; then |
| 314 KDESU=`kde4-config --locate kdesu --path exe 2>/dev/null` |
| 315 else |
| 316 KDESU=`which kdesu 2>/dev/null` |
| 317 fi |
| 318 if [ $? -eq 0 ] ; then |
| 319 if [ -z "$user" ] ; then |
| 320 $KDESU -c "$cmd" |
| 321 else |
| 322 $KDESU -u "$user" -c "$cmd" |
| 323 fi |
| 324 |
| 325 if [ $? -eq 0 ]; then |
| 326 exit_success |
| 327 else |
| 328 exit_failure_operation_failed |
| 329 fi |
| 330 else |
| 331 su_generic |
| 332 fi |
| 333 } |
| 334 |
| 335 su_gnome() |
| 336 { |
| 337 GSU=`which gnomesu 2>/dev/null` |
| 338 if [ $? -ne 0 ] ; then |
| 339 GSU=`which xsu 2>/dev/null` |
| 340 fi |
| 341 if [ $? -eq 0 ] ; then |
| 342 if [ -z "$user" ] ; then |
| 343 $GSU -c "$cmd" |
| 344 else |
| 345 $GSU -u "$user" -c "$cmd" |
| 346 fi |
| 347 |
| 348 if [ $? -eq 0 ]; then |
| 349 exit_success |
| 350 else |
| 351 exit_failure_operation_failed |
| 352 fi |
| 353 else |
| 354 su_generic |
| 355 fi |
| 356 } |
| 357 |
| 358 su_generic() |
| 359 { |
| 360 if [ -z "$user" ] ; then |
| 361 xterm -geom 60x5 -T "xdg-su: $cmd" -e su -c "$cmd" |
| 362 else |
| 363 xterm -geom 60x5 -T "xdg-su: $cmd" -e su -u "$user" -c "$cmd" |
| 364 fi |
| 365 |
| 366 if [ $? -eq 0 ]; then |
| 367 exit_success |
| 368 else |
| 369 exit_failure_operation_failed |
| 370 fi |
| 371 } |
| 372 |
| 373 [ x"$1" != x"" ] || exit_failure_syntax |
| 374 |
| 375 user= |
| 376 cmd= |
| 377 while [ $# -gt 0 ] ; do |
| 378 parm="$1" |
| 379 shift |
| 380 |
| 381 case "$parm" in |
| 382 -u) |
| 383 if [ -z "$1" ] ; then |
| 384 exit_failure_syntax "user argument missing for -u" |
| 385 fi |
| 386 user="$1" |
| 387 shift |
| 388 ;; |
| 389 |
| 390 -c) |
| 391 if [ -z "$1" ] ; then |
| 392 exit_failure_syntax "command argument missing for -c" |
| 393 fi |
| 394 cmd="$1" |
| 395 shift |
| 396 ;; |
| 397 |
| 398 -*) |
| 399 exit_failure_syntax "unexpected option '$parm'" |
| 400 ;; |
| 401 |
| 402 *) |
| 403 exit_failure_syntax "unexpected argument '$parm'" |
| 404 ;; |
| 405 esac |
| 406 done |
| 407 |
| 408 if [ -z "${cmd}" ] ; then |
| 409 exit_failure_syntax "command missing" |
| 410 fi |
| 411 |
| 412 detectDE |
| 413 |
| 414 if [ x"$DE" = x"" ]; then |
| 415 XSU=`which xsu 2>/dev/null` |
| 416 if [ $? -eq 0 ] ; then |
| 417 DE=generic |
| 418 fi |
| 419 fi |
| 420 |
| 421 case "$DE" in |
| 422 kde) |
| 423 su_kde |
| 424 ;; |
| 425 |
| 426 gnome) |
| 427 su_gnome |
| 428 ;; |
| 429 |
| 430 generic) |
| 431 su_generic |
| 432 ;; |
| 433 |
| 434 *) |
| 435 [ x"$user" = x"" ] && user=root |
| 436 exit_failure_operation_impossible "no graphical method available for invokin
g '$cmd' as '$user'" |
| 437 ;; |
| 438 esac |
OLD | NEW |