Release new version 2.13.0
[grml-scripts.git] / usr_bin / grml-lock
index 2d75050..46e0ab5 100755 (executable)
-#!/bin/zsh
+#!/bin/bash
 # Filename:      grml-lock
 # Purpose:       lock console
 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
 # Bug-Reports:   see http://grml.org/bugs/
 # License:       This file is licensed under the GPL v2.
-# Latest change: Sam Mai 27 15:12:24 CEST 2006 [mika]
 ################################################################################
 
-PN=$0
+PN="$(basename "$0")"
 
-if [ -r /etc/grml_version ] ; then
-else
-  dialog --stdout --title "$PN" --msgbox "Warning: this system does not look like a grml-system
-it might not work as intended." 7 70
+[ -n "$USER" ] || USER=grml
+
+if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
+  echo "${PN}: wrapper script to lock desktop
+
+This script is a wrapper to lock your desktop session
+through the physlock application.
+
+Usage: just execute $PN without any further options."
+  exit 0
 fi
 
-welcome_screen() {
-dialog --stdout --title "$PN" --msgbox "Welcome to $PN!
+if [ -r /etc/grml/script-functions ] ; then
+   # shellcheck disable=SC1091
+   . /etc/grml/script-functions
+   check4progs physlock sudo chpasswd dialog || { echo "Sorry, necessary tools missing - can not continue. Exiting.">&2 ; exit 1 ; }
+fi
 
-This script will lock virtual consoles when running
-on console or lock X server when running X.
+PWD_TEXT1="Set password (hidden typing):"
+PWD_TEXT2="Retype new password:"
 
-Notice that you can not lock a GNU screen-session
-via this script, use the lockscreen command
-instead (default is pressing ctrl-a x inside
-GNU screen)!
+# by default use console frontend
+DIALOG='dialog'
+PWD_CMD="dialog --stdout --title $PN --passwordbox"
 
-Report bugs, send wishes and feedback to the grml team:
-http://www.grml.org/ - contact (at) grml.org
-" 16 65
-}
+GUI=false
+# only when using X and zenity is available use graphical frontend
+if [ -n "${DISPLAY}" ] && [ -x "$(command -v zenity)" ] ; then
+  DIALOG='zenity'
+  PWD_CMD="zenity --title $PN --entry --hide-text"
+  GUI=true
+fi
 
-lockcons()
-{
-  vlock -a
-}
+if ! [ -r /etc/grml_version ] ; then
+  if [ "${GUI}" = true ] ; then
+    $DIALOG --no-wrap --title "$PN" --warning --text "Warning: this system does not look like a Grml (based) system,\n
+    and therefore might not work as intended."
+  else
+    $DIALOG --title "$PN" --msgbox "Warning: this system does not look like a Grml (based) system
+    and therefore might not work as intended." 7 70
+  fi
+fi
 
-lockx()
-{
-  xlock
+lock_desktop() {
+  # be backwards compatible, see https://github.com/grml/grml/issues/87
+  if physlock --help 2>&1 | grep -q -- '-u user' ; then
+    sudo physlock -u "$USER"
+  else
+    sudo physlock
+  fi
 }
 
-askxlock()
-{
-  dialog --stdout --title "$PN" --yesno "Now lock X?" 0 0
-  retval=$?
-  case $retval in
-    0) 
-       lockx
-       ;;
-  esac
+is_passwd_set() {
+  if sudo grep -q "$USER:\*:" /etc/shadow ; then
+    return 1
+  else
+    return 0
+  fi
 }
 
-askvlock()
-{
-  dialog --stdout --title "$PN" --yesno "Now lock consoles?" 0 0
-  retval=$?
-  case $retval in
-    0)
-       lockcons
-       ;;
-  esac
+set_passwd() {
+  if [ "${GUI}" = true ] ; then
+    PASSWD1="$($PWD_CMD --text="$PWD_TEXT1")"
+    PASSWD2="$($PWD_CMD --text="$PWD_TEXT2")"
+  else
+    PASSWD1="$($PWD_CMD "$PWD_TEXT1" 0 0)"
+    PASSWD2="$($PWD_CMD "$PWD_TEXT2" 0 0)"
+  fi
+
+  if [ -z "$PASSWD1" ] ; then
+    if [ -n "${GUI}" ] ; then
+      $DIALOG --title "$PN" --error --text "Error retrieving password. Exiting."
+    else
+      $DIALOG --title "$PN" --msgbox "Error retrieving password. Exiting." 0 0
+    fi
+    exit 1
+  fi
+
+  if [ "$PASSWD1" = "$PASSWD2" ] ; then
+    echo "$USER:$PASSWD2" | sudo chpasswd
+  else
+    if [ "${GUI}" = true ] ; then
+      $DIALOG --no-wrap --title "$PN" --error --text "Error: passwords do not match.\nExiting."
+    else
+      $DIALOG --title "$PN" --msgbox "Error: passwords do not match. Exiting." 0 0
+    fi
+    exit 1
+  fi
 }
 
-askpwd()
-{
-dialog --stdout --title "$PN" --yesno "Set password for user $USER?" 0 0
-retval=$?
-case $retval in
-  0) 
-     while [ -z "$PASSWD" ] ; do
-       PASSWD=$(dialog --stdout --title "$PN" --passwordbox "Set password (hidden typing):" 0 0)
-       if [ -n "$PASSWD" ] ; then
-         echo "$USER:$PASSWD" | sudo chpasswd
-       else
-         dialog --stdout --title "$PN" --msgbox "Error while setting password. Empty passwords are not accepted." 6 65
-       fi
-     done
-     ;;
-  *)
-#     asklock
-     ;;
-esac
+askpwd() {
+  if [ "${GUI}" = true ] ; then
+    $DIALOG --no-wrap --title="$PN" --question --cancel-label='Exit' --ok-label='Set password' --text="User $USER has no password set yet.\nWithout a password you will not be able to log in again.\nSet password for user $USER?"
+    RC=$?
+  else
+    $DIALOG --title "$PN" --no-label Exit --yes-label Continue --yesno "User $USER has no password set yet. Without a password you will not be able to log in again. Set password for user $USER?" 0 0
+    RC=$?
+  fi
+
+  if [ "$RC" = "0" ] ; then
+    set_passwd
+  else
+    exit 1
+  fi
 }
 
-if [[ $(tty) == /dev/tty* ]] ; then
-  welcome_screen
-  askpwd
-  askvlock
-elif [ -n "$DISPLAY" ] ; then
-  welcome_screen
-  askpwd
-  askxlock
+if ! isgrmlcd ; then
+  lock_desktop
+else
+  if is_passwd_set || askpwd ; then
+    lock_desktop
+  else
+    exit 1
+  fi
 fi
 
 ## END OF FILE #################################################################