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.

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