PageRenderTime 27ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/src/web/html/function_vpn_ssl.php

http://windowsfw.googlecode.com/
PHP | 342 lines | 181 code | 40 blank | 121 comment | 18 complexity | 9aa276cb6f0f811d37f6a2e14af257e4 MD5 | raw file
  1. <?php
  2. include_once "function_base_xml_class.php";
  3. class CVPN_SSL_Server extends base_xml_class
  4. {
  5. function __construct()
  6. {
  7. $this->root_path = "/MINI/NETWORK/VPN";
  8. $this->node_name = "SSL";
  9. $this->key_array = array("Enabled", "Srv_Type", "Dev_Type", "Protocol", "Port", "Lan_Ip", "Lan_Mask","Route_Ip", "Route_Mask",
  10. "Wan_Ip", "Dns1", "Dns2", "Wins");
  11. }
  12. private function CreateServerConf($ret)
  13. {
  14. $crt = new CCertificate();
  15. $r = $crt -> get_item_by_num(0);
  16. $server_name = $r['User'];
  17. $server_conf = "/usr/local/etc/openvpn/easy-rsa/server.conf";
  18. $str = "port ". $ret['Port']. " \n";
  19. $str .= "proto ". $ret['Protocol']. " \n";
  20. $str .= "dev ". $ret['Dev_Type']. " \n";
  21. $str .= "ca /usr/local/etc/openvpn/easy-rsa/keys/ca.crt \n";
  22. $str .= "cert /usr/local/etc/openvpn/easy-rsa/keys/". $server_name. ".crt \n";
  23. $str .= "key /usr/local/etc/openvpn/easy-rsa/keys/". $servre_name. ".key \n";
  24. $str .= "dh /usr/local/etc/openvpn/easy-rsa/keys/dh1024.pem \n";
  25. $str .= "server ". $ret['Lan_Ip']. " ". $ret['Lan_Mask']. " \n";
  26. $str .= "ifconfig-pool-persist /usr/local/etc/openvpn/easy-rsa/ipp.txt \n";
  27. $str .= "push \"". $ret['Route_Ip']. ' '. $ret['Route_Mask']. "\" \n";
  28. $str .= "push \"dhcp-option DNS ". $ret['Dns1']. "\" \n";
  29. if ($ret['Dns2'] != "")
  30. $str .= "push \"dhcp-option DNS ". $ret['Dns2']. "\" \n";
  31. if ($ret['Wins'] != "")
  32. $str .= "push \"dhcp-option WINS ". $ret['Wins']. "\" \n";
  33. $str .= "keepalive 10 120 \n";
  34. $str .= "crl-verify /usr/local/etc/openvpn/easy-rsa/keys/crl.pem \n";
  35. $str .= "comp-lzo \n";
  36. $str .= "persist-key \n";
  37. $str .= "persist-tun \n";
  38. $str .= "verb 3 \n";
  39. $handle = fopen($server_conf, "w+");
  40. if ($handle == FALSE)
  41. return FALSE;
  42. $ret = fwrite($handle, $str);
  43. if ($ret === FALSE)
  44. return FALSE;
  45. fclose($handle);
  46. return $str;
  47. }
  48. function GetSysRunStatus()
  49. {
  50. $command = "/bin/pidof openvpn ;";
  51. $ret = shell_exec($command);
  52. if (trim($ret) != "")
  53. return "??";
  54. else
  55. return "??";
  56. }
  57. function Apply_Iptables()
  58. {
  59. $chain = "APPLY";
  60. $ret_list = $this -> get_list();
  61. $ret = $ret_list[0];
  62. $command_iptables_D = "/usr/local/bin/iptables -F ". $chain. " ;";
  63. $command_iptables_A = "/usr/local/bin/iptables -A ". $chain. " -p tcp --dport ". $ret['Port']. " -j ACCEPT;";
  64. $command_iptables_A .= "/usr/local/bin/iptables -A ". $chain. " -p udp --dport ". $ret['Port']. " -j ACCEPT;";
  65. shell_exec($command_iptables_D);
  66. if ($ret['Enabled'] == '1'){
  67. shell_exec($command_iptables_A);
  68. }
  69. }
  70. function Apply()
  71. {
  72. $ret_list = $this -> get_list();
  73. $ret = $ret_list[0];
  74. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  75. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  76. //$command .= "/usr/local/etc/openvpn/easy-rsa/clean-all; ";
  77. //$command .= "/usr/local/etc/openvpn/easy-rsa//build-dh; ";
  78. //$command .= "/usr/local/etc/openvpn/easy-rsa/pkitool --initca; ";
  79. //$command .= "/usr/local/etc/openvpn/easy-rsa/pkitool --server vpn_server; ";
  80. $command_kill = "/bin/kill -9 `pidof openvpn` 1>/dev/null 2>&1";
  81. $command_start = "/usr/local/sbin/openvpn --daemon --config /usr/local/etc/openvpn/easy-rsa/server.conf 1>/dev/null 2>&1; ";
  82. shell_exec($command_kill);
  83. if ($ret['Enabled'] == '1'){
  84. $this -> CreateServerConf($ret);
  85. shell_exec($command);
  86. shell_exec($command_start);
  87. }
  88. $this -> Apply_Iptables();
  89. }
  90. }
  91. class CVPN_SSL_Client extends base_xml_class
  92. {
  93. function __construct()
  94. {
  95. $this->root_path = "/MINI/NETWORK/VPN";
  96. $this->node_name = "SSL";
  97. $this->key_array = array( "Dev_Type", "Protocol", "Port", "Wan_Ip");
  98. }
  99. function GetClientList()
  100. {
  101. $ret_list = array();
  102. $command = "/bin/ls /ldisk/vpn/ssl/user/ | /bin/grep tar.gz | /bin/awk -F. '{print $1}'";
  103. $ret_command = shell_exec($command);
  104. $list = explode("\n", $ret_command);
  105. $i = 0;
  106. foreach ($list as $l){
  107. if ($l == "")
  108. continue;
  109. $ret_list[$i]['Name'] = $l;
  110. $tem_list = $this -> GetClientCrtTime($l);
  111. $ret_list[$i]['Before'] = $tem_list['Before'];
  112. $ret_list[$i++]['After'] = $tem_list['After'];
  113. }
  114. return $ret_list;
  115. }
  116. function GetClientCrtTime($name)
  117. {
  118. $ret_list = array();
  119. $command_b = "/bin/cat /ldisk/vpn/ssl/keys/". $name.
  120. ".crt | /bin/grep \"Not Before\" | /bin/awk -FBefore: '{printf $2}' ";
  121. //print $command_b;
  122. $before = shell_exec($command_b);
  123. $command_a = "/bin/cat /ldisk/vpn/ssl/keys/". $name.
  124. ".crt | /bin/grep \"Not After\" | /bin/awk -F\"After :\" '{printf $2}' ";
  125. //print $command_a;
  126. $after= shell_exec($command_a);
  127. $ret_list ['Before'] = $before;
  128. $ret_list ['After'] = $after;
  129. return $ret_list;
  130. }
  131. private function CreateClientConf($client_name)
  132. {
  133. $client_conf = "/usr/local/etc/openvpn/easy-rsa/client.ovpn";
  134. $ret_list = $this -> get_list();
  135. $ret = $ret_list[0];
  136. $str = "client \n";
  137. $str .= "dev ". $ret['Dev_Type']. " \n";
  138. $str .= "proto ". $ret['Protocol']. " \n";
  139. $str .= "remote ". $ret['Wan_Ip']. " ". $ret['Port']. " \n";
  140. $str .= "resolv-retry infinite \n";
  141. $str .= "nobind \n";
  142. $str .= "persist-key \n";
  143. $str .= "persist-tun \n";
  144. $str .= "ca keys/ca.crt \n";
  145. $str .= "cert keys/". $client_name. ".crt \n";
  146. $str .= "key keys/". $client_name. ".key \n";
  147. $str .= "comp-lzo \n";
  148. $str .= "verb 3 \n";
  149. $handle = fopen($client_conf, "w+");
  150. if ($handle == FALSE)
  151. return FALSE;
  152. $ret = fwrite($handle, $str);
  153. if ($ret === FALSE)
  154. return FALSE;
  155. fclose($handle);
  156. return $str;
  157. }
  158. function CreateCrtUser($client_name)
  159. {
  160. $user_path = "/ldisk/vpn/ssl/user/";
  161. $str_client = $this -> CreateClientConf($client_name);
  162. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  163. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  164. $command .= "/usr/local/etc/openvpn/easy-rsa/pkitool ". $client_name. " ; ";
  165. //print $command;
  166. $ret = shell_exec ($command);
  167. $command_tar = "cd /usr/local/etc/openvpn/easy-rsa ;";
  168. $command_tar .= "/bin/tar -czvf ". $user_path. $client_name. ".tar.gz ";
  169. $command_tar .= "keys/ca.crt ";
  170. $command_tar .= "keys/". $client_name. ".crt ";
  171. $command_tar .= "keys/". $client_name. ".key ";
  172. $command_tar .= "client.ovpn ";
  173. //print $command_tar;
  174. $ret = shell_exec ($command_tar);
  175. return $ret;
  176. }
  177. function DelCrtUser($name)
  178. {
  179. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  180. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  181. $command .= "/usr/local/etc/openvpn/easy-rsa/revoke-full ". $name. " ;";
  182. $ret = shell_exec($command);
  183. $command_del = "/bin/rm -rf /ldisk/vpn/ssl/user/". $name. ".tar.gz ;";
  184. $ret = shell_exec($command_del);
  185. return $ret;
  186. }
  187. function Apply()
  188. {
  189. $command_del = "/bin/rm -rf /ldisk/vpn/ssl/keys/*; ";
  190. $command_cp = "cp -rf /usr/local/etc/openvpn/easy-rsa/keys/* /ldisk/vpn/ssl/keys/ ;";
  191. $command_cp .= "/bin/sync ; ";
  192. //print $command_del;
  193. //print $command_cp;
  194. $ret = shell_exec ($command_del);
  195. $ret = shell_exec ($command_cp);
  196. }
  197. }
  198. class CCertificate extends base_xml_class
  199. {
  200. function __construct()
  201. {
  202. $this->root_path = "/MINI/NETWORK/VPN";
  203. $this->node_name = "CERTIFICATE";
  204. $this->key_array = array( "Time", "City", "State", "Org", "User", "Email");
  205. }
  206. function CreateDH()
  207. {
  208. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  209. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  210. //$command .= "/usr/local/etc/openvpn/easy-rsa/clean-all; ";
  211. $command .= "/usr/local/etc/openvpn/easy-rsa/build-dh; ";
  212. //print $command;
  213. shell_exec($command);
  214. }
  215. function CreateCA()
  216. {
  217. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  218. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  219. $command .= "/usr/local/etc/openvpn/easy-rsa/pkitool --initca; ";
  220. //print $command;
  221. shell_exec($command);
  222. }
  223. function CreateSERVER()
  224. {
  225. $r = $this -> get_item_by_num(0);
  226. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  227. $command .= "source /usr/local/etc/openvpn/easy-rsa/vars; ";
  228. $command .= "/usr/local/etc/openvpn/easy-rsa/pkitool --server ". $r['User']. " ; ";
  229. //print $command;
  230. shell_exec($command);
  231. }
  232. private function CreateCommandStr($title, $v)
  233. {
  234. $command = "";
  235. $command .= "cat /usr/local/etc/openvpn/easy-rsa/vars | sed '/export ". $title. "/d' > /usr/local/etc/openvpn/easy-rsa/vars ;";
  236. $command .= "echo \"export ". $title. "=" .$v. "\" >> /usr/local/etc/openvpn/easy-rsa/vars ;";
  237. return $command;
  238. }
  239. function CreateVars() //BOOT
  240. {
  241. $r = $this -> get_item_by_num(0);
  242. $command = "cd /usr/local/etc/openvpn/easy-rsa; ";
  243. if (trim($r['Time']) != ""){
  244. $command .= $this -> CreateCommandStr("CA_EXPIRE", $r['Time']);
  245. $command .= $this -> CreateCommandStr("KEY_EXPIRE", $r['Time']);
  246. }
  247. if (trim($r['State']) != "")
  248. $command .= $this -> CreateCommandStr("KEY_PROVINCE", $r['State']);
  249. if (trim($r['City']) != "")
  250. $command .= $this -> CreateCommandStr("KEY_CITY", $r['City']);
  251. if (trim($r['Org']) != "")
  252. $command .= $this -> CreateCommandStr("KEY_ORG", $r['Org']);
  253. if (trim($r['Email']) != "")
  254. $command .= $this -> CreateCommandStr("KEY_EMAIL", $r['Email']);
  255. $command .= "/bin/chmod +x /usr/local/etc/openvpn/easy-rsa/vars; /bin/sync;";
  256. //print $command;
  257. shell_exec($command);
  258. }
  259. function Apply()
  260. {
  261. $this -> CreateVars();
  262. $command_del = "/bin/rm -rf /ldisk/vpn/ssl/keys/*; ";
  263. $command_cp = "cp -rf /usr/local/etc/openvpn/easy-rsa/keys/* /ldisk/vpn/ssl/keys/ ;";
  264. $command_cp .= "/bin/sync ; ";
  265. //print $command_del;
  266. //print $command_cp;
  267. $ret = shell_exec ($command_del);
  268. $ret = shell_exec ($command_cp);
  269. }
  270. }
  271. class CVPN_SSL
  272. {
  273. function Apply()
  274. {
  275. $crt = new CCertificate();
  276. $crt -> CreateVars();
  277. $command_del = "/bin/rm -rf /usr/local/etc/openvpn/easy-rsa/keys/*; ";
  278. $ret = shell_exec ($command_del);
  279. $command_cp = "cp -rf /ldisk/vpn/ssl/keys/ /usr/local/etc/openvpn/easy-rsa/keys/ ;";
  280. $command_cp .= "/bin/sync ; ";
  281. $ret = shell_exec ($command_cp);
  282. $server = new CVPN_SSL_Server();
  283. $server -> Apply();
  284. }
  285. }
  286. ?>