Skip to content

Commit 26d4677

Browse files
committed
Allow inline_index_update() 'del' to recover index on failure
The original inline-index is kept until the update successfully completes. Otherwise, the original inline-index is put back in place. Signed-off-by: Richard T Bonhomme <[email protected]>
1 parent a162ff9 commit 26d4677

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

easytls

+112-4
Original file line numberDiff line numberDiff line change
@@ -2829,12 +2829,25 @@ inline_index_update ()
28292829

28302830
easytls_verbose " DEL: ${old_record}"
28312831

2832+
# backup old index
2833+
"${EASYTLS_CP}" "${EASYTLS_INLINE_INDEX}" \
2834+
"${EASYTLS_INLINE_INDEX}-deleted" || {
2835+
error_msg "inline_index_update - del: backup old index"
2836+
return 1
2837+
}
2838+
28322839
# Remove old record
28332840
"${EASYTLS_SED}" -i -e "/^${old_record}.*$/d" \
28342841
"${EASYTLS_INLINE_INDEX}" || {
28352842
error_msg "inline_index_update del: Remove old record"
28362843
return 1
28372844
}
2845+
2846+
# Remove backup old index
2847+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}-deleted" || {
2848+
error_msg "inline_index_update - del: Remove backup old index"
2849+
return 1
2850+
}
28382851
;;
28392852
force-del)
28402853
# Build old record without inline file HASH, with --sub-key-name
@@ -3117,6 +3130,14 @@ remove_inline ()
31173130
die "Failed to remove: ${inline_file}"
31183131
fi
31193132
else
3133+
# Restore original inline-index
3134+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
3135+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
3136+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
3137+
"${EASYTLS_INLINE_INDEX}" || \
3138+
die "remove_inline - Restore original inline-index"
3139+
fi
3140+
31203141
# Undo move
31213142
if [ -z "${force_remove}" ]; then
31223143
"${EASYTLS_MV}" "${inline_file}-deleteme" "${inline_file}" || \
@@ -3207,6 +3228,14 @@ remove_group_inline ()
32073228
die "Failed to remove: ${inline_file}"
32083229
fi
32093230
else
3231+
# Restore original inline-index
3232+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
3233+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
3234+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
3235+
"${EASYTLS_INLINE_INDEX}" || \
3236+
die "remove_inline - Restore original inline-index"
3237+
fi
3238+
32103239
# Undo move
32113240
if [ -z "${force_remove}" ]; then
32123241
"${EASYTLS_MV}" "${inline_file}-deleted" "${inline_file}" || \
@@ -3272,7 +3301,29 @@ remove_metadata ()
32723301
# Remove client from inline index
32733302
known_inline_hash="${verified_inline_hash}"
32743303
inline_serial="$(inline_index_ilhash_to_serial)"
3275-
inline_index_update del || die "Failed to update inline-index"
3304+
if inline_index_update del; then
3305+
: # OK
3306+
else
3307+
# Restore original inline-index
3308+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
3309+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
3310+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
3311+
"${EASYTLS_INLINE_INDEX}" || \
3312+
die "remove_inline - Restore original inline-index"
3313+
fi
3314+
3315+
# Undo move
3316+
if [ -z "${force_remove}" ]; then
3317+
"${EASYTLS_MV}" "${inline_file}-deleted" "${inline_file}" || \
3318+
die "Failed to restore: ${inline_file}"
3319+
else
3320+
"${EASYTLS_MV}" "${inline_file}-badhash" "${inline_file}" || \
3321+
die "Failed to restore: ${inline_file}"
3322+
fi
3323+
# Always die
3324+
die "Failed to update inline-index"
3325+
fi
3326+
32763327
# Reset inline_index_save_hash_block, to add this inline back to the index
32773328
unset -v inline_index_save_hash_block inline_index_verify_hash_block
32783329

@@ -3313,7 +3364,18 @@ remove_metadata ()
33133364
fi
33143365

33153366
# Add client to inline-index
3316-
inline_index_update add || die "Failed to update inline-index"
3367+
if inline_index_update add; then
3368+
: # OK
3369+
else
3370+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
3371+
# Restore original inline-index
3372+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
3373+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
3374+
"${EASYTLS_INLINE_INDEX}"
3375+
die "Failed to update inline-index"
3376+
fi
3377+
fi
3378+
33173379
[ -n "${metadata_updated}" ] && notice "inline file updated"
33183380
easytls_verbose
33193381
} # => remove_metadata ()
@@ -4121,6 +4183,7 @@ inline_tls_crypt_v1 ()
41214183
fi
41224184
fi
41234185

4186+
# share this client FP with server defined by -r=<serv-name> option
41244187
inline_share_fingerprint "${name}" || die "Failed to share fingerprint"
41254188

41264189
notice "Inline TLS crypt file created: ${inline_file}"
@@ -4316,6 +4379,7 @@ inline_tls_crypt_v2 ()
43164379
fi
43174380
fi
43184381

4382+
# share this client FP with server defined by -r=<serv-name> option
43194383
inline_share_fingerprint "${name}" || die "Failed to share fingerprint"
43204384

43214385
notice "Inline TLS crypt v2 ${cert_purpose} file created: ${inline_file}"
@@ -4797,7 +4861,29 @@ inline_share_fingerprint ()
47974861

47984862
# Must unset the usage block
47994863
unset inline_index_save_hash_block
4800-
inline_index_update del || die "Failed to update inline-index"
4864+
if inline_index_update del; then
4865+
: # OK
4866+
else
4867+
# Restore original inline-index
4868+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
4869+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
4870+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
4871+
"${EASYTLS_INLINE_INDEX}" || \
4872+
die "remove_inline - Restore original inline-index"
4873+
fi
4874+
4875+
# Undo move
4876+
if [ -z "${force_remove}" ]; then
4877+
"${EASYTLS_MV}" "${inline_file}-deleted" "${inline_file}" || \
4878+
die "Failed to restore: ${inline_file}"
4879+
else
4880+
"${EASYTLS_MV}" "${inline_file}-badhash" "${inline_file}" || \
4881+
die "Failed to restore: ${inline_file}"
4882+
fi
4883+
# Always die
4884+
die "Failed to update inline-index"
4885+
fi
4886+
48014887
else
48024888
#die "Why is client inline hash missing from index ?"
48034889
# No-CA mode
@@ -4868,7 +4954,29 @@ inline_share_fingerprint ()
48684954

48694955
# Must unset the usage block
48704956
unset inline_index_save_hash_block
4871-
inline_index_update del || die "Failed to update inline-index"
4957+
if inline_index_update del; then
4958+
: # OK
4959+
else
4960+
# Restore original inline-index
4961+
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
4962+
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
4963+
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
4964+
"${EASYTLS_INLINE_INDEX}" || \
4965+
die "remove_inline - Restore original inline-index"
4966+
fi
4967+
4968+
# Undo move
4969+
if [ -z "${force_remove}" ]; then
4970+
"${EASYTLS_MV}" "${inline_file}-deleted" "${inline_file}" || \
4971+
die "Failed to restore: ${inline_file}"
4972+
else
4973+
"${EASYTLS_MV}" "${inline_file}-badhash" "${inline_file}" || \
4974+
die "Failed to restore: ${inline_file}"
4975+
fi
4976+
# Always die
4977+
die "Failed to update inline-index"
4978+
fi
4979+
48724980
else
48734981
#die "Why is server inline hash missing from index ?"
48744982
# No-CA mode

0 commit comments

Comments
 (0)