1
0

ocserv.init 6.9 KB

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