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.

206 lines
6.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
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. START=50
  3. USE_PROCD=1
  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 compression $1 compression "0"
  11. config_get udp $1 udp "1"
  12. config_get auth $1 auth "plain"
  13. config_get cisco_compat $1 cisco_compat "1"
  14. config_get ipaddr $1 ipaddr ""
  15. config_get netmask $1 netmask ""
  16. config_get ip6addr $1 ip6addr ""
  17. config_get proxy_arp $1 proxy_arp "0"
  18. config_get ping_leases $1 ping_leases "0"
  19. config_get default_domain $1 default_domain ""
  20. # Enable proxy arp, and make sure that ping leases is set to true in that case,
  21. # to prevent conflicts.
  22. if test "$proxy_arp" = 1;then
  23. local ip
  24. # IP address is empty. Auto-configure LAN + VPN.
  25. if test -z "$ipaddr";then
  26. local mask
  27. mask=$(uci get network.lan.netmask)
  28. if test "$mask" = "255.255.255.0";then
  29. uci set dhcp.lan.start=100
  30. uci set dhcp.lan.limit=91
  31. fi
  32. ip=$(uci get network.lan.ipaddr)
  33. ipaddr="$(echo $ip|cut -d . -f1,2,3).192"
  34. netmask="255.255.255.192"
  35. uci set ocserv.config.ipaddr="$ipaddr"
  36. uci set ocserv.config.netmask="$netmask"
  37. uci commit
  38. fi
  39. if test -z "$ip6addr";then
  40. ip6addr=$(uci get network.lan.ip6addr 2>/dev/null)
  41. test -n "$ip6addr" && uci set ocserv.config.ip6addr="$ip6addr"
  42. uci commit
  43. fi
  44. ping_leases=1
  45. test -n "$ipaddr" && sysctl -w "net.ipv4.conf.$(uci get network.lan.ifname).proxy_arp"=1 >/dev/null
  46. test -n "$ip6addr" && sysctl -w "net.ipv6.conf.$(uci get network.lan.ifname).proxy_ndp"=1 >/dev/null
  47. else
  48. test "$ipaddr" = "" && ipaddr="192.168.100.0"
  49. test "$netmask" = "" && ipaddr="255.255.255.0"
  50. fi
  51. enable_default_domain="#"
  52. enable_udp="#"
  53. enable_compression="#"
  54. test $predictable_ips = "0" && predictable_ips="false"
  55. test $predictable_ips = "1" && predictable_ips="true"
  56. test $cisco_compat = "0" && cisco_compat="false"
  57. test $cisco_compat = "1" && cisco_compat="true"
  58. test $ping_leases = "0" && ping_leases="false"
  59. test $ping_leases = "1" && ping_leases="true"
  60. test $udp = "1" && enable_udp=""
  61. test $compression = "1" && enable_compression=""
  62. test -z $default_domain && enable_default_domain=""
  63. test -z $ip6addr && enable_ipv6="#"
  64. test $auth = "plain" && authsuffix="\[passwd=/var/etc/ocpasswd\]"
  65. dyndns="false"
  66. hostname=`uci show ddns 2>/dev/null|grep domain|head -1|cut -d '=' -f 2`
  67. [ -n "$hostname" ] && dyndns="true"
  68. mkdir -p /var/etc
  69. sed -e "s/|PORT|/$port/g" \
  70. -e "s/|MAX_CLIENTS|/$max_clients/g" \
  71. -e "s/|MAX_SAME|/$max_same/g" \
  72. -e "s/|DPD|/$dpd/g" \
  73. -e "s#|AUTH|#$auth$authsuffix#g" \
  74. -e "s#|DYNDNS|#$dyndns#g" \
  75. -e "s/|PREDICTABLE_IPS|/$predictable_ips/g" \
  76. -e "s/|DEFAULT_DOMAIN|/$default_domain/g" \
  77. -e "s/|ENABLE_DEFAULT_DOMAIN|/$enable_default_domain/g" \
  78. -e "s/|CISCO_COMPAT|/$cisco_compat/g" \
  79. -e "s/|PING_LEASES|/$ping_leases/g" \
  80. -e "s/|UDP|/$enable_udp/g" \
  81. -e "s/|COMPRESSION|/$enable_compression/g" \
  82. -e "s/|IPV4ADDR|/$ipaddr/g" \
  83. -e "s/|NETMASK|/$netmask/g" \
  84. -e "s#|IPV6ADDR|#$ip6addr#g" \
  85. -e "s/|ENABLE_IPV6|/$enable_ipv6/g" \
  86. /etc/ocserv/ocserv.conf.template > /var/etc/ocserv.conf
  87. test -f /etc/ocserv/ocserv.conf.local && cat /etc/ocserv/ocserv.conf.local >> /var/etc/ocserv.conf
  88. }
  89. setup_users() {
  90. local name
  91. local group
  92. local password
  93. config_get name $1 name
  94. config_get group $1 group '*'
  95. config_get password $1 password
  96. [ -z "$name" -o -z "$password" ] && return
  97. echo "$name:$group:$password" >> /var/etc/ocpasswd
  98. }
  99. setup_routes() {
  100. local routes
  101. config_get ip $1 ip
  102. config_get netmask $1 netmask
  103. [ -z "$ip" -o -z "$netmask" ] && return
  104. echo "route = $ip/$netmask" >> /var/etc/ocserv.conf
  105. }
  106. setup_dns() {
  107. local routes
  108. config_get ip $1 ip
  109. [ -z "$ip" ] && return
  110. echo "dns = $ip" >> /var/etc/ocserv.conf
  111. }
  112. start_service() {
  113. local hostname iface
  114. hostname=`uci show ddns 2>/dev/null|grep domain|head -1|cut -d '=' -f 2`
  115. [ -z "$hostname" ] && hostname=`uci get system.@system[0].hostname 2>/dev/null`
  116. [ -f /etc/config/ocserv-dir/ca-key.pem ] && mv /etc/config/ocserv-dir/ca-key.pem /etc/ocserv/ca-key.pem
  117. [ -f /etc/config/ocserv-dir/ca.pem ] && mv /etc/config/ocserv-dir/ca.pem /etc/ocserv/ca.pem
  118. [ -f /etc/config/ocserv-dir/server-key.pem ] && mv /etc/config/ocserv-dir/server-key.pem /etc/ocserv/server-key.pem
  119. [ -f /etc/config/ocserv-dir/server-cert.pem ] && mv /etc/config/ocserv-dir/server-cert.pem /etc/ocserv/server-cert.pem
  120. [ -d /etc/config/ocserv-dir ] && rmdir /etc/config/ocserv-dir
  121. [ ! -f /etc/ocserv/ca-key.pem ] && [ -x /usr/bin/certtool ] && {
  122. logger -t ocserv "Generating CA certificate..."
  123. mkdir -p /etc/ocserv/pki/
  124. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/ca-key.pem >/dev/null 2>&1
  125. echo "cn=$hostname CA" >/etc/ocserv/pki/ca.tmpl
  126. echo "expiration_days=-1" >>/etc/ocserv/pki/ca.tmpl
  127. echo "serial=1" >>/etc/ocserv/pki/ca.tmpl
  128. echo "ca" >>/etc/ocserv/pki/ca.tmpl
  129. echo "cert_signing_key" >>/etc/ocserv/pki/ca.tmpl
  130. certtool --template /etc/ocserv/pki/ca.tmpl \
  131. --generate-self-signed --load-privkey /etc/ocserv/ca-key.pem \
  132. --outfile /etc/ocserv/ca.pem >/dev/null 2>&1
  133. }
  134. #generate server certificate/key
  135. [ ! -f /etc/ocserv/server-key.pem ] && [ -x /usr/bin/certtool ] && {
  136. logger -t ocserv "Generating server certificate..."
  137. mkdir -p /etc/ocserv/pki/
  138. certtool --bits 2048 --generate-privkey --outfile /etc/ocserv/server-key.pem >/dev/null 2>&1
  139. echo "cn=$hostname" >/etc/ocserv/pki/server.tmpl
  140. echo "serial=2" >>/etc/ocserv/pki/server.tmpl
  141. echo "expiration_days=-1" >>/etc/ocserv/pki/server.tmpl
  142. echo "signing_key" >>/etc/ocserv/pki/server.tmpl
  143. echo "encryption_key" >>/etc/ocserv/pki/server.tmpl
  144. certtool --template /etc/ocserv/pki/server.tmpl \
  145. --generate-certificate --load-privkey /etc/ocserv/server-key.pem \
  146. --load-ca-certificate /etc/ocserv/ca.pem --load-ca-privkey \
  147. /etc/ocserv/ca-key.pem --outfile /etc/ocserv/server-cert.pem >/dev/null 2>&1
  148. }
  149. [ -f /var/run/ocserv.pid ] || {
  150. touch /var/run/ocserv.pid
  151. chown ocserv:ocserv /var/run/ocserv.pid
  152. }
  153. [ -d /var/lib/ocserv ] || {
  154. mkdir -m 0755 -p /var/lib/ocserv
  155. chmod 0700 /var/lib/ocserv
  156. chown ocserv:ocserv /var/lib/ocserv
  157. }
  158. config_load "ocserv"
  159. rm -f /var/etc/ocserv.conf
  160. touch /var/etc/ocserv.conf
  161. setup_config config
  162. config_foreach setup_routes routes
  163. config_foreach setup_dns dns
  164. rm -f /var/etc/ocpasswd
  165. touch /var/etc/ocpasswd
  166. chmod 600 /var/etc/ocpasswd
  167. config_foreach setup_users ocservusers
  168. procd_open_instance
  169. procd_set_param command /usr/sbin/ocserv -f -c /var/etc/ocserv.conf
  170. procd_set_param respawn
  171. procd_close_instance
  172. }