Browse code

Add keytab-editor.

keytab-editor is a script that combines yamltab and the default text editor to
offer seamless edition of keytab files.

Xavier G authored on26/04/2020 21:40:28
Showing1 changed files

1 1
new file mode 100755
... ...
@@ -0,0 +1,63 @@
1
+#!/usr/bin/env bash
2
+
3
+# Copyright © 2020 Xavier G. <xavier.yamltab@kindwolf.org>
4
+# This work is free. You can redistribute it and/or modify it under the
5
+# terms of the Do What The Fuck You Want To Public License, Version 2,
6
+# as published by Sam Hocevar. See the COPYING file for more details.
7
+
8
+# Helper variables:
9
+yamltab="${YAMLTAB_PATH:-yamltab}"
10
+
11
+# Helper functions:
12
+function edit {
13
+	if `which editor > /dev/null`; then
14
+		editor "$@"
15
+	else
16
+		"${EDITOR:-vi}" "$@"
17
+	fi
18
+}
19
+
20
+function cleanup {
21
+	[ -f "${tmp_file}" ] && unlink "${tmp_file}"
22
+	[ -f "${tmp_file}.yaml" ] && unlink "${tmp_file}.yaml"
23
+	[ -d "${tmp_dir}" ] && rmdir "${tmp_dir}"
24
+}
25
+
26
+function exit {
27
+	cleanup
28
+	builtin exit $1
29
+}
30
+
31
+# Expect a keytab file as single argument:
32
+keytab="${1:?Usage: $0 keytab}"
33
+keytab_filename="$(basename "${keytab}")"
34
+
35
+# Redundant with the chmod below; might compensate for some whacky editors?
36
+umask 0077
37
+
38
+# Prepare temporary files:
39
+tmp_dir="$(mktemp --directory)" || exit $?
40
+tmp_file="${tmp_dir}/${keytab_filename}"
41
+touch "${tmp_file}" "${tmp_file}.yaml" || exit $?
42
+chmod go-rwx "${tmp_file}" "${tmp_file}.yaml" || exit $?
43
+
44
+# Dump the given keytab as YAML:
45
+"${yamltab}" "${keytab}" > "${tmp_file}.yaml" || exit $?
46
+md5_before="$(md5sum "${tmp_file}.yaml")"
47
+
48
+# Edit the keytab as YAML:
49
+edit "${tmp_file}.yaml"
50
+rc=$?
51
+
52
+# If the YAML dump was not modified, leave the keytab untouched:
53
+md5_after="$(md5sum "${tmp_file}.yaml")"
54
+if [ "${md5_before}" == "${md5_after}" ]; then
55
+	echo "No changes detected, ${keytab} left untouched."
56
+	exit 0
57
+else
58
+	# Convert the YAML into a binary keytab:
59
+	"${yamltab}" "${tmp_file}.yaml" > "${tmp_file}" || exit $?
60
+	# Replace the old keytab with the new one:
61
+	cat "${tmp_file}" > "${keytab}" || exit $?
62
+fi
63
+exit $rc