Skip to content

Commit f428bc4

Browse files
committed
ssh: Cleanup permanent ssh patch
1 parent c868bb4 commit f428bc4

4 files changed

Lines changed: 77 additions & 72 deletions

File tree

data/ssh_install.sh

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,8 @@
22

33
DIR_PATCH=/etc/crontabs/patches
44

5-
if [ "$( grep 'ssh_patch.sh' /etc/crontabs/root )" != "" ]; then
6-
if [ -e "/tmp/ssh_uninstall.sh" ]; then
7-
sh /tmp/ssh_uninstall.sh
8-
fi
9-
fi
10-
115
if [ ! -d $DIR_PATCH ]; then
12-
mkdir $DIR_PATCH
6+
mkdir -p $DIR_PATCH
137
chown root $DIR_PATCH
148
chmod 0755 $DIR_PATCH
159
fi
@@ -20,7 +14,20 @@ chmod +x $DIR_PATCH/ssh_patch.sh
2014
nvram set ssh_en=1
2115
nvram commit
2216

23-
grep -v "/ssh_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new
24-
echo "*/1 * * * * /etc/crontabs/patches/ssh_patch.sh >/dev/null 2>&1" >> /etc/crontabs/root.new
25-
mv /etc/crontabs/root.new /etc/crontabs/root
26-
/etc/init.d/cron restart
17+
INSTALL_METHOD=2
18+
19+
if [ $INSTALL_METHOD = 1 ]; then
20+
FILE_FOR_EDIT=/etc/crontabs/root
21+
grep -v "/ssh_patch.sh" $FILE_FOR_EDIT > $FILE_FOR_EDIT.new
22+
echo "*/1 * * * * /etc/crontabs/patches/ssh_patch.sh >/dev/null 2>&1" >> $FILE_FOR_EDIT.new
23+
mv $FILE_FOR_EDIT.new $FILE_FOR_EDIT
24+
/etc/init.d/cron restart
25+
fi
26+
27+
if [ $INSTALL_METHOD = 2 ]; then
28+
uci set firewall.auto_ssh_patch=include
29+
uci set firewall.auto_ssh_patch.type='script'
30+
uci set firewall.auto_ssh_patch.path="$DIR_PATCH/ssh_patch.sh"
31+
uci set firewall.auto_ssh_patch.enabled='1'
32+
uci commit firewall
33+
fi

data/ssh_patch.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@
22

33
[ -e "/tmp/ssh_patch.log" ] && return 0
44

5-
HAVE_PATCH=$( grep '= "release"' /etc/init.d/dropbear )
6-
if [ -z "$HAVE_PATCH" ]; then
7-
return 0
5+
SSH_EN=`nvram get ssh_en`
6+
if [ "$SSH_EN" != "1" ]; then
7+
nvram set ssh_en=1
8+
nvram commit
89
fi
910

10-
sed -i 's/= "release"/= "XXXXXX"/g' /etc/init.d/dropbear
11+
if grep -q '= "release"' /etc/init.d/dropbear ; then
12+
sed -i 's/= "release"/= "XXXXXX"/g' /etc/init.d/dropbear
13+
fi
1114

1215
/etc/init.d/dropbear enable
1316
/etc/init.d/dropbear restart

data/ssh_uninstall.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
#!/bin/sh
22

3-
grep -v "/ssh_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new
4-
mv /etc/crontabs/root.new /etc/crontabs/root
5-
/etc/init.d/cron restart
3+
DIR_PATCH=/etc/crontabs/patches
64

7-
rm -f /etc/crontabs/patches/ssh_patch.sh
5+
if grep -q '/ssh_patch.sh' /etc/crontabs/root ; then
6+
# remove older version of patch
7+
grep -v "/ssh_patch.sh" /etc/crontabs/root > /etc/crontabs/root.new
8+
mv /etc/crontabs/root.new /etc/crontabs/root
9+
/etc/init.d/cron restart
10+
fi
11+
uci delete firewall.auto_ssh_patch
12+
uci commit firewall
13+
14+
rm -f $DIR_PATCH/ssh_patch.sh
815
rm -f /tmp/ssh_patch.log
916

install_ssh.py

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
1-
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
3-
4-
import os
5-
import sys
6-
7-
import xmir_base
8-
import gateway
9-
from gateway import die
10-
11-
12-
gw = gateway.Gateway()
13-
14-
fn_dir = 'data/'
15-
fn_local = 'data/ssh_patch.sh'
16-
fn_remote = '/tmp/ssh_patch.sh'
17-
fn_local_i = 'data/ssh_install.sh'
18-
fn_remote_i = '/tmp/ssh_install.sh'
19-
fn_local_u = 'data/ssh_uninstall.sh'
20-
fn_remote_u = '/tmp/ssh_uninstall.sh'
21-
22-
action = 'install'
23-
if len(sys.argv) > 1:
24-
if sys.argv[1].startswith('u') or sys.argv[1].startswith('r'):
25-
action = 'uninstall'
26-
27-
if action == 'install':
28-
gw.upload(fn_local, fn_remote)
29-
gw.upload(fn_local_i, fn_remote_i)
30-
31-
gw.upload(fn_local_u, fn_remote_u)
32-
33-
print("All files uploaded!")
34-
'''
35-
if action == 'install':
36-
gw.ssh_close()
37-
import passw
38-
gw = gateway.Gateway()
39-
if not gw.ping():
40-
die('SSH not active!')
41-
'''
42-
43-
print("Run scripts...")
44-
if action == 'install':
45-
gw.run_cmd("sh " + fn_remote_i)
46-
else:
47-
gw.run_cmd("sh " + fn_remote_u)
48-
49-
gw.run_cmd("rm -f " + fn_remote)
50-
gw.run_cmd("rm -f " + fn_remote_i)
51-
gw.run_cmd("rm -f " + fn_remote_u)
52-
53-
print("Ready! The SSH patch installed.")
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import sys
6+
import time
7+
8+
import xmir_base
9+
from gateway import *
10+
11+
gw = Gateway()
12+
13+
FN_patch = 'data/ssh_patch.sh'
14+
fn_patch = '/tmp/ssh_patch.sh'
15+
FN_install = 'data/ssh_install.sh'
16+
fn_install = '/tmp/ssh_install.sh'
17+
FN_uninstall = 'data/ssh_uninstall.sh'
18+
fn_uninstall = '/tmp/ssh_uninstall.sh'
19+
20+
action = 'install'
21+
if len(sys.argv) > 1:
22+
if sys.argv[1].startswith('u') or sys.argv[1].startswith('r'):
23+
action = 'uninstall'
24+
25+
if action == 'install':
26+
gw.upload(FN_patch, fn_patch)
27+
gw.upload(FN_install, fn_install)
28+
29+
gw.upload(FN_uninstall, fn_uninstall)
30+
31+
print("All files uploaded!")
32+
33+
print("Run scripts...")
34+
run_script = fn_install if action == 'install' else fn_uninstall
35+
gw.run_cmd(f"chmod +x {run_script} ; {run_script}")
36+
37+
time.sleep(1.5)
38+
39+
gw.run_cmd(f"rm -f {fn_patch} ; rm -f {fn_install} ; rm -f {fn_uninstall}")
40+
41+
print("Ready! The Permanent SSH patch installed.")

0 commit comments

Comments
 (0)