dd97c753356b2f940f77a53d87346bb8e037a522
[grml-scripts.git] / usr_bin / grml-lock
1 #!/bin/zsh
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="$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 vlock application.
18
19 Usage: just execute $PN without any further options."
20   exit 0
21 fi
22 if [ -r /etc/grml/script-functions ] ; then
23    . /etc/grml/script-functions
24    check4progs vlock sudo chpasswd dialog || { echo "Sorry, can't continue. Exiting.">&2 ; exit 1 }
25 fi
26
27 if ! [ -r /etc/grml_version ] ; then
28   dialog --stdout --title "$PN" --msgbox "Warning: this system does not look like a grml (based) system
29 and therefore might not work as intended." 7 70
30 fi
31
32 lock_desktop() {
33   vlock -a -n -s
34 }
35
36 is_passwd_set() {
37   if sudo grep -q "$USER:\*:" /etc/shadow ; then
38     return 1
39   else
40     return 0
41   fi
42 }
43
44 set_passwd() {
45   PASSWD1=$(dialog --stdout --title "$PN" --passwordbox "Set password (hidden typing):" 0 0)
46   PASSWD2=$(dialog --stdout --title "$PN" --passwordbox "Retype new password:" 0 0)
47   if [ "$PASSWD1" = "$PASSWD2" ] ; then
48     echo "$USER:$PASSWD2" | sudo chpasswd
49   else
50     dialog --stdout --title "$PN" --msgbox "Error: passwords to not match. Exiting." 0 0
51     exit 1
52   fi
53 }
54
55 askpwd() {
56   dialog --stdout --title "$PN" --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
57   if [ "$?" -eq 0 ] ; then
58     set_passwd
59   else
60     exit 1
61   fi
62 }
63
64 if ! isgrmlcd ; then
65   lock_desktop
66 else
67   is_passwd_set || askpwd
68   lock_desktop
69 fi
70
71 ## END OF FILE #################################################################