summaryrefslogtreecommitdiff
path: root/cryptfs.in
blob: 9d5b35f9ef76d54815a672a87aaba49f6be4b5f0 (plain)
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
#!/bin/bash
#
# #RCDDIR#/cryptfs: mount/umount encrypted partitions
#

set -o pipefail

cryptTab="$(grep -v "^\s*\(#\|\$\)" #ETCDIR#/crypttab | sed "s/^\s*//")"
fsTab="$(grep -v "^\s*\(#\|\$\)" #ETCDIR#/fstab | sed "s/^\s*//")"

cryptedFss="$(echo "${cryptTab}" | awk '{print "#MAPDIR#/"$1}')"
neededKeyFss="$(
    for fs in $(echo "${fsTab}" | awk '{print $2}' | grep "^/")
    do
        if echo "${cryptTab}" | grep -q "^\S\+\s\+\S\+\s\+${fs}\(/\|\s\)"
        then
            echo "${fs}"
        fi
    done
)"
innermostFss="$(
    for fs in ${cryptedFss}
    do
        echo "${fsTab}" | \
          awk '{print $2}' | \
          grep "^$(
            echo "${fsTab}" | \
            grep "^${fs}\s" | \
            awk '{print $2}'
          )/"
    done
)"

case $1 in

start)

    for neededKeyFs in ${neededKeyFss}
    do
        if ! mountpoint -q "${neededKeyFs}"
        then
            if echo "${fsTab}" | grep -q "^\(ssh\|ftp\)fs#\S*\s\+${neededKeyFs}\s"
            then
                maxWait=120
                while [ ${maxWait} -gt 0 ] && ! /sbin/ip -o addr show scope global | grep -q '\S'
                do
                    >&2 echo -ne "."
                    sleep 1
                    maxWait=$[${maxWait}-1]
                done
            fi
            mount ${neededKeyFs} || exit $?
        fi
    done

    for ((cryptDeviceNum=0; cryptDeviceNum<$(echo "${cryptTab}" | wc -l); cryptDeviceNum++))
    do
        zeile="$(echo "${cryptTab}" | head -n$[${cryptDeviceNum}+1] | tail -n1)"
        name="$(echo "${zeile}" | awk '{print $1}')"
        device="$(echo "${zeile}" | awk '{print $2}')"
        if [[ "${device}" == "UUID="* ]]
        then
            device="#UUIDDIR#/${device#UUID=}"
        fi
        keyfile="$(echo "${zeile}" | awk '{print $3}')"
        if [ ! -b "#MAPDIR#/${name}" ]
        then
            cryptsetup luksOpen "${device}" "${name}" --key-file="${keyfile}" || exit $?
        fi
    done

    for cryptedFs in ${cryptedFss}
    do
        if ! findmnt --source "${cryptedFs}" > /dev/null
        then
            e2fsck "${cryptedFs}"
            mount "${cryptedFs}" || exit $?
        fi
    done

    for innermostFs in ${innermostFss}
    do
        if ! mountpoint -q "${innermostFs}"
        then
            mount "${innermostFs}" || exit $?
        fi
    done

    ;;

stop)

    for innermostFs in ${innermostFss}
    do
        if mountpoint -q "${innermostFs}"
        then
            umount "${innermostFs}" || exit $?
        fi
    done

    for cryptedFs in ${cryptedFss}
    do
        if findmnt --source "${cryptedFs}" > /dev/null
        then
            umount "${cryptedFs}" || exit $?
        fi
    done

    for name in $(echo "${cryptTab}" | awk '{print $1}')
    do
        if [ -b "#MAPDIR#/${name}" ]
        then
            cryptsetup luksClose "${name}" || exit $?
        fi
    done

    for neededKeyFs in ${neededKeyFss}
    do
        if [ ! "${neededKeyFs}" == "/" ] && mountpoint -q "${neededKeyFs}"
        then
            umount ${neededKeyFs} || exit $?
        fi
    done

    ;;

    restart)
    $0 stop
    sleep 2
    $0 start
    ;;

*)
    echo "usage: $0 [start|stop|restart]"
    ;;

esac

# End of file