You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
6.5 KiB

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