Skip to content

Commit 6008da1

Browse files
committed
Introduce universal_update()
Allow updates to config, indexes and lists to use a single function. Include file protection measures in the one function. Also make inline_index_update() and tlskey_index_update() use this function. Signed-off-by: Richard T Bonhomme <[email protected]>
1 parent 6fd11b4 commit 6008da1

File tree

1 file changed

+100
-167
lines changed

1 file changed

+100
-167
lines changed

easytls

+100-167
Original file line numberDiff line numberDiff line change
@@ -2809,18 +2809,6 @@ inline_index_update ()
28092809
{
28102810
update_index_action="${1}"
28112811

2812-
# Verify inline-index Hash
2813-
#inline_index_verify_hash || {
2814-
# error_msg "inline-index is corrupt"
2815-
# return 1
2816-
# }
2817-
2818-
# backup old index
2819-
"${EASYTLS_CP}" "${EASYTLS_INLINE_INDEX}" "${EASYTLS_INLINE_INDEX}.tmp" || {
2820-
error_msg "inline_index_update - backup old index"
2821-
return 1
2822-
}
2823-
28242812
if [ "${update_index_action}" = 'del' ] && [ -n "${force_remove}" ]; then
28252813
update_index_action='force-del'
28262814
fi
@@ -2860,64 +2848,31 @@ inline_index_update ()
28602848

28612849
easytls_verbose " ADD: ${new_record}"
28622850

2863-
# Write new record
2864-
"${EASYTLS_PRINTF}" "%s\n" "${new_record}" > "${EASYTLS_TEMP_RECORD}" || {
2865-
error_msg "inline_index_update - add: Write new record"
2866-
return 1
2867-
}
2868-
2869-
# copy old index to temp-list
2870-
"${EASYTLS_CP}" "${EASYTLS_INLINE_INDEX}" "${EASYTLS_TEMP_LIST}" || {
2871-
error_msg "inline_index_update - add: copy old index"
2851+
# universal_update
2852+
if universal_update add "${EASYTLS_INLINE_INDEX}" "${new_record}"; then
2853+
: # ok
2854+
update_master_hash=1
2855+
return 0
2856+
else
2857+
error_msg "inline_index_update - universal_update - add"
28722858
return 1
2873-
}
2874-
2875-
# move old index
2876-
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}" \
2877-
"${EASYTLS_INLINE_INDEX}-deleted" || {
2878-
error_msg "inline_index_update - add: move old index"
2879-
return 1
2880-
}
2881-
2882-
# Append temp record to temp-list and write new index
2883-
"${EASYTLS_CAT}" "${EASYTLS_TEMP_LIST}" "${EASYTLS_TEMP_RECORD}" > \
2884-
"${EASYTLS_INLINE_INDEX}" || {
2885-
error_msg "inline_index_update - add: write new index"
2886-
return 1
2887-
}
2859+
fi
28882860
;;
28892861
del)
28902862
# Note: Inline HASH is unique, regardless of --sub-key-name
28912863
# Identify old record
28922864
old_record="${verified_inline_hash}[[:blank:]]${inline_serial}[[:blank:]]"
28932865

2894-
# Find old record
2895-
"${EASYTLS_GREP}" -q "^${old_record}" "${EASYTLS_INLINE_INDEX}" || {
2896-
error_msg "inline_index_update del: find: ${old_record}"
2897-
return 1
2898-
}
2899-
29002866
easytls_verbose " DEL: ${old_record}"
29012867

2902-
# backup old index
2903-
"${EASYTLS_CP}" "${EASYTLS_INLINE_INDEX}" \
2904-
"${EASYTLS_INLINE_INDEX}-deleted" || {
2905-
error_msg "inline_index_update - del: backup old index"
2906-
return 1
2907-
}
2908-
2909-
# Remove old record
2910-
"${EASYTLS_SED}" -i -e "/^${old_record}.*$/d" \
2911-
"${EASYTLS_INLINE_INDEX}" || {
2912-
error_msg "inline_index_update del: Remove old record"
2913-
return 1
2914-
}
2915-
2916-
# Remove backup old index
2917-
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}-deleted" || {
2918-
error_msg "inline_index_update - del: Remove backup old index"
2868+
if universal_update del "${EASYTLS_INLINE_INDEX}" "${old_record}"; then
2869+
: # ok
2870+
update_master_hash=1
2871+
return 0
2872+
else
2873+
error_msg "inline_index_update - universal_update - del"
29192874
return 1
2920-
}
2875+
fi
29212876
;;
29222877
force-del)
29232878
# Build old record without inline file HASH, with --sub-key-name
@@ -2926,14 +2881,14 @@ inline_index_update ()
29262881
old_record="${old_record}[[:blank:]]${name}"
29272882
old_record="${old_record}[[:blank:]]${TLSKEY_SUBNAME}"
29282883

2884+
easytls_verbose " DEL: ${old_record}"
2885+
29292886
# Find old record
29302887
"${EASYTLS_GREP}" -q "^${old_record}" "${EASYTLS_INLINE_INDEX}" || {
29312888
error_msg "inline_index_update force-del: Find ${old_record}"
29322889
return 1
29332890
}
29342891

2935-
easytls_verbose " DEL: ${old_record}"
2936-
29372892
# Remove old record
29382893
"${EASYTLS_SED}" -i -e "/^${old_record}.*$/d" \
29392894
"${EASYTLS_INLINE_INDEX}" || {
@@ -2947,16 +2902,6 @@ inline_index_update ()
29472902
;;
29482903
esac
29492904

2950-
# Remove temp files
2951-
"${EASYTLS_RM}" -f "${EASYTLS_TEMP_LIST}" "${EASYTLS_TEMP_RECORD}" \
2952-
"${EASYTLS_INLINE_INDEX}.tmp"
2953-
2954-
# Keep a hash of the inline-index
2955-
#inline_index_save_hash || {
2956-
# error_msg "Failed to update inline-index hash"
2957-
# return 1
2958-
# }
2959-
29602905
easytls_verbose "Inline Index Update complete!"
29612906
update_master_hash=1
29622907
} # => inline_index_update ()
@@ -3359,7 +3304,7 @@ remove_metadata ()
33593304
[ -f "${inline_file}" ] || missing_file "${inline_file}"
33603305

33613306
tlskey_serial="$(inline_tlskey_serial)"
3362-
if "${EASYTLS_GREP}" "UV_TLSKEY_SERIAL ${tlskey_serial}" "${inline_file}"
3307+
if "${EASYTLS_GREP}" -q "UV_TLSKEY_SERIAL ${tlskey_serial}" "${inline_file}"
33633308
then
33643309
: # OK
33653310
else
@@ -3384,23 +3329,6 @@ remove_metadata ()
33843329
if inline_index_update del; then
33853330
: # OK
33863331
else
3387-
# Restore original inline-index
3388-
if [ -f "${EASYTLS_INLINE_INDEX}-deleted" ]; then
3389-
"${EASYTLS_RM}" -f "${EASYTLS_INLINE_INDEX}"
3390-
"${EASYTLS_MV}" "${EASYTLS_INLINE_INDEX}-deleted" \
3391-
"${EASYTLS_INLINE_INDEX}" || \
3392-
die "remove_inline - Restore original inline-index"
3393-
fi
3394-
3395-
# Undo move
3396-
if [ -z "${force_remove}" ]; then
3397-
"${EASYTLS_MV}" "${inline_file}-deleted" "${inline_file}" || \
3398-
die "Failed to restore: ${inline_file}"
3399-
else
3400-
"${EASYTLS_MV}" "${inline_file}-badhash" "${inline_file}" || \
3401-
die "Failed to restore: ${inline_file}"
3402-
fi
3403-
# Always die
34043332
die "Failed to update inline-index"
34053333
fi
34063334

@@ -3487,99 +3415,30 @@ tlskey_index_update ()
34873415
update_index_action="${1}"
34883416

34893417
# Verify tlskey_serial
3490-
[ -n "${tlskey_serial}" ] || return 1
3491-
3492-
# Verify tlskey-index Hash
3493-
#tlskey_index_verify_hash || {
3494-
# error_msg "tlskey-index is corrupt"
3495-
# return 1
3496-
# }
3497-
3498-
# backup old index
3499-
"${EASYTLS_CP}" "${EASYTLS_TLSKEY_INDEX}" "${EASYTLS_TLSKEY_INDEX}.tmp" || {
3500-
error_msg "tlskey_index_update - backup old index"
3501-
return 1
3502-
}
3418+
[ -n "${tlskey_serial}" ] || die "tlskey_index_update - tlskey_serial"
35033419

35043420
# Update
35053421
case "${update_index_action}" in
35063422
add)
3507-
# Create new record
35083423
new_record="${tlskey_serial} ${cert_serial} ${cli_name} ${TLSKEY_SUBNAME}"
3509-
3510-
# Verify new record does not exist
3511-
if "${EASYTLS_GREP}" -q "^${new_record}\$" "${EASYTLS_TLSKEY_INDEX}"
3512-
then
3513-
error_msg "tlskey_index_update - add: Cannot add duplicate record"
3424+
if universal_update add "${EASYTLS_TLSKEY_INDEX}" "${new_record}"; then
3425+
: # ok
3426+
else
35143427
return 1
35153428
fi
3516-
3517-
easytls_verbose " ADD: ${new_record}"
3518-
3519-
# Write new record
3520-
"${EASYTLS_PRINTF}" '%s\n' "${new_record}" > "${EASYTLS_TEMP_RECORD}" || {
3521-
error_msg "tlskey_index_update - add: Failed to create temp record"
3522-
return 1
3523-
}
3524-
3525-
# Append temp record to TLS key index
3526-
"${EASYTLS_CP}" "${EASYTLS_TLSKEY_INDEX}" "${EASYTLS_TEMP_LIST}"
3527-
"${EASYTLS_CAT}" "${EASYTLS_TEMP_LIST}" "${EASYTLS_TEMP_RECORD}" > \
3528-
"${EASYTLS_TLSKEY_INDEX}"
3529-
3530-
# Verify new record does exist
3531-
"${EASYTLS_GREP}" -q "^${new_record}\$" "${EASYTLS_TLSKEY_INDEX}" || {
3532-
# Restore original TLS key index
3533-
"${EASYTLS_CP}" "${EASYTLS_TEMP_LIST}" "${EASYTLS_TLSKEY_INDEX}"
3534-
error_msg "tlskey_index_update - add: failed to add record"
3535-
return 1
3536-
}
35373429
;;
35383430
del)
3539-
# Note:
3540-
# Client tlskey_serial is unique, regardless of --sub-key-name
3541-
# Server tlskey_serial is always 40 or 64 zeros
3542-
# Identify old record
35433431
old_record="${tlskey_serial}[[:blank:]]${cert_serial}[[:blank:]].*"
3544-
3545-
# Find old record
3546-
"${EASYTLS_GREP}" -q "^${old_record}\$" "${EASYTLS_TLSKEY_INDEX}" || {
3547-
error_msg "tlskey_index_update - del: Failed to find old record"
3548-
return 1
3549-
}
3550-
3551-
easytls_verbose " DEL: ${old_record}"
3552-
3553-
# Remove old record
3554-
"${EASYTLS_SED}" -i \
3555-
-e "/^${old_record}\$/d" "${EASYTLS_TLSKEY_INDEX}" || {
3556-
error_msg "tlskey_index_update - del: Failed write"
3557-
return 1
3558-
}
3559-
3560-
# Verify old record does not exist
3561-
if "${EASYTLS_GREP}" -q "^${old_record}\$" "${EASYTLS_TLSKEY_INDEX}"
3562-
then
3563-
error_msg "tlskey_index_update - del: Failed to delete old record"
3432+
if universal_update del "${EASYTLS_TLSKEY_INDEX}" "${old_record}"; then
3433+
: # ok
3434+
else
35643435
return 1
35653436
fi
35663437
;;
3567-
*)
3568-
error_msg "Unknown index action: ${update_index_action}"
3438+
*) error_msg "Unknown index action: ${update_index_action}"
35693439
return 1
3570-
;;
35713440
esac
35723441

3573-
# Remove temp files
3574-
"${EASYTLS_RM}" -f "${EASYTLS_TEMP_LIST}" "${EASYTLS_TEMP_RECORD}" \
3575-
"${EASYTLS_TLSKEY_INDEX}.tmp"
3576-
3577-
# Keep a hash of the tlskey-index
3578-
#tlskey_index_save_hash || {
3579-
# error_msg "Failed to update tlskey-index hash"
3580-
# return 1
3581-
# }
3582-
35833442
easytls_verbose "tlskey-index Update complete!"
35843443
update_master_hash=1
35853444
} # => tlskey_index_update ()
@@ -6497,6 +6356,78 @@ save_id ()
64976356

64986357

64996358

6359+
############################################################################
6360+
#
6361+
# TEST Section
6362+
#
6363+
6364+
# universal update config/index/list
6365+
universal_update ()
6366+
{
6367+
[ "$#" -eq 3 ] || return 1
6368+
6369+
action="${1}"
6370+
target="${2}"
6371+
record="${3}"
6372+
6373+
# Valid target
6374+
[ -f "${target}" ] || missing_file "universal_update - ${target}"
6375+
[ ! -f "${EASYTLS_TEMP_LIST}" ] || "${EASYTLS_RM}" -f "${EASYTLS_TEMP_LIST}"
6376+
"${EASYTLS_CP}" "${target}" "${EASYTLS_TEMP_LIST}" || {
6377+
error_msg "universal_update - copy target to temp-list"
6378+
return 1
6379+
}
6380+
6381+
unset -v universal_update_ok
6382+
6383+
# Action
6384+
case "${action}" in
6385+
add)
6386+
if "${EASYTLS_GREP}" -q "${record}" "${EASYTLS_TEMP_LIST}"; then
6387+
error_msg "universal_update - add - record exists"
6388+
else
6389+
# Add record
6390+
if { "${EASYTLS_CAT}" "${EASYTLS_TEMP_LIST}"
6391+
"${EASYTLS_PRINTF}" '%s\n' "${record}"
6392+
} > "${EASYTLS_TEMP_UPDATE}"
6393+
then
6394+
# Success
6395+
universal_update_ok=1
6396+
else
6397+
error_msg "universal_update - Add record"
6398+
fi
6399+
fi
6400+
;;
6401+
del)
6402+
if "${EASYTLS_GREP}" -q "^${record}" "${EASYTLS_TEMP_LIST}"; then
6403+
# Delete record
6404+
if "${EASYTLS_SED}" -e "/^${record}/d" \
6405+
"${EASYTLS_TEMP_LIST}" > "${EASYTLS_TEMP_UPDATE}"
6406+
then
6407+
# Success
6408+
universal_update_ok=1
6409+
else
6410+
error_msg "universal_update - Delete record"
6411+
fi
6412+
else
6413+
error_msg "universal_update - del - record does not exist"
6414+
fi
6415+
;;
6416+
*) die "universal_update - unknown action - ${action}"
6417+
esac
6418+
6419+
if [ -n "${universal_update_ok}" ]; then
6420+
# Move temp file over target
6421+
"${EASYTLS_RM}" -f "${target}"
6422+
"${EASYTLS_MV}" "${EASYTLS_TEMP_UPDATE}" "${target}" || \
6423+
die "universal_update - Move temp file over target"
6424+
else
6425+
return 1
6426+
fi
6427+
} # => universal_update ()
6428+
6429+
6430+
65006431
############################################################################
65016432
#
65026433
# DISABLED-LIST Section
@@ -9522,6 +9453,7 @@ shellcheck_ignore_2154 ()
95229453
EASYTLS_DISABLED_LIST=
95239454
EASYTLS_DISABLED_HASH=
95249455

9456+
EASYTLS_TEMP_UPDATE=
95259457
EASYTLS_TEMP_LIST=
95269458
EASYTLS_TEMP_RECORD=
95279459
EASYTLS_TEMP_LOCK=
@@ -9767,6 +9699,7 @@ vars_setup()
97679699
set_var EASYTLS_DISABLED_HASH \
97689700
"${EASYTLS_DATA_DIR}/easytls-disabled-list.hash"
97699701

9702+
set_var EASYTLS_TEMP_UPDATE "${EASYTLS_DATA_DIR}/easytls-temp.update"
97709703
set_var EASYTLS_TEMP_LIST "${EASYTLS_DATA_DIR}/easytls-temp.list"
97719704
set_var EASYTLS_TEMP_RECORD "${EASYTLS_DATA_DIR}/easytls-temp.record"
97729705
set_var EASYTLS_TEMP_LOCK "${EASYTLS_DATA_DIR}/easytls-temp.lock.d"

0 commit comments

Comments
 (0)