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.

168 lines
4.6 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. user_exists ocserv 72 || user_add ocserv 72 72 /var/lib/ocserv
  69. group_exists ocserv 72 || group_add ocserv 72
  70. hostname=`uci get ddns.myddns.domain`
  71. [ -z "$hostname" ] && hostname=`uci get system.@system[0].hostname`
  72. [ ! -f /etc/ocserv/ca-key.pem ] && [ -x /usr/bin/certtool ] && {
  73. logger -t ocserv "Generating CA certificate..."
  74. mkdir -p /etc/ocserv/pki/
  75. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/ca-key.pem >/dev/null 2>&1
  76. echo "cn=$hostname CA" >/etc/ocserv/pki/ca.tmpl
  77. echo "expiration_days=-1" >>/etc/ocserv/pki/ca.tmpl
  78. echo "serial=1" >>/etc/ocserv/pki/ca.tmpl
  79. echo "ca" >>/etc/ocserv/pki/ca.tmpl
  80. echo "cert_signing_key" >>/etc/ocserv/pki/ca.tmpl
  81. certtool --template /etc/ocserv/pki/ca.tmpl \
  82. --generate-self-signed --load-privkey /etc/ocserv/ca-key.pem \
  83. --outfile /etc/ocserv/ca.pem >/dev/null 2>&1
  84. }
  85. #generate server certificate/key
  86. [ ! -f /etc/ocserv/server-key.pem ] && [ -x /usr/bin/certtool ] && {
  87. logger -t ocserv "Generating server certificate..."
  88. mkdir -p /etc/ocserv/pki/
  89. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/server-key.pem >/dev/null 2>&1
  90. echo "cn=$hostname" >/etc/ocserv/pki/server.tmpl
  91. echo "serial=2" >>/etc/ocserv/pki/server.tmpl
  92. echo "expiration_days=-1" >>/etc/ocserv/pki/server.tmpl
  93. echo "signing_key" >>/etc/ocserv/pki/server.tmpl
  94. echo "encryption_key" >>/etc/ocserv/pki/server.tmpl
  95. certtool --template /etc/ocserv/pki/server.tmpl \
  96. --generate-certificate --load-privkey /etc/ocserv/server-key.pem \
  97. --load-ca-certificate /etc/ocserv/ca.pem --load-ca-privkey \
  98. /etc/ocserv/ca-key.pem --outfile /etc/ocserv/server-cert.pem >/dev/null 2>&1
  99. }
  100. [ -f /var/run/ocserv.pid ] || {
  101. touch /var/run/ocserv.pid
  102. chown ocserv:ocserv /var/run/ocserv.pid
  103. }
  104. [ -d /var/lib/ocserv ] || {
  105. mkdir -m 0755 -p /var/lib/ocserv
  106. chmod 0700 /var/lib/ocserv
  107. chown ocserv:ocserv /var/lib/ocserv
  108. }
  109. config_load "ocserv"
  110. rm -f /var/etc/ocserv.conf
  111. touch /var/etc/ocserv.conf
  112. setup_config config
  113. config_foreach setup_routes routes
  114. config_foreach setup_dns dns
  115. rm -f /var/etc/ocpasswd
  116. touch /var/etc/ocpasswd
  117. chmod 600 /var/etc/ocpasswd
  118. config_foreach setup_users ocservusers
  119. service_start /usr/sbin/ocserv -c /var/etc/ocserv.conf
  120. }
  121. stop() {
  122. service_stop /usr/sbin/ocserv
  123. }
  124. reload() {
  125. rm -f /var/etc/ocpasswd
  126. touch /var/etc/ocpasswd
  127. chmod 600 /var/etc/ocpasswd
  128. config_foreach setup_users ocservusers
  129. /usr/bin/occtl show status >/dev/null 2>&1
  130. if test $? != 0;then
  131. start
  132. else
  133. /usr/bin/occtl reload
  134. fi
  135. }