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.

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