-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathcustom-configs.sh
More file actions
452 lines (372 loc) · 14 KB
/
custom-configs.sh
File metadata and controls
452 lines (372 loc) · 14 KB
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
#!/bin/sh
# Made by Jack'lul <jacklul.github.io>
#
# Modify configuration files of some selected services
#
# Implements Custom config files feature from Asuswrt-Merlin:
# https://github.com/RMerl/asuswrt-merlin.ng/wiki/Custom-config-files
#
#jas-update=custom-configs.sh
#shellcheck shell=ash
#shellcheck disable=SC2155
#shellcheck source=./common.sh
readonly common_script="$(dirname "$0")/common.sh"
if [ -f "$common_script" ]; then . "$common_script"; else { echo "$common_script not found" >&2; exit 1; } fi
KILL_TIMEOUT=5 # how many seconds to wait before SIGKILL if the process does not stop after SIGTERM, empty or -1 means no waiting
# No RUN_EVERY_MINUTE option here as this script HAS to run every minute to check if any of the configs reverted to default
load_script_config
[ -z "$KILL_TIMEOUT" ] && KILL_TIMEOUT=-1 # empty value disables timeout
readonly FILES="/etc/profile /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/hosts /etc/resolv.conf " # files we can modify
readonly NO_ADD_FILES="/etc/resolv.conf" # files that cannot appended to
readonly NO_REPLACE_FILES="/etc/profile /etc/passwd /etc/shadow /etc/group /etc/gshadow /etc/stubby/stubby.yml" # files that cannot be replaced
readonly NO_POSTCONF_FILES="/etc/profile /etc/passwd /etc/shadow /etc/group /etc/gshadow" # files that cannot run postconf script
get_binary_location() {
[ -z "$1" ] && { echo "Binary name not provided" >&2; exit 1; }
local _binary_name="$(echo "$1"| awk '{print $1}')"
local _base_path
for _base_path in /usr/sbin /usr/bin /sbin /bin; do
[ -f "$_base_path/$_binary_name" ] && echo "$_base_path/$_binary_name" && return
done
}
restart_process() {
[ -z "$1" ] && { echo "Process name not provided" >&2; exit 1; }
local _pid _cmdline _timeout _started _full_binary_path
for _pid in $(/bin/ps w | grep -F "$1" | grep -v "grep\|/jffs/scripts" | awk '{print $1}'); do
[ ! -f "/proc/$_pid/cmdline" ] && continue
_cmdline="$(tr '\0' ' ' < "/proc/$_pid/cmdline")"
kill -s SIGTERM "$_pid" 2> /dev/null
_timeout="$KILL_TIMEOUT"
while [ -f "/proc/$_pid/cmdline" ] && [ "$_timeout" -ge 0 ]; do
sleep 1
_timeout=$((_timeout-1))
done
[ -f "/proc/$_pid/cmdline" ] && kill -s SIGKILL "$_pid" 2> /dev/null
if [ -z "$_started" ]; then
# make sure we are executing build-in binary
_full_binary_path=
if [ "$(echo "$_cmdline" | cut -c 1-1)" != "/" ]; then
_full_binary_path="$(get_binary_location "$_cmdline")"
fi
if [ -n "$_full_binary_path" ]; then
_cmdline="$(echo "$_cmdline" | awk '{for (i=2; i<NF; i++) printf $i " "; print $NF}')"
#shellcheck disable=SC2086
"$_full_binary_path" $_cmdline && _started=1 && logecho "Restarted process: $_full_binary_path $_cmdline" alert
else
$_cmdline && _started=1 && logecho "Restarted process: $_cmdline" alert
fi
fi
done
}
is_file_add_supported() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ -z "$NO_ADD_FILES" ] && return 0
local _new="$(echo "$1" | sed 's/\.new$//')"
local _no_add_file
for _no_add_file in $NO_ADD_FILES; do
[ "$_no_add_file" = "$_new" ] && return 1
done
return 0
}
is_file_replace_supported() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ -z "$NO_REPLACE_FILES" ] && return 0
local _new="$(echo "$1" | sed 's/\.new$//')"
local _no_replace_file
for _no_replace_file in $NO_REPLACE_FILES; do
[ "$_no_replace_file" = "$_new" ] && return 1
done
return 0
}
is_file_postconf_supported() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ -z "$NO_POSTCONF_FILES" ] && return 0
local _new="$(echo "$1" | sed 's/\.new$//')"
local _no_postconf_file
for _no_postconf_file in $NO_POSTCONF_FILES; do
[ "$_no_postconf_file" = "$_new" ] && return 1
done
return 0
}
is_config_file_modified() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; return 1; }
local _basename="$(basename "$1" | cut -d '.' -f 1)"
case "$_basename" in
"passwd"|"shadow"|"group"|"gshadow")
if [ -f "$1.new" ] && ! is_config_file_different "$1"; then
return 0
fi
;;
*)
if grep -Fq "Modified by $script_name script" "$1"; then
return 0
fi
;;
esac
return 1
}
is_config_file_different() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; return 1; }
[ ! -f "$1.new" ] && { echo "File '$1.new' does not exist" >&2; return 1; }
if [ "$(md5sum "$1" | awk '{print $1}')" != "$(md5sum "$1.new" | awk '{print $1}')" ]; then
return 0
fi
return 1
}
modify_config_file() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; return 1; }
# $2 = custom /jffs/configs/[NAME.conf]
local _basename="$(basename "$1")"
[ -n "$2" ] && _basename="$2"
if [ -f "/jffs/configs/$_basename" ] && is_file_replace_supported "$1"; then
logecho "Replacing '$1' with '/jffs/configs/$_basename'..." alert
cat "/jffs/configs/$_basename" > "$1.new"
elif [ -f "/jffs/configs/$_basename.add" ] && is_file_add_supported "$1"; then
logecho "Appending '/jffs/configs/$_basename.add' to '$1'..." alert
cp "$1" "$1.new"
cat "/jffs/configs/$_basename.add" >> "$1.new"
fi
}
run_postconf_script() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; exit 1; }
if ! is_file_postconf_supported "$1"; then
return
fi
local _basename="$(basename "$1" | cut -d '.' -f 1)"
if [ -x "/jffs/scripts/$_basename.postconf" ]; then
logecho "Running '/jffs/scripts/$_basename.postconf' script..." alert
[ ! -f "$1.new" ] && cp "$1" "$1.new"
sh "/jffs/scripts/$_basename.postconf" "$1.new" < /dev/null
fi
}
add_modified_mark() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; exit 1; }
local _basename="$(basename "$1" | cut -d '.' -f 1)"
local _comment="#"
case "$_basename" in
"zebra")
_comment="!"
;;
"passwd"|"shadow"|"group"|"gshadow")
_comment= # unsupported
;;
esac
if [ -n "$_comment" ]; then
echo "" >> "$1"
echo "$_comment Modified by $script_name script" >> "$1"
fi
}
commit_new_file() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ ! -f "$1" ] && { echo "File '$1' does not exist" >&2; exit 1; }
[ ! -f "$1.new" ] && { echo "File '$1.new' does not exist" >&2; exit 1; }
cp -f "$1.new" "$1"
}
modify_files() {
local _file
for _file in $FILES; do
if [ -f "$_file" ] && ! is_config_file_modified "$_file"; then
[ ! -f "$_file.bak" ] && cp "$_file" "$_file.bak"
modify_config_file "$_file"
run_postconf_script "$_file"
if [ -f "$_file.new" ]; then
add_modified_mark "$_file.new"
is_config_file_different "$_file" && commit_new_file "$_file"
fi
fi
done
}
restore_files() {
local _file
for _file in $FILES; do
if [ -f "$_file.bak" ] && [ -f "$_file.new" ]; then
cp -f "$_file.bak" "$_file"
rm -f "$_file.new"
logecho "Restored: $_file" alert
fi
done
}
modify_service_config_file() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ -z "$2" ] && { echo "Process name not provided" >&2; exit 1; }
# $3 = custom /jffs/configs/[NAME.conf]
local _match="$2"
case "$2" in
"samba")
_match="nmbd\|smbd"
;;
esac
if /bin/ps w | grep -v "grep" | grep -q "$_match" && [ -f "$1" ] && ! is_config_file_modified "$1"; then
if [ -n "$3" ]; then
modify_config_file "$1" "$3"
else
modify_config_file "$1"
fi
run_postconf_script "$1"
if [ -f "$1.new" ] && [ "$(md5sum "$1" | awk '{print $1}')" != "$(md5sum "$1.new" | awk '{print $1}')" ]; then
add_modified_mark "$1.new"
if is_config_file_different "$1"; then
commit_new_file "$1"
case "$2" in
"avahi-daemon")
/usr/sbin/avahi-daemon --kill && /usr/sbin/avahi-daemon -D && logecho "Restarted process: avahi-daemon" alert
;;
"samba")
restart_process nmbd
restart_process smbd
;;
"ipsec")
ipsec restart > /dev/null 2>&1
;;
"mcpd")
killall -SIGKILL /bin/mcpd 2> /dev/null
nohup /bin/mcpd > /dev/null 2>&1 &
;;
*)
restart_process "$2"
;;
esac
fi
fi
fi
}
restore_service_config_file() {
[ -z "$1" ] && { echo "File path not provided" >&2; exit 1; }
[ -z "$2" ] && { echo "Process/service name not provided" >&2; exit 1; }
local _match="$2"
local _service="$2"
case "$2" in
"mcpd")
_service="wan"
;;
"miniupnpd")
_service="upnp"
;;
"avahi-daemon")
_service="mdns"
;;
"zebra"|"ripd")
_service="quagga"
;;
"igmpproxy")
_service="wan_line"
;;
"vsftpd")
_service="ftpd"
;;
"samba")
_match="nmbd\|smbd"
;;
"minidlna")
_service="dms"
;;
"mt-daapd")
_service="mt_daapd"
;;
esac
if /bin/ps w | grep -v "grep" | grep -q "$_match" && [ -f "$1.new" ]; then
case "$_service" in
"ipsec")
service "ipsec_restart" > /dev/null
;;
*)
service "restart_${_service}" > /dev/null
;;
esac
rm -f "$1.new"
logecho "Restarted service: $_service" alert
fi
}
configs() {
lockfile lockwait # not lockfail as multiple service restarts might queue this one up via service-event script
case "$1" in
"modify")
# services.c
modify_files
modify_service_config_file "/etc/dnsmasq.conf" "dnsmasq"
modify_service_config_file "/etc/stubby/stubby.yml" "stubby"
# inadyn.conf - currently no way to support this as it is not running as daemon
modify_service_config_file "/var/mcpd.conf" "mcpd"
modify_service_config_file "/etc/upnp/config" "miniupnpd" "upnp"
modify_service_config_file "/tmp/avahi/avahi-daemon.conf" "avahi-daemon"
# afpd.service, adisk.service, mt-daap.service - use avahi-daemon.postconf to modify these
modify_service_config_file "/etc/zebra.conf" "zebra"
modify_service_config_file "/etc/ripd.conf" "ripd"
modify_service_config_file "/tmp/torrc" "tor"
# wan.c
modify_service_config_file "/tmp/igmpproxy.conf" "igmpproxy"
# snmpd.c
modify_service_config_file "/tmp/snmpd.conf" "snmpd"
# usb.c
modify_service_config_file "/etc/vsftpd.conf" "vsftpd"
modify_service_config_file "/etc/smb.conf" "samba"
modify_service_config_file "/etc/minidlna.conf" "minidlna"
modify_service_config_file "/etc/mt-daapd.conf" "mt-daapd"
# vpn.c
modify_service_config_file "/tmp/pptpd/pptpd.conf" "pptpd"
# rc_ipsec.c
modify_service_config_file "/etc/ipsec.conf" "ipsec"
;;
"restore")
# services.c
restore_files
restore_service_config_file "/etc/dnsmasq.conf" "dnsmasq"
restore_service_config_file "/etc/stubby/stubby.yml" "stubby"
# inadyn.conf
restore_service_config_file "/var/mcpd.conf" "mcpd"
restore_service_config_file "/etc/upnp/config" "miniupnpd"
restore_service_config_file "/tmp/avahi/avahi-daemon.conf" "avahi-daemon"
# afpd.service, adisk.service, mt-daap.service
restore_service_config_file "/etc/zebra.conf" "zebra"
restore_service_config_file "/etc/ripd.conf" "ripd"
restore_service_config_file "/tmp/torrc" "tor"
# wan.c
restore_service_config_file "/tmp/igmpproxy.conf" "igmpproxy"
# snmpd.c
restore_service_config_file "/tmp/snmpd.conf" "snmpd"
# usb.c
restore_service_config_file "/etc/vsftpd.conf" "vsftpd"
restore_service_config_file "/etc/smb.conf" "samba"
restore_service_config_file "/etc/minidlna.conf" "minidlna"
restore_service_config_file "/etc/mt-daapd.conf" "mt-daapd"
# vpn.c
restore_service_config_file "/tmp/pptpd/pptpd.conf" "pptpd"
# rc_ipsec.c
restore_service_config_file "/etc/ipsec.conf" "ipsec"
;;
esac
lockfile unlock
}
case "$1" in
"run")
configs modify
;;
"modify")
lockfile lockfail modify
modify_config_file "$2"
run_postconf_script "$2"
add_modified_mark "$2.new"
commit_new_file "$2"
lockfile unlock modify
;;
"start")
crontab_entry add "*/1 * * * * $script_path run"
configs modify
;;
"stop")
crontab_entry delete
configs restore
;;
"restart")
sh "$script_path" stop
sh "$script_path" start
;;
*)
echo "Usage: $0 run|start|stop|restart"
exit 1
;;
esac