-
Notifications
You must be signed in to change notification settings - Fork 63
/
tvi
executable file
·127 lines (126 loc) · 3.49 KB
/
tvi
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
#! /bin/sh
# SPDX-License-Identifier: GPL-2.0-or-later
find_executable()
{
e="$1"
IFS=:
set -- $PATH
while [ ${#1} -gt 0 ]; do
[ -x "$1/$e" ] && return 0
shift
done
return 1
}
if [ -z $1 ]; then
printf "Usage: $0 <filename | id> [ --no-decode ]\n\n" 1>&2
printf "View/edit a TFFS file\n\n" 1>&2
printf "<filename> - file name within /var/flash directory\n" 1>&2
printf "<id> - TFFS character device minor id\n\n" 1>&2
printf "The file content will be copied to a temporary file, its content will be decrypted, if the\n" 1>&2
printf "needed binary (decode_secrets) exists and 'vi' will be called for it.\n\n" 1>&2
printf "It will only be copied back after user made *intentional* changes to the file.\n\n" 1>&2
printf "If the changed content is related to 'ctlmgr' (e.g. it's a .cfg file at /var/flash), you\n" 1>&2
printf "will be asked to stop 'ctlmgr' while storing the new data.\n" 1>&2
exit 1
fi
major=$(sed -n -e "s|^ *\([0-9]\{1,3\}\) tffs\$|\1|p" /proc/devices)
[ "$(expr "$1" : "\(^[0-9]*$\)")" = "$1" ] && i=$1 || i=-1
if [ $i -gt 0 ] && [ $i -le 255 ]; then
minor=$i
fn="/var/tmp/mknod_$$_$(date +%s)"
mknod "$fn" c $major $minor
rmf="$fn"
else
fn="$1"
dir=/var/flash
if [ -c "$dir/$fn" ]; then
minor=$(( 0x$(stat -c %T "$dir/$fn") ))
fn="$dir/$fn"
else
printf "The specified file '%s' isn't a character device at '%s'.\n" "$fn" "$dir" 1>&2
exit 1
fi
unset rmf
fi
tf="/var/tmp/temp_$$_$(date +%s)"
find_executable "decode_secrets" && decode="decode_secrets" || decode="cat"
[ "$2" = "--no-decode" ] && decode="cat"
cat "$fn" | gunzip -c 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
$decode <"$fn" >"$tf"
zip=0
else
cat "$fn" | gunzip -c | $decode >"$tf"
zip=1
fi
cks_before=$(md5sum <"$tf" | sed -n -e 's/^\([0-9a-f]*\).*/\1/p')
vi "$tf"
cks_after=$(md5sum <"$tf" | sed -n -e 's/^\([0-9a-f]*\).*/\1/p')
if ! [ "$cks_after" = "$cks_before" ]; then
error=2
while [ $error -eq 2 ]; do
printf "The file was changed. Do you want to write the changes to TFFS ? (y/N): " 1>&2
read answr
if [ ${#answr} -eq 0 ]; then
answr='n'
fi
if [ "$answr" = "Y" ] || [ "$answr" = "y" ]; then
fe="${fn#*.}"
if [ "$fe" = "cfg" ]; then
restart=2
else
restart=0
restartmsg=" not"
fi
while [ $restart -eq 2 ]; do
printf "Do you want to restart 'ctlmgr' to activate your changes ? (y/N): " 1>&2
read answr
if [ ${#answr} -eq 0 ]; then
answr='n'
fi
if [ "$answr" = "Y" ] || [ "$answr" = "y" ]; then
restart=1
restartmsg=""
else
if [ "$answr" = "N" ] || [ "$answr" = "n" ]; then
restart=0
restartmsg=" not"
else
printf "\nPlease choose 'y' or 'n' and answer again ...\n\a" 1>&2
fi
fi
done
if [ $restart -eq 1 ]; then
printf "Stopping ctlmgr ... " 1>&2
ctlmgr -s
printf "done, error = $?"
fi
if [ $zip -eq 0 ]; then
cat "$tf" >"$fn"
else
gzip -c "$tf" | cat >"$fn"
fi
printf "\nChanges saved.\n" 1>&2
error=0
if [ $restart -eq 1 ]; then
printf "Starting ctlmgr ... " 1>&2
LD_LIBRARY_PATH= ctlmgr
printf "done, error = $?\n" 1>&2
fi
eventadd 1 "The configuration file with minor ID '$minor' was changed and ctlmgr was$restartmsg restarted."
else
if [ "$answr" = "N" ] || [ "$answr" = "n" ]; then
printf "\nThe changes were not saved.\n" 1>&2
error=1
else
printf "\nPlease choose 'y' or 'n' and answer again ...\n" 1>&2
fi
fi
done
else
printf "The file was not changed.\n" 1>&2
error=1
fi
rm "$tf"
[ ${#rmf} -gt 0 ] && rm "$rmf"
exit $error