blob: a0fe8192d71e9fe5eeed17eb202973b3ac34e14d [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001#!/bin/sh /etc/rc.common
2
3USE_PROCD=1
4START=30
5EXTRA_COMMANDS="get_key probeid log create_backup load_backup create_key"
6EXTRA_HELP=" get_key print probe public key (used for probe registration)
7 probeid print probe id
8 log print probe status log
9 create_backup backup ssh key to tar.gz
10 load_backup 'backup.tar.gz' load backup ssh key from tar.gz
11 create_key create probe priv/pub key
12"
13
14SCRIPTS_DIR="/usr/libexec/atlas-probe-scripts"
15TMP_BASE_DIR="/tmp/ripe_atlas_probe"
16PUB_KEY_FILE="$SCRIPTS_DIR/etc/probe_key.pub"
17PRIV_KEY_FILE="$SCRIPTS_DIR/etc/probe_key"
18PROBE_ID_FILE="$TMP_BASE_DIR/status/reg_init_reply.txt"
19LOG_FILE="/tmp/log/ripe_sw_probe"
20STATE_CONFIG="$SCRIPTS_DIR/state/config.txt"
21
22load_backup() {
23 local backup_arch
24 local tmp_dir
25
26 backup_arch="$1"
27 tmp_dir="$(mktemp -u -p /var/run/atlas)"
28 if [ -f "$backup_arch" ]; then
29 safe_mkdir "$tmp_dir"
30 tar -xzf "$backup_arch" -C "$tmp_dir/"
31 if [ -f "$tmp_dir/probe_key.pub" ] && [ -f "$tmp_dir/probe_key" ]; then
32 mv "$tmp_dir/probe_key.pub" "$PUB_KEY_FILE"
33 mv "$tmp_dir/probe_key" "$PRIV_KEY_FILE"
34 rm -rf "$tmp_dir"
35 print_msg "Info: public and private key loaded from backup"
36 else
37 print_msg "Error: Could not extract probe_key or probe_key form backup archive"
38 rm -rf "$tmp_dir"
39 exit 1
40 fi
41 else
42 print_msg "Error: Provided backup file $backup_arch does not exists"
43 exit 1
44 fi
45}
46
47create_backup() {
48 local back_dir
49
50 back_dir="$(pwd)"
51
52 if [ -f "$PUB_KEY_FILE" -a -f "$PRIV_KEY_FILE" ]; then
53 print_msg "Info: Creating backup arch in $back_dir"
54 tar -czf "$back_dir/atlas-key-backup.tar.gz" -C "$SCRIPTS_DIR/etc" probe_key probe_key.pub
55 else
56 print_msg "Error: private or public key does not exists."
57 exit 1
58 fi
59}
60
61create_key() {
62 local username
63 local probe_key=/etc/atlas/probe_key
64 local probe_pub_key=/etc/atlas/probe_key.pub
65
66 config_load atlas
67
68 config_get username "common" username
69
70 if [ -f "$PRIV_KEY_FILE" ]; then
71 if [ ! -f $probe_key ]; then
72 print_msg "Missing probe_key in /etc/atlas"
73 print_msg "The key will be lost on sysupgrade. Cosider moving the keys in /etc/atlas and create a link in the $SCRIPTS_DIR/etc/ dir."
74 fi
75
76 print_msg "probe_key already present. Exiting..."
77 exit 1
78 fi
79
80 if [ -z "$username" ]; then
81 print_msg "Username not set in atlas config file. Enter your ripe-atlas username."
82 exit 1
83 fi
84
85 if [ -n "$(which ssh-keygen)" ]; then
86 ssh-keygen -t rsa -b 2048 -f $probe_key -N ""
87 sed -i "s/ \S*$/ "$username"/" $probe_pub_key
88 elif [ -n "$(which dropbearkey)" ] && [ -n "$(which dropbearconvert)" ]; then
89 local public_key
90
91 public_key="$(dropbearkey -t rsa -f /etc/atlas/probe_key_dropbear -s 2048 | sed -n 2p)"
92 public_key="$(echo "$public_key" | sed "s/ \S*$/ "$username"/")"
93 echo $public_key > $probe_pub_key
94 dropbearconvert dropbear openssh /etc/atlas/probe_key_dropbear $probe_key
95 rm /etc/atlas/probe_key_dropbear
96 else
97 print_msg "Can't find a way to generate key."
98 exit 1
99 fi
100
101 #Link priv/pub key
102 [ -f $PRIV_KEY_FILE ] || ln -s $probe_key $PRIV_KEY_FILE
103 [ -f $PUB_KEY_FILE ] || ln -s $probe_pub_key $PUB_KEY_FILE
104
105 #Fix permission
106 chown atlas $probe_key $probe_pub_key
107 chgrp atlas $probe_key $probe_pub_key
108 chmod 644 $probe_key $probe_pub_key
109
110 print_msg "Key generated successfully. Use the get_key command to show the public key and get instruction on how to register your probe."
111}
112
113log() {
114 if [ -f "$LOG_FILE" ];then
115 tail "$LOG_FILE"
116 else
117 print_msg "Error. No log file found. Probe isn't probably running"
118 exit 1
119 fi
120}
121
122get_key() {
123 if [ -f "$PUB_KEY_FILE" ]; then
124 echo "Probe public key (use for registration)"
125 echo "URL with registration form https://atlas.ripe.net/apply/swprobe/"
126 echo "=========================================="
127 cat "$PUB_KEY_FILE"
128 else
129 print_msg "Error! Pub. key not found"
130 exit 1
131 fi
132}
133
134probeid() {
135 local probe_id
136
137 if [ -f "$PROBE_ID_FILE" ]; then
138 probe_id="$(awk '/PROBE_ID/ {print $2}' "$PROBE_ID_FILE")"
139 if [ -z "$probe_id" ]; then
140 print_msg "Probe ID not found SW probe isn't probably registered yet"
141 exit 1
142 else
143 print_msg "Probe ID is $probe_id"
144 fi
145 else
146 print_msg "Probe ID not found. SW probe is not running or probe_key isn't registered yet"
147 exit 1
148 fi
149}
150
151print_msg() {
152 echo "$1" >&2
153 logger -t atlas-sw-probe "$1"
154}
155
156stop_service() {
157 local atlas_pid
158 local tunnel_pid
159 local pid_file
160
161 print_msg "Stopping atlas sw probe"
162 print_msg "Kill all atlas processes"
163
164 for pid_file in "$SCRIPTS_DIR/run/"*.vol; do
165 [ -f "$pid_file" ] || continue
166 # test if proccess is still running
167 atlas_pid="$(cat "$pid_file")"
168 if kill -0 "$atlas_pid" 2>/dev/null; then
169 kill "$atlas_pid"
170 fi
171 done
172
173 if [ -f "$SCRIPTS_DIR/status/con_keep_pid.vol" ]; then
174 print_msg "Kill ssh tunnel"
175 tunnel_pid="$(cat "$SCRIPTS_DIR/status/con_keep_pid.vol")"
176 if kill -0 "$tunnel_pid" 2>/dev/null; then
177 kill "$tunnel_pid"
178 fi
179 fi
180
181 # Clean run dir
182 rm -r $TMP_BASE_DIR
183}
184
185safe_mkdir() {
186 local dir="$1"
187 if [ -e "$dir" ] && [ ! -d "$dir" -o -L "$dir" ]; then
188 rm -rf "$dir"
189 fi
190 mkdir -p "$dir"
191 chmod 700 "$dir"
192 chown root:root "$dir"
193}
194
195create_tmp_dirs() {
196 local dirs
197
198 chown -R atlas:atlas "$SCRIPTS_DIR/bin"
199 chmod 755 "$SCRIPTS_DIR/bin"
200 dirs='crons data run status'
201
202 safe_mkdir "$TMP_BASE_DIR"
203 for i in $dirs; do
204 safe_mkdir "$TMP_BASE_DIR/$i"
205 done
206}
207
208start_service() {
209 local log_stderr
210 local log_stdout
211 local rxtxrpt
212 local test_setting
213 local probe_key=/etc/atlas/probe_key
214 local probe_pub_key=/etc/atlas/probe_key.pub
215
216 # The link is not saved across sysupgrade, recreate if missing
217 if [ ! -f $PRIV_KEY_FILE ]; then
218 [ -f $probe_key ] && ln -s $probe_key $PRIV_KEY_FILE
219 [ -f $probe_pub_key ] && ln -s $probe_pub_key $PUB_KEY_FILE
220 fi
221
222 # With the precheck done, check if the priv key is actually present
223 if [ ! -f $PRIV_KEY_FILE ]; then
224 print_msg "Missing probe_key. To init the key follow instruction in /etc/atlas/atlas.readme"
225 print_msg "Assuming atlas-sw-probe not init. Exiting..."
226 exit 1
227 fi
228
229 create_tmp_dirs
230
231 config_load atlas
232 config_get_bool log_stderr "common" log_stderr "0"
233 config_get_bool log_stdout "common" log_stdout "0"
234 config_get_bool rxtxrpt "common" rxtxrpt "1"
235 test_setting=$(grep "^[ ]*RXTXRPT=yes" "$STATE_CONFIG")
236
237 # Decide if we should write to permanent storage
238 if [ "$rxtxrpt" == "1" ] && [ -z "$test_setting" ]; then
239 echo "RXTXRPT=yes">$STATE_CONFIG
240 elif [ "$rxtxrpt" == "0" ] && [ ! -z "$test_setting" ]; then
241 echo "RXTXRPT=no">$STATE_CONFIG
242 fi
243
244 procd_open_instance
245 procd_set_param command "$SCRIPTS_DIR/bin/ATLAS"
246 procd_set_param stdout "$log_stdout"
247 procd_set_param stderr "$log_stderr"
248 procd_close_instance
249}