net/snort3: Include default configs and snort2lua
[feed/packages.git] / net / travelmate / files / travelmate.sh
1 #!/bin/sh
2 # travelmate, a wlan connection manager for travel router
3 # Copyright (c) 2016-2021 Dirk Brenken (dev@brenken.org)
4 # This is free software, licensed under the GNU General Public License v3.
5
6 # set (s)hellcheck exceptions
7 # shellcheck disable=1091,2016,2039,2059,2086,2143,2181,2188
8
9 export LC_ALL=C
10 export PATH="/usr/sbin:/usr/bin:/sbin:/bin"
11 set -o pipefail
12
13 trm_ver="2.0.3"
14 trm_enabled=0
15 trm_debug=0
16 trm_iface=""
17 trm_captive=1
18 trm_proactive=1
19 trm_netcheck=0
20 trm_autoadd=0
21 trm_randomize=0
22 trm_mail=0
23 trm_vpn=0
24 trm_mailpgm="/etc/travelmate/travelmate.mail"
25 trm_vpnpgm="/etc/travelmate/travelmate.vpn"
26 trm_vpnservice=""
27 trm_scanbuffer=1024
28 trm_minquality=35
29 trm_maxretry=3
30 trm_maxwait=30
31 trm_timeout=60
32 trm_radio=""
33 trm_connection=""
34 trm_wpaflags=""
35 trm_rtfile="/tmp/trm_runtime.json"
36 trm_wifi="$(command -v wifi)"
37 trm_fetch="$(command -v curl)"
38 trm_iwinfo="$(command -v iwinfo)"
39 trm_logger="$(command -v logger)"
40 trm_wpa="$(command -v wpa_supplicant)"
41 trm_captiveurl="http://captive.apple.com"
42 trm_useragent="Mozilla/5.0 (Linux x86_64; rv:80.0) Gecko/20100101 Firefox/80.0"
43 trm_ntpfile="/var/state/travelmate.ntp"
44 trm_vpnfile="/var/state/travelmate.vpn"
45 trm_mailfile="/var/state/travelmate.mail"
46 trm_refreshfile="/var/state/travelmate.refresh"
47 trm_pidfile="/var/run/travelmate.pid"
48 trm_action="${1:-"start"}"
49
50 # load travelmate environment
51 #
52 f_env()
53 {
54 local IFS check wpa_checks ubus_check result
55
56 # do nothing on stop
57 #
58 if [ "${trm_action}" = "stop" ]
59 then
60 return
61 fi
62
63 # (re-)initialize global list variables
64 #
65 unset trm_stalist trm_radiolist trm_uplinklist trm_wpaflags trm_activesta
66
67 # get system information
68 #
69 trm_sysver="$(ubus -S call system board 2>/dev/null | jsonfilter -e '@.model' -e '@.release.description' | \
70 awk 'BEGIN{ORS=", "}{print $0}' | awk '{print substr($0,1,length($0)-2)}')"
71
72 # check travelmate config
73 #
74 if [ ! -r "/etc/config/travelmate" ] || [ -z "$(uci -q show travelmate.global.trm_vpn)" ]
75 then
76 f_log "err" "invalid travelmate config, please re-install the package via opkg with the '--force-reinstall --force-maintainer' options"
77 fi
78
79 # load travelmate config
80 #
81 config_cb()
82 {
83 local name="${1}" type="${2}"
84 if [ "${name}" = "travelmate" ] && [ "${type}" = "global" ]
85 then
86 option_cb()
87 {
88 local option="${1}" value="${2}"
89 eval "${option}=\"${value}\""
90 }
91 else
92 option_cb()
93 {
94 return 0
95 }
96 fi
97 }
98 config_load travelmate
99
100 # check 'enabled' option
101 #
102 if [ "${trm_enabled}" != "1" ]
103 then
104 f_log "info" "travelmate is currently disabled, please set 'trm_enabled' to '1' to use this service"
105 /etc/init.d/travelmate stop
106 fi
107
108 # check ubus network interface
109 #
110 if [ -n "${trm_iface}" ]
111 then
112 ubus_check="$(ubus -t "${trm_maxwait}" wait_for network.wireless network.interface."${trm_iface}" 2>&1)"
113 if [ -n "${ubus_check}" ]
114 then
115 f_log "info" "travelmate interface '${trm_iface}' does not appear on ubus, please check your network setup"
116 /etc/init.d/travelmate stop
117 fi
118 else
119 f_log "info" "travelmate is currently not configured, please use the 'Interface Setup' in LuCI or the 'setup' option in CLI"
120 /etc/init.d/travelmate stop
121 fi
122
123 # check wpa capabilities
124 #
125 wpa_checks="sae owe eap suiteb192"
126 for check in ${wpa_checks}
127 do
128 if [ -x "${trm_wpa}" ]
129 then
130 result="$("${trm_wpa}" -v${check} >/dev/null 2>&1; printf "%u" "${?}")"
131 if [ -z "${trm_wpaflags}" ]
132 then
133 if [ "${result}" = "0" ]
134 then
135 trm_wpaflags="${check}: $(f_char 1)"
136 else
137 trm_wpaflags="${check}: $(f_char 0)"
138 fi
139 else
140 if [ "${result}" = "0" ]
141 then
142 trm_wpaflags="$(f_trim "${trm_wpaflags}, ${check}: $(f_char 1)")"
143 else
144 trm_wpaflags="$(f_trim "${trm_wpaflags}, ${check}: $(f_char 0)")"
145 fi
146 fi
147 fi
148 done
149
150 # get and enable wifi devices
151 #
152 config_load wireless
153 config_foreach f_prepdev wifi-device
154 if [ -n "$(uci -q changes "wireless")" ]
155 then
156 uci_commit "wireless"
157 f_reconf
158 fi
159
160 # load json runtime file
161 #
162 json_load_file "${trm_rtfile}" >/dev/null 2>&1
163 json_select data >/dev/null 2>&1
164 if [ "${?}" != "0" ]
165 then
166 > "${trm_rtfile}"
167 json_init
168 json_add_object "data"
169 fi
170 f_log "debug" "f_env ::: wpa_flags: ${trm_wpaflags}, sys_ver: ${trm_sysver}"
171 }
172
173 # trim helper function
174 #
175 f_trim()
176 {
177 local IFS trim="${1}"
178
179 trim="${trim#"${trim%%[![:space:]]*}"}"
180 trim="${trim%"${trim##*[![:space:]]}"}"
181 printf "%s" "${trim}"
182 }
183
184 # status helper function
185 #
186 f_char()
187 {
188 local result input="${1}"
189
190 if [ "${input}" = "1" ]
191 then
192 result="✔"
193 else
194 result="✘"
195 fi
196 printf "%s" "${result}"
197 }
198
199 # wifi reconf helper function
200 #
201 f_reconf()
202 {
203 local radio tmp_radio cnt="0"
204
205 "${trm_wifi}" reconf
206 for radio in ${trm_radiolist}
207 do
208 while [ "$(ubus -S call network.wireless status | jsonfilter -l1 -e "@.${radio}.up")" != "true" ]
209 do
210 if [ "${cnt}" -ge "${trm_maxwait}" ]
211 then
212 break 2
213 fi
214 if [ "${radio}" != "${tmp_radio}" ]
215 then
216 "${trm_wifi}" up "${radio}"
217 tmp_radio="${radio}"
218 fi
219 cnt="$((cnt+1))"
220 sleep 1
221 done
222 done
223 f_log "debug" "f_reconf ::: radio_list: ${trm_radiolist}, radio: ${radio}, cnt: ${cnt}"
224 }
225
226 # vpn helper function
227 #
228 f_vpn()
229 {
230 local IFS rc action="${1}"
231
232 if [ "${trm_vpn}" = "1" ] && [ -x "${trm_vpnpgm}" ]
233 then
234 if [ "${action}" = "disable" ] || { [ "${action}" = "enable" ] && [ ! -f "${trm_vpnfile}" ]; }
235 then
236 "${trm_vpnpgm}" "${action}" >/dev/null 2>&1
237 rc="${?}"
238 fi
239 if [ "${action}" = "enable" ] && [ "${rc}" = "0" ]
240 then
241 > "${trm_vpnfile}"
242 elif [ "${action}" = "disable" ] && [ -f "${trm_vpnfile}" ]
243 then
244 rm -f "${trm_vpnfile}"
245 fi
246 fi
247 f_log "debug" "f_vpn ::: vpn: ${trm_vpn}, vpnservice: ${trm_vpnservice:-"-"}, vpnpgm: ${trm_vpnpgm}, action: ${action}, rc: ${rc:-"-"}"
248 }
249
250 # mac randomizer helper function
251 #
252 f_mac()
253 {
254 local result ifname action="${1}" section="${2}"
255
256 if [ "${trm_randomize}" = "1" ] && [ "${action}" = "set" ]
257 then
258 result="$(hexdump -n6 -ve '/1 "%.02X "' /dev/random 2>/dev/null | \
259 awk -v local="2,6,A,E" -v seed="$(date +%s)" 'BEGIN{srand(seed)}NR==1{split(local,b,",");seed=int(rand()*4+1);printf "%s%s:%s:%s:%s:%s:%s",substr($1,0,1),b[seed],$2,$3,$4,$5,$6}')"
260 uci_set "wireless" "${section}" "macaddr" "${result}"
261 else
262 result="$(uci_get "wireless" "${section}" "macaddr")"
263 if [ -z "${result}" ]
264 then
265 ifname="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
266 result="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk '/Access Point:/{printf "%s",$3}')"
267 fi
268 fi
269 printf "%s" "${result}"
270 f_log "debug" "f_mac ::: action: ${action:-"-"}, section: ${section:-"-"}, mac: ${result:-"-"}"
271 }
272
273 # track/set travelmate connection information
274 #
275 f_contrack()
276 {
277 local uplink_config radio_config essid_config bssid_config expiry action="${1}" radio="${2}" essid="${3}" bssid="${4}" cnt=0
278
279 while [ "$(uci_get "travelmate" "@uplink[$cnt]" >/dev/null 2>&1; echo $?)" = "0" ]
280 do
281 radio_config="$(uci_get "travelmate" "@uplink[$cnt]" "device")"
282 essid_config="$(uci_get "travelmate" "@uplink[$cnt]" "ssid")"
283 bssid_config="$(uci_get "travelmate" "@uplink[$cnt]" "bssid")"
284 if [ "${radio_config}" = "${radio}" ] && [ "${essid_config}" = "${essid}" ] && [ "${bssid_config}" = "${bssid}" ]
285 then
286 uplink_config="@uplink[$cnt]"
287 fi
288 cnt="$((cnt+1))"
289 done
290 if [ -n "${uplink_config}" ]
291 then
292 case "${action}" in
293 "start")
294 uci_remove "travelmate" "${uplink_config}" "con_start" 2>/dev/null
295 uci_remove "travelmate" "${uplink_config}" "con_end" 2>/dev/null
296 if [ -f "${trm_ntpfile}" ]
297 then
298 uci_set "travelmate" "${uplink_config}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
299 fi
300 ;;
301 "refresh")
302 if [ -f "${trm_ntpfile}" ] && [ -z "$(uci_get "travelmate" "${uplink_config}" "con_start")" ]
303 then
304 uci_set "travelmate" "${uplink_config}" "con_start" "$(date "+%Y.%m.%d-%H:%M:%S")"
305 fi
306 ;;
307 "end")
308 if [ -f "${trm_ntpfile}" ]
309 then
310 uci_set "travelmate" "${uplink_config}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
311 fi
312 ;;
313 "start_expiry")
314 if [ -f "${trm_ntpfile}" ]
315 then
316 expiry="$(uci_get "travelmate" "${uplink_config}" "con_start_expiry")"
317 uci_set "travelmate" "${uplink_config}" "enabled" "0"
318 uci_set "travelmate" "${uplink_config}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
319 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' expired after ${expiry} minutes"
320 fi
321 ;;
322 "end_expiry")
323 if [ -f "${trm_ntpfile}" ]
324 then
325 expiry="$(uci_get "travelmate" "${uplink_config}" "con_end_expiry")"
326 uci_set "travelmate" "${uplink_config}" "enabled" "1"
327 uci_remove "travelmate" "${uplink_config}" "con_start" 2>/dev/null
328 uci_remove "travelmate" "${uplink_config}" "con_end" 2>/dev/null
329 f_log "info" "uplink '${radio}/${essid}/${bssid:-"-"}' re-enabled after ${expiry} minutes"
330 fi
331 ;;
332 "disabled")
333 uci_set "travelmate" "${uplink_config}" "enabled" "0"
334 if [ -f "${trm_ntpfile}" ]
335 then
336 uci_set "travelmate" "${uplink_config}" "con_end" "$(date "+%Y.%m.%d-%H:%M:%S")"
337 fi
338 ;;
339 esac
340 if [ -n "$(uci -q changes "travelmate")" ]
341 then
342 uci_commit "travelmate"
343 if [ ! -f "${trm_refreshfile}" ]
344 then
345 printf "%s" "cfg_reload" > "${trm_refreshfile}"
346 fi
347 fi
348 fi
349 }
350
351 # get/match travelmate uplink option
352 #
353 f_uplink()
354 {
355 local IFS result t_radio t_essid t_bssid t_option="${1}" w_radio="${2}" w_essid="${3}" w_bssid="${4}" cnt=0
356
357 while [ "$(uci_get "travelmate" "@uplink[$cnt]" >/dev/null 2>&1; echo $?)" = "0" ]
358 do
359 t_radio="$(uci_get "travelmate" "@uplink[$cnt]" "device")"
360 t_essid="$(uci_get "travelmate" "@uplink[$cnt]" "ssid")"
361 t_bssid="$(uci_get "travelmate" "@uplink[$cnt]" "bssid")"
362 if [ -n "${w_radio}" ] && [ -n "${w_essid}" ] && \
363 [ "${t_radio}" = "${w_radio}" ] && [ "${t_essid}" = "${w_essid}" ] && [ "${t_bssid}" = "${w_bssid}" ]
364 then
365 result="$(uci_get "travelmate" "@uplink[$cnt]" "${t_option}")"
366 break
367 fi
368 cnt="$((cnt+1))"
369 done
370 printf "%s" "${result}"
371 f_log "debug" "f_uplink ::: option: ${t_option}, result: ${result}"
372 }
373
374 # prepare the 'wifi-device' sections
375 #
376 f_prepdev()
377 {
378 local IFS disabled radio="${1}"
379
380 disabled="$(uci_get "wireless" "${radio}" "disabled")"
381 if [ "${disabled}" = "1" ]
382 then
383 uci_set wireless "${radio}" disabled 0
384 fi
385
386 if [ -z "${trm_radio}" ] && [ -z "$(printf "%s" "${trm_radiolist}" | grep -Fo "${radio}")" ]
387 then
388 trm_radiolist="$(f_trim "${trm_radiolist} ${radio}")"
389 elif [ -n "${trm_radio}" ] && [ -z "${trm_radiolist}" ]
390 then
391 trm_radiolist="$(f_trim "$(printf "%s" "${trm_radio}" | \
392 awk '{while(match(tolower($0),/[a-z0-9_]+/)){ORS=" ";print substr(tolower($0),RSTART,RLENGTH);$0=substr($0,RSTART+RLENGTH)}}')")"
393 fi
394 f_log "debug" "f_prepdev ::: trm_radio: ${trm_radio:-"-"}, radio: ${radio}, radio_list: ${trm_radiolist:-"-"}, disabled: ${disabled:-"-"}"
395 }
396
397 # add open uplink to new 'wifi-iface' section
398 #
399 f_addif()
400 {
401 local IFS uci_cfg offset=1 radio="${1}" essid="${2}"
402
403 config_cb()
404 {
405 local type="${1}" name="${2}"
406 if [ "${type}" = "wifi-iface" ]
407 then
408 if [ "$(uci -q get "wireless.${name}.ssid")" = "${essid}" ]
409 then
410 offset=0
411 elif [ "${offset}" != "0" ]
412 then
413 offset="$((offset+1))"
414 fi
415 fi
416 return "${offset}"
417 }
418 config_load wireless
419
420 if [ "${offset}" != "0" ]
421 then
422 uci_cfg="trm_uplink${offset}"
423 while [ -n "$(uci -q get "wireless.${uci_cfg}")" ]
424 do
425 offset="$((offset+1))"
426 uci_cfg="trm_uplink${offset}"
427 done
428 uci -q batch <<-EOC
429 set wireless."${uci_cfg}"="wifi-iface"
430 set wireless."${uci_cfg}".mode="sta"
431 set wireless."${uci_cfg}".network="${trm_iface}"
432 set wireless."${uci_cfg}".device="${radio}"
433 set wireless."${uci_cfg}".ssid="${essid}"
434 set wireless."${uci_cfg}".encryption="none"
435 set wireless."${uci_cfg}".disabled="1"
436 EOC
437 uci_cfg="$(uci -q add travelmate uplink)"
438 uci -q batch <<-EOC
439 set travelmate."${uci_cfg}".device="${radio}"
440 set travelmate."${uci_cfg}".ssid="${essid}"
441 set travelmate."${uci_cfg}".con_start_expiry="0"
442 set travelmate."${uci_cfg}".con_end_expiry="0"
443 set travelmate."${uci_cfg}".enabled="1"
444 EOC
445 if [ -n "$(uci -q changes "travelmate")" ] || [ -n "$(uci -q changes "wireless")" ]
446 then
447 uci_commit "travelmate"
448 uci_commit "wireless"
449 f_reconf
450 if [ ! -f "${trm_refreshfile}" ]
451 then
452 printf "%s" "ui_reload" > "${trm_refreshfile}"
453 fi
454 f_log "info" "open uplink '${radio}/${essid}' added to wireless config"
455 fi
456 fi
457 f_log "debug" "f_addif ::: radio: ${radio:-"-"}, essid: ${essid}, offset: ${offset:-"-"}"
458 }
459
460 # prepare the 'wifi-iface' sections
461 #
462 f_prepif()
463 {
464 local IFS mode radio essid bssid disabled status con_start con_end con_start_expiry con_end_expiry section="${1}" proactive="${2}"
465
466 mode="$(uci_get "wireless" "${section}" "mode")"
467 radio="$(uci_get "wireless" "${section}" "device")"
468 essid="$(uci_get "wireless" "${section}" "ssid")"
469 bssid="$(uci_get "wireless" "${section}" "bssid")"
470 disabled="$(uci_get "wireless" "${section}" "disabled")"
471 status="$(f_uplink "enabled" "${radio}" "${essid}" "${bssid}")"
472 con_start="$(f_uplink "con_start" "${radio}" "${essid}" "${bssid}")"
473 con_end="$(f_uplink "con_end" "${radio}" "${essid}" "${bssid}")"
474 con_start_expiry="$(f_uplink "con_start_expiry" "${radio}" "${essid}" "${bssid}")"
475 con_end_expiry="$(f_uplink "con_end_expiry" "${radio}" "${essid}" "${bssid}")"
476
477 if [ "${status}" = "0" ] && [ -n "${con_end}" ] && [ -n "${con_end_expiry}" ] && [ "${con_end_expiry}" != "0" ]
478 then
479 d1="$(date -d "${con_end}" "+%s")"
480 d2="$(date "+%s")"
481 d3="$(((d2-d1)/60))"
482 if [ "${d3}" -ge "${con_end_expiry}" ]
483 then
484 status="1"
485 f_contrack "end_expiry" "${radio}" "${essid}" "${bssid}"
486 fi
487 elif [ "${status}" = "1" ] && [ -n "${con_start}" ] && [ -n "${con_start_expiry}" ] && [ "${con_start_expiry}" != "0" ]
488 then
489 d1="$(date -d "${con_start}" "+%s")"
490 d2="$(date "+%s")"
491 d3="$((d1+(con_start_expiry*60)))"
492 if [ "${d2}" -gt "${d3}" ]
493 then
494 status="0"
495 f_contrack "start_expiry" "${radio}" "${essid}" "${bssid}"
496 fi
497 fi
498
499 if [ "${mode}" = "sta" ]
500 then
501 if [ "${status}" = "0" ] || \
502 { { [ -z "${disabled}" ] || [ "${disabled}" = "0" ]; } && { [ "${proactive}" = "0" ] || [ "${trm_ifstatus}" != "true" ]; } }
503 then
504 uci_set "wireless" "${section}" "disabled" "1"
505 elif [ "${disabled}" = "0" ] && [ "${trm_ifstatus}" = "true" ] && [ "${proactive}" = "1" ]
506 then
507 if [ -z "${trm_activesta}" ]
508 then
509 trm_activesta="${section}"
510 else
511 uci_set "wireless" "${section}" "disabled" "1"
512 fi
513 fi
514 if [ "${status}" = "1" ]
515 then
516 trm_stalist="$(f_trim "${trm_stalist} ${section}-${radio}")"
517 fi
518 fi
519 f_log "debug" "f_prepif ::: status: ${status}, section: ${section}, active_sta: ${trm_activesta:-"-"}"
520 }
521
522 # check net status
523 #
524 f_net()
525 {
526 local IFS err err_rc err_domain json_raw json_cp json_rc cp_domain result="net nok"
527
528 json_raw="$(${trm_fetch} --user-agent "${trm_useragent}" --referer "http://www.example.com" --write-out "%{json}" --silent --show-error --connect-timeout $((trm_maxwait/10)) "${trm_captiveurl}" 2>/tmp/trm_fetch.err)"
529 json_raw="${json_raw#*\{}"
530 if [ -s "/tmp/trm_fetch.err" ]
531 then
532 err="$(awk 'BEGIN{FS="[()'\'' ]"}{printf "%s %s",$3,$(NF-1)}' "/tmp/trm_fetch.err")"
533 err_rc="${err% *}"
534 err_domain="${err#* }"
535 if [ "${err_rc}" = "6" ]
536 then
537 if [ -n "${err_domain}" ] && [ "${err_domain}" != "timed" ] && [ "${err_domain}" != "${trm_captiveurl#http*://*}" ]
538 then
539 result="net cp '${err_domain}'"
540 fi
541 fi
542 elif [ -n "${json_raw}" ]
543 then
544 json_cp="$(printf "%s" "{${json_raw}" | jsonfilter -l1 -e '@.redirect_url' 2>/dev/null)"
545 json_rc="$(printf "%s" "{${json_raw}" | jsonfilter -l1 -e '@.response_code' 2>/dev/null)"
546 if [ -n "${json_cp}" ]
547 then
548 cp_domain="${json_cp#http*://*}"
549 cp_domain="${cp_domain%%/*}"
550 result="net cp '${cp_domain}'"
551 else
552 if [ "${json_rc}" = "200" ] || [ "${json_rc}" = "204" ]
553 then
554 result="net ok"
555 fi
556 fi
557 fi
558 rm -f "/tmp/trm_fetch.err"
559 printf "%s" "${result}"
560 f_log "debug" "f_net ::: fetch: ${trm_fetch}, timeout: $((trm_maxwait/6)), url: ${trm_captiveurl}, user_agent: ${trm_useragent}, result: ${result}, error: ${err:-"-"}"
561 }
562
563 # check interface status
564 #
565 f_check()
566 {
567 local IFS ifname radio dev_status result login_script login_script_args cp_domain wait_time="1" enabled="1" mode="${1}" status="${2}" sta_radio="${3}" sta_essid="${4}" sta_bssid="${5}"
568
569 if [ "${mode}" = "initial" ] || [ "${mode}" = "dev" ]
570 then
571 json_get_var station_id "station_id"
572 sta_radio="${station_id%%/*}"
573 sta_essid="${station_id%/*}"
574 sta_essid="${sta_essid#*/}"
575 sta_bssid="${station_id##*/}"
576 sta_bssid="${sta_bssid//-/}"
577 fi
578 if [ "${mode}" != "rev" ] && [ -n "${sta_radio}" ] && [ "${sta_radio}" != "-" ] && [ -n "${sta_essid}" ] && [ "${sta_essid}" != "-" ]
579 then
580 enabled="$(f_uplink "enabled" "${sta_radio}" "${sta_essid}" "${sta_bssid}")"
581 fi
582 if { [ "${mode}" != "initial" ] && [ "${mode}" != "dev" ] && [ "${status}" = "false" ]; } || \
583 { [ "${mode}" = "dev" ] && { [ "${status}" = "false" ] || { [ "${trm_ifstatus}" != "${status}" ] && [ "${enabled}" = "0" ]; }; }; }
584 then
585 f_reconf
586 fi
587 while [ "${wait_time}" -le "${trm_maxwait}" ]
588 do
589 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
590 if [ -n "${dev_status}" ]
591 then
592 if [ "${mode}" = "dev" ]
593 then
594 if [ "${trm_ifstatus}" != "${status}" ]
595 then
596 trm_ifstatus="${status}"
597 f_jsnup
598 fi
599 if [ "${status}" = "false" ]
600 then
601 sleep "$((trm_maxwait/5))"
602 fi
603 break
604 elif [ "${mode}" = "rev" ]
605 then
606 break
607 else
608 ifname="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].ifname')"
609 if [ -n "${ifname}" ] && [ "${enabled}" = "1" ]
610 then
611 result="$(f_net)"
612 trm_ifquality="$(${trm_iwinfo} "${ifname}" info 2>/dev/null | awk -F '[ ]' '/Link Quality:/{split($NF,var0,"/");printf "%i\n",(var0[1]*100/var0[2])}')"
613 if [ "${trm_ifquality}" -ge "${trm_minquality}" ]
614 then
615 trm_ifstatus="$(ubus -S call network.interface dump 2>/dev/null | jsonfilter -l1 -e "@.interface[@.device=\"${ifname}\"].up")"
616 if [ "${trm_ifstatus}" = "true" ]
617 then
618 if [ "${trm_captive}" = "1" ]
619 then
620 cp_domain="$(printf "%s" "${result}" | awk -F '['\''| ]' '/^net cp/{printf "%s",$4}')"
621 if [ -x "/etc/init.d/dnsmasq" ] && [ -f "/etc/config/dhcp" ] && \
622 [ -n "${cp_domain}" ] && [ -z "$(uci_get "dhcp" "@dnsmasq[0]" "rebind_domain" | grep -Fo "${cp_domain}")" ]
623 then
624 uci_add_list "dhcp" "@dnsmasq[0]" "rebind_domain" "${cp_domain}"
625 uci_commit "dhcp"
626 /etc/init.d/dnsmasq reload
627 f_log "info" "captive portal domain '${cp_domain}' added to to dhcp rebind whitelist"
628 fi
629 if [ -n "${cp_domain}" ] && [ "${trm_captive}" = "1" ]
630 then
631 trm_connection="${result:-"-"}/${trm_ifquality}"
632 f_jsnup
633 login_script="$(f_uplink "script" "${sta_radio}" "${sta_essid}" "${sta_bssid}")"
634 if [ -x "${login_script}" ]
635 then
636 login_script_args="$(f_uplink "script_args" "${sta_radio}" "${sta_essid}" "${sta_bssid}")"
637 "${login_script}" ${login_script_args} >/dev/null 2>&1
638 rc="${?}"
639 f_log "info" "captive portal login '${login_script:0:40} ${login_script_args:0:20}' for '${cp_domain}' has been executed with rc '${rc}'"
640 if [ "${rc}" = "0" ]
641 then
642 result="$(f_net)"
643 fi
644 fi
645 fi
646 fi
647 if [ "${trm_netcheck}" = "1" ] && [ "${result}" = "net nok" ]
648 then
649 f_log "info" "uplink has no internet (new connection)"
650 f_vpn "disable"
651 trm_ifstatus="${status}"
652 f_jsnup
653 break
654 fi
655 trm_connection="${result:-"-"}/${trm_ifquality}"
656 f_jsnup
657 break
658 fi
659 elif [ -n "${trm_connection}" ]
660 then
661 if [ "${trm_ifquality}" -lt "${trm_minquality}" ]
662 then
663 f_log "info" "uplink is out of range (${trm_ifquality}/${trm_minquality})"
664 f_vpn "disable"
665 unset trm_connection
666 trm_ifstatus="${status}"
667 f_contrack "end" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
668 elif [ "${trm_netcheck}" = "1" ] && [ "${result}" = "net nok" ]
669 then
670 f_log "info" "uplink has no internet (existing connection)"
671 f_vpn "disable"
672 unset trm_connection
673 trm_ifstatus="${status}"
674 fi
675 f_jsnup
676 break
677 elif [ "${mode}" = "initial" ]
678 then
679 trm_ifstatus="${status}"
680 f_jsnup
681 break
682 fi
683 elif [ -n "${trm_connection}" ]
684 then
685 f_vpn "disable"
686 unset trm_connection
687 trm_ifstatus="${status}"
688 f_jsnup
689 break
690 elif [ "${mode}" = "initial" ]
691 then
692 trm_ifstatus="${status}"
693 f_jsnup
694 break
695 fi
696 fi
697 fi
698 if [ "${mode}" = "initial" ]
699 then
700 trm_ifstatus="${status}"
701 f_jsnup
702 break
703 fi
704 wait_time="$((wait_time+1))"
705 sleep 1
706 done
707 f_log "debug" "f_check ::: mode: ${mode}, name: ${ifname:-"-"}, status: ${trm_ifstatus}, enabled: ${enabled}, connection: ${trm_connection:-"-"}, wait: ${wait_time}, max_wait: ${trm_maxwait}, min_quality: ${trm_minquality}, captive: ${trm_captive}, netcheck: ${trm_netcheck}"
708 }
709
710 # update runtime information
711 #
712 f_jsnup()
713 {
714 local IFS section last_date last_station sta_iface sta_radio sta_essid sta_bssid sta_mac dev_status last_status status="${trm_ifstatus}" ntp_done="0" vpn_done="0" mail_done="0"
715
716 if [ "${status}" = "true" ]
717 then
718 status="connected (${trm_connection:-"-"})"
719 dev_status="$(ubus -S call network.wireless status 2>/dev/null)"
720 if [ -n "${dev_status}" ]
721 then
722 section="$(printf "%s" "${dev_status}" | jsonfilter -l1 -e '@.*.interfaces[@.config.mode="sta"].section')"
723 if [ -n "${section}" ]
724 then
725 sta_iface="$(uci_get "wireless" "${section}" "network")"
726 sta_radio="$(uci_get "wireless" "${section}" "device")"
727 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
728 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
729 sta_mac="$(f_mac "get" "${section}")"
730 fi
731 fi
732 json_get_var last_date "last_run"
733 json_get_var last_station "station_id"
734 json_get_var last_status "travelmate_status"
735
736 if { [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]; } || [ "${last_status}" = "running (not connected)" ] || \
737 { [ -n "${last_station}" ] && [ "${last_station}" != "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}" ]; }
738 then
739 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
740 if [ -f "${trm_ntpfile}" ] && [ ! -s "${trm_ntpfile}" ]
741 then
742 printf "%s" "${last_date}" > "${trm_ntpfile}"
743 fi
744 fi
745 elif [ "${status}" = "error" ]
746 then
747 unset trm_connection
748 status="program error"
749 else
750 unset trm_connection
751 status="running (not connected)"
752 fi
753
754 if [ -z "${last_date}" ]
755 then
756 last_date="$(date "+%Y.%m.%d-%H:%M:%S")"
757 fi
758 if [ -s "${trm_ntpfile}" ]
759 then
760 ntp_done="1"
761 fi
762 if [ "${trm_vpn}" = "1" ] && [ -f "${trm_vpnfile}" ]
763 then
764 vpn_done="1"
765 fi
766 if [ "${trm_mail}" = "1" ] && [ -f "${trm_mailfile}" ]
767 then
768 mail_done="1"
769 fi
770 json_add_string "travelmate_status" "${status}"
771 json_add_string "travelmate_version" "${trm_ver}"
772 json_add_string "station_id" "${sta_radio:-"-"}/${sta_essid:-"-"}/${sta_bssid:-"-"}"
773 json_add_string "station_mac" "${sta_mac:-"-"}"
774 json_add_string "station_interface" "${sta_iface:-"-"}"
775 json_add_string "wpa_flags" "${trm_wpaflags:-"-"}"
776 json_add_string "run_flags" "captive: $(f_char ${trm_captive}), proactive: $(f_char ${trm_proactive}), netcheck: $(f_char ${trm_netcheck}), autoadd: $(f_char ${trm_autoadd}), randomize: $(f_char ${trm_randomize})"
777 json_add_string "ext_hooks" "ntp: $(f_char ${ntp_done}), vpn: $(f_char ${vpn_done}), mail: $(f_char ${mail_done})"
778 json_add_string "last_run" "${last_date}"
779 json_add_string "system" "${trm_sysver}"
780 json_dump > "${trm_rtfile}"
781
782 if [ "${status%% (net ok/*}" = "connected" ]
783 then
784 f_vpn "enable"
785 if [ "${trm_mail}" = "1" ] && [ -x "${trm_mailpgm}" ] && [ "${ntp_done}" = "1" ] && [ "${mail_done}" = "0" ]
786 then
787 if [ "${trm_vpn}" = "0" ] || [ "${vpn_done}" = "1" ]
788 then
789 > "${trm_mailfile}"
790 "${trm_mailpgm}" >/dev/null 2>&1
791 fi
792 fi
793 else
794 f_vpn "disable"
795 fi
796 f_log "debug" "f_jsnup ::: section: ${section:-"-"}, status: ${status:-"-"}, sta_iface: ${sta_iface:-"-"}, sta_radio: ${sta_radio:-"-"}, sta_essid: ${sta_essid:-"-"}, sta_bssid: ${sta_bssid:-"-"}, ntp: ${ntp_done}, vpn: ${trm_vpn}/${vpn_done}, mail: ${trm_mail}/${mail_done}"
797 }
798
799 # write to syslog
800 #
801 f_log()
802 {
803 local IFS class="${1}" log_msg="${2}"
804
805 if [ -n "${log_msg}" ] && { [ "${class}" != "debug" ] || [ "${trm_debug}" = "1" ]; }
806 then
807 if [ -x "${trm_logger}" ]
808 then
809 "${trm_logger}" -p "${class}" -t "trm-${trm_ver}[${$}]" "${log_msg}"
810 else
811 printf "%s %s %s\\n" "${class}" "trm-${trm_ver}[${$}]" "${log_msg}"
812 fi
813 if [ "${class}" = "err" ]
814 then
815 trm_ifstatus="error"
816 f_jsnup
817 > "${trm_pidfile}"
818 exit 1
819 fi
820 fi
821 }
822
823 # main function for connection handling
824 #
825 f_main()
826 {
827 local IFS cnt retrycnt spec scan_dev scan_list scan_essid scan_bssid scan_open scan_quality
828 local station_id section sta sta_essid sta_bssid sta_radio sta_iface sta_mac config_essid config_bssid config_radio
829
830 f_check "initial" "false"
831 f_log "debug" "f_main ::: status: ${trm_ifstatus}, proactive: ${trm_proactive}"
832 if [ "${trm_ifstatus}" != "true" ] || [ "${trm_proactive}" = "1" ]
833 then
834 config_load wireless
835 config_foreach f_prepif wifi-iface ${trm_proactive}
836 if [ "${trm_ifstatus}" = "true" ] && [ -n "${trm_activesta}" ] && [ "${trm_proactive}" = "1" ]
837 then
838 json_get_var station_id "station_id"
839 config_radio="${station_id%%/*}"
840 config_essid="${station_id%/*}"
841 config_essid="${config_essid#*/}"
842 config_bssid="${station_id##*/}"
843 config_bssid="${config_bssid//-/}"
844 f_check "dev" "true"
845 f_log "debug" "f_main ::: config_radio: ${config_radio}, config_essid: \"${config_essid}\", config_bssid: ${config_bssid:-"-"}"
846 else
847 uci_commit "wireless"
848 f_check "dev" "false"
849 fi
850 f_log "debug" "f_main ::: radio_list: ${trm_radiolist}, sta_list: ${trm_stalist:0:${trm_scanbuffer}}"
851
852 # radio loop
853 #
854 for radio in ${trm_radiolist}
855 do
856 if [ -z "$(printf "%s" "${trm_stalist}" | grep -o "\\-${radio}")" ]
857 then
858 f_log "info" "no station on radio '${radio}'"
859 continue
860 fi
861
862 # station loop
863 #
864 for sta in ${trm_stalist}
865 do
866 section="${sta%%-*}"
867 sta_radio="$(uci_get "wireless" "${section}" "device")"
868 sta_essid="$(uci_get "wireless" "${section}" "ssid")"
869 sta_bssid="$(uci_get "wireless" "${section}" "bssid")"
870 sta_iface="$(uci_get "wireless" "${section}" "network")"
871 sta_mac="$(f_mac "get" "${section}")"
872 if [ -z "${sta_radio}" ] || [ -z "${sta_essid}" ] || [ -z "${sta_iface}" ]
873 then
874 f_log "info" "invalid wireless section '${section}'"
875 continue
876 fi
877 if [ "${sta_radio}" = "${config_radio}" ] && [ "${sta_essid}" = "${config_essid}" ] && [ "${sta_bssid}" = "${config_bssid}" ]
878 then
879 f_contrack "refresh" "${config_radio}" "${config_essid}" "${config_bssid}"
880 f_log "info" "uplink still in range '${config_radio}/${config_essid}/${config_bssid:-"-"}' with mac '${sta_mac:-"-"}'"
881 break 2
882 fi
883 f_log "debug" "f_main ::: sta_radio: ${sta_radio}, sta_essid: \"${sta_essid}\", sta_bssid: ${sta_bssid:-"-"}"
884 if [ -z "${scan_list}" ]
885 then
886 scan_dev="$(ubus -S call network.wireless status 2>/dev/null | jsonfilter -l1 -e "@.${radio}.interfaces[0].ifname")"
887 scan_list="$("${trm_iwinfo}" "${scan_dev:-${radio}}" scan 2>/dev/null | \
888 awk 'BEGIN{FS="[[:space:]]"}/Address:/{var1=$NF}/ESSID:/{var2="";for(i=12;i<=NF;i++)if(var2==""){var2=$i}else{var2=var2" "$i};
889 gsub(/,/,".",var2)}/Quality:/{split($NF,var0,"/")}/Encryption:/{if($NF=="none"){var3="+"}else{var3="-"};printf "%i,%s,%s,%s\n",(var0[1]*100/var0[2]),var1,var2,var3}' | \
890 sort -rn | awk -v buf="${trm_scanbuffer}" 'BEGIN{ORS=","}{print substr($0,1,buf)}')"
891 f_log "debug" "f_main ::: radio: ${radio}, scan_device: ${scan_dev}, scan_buffer: ${trm_scanbuffer}, scan_list: ${scan_list:-"-"}"
892 if [ -z "${scan_list}" ]
893 then
894 f_log "info" "no scan results on '${radio}'"
895 continue 2
896 fi
897 fi
898
899 # scan loop
900 #
901 IFS=","
902 for spec in ${scan_list}
903 do
904 if [ -z "${scan_quality}" ]
905 then
906 scan_quality="${spec}"
907 elif [ -z "${scan_bssid}" ]
908 then
909 scan_bssid="${spec}"
910 elif [ -z "${scan_essid}" ]
911 then
912 scan_essid="${spec}"
913 elif [ -z "${scan_open}" ]
914 then
915 scan_open="${spec}"
916 fi
917 if [ -n "${scan_quality}" ] && [ -n "${scan_bssid}" ] && [ -n "${scan_essid}" ] && [ -n "${scan_open}" ]
918 then
919 if [ "${scan_quality}" -ge "${trm_minquality}" ]
920 then
921 if { { [ "${scan_essid}" = "\"${sta_essid//,/.}\"" ] && { [ -z "${sta_bssid}" ] || [ "${scan_bssid}" = "${sta_bssid}" ]; } } || \
922 { [ "${scan_bssid}" = "${sta_bssid}" ] && [ "${scan_essid}" = "unknown" ]; } } && [ "${radio}" = "${sta_radio}" ]
923 then
924 f_vpn "disable"
925 f_log "debug" "f_main ::: scan_quality: ${scan_quality}, scan_essid: ${scan_essid}, scan_bssid: ${scan_bssid:-"-"}, scan_open: ${scan_open}"
926 if [ -n "${config_radio}" ]
927 then
928 uci_set "wireless" "${trm_activesta}" "disabled" "1"
929 uci_commit "wireless"
930 f_contrack "end" "${config_radio}" "${config_essid}" "${config_bssid}"
931 f_log "info" "uplink connection terminated '${config_radio}/${config_essid}/${config_bssid:-"-"}'"
932 unset trm_connection config_radio config_essid config_bssid
933 fi
934
935 # retry loop
936 #
937 retrycnt=1
938 trm_radio="${sta_radio}"
939 while [ "${retrycnt}" -le "${trm_maxretry}" ]
940 do
941 if [ "${trm_randomize}" = "1" ]
942 then
943 sta_mac="$(f_mac "set" "${section}")"
944 fi
945 uci_set "wireless" "${section}" "disabled" "0"
946 f_check "sta" "false" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
947 if [ "${trm_ifstatus}" = "true" ]
948 then
949 unset IFS scan_list
950 rm -f "${trm_mailfile}"
951 uci_commit "wireless"
952 f_contrack "start" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
953 if [ "${trm_randomize}" = "0" ]
954 then
955 sta_mac="$(f_mac "get" "${section}")"
956 fi
957 f_log "info" "connected to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' with mac '${sta_mac:-"-"}' (${retrycnt}/${trm_maxretry})"
958 return 0
959 else
960 uci -q revert "wireless"
961 f_check "rev" "false"
962 if [ "${retrycnt}" = "${trm_maxretry}" ]
963 then
964 f_contrack "disabled" "${sta_radio}" "${sta_essid}" "${sta_bssid}"
965 f_log "info" "uplink has been disabled '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
966 break 2
967 else
968 f_jsnup
969 f_log "info" "can't connect to uplink '${sta_radio}/${sta_essid}/${sta_bssid:-"-"}' (${retrycnt}/${trm_maxretry})"
970 fi
971 fi
972 retrycnt="$((retrycnt+1))"
973 sleep "$((trm_maxwait/6))"
974 done
975 elif [ "${trm_autoadd}" = "1" ] && [ "${scan_open}" = "+" ] && [ "${scan_essid}" != "unknown" ]
976 then
977 scan_essid="${scan_essid%?}"
978 scan_essid="${scan_essid:1}"
979 f_addif "${sta_radio}" "${scan_essid}"
980 fi
981 unset scan_quality scan_bssid scan_essid scan_open
982 continue
983 else
984 unset scan_quality scan_bssid scan_essid scan_open
985 continue
986 fi
987 fi
988 done
989 unset IFS scan_quality scan_bssid scan_essid scan_open
990 done
991 unset scan_list
992 done
993 fi
994 }
995
996 # source required system libraries
997 #
998 if [ -r "/lib/functions.sh" ] && [ -r "/usr/share/libubox/jshn.sh" ]
999 then
1000 . "/lib/functions.sh"
1001 . "/usr/share/libubox/jshn.sh"
1002 else
1003 f_log "err" "system libraries not found"
1004 fi
1005
1006 # control travelmate actions
1007 #
1008 if [ "${trm_action}" != "stop" ]
1009 then
1010 f_env
1011 fi
1012 while true
1013 do
1014 if [ -z "${trm_action}" ]
1015 then
1016 rc=0
1017 while true
1018 do
1019 if [ "${rc}" = "0" ]
1020 then
1021 f_check "initial" "false"
1022 fi
1023 sleep "${trm_timeout}" 0
1024 rc=${?}
1025 if [ "${rc}" != "0" ]
1026 then
1027 f_check "initial" "false"
1028 fi
1029 if [ "${rc}" = "0" ] || { [ "${rc}" != "0" ] && [ "${trm_ifstatus}" = "false" ]; }
1030 then
1031 break
1032 fi
1033 done
1034 elif [ "${trm_action}" = "stop" ]
1035 then
1036 if [ -s "${trm_pidfile}" ]
1037 then
1038 f_log "info" "travelmate instance stopped ::: action: ${trm_action}, pid: $(cat ${trm_pidfile} 2>/dev/null)"
1039 > "${trm_rtfile}"
1040 > "${trm_pidfile}"
1041 fi
1042 break
1043 else
1044 f_log "info" "travelmate instance started ::: action: ${trm_action}, pid: ${$}"
1045 fi
1046 json_cleanup
1047 f_env
1048 f_main
1049 unset trm_action
1050 done