summaryrefslogtreecommitdiff
path: root/cryptfs.bin.in
blob: 435058f02eed4e49f3b27f312b9e8785a5fd0f84 (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
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
#!/bin/bash
#
# #BINDIR#/cryptfs: mount/umount encrypted partitions
#

set -e

if [ $# -eq 1 ] && [ "x$1" = 'x-u' ]; then
  unmount=true
elif [ $# -eq 0 ]; then
  unmount=false
else
  >&2 printf 'Usage: %s [-u]\n' "$0"
  exit 1
fi

give_all_parent_dirs() {
  local odir
  local dir
  odir="$1"
  odir="/${odir#/}"
  dir="${odir%/*}"
  while [ "${dir}" != '' ]; do
    printf '%s %s\n' "${odir}" "${dir}"
    dir="${dir%/*}"
  done
}

inner_fs_to_mountorder() {
  {
    printf '%s\n' "${fstab}" | \
      grep "^$1 " | \
      awk '{print "x " $3}'
    printf '%s\n' "${fstab}" | \
      sed -n '
        s|^\(\S\+\) \S\+ \(\S\+\)\( .*\)\?$|\1 \2|
        T
        p
      ' | \
      while read -r order dir; do
        give_all_parent_dirs "${dir}" | \
          awk '{print "'"${order}"' " $1 " " $2}'
      done | \
        sort -k3,3 -k2r,2 | \
        uniq -f2 | \
        cut -d' ' -f1,3
  } | \
    sort -k2r,2 -k1,1 | \
    uniq -f1 -d | \
    cut -d' ' -f1
}

mountpoint_to_file() {
  {
    printf '%s\n' "${fstab}" | \
      awk '{print $1 " " $3}'
    give_all_parent_dirs "$1" | \
      awk '{print "x " $2}'
  } | \
    sort -k2r,2 -k1,1 | \
    uniq -f1 -d | \
    cut -d' ' -f1 | \
    head -n1
}

mountorder_to_device() {
  {
    printf 'x %s\n' "$1"
    printf '%s\n' "${fstab}" | \
      awk '{print $1 " " $2}' | \
      sort -k2,2 -u
  } | \
    sort -k2,2 -k1,1 | \
    uniq -f1 -d | \
    cut -d' ' -f1
}

crypttab=$(
  sed '
    s|^\s*#CONF_PREFIXES#\s*||
    /^\s*#/d
    /^\s*$/d
    s/\s\+/ /g
    s|UUID=|#UUIDDIR#/|g
    s/^ //
  ' '#ETCDIR#/crypttab' | \
    sort -u
)
fstab=$(
  sed '
    s|^\s*#CONF_PREFIXES#\s*||
    /^\s*#/d
    /^\s*\S\+\s\+[^\/[:space:]]/d
    /^\s*$/d
    s/\s\+/ /g
  ' '#ETCDIR#/fstab' | \
    sort -u | \
    cat -n | \
  sed '
    s|UUID=|#UUIDDIR#/|
    s/\s\+/ /g
    s/^ //
    s/ $//
  '
)

mount_details() {
  printf '%s\n' "${fstab}" | \
    grep "^$2 " | \
    while read -r _ source dest fs opts _; do
      case "$1" in
        'mp')
          printf '%s\n' "${dest}"
        ;;
        'dev')
          printf '%s\n' "${source}"
        ;;
        'fs')
          printf '%s\n' "${fs}"
        ;;
        'all')
          printf -- '-t %s ' "${fs}"
          if [ -n "${opts}" ] && \
            [ "${opts}" != 'defaults' ]; then
            printf -- '-o %s ' "${opts}"
          fi
          printf '%s %s\n' "${source}" "${dest}"
        ;;
        *)
          >&2 printf 'unknown mount_details "%s"\n' "$1"
          exit 42
        ;;
      esac
    done
}

is_mounted() {
  mountpoint -q "$(mount_details mp "$1")"
}

is_unlocked() {
  cryptsetup status "$1" >/dev/null 2>&1
}

do_mount() {
  local inner_fs
  local max_tries
  local mount_paramters
  if is_mounted "$1"; then
    return
  fi
  inner_fs=$(inner_fs_to_mountorder "$1")

  case "$(mount_details fs "$1")" in
    'ext'*)
      e2fsck -p $(mount_details dev "$1")
    ;;
    'fuse.sshfs'|'fuse.ftps')
      local max_wait
      local host
      max_wait=$(($(date +%s)+120))
      host=$(
        mount_details dev "$1" | \
          sed 's>^\([^:@]*@\)\?\([^:@]\+\):\S*$>\2>'
      )
      while [ $(date +%s) -lt ${max_wait} ] && \
        ! #PING6# -c1 "${host}" >/dev/null 2>&1 && \
        ! ping -c1 "${host}" >/dev/null 2>&1; do
        >&2 printf ','
        sleep 1
      done
    ;;
  esac

  if [ -n "${inner_fs}" ]; then
    do_unmount -l "${inner_fs}"
  fi
  max_tries=10
  mount_parameters=$(mount_details all "$1")
  while ! mount ${mount_parameters}; do
    max_tries=$((max_tries-1))
    if [ ${max_tries} -le 0 ]; then
      exit 1
    fi
    sleep 1
  done
  if [ -n "${inner_fs}" ]; then
    do_mount "${inner_fs}"
  fi
}

do_unmount() {
  local inner_fs
  local lazy
  local mount_point
  if [ "x$1" = 'x-l' ]; then
    lazy='-l'
    shift
  else
    lazy=''
  fi
  if ! is_mounted "$1"; then
    return
  fi
  inner_fs=$(inner_fs_to_mountorder "$1")
  if [ -n "${inner_fs}" ]; then
    do_unmount -l "${inner_fs}"
  fi
  mount_point=$(mount_details mp "$1")
  while ! umount ${lazy} "${mount_point}"; do
    fuser -k -M -m "${mount_point}"
    sleep 1
  done
  if [ -n "${inner_fs}" ]; then
    do_mount "${inner_fs}"
  fi
}

do_crypt_close() {
  if is_unlocked "$1"; then
    while ! cryptsetup luksClose "$1"; do
      sleep 1
    done
  fi
}

do_crypt_open() {
  local max_tries
  if ! is_unlocked "$1"; then
    max_tries=10
    while ! cryptsetup luksOpen "$2" "$1" --key-file="$3"; do
      sleep 1
      max_tries=$((max_tries-1))
      if [ ${max_tries} -le 0 ]; then
        exit 1
      fi
    done
  fi
}

printf '%s\n' "${crypttab}" | \
  grep -vxF '' | \
  while read -r name raw key; do
    key="${key%% *}"
    key_mount=$(mountpoint_to_file "${key}")
    mount_order=$(mountorder_to_device "#MAPDIR#/${name}")

    if ${unmount}; then
      do_unmount "${mount_order}"
      do_crypt_close "${name}"
    else
      do_mount "${key_mount}"
      do_crypt_open "${name}" "${raw}" "${key}"
      do_unmount -l "${key_mount}"
      do_mount "${mount_order}"
    fi
  done

# End of file