Advertisement
devinteske

rc.d/pptp

Jan 12th, 2016
576
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.80 KB | None | 0 0
  1. #!/bin/sh
  2. # FILE: /usr/local/etc/rc.d/pptp
  3. # EXAMPLE: sysrc pptp_enable=YES pptp_flags="hostname ppp-profile-name"
  4. # XNEEDED: "ppp-profile-name" must be configured in /etc/ppp/ppp.conf
  5.  
  6. # PROVIDE: pptp
  7. # REQUIRE: DAEMON LOGIN FILESYSTEMS
  8. # KEYWORD: shutdown
  9.  
  10. . /etc/rc.subr
  11.  
  12. name="pptp"
  13. rcvar="${name}_enable"
  14. command="/usr/local/sbin/${name}"
  15. pidfile="/var/run/${name}.pid"
  16. extra_commands="iface inet"
  17.  
  18. pptp_query()
  19. {
  20.     local qtype="$1"
  21.     ifconfig -l | awk '{
  22.         n = split($0, a)
  23.         for (i = 1; i <= n; i++)
  24.             if (a[i] ~ /^tun[[:digit:]]+/) print a[i]
  25.     }' | xargs -rn1 -Iif ifconfig if inet | awk -v qtype="$qtype" '
  26.     BEGIN { qtype = tolower(qtype) }
  27.     /^[^[:space:]]/ { iface = $1; next }
  28.     iface && $1 == "inet" && $3 == "-->" { inet[iface] = $2; next }
  29.     $1$2$3 == "OpenedbyPID" { pid[iface] = $4 }
  30.     END {
  31.         for (iface in pid) {
  32.             (cmd = "ps -o ucomm= -p " pid[iface]) | getline ucomm
  33.             close(cmd)
  34.             if (ucomm != "ppp") continue
  35.             if (qtype == "iface") {
  36.                 sub(/:.*/, "", iface)
  37.                 print iface
  38.                 found = 1
  39.             } else if (qtype == "inet" && inet[iface]) {
  40.                 print inet[iface]
  41.                 found = 1
  42.             } else if (qtype != "inet") {
  43.                 print pid[iface]
  44.                 found = 1
  45.             }
  46.         }
  47.         exit !found
  48.     }'
  49. }
  50.  
  51. pptp_start()
  52. {
  53.     local pid inet
  54.     if pid=$( pptp_query pid ); then
  55.         echo "$name already running as pid $pid."
  56.         return 1
  57.     fi
  58.  
  59.     debug "$command $pptp_flags &"
  60.     eval $command $pptp_flags \&
  61.     echo -n "Waiting for pptp to start"
  62.     while ! pid=$( pptp_query pid ); do sleep 1; echo -n .; done
  63.     echo
  64.     echo -n "Waiting for ppp session"
  65.     while pid=$( pptp_query pid ); do
  66.         inet=$( pptp_query inet ) && break
  67.         sleep 1
  68.         echo -n .
  69.     done
  70.     echo
  71.     if ! inet=$( pptp_query inet ); then
  72.         rm -f "$pidfile"
  73.         echo "pptp failed to start."
  74.         return 1
  75.     fi
  76.     echo "$pid" > "$pidfile"
  77. }
  78.  
  79. pptp_stop()
  80. {
  81.     local pid
  82.     if ! pid=$( pptp_query pid ); then
  83.         echo "$name is not running."
  84.         return 1
  85.     fi
  86.  
  87.     kill $pid
  88.     echo -n "Waiting for pid $pid to exit"
  89.     while pid=$( pptp_query pid ); do sleep 1; echo -n .; done
  90.     echo
  91.     rm -f "$pidfile"
  92.     echo "$name stopped."
  93. }
  94.  
  95. pptp_status()
  96. {
  97.     local pid
  98.     if ! pid=$( pptp_query pid ); then
  99.         echo "$name is not running."
  100.         return 1
  101.     fi
  102.     echo "$name is running as pid $pid."
  103. }
  104.  
  105. pptp_iface()
  106. {
  107.     local pid iface
  108.     if ! pid=$( pptp_query pid ); then
  109.         echo "$name is not running." >&2
  110.         return 1
  111.     fi
  112.     if ! iface=$( pptp_query iface ); then
  113.         echo "$name not associated with any interface." >&2
  114.         return 1
  115.     fi
  116.     echo "$iface"
  117. }
  118.  
  119. pptp_inet()
  120. {
  121.     local pid inet
  122.     if ! pid=$( pptp_query pid ) || ! inet=$( pptp_query inet ); then
  123.         echo "$name is not running." >&2
  124.         return 1
  125.     fi
  126.     echo "$inet"
  127. }
  128.  
  129. start_cmd=pptp_start
  130. stop_cmd=pptp_stop
  131. status_cmd=pptp_status
  132. iface_cmd=pptp_iface
  133. inet_cmd=pptp_inet
  134.  
  135. load_rc_config $name
  136. run_rc_command "$1"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement