pppossh.sh 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/bin/sh
  2. SSH=/usr/bin/ssh
  3. [ -x "$SSH" ] || {
  4. echo "Cannot find executable $SSH." >&2
  5. exit 1
  6. }
  7. . /lib/functions.sh
  8. . ../netifd-proto.sh
  9. init_proto "$@"
  10. INCLUDE_ONLY=1
  11. . ./ppp.sh
  12. proto_pppossh_init_config() {
  13. ppp_generic_init_config
  14. config_add_string server sshuser ipaddr peeraddr ssh_options
  15. config_add_array 'identity:list(string)'
  16. config_add_int port use_hostdep
  17. available=1
  18. no_device=1
  19. }
  20. proto_pppossh_setup() {
  21. local config="$1"
  22. local iface="$2"
  23. local user="$(id -nu)"
  24. local home=$(sh -c "echo ~$user")
  25. local server port sshuser ipaddr peeraddr ssh_options identity use_hostdep
  26. local ip fn errmsg opts pty
  27. json_get_vars port sshuser ipaddr peeraddr ssh_options use_hostdep
  28. json_get_var server server && {
  29. [ -z "$use_hostdep" ] && use_hostdep=1
  30. for ip in $(resolveip -t 5 "$server"); do
  31. if [ "$use_hostdep" -gt 0 ]; then
  32. ( proto_add_host_dependency "$config" "$ip" )
  33. else
  34. break
  35. fi
  36. done
  37. }
  38. [ -n "$ip" ] || errmsg="${errmsg}Could not resolve $server\n"
  39. [ -n "$sshuser" ] || errmsg="${errmsg}Missing sshuser option\n"
  40. json_get_values identity identity
  41. [ -z "$identity" ] && identity="$home/.ssh/id_rsa $home/.ssh/id_dsa"
  42. for fn in $identity; do
  43. [ -f "$fn" ] && opts="$opts -i $fn"
  44. done
  45. [ -n "$opts" ] || errmsg="${errmsg}Cannot find valid identity file\n"
  46. [ -n "$errmsg" ] && {
  47. echo -ne "$errmsg" >&2
  48. proto_setup_failed "$config"
  49. exit 1
  50. }
  51. opts="$opts ${port:+-p $port}"
  52. opts="$opts ${ssh_options}"
  53. opts="$opts $sshuser@$server"
  54. pty="exec env 'HOME=$home' $SSH $opts pppd nodetach notty noauth"
  55. ppp_generic_setup "$config" noauth pty "$pty" "$ipaddr:$peeraddr"
  56. }
  57. proto_pppossh_teardown() {
  58. ppp_generic_teardown "$@"
  59. }
  60. add_protocol pppossh