summaryrefslogtreecommitdiff
path: root/cryptfs.bin.in
blob: d9a358f93014bc004337dfe0a1b217111ac14188 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/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

extract_ssh_host() {
  {
    printf '%s\n' "$1"
    if [ -f ~/.ssh/config ]; then
      sed '
        /^Host\s\+'"$1"'$/,/^Host\s/ {
          s/^\s*Hostname\s\+//
          t
        }
        d
      ' ~/.ssh/config
    fi
  } \
    | tail -n1
}

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
}

raid_device_to_disk() {
  printf '%s\n' "${mdadm_conf}" | \
    grep -F " $1 " | \
    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/ $//
    '
)
mdadm_conf=$(
  sed '
    s|^\s*#CONF_PREFIXES#\s*||
    /^\s*#/d
  ' #ETCDIR#/mdadm.conf | \
    sed -n '
      /^ARRAY / {
        N
        s,^ARRAY \(\S\+\) [^\n]\+\n\s*devices=\(\S\+\)$,\1 \2 ,
        y/,/ /
        s|UUID=|#UUIDDIR#/|
        p
      }
    ' | \
    sort -u
)

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>'
      )
      host=$(extract_ssh_host "${host}")
      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 key
  local max_tries
  local name
  local raw
  name="$1"
  raw="$2"
  key="$3"
  shift 3
  if ! is_unlocked "${name}"; then
    max_tries=10
    while ! cryptsetup luksOpen "${raw}" "${name}" --key-file="${key}" "$@"; do
      sleep 1
      max_tries=$((max_tries-1))
      if [ ${max_tries} -le 0 ]; then
        exit 1
      fi
    done
  fi
}

do_integrity_close() {
  if is_unlocked "$1"; then
    while ! integritysetup close "$1" && ! dmsetup remove "$1"; do
      sleep 1
    done
  fi
}

do_integrity_open() {
  local name
  local raw
  name="$1"
  raw="$2"
  if ! is_unlocked "${name}"; then
    max_tries=10
    while ! integritysetup open "${raw}" "${name}"; do
      sleep 1
      max_tries=$((max_tries-1))
      if [ ${max_tries} -le 0 ]; then
        exit 1
      fi
    done
  fi
}

do_mdadm_stop() {
  device="$1"
  if mdadm --detail "${device}" | \
    grep -q '^\s*State : \(active\|clean\)\s*$'; then
    mdadm --stop "${device}"
  fi
}

printf '%s\n' "${crypttab}" | \
  grep -vxF '' | \
  while read -r name raw key options; do
    if [ "x${options}" = 'xro' ]; then
      options='--readonly'
    else
      unset options
    fi
    if [ "${key#\[*]}" != "${key}" ]; then
      key_slot_option="${key%%]*}"
      key_slot_option='--key-slot='"${key_slot_option#\[}"''
      key="${key#\[*]}"
    else
      unset key_slot_option
    fi
    key_mount=$(mountpoint_to_file "${key}")
    raid_device=$(raid_device_to_disk "#MAPDIR#/${name}")
    if [ -n "${raid_device}" ]; then
      mount_order=$(mountorder_to_device "${raid_device}")
    else
      mount_order=$(mountorder_to_device "#MAPDIR#/${name}")
    fi
    if ${unmount}; then
      if [ -n "${mount_order}" ]; then
        printf '0 do_unmount %s\n' "${mount_order}"
      fi
      if [ -n "${raid_device}" ]; then
        printf '1 do_mdadm_stop %s\n' "${raid_device}"
      fi
      if [ -n "${key}" ]; then
        printf '3 do_crypt_close %s\n' "${name}"
      else
        printf '2 do_integrity_close %s\n' "${name}"
      fi
    else
      if [ -n "${key}" ]; then
        printf '0 do_mount %s\n' "${key_mount}"
        printf '1 do_crypt_open %s\n' "${name} ${raw} ${key} ${key_slot_option} ${options}"
        printf '2 do_unmount -l %s\n' "${key_mount}"
      else
        printf '3 do_integrity_open %s\n' "${name} ${raw}"
      fi
      if [ -n "${mount_order}" ]; then
        printf '4 do_mount %s\n' "${mount_order}"
      fi
    fi
  done | \
  sort -u | \
  sort -k1n,1 | \
  while read -r _ aktion params; do
    ${aktion} ${params}
  done

# End of file