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.

694 lines
20 KiB

  1. #!/bin/sh
  2. ##############################################
  3. # function library used by adblock-update.sh #
  4. # written by Dirk Brenken (dirk@brenken.org) #
  5. ##############################################
  6. #####################################
  7. # f_envload: load adblock environment
  8. #
  9. f_envload()
  10. {
  11. # source in openwrt function library
  12. #
  13. if [ -r "/lib/functions.sh" ]
  14. then
  15. . "/lib/functions.sh" 2>/dev/null
  16. else
  17. rc=500
  18. f_log "openwrt function library not found" "${rc}"
  19. f_deltemp
  20. fi
  21. # source in openwrt json helpers library
  22. #
  23. if [ -r "/usr/share/libubox/jshn.sh" ]
  24. then
  25. . "/usr/share/libubox/jshn.sh" 2>/dev/null
  26. else
  27. rc=505
  28. f_log "openwrt json helpers library not found" "${rc}"
  29. f_deltemp
  30. fi
  31. # get list with all installed openwrt packages
  32. #
  33. pkg_list="$(opkg list-installed 2>/dev/null)"
  34. if [ -z "${pkg_list}" ]
  35. then
  36. rc=510
  37. f_log "empty openwrt package list" "${rc}"
  38. f_deltemp
  39. fi
  40. }
  41. ######################################################
  42. # f_envparse: parse adblock config and set environment
  43. #
  44. f_envparse()
  45. {
  46. # set the C locale, characters are single bytes, the charset is ASCII
  47. # speeds up sort, grep etc.
  48. #
  49. LC_ALL=C
  50. # set initial defaults (may be overwritten by setting appropriate adblock config options)
  51. #
  52. adb_if="adblock"
  53. adb_minspace="20000"
  54. adb_maxtime="60"
  55. adb_maxloop="5"
  56. adb_unique="1"
  57. adb_blacklist="/etc/adblock/adblock.blacklist"
  58. adb_whitelist="/etc/adblock/adblock.whitelist"
  59. # adblock device name auto detection
  60. # derived from first entry in openwrt lan ifname config
  61. #
  62. adb_dev="$(uci get network.lan.ifname 2>/dev/null)"
  63. adb_dev="${adb_dev/ *}"
  64. # adblock ntp server name auto detection
  65. # derived from ntp list found in openwrt ntp server config
  66. #
  67. adb_ntpsrv="$(uci get system.ntp.server 2>/dev/null)"
  68. # function to read/set global options by callback,
  69. # prepare list items and build option list for all others
  70. #
  71. config_cb()
  72. {
  73. local type="${1}"
  74. local name="${2}"
  75. if [ "${type}" = "adblock" ]
  76. then
  77. option_cb()
  78. {
  79. local option="${1}"
  80. local value="${2}"
  81. eval "${option}=\"${value}\""
  82. }
  83. else
  84. option_cb()
  85. {
  86. local option="${1}"
  87. local value="${2}"
  88. local opt_out="$(printf "${option}" | sed -n '/.*_ITEM[0-9]$/p; /.*_LENGTH$/p; /enabled/p' 2>/dev/null)"
  89. if [ -z "${opt_out}" ]
  90. then
  91. all_options="${all_options} ${option}"
  92. fi
  93. }
  94. list_cb()
  95. {
  96. local list="${1}"
  97. local value="${2}"
  98. if [ "${list}" = "adb_wanlist" ]
  99. then
  100. adb_wandev="${adb_wandev} ${value}"
  101. elif [ "${list}" = "adb_ntplist" ]
  102. then
  103. adb_ntpsrv="${adb_ntpsrv} ${value}"
  104. elif [ "${list}" = "adb_catlist" ]
  105. then
  106. adb_cat_shalla="${adb_cat_shalla} ${value}"
  107. fi
  108. }
  109. fi
  110. }
  111. # function to iterate through option list, read/set all options in "enabled" sections
  112. #
  113. parse_config()
  114. {
  115. local config="${1}"
  116. config_get switch "${config}" "enabled"
  117. if [ "${switch}" = "1" ]
  118. then
  119. for option in ${all_options}
  120. do
  121. config_get value "${config}" "${option}"
  122. if [ -n "${value}" ]
  123. then
  124. local opt_src="$(printf "${option}" | sed -n '/^adb_src_[a-z0-9]*$/p' 2>/dev/null)"
  125. if [ -n "${opt_src}" ]
  126. then
  127. adb_sources="${adb_sources} ${value}"
  128. else
  129. eval "${option}=\"${value}\""
  130. fi
  131. fi
  132. done
  133. elif [ "${config}" = "wancheck" ]
  134. then
  135. unset adb_wandev
  136. elif [ "${config}" = "ntpcheck" ]
  137. then
  138. unset adb_ntpsrv
  139. elif [ "${config}" = "shalla" ]
  140. then
  141. unset adb_cat_shalla
  142. fi
  143. }
  144. # load adblock config and start parsing functions
  145. #
  146. config_load adblock
  147. config_foreach parse_config service
  148. config_foreach parse_config source
  149. # set temp variables and defaults
  150. #
  151. adb_tmpfile="$(mktemp -tu 2>/dev/null)"
  152. adb_tmpdir="$(mktemp -p /tmp -d 2>/dev/null)"
  153. unset adb_srcfind
  154. unset adb_revsrcfind
  155. # set adblock source ruleset definitions
  156. #
  157. rset_start="sed -r 's/[[:space:]]|[\[!#/:;_].*|[0-9\.]*localhost.*//g; s/[\^#/:;_\.\t ]*$//g'"
  158. rset_end="sed '/^[#/:;_\s]*$/d'"
  159. rset_adaway="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  160. rset_blacklist="${rset_start} | ${rset_end}"
  161. rset_disconnect="${rset_start} | ${rset_end}"
  162. rset_dshield="${rset_start} | ${rset_end}"
  163. rset_feodo="${rset_start} | ${rset_end}"
  164. rset_malware="${rset_start} | ${rset_end}"
  165. rset_palevo="${rset_start} | ${rset_end}"
  166. rset_shalla="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}$//g' | ${rset_end}"
  167. rset_spam404="${rset_start} | sed 's/^\|\|//g' | ${rset_end}"
  168. rset_whocares="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  169. rset_winhelp="${rset_start} | sed 's/\([0-9]\{1,3\}\.\)\{3\}[0-1]\{1,1\}//g' | ${rset_end}"
  170. rset_yoyo="${rset_start} | sed 's/,/\n/g' | ${rset_end}"
  171. rset_zeus="${rset_start} | ${rset_end}"
  172. # set dnsmasq defaults
  173. #
  174. adb_dnsdir="/tmp/dnsmasq.d"
  175. adb_dnsformat="sed 's/^/address=\//;s/$/\/'${adb_ip}'/'"
  176. adb_dnsprefix="adb_list"
  177. }
  178. #############################################
  179. # f_envcheck: check environment prerequisites
  180. #
  181. f_envcheck()
  182. {
  183. # check adblock config file
  184. #
  185. check_config="$(grep -F "ruleset=rset_default" /etc/config/adblock 2>/dev/null)"
  186. if [ -n "${check_config}" ]
  187. then
  188. rc=515
  189. grep -Fv "#" "/etc/adblock/samples/adblock.conf.sample" > /etc/config/adblock
  190. f_log "new default adblock config applied, please check your configuration settings in /etc/config/adblock" "${rc}"
  191. f_deltemp
  192. fi
  193. # check required config options
  194. #
  195. adb_varlist="adb_ip adb_dev adb_domain"
  196. for var in ${adb_varlist}
  197. do
  198. if [ -z "$(eval printf \"\$"${var}"\")" ]
  199. then
  200. rc=520
  201. f_log "missing adblock config option (${var})" "${rc}"
  202. f_deltemp
  203. fi
  204. done
  205. # check main uhttpd configuration
  206. #
  207. check_uhttpd="$(uci get uhttpd.main.listen_http 2>/dev/null | grep -Fo "0.0.0.0" 2>/dev/null)"
  208. if [ -n "${check_uhttpd}" ]
  209. then
  210. rc=525
  211. lan_ip="$(uci get network.lan.ipaddr 2>/dev/null)"
  212. f_log "please bind main uhttpd instance to LAN only (lan ip: ${lan_ip})" "${rc}"
  213. f_deltemp
  214. fi
  215. # check adblock network device configuration
  216. #
  217. if [ ! -d "/sys/class/net/${adb_dev}" ]
  218. then
  219. rc=530
  220. f_log "invalid adblock network device input (${adb_dev})" "${rc}"
  221. f_deltemp
  222. fi
  223. # check adblock network interface configuration
  224. #
  225. check_if="$(printf "${adb_if}" | sed -n '/[^._0-9A-Za-z]/p' 2>/dev/null)"
  226. banned_if="$(printf "${adb_if}" | sed -n '/.*lan.*\|.*wan.*\|.*switch.*\|main\|globals\|loopback\|px5g/p' 2>/dev/null)"
  227. if [ -n "${check_if}" ] || [ -n "${banned_if}" ]
  228. then
  229. rc=535
  230. f_log "invalid adblock network interface input (${adb_if})" "${rc}"
  231. f_deltemp
  232. fi
  233. # check adblock ip address configuration
  234. #
  235. check_ip="$(printf "${adb_ip}" | sed -n '/\([0-9]\{1,3\}\.\)\{3\}[0-9]\{1,3\}/p' 2>/dev/null)"
  236. lan_ip="$(uci get network.lan.ipaddr 2>/dev/null)"
  237. if [ -z "${check_ip}" ]
  238. then
  239. rc=540
  240. f_log "invalid adblock ip address input (${adb_ip})" "${rc}"
  241. f_deltemp
  242. elif [ "${adb_ip}" = "${lan_ip}" ]
  243. then
  244. rc=545
  245. f_log "adblock ip needs to be a different subnet from the normal LAN (adblock ip: ${adb_ip})" "${rc}"
  246. f_deltemp
  247. fi
  248. # check adblock blacklist/whitelist configuration
  249. #
  250. if [ ! -r "${adb_blacklist}" ]
  251. then
  252. rc=550
  253. f_log "adblock blacklist not found" "${rc}"
  254. f_deltemp
  255. elif [ ! -r "${adb_whitelist}" ]
  256. then
  257. rc=555
  258. f_log "adblock whitelist not found" "${rc}"
  259. f_deltemp
  260. fi
  261. # check adblock temp directory
  262. #
  263. if [ -n "${adb_tmpdir}" ] && [ -d "${adb_tmpdir}" ]
  264. then
  265. f_space "${adb_tmpdir}"
  266. tmp_ok="true"
  267. else
  268. rc=560
  269. tmp_ok="false"
  270. f_log "temp directory not found" "${rc}"
  271. f_deltemp
  272. fi
  273. # check curl package dependency
  274. #
  275. check="$(printf "${pkg_list}" | grep "^curl -" 2>/dev/null)"
  276. if [ -z "${check}" ]
  277. then
  278. rc=565
  279. f_log "curl package not found" "${rc}"
  280. f_deltemp
  281. fi
  282. # check wget package dependency
  283. #
  284. check="$(printf "${pkg_list}" | grep "^wget -" 2>/dev/null)"
  285. if [ -z "${check}" ]
  286. then
  287. rc=570
  288. f_log "wget package not found" "${rc}"
  289. f_deltemp
  290. fi
  291. # check ca-certificates package and set wget/curl options accordingly
  292. #
  293. check="$(printf "${pkg_list}" | grep "^ca-certificates -" 2>/dev/null)"
  294. if [ -z "${check}" ]
  295. then
  296. curl_parm="-q --insecure --silent"
  297. wget_parm="--no-config --no-hsts --no-check-certificate --quiet"
  298. else
  299. curl_parm="-q --silent"
  300. wget_parm="--no-config --no-hsts --quiet"
  301. fi
  302. # check total and swap memory
  303. #
  304. mem_total="$(grep -F "MemTotal" "/proc/meminfo" 2>/dev/null | grep -o "[0-9]*" 2>/dev/null)"
  305. mem_free="$(grep -F "MemFree" "/proc/meminfo" 2>/dev/null | grep -o "[0-9]*" 2>/dev/null)"
  306. swap_total="$(grep -F "SwapTotal" "/proc/meminfo" 2>/dev/null | grep -o "[0-9]*" 2>/dev/null)"
  307. if [ $((mem_total)) -le 64000 ] && [ $((swap_total)) -eq 0 ]
  308. then
  309. adb_unique=0
  310. f_log "overall sort/unique processing will be disabled,"
  311. f_log "please consider adding an external swap device to supersize your /tmp directory (total: ${mem_total}, free: ${mem_free}, swap: ${mem_swap})"
  312. fi
  313. # check backup configuration
  314. #
  315. if [ -n "${adb_backupdir}" ] && [ -d "${adb_backupdir}" ]
  316. then
  317. f_space "${adb_backupdir}"
  318. backup_ok="true"
  319. else
  320. backup_ok="false"
  321. f_log "backup/restore will be disabled"
  322. fi
  323. # check dns query log configuration
  324. #
  325. adb_querydir="${adb_queryfile%/*}"
  326. adb_querypid="/var/run/adb_query.pid"
  327. if [ -n "${adb_querydir}" ] && [ -d "${adb_querydir}" ]
  328. then
  329. # check find capabilities
  330. #
  331. check="$(find --help 2>&1 | grep -F "mtime" 2>/dev/null)"
  332. if [ -z "${check}" ]
  333. then
  334. query_ok="false"
  335. f_log "busybox without 'find/mtime' support (min. r47362), dns query logging will be disabled"
  336. else
  337. f_space "${adb_querydir}"
  338. query_ok="true"
  339. query_name="${adb_queryfile##*/}"
  340. query_ip="${adb_ip//./\\.}"
  341. fi
  342. else
  343. query_ok="false"
  344. f_log "dns query logging will be disabled"
  345. if [ -s "${adb_querypid}" ]
  346. then
  347. kill -9 "$(cat "${adb_querypid}")" >/dev/null 2>&1
  348. f_log "remove old dns query log background process (pid: $(cat "${adb_querypid}" 2>/dev/null))"
  349. > "${adb_querypid}"
  350. fi
  351. fi
  352. # check debug log configuration
  353. #
  354. adb_logdir="${adb_logfile%/*}"
  355. if [ -n "${adb_logdir}" ] && [ -d "${adb_logdir}" ]
  356. then
  357. f_space "${adb_logdir}"
  358. log_ok="true"
  359. else
  360. log_ok="false"
  361. f_log "debug logging will be disabled"
  362. fi
  363. # check wan update configuration
  364. #
  365. if [ -n "${adb_wandev}" ]
  366. then
  367. f_wancheck "${adb_maxloop}"
  368. else
  369. wan_ok="false"
  370. f_log "wan update check will be disabled"
  371. fi
  372. # check ntp sync configuration
  373. #
  374. if [ -n "${adb_ntpsrv}" ]
  375. then
  376. f_ntpcheck "${adb_maxloop}"
  377. else
  378. ntp_ok="false"
  379. f_log "ntp time sync will be disabled"
  380. fi
  381. # check dynamic/volatile adblock network interface configuration
  382. #
  383. rc="$(ifstatus "${adb_if}" >/dev/null 2>&1; printf ${?})"
  384. if [ $((rc)) -ne 0 ]
  385. then
  386. json_init
  387. json_add_string name "${adb_if}"
  388. json_add_string ifname "${adb_dev}"
  389. json_add_string proto "static"
  390. json_add_array ipaddr
  391. json_add_string "" "${adb_ip}"
  392. json_close_array
  393. json_close_object
  394. ubus call network add_dynamic "$(json_dump)"
  395. rc=${?}
  396. if [ $((rc)) -eq 0 ]
  397. then
  398. f_log "created new dynamic/volatile network interface (${adb_if}, ${adb_ip})"
  399. else
  400. f_log "failed to initialize new dynamic/volatile network interface (${adb_if}, ${adb_ip})" "${rc}"
  401. f_remove
  402. fi
  403. fi
  404. # check dynamic/volatile adblock uhttpd instance configuration
  405. #
  406. rc="$(ps | grep "[u]httpd.*\-r ${adb_if}" >/dev/null 2>&1; printf ${?})"
  407. if [ $((rc)) -ne 0 ]
  408. then
  409. uhttpd -h "/www/adblock" -r "${adb_if}" -E "/adblock.html" -p "${adb_ip}:80" >/dev/null 2>&1
  410. rc=${?}
  411. if [ $((rc)) -eq 0 ]
  412. then
  413. f_log "created new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})"
  414. else
  415. f_log "failed to initialize new dynamic/volatile uhttpd instance (${adb_if}, ${adb_ip})" "${rc}"
  416. f_remove
  417. fi
  418. fi
  419. # remove no longer used package list
  420. #
  421. unset pkg_list
  422. }
  423. ################################################
  424. # f_log: log messages to stdout, syslog, logfile
  425. #
  426. f_log()
  427. {
  428. local log_msg="${1}"
  429. local log_rc="${2}"
  430. local class="info "
  431. if [ -n "${log_msg}" ]
  432. then
  433. if [ $((log_rc)) -ne 0 ]
  434. then
  435. class="error"
  436. log_rc=", rc: ${log_rc}"
  437. log_msg="${log_msg}${log_rc}"
  438. fi
  439. /usr/bin/logger -s -t "adblock[${pid}] ${class}" "${log_msg}"
  440. if [ "${log_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
  441. then
  442. printf "%s\n" "$(/bin/date "+%d.%m.%Y %H:%M:%S") adblock[${pid}] ${class}: ${log_msg}" >> "${adb_logfile}"
  443. fi
  444. fi
  445. }
  446. ################################################
  447. # f_space: check mount points/space requirements
  448. #
  449. f_space()
  450. {
  451. local mp="${1}"
  452. if [ -d "${mp}" ]
  453. then
  454. df "${mp}" 2>/dev/null |\
  455. tail -n1 2>/dev/null |\
  456. while read filesystem overall used available scrap
  457. do
  458. av_space="${available}"
  459. if [ $((av_space)) -eq 0 ]
  460. then
  461. rc=575
  462. f_log "no space left on device/not mounted (${mp})" "${rc}"
  463. exit ${rc}
  464. elif [ $((av_space)) -lt $((adb_minspace)) ]
  465. then
  466. rc=580
  467. f_log "not enough space left on device (${mp})" "${rc}"
  468. exit ${rc}
  469. fi
  470. done
  471. rc=${?}
  472. if [ $((rc)) -eq 0 ]
  473. then
  474. space_ok="true"
  475. else
  476. space_ok="false"
  477. f_deltemp
  478. fi
  479. fi
  480. }
  481. ####################################################
  482. # f_deltemp: delete temp files, directories and exit
  483. #
  484. f_deltemp()
  485. {
  486. if [ -f "${adb_tmpfile}" ]
  487. then
  488. rm -f "${adb_tmpfile}" >/dev/null 2>&1
  489. fi
  490. if [ -d "${adb_tmpdir}" ]
  491. then
  492. rm -rf "${adb_tmpdir}" >/dev/null 2>&1
  493. fi
  494. f_log "domain adblock processing finished (${adb_version}, ${openwrt_version}, $(/bin/date "+%d.%m.%Y %H:%M:%S"))"
  495. exit ${rc}
  496. }
  497. ####################################################
  498. # f_remove: maintain and (re-)start domain query log
  499. #
  500. f_remove()
  501. {
  502. local query_pid
  503. local query_date
  504. local query_total
  505. local query_blocked
  506. if [ "${query_ok}" = "true" ] && [ "${ntp_ok}" = "true" ]
  507. then
  508. query_date="$(date "+%Y%m%d")"
  509. if [ -s "${adb_querypid}" ] && [ -f "${adb_queryfile}.${query_date}" ]
  510. then
  511. query_total="$(grep -F "query[A]" "${adb_queryfile}.${query_date}" 2>/dev/null | wc -l)"
  512. query_blocked="$(grep -Fv "query[A]" "${adb_queryfile}.${query_date}" 2>/dev/null | wc -l)"
  513. f_log "adblock statistics for query date ${query_date} (total: ${query_total}, blocked: ${query_blocked})"
  514. fi
  515. if [ -s "${adb_querypid}" ] && [ ! -f "${adb_queryfile}.${query_date}" ]
  516. then
  517. query_pid="$(cat "${adb_querypid}" 2>/dev/null)"
  518. > "${adb_querypid}"
  519. kill -9 "${query_pid}" >/dev/null 2>&1
  520. rc=${?}
  521. if [ $((rc)) -eq 0 ]
  522. then
  523. find "${adb_backupdir}" -maxdepth 1 -type f -mtime +"${adb_queryhistory}" -name "${query_name}.*" -exec rm -f "{}" \; 2>/dev/null
  524. rc=${?}
  525. if [ $((rc)) -eq 0 ]
  526. then
  527. f_log "remove old domain query background process (pid: ${query_pid}) and do logfile housekeeping"
  528. else
  529. f_log "error during domain query logfile housekeeping" "${rc}"
  530. fi
  531. else
  532. f_log "error during domain query background process removal (pid: ${query_pid})" "${rc}"
  533. fi
  534. fi
  535. if [ ! -s "${adb_querypid}" ]
  536. then
  537. (logread -f 2>/dev/null & printf ${!} > "${adb_querypid}") | grep -Eo "(query\[A\].*)|([a-z0-9\.\-]* is ${query_ip}$)" 2>/dev/null >> "${adb_queryfile}.${query_date}" &
  538. rc=${?}
  539. if [ $((rc)) -eq 0 ]
  540. then
  541. sleep 1
  542. f_log "new domain query log background process started (pid: $(cat "${adb_querypid}" 2>/dev/null))"
  543. else
  544. f_log "error during domain query background process start" "${rc}"
  545. fi
  546. fi
  547. fi
  548. f_deltemp
  549. }
  550. ################################################################
  551. # f_restore: restore last adblocklist backup and restart dnsmasq
  552. #
  553. f_restore()
  554. {
  555. # remove bogus adblocklists
  556. #
  557. if [ -n "${adb_revsrclist}" ]
  558. then
  559. find "${adb_dnsdir}" -maxdepth 1 -type f \( ${adb_revsrcfind} \) -exec rm -f "{}" \; 2>/dev/null
  560. if [ $((rc)) -eq 0 ]
  561. then
  562. f_log "bogus adblocklists removed"
  563. else
  564. f_log "error during removal of bogus adblocklists" "${rc}"
  565. f_remove
  566. fi
  567. fi
  568. # restore backups
  569. #
  570. if [ "${backup_ok}" = "true" ] && [ -d "${adb_backupdir}" ] && [ "$(printf "${adb_backupdir}/${adb_dnsprefix}."*)" != "${adb_backupdir}/${adb_dnsprefix}.*" ]
  571. then
  572. cp -f "${adb_backupdir}/${adb_dnsprefix}."* "${adb_dnsdir}" >/dev/null 2>&1
  573. rc=${?}
  574. if [ $((rc)) -eq 0 ]
  575. then
  576. f_log "all available backups restored"
  577. else
  578. f_log "error during restore" "${rc}"
  579. f_remove
  580. fi
  581. fi
  582. /etc/init.d/dnsmasq restart >/dev/null 2>&1
  583. f_remove
  584. }
  585. #######################################################
  586. # f_wancheck: check for usable adblock update interface
  587. #
  588. f_wancheck()
  589. {
  590. local cnt=0
  591. local cnt_max="${1}"
  592. local dev
  593. local dev_out
  594. while [ $((cnt)) -le $((cnt_max)) ]
  595. do
  596. for dev in ${adb_wandev}
  597. do
  598. if [ -d "/sys/class/net/${dev}" ]
  599. then
  600. dev_out="$(cat /sys/class/net/${dev}/operstate 2>/dev/null)"
  601. rc=${?}
  602. if [ "${dev_out}" = "up" ]
  603. then
  604. wan_ok="true"
  605. f_log "get wan/update interface (${dev}), after ${cnt} loops"
  606. break 2
  607. fi
  608. fi
  609. done
  610. sleep 1
  611. cnt=$((cnt + 1))
  612. done
  613. if [ -z "${wan_ok}" ]
  614. then
  615. rc=585
  616. wan_ok="false"
  617. f_log "no wan/update interface(s) found (${adb_wandev# })" "${rc}"
  618. f_restore
  619. fi
  620. }
  621. #####################################
  622. # f_ntpcheck: check/get ntp time sync
  623. #
  624. f_ntpcheck()
  625. {
  626. local cnt=0
  627. local cnt_max="${1}"
  628. local ntp_pool
  629. for srv in ${adb_ntpsrv}
  630. do
  631. ntp_pool="${ntp_pool} -p ${srv}"
  632. done
  633. while [ $((cnt)) -le $((cnt_max)) ]
  634. do
  635. /usr/sbin/ntpd -nq ${ntp_pool} >/dev/null 2>&1
  636. rc=${?}
  637. if [ $((rc)) -eq 0 ]
  638. then
  639. ntp_ok="true"
  640. f_log "get ntp time sync (${adb_ntpsrv# }), after ${cnt} loops"
  641. break
  642. fi
  643. sleep 1
  644. cnt=$((cnt + 1))
  645. done
  646. if [ -z "${ntp_ok}" ]
  647. then
  648. rc=590
  649. ntp_ok="false"
  650. f_log "ntp time sync failed (${adb_ntpsrv# })" "${rc}"
  651. f_restore
  652. fi
  653. }