grml-lock: drop gdialog usage and don't wrap text in zenity
[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    . /etc/grml/script-functions
25    check4progs physlock sudo chpasswd dialog || { echo "Sorry, necessary tools missing - can not continue. Exiting.">&2 ; exit 1 ; }
26 fi
27
28 PWD_TEXT1="Set password (hidden typing):"
29 PWD_TEXT2="Retype new password:"
30
31 # by default use console frontend
32 DIALOG='dialog'
33 PWD_CMD="dialog --stdout --title $PN --passwordbox"
34
35 GUI=false
36 # only when using X and zenity is available use graphical frontend
37 if [ -n "${DISPLAY}" ] && [ -x "$(command -v zenity)" ] ; then
38   DIALOG='zenity'
39   PWD_CMD="zenity --title $PN --entry --hide-text"
40   GUI=true
41 fi
42
43 if ! [ -r /etc/grml_version ] ; then
44   if [ "${GUI}" = true ] ; then
45     $DIALOG --no-wrap --title "$PN" --warning --text "Warning: this system does not look like a Grml (based) system,\n
46     and therefore might not work as intended."
47   else
48     $DIALOG --title "$PN" --msgbox "Warning: this system does not look like a Grml (based) system
49     and therefore might not work as intended." 7 70
50   fi
51 fi
52
53 lock_desktop() {
54   # be backwards compatible, see https://github.com/grml/grml/issues/87
55   if physlock --help 2>&1 | grep -q -- '-u user' ; then
56     sudo physlock -u "$USER"
57   else
58     sudo physlock
59   fi
60 }
61
62 is_passwd_set() {
63   if sudo grep -q "$USER:\*:" /etc/shadow ; then
64     return 1
65   else
66     return 0
67   fi
68 }
69
70 set_passwd() {
71   if [ "${GUI}" = true ] ; then
72     PASSWD1="$($PWD_CMD --text="$PWD_TEXT1")"
73     PASSWD2="$($PWD_CMD --text="$PWD_TEXT2")"
74   else
75     PASSWD1="$($PWD_CMD "$PWD_TEXT1" 0 0)"
76     PASSWD2="$($PWD_CMD "$PWD_TEXT2" 0 0)"
77   fi
78
79   if [ -z "$PASSWD1" ] ; then
80     if [ -n "${GUI}" ] ; then
81       $DIALOG --title "$PN" --error --text "Error retrieving password. Exiting."
82     else
83       $DIALOG --title "$PN" --msgbox "Error retrieving password. Exiting." 0 0
84     fi
85     exit 1
86   fi
87
88   if [ "$PASSWD1" = "$PASSWD2" ] ; then
89     echo "$USER:$PASSWD2" | sudo chpasswd
90   else
91     if [ "${GUI}" = true ] ; then
92       $DIALOG --no-wrap --title "$PN" --error --text "Error: passwords do not match.\nExiting."
93     else
94       $DIALOG --title "$PN" --msgbox "Error: passwords do not match. Exiting." 0 0
95     fi
96     exit 1
97   fi
98 }
99
100 askpwd() {
101   if [ "${GUI}" = true ] ; then
102     $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?"
103     RC=$?
104   else
105     $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
106     RC=$?
107   fi
108
109   if [ "$RC" = "0" ] ; then
110     set_passwd
111   else
112     exit 1
113   fi
114 }
115
116 if ! isgrmlcd ; then
117   lock_desktop
118 else
119   if is_passwd_set || askpwd ; then
120     lock_desktop
121   else
122     exit 1
123   fi
124 fi
125
126 ## END OF FILE #################################################################