forked from poppyred/1.6.x
/
exec.pptpd.php
executable file
·595 lines (477 loc) · 22.3 KB
/
exec.pptpd.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
<?php
include_once(dirname(__FILE__).'/ressources/class.templates.inc');
include_once(dirname(__FILE__).'/ressources/class.mysql.inc');
include_once(dirname(__FILE__).'/framework/frame.class.inc');
include_once(dirname(__FILE__).'/framework/class.unix.inc');
include_once(dirname(__FILE__).'/ressources/class.users.menus.inc');
if(posix_getuid()<>0){die("Cannot be used in web server mode\n\n");}
if(preg_match("#--verbose#",implode(" ",$argv))){$GLOBALS["VERBOSE"]=true;$GLOBALS["debug"]=true;ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);}
if(preg_match("#--simule#",implode(" ",$argv))){$GLOBALS["SIMULE"]=true;$GLOBALS["SIMULE"]=true;}
if(preg_match("#--force#",implode(" ",$argv))){$GLOBALS["FORCE"]=true;$GLOBALS["FORCE"]=true;}
events(implode(" ",$argv),"MAIN",__FILE__,__LINE__);
if($argv[1]=="--build"){build();die();}
if($argv[1]=="--chap"){chapsecrets();die();}
if($argv[1]=="--clients"){clients_connexions();die();}
if($argv[1]=="--client"){clients_connexions();die();}
if($argv[1]=="--ifup"){client_net_config($argv[2],$argv[3],$argv[4],$argv[5]);die();}
if($argv[1]=="--ifdown"){client_net_config_down($argv[2],$argv[3],$argv[4],$argv[5]);die();}
if($argv[1]=="--clients-start"){clients_start();}
if($argv[1]=="--clients-stop"){clients_stop();}
if($argv[1]=="--hook"){pptp_hook();}
function build(){
$sock=new sockets();
$users=new usersMenus();
$unix=new unix();
$PPTPDConfig=unserialize(base64_decode($sock->GET_INFO("PPTPDConfig")));
$EnablePPTPDVPN=$sock->GET_INFO("EnablePPTPDVPN");
if($EnablePPTPDVPN==null){$EnablePPTPDVPN=0;}
$sysctl=$unix->find_program("sysctl");
shell_exec("$sysctl -w net.ipv4.ip_forward=1 >/dev/null 2>&1");
if(!is_file("/var/log/ppp-ipupdown.log")){shell_exec("/bin/touch /var/log/ppp-ipupdown.log");}
if(trim($PPTPDConfig["SERVER_IP"])==null){$PPTPDConfig["SERVER_IP"]="192.168.25.1";}
if(trim($PPTPDConfig["NETMASK"])==null){$PPTPDConfig["NETMASK"]="255.255.255.0";}
if(trim($PPTPDConfig["SERVER_NAME"])==null){$PPTPDConfig["SERVER_NAME"]=$users->hostname;}
if($PPTPDConfig["SERVER_IP_FROM"]==null){
preg_match("#^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$#",$PPTPDConfig["SERVER_IP"],$rI);
$end=$rI[4]+1;
if($end>255){$end=254;}
$newip="{$rI[1]}.{$rI[2]}.{$rI[3]}.".$end;
$PPTPDConfig["SERVER_IP_FROM"]=$newip;
}
if($PPTPDConfig["SERVER_IP_TO"]==null){
preg_match("#^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$#",$PPTPDConfig["SERVER_IP"],$re);
$end=$re[4]+50;
if($end>255){$end=254;}
$PPTPDConfig["SERVER_IP_TO"]="{$re[1]}.{$re[2]}.{$re[3]}.".$end;
}
@mkdir("/etc/ppp",0666,true);
$conf[]="option /etc/ppp/pptpd-options";
$conf[]="logwtmp";
if($PPTPDConfig["bcrelay"]<>null){$conf[]="bcrelay {$PPTPDConfig["bcrelay"]}";}
preg_match("#^([0-9]+)\.([0-9]+)\.([0-9]+)\.([0-9]+)$#",$PPTPDConfig["SERVER_IP_TO"],$re);
$conf[]="remoteip {$PPTPDConfig["SERVER_IP_FROM"]}-{$re[4]}";
$conf[]="localip {$PPTPDConfig["SERVER_IP"]}";
$conf[]="";
echo "Starting......: ".date("H:i:s")." PPTP VPN connexion name \"{$PPTPDConfig["SERVER_NAME"]}\"\n";
echo "Starting......: ".date("H:i:s")." PPTP VPN {$PPTPDConfig["SERVER_IP"]} <- {$PPTPDConfig["SERVER_IP_FROM"]}-{$re[4]}/{$PPTPDConfig["NETMASK"]}\n";
@file_put_contents("/etc/pptpd.conf",@implode($conf,"\n"));
unset($conf);
$conf[]="################################################################################";
$conf[]="# \$Id: pptpd-options 4643 2006.-11-06 18:42:43Z rene \$";
$conf[]="#";
$conf[]="# Sample Poptop PPP options file /etc/ppp/pptpd-options";
$conf[]="# Options used by PPP when a connection arrives from a client.";
$conf[]="# This file is pointed to by /etc/pptpd.conf option keyword.";
$conf[]="# Changes are effective on the next connection. See \"man pppd\".";
$conf[]="#";
$conf[]="# You are expected to change this file to suit your system. As";
$conf[]="# packaged, it requires PPP 2.4.2 and the kernel MPPE module.";
$conf[]="# updated by Artica on ". date("Y-m-d H:i:s");
$conf[]="###############################################################################";
$conf[]="";
$conf[]="";
$conf[]="# Authentication";
$conf[]="";
$conf[]="# Name of the local system for authentication purposes";
$conf[]="# (must match the second field in /etc/ppp/chap-secrets entries)";
$conf[]="name {$PPTPDConfig["SERVER_NAME"]}";
$conf[]="# domain mydomain.net";
$conf[]="#chapms-strip-domain";
if($PPTPDConfig["DNS_1"]<>null){$conf[]="ms-dns {$PPTPDConfig["DNS_1"]}";}
if($PPTPDConfig["DNS_2"]<>null){$conf[]="ms-dns {$PPTPDConfig["DNS_2"]}";}
$conf[]="#ms-wins 10.0.0.3";
$conf[]="#ms-wins 10.0.0.4";
if($PPTPDConfig["CRYPT"]==1){
$conf[]="refuse-pap";
$conf[]="refuse-chap";
$conf[]="refuse-mschap";
$conf[]="require-mschap-v2";
$conf[]="require-mppe-128";
$conf[]="lcp-echo-failure 30";
$conf[]="lcp-echo-interval 5";
$conf[]="ipcp-accept-local";
$conf[]="ipcp-accept-remote";
$conf[]="asyncmap 0";
$conf[]="nobsdcomp";
}else{
$conf[]="refuse-chap";
$conf[]="refuse-pap";
$conf[]="refuse-eap";
$conf[]="refuse-mschap";
$conf[]="nopcomp";
}
$conf[]="proxyarp";
$conf[]="nodefaultroute";
$conf[]="debug";
$conf[]="lock";
$conf[]="netmask {$PPTPDConfig["NETMASK"]}";
$conf[]="noipx";
//http://www.maclive.net/sid/132
// 1. TCP Port 1723 should be passed through to your server
// 2. IP Protocol ID of 47 (0x2F) (not port 47) needs to be passed. This is not TCP or UDP it's a packet type used to support GRE (Generic Routing Encapsulation)
events("WRITING:: /etc/ppp/pptpd-options",__FUNCTION__,__FILE__,__LINE__);
@file_put_contents("/etc/ppp/pptpd-options",@implode($conf,"\n"));
unset($conf);
chapsecrets();
pptp_hook();
}
function chapsecrets(){
$file="/etc/ppp/chap-secrets";
$sock=new sockets();
$PPTPDConfig=unserialize(base64_decode($sock->GET_INFO("PPTPDConfig")));
if(trim($PPTPDConfig["SERVER_NAME"])==null){
$users=new usersMenus();
$PPTPDConfig["SERVER_NAME"]=$users->hostname;
}
$array=unserialize(base64_decode($sock->GET_INFO("PPTPDMembers")));
if(!is_array($array)){return;}
if(count($array)==0){return;}
if(is_array($array)){
while (list ($uid, $conf) = each ($array) ){
$ct=new user($uid);
if($conf["ASSIGN_IP"]==null){$conf["ASSIGN_IP"]="*";}
if(!preg_match("#[0-9\.\:]+#",$conf["ASSIGN_IP"])){$conf["ASSIGN_IP"]="*";}
$f[]="$uid\t{$PPTPDConfig["SERVER_NAME"]}\t$ct->password\t\"{$conf["ASSIGN_IP"]}\"";
}
}
events("WRITING:: PPTP VPN ". count($f). " user(s)",__FUNCTION__,__FILE__,__LINE__);
echo "Starting......: ".date("H:i:s")." PPTP VPN ". count($f). " user(s)\n";
$f[]="";
@file_put_contents($file,@implode($f,"\n"));
}
function clients_connexions(){
if($GLOBALS["VERBOSE"]){echo "DEBUG:clients_connexions(): START\n";}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
if(!is_array($array)){
if($GLOBALS["VERBOSE"]){echo "DEBUG:clients_connexions(): PPTPVpnClients -> Not an array\n";}
}
if(count($array)==0){
if($GLOBALS["VERBOSE"]){echo "DEBUG:clients_connexions(): PPTPVpnClients = 0 Not an array\n";}
return;}
@mkdir("/etc/ppp/peers",666,true);
if($GLOBALS["VERBOSE"]){echo "DEBUG:clients_connexions(): -> chapsecrets()\n";}
chapsecrets();
$chap=explode("\n",@file_get_contents("/etc/ppp/chap-secrets"));
while (list ($connexionname, $PPTPDConfig) = each ($array) ){
if($PPTPDConfig["ENABLED"]<>1){continue;}
echo "Starting......: ".date("H:i:s")." PPTP VPN Client connection $connexionname\n";
$chap[]="{$PPTPDConfig["username"]}\t\"{$PPTPDConfig["username"]}\t{$PPTPDConfig["password"]}\t\*";
$peers[]="# PPTP Tunnel configuration for tunnel $connexionname";
$peers[]="# Server IP: {$PPTPDConfig["vpn_servername"]}";
$peers[]="name {$PPTPDConfig["username"]}";
$peers[]="file /etc/ppp/options.pptp";
$peers[]="require-mppe-128";
$peers[]="ipparam $connexionname";
$peers[]="remotename $connexionname";
$peers[]="persist";
$peers[]="";
@file_put_contents("/etc/ppp/peers/$connexionname",@implode("\n",$peers));
unset($peers);
$cons["$connexionname"]=$PPTPDConfig["vpn_servername"];
}
$chap[]="";
@file_put_contents("/etc/ppp/chap-secrets",@implode("\n",$chap));
pptp_hook();
//sudo route add -net 10.8.0.0 netmask 255.255.0.0 dev ppp0
//pptp tougeron.eu call florent
}
function pptp_hook(){
if($GLOBALS["VERBOSE"]){echo "DEBUG:pptp_hook(): START\n";}
$f[]="#!/bin/sh";
$f[]=LOCATE_PHP5_BIN2()." " .__FILE__." --ifup \$4 \$5 \${IFNAME} \${PPP_IPPARAM}";
$f[]="";
file_put_contents("/etc/ppp/ip-up.d/detectnet",@implode("\n",$f));
chmod("/etc/ppp/ip-up.d/detectnet",0755);
if($GLOBALS["VERBOSE"]){echo "DEBUG:pptp_hook(): /etc/ppp/ip-up.d/detectnet done\n";}
unset($f);
$f[]="#!/bin/sh";
$f[]=LOCATE_PHP5_BIN2()." " .__FILE__." --ifdown \$4 \$5 \${IFNAME} \${PPP_IPPARAM}";
$f[]="";
file_put_contents("/etc/ppp/ip-down.d/detectnet",@implode("\n",$f));
chmod("/etc/ppp/ip-down.d/detectnet",0755);
if($GLOBALS["VERBOSE"]){echo "DEBUG:pptp_hook(): /etc/ppp/ip-down.d/detectnet done\n";}
unset($f);
}
// $4 ip of IFNAME
// $5 server ip
//php5 /usr/share/artica-postfix/exec.pptpd.php $4 $5 ${IFNAME} ${PPP_IPPARAM}
function client_net_config_down($localip,$serverip,$IFNAME,$PPP_IPPARAM){
events("LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",__FUNCTION__,__FILE__,__LINE__);
vpn_msql_events("VPN Tunnel stopped ","LOCAL IP:<b>$localip</b><br>SERVER=<strong>$serverip</strong><br>INTERFACE=<strong>$IFNAME</strong><br>CONNECTION:<strong>$PPP_IPPARAM</strong>",$PPP_IPPARAM);
if(preg_match("#[0-9\.]+\.[0-9\.]+\.[0-9\.]+\.[0-9\.]+#",$PPP_IPPARAM)){
send_email_events("VPN (PPTPD) connection $localip closed for remote $PPP_IPPARAM","LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",'VPN');
events("PPP_IPPARAM \"$PPP_IPPARAM\", server detected delete bridge for $IFNAME if it was created on server...",__FUNCTION__,__FILE__,__LINE__);
DeleteBridge($IFNAME);
ServerRoutesDel($IFNAME,$serverip,$PPP_IPPARAM);
events("IF-DOWN:: $PPP_IPPARAM:: STOPPING SCRIPT",__FUNCTION__,__FILE__,__LINE__);
return;
}
events("IF-UP:: $PPP_IPPARAM:: client detected $IFNAME deleted on client...",__FUNCTION__,__FILE__,__LINE__);
send_email_events("VPN (PPTP) connection $PPP_IPPARAM closed for remote $serverip","LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",'VPN');
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
if(is_array($array[$PPP_IPPARAM]["ROUTES"])){
events("IF-DOWN:: $PPP_IPPARAM:: ". count($array[$PPP_IPPARAM]["ROUTES"]). " routes",__FUNCTION__,__FILE__,__LINE__);
if(count($array[$PPP_IPPARAM]["ROUTES"])>0){
client_routes_del($array[$PPP_IPPARAM]["ROUTES"],$PPP_IPPARAM,$IFNAME);
}
}
DeleteBridge($IFNAME);
}
function client_net_config($localip,$serverip,$IFNAME,$PPP_IPPARAM){
$sock=new sockets();
events("IF-UP:: $PPP_IPPARAM:: LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",__FUNCTION__,__FILE__,__LINE__);
vpn_msql_events("VPN Tunnel started ","LOCAL IP:<b>$localip</b><br>SERVER=<strong>$serverip</strong><br>INTERFACE=<strong>$IFNAME</strong><br>CONNECTION:<strong>$PPP_IPPARAM</strong>",$PPP_IPPARAM);
if(preg_match("#[0-9\.]+\.[0-9\.]+\.[0-9\.]+\.[0-9\.]+#",$PPP_IPPARAM)){
send_email_events("VPN (PPTPD) connection $localip open for remote $PPP_IPPARAM","LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",'VPN');
events("IF-UP:: PPP_IPPARAM \"$PPP_IPPARAM\", server detected new $IFNAME created on server...",__FUNCTION__,__FILE__,__LINE__);
$PPTPDConfig=unserialize(base64_decode($sock->GET_INFO("PPTPDConfig")));
if($PPTPDConfig["bcrelay"]<>null){
events("IF-UP:: $PPP_IPPARAM::Link $IFNAME to {$PPTPDConfig["bcrelay"]}",__FUNCTION__,__FILE__,__LINE__);
CreateBridge($PPTPDConfig["bcrelay"],$IFNAME);
if($PPTPDConfig["LINK_NET_FROM"]==1){
events("Link {$PPTPDConfig["bcrelay"]} to $IFNAME",__FUNCTION__,__FILE__,__LINE__);
CreateBridge($IFNAME,$PPTPDConfig["bcrelay"]);
ServerRoutesAdd($IFNAME,$serverip,$PPP_IPPARAM);
}
}
events("IF-UP:: $PPP_IPPARAM:: STOPPING SCRIPT",__FUNCTION__,__FILE__,__LINE__);
return;
}else{
send_email_events("VPN (PPTP) connection $PPP_IPPARAM open for remote $serverip","LOCAL=$localip,SERVER=$serverip,NIC=$IFNAME,CON:$PPP_IPPARAM",'VPN');
events("IF-UP:: $PPP_IPPARAM:: client detected new $IFNAME created on client...",__FUNCTION__,__FILE__,__LINE__);
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
if(is_array($array[$PPP_IPPARAM]["ROUTES"])){
events("IF-UP:: $PPP_IPPARAM:: ". count($array[$PPP_IPPARAM]["ROUTES"]). " routes",__FUNCTION__,__FILE__,__LINE__);
if(count($array[$PPP_IPPARAM]["ROUTES"])>0){
client_routes_add($array[$PPP_IPPARAM]["ROUTES"],$PPP_IPPARAM,$IFNAME);
}
}
if($array[$PPP_IPPARAM]["LANTOLAN"]==1){
events("IF-UP:: $PPP_IPPARAM:: Act has a gateway",__FUNCTION__,__FILE__,__LINE__);
shell_exec("$sysctl -w net.ipv4.ip_forward=1 >/dev/null 2>&1");
CreateBridge($array[$PPP_IPPARAM]["ETH_LINK"],$IFNAME);
CreateBridge($IFNAME,$array[$PPP_IPPARAM]["ETH_LINK"]);
}
events("IF-UP:: $PPP_IPPARAM:: STOPPING SCRIPT",__FUNCTION__,__FILE__,__LINE__);
}
}
function ServerRoutesAdd($IFNAME,$ip_connect,$PPP_IPPARAM){
$unix=new unix();
$route=$unix->find_program("route");
if(!is_file($route)){events("IF-UP:: $PPP_IPPARAM:: ip tool no such file",__FUNCTION__,__FILE__,__LINE__);return;}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPDConfigRoutes")));
if(count($array)==0){return;}
while (list ($ip, $cdir) = each ($array) ){
$pattern=str_replace(".",'\.',$ip);
$pattern=str_replace("*",'.?',$ip);
if(preg_match("#$pattern#",$ip_connect)){
events("$PPP_IPPARAM:: Link $cdir to NIC $IFNAME",__FUNCTION__,__FILE__,__LINE__);
vpn_msql_events("IF-UP:: $PPP_IPPARAM:: Link $cdir to NIC $IFNAME",null,$PPP_IPPARAM);
$cmd="$route add -net $cdir dev $IFNAME";
events("IF-UP:: $PPP_IPPARAM:: $cmd",__FUNCTION__,__FILE__,__LINE__);
exec($cmd,$results);
while (list ($a, $b) = each ($results) ){events("IF-UP:: $PPP_IPPARAM::$b",__FUNCTION__,__FILE__,__LINE__);}
}
}
}
function ServerRoutesDel($IFNAME,$ip_connect,$PPP_IPPARAM){
$unix=new unix();
$route=$unix->find_program("route");
if(!is_file($route)){events("IF-DOWN:: $PPP_IPPARAM:: ip tool no such file",__FUNCTION__,__FILE__,__LINE__);return;}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPDConfigRoutes")));
if(count($array)==0){return;}
while (list ($ip, $cdir) = each ($array) ){
$pattern=str_replace(".",'\.',$ip);
$pattern=str_replace("*",'.?',$ip);
if(preg_match("#$pattern#",$ip_connect)){
events("$PPP_IPPARAM:: unlink $cdir to NIC $IFNAME",__FUNCTION__,__FILE__,__LINE__);
vpn_msql_events("IF-DOWN:: $PPP_IPPARAM:: Link $cdir to NIC $IFNAME",null,$PPP_IPPARAM);
$cmd="$route del -net $cdir dev $IFNAME";
events("IF-DOWN:: $PPP_IPPARAM:: $cmd",__FUNCTION__,__FILE__,__LINE__);
exec($cmd,$results);
while (list ($a, $b) = each ($results) ){events("IF-UP:: $PPP_IPPARAM::$b",__FUNCTION__,__FILE__,__LINE__);}
}
}
}
function client_routes_add($array,$PPP_IPPARAM,$IFNAME){
$unix=new unix();
$route=$unix->find_program("route");
if(!is_file($route)){events("IF-UP:: $PPP_IPPARAM:: ip tool no such file",__FUNCTION__,__FILE__,__LINE__);return;}
while (list ($network, $config) = each ($array) ){
$use_vpn_server=$config["use_vpn_server"];
$gateway=$config["gateway"];
events("IF-UP:: $PPP_IPPARAM:: $network gateway:$gateway, use_vpn_server=$use_vpn_server",__FUNCTION__,__FILE__,__LINE__);
if($use_vpn_server==1){
$cmd="$route add -net $network dev $IFNAME";
events("IF-UP:: $PPP_IPPARAM:: $cmd",__FUNCTION__,__FILE__,__LINE__);
exec($cmd,$results);
while (list ($a, $b) = each ($results) ){events("IF-UP:: $PPP_IPPARAM::$b",__FUNCTION__,__FILE__,__LINE__);}
}
}
}
function client_routes_del($array,$PPP_IPPARAM,$IFNAME){
$unix=new unix();
$route=$unix->find_program("route");
if(!is_file($route)){events("IF-UP:: $PPP_IPPARAM:: ip tool no such file",__FUNCTION__,__FILE__,__LINE__);return;}
while (list ($network, $config) = each ($array) ){
$use_vpn_server=$config["use_vpn_server"];
$gateway=$config["gateway"];
events("IF-UP:: $PPP_IPPARAM:: $network gateway:$gateway, use_vpn_server=$use_vpn_server",__FUNCTION__,__FILE__,__LINE__);
if($use_vpn_server==1){
$cmd="$route del -net $network dev $IFNAME";
events("IF-UP:: $PPP_IPPARAM:: $cmd",__FUNCTION__,__FILE__,__LINE__);
exec($cmd,$results);
while (list ($a, $b) = each ($results) ){events("IF-UP:: $PPP_IPPARAM::$b",__FUNCTION__,__FILE__,__LINE__);}
}
}
}
function CreateBridge($localnic,$remotenic){
$comment="PPTP:$localnic-$remotenic";
events("BRIDGE:: $comment",__FUNCTION__,__FILE__,__LINE__);
$unix=new unix();
$iptables=$unix->find_program("iptables");
$iptables_rules[]="$iptables -A FORWARD -i $remotenic -o $localnic -m state --state ESTABLISHED,RELATED -j ACCEPT -m comment --comment \"$comment\" 2>&1";
$iptables_rules[]="$iptables -A FORWARD -i $remotenic -o $localnic -j ACCEPT -m comment --comment \"$comment\" 2>&1";
$iptables_rules[]="$iptables -t nat -A POSTROUTING -o $localnic -j MASQUERADE -m comment --comment \"$comment\" 2>&1";
while (list ($index, $cmd) = each ($iptables_rules) ){
events("$cmd",__FUNCTION__,__FILE__,__LINE__);
shell_exec($cmd);
}
}
function DeleteBridge($remotenic){
$unix=new unix();
events("IPTABLES_DELETE_REGEX_ENTRIES(\"PPTP:.*?$remotenic\")",__FUNCTION__,__FILE__,__LINE__);
$unix->IPTABLES_DELETE_REGEX_ENTRIES("PPTP:.*?$remotenic");
}
function clients_start(){
if(!is_file("/var/log/ppp-ipupdown.log")){shell_exec("/bin/touch /var/log/ppp-ipupdown.log");}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
if(!is_array($array)){return;}
if(count($array)==0){return;}
while (list ($connexionname, $PPTPDConfig) = each ($array) ){
if($PPTPDConfig["ENABLED"]<>1){continue;}
events("con:$connexionname server:{$PPTPDConfig["vpn_servername"]}",__FUNCTION__,__FILE__,__LINE__);
client_start_connexion($connexionname,$PPTPDConfig["vpn_servername"]);
}
}
function clients_stop(){
if(!is_file("/var/log/ppp-ipupdown.log")){shell_exec("/bin/touch /var/log/ppp-ipupdown.log");}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
if(!is_array($array)){return;}
if(count($array)==0){return;}
while (list ($connexionname, $PPTPDConfig) = each ($array) ){
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->con:$connexionname server:{$PPTPDConfig["vpn_servername"]}\n";}
client_stop_connexion($connexionname,$PPTPDConfig["vpn_servername"]);
}
}
function client_stop_connexion($connexionname,$server){
$unix=new unix();
$kill=$unix->find_program("kill");
$poff=$unix->find_program("poff");
$arrayPIDS=client_is_active($connexionname);
if(!is_array($arrayPIDS)){
echo "Stopping PPTP................: connection $connexionname already stopped\n";
return;
}
echo "Stopping PPTP................: connection $connexionname PIDs ". @implode(", ",$arrayPIDS)."\n";
$cmd="$kill ".@implode(" ",$arrayPIDS)." >/dev/null 2>&1";
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->$cmd\n";}
shell_exec($cmd);
sleep(2);
for($i=0;$i<5;$i++){
$arrayPIDS=client_is_active($connexionname);
if(!is_array($arrayPIDS)){break;}
shell_exec($cmd);
sleep(1);
}
$arrayPIDS=client_is_active($connexionname);
if(is_array($arrayPIDS)){
echo "Stopping PPTP................: connection $connexionname failed\n";
return;
}
echo "Stopping PPTP................: connection $connexionname success\n";
}
function client_start_connexion($connexionname,$server){
$unix=new unix();
$pptp=$unix->find_program("pptp");
$nohup=$unix->find_program("nohup");
if(!is_file($pptp)){
echo "Starting......: ".date("H:i:s")." PPTP VPN Client pptp no such file\n";
return null;
}
$arrayPIDS=client_is_active($connexionname);
if(is_array($arrayPIDS)){
events("$connexionname already executed PIDs ". @implode(", ",$arrayPIDS)."",__FUNCTION__,__FILE__,__LINE__);
echo "Starting......: ".date("H:i:s")." PPTP VPN connection $connexionname already executed PIDs ". @implode(", ",$arrayPIDS)."\n";
return;
}
echo "Starting......: ".date("H:i:s")." PPTP VPN Client connection $connexionname@$server\n";
$cmd="$nohup $pptp $server call $connexionname >/dev/null 2>&1 &";
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->$cmd\n";}
shell_exec($cmd);
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->sleep(1)\n";}
sleep(1);
for($i=0;$i<5;$i++){
$arrayPIDS=client_is_active($connexionname);
if(is_array($arrayPIDS)){break;}
sleep(1);
}
if(is_array($arrayPIDS)){
echo "Starting......: ".date("H:i:s")." PPTP VPN connection $connexionname successfully started PIDs ". @implode(", ",$arrayPIDS)."\n";
return;
}
echo "Starting......: ".date("H:i:s")." PPTP VPN connection $connexionname failed\n";
}
function client_is_active($connexionname){
if($GLOBALS["PGREP"]==null){
$unix=new unix();
$GLOBALS["PGREP"]=$unix->find_program("pgrep");
}
$cmd="{$GLOBALS["PGREP"]} -l -f \"pptp.+?call $connexionname\"";
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->$cmd\n";}
exec($cmd,$results);
while (list ($num, $line) = each ($results) ){
if(preg_match("#^([0-9]+).+?pptp#",$line,$re)){
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." ->PID: {$re[1]}\n";}
$arr[$re[1]]=$re[1];
}else{
if($GLOBALS["VERBOSE"]){echo __FUNCTION__." NO MATCH \"$line\"\n";}
}
}
return $arr;
}
function ClientsStatus(){
$unix=new unix();
$pptp=$unix->find_program("pptp");
if(!is_file($pptp)){return null;}
$sock=new sockets();
$array=unserialize(base64_decode($sock->GET_INFO("PPTPVpnClients")));
}
function vpn_msql_events($subject,$text,$IPPARAM){
$subject=addslashes($subject);
$text=addslashes($text);
$time=time();
$sql="INSERT INTO vpn_events (`stime`,`subject`,`text`,`IPPARAM`)
VALUES('$time','$subject','$text','$IPPARAM')";
$q=new mysql();
$q->QUERY_SQL($sql,"artica_events");
if(!$q->ok){events($q->mysql_error ." $sql",__FUNCTION__,__FILE__,__LINE__);}
}
function events($text,$function,$file=null,$line=0){
$pid=@getmypid();
$date=@date("H:i:s");
$logFile="/var/log/artica-postfix/pptp.log";
$size=@filesize($logFile);
if($size>1000000){@unlink($logFile);}
$f = @fopen($logFile, 'a');
$text="[$pid] $date $function:: $text (L.$line)\n";
if($GLOBALS["VERBOSE"]){echo $text;}
@fwrite($f, $text);
@fclose($f);
if($GLOBALS["VERBOSE"]){writelogs($text,$function,$file,$line);}
}