#!/bin/sh # rpcd plugin "esim-tools" for the LuCI page (installed as /usr/libexec/rpcd/esim-tools). # # status settings, where the AT/MBIM ports are (USB path, driver), interface, # ModemManager, dependencies, kit version, auto-update, history # modem AT state: ATI, slot, SIM, operator, registration, signal # profiles cached lpac answers (profile list, chip info) with their age # run start a whitelisted esim command as the background job # job the job's command, state, exit code and output # cancel stop the job: esim gets TERM and still restarts ModemManager # at one raw AT command to AT_PORT # config_set AT_PORT / WDM / IFACE into /etc/esim-tools/esim.conf # update_check latest commit on the git server vs the installed one # sms_get SMS -> Telegram settings (the token only as set / not set) + log # sms_set save them; the cron line follows SMS_FORWARD # # rpcd kills calls after ~30 s; anything with lpac or a modem reset takes # longer (ModemManager stop/start), hence the job. . /usr/share/libubox/jshn.sh export PATH=/usr/sbin:/usr/bin:/sbin:/bin umask 022 KIT_DIR=/etc/esim-tools CONF=$KIT_DIR/esim.conf CACHE=$KIT_DIR/cache HISTORY=$KIT_DIR/history.log JOB=/tmp/esim-tools-job b() { "$@" >/dev/null 2>&1 && echo 1 || echo 0; } job_running() { [ -f "$JOB/pid" ] && kill -0 "$(cat "$JOB/pid")" 2>/dev/null; } reply_error() { json_init json_add_string error "$1" json_dump exit 0 } # AT_PORT, WDM, IFACE with the script's defaults load_config() { esim config > /tmp/esim-tools-cfg.$$ 2>/dev/null while read -r k v; do eval "cfg_$k=\$v" done < /tmp/esim-tools-cfg.$$ rm -f /tmp/esim-tools-cfg.$$ } # port_info : fields of the current json object - which USB device and # interface the node belongs to (the M.2 modem shows up as a USB device) port_info() { n=${1##*/} case "$n" in cdc-wdm*) s=/sys/class/usbmisc/$n/device ;; wwan*) s=/sys/class/wwan/$n/device ;; *) s=/sys/class/tty/$n/device ;; esac json_add_string dev "$1" json_add_boolean exists "$(b test -e "$1")" i=$(readlink -f "$s" 2>/dev/null) while [ -n "$i" ] && [ ! -f "$i/bInterfaceNumber" ]; do i=${i%/*} done [ -n "$i" ] || return 0 u=${i%/*} json_add_string ifnum "$(cat "$i/bInterfaceNumber")" json_add_string driver "$(basename "$(readlink "$i/driver")" 2>/dev/null)" json_add_string usb "${u##*/}" json_add_string id "$(cat "$u/idVendor"):$(cat "$u/idProduct")" json_add_string product "$(cat "$u/manufacturer" 2>/dev/null) $(cat "$u/product" 2>/dev/null)" json_add_string speed "$(cat "$u/speed" 2>/dev/null)" } m_status() { load_config json_init json_add_string at_port "$cfg_at_port" json_add_string wdm "$cfg_wdm" json_add_string iface "$cfg_iface" json_add_object at_info port_info "$cfg_at_port" json_close_object json_add_object wdm_info port_info "$cfg_wdm" json_close_object # every modem node, for the port selects json_add_array ports for d in /dev/ttyUSB* /dev/ttyACM* /dev/cdc-wdm* /dev/wwan*; do [ -e "$d" ] || continue json_add_object "" port_info "$d" json_close_object done json_close_array json_add_array ifaces for n in $(uci -q show network | sed -n 's/^network\.\([^.=]*\)=interface$/\1/p'); do json_add_object "" json_add_string name "$n" json_add_string proto "$(uci -q get "network.$n.proto")" json_close_object done json_close_array st=$(ifstatus "$cfg_iface" 2>/dev/null) json_add_boolean iface_exists "$(b test -n "$st")" json_add_boolean iface_up "$(b test "$(jsonfilter -s "$st" -e '@.up' 2>/dev/null)" = true)" json_add_string iface_proto "$(uci -q get "network.$cfg_iface.proto")" json_add_string iface_ip "$(jsonfilter -s "$st" -e '@["ipv4-address"][0].address' 2>/dev/null)" json_add_string iface_uptime "$(jsonfilter -s "$st" -e '@.uptime' 2>/dev/null)" json_add_string iface_device "$(jsonfilter -s "$st" -e '@.l3_device' 2>/dev/null)" json_add_string apn "$(uci -q get "network.$cfg_iface.apn")" json_add_boolean mm_running "$(b pidof ModemManager)" json_add_object deps for c in lpac uqmi sms_tool jsonfilter; do json_add_boolean "$c" "$(b command -v "$c")" done json_close_object json_add_string version "$(cat "$KIT_DIR/VERSION" 2>/dev/null)" json_add_boolean auto_update "$(b grep -q 'esim auto-update' /etc/crontabs/root)" json_add_string auto_update_last "$(tail -n 1 "$KIT_DIR/auto-update.log" 2>/dev/null)" json_add_boolean busy "$(b job_running)" # "\t\t", newest first json_add_array history tail -n 15 "$HISTORY" 2>/dev/null | sed -n '1!G;h;$p' > /tmp/esim-tools-hist.$$ while IFS="$(printf '\t')" read -r t rc c; do json_add_object "" json_add_string time "$t" json_add_int rc "$rc" json_add_string cmd "$c" json_close_object done < /tmp/esim-tools-hist.$$ rm -f /tmp/esim-tools-hist.$$ json_close_array json_dump } m_modem() { json_init # the job owns the modem (resets it, lpac talks to it) if job_running; then json_add_boolean busy 1 json_add_string cmd "$(cat "$JOB/cmd")" json_dump return fi out=$(esim state 2>&1) || reply_error "$(echo "$out" | tail -n 1 | sed 's/^!! //')" echo "$out" > /tmp/esim-tools-state.$$ while read -r k v; do json_add_string "$k" "$v" done < /tmp/esim-tools-state.$$ rm -f /tmp/esim-tools-state.$$ json_dump } # apn.map (tab-separated: KEY APN USER PASS AUTH IPTYPE INIT_APN, or the old # "KEY APN") -> {"": {apn, user, pass, auth, ip, init}}; \037 between # fields so that read keeps the empty ones apns_json() { us=$(printf '\037') json_init awk -F'\t' -v us="$us" ' NF == 1 { split($0, a, " "); $0 = a[1] "\t" a[2] } $1 != "" { print $1 us $2 us $3 us $4 us $5 us $6 us $7 }' "$KIT_DIR/apn.map" 2>/dev/null > /tmp/esim-tools-apn.$$ while IFS="$us" read -r k apn user pass auth ip init; do json_add_object "$k" json_add_string apn "$apn" json_add_string user "$user" json_add_string pass "$pass" json_add_string auth "$auth" json_add_string ip "$ip" json_add_string init "$init" json_close_object done < /tmp/esim-tools-apn.$$ rm -f /tmp/esim-tools-apn.$$ json_dump } # lpac's last lines are JSON already: glued as is m_profiles() { l=$(cat "$CACHE/profiles.json" 2>/dev/null) c=$(cat "$CACHE/chip.json" 2>/dev/null) lt=$(date -r "$CACHE/profiles.json" +%s 2>/dev/null) ct=$(date -r "$CACHE/chip.json" +%s 2>/dev/null) printf '{"list":%s,"list_time":%s,"chip":%s,"chip_time":%s,"apns":%s,"now":%s}\n' \ "${l:-null}" "${lt:-0}" "${c:-null}" "${ct:-0}" "$(apns_json)" "$(date +%s)" } iccid_ok() { case "$1" in '' | *[!0-9A-Fa-f]*) return 1 ;; esac [ ${#1} -ge 18 ] && [ ${#1} -le 22 ] } m_run() { input=$(cat) cmd=$(jsonfilter -s "$input" -e '@.cmd' 2>/dev/null) a1=$(jsonfilter -s "$input" -e '@.a' 2>/dev/null) a2=$(jsonfilter -s "$input" -e '@.b' 2>/dev/null) a3=$(jsonfilter -s "$input" -e '@.c' 2>/dev/null) flag=$(jsonfilter -s "$input" -e '@.flag' 2>/dev/null) a4=$(jsonfilter -s "$input" -e '@.d' 2>/dev/null) a5=$(jsonfilter -s "$input" -e '@.e' 2>/dev/null) a6=$(jsonfilter -s "$input" -e '@.f' 2>/dev/null) a7=$(jsonfilter -s "$input" -e '@.g' 2>/dev/null) label= case "$cmd" in refresh) set -- esim refresh ;; enable | disable) iccid_ok "$a1" || reply_error "bad ICCID" set -- esim "$cmd" "$a1" ;; delete) iccid_ok "$a1" || reply_error "bad ICCID" set -- esim delete "$a1" --yes ;; rename) iccid_ok "$a1" || reply_error "bad ICCID" a2=$(printf '%s' "$a2" | tr -d '\r\n\t') [ -n "$a2" ] || reply_error "пустое имя" [ ${#a2} -le 64 ] || reply_error "имя длиннее 64 символов" set -- esim rename "$a1" "$a2" ;; add) # a = SM-DP+ address, b = matching id, c = confirmation code case "$a1" in '' | *[!A-Za-z0-9.:-]*) reply_error "bad SM-DP+ address" ;; esac case "$a2" in '' | *[!A-Za-z0-9_.-]*) reply_error "bad matching id" ;; esac case "$a3" in *[!A-Za-z0-9]*) reply_error "bad confirmation code" ;; esac en=--no-enable [ "$flag" = true ] || [ "$flag" = 1 ] && en=--enable set -- esim add "$a1" "$a2" [ -n "$a3" ] && set -- "$@" "$a3" set -- "$@" "$en" # the activation code stays out of the page and the history label="esim add $a1 ***${a3:+ ***} $en" ;; slot) case "$a1" in esim | sim) ;; *) reply_error "bad slot" ;; esac set -- esim slot "$a1" ;; apn) case "$a1" in '' | *[!A-Za-z0-9._-]*) reply_error "bad APN" ;; esac set -- esim apn "$a1" ;; profile-apn) # a = ICCID|sim1, b = APN (empty: remove), c = user, d = password, # e = auth ("pap chap"), f = ipv4|ipv6|ipv4v6, g = APN for the LTE attach [ "$a1" = sim1 ] || iccid_ok "$a1" || reply_error "bad ICCID" case "$a2$a7" in *[!A-Za-z0-9._-]*) reply_error "bad APN" ;; esac for v in "$a3" "$a4"; do [ "$(printf '%s' "$v" | tr -d '\t\r\n\037')" = "$v" ] || reply_error "управляющие символы в логине/пароле" done for v in $a5; do case "$v" in pap | chap | mschap | mschapv2 | eap | none) ;; *) reply_error "bad auth: $v" ;; esac done case "$a6" in '' | ipv4 | ipv6 | ipv4v6) ;; *) reply_error "bad IP type" ;; esac set -- esim profile-apn "$a1" "${a2:--}" label="esim profile-apn $a1 ${a2:--}" if [ -n "$a2" ]; then [ -n "$a3" ] && set -- "$@" "user=$a3" && label="$label user=$a3" # the password stays out of the page and the history [ -n "$a4" ] && set -- "$@" "pass=$a4" && label="$label pass=***" [ -n "$a5" ] && set -- "$@" "auth=$a5" && label="$label auth=$a5" [ -n "$a6" ] && set -- "$@" "ip=$a6" && label="$label ip=$a6" [ -n "$a7" ] && set -- "$@" "init=$a7" && label="$label init=$a7" fi ;; radio | reboot) set -- esim "$cmd" ;; sms-forward) set -- esim sms-forward -f ;; tg-test | tg-chats) set -- esim "$cmd" ;; update) set -- esim update ;; autoupdate-on) set -- esim auto-update on ;; autoupdate-off) set -- esim auto-update off ;; *) reply_error "unknown command: $cmd" ;; esac start_job "${label:-$*}" "$@" } # start_job