File: lock

package info (click to toggle)
hibernate 2.0%2B15%2Bg88d54a8-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 740 kB
  • ctags: 114
  • sloc: sh: 1,223; makefile: 17
file content (533 lines) | stat: -rw-r--r-- 16,386 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet

AddConfigHandler LockOptions
AddConfigHelp "LockKDE <boolean>" "Lock all local KDE sessions before suspending."
AddConfigHelp "LockFreedesktop <boolean>" "Lock all local sessions compatible with freedesktop.org screensaver interface before suspending."
AddConfigHelp "LockXScreenSaver <boolean>" "Lock all local X11 sessions with xscreensaver running before suspending."
AddConfigHelp "LockGnomeScreenSaver <boolean>" "Lock all local X11 sessions with gnome-screensaver running before suspending."
AddConfigHelp "LockXLock <boolean>" "Lock active X11 session using xlock."
AddConfigHelp "LockXAutoLock <boolean>" "Lock all local X11 sessions running xautolock."
AddConfigHelp "LockConsoleAs <username>" "Locks the entire system after resuming, requiring you to enter either <username>'s or root's password to unlock it. (Requires vlock)."
AddConfigHelp "LockXtrLock <boolean>" "Lock active X11 session using xtrlock."
AddConfigHelp "LockEnlightenment17 <boolean>" "Lock enlightenment17 session before suspending."

AddOptionHandler LockCmdlineOptions
AddLongOption 'lock-console-as:'
AddOptionHelp '--lock-console-as <username>' 'Uses vlock to lock the entire system after resuming, requirng you to enter the password for the given user to unlock it. This overrides any username given in the configuration file. (Requires vlock)'

LockFreedesktop() {
    [ x"$LOCK_FREEDESKTOP" != "x1" ] && return 0

    for userdir in /home/*; do
        user="$(basename $userdir)"
        for session in /home/$user/.dbus/session-bus/*; do
            if [ -e "$session" ]; then
                . $session
                su -c "dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call /ScreenSaver org.freedesktop.ScreenSaver.Lock" "$user"
            fi
        done
    done

    return 0
}

LockKDE() {
    [ x"$LOCK_KDE" != "x1" ] && return 0

    if ! ps ax | grep -q '[d]copserver' ; then
	vecho 1 'No KDE sessions detected. Not locking KDE.'
	return 0
    fi

    if ! command -v dcop > /dev/null 2>&1 ; then
	# Gentoo and Slackware stash this in a silly place.
	for i in /usr/kde/*/bin/dcop /opt/kde/bin/dcop ; do
	    if [ -x "$i" ] ; then
		PATH=$PATH:${i%%/dcop}
		export PATH
		break
	    fi
	done
    fi

    if ! command -v dcop > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock KDE. `dcop` program not found.'
	return 1 # abort, unless forced
    fi

    local avail_sessions
    local session

    # get all sessions (ignore non local ones!)
    avail_sessions=`dcop --all-users --list-sessions | grep '.DCOP.*__0'`

    ShouldLock || return 0

    # send the lock command to all sessions
    for session in $avail_sessions; do
	# Check first that the session is not already locked, to prevent dcop
	# from hanging.
	locked=`dcop --session "$session" --all-users kdesktop KScreensaverIface isBlanked 2>/dev/null`
	if [ x"$locked" = "xfalse" ] ; then
	    vecho 1 "Locking session $session"
	    # /dev/null because dcop warns if it can't connect to X (this is normal!)
	    dcop --session "$session" --all-users kdesktop KScreensaverIface lock > /dev/null 2>&1
	else
	    vecho 2 "Session $session is already locked!"
	fi
    done

    # returning 0 because dcop warns if it can't connect to X (this is normal!)
    return 0
}

LockXlock() {
    FindXServer || return 0
    ShouldLock || return 0

    if command -v xlock > /dev/null 2>&1 ; then
	vecho 1 'Trying xlock'
	su $XUSER -c "xlock -mode blank &"
	return 0
    fi
    return 0
}

LockXScreensaver() {
    local locked_one
    locked_one=

    [ x"$LOCK_XSCREENSAVER" != "x1" ] && return 0

    if ! command -v xscreensaver-command > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock with xscreensaver. `xscreensaver-command` not found.'
	# Try xlock.
	LockXlock
	return 0
    fi

    ShouldLock || return 0

    local xpid
    for xpid in `pidof xscreensaver` ; do
	local xuser xdisp xauth xhome
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Locking $xuser's xscreensaver on display $xdisp using authority file $xauth"
	DISPLAY=$xdisp XAUTHORITY=$xauth su $xuser -c "$(command -v xscreensaver-command) -lock"
	if [ $? -ne 0 ] ; then
	    vecho 0 "Failed to activate xscreensaver on $xdisp using authority file $xauth."
	    DISPLAY=$xdisp XAUTHORITY=$xauth LockXlock && locked_one=1
	else
	    locked_one=1
	fi
    done

    # Fall back to xlock if nothing worked.
    [ -z "$locked_one" ] && LockXlock

    # Failing is silly. What would they do about it?
    return 0
}

UnlockXScreensaver() {
    # This function name is kind of bad, it doesn't actually unlock
    # anything.  It just calls xscreensaver-command -deactivate, which makes
    # the password prompt appear on the display.

    # FIXME: refactor this code so it looks less obviously like a
    # cut-n-paste job from LockXScreensaver.

    [ x"$LOCK_XSCREENSAVER" != "x1" ] && return 0

    command -v xscreensaver-command > /dev/null 2>&1 || return

    local xpid

    for xpid in `pidof xscreensaver` ; do
	local xuser xdisp xauth xhome
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Unlocking $xuser's xscreensaver on display $xdisp using authority file $xauth"
	DISPLAY=$xdisp XAUTHORITY=$xauth su $xuser -c "$(command -v xscreensaver-command) -deactivate"
    done

    # Failing is silly. What would they do about it?
    return 0
}

LockGnomeScreensaver() {
    local locked_one

    locked_one=

    [ x"$LOCK_GNOMESCREENSAVER" != "x1" ] && return 0

    if ! command -v gnome-screensaver-command > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock with gnome-screensaver. `gnome-screensaver-command` not found.'
	# Try xlock.
	LockXlock
	return 0
    fi

    ShouldLock || return 0

    local xpid
    for xpid in `pidof gnome-screensaver` ; do
	local xuser xdisp xauth xhome xdbus
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	xdbus=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DBUS_SESSION_BUS_ADDRESS"){print $2"="$3"="$4}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Locking $xuser's gnome-screensaver on display $xdisp using authority file $xauth"
	DISPLAY=$xdisp XAUTHORITY=$xauth DBUS_SESSION_BUS_ADDRESS=$xdbus su $xuser -c "$(command -v gnome-screensaver-command) --lock"
	if [ $? -ne 0 ] ; then
	    vecho 0 "Failed to activate gnome-screensaver on $xdisp using authority file $xauth."
	    DISPLAY=$xdisp XAUTHORITY=$xauth LockXlock && locked_one=1
	else
	    locked_one=1
	fi
    done

    # Fall back to xlock if nothing worked.
    [ -z "$locked_one" ] && LockXlock

    # Failing is silly. What would they do about it?
    return 0
}

UnlockGnomeScreensaver() {
    # This function name is kind of bad, it doesn't actually unlock
    # anything.  It just calls gnome-screensaver-command --poke, which makes
    # the password prompt appear on the display.

    # FIXME: refactor this code so it looks less obviously like a
    # cut-n-paste job from LockXScreensaver :) .

    [ x"$LOCK_GNOMESCREENSAVER" != "x1" ] && return 0

    command -v gnome-screensaver-command > /dev/null 2>&1 || return

    local xpid
    for xpid in `pidof gnome-screensaver` ; do
	local xuser xdisp xauth xhome xdbus
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	xdbus=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DBUS_SESSION_BUS_ADDRESS"){print $2"="$3"="$4}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Unlocking $xuser's gnome-screensaver on display $xdisp using authority file $xauth"
	DISPLAY=$xdisp XAUTHORITY=$xauth DBUS_SESSION_BUS_ADDRESS=$xdbus su $xuser -c "$(command -v gnome-screensaver-command) --poke"
    done

    # Failing is silly. What would they do about it?
    return 0
}

LockXAutoLock() {
    [ x"$LOCK_XAUTOLOCK" != "x1" ] && return 0

    if ! command -v xautolock > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock with xautolock. `xautolock` not found.'
	return
    fi

    ShouldLock || return 0

    local xpid
    for xpid in `pidof xautolock` ; do
	local xuser xdisp xauth xhome
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Locking $xuser's xautolock on display $xdisp using authority file $xauth"
	DISPLAY=$xdisp XAUTHORITY=$xauth su $xuser -c "xautolock -locknow"
    done

    # Failing is silly. What would they do about it?
    return 0
}

LockE17() {
    [ x"$LOCK_E17" != "x1" ] && return 0

    if ! command -v enlightenment_remote > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock with enlightenment_remote. `enlightenment_remote` not found.'
	return
    fi

    ShouldLock || return 0

    local xpid
    for xpid in `pidof enlightenment` ; do
	local xuser xdisp xauth xhome
	xuser=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$xpid/environ)
	xdisp=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "DISPLAY"){print $2}' < /proc/$xpid/environ)
	xauth=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "XAUTHORITY"){print $2}' < /proc/$xpid/environ)
	if [ -z "$xauth" ] ; then
	    xhome=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "HOME"){print $2}' < /proc/$xpid/environ)
	    xauth="$xhome/.Xauthority"
	fi

	vecho 2 "Locking $xuser's e17 on display $xdisp using authority file $xauth"
	#We need export this wariable to run enlightenment_remote
	export E_IPC_SOCKET=/tmp/enlightenment-$xuser/disp-$xdisp.0-$xpid
	DISPLAY=$xdisp XAUTHORITY=$xauth su $xuser -c "enlightenment_remote -lock-desktop"
    done

    # Failing is silly. What would they do about it?
    return 0
}

LockConsole() {
    # Prerequistes are tested for in SwitchToLockConsole

    [ -z "$LOCK_CONSOLE_USER" ] && return 0

    ShouldLock || return 0

    # Use vlock to lock all consoles. We must already be at the given console.
    vecho 1 "Locking all consoles"
    openvt -wfc $LOCK_DEST_VT -- su - "$LOCK_CONSOLE_USER" -c "TERM=linux tput clear; vlock -a"

    # This will hang until the root password is entered.

    # Switch back to original console
    vecho 3 "lock: changing console back to $LOCK_ORIGINAL_VT"
    chvt $LOCK_ORIGINAL_VT
}

SwitchToLockConsole() {
    [ -z "$LOCK_CONSOLE_USER" ] && return 0

    if ! command -v openvt > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock console. `openvt` program not found.'
	LOCK_CONSOLE_USER=
	return 1 # abort, unless forced
    fi

    if ! command -v tput > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock console. `tput` program not found.'
	LOCK_CONSOLE_USER=
	return 1 # abort, unless forced
    fi

    if ! command -v vlock > /dev/null 2>&1 ; then
	vecho 0 'Cannot lock console. `vlock` program not found.'
	LOCK_CONSOLE_USER=
	return 1 # abort, unless forced
    fi

    if command -v fgconsole > /dev/null 2>&1 ; then
	LOCK_ORIGINAL_VT=`fgconsole`
    else
	LOCK_ORIGINAL_VT=1
    fi

    LOCK_DEST_VT=61 # Choose something different - can't afford to have silent bootsplash there
    vecho 2 "lock: changing console from $LOCK_ORIGINAL_VT to $LOCK_DEST_VT"
    chvt $LOCK_DEST_VT || return 1

    return 0
}

LockXtrlock() {
    FindXServer || return 0
    if command -v xtrlock > /dev/null 2>&1 ; then
    	vecho 1 'Trying xtrlock'

    	## somehow the xdisp & xauth & xuser vars are clobbered
    	## by the time they reach here (or not exported ?)
    	local xdisp xuser xauth
    	xdisp=$DISPLAY
    	if [ -n "$XUSER" ] && [ -n "$XAUTHORITY" ] ; then
      	    xuser=$XUSER
      	    xauth=$XAUTHORITY
    	elif [ -n "$xhome" ] ; then
      	    xuser="${xhome##*/}"
      	    xauth=$xhome/.Xauthority
    	else
      	    return 0
    	fi
    	vecho 2 "Locking ${xuser}'s xtrlock on display $xdisp using authority file $xauth"
    	DISPLAY=$xdisp XAUTHORITY=$xauth su $xuser -c "xtrlock &"
    	if [ $? -ne 0 ] ; then
      	    vecho 0 "Failed to activate ${xuser}'s xtrlock on $xdisp using authority file $xauth."
    	fi
    	return 0
    fi
    return 0
}

LockOptions() {
    case $1 in
	lockkde)
	    if BoolIsOn "$1" "$2" ; then
		LOCK_KDE=1
		if [ -z "$KDELOCK_HOOKED" ] ; then
		    AddSuspendHook 91 LockKDE
		    KDELOCK_HOOKED=1
		fi
	    else
		LOCK_KDE=0
	    fi
	    ;;
	lockfreedesktop)
	    if BoolIsOn "$1" "$2" ; then
		LOCK_FREEDESKTOP=1
		if [ -z "$FREEDESKTOPLOCK_HOOKED" ] ; then
		    AddSuspendHook 91 LockFreedesktop
		    FREEDESKTOPLOCK_HOOKED=1
		fi
	    else
		LOCK_FREEDESKTOP=0
	    fi
	    ;;
	lockxscreensaver)
	    if BoolIsOn "$1" "$2" ; then 
		LOCK_XSCREENSAVER=1
		if [ -z "$XSCREENSAVERLOCK_HOOKED" ]; then
		    AddSuspendHook 91 LockXScreensaver
		    AddResumeHook 30 UnlockXScreensaver
		    XSCREENSAVERLOCK_HOOKED=1
		fi
	    else
		LOCK_XSCREENSAVER=0
	    fi
	    ;;

	lockgnomescreensaver)
	    if BoolIsOn "$1" "$2" ; then
		LOCK_GNOMESCREENSAVER=1
		if [ -z "$GNOMESCREENSAVERLOCK_HOOKED" ]; then
		    AddSuspendHook 91 LockGnomeScreensaver
		    AddResumeHook 30 UnlockGnomeScreensaver
		    GNOMESCREENSAVERLOCK_HOOKED=1
		fi
	    else
		LOCK_GNOMESCREENSAVER=0
	    fi
	    ;;

        lockxlock)
            if BoolIsOn "$1" "$2" ; then
		LOCK_XLOCK=1
		if [ -z "$XLOCK_HOOKED" ]; then
		    AddResumeHook 91 LockXlock
		    XLOCK_HOOKED=1
		fi
	    else
		LOCK_XLOCK=0
            fi
            ;;

        lockxautolock)
            if BoolIsOn "$1" "$2" ; then
		LOCK_XAUTOLOCK=1
		if [ -z "$XAUTOLOCK_HOOKED" ]; then
		    AddResumeHook 91 LockXAutoLock
		    XAUTOLOCK_HOOKED=1
		fi
	    else
		LOCK_XAUTOLOCK=0
            fi
            ;;

        locke17)
            if BoolIsOn "$1" "$2" ; then
                LOCK_E17=1
                if [ -z "$E17_HOOKED" ]; then
                    AddResumeHook 91 LockE17
                    E17_HOOKED=1
                fi
            else
                LOCK_E17=0
            fi
            ;;

	lockconsoleas)
	    LOCK_CONSOLE_USER="$2"

	    if [ -z "$CONSOLELOCK_HOOKED" ] ; then
		AddResumeHook 96 LockConsole
		AddSuspendHook 96 SwitchToLockConsole

		CONSOLELOCK_HOOKED=1
	    fi
	    ;;

        lockxtrlock)
            if BoolIsOn "$1" "$2" ; then
    	    	LOCK_XTRLOCK=1
    	    	if [ -z "$XTRLOCK_HOOKED" ]; then
            	    AddResumeHook 91 LockXtrlock
            	    XTRLOCK_HOOKED=1
    	    	fi
      	    	else
    	    	LOCK_XTRLOCK=0
            fi
            ;;

	*)
	    return 1
    esac

    return 0
}

LockCmdlineOptions() {
    case $1 in
	--lock-console-as)
	    LOCK_CONSOLE_USER="$2"

	    if [ -z "$CONSOLELOCK_HOOKED" ] ; then
		AddResumeHook 96 LockConsole
		AddSuspendHook 96 SwitchToLockConsole

		CONSOLELOCK_HOOKED=1
	    fi
	    ;;
	*)
	    return 1
    esac
    return 0
}

ShouldLock() {
    case "/$SWSUSP2_NO_SUSPEND/$USUSPEND_NO_SUSPEND/$SWSUSP2_BUG_REPORT/" in
	*1*)
	    vecho 1 'Not locking the screen with --no-suspend/-n'
	    return 1
	    ;;
    esac
    return 0
}

# $Id$