Remove various scripts
[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 vlock 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 vlock 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 # only if using X and gdialog + zenity are available use graphical frontend
36 if [ -n "$DISPLAY" ] && [ -x "$(which gdialog)" ] && [ -x "$(which zenity)" ] ; then
37   DIALOG='gdialog'
38   PWD_CMD="zenity --title $PN --entry --hide-text"
39 fi
40
41 if ! [ -r /etc/grml_version ] ; then
42   $DIALOG --title "$PN" --msgbox "Warning: this system does not look like a Grml (based) system
43 and therefore might not work as intended." 7 70
44 fi
45
46 lock_desktop() {
47   vlock -a -n -s
48 }
49
50 is_passwd_set() {
51   if sudo grep -q "$USER:\*:" /etc/shadow ; then
52     return 1
53   else
54     return 0
55   fi
56 }
57
58 set_passwd() {
59   if [ "$DIALOG" = "gdialog" ] ; then
60     PASSWD1="$($PWD_CMD --text="$PWD_TEXT1")"
61     PASSWD2="$($PWD_CMD --text="$PWD_TEXT2")"
62   else
63     PASSWD1="$($PWD_CMD "$PWD_TEXT1" 0 0)"
64     PASSWD2="$($PWD_CMD "$PWD_TEXT2" 0 0)"
65   fi
66
67   if [ -z "$PASSWD1" ] ; then
68     $DIALOG --title "$PN" --msgbox "Error retrieving password. Exiting." 0 0
69     exit 1
70   fi
71   if [ "$PASSWD1" = "$PASSWD2" ] ; then
72     echo "$USER:$PASSWD2" | sudo chpasswd
73   else
74     $DIALOG --title "$PN" --msgbox "Error: passwords to not match. Exiting." 0 0
75     exit 1
76   fi
77 }
78
79 askpwd() {
80   if [ "$DIALOG" = "gdialog" ] ; then
81     zenity --title="$PN" --question --cancel-label='Exit' --ok-label='Set password' --text="User $USER has no password set yet. Without a password you will not be able to log in again. Set password for user $USER?"
82     RC=$?
83   else
84     $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
85     RC=$?
86   fi
87
88   if [ "$RC" = "0" ] ; then
89     set_passwd
90   else
91     exit 1
92   fi
93 }
94
95 if ! isgrmlcd ; then
96   lock_desktop
97 else
98   is_passwd_set || askpwd
99   [ "$?" = "0" ] && lock_desktop || exit 1
100 fi
101
102 ## END OF FILE #################################################################