File: cryptdisks.functions

package info (click to toggle)
cryptsetup 2%3A1.0.4%2Bsvn26-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,572 kB
  • ctags: 418
  • sloc: sh: 10,706; ansic: 3,187; makefile: 329; python: 90; perl: 40; sed: 16
file content (458 lines) | stat: -rw-r--r-- 9,560 bytes parent folder | download
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
CRYPTCMD=/sbin/cryptsetup
DEVMAPCMD=/sbin/dmsetup
LOSETUP=/sbin/losetup
TABFILE=/etc/crypttab
MAPPER=/dev/mapper

MOUNT="${CRYPTDISKS_MOUNT}"

test -x $CRYPTCMD  || exit 0
test -x $DEVMAPCMD || exit 0
test -f $TABFILE   || exit 0


parse_opts () {
	local opts="$1"

	LOUD=""
	PARAMS=""
	CHECK=""
	CHECKARGS=""
	PRECHECK=""
	TRIES="3"
	MAKETMP=""
	MAKESWAP=""
	USELUKS=""
	TIMEOUT=""
	KEYSCRIPT=""

	# Parse the options field, convert to cryptsetup parameters
	# and construct the command line
	while test "x$opts" != "x" ; do
		ARG=`echo $opts | sed "s/,.*//"`
		opts=${opts##$ARG}
		opts=${opts##,}
		PARAM=`echo $ARG | sed "s/=.*//"`
		
		case "$ARG" in
		  *=*)
		  	PARAM=`echo $ARG | sed "s/=.*//"`
		  	VALUE=${ARG##$PARAM=}
		  	;;
		  *)
		  	PARAM=$ARG
		  	VALUE=""
			;;
		esac
		
		case "$PARAM" in 
		  readonly)
		  	PARAMS="$PARAMS -r"
		  	;;
		  cipher)
		  	PARAMS="$PARAMS -c $VALUE"
		  	if test "x$VALUE" = "x" ; then
		  		echo "no value for cipher option, skipping" >&2
		  		return 1
		  	fi
		  	;;
		  size)
		  	PARAMS="$PARAMS -s $VALUE"
		  	if test "x$VALUE" = "x" ; then
		  		echo "no value for size option, skipping" >&2
		  		return 1
		  	fi
		  	;;
		  hash)
		  	PARAMS="$PARAMS -h $VALUE"
		  	if test "x$VALUE" = "x" ; then
		  		echo "no value for hash option, skipping" >&2
		  		return 1
		  	fi
		  	;;
		  verify)
		  	PARAMS="$PARAMS -y"
		  	;;
		  check)
		  	if test "x$VALUE" = "x" ; then
				CHECK="${CRYPTDISKS_CHECK}"
			else
		  		CHECK="$VALUE"
		  	fi
		  	if [ "$CHECK" -a -x /lib/cryptsetup/checks/"$CHECK" ]; then
		   		CHECK="/lib/cryptsetup/checks/$CHECK"
		  	#else
		  	#	CHECK=""
		  	fi
		  	;;
		  checkargs)
		  	if test "x$VALUE" != "x"; then
				CHECKARGS="$VALUE"
			fi
			;;
		  precheck)
		  	if test "x$VALUE" = "x" ; then
				PRECHECK="${CRYPTDISKS_PRECHECK}"
			else
		  		PRECHECK="$VALUE"
		  	fi
		   	if [ "$PRECHECK" -a -x /lib/cryptsetup/checks/"$PRECHECK" ]; then
		  		PRECHECK="/lib/cryptsetup/checks/$PRECHECK"
		  	else
		  		PRECHECK=""
		  	fi
		  	;;
		  tries)
		  	if test "x$VALUE" != "x" ; then
		  		TRIES="$VALUE"
		  	fi
		  	case "$TRIES" in
		  	  [0-9]*)
		  	  	;;
		  	  *)
			  	echo "option TRIES is wrongly set to $TRIES - forced to '3' " >&2
			  	TRIES="3"
			  	;;
			esac
		  	PARAMS="$PARAMS --tries=$TRIES"
		  	;;
		  timeout)
		  	if test "x$VALUE" = "x" ; then
				TIMEOUT="${CRYPTDISKS_TIMEOUT}"
			else
		  		TIMEOUT="$VALUE"
		  	fi
			case "$TIMEOUT" in
			  [0-9]*)
			  	;;
			  *)
			  	echo "option TIMEOUT is wrongly set to $TIMEOUT - forced to '0' " >&2
				TIMEOUT="0"
				;;
			esac
		  	PARAMS="$PARAMS --timeout=$TIMEOUT"
		  	;;
		  swap)
		  	MAKESWAP=yes
			SWCHECK="/lib/cryptsetup/checks/un_vol_id"
			SWCHECKARGS="swap"
		  	;;
		  tmp)
		  	MAKETMP=yes
		  	;;
		  luks)
		  	USELUKS=yes
			;;
		  loud)
		  	LOUD=yes
			;;
		  ssl)
			if [ -n "$KEYSCRIPT" ]; then
				echo "multiple key decryption options are not allowed together, skipping" >&2
				return 1
			fi
			KEYSCRIPT="/lib/cryptsetup/scripts/decrypt_old_ssl"
			;;
		  gpg)
			if [ -n "$KEYSCRIPT" ]; then
				echo "multiple key decryption options are not allowed together, skipping" >&2
				return 1
			fi
			KEYSCRIPT="/lib/cryptsetup/scripts/decrypt_gpg"
			;;
		  keyscript)
			if [ -n "$KEYSCRIPT" ]; then
				echo "multiple key decryption options are not allowed together, skipping" >&2
				return 1
			elif test "x$VALUE" = "x" ; then
		  		echo "no value for keyscript option, skipping" >&2
		  		return 1
		  	fi
			KEYSCRIPT="$VALUE"
		  	;;
		esac
	done

	return 0
}

lo_setup () {
	local LOOP_ID=""

	# Set up loopback devices
	if [ ! -f "$src" ]; then
		return 0
	fi

	if [ ! -d "/sys/block/loop0" ]; then
		modprobe loop || return 1
	fi

	for i in 0 1 2 3 4 5 6 7 ; do
		if [ "$(cat /sys/block/loop$i/size)" -eq 0 ]; then
			LOOP_ID=$i
			break
		fi
	done

	if [ -z "$LOOP_ID" ]; then
		return 1
	fi

	$LOSETUP /dev/loop$LOOP_ID $src || return 1
	src=/dev/loop$LOOP_ID
	return 0
}

check_key () {
	# If the keyscript option is set, the "key" is just an argument to
	# the keyscript and not necessarily a file
	if [ -n "$KEYSCRIPT" ]; then
		return 0
	fi

	if [ -z "$key" ] || [ "$key" = "none" ]; then
		INTERACTIVE="yes"
		echo ""
		return 0
	fi

	if [ ! -e "$key" ]; then
		echo "Keyfile for '$dst' not found, skipping" >&2
		return 1
	fi

	# FIXME: Use stat here instead
	MODE=`ls -l $key | sed 's/^....\(......\).*/\1/'`
	OWNER=`ls -l $key | sed 's/^[^ ]* *[^ ]* *\([^ ]*\).*/\1/'`

	# LUKS requires a persistent key, /dev/*random is not supported
	if [ "$USELUKS" = "yes" ] && [ "$key" != "${key%random}" ]; then
		echo "LUKS does not work with random data as key, skipping" >&2
		return 1
	fi

	if [ "$key" = "${key%random}" ] && [ "$MODE" != "------" ]; then
		echo "INSECURE MODE FOR $key, see /usr/share/doc/cryptsetup/README.Debian." >&2
		# don't break here for now
		# return 1
	fi

	if [ "$OWNER" != "root" ]; then
		echo "INSECURE OWNER FOR $key, see /usr/share/doc/cryptsetup/README.Debian." >&2
		# don't break here for now
		# return 1
	fi

	INTERACTIVE="no"
	return 0
}

do_luks () {
	if ! $CRYPTCMD isLuks $src >/dev/null 2>&1; then
		echo "Device '$src' is not a LUKS partition, skipping" >&2
		return 1
	fi

	if test "x$KEYSCRIPT" != "x" ; then
		PARAMS="$PARAMS --key-file=-"
		$KEYSCRIPT $key <&1 | $CRYPTCMD $PARAMS luksOpen $src $dst
	else
		if test "x$INTERACTIVE" != "xyes" ; then
			PARAMS="$PARAMS --key-file=$key"
		fi
		$CRYPTCMD $PARAMS luksOpen $src $dst <&1
	fi

	if [ $? != 0 ]; then
		return 1
	fi

	if [ "$CHECK" != ""  ] && ! $CHECK $MAPPER/$dst $CHECKARGS; then
		echo "the check for '$MAPPER/$dst' failed" >&2
		echo "removing the crypto device $dst" >&2
		$CRYPTCMD luksClose $dst
		return 1
	fi

	return 0
}

do_noluks () {
	if [ "$PRECHECK" = "" ]; then
		PRECHECK="/lib/cryptsetup/checks/un_vol_id"
	fi

	if pre_out=$($PRECHECK $src); then
		run=1
	elif test "x$MAKESWAP" = "xyes" && /lib/cryptsetup/checks/vol_id $src swap>/dev/null; then
		run=1
	else
		run=0
		echo "$pre_out" >&2
		echo "the precheck for '$src' failed, skipping" >&2
		return 1
	fi
	
	if test "x$KEYSCRIPT" != "x" ; then
		PARAMS="$PARAMS --key-file=-"
		$KEYSCRIPT $key <&1 | $CRYPTCMD $PARAMS create $dst $src
	else
		if test "x$INTERACTIVE" != "xyes" ; then
			PARAMS="$PARAMS --key-file=$key"
		fi
		$CRYPTCMD $PARAMS create $dst $src <&1
	fi

	if [ $? != 0 ] ; then
		return 1
	fi


	if [ "$CHECK" != ""  ] && ! $CHECK $MAPPER/$dst $CHECKARGS; then
		echo "the check for '$MAPPER/$dst' failed - maybe the password is wrong" >&2
		echo "removing the crypto device $dst" >&2
		$CRYPTCMD remove $dst
		return 1
	fi

	return 0
}

mount_fs () {
	for point in $MOUNT; do
		MOUNT_YES="yes"
		mount $point 2>/dev/null 1>&2 || MOUNT_YES="no"
	done
}

umount_fs () {
	if [ "x$MOUNT_YES" = "xyes" ]; then
		for point in $MOUNT; do
			umount $point
		done
	fi
}

do_swap () {
	if test "x$MAKESWAP" = "xyes" && test -b $MAPPER/$dst; then
		if swap_out=$(/lib/cryptsetup/checks/un_vol_id $MAPPER/$dst) || \
				/lib/cryptsetup/checks/vol_id $MAPPER/$dst swap>/dev/null; then
			mkswap $MAPPER/$dst 2>/dev/null >/dev/null
		else
			echo "$swap_out" >&2
			echo "the check for '$MAPPER/$dst' failed. $MAPPER/$dst contains data." >&2
			echo "removing the crypto device $dst" >&2
			do_close
		fi
	fi
}

do_tmp () {
	if test "x$MAKETMP" = "xyes" && test -b $MAPPER/$dst; then
		mke2fs $MAPPER/$dst >/dev/null
		mount -t ext2 $MAPPER/$dst /tmp
		chmod 1777 /tmp
		umount /tmp
	fi
}

do_close () {
	if echo $opts | grep -q "luks"; then
		$CRYPTCMD luksClose $dst
	else
		$CRYPTCMD remove $dst
	fi
}

do_start () {
	modprobe -q dm-crypt || true
	$DEVMAPCMD mknodes
	log_action_begin_msg "Starting $INITSTATE crypto disks"
	mount_fs
	egrep -v "^[[:space:]]*(#|$)" $TABFILE | while read dst src key opts; do

		# Make sure that all fields are present
		if [ "x$dst" = "x" ]; then
			continue
		elif [ -z "$src" ] || [ -z "$key" ] || [ -z "$opts" ]; then
			log_progress_msg "$dst(skipped, missing parameters)"
			continue
		fi

		# Make sure source device exists
		if [ ! -r "$src" ]; then
			if [ "x$LOUD" = "xyes" ]; then
				log_progress_msg "$dst(skipped, device $src does not exist)"
			fi
			continue
		fi

		# Make sure that target device doesn't exist
		if [ -b $MAPPER/$dst ]; then
			log_progress_msg "$dst(running)"
			continue
		fi

		# All checks passed, do the real setup
		log_progress_msg "$dst(starting)"

		parse_opts "$opts" || continue
		check_key || continue
		lo_setup || continue

		if test "x$USELUKS" = "xyes" ; then
			do_luks
		else
			do_noluks
		fi

		if [ $? -ne 0 ]; then
			log_progress_msg "$dst(failed)"
		else
			do_swap
			do_tmp
		fi
	done
	
	umount_fs
	log_action_end_msg 0
}

do_stop () {
	$DEVMAPCMD mknodes
	log_action_begin_msg "Stopping $INITSTATE crypto disks"
	egrep -v "^[[:space:]]*(#|$)" $TABFILE | while read dst src key opts; do
		if test -b $MAPPER/$dst; then
			if $DEVMAPCMD info $dst | grep -q '^Open count: *0$'; then
				dev=`$DEVMAPCMD table $dst | sed 's/^.* \([0-9]*:[0-9]*\) .*/\1/'`
				major=`echo $dev | sed 's/:.*//'`
				minor=`echo $dev | sed 's/.*://'`

				log_progress_msg "$dst(stopping)"
				do_close

				# Detach loopback device, if attached
				if test -f $src -a $major -eq 7; then
					$LOSETUP -d /dev/loop$minor
				fi
			else
				log_progress_msg "$dst(busy)"
			fi
		else
			log_progress_msg "$dst(stopped)"
		fi
	done

	log_action_end_msg 0
}

device_msg () {
	local dst msg
	dst="$1"
	msg="$2"

	if [ "$VERBOSE" != "no" ]; then
		log_action_cont_msg "$dst ($msg)"
	fi
}