Transparent Encryption for a Home folder

The following steps have been completed on a fresh Debian Squeeze install to allow users on a desktop to encrypt their Home directory.

Problem Statement

Many organisations are requesting users encrypt their laptop to protect confidential information (customer names, internal contact details, etc). On many Windows systems this is problematic and intrusive. This procedure is to help Debian users configure their systems to encrypt/decrypt their Home folder auto-magically.

Install Debian Squeeze

This testing was based on the "Debian Live CD Gome Desktop", but any installation media should do. For more details on installing Debian go to the source.

Dummy user account

When setting up the first user (during the install), I suggest you create a dummy account (e.g. "Dummy User"). This will allow the configuration to be completed for the 'real' user.

Disk setup

For the disk setup, choose "Guided - use entire disk" or "Guided - use entire disk and setup LVM". We are NOT setting up encryption using the Debian install method.

The recommendation is to select "All files in one partition" and accept the defaults.

When the install is complete, log in with the dummy user created during the install. This user will be able to get root privileges for the rest of the process.

After installing Debian always make sure your system is up to date so run the following:-

aptitude update ; aptitude safe-upgrade

Setup encrypted home directories

Move the dummy user to a directory which is not managed by encfs (e.g. not under /home)

mkdir -p /home.original/
cp -a /home/username /home.original/

Edit /etc/passwd so the dummy user's home directory path is setup correctly (e.g. /home.original/username)

Then logout and login. Remove the dummy user's old home

rm -rf /home/username

On an up to date Debian system, install the following packages:-

aptitude install encfs libpam-encfs libpam-mount

Update configuration files

I have pasted the contents of my configuration files as the order of the insertions into the files is important and when I set this up the articles I referred to only had the file additions.

/etc/security/pam_encfs.conf

#Note that I dont support spaces in params
#So if your for example gonna specify idle time use --idle=X not -i X.

#If this is specified program will attempt to drop permissions before running encfs.
# (will not work with --public for example)
drop_permissions

#This specifies default encfs options
#encfs_default --idle=1
encfs_default

#Same for fuse, note that allow_root (or allow_other, or --public in encfs) is needed to run gdm/X.
fuse_default allow_root,nonempty

#- means match all, put any overrides over it.
#if - is in username it will take source path + "/$USER", and mount to $HOME

#USERNAME       SOURCE          TARGET PATH     ENCFS Options                   FUSE Options
#user           /home/.enc/user /home/user      -v,--idle=1,-test,-test2        allow_root
#-              /home/.enc      -               -v                              allow_other
#-              /home/.enc      -               -v                              allow_root
-               /home/.encfs    -       -v      -

/etc/security/pam_env.conf

This change is required to remove the annoying message "Cannot update ICEauthority /home/(user)/.ICEauthority. The last line of this example contains the change.

#
# This is the configuration file for pam_env, a PAM module to load in 
# a configurable list of environment variables for a 
# 
# The original idea for this came from Andrew G. Morgan ...
#<quote>
#   Mmm. Perhaps you might like to write a pam_env module that reads a
#   default environment from a file? I can see that as REALLY
#   useful... Note it would be an "auth" module that returns PAM_IGNORE
#   for the auth part and sets the environment returning PAM_SUCCESS in
#   the setcred function...
#</quote>
#
# What I wanted was the REMOTEHOST variable set, purely for selfish
# reasons, and AGM didn't want it added to the SimpleApps login
# program (which is where I added the patch). So, my first concern is
# that variable, from there there are numerous others that might/would
# be useful to be set: NNTPSERVER, LESS, PATH, PAGER, MANPAGER .....
#
# Of course, these are a different kind of variable than REMOTEHOST in
# that they are things that are likely to be configured by
# administrators rather than set by logging in, how to treat them both
# in the same config file?
#
# Here is my idea: 
#
# Each line starts with the variable name, there are then two possible
# options for each variable DEFAULT and OVERRIDE. 
# DEFAULT allows and administrator to set the value of the
# variable  to some default value, if none is supplied then the empty
# string is assumed. The OVERRIDE option tells pam_env that it should
# enter in its value (overriding the default value) if there is one
# to use. OVERRIDE is not used, "" is assumed and no override will be
# done. 
#
# VARIABLE   [DEFAULT=[value]]  [OVERRIDE=[value]]
#
# (Possibly non-existent) environment variables may be used in values
# using the ${string} syntax and (possibly non-existent) PAM_ITEMs may
# be used in values using the @{string} syntax. Both the $ and @
# characters can be backslash escaped to be used as literal values
# values can be delimited with "", escaped " not supported.
# Note that many environment variables that you would like to use
# may not be set by the time the module is called.
# For example, HOME is used below several times, but 
# many PAM applications don't make it available by the time you need it.
#
#
# First, some special variables
#
# Set the REMOTEHOST variable for any hosts that are remote, default
# to "localhost" rather than not being set at all
#REMOTEHOST     DEFAULT=localhost OVERRIDE=@{PAM_RHOST}
#
# Set the DISPLAY variable if it seems reasonable 
#DISPLAY                DEFAULT=${REMOTEHOST}:0.0 OVERRIDE=${DISPLAY}
#
#
#  Now some simple variables
#
#PAGER          DEFAULT=less
#MANPAGER       DEFAULT=less
#LESS           DEFAULT="M q e h15 z23 b80"
#NNTPSERVER     DEFAULT=localhost
#PATH           DEFAULT=${HOME}/bin:/usr/local/bin:/bin\
#:/usr/bin:/usr/local/bin/X11:/usr/bin/X11
#
# silly examples of escaped variables, just to show how they work.
#
#DOLLAR         DEFAULT=\$
#DOLLARDOLLAR   DEFAULT=        OVERRIDE=\$${DOLLAR}
#DOLLARPLUS     DEFAULT=\${REMOTEHOST}${REMOTEHOST}
#ATSIGN         DEFAULT=""      OVERRIDE=\@
#
# set the ICEAUTHORITY file location to allow GNOME to start on encfs $HOME
ICEAUTHORITY DEFAULT=/tmp/.ICEauthority_@{PAM_USER}

/etc/fuse.conf

# Set the maximum number of FUSE mounts allowed to non-root users.
# The default is 1000.
#
#mount_max = 1000

# Allow non-root users to specify the 'allow_other' or 'allow_root'
# mount options.
#
#user_allow_other
user_allow_other

/etc/pam.d/common-session

#
# /etc/pam.d/common-session - session-related modules common to all services
#
# This file is included from other service-specific PAM config files,
# and should contain a list of modules that define tasks to be performed
# at the start and end of sessions of *any* kind (both interactive and
# non-interactive).
#
# As of pam 1.0.1-6, this file is managed by pam-auth-update by default.
# To take advantage of this, it is recommended that you configure any
# local modules either before or after the default block, and use
# pam-auth-update to manage selection of other modules.  See
# pam-auth-update(8) for details.

# here are the per-package modules (the "Primary" block)
session [default=1]                     pam_permit.so
# here's the fallback if no module succeeds
session requisite                       pam_deny.so
# prime the stack with a positive return value if there isn't one already;
# this avoids us returning an error just because nothing sets a success code
# since the modules above will each just jump around
session required                        pam_permit.so
# and here are more per-package modules (the "Additional" block)
session required        pam_encfs.so 
session required        pam_unix.so 
session optional        pam_mount.so 
session optional                        pam_ck_connector.so nox11
# end of pam-auth-update config

/etc/pam.d/common-auth

#
# /etc/pam.d/common-auth - authentication settings common to all services
#
# This file is included from other service-specific PAM config files,
# and should contain a list of the authentication modules that define
# the central authentication scheme for use on the system
# (e.g., /etc/shadow, LDAP, Kerberos, etc.).  The default is to use the
# traditional Unix authentication mechanisms.
#
# As of pam 1.0.1-6, this file is managed by pam-auth-update by default.
# To take advantage of this, it is recommended that you configure any
# local modules either before or after the default block, and use
# pam-auth-update to manage selection of other modules.  See
# pam-auth-update(8) for details.

auth    sufficient      pam_encfs.so
auth    [success=1 default=ignore]      pam_unix.so use_first_pass nullok_secure
#auth   required        pam_unix.so use_first_pass nullok_secure
#auth   required        pam_unix.so nullok_secure use_first_pass
# here are the per-package modules (the "Primary" block)
#auth   [success=1 default=ignore]      pam_unix.so nullok_secure
# here's the fallback if no module succeeds
auth    requisite                       pam_deny.so
# prime the stack with a positive return value if there isn't one already;
# this avoids us returning an error just because nothing sets a success code
# since the modules above will each just jump around
auth    required                        pam_permit.so
# and here are more per-package modules (the "Additional" block)
auth    optional        pam_mount.so 
# end of pam-auth-update config

Create the encrypted Home folder

The configuration will setup encfs for a new "real" user.

Now create your "real" user and ensure they're added to the fuse group.

adduser username
usermod -aG fuse username

Move their home directory/files out of the way

mkdir /var/backups/username_home
mv /home/username/.* /var/backups/username_home/.
rmdir /home/username
mkdir -p /home/.encfs/username /home/username
chown username.username /home/.encfs/username /home/username

Create encrypted folder (Problems with Permissions? See next section)

encfs -v /home/.encfs/username /home/username

Accept default options, or tinker with the encryption settings. Paranoia mode works, but won't support hard links. The author has used paranoid mode successfully.

{i} SET THE PASSWORD TO BE THE SAME AS YOUR LOGIN PASSWORD

Check the encrypted filesystem is mounted correctly

mount

This should output something like the following

encfs on /home/username type fuse.encfs (rw,nosuid,nodev,default_permissions)

Move the home directory/files back

mv /var/backups/username_home/* /home/username/.

REBOOT

You will need to reboot to reload all the PAM libraries, etc.

Now you can test with your 'real' user. If you have a problem, then you may need to login as the dummy user to resolve any issues. If this all works as expected, then simply comment out the dummy user from the password file and the shadow password file.

To prove you are encrypting writes to your home directory, create a junk file in your home directory and see if there is a corresponding file in the encrypted home directory. Get the time stamp for the file, and check for files/directories created in the encrypted home directory (e.g. /home/.encfs/username)

cp /etc/passwd $HOME/.
ls -l /home/username/passwd
ls -ltra /home/.encfs/username

Potential issues

If things go wrong here you can be stuck with a system you cannot log in. You must test it before doing anything else. First, check you can run encfs /home/.encfs/username /home/username as a normal user (not sudo), as well as /bin/fusermount -u /home/username. If it does, use Ctrl-Alt-F2 to change to another VT and see if you can log in without any problems (make sure you're in the encrypted home directory!). Use Ctrl-Alt-F7 to go back to Gnome and test it there.

If you're extra-paranoid, consider adding another user and test it with that user first (you'll have to manually add him to the admin and fuse groups).

OK, if you're sure it works, you can reboot. If it does, log in and delete your username.original folder.

Permissions

If you get problems with permission, you might want to check this:

ref: http://wiki.geteasypeasy.com/Transparent_Encryption_for_home_folder