Release new version 2.13.0
[grml-scripts.git] / usr_bin / grml-lock
1 #!/bin/bash
2 # Filename:      grml-lock
3 # Purpose:       lock console
4 # Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 PN="$(basename "$0")"
10
11 [ -n "$USER" ] || USER=grml
12
13 if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
14   echo "${PN}: wrapper script to lock desktop
15
16 This script is a wrapper to lock your desktop session
17 through the physlock application.
18
19 Usage: just execute $PN without any further options."
20   exit 0
21 fi
22
23 if [ -r /etc/grml/script-functions ] ; then
24    # shellcheck disable=SC1091
25    . /etc/grml/script-functions
26    check4progs physlock sudo chpasswd dialog || { echo "Sorry, necessary tools missing - can not continue. Exiting.">&2 ; exit 1 ; }
27 fi
28
29 PWD_TEXT1="Set password (hidden typing):"
30 PWD_TEXT2="Retype new password:"
31
32 # by default use console frontend
33 DIALOG='dialog'
34 PWD_CMD="dialog --stdout --title $PN --passwordbox"
35
36 GUI=false
37 # only when using X and zenity is available use graphical frontend
38 if [ -n "${DISPLAY}" ] && [ -x "$(command -v zenity)" ] ; then
39   DIALOG='zenity'
40   PWD_CMD="zenity --title $PN --entry --hide-text"
41   GUI=true
42 fi
43
44 if ! [ -r /etc/grml_version ] ; then
45   if [ "${GUI}" = true ] ; then
46     $DIALOG --no-wrap --title "$PN" --warning --text "Warning: this system does not look like a Grml (based) system,\n
47     and therefore might not work as intended."
48   else
49     $DIALOG --title "$PN" --msgbox "Warning: this system does not look like a Grml (based) system
50     and therefore might not work as intended." 7 70
51   fi
52 fi
53
54 lock_desktop() {
55   # be backwards compatible, see https://github.com/grml/grml/issues/87
56   if physlock --help 2>&1 | grep -q -- '-u user' ; then
57     sudo physlock -u "$USER"
58   else
59     sudo physlock
60   fi
61 }
62
63 is_passwd_set() {
64   if sudo grep -q "$USER:\*:" /etc/shadow ; then
65     return 1
66   else
67     return 0
68   fi
69 }
70
71 set_passwd() {
72   if [ "${GUI}" = true ] ; then
73     PASSWD1="$($PWD_CMD --text="$PWD_TEXT1")"
74     PASSWD2="$($PWD_CMD --text="$PWD_TEXT2")"
75   else
76     PASSWD1="$($PWD_CMD "$PWD_TEXT1" 0 0)"
77     PASSWD2="$($PWD_CMD "$PWD_TEXT2" 0 0)"
78   fi
79
80   if [ -z "$PASSWD1" ] ; then
81     if [ -n "${GUI}" ] ; then
82       $DIALOG --title "$PN" --error --text "Error retrieving password. Exiting."
83     else
84       $DIALOG --title "$PN" --msgbox "Error retrieving password. Exiting." 0 0
85     fi
86     exit 1
87   fi
88
89   if [ "$PASSWD1" = "$PASSWD2" ] ; then
90     echo "$USER:$PASSWD2" | sudo chpasswd
91   else
92     if [ "${GUI}" = true ] ; then
93       $DIALOG --no-wrap --title "$PN" --error --text "Error: passwords do not match.\nExiting."
94     else
95       $DIALOG --title "$PN" --msgbox "Error: passwords do not match. Exiting." 0 0
96     fi
97     exit 1
98   fi
99 }
100
101 askpwd() {
102   if [ "${GUI}" = true ] ; then
103     $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?"
104     RC=$?
105   else
106     $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
107     RC=$?
108   fi
109
110   if [ "$RC" = "0" ] ; then
111     set_passwd
112   else
113     exit 1
114   fi
115 }
116
117 if ! isgrmlcd ; then
118   lock_desktop
119 else
120   if is_passwd_set || askpwd ; then
121     lock_desktop
122   else
123     exit 1
124   fi
125 fi
126
127 ## END OF FILE #################################################################