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.

196 lines
5.2 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. #!/bin/sh /etc/rc.common
  2. SERVICE_USE_PID=1
  3. START=50
  4. setup_firewall() {
  5. local port fw
  6. config_get port $1 port
  7. test -z "$port" && return
  8. config_get fwport $1 "fwport"
  9. test "$fwport" = "$port" && return
  10. #can we remove the old rule?
  11. uci add firewall rule
  12. uci set firewall.@rule[-1].src=wan
  13. uci set firewall.@rule[-1].target=ACCEPT
  14. uci set firewall.@rule[-1].proto=tcpudp
  15. uci set firewall.@rule[-1].dest_port=$port
  16. uci commit firewall
  17. /etc/init.d/firewall restart
  18. uci set ocserv.config.fwport="$port"
  19. uci commit ocserv
  20. }
  21. clear_firewall() {
  22. iptables-save | grep -v ocserv-rule | iptables-restore
  23. }
  24. setup_config() {
  25. config_get port $1 port "4443"
  26. config_get max_clients $1 max_clients "8"
  27. config_get max_same $1 max_same "2"
  28. config_get dpd $1 dpd "120"
  29. config_get predictable_ips $1 predictable_ips "1"
  30. config_get udp $1 udp "1"
  31. config_get auth $1 auth "plain"
  32. config_get cisco_compat $1 cisco_compat "1"
  33. config_get ipaddr $1 ipaddr "192.168.100.0"
  34. config_get netmask $1 netmask "255.255.255.0"
  35. config_get ip6addr $1 ip6addr ""
  36. test $predictable_ips = "0" && predictable_ips="false"
  37. test $predictable_ips = "1" && predictable_ips="true"
  38. test $cisco_compat = "0" && cisco_compat="false"
  39. test $cisco_compat = "1" && cisco_compat="true"
  40. test $udp = "0" && udp="#"
  41. test $udp = "1" && udp=""
  42. test -z $ip6addr && enable_ipv6="#"
  43. ipv6_addr=`echo $ip6addr|cut -d '/' -f 1`
  44. ipv6_prefix=`echo $ip6addr|cut -d '/' -f 2`
  45. test $auth = "plain" && authsuffix="\[/var/etc/ocpasswd\]"
  46. mkdir -p /var/etc
  47. sed -e "s/|PORT|/$port/g" \
  48. -e "s/|MAX_CLIENTS|/$max_clients/g" \
  49. -e "s/|MAX_SAME|/$max_same/g" \
  50. -e "s/|DPD|/$dpd/g" \
  51. -e "s#|AUTH|#$auth$authsuffix#g" \
  52. -e "s/|PREDICTABLE_IPS|/$predictable_ips/g" \
  53. -e "s/|CISCO_COMPAT|/$cisco_compat/g" \
  54. -e "s/|UDP|/$udp/g" \
  55. -e "s/|IPV4ADDR|/$ipaddr/g" \
  56. -e "s/|NETMASK|/$netmask/g" \
  57. -e "s/|IPV6ADDR|/$ipv6_addr/g" \
  58. -e "s/|IPV6PREFIX|/$ipv6_prefix/g" \
  59. -e "s/|ENABLE_IPV6|/$enable_ipv6/g" \
  60. /etc/ocserv/ocserv.conf.template > /var/etc/ocserv.conf
  61. }
  62. setup_users() {
  63. local name
  64. local group
  65. local password
  66. config_get name $1 name
  67. config_get group $1 group
  68. config_get password $1 password
  69. [ -z "$group" ] && group='*'
  70. [ -z "$name" -o -z "$password" ] && return
  71. echo "$name:$group:$password" >> /var/etc/ocpasswd
  72. }
  73. setup_routes() {
  74. local routes
  75. config_get ip $1 ip
  76. config_get netmask $1 netmask
  77. [ -z "$ip" -o -z "$netmask" ] && return
  78. echo "route = $ip/$netmask" >> /var/etc/ocserv.conf
  79. }
  80. setup_dns() {
  81. local routes
  82. config_get ip $1 ip
  83. [ -z "$ip" ] && return
  84. echo "dns = $ip" >> /var/etc/ocserv.conf
  85. }
  86. start() {
  87. local hostname iface
  88. user_exists ocserv 72 || user_add ocserv 72 72 /var/lib/ocserv
  89. group_exists ocserv 72 || group_add ocserv 72
  90. hostname=`uci get ddns.myddns.domain`
  91. [ -z "$hostname" ] && hostname=`uci get system.@system[0].hostname`
  92. [ ! -f /etc/ocserv/ca-key.pem ] && [ -x /usr/bin/certtool ] && {
  93. logger -t ocserv "Generating CA certificate..."
  94. mkdir -p /etc/ocserv/pki/
  95. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/ca-key.pem >/dev/null 2>&1
  96. echo "cn=$hostname CA" >/etc/ocserv/pki/ca.tmpl
  97. echo "expiration_days=-1" >>/etc/ocserv/pki/ca.tmpl
  98. echo "serial=1" >>/etc/ocserv/pki/ca.tmpl
  99. echo "ca" >>/etc/ocserv/pki/ca.tmpl
  100. echo "cert_signing_key" >>/etc/ocserv/pki/ca.tmpl
  101. certtool --template /etc/ocserv/pki/ca.tmpl \
  102. --generate-self-signed --load-privkey /etc/ocserv/ca-key.pem \
  103. --outfile /etc/ocserv/ca.pem >/dev/null 2>&1
  104. }
  105. #generate server certificate/key
  106. [ ! -f /etc/ocserv/server-key.pem ] && [ -x /usr/bin/certtool ] && {
  107. logger -t ocserv "Generating server certificate..."
  108. mkdir -p /etc/ocserv/pki/
  109. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/server-key.pem >/dev/null 2>&1
  110. echo "cn=$hostname" >/etc/ocserv/pki/server.tmpl
  111. echo "serial=2" >>/etc/ocserv/pki/server.tmpl
  112. echo "expiration_days=-1" >>/etc/ocserv/pki/server.tmpl
  113. echo "signing_key" >>/etc/ocserv/pki/server.tmpl
  114. echo "encryption_key" >>/etc/ocserv/pki/server.tmpl
  115. certtool --template /etc/ocserv/pki/server.tmpl \
  116. --generate-certificate --load-privkey /etc/ocserv/server-key.pem \
  117. --load-ca-certificate /etc/ocserv/ca.pem --load-ca-privkey \
  118. /etc/ocserv/ca-key.pem --outfile /etc/ocserv/server-cert.pem >/dev/null 2>&1
  119. }
  120. [ -f /var/run/ocserv.pid ] || {
  121. touch /var/run/ocserv.pid
  122. chown ocserv:ocserv /var/run/ocserv.pid
  123. }
  124. [ -d /var/lib/ocserv ] || {
  125. mkdir -m 0755 -p /var/lib/ocserv
  126. chmod 0700 /var/lib/ocserv
  127. chown ocserv:ocserv /var/lib/ocserv
  128. }
  129. config_load "ocserv"
  130. rm -f /var/etc/ocserv.conf
  131. touch /var/etc/ocserv.conf
  132. setup_config config
  133. config_foreach setup_routes routes
  134. config_foreach setup_dns dns
  135. rm -f /var/etc/ocpasswd
  136. touch /var/etc/ocpasswd
  137. chmod 600 /var/etc/ocpasswd
  138. config_foreach setup_users ocservusers
  139. setup_firewall config
  140. service_start /usr/sbin/ocserv -c /var/etc/ocserv.conf
  141. }
  142. stop() {
  143. service_stop /usr/sbin/ocserv
  144. clear_firewall
  145. }
  146. reload() {
  147. rm -f /var/etc/ocpasswd
  148. touch /var/etc/ocpasswd
  149. chmod 600 /var/etc/ocpasswd
  150. config_foreach setup_users ocservusers
  151. /usr/bin/occtl show status >/dev/null 2>&1
  152. if test $? != 0;then
  153. start
  154. else
  155. /usr/bin/occtl reload
  156. fi
  157. }