~ecryptfs/ecryptfs/trunk

« back to all changes in this revision

Viewing changes to src/utils/ecryptfs-migrate-home

  • Committer: Dustin Kirkland
  • Date: 2009-02-13 15:57:24 UTC
  • Revision ID: kirkland@canonical.com-20090213155724-1q3qz2o0cbyimu9x
debian/ubuntu packaging

Initial checkin of the Debian/Ubuntu packaging

Signed-off-by: Dustin Kirkland <kirkland@canonical.com>

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/bin/sh
2
 
# -*- sh-basic-offset: 4; sh-indentation: 4; tab-width: 4; indent-tabs-mode: t; sh-indent-comment: t; -*-
3
 
# This script encrypts an user's home
4
 
#
5
 
# Written by Yan Li <yan.i.li@intel.com>, <yanli@gnome.org>
6
 
# Copyright (C) 2010 Intel Corporation
7
 
#
8
 
# Modified by Dustin Kirkland <kirkland@ubuntu.com>
9
 
#
10
 
# This program is free software; you can redistribute it and/or
11
 
# modify it under the terms of the GNU General Public License as
12
 
# published by the Free Software Foundation; either version 2 of the
13
 
# License, or (at your option) any later version.
14
 
#
15
 
# This program is distributed in the hope that it will be useful, but
16
 
# WITHOUT ANY WARRANTY; without even the implied warranty of
17
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18
 
# General Public License for more details.
19
 
#
20
 
# You should have received a copy of the GNU General Public License
21
 
# along with this program; if not, write to the Free Software
22
 
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23
 
# 02111-1307, USA.
24
 
 
25
 
set -e
26
 
 
27
 
PRIVATE_DIR="Private"
28
 
 
29
 
usage() {
30
 
       echo "
31
 
Usage:
32
 
 
33
 
$0 -u USER
34
 
 
35
 
 -u,--user       Migrate USER's home directory to an encrypted home directory
36
 
 
37
 
WARNING: Make a complete backup copy of the non-encrypted data to
38
 
another system or external media. This script is dangerous and, in
39
 
case of an error, could result in data lost, or lock you out of your
40
 
system!
41
 
 
42
 
This program must be executed by root.
43
 
 
44
 
"
45
 
        exit 1
46
 
}
47
 
 
48
 
error() {
49
 
        echo "$(gettext 'ERROR: ')" "$@" 1>&2
50
 
        exit 1
51
 
}
52
 
 
53
 
warning() {
54
 
        echo "$(gettext 'WARNING: ')" "$@" 1>&2
55
 
}
56
 
 
57
 
info() {
58
 
        echo "$(gettext 'INFO: ')" "$@" 1>&2
59
 
}
60
 
 
61
 
assert_dir_empty() {
62
 
        local DIR="$1"
63
 
        if [ -e "$DIR" ]; then
64
 
                # if $DIR is a directory, make sure it's empty
65
 
                if [ -d "$DIR" ]; then
66
 
                        ls=$(ls -A "$DIR" | wc -l)
67
 
                        if [ "$ls" != "0" ]; then
68
 
                                echo 1>&2 "If you already have some data in directory $DIR,"
69
 
                                echo 1>&2 "please move all of these files and directories out of the way, and"
70
 
                                echo 1>&2 "follow the instructions in:"
71
 
                                echo 1>&2 "    ecryptfs-setup-private --undo"
72
 
                                echo 1>&2 
73
 
                                error "$DIR is not empty, cannot continue."
74
 
                        fi
75
 
                else
76
 
                        error "$DIR exists but is not an empty directory, cannot continue."
77
 
                fi
78
 
        fi
79
 
}
80
 
 
81
 
# get user home by username
82
 
get_user_home () {
83
 
        local USER_NAME="$1"
84
 
        local USER_HOME=$(getent passwd "$USER_NAME" | cut -d":" -f 6)
85
 
        if [ -z "$USER_HOME" ]; then
86
 
                error "Cannot find the home directory of $USER_NAME."
87
 
        fi
88
 
        echo "$USER_HOME"
89
 
}
90
 
 
91
 
sanity_check () {
92
 
        local USER_NAME="$1"
93
 
        local USER_HOME="$2"
94
 
        if [ -e "$USER_HOME/.ecryptfs" ]; then
95
 
                error "$USER_HOME appears to be encrypted already."
96
 
        fi
97
 
        # Check for rsync
98
 
        if ! which rsync >/dev/null 2>&1; then
99
 
                error "Please install the rsync package."
100
 
        fi
101
 
        # Check free space: make sure we have sufficient disk space
102
 
        # available. To make a full copy, we will need at least 2.5x the
103
 
        # disk usage of the target home directory.
104
 
        info "Checking disk space, this may take a few moments.  Please be patient."
105
 
        needed=$(du -s "$USER_HOME" | awk '{printf "%.0f", $1*2.5}')
106
 
        free=$(df -P "$USER_HOME" | tail -n 1 | awk '{print $4}')
107
 
        if [ $needed -gt $free ]; then
108
 
                info "2.5x the size your current home directory is required to perform a migration."
109
 
                info "Once the migration succeeds, you may recover most of this space by deleting the cleartext directory."
110
 
                error "Not enough free disk space."
111
 
        fi
112
 
        assert_dir_empty "$USER_HOME/.$PRIVATE_DIR"
113
 
        assert_dir_empty "$USER_HOME/.ecryptfs"
114
 
        assert_dir_empty "/home/.ecryptfs/$USER_NAME"
115
 
}
116
 
 
117
 
encrypt_dir () {
118
 
        local USER_NAME="$1"
119
 
        local USER_HOME="$2"
120
 
        if ! which lsof >/dev/null 2>&1; then
121
 
                info "Please install lsof."
122
 
                error "Can not tell whether $USER_HOME is in use or not."
123
 
        fi
124
 
        info "Checking for open files in $USER_HOME"
125
 
        lsof=$(lsof +D "$USER_HOME" | wc -l)
126
 
        if [ "$lsof" != "0" ]; then
127
 
                info "The following files are in use:"
128
 
                echo
129
 
                lsof +D "$USER_HOME" | sed "s/^/    /"
130
 
                echo
131
 
                error "Cannot proceed."
132
 
        fi
133
 
        # start encryption
134
 
        orig=$(mktemp /home/$USER_NAME.XXXXXXXX)
135
 
        rm "$orig" && mv "$USER_HOME" "$orig"
136
 
        chmod 700 "$orig"
137
 
        mkdir -p -m 700 "$USER_HOME"
138
 
        USER_GROUP=$(id -g "$USER_NAME")
139
 
        chown "$USER_NAME:$USER_GROUP" "$USER_HOME" "$orig"
140
 
        ECRYPTFS_SETUP_PRIVATE_ARGS=""
141
 
        if [ -n "$LOGINPASS" ]; then
142
 
                ECRYPTFS_SETUP_PRIVATE_ARGS="-l $LOGINPASS"
143
 
        fi
144
 
        if [ -n "$MOUNTPASS" ]; then
145
 
                ECRYPTFS_SETUP_PRIVATE_ARGS="$ECRYPTFS_SETUP_PRIVATE_ARGS -m $MOUNTPASS"
146
 
        fi
147
 
        export ECRYPTFS_MIGRATE="1"
148
 
        if ! ecryptfs-setup-private -u "$USER_NAME" -b $ECRYPTFS_SETUP_PRIVATE_ARGS; then
149
 
                # too bad, something went wrong, we'll try to recover
150
 
                rm -rf "$USER_HOME"
151
 
                mv "$orig" "$USER_HOME"
152
 
                exit 1
153
 
        fi
154
 
        info "Encrypted home has been set up, encrypting files now...this may take a while."
155
 
        # Show progress, but on stderr, in case the user wants to filter that out
156
 
        rsync -aP "$orig/" "$USER_HOME/" 1>&2
157
 
        umount "$USER_HOME/"
158
 
        echo
159
 
        echo "========================================================================"
160
 
        echo "Some Important Notes!"
161
 
        echo
162
 
        echo " 1. The file encryption appears to have completed successfully, however,"
163
 
        echo "    $USER_NAME MUST LOGIN IMMEDIATELY, _BEFORE_THE_NEXT_REBOOT_,"
164
 
        echo "    TO COMPLETE THE MIGRATION!!!"
165
 
        echo
166
 
        echo " 2. If $USER_NAME can log in and read and write their files, then the migration is complete,"
167
 
        echo "    and you should remove $orig."
168
 
        echo "    Otherwise, restore $orig back to $USER_HOME."
169
 
        echo
170
 
        echo " 3. $USER_NAME should also run 'ecryptfs-unwrap-passphrase' and record"
171
 
        echo "    their randomly generated mount passphrase as soon as possible."
172
 
        echo
173
 
        echo " 4. To ensure the integrity of all encrypted data on this system, you"
174
 
        echo "    should also encrypt swap space with 'ecryptfs-setup-swap'."
175
 
        echo "========================================================================"
176
 
        echo
177
 
}
178
 
 
179
 
DO_ENCRYPT=
180
 
while true; do
181
 
        [ -z "$1" ] && break
182
 
        case "$1" in
183
 
                -u|--user)
184
 
                        DO_ENCRYPT=1
185
 
                        USER_NAME="$2"
186
 
                        shift 2
187
 
                ;;
188
 
                *)
189
 
                        usage
190
 
                ;;
191
 
        esac
192
 
done
193
 
 
194
 
if [ "$DO_ENCRYPT" != "1" ]; then
195
 
        usage
196
 
fi
197
 
 
198
 
if [ "$(id -u)" != "0" ]; then
199
 
        error "This program must be executed with root privileges"
200
 
fi
201
 
 
202
 
if [ "$DO_ENCRYPT" = "1" ]; then
203
 
        USER_HOME=$(get_user_home "$USER_NAME")
204
 
        sanity_check "$USER_NAME" "$USER_HOME"
205
 
        encrypt_dir "$USER_NAME" "$USER_HOME" "$LOGINPASS" "$MOUNTPASS"
206
 
fi