PAM Script to Turn off auto screen-lock in Gnome Shell

I’ve created a new version of this script that can be run by NetworkManager, turning Gnome screensaver auto-lock on or off as the wireless essid changes.

In Linux, we have Pluggable Authentication Modules and various applications use PAM to authenticate users. Marco Ditri posted about using PAM to run a script that would prevent xscreensaver from locking the screen on his laptop when connected to his home wireless network.

I wanted to do the same in Gnome, so I’ve modified Marco’s script to use gsettings to disable Gnome Shell auto screen-lock when authenticated to particular wireless networks and enable it everywhere else. You’ll find comments in the script on how to incorporate it in your local PAM configuration.

#!/bin/sh -e

# Turn off gnome auto screen-lock when connected to specific wireless networks

# USAGE
# Add the following to /etc/pam.d/gdm-password, just after pam_gnome_keyring.so in the auth block:
#   auth optional pam_exec.so quiet /usr/local/sbin/pam_auth_inhibit_lock

# Hat tip - md, http://blog.bofh.it/debian/id_444

WIRELESS_IFACE=wlan0
AUTH_ESSID=orchard
AUTH_USER=steve

# return the ESSID of this interface
current_essid() {
  /sbin/iwconfig $1 | sed -nre '/ESSID/s/.*ESSID:"([^"]+)".*/\1/p'
}

# automatically unlock only for these users
case "$PAM_USER" in
  "")         echo "This program must be run by pam_auth.so!"
              exit 1
              ;;

  $AUTH_USER) ;;

  *)          exit 1
              ;;
esac

CURRENT_ESSID=$(current_essid $WIRELESS_IFACE)

# automatically turn off auto screen-lock when connected to these networks,
# otherwise turn on auto screen-lock
case "$CURRENT_ESSID" in
  $AUTH_ESSID)
    su -c "DISPLAY=:0 gsettings set org.gnome.desktop.screensaver lock-enabled false" $AUTH_USER
    exit 1
    ;;
  *)
    su -c "DISPLAY=:0 gsettings set org.gnome.desktop.screensaver lock-enabled true" $AUTH_USER
    exit 1
    ;;
esac

exit 6