forked from poppyred/1.6.x
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exec.user.ask.meta-client.php
executable file
·552 lines (450 loc) · 15.9 KB
/
exec.user.ask.meta-client.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
<?php
if(preg_match("#--verbose#",implode(" ",$argv))){
$GLOBALS["VERBOSE"]=true;ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);
}
include_once(dirname(__FILE__)."/ressources/class.templates.inc");
include_once(dirname(__FILE__)."/ressources/class.ldap.inc");
include_once(dirname(__FILE__)."/ressources/class.user.inc");
include_once(dirname(__FILE__)."/ressources/class.sockets.inc");
include_once(dirname(__FILE__)."/ressources/class.mysql.inc");
include_once(dirname(__FILE__)."/ressources/class.resolv.conf.inc");
include_once(dirname(__FILE__)."/ressources/class.system.network.inc");
include_once(dirname(__FILE__)."/ressources/class.system.nics.inc");
include_once(dirname(__FILE__)."/framework/class.unix.inc");
include_once(dirname(__FILE__)."/framework/frame.class.inc");
include_once(dirname(__FILE__)."/ressources/class.os.system.inc");
include_once(dirname(__FILE__)."/ressources/class.tcpip.inc");
MAIN_MENU();
function MAIN_MENU(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
echo "MetaClient Menu\n";
echo "---------------------------------------------\n";
echo "Join to a Meta server............: [1]\n";
echo "Ping the Meta server.............: [2]\n";
echo "Exit menu........................: [q]\n";
echo "\n";
$answer=trim(strtolower(fgets(STDIN)));
switch ($answer) {
case "1":ACTION_JOIN();break;
case "2":ACTION_PING();break;
case "3":ACTION_KILL_IPTABLES();break;
case "4":ACTION_DNS();break;
case "5":REMOVE_NETWORK();break;
case "6":REBUILD_NETWORK();break;
case "q":die();break;
default:
;
break;
MAIN_MENU();
return;
}
}
function ACTION_KILL_IPTABLES(){
$unix=new unix();
echo "Warning, this operation will remove all NAT/REDIRECT methods.\n";
echo "Do you need to perform this operation ?\n";
echo "Enter y to confirm or any key to abort.\n";
$answer=trim(strtolower(fgets(STDIN)));
if($answer<>"y"){return;}
$iptables=$unix->find_program("iptables");
shell_exec("$iptables -P INPUT ACCEPT");
shell_exec("$iptables -P FORWARD ACCEPT");
shell_exec("$iptables -P OUTPUT ACCEPT");
shell_exec("$iptables -t nat -F");
shell_exec("$iptables -t mangle -F");
shell_exec("$iptables -F");
shell_exec("$iptables -X");
echo "\nSuccess\nPress any key to exit.\n";
$answer = trim(strtolower(fgets(STDIN)));
return;
}
function REBUILD_NETWORK(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
system("/etc/init.d/artica-ifup reconfigure");
echo "\nPress any key to exit.\n";
$answer = trim(strtolower(fgets(STDIN)));
return;
}
function ACTION_NETWORK_RESTART(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
system("/etc/init.d/artica-ifup stop");
system("/etc/init.d/artica-ifup start");
echo "\nPress any key to exit.\n";
$answer = trim(strtolower(fgets(STDIN)));
return;
}
function ACTION_DNS(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
echo "This wizard will help to configure DNS.\n";
echo "Press q letter to exit or any key to continue:";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
if(is_file($clear)){system("$clear");}
$tcp=new IP();
$GLOBALS["PROGRESS"]=true;
$f=explode("\n",@file_get_contents("/etc/resolv.conf"));
while (list ($gpid, $val) = each ($f) ){
if(preg_match("#^domain\s+(.+)#", $val,$re)){ $domain=$re[1]; break; }
}
$DN1=ASK_DNS1();
$DN2=ASK_DNS2();
echo "Set the search domain suffix: [$domain]\n";
$domain2 = trim(strtolower(fgets(STDIN)));
if($domain2<>null){$domain=$domain2;}
echo "DNS server 1........: \"$DN1\"\n";
echo "DNS server 2........: \"$DN2\"\n";
echo "Search domain suffix: \"$domain\"\n";
echo "\n";
//ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);
echo "If your are agree with these settings\n";
echo "Press any key to apply settings or press \"q\" to return to menu.\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
echo "Loading DNS library...\n";
$GLOBALS["PROGRESS"]=true;
$resolv=new resolv_conf();
$resolv->MainArray["DNS1"]=$DN1;
$resolv->MainArray["DNS2"]=$DN2;
$resolv->MainArray["DOMAINS1"]=$domain;
echo "Saving DNS items ( please wait )...\n";
$resolv->save();
if($domain<>null){
$f[]="domain $domain";
$f[]="search $domain";
}
$f[]="options attempts:5 timeout:5";
$f[]="nameserver $DN1";
$f[]="nameserver $DN2\n";
@file_put_contents("/etc/resolv.conf", @implode("\n", $f));
echo "Saving DNS items done...\n";
echo "Press any key to return to menu.";
$answer = trim(strtolower(fgets(STDIN)));
MAIN_MENU();
}
function ACTION_NETWORK(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
$users=new usersMenus();
$q=new mysql();
if(!$q->BD_CONNECT(true)){
echo "There is an issue while connecting to MySQL\n$q->mysql_error\nPress Key to exit.\n";
$line = fgets(STDIN);
return;
}
$DEFAULT=null;
$net=new networking();
$interfaces=$net->Local_interfaces();
unset($interfaces["lo"]);
if(isset($interfaces["eth0"])){$DEFAULT="eth0";}
while (list ($num, $letter) = each ($interfaces) ){
$int[]="\"$num\"";
}
if($DEFAULT==null){$DEFAULT=$int[0];}
$q->BuildTables();
echo "This wizard will help to configure network.\n";
echo "Press q letter to exit or any key to continue:";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
if(is_file($clear)){system("$clear");}
echo "Give here the interface name of the network interface\n";
echo "you need to setup.\n\n";
echo "Should be one of :".@implode(", ", $int)."\n";
echo "Default: [$DEFAULT]\n";
$NIC = trim(strtolower(fgets(STDIN)));
if($NIC==null){$NIC=$DEFAULT;}
if(!preg_match("#([a-z])([0-9+)$#", $NIC)){$NIC=$DEFAULT;}
$ETH_IP=trim(ASK_ETH_IP($NIC));
$GATEWAY=trim(ASK_GATEWAY($NIC));
$NETMASK=trim(ASK_NETMASK($NIC));
$DNS=trim(ASK_DNS1($NIC));
if(is_file($clear)){system("$clear");}
echo "Your Settings:\n";
echo "Interface.........: \"$NIC\"\n";
echo "IP address........: \"$ETH_IP\"\n";
echo "Gateway...........: \"$GATEWAY\"\n";
echo "Netmask...........: \"$NETMASK\"\n";
echo "DNS server 1......: \"$DNS\"\n";
echo "\n";
//ini_set('display_errors', 1);ini_set('error_reporting', E_ALL);ini_set('error_prepend_string',null);ini_set('error_append_string',null);
echo "If your are agree with these settings\n";
echo "Press any key to apply settings or press \"q\" to return to menu.\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
echo "5%] Please Wait, saving configuration...\n";
$nics=new system_nic($NIC);
$nics->eth=$NIC;
$nics->IPADDR=$ETH_IP;
$nics->NETMASK=$NETMASK;
$nics->GATEWAY=$GATEWAY;
$nics->DNS1=$DNS;
$nics->dhcp=0;
$nics->metric=1;
$nics->defaultroute=1;
$nics->enabled=1;
echo "7%] Please Wait, saving Networks parameters to MySQL DB...\n";
if(!$nics->SaveNic()){
echo "There is an issue while saving your settings\n";
echo "Press any key to exit.\n";
$answer = trim(strtolower(fgets(STDIN)));
return;
}
echo "10%] Please Wait, building configuration....\n";
$php=$unix->LOCATE_PHP5_BIN();
$nohup=$unix->find_program("nohup");
$php5=$php;
shell_exec2("$php5 ".dirname(__FILE__)." /exec.virtuals-ip.php --build --force >/dev/null 2>&1");
echo "20%] Please Wait, apply network configuration....\n";
shell_exec2("$php5 /usr/share/artica-postfix/exec.initslapd.php");
shell_exec2("/etc/init.d/artica-ifup start");
echo "30%] Please Wait, restarting services....\n";
$unix->THREAD_COMMAND_SET("$php5 /usr/share/artica-postfix/exec.postfix.maincf.php --reconfigure");
$unix->THREAD_COMMAND_SET("/usr/share/artica-postfix/bin/artica-install --reconfigure-cyrus");
shell_exec2("$nohup /etc/init.d/artica-status reload >/dev/null 2>&1 &");
shell_exec2("$nohup /etc/init.d/nginx restart >/dev/null 2>&1 &");
shell_exec2("$nohup /etc/init.d/monit restart >/dev/null 2>&1 &");
echo "30%] Please Wait, Changing IP address to $NIC....\n";
$ifconfig=$unix->find_program("ifconfig");
shell_exec2("$ifconfig $NIC down");
shell_exec2("$ifconfig $NIC $ETH_IP netmask $NETMASK up");
shell_exec2("/bin/ip route add 127.0.0.1 dev lo");
if($GATEWAY<>"0.0.0.0"){
echo "31%] Please Wait, Define default gateway to $GATEWAY....\n";
shell_exec2("/sbin/route add $GATEWAY dev $NIC");
$route=$unix->find_program("route");
shell_exec("$route add -net 0.0.0.0 gw $GATEWAY dev $NIC metric 1");
}
echo "80%] Please Wait, Changing DNS to $DNS....\n";
echo "81%] Please Wait, Loading DNS library\n";
$GLOBALS["PROGRESS"]=true;
$resolv=new resolv_conf();
echo "92%] Set DNS1 to $DNS\n";
$resolv->MainArray["DNS1"]=$DNS;
$resolv->output=true;
echo "93%] Saving config\n";
$resolvDatas=$resolv->build();
echo "94%] Saving /etc/resolv.conf\n";
@file_put_contents("/etc/resolv.conf", $resolvDatas);
echo "95%] Restarting Web Console\n";
shell_exec2("$nohup /etc/init.d/artica-webconsole restart");
echo "100%] Configuration done.\n";
echo "Press any key to return to menu.";
$answer = trim(strtolower(fgets(STDIN)));
MAIN_MENU();
}
function shell_exec2($cmd){
echo "Executing: $cmd\n";
shell_exec($cmd);
}
function ASK_ETH_IP($NIC){
$tcp=new IP();
$unix=new unix();
$sock=new sockets();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
if($NIC=="eth0"){
$savedsettings=unserialize(base64_decode($sock->GET_INFO("WizardSavedSettings")));
$DEFAULT=$savedsettings["IPADDR"];
}
if($DEFAULT==null){
$NETWORK_ALL_INTERFACES=$unix->NETWORK_ALL_INTERFACES();
$DEFAULT=$NETWORK_ALL_INTERFACES[$NIC]["IPADDR"];
}
echo "$NIC TCP/IP address:\n";
echo "Set here the IP address of your $NIC interface (default $DEFAULT)\n";
$ip=trim(strtolower(fgets(STDIN)));
if($ip==null){$ip=$DEFAULT;}
if(!$tcp->isValid($ip)){
echo "$ip is not a valid IP address\n";
echo "Type q to exit or press key to retry\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
ASK_ETH_IP($NIC);
return $ip;
}
return $ip;
}
function REMOVE_NETWORK(){
$unix=new unix();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
echo "This section will flush network parameters\n";
echo "Type 'yes' to confirm the operation or 'q' to exit\n\n";
$asw=trim(strtolower(fgets(STDIN)));
if($asw=="q"){return;}
$q=new mysql();
$q->QUERY_SQL("TRUNCATE table `nics`","artica_backup");
if(!$q->ok){
echo "Flush network failed\n$q->mysql_error\n";
$asw=trim(strtolower(fgets(STDIN)));
return;
}
echo "Flush network Success\nPress Enter to return\n";
$asw=trim(strtolower(fgets(STDIN)));
}
function ACTION_PING(){
$tcp=new IP();
$DEFAULT=null;
$unix=new unix();
$sock=new sockets();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
$php5=$unix->LOCATE_PHP5_BIN();
$ArticaMetaUsername=$sock->GET_INFO("ArticaMetaUsername");
$ArticaMetaPassword=$sock->GET_INFO("ArticaMetaPassword");
$ArticaMetaHost=$sock->GET_INFO("ArticaMetaHost");
$ArticaMetaPort=intval($sock->GET_INFO("ArticaMetaPort"));
if($ArticaMetaPort==0){$ArticaMetaPort=9000;}
$EnableArticaMetaServer=intval($sock->GET_INFO("EnableArticaMetaServer"));
if($EnableArticaMetaServer==1){
echo "This is a Meta server, it cannot PING itself\n";
echo "Press Enter key to return to Menu:\n";
$pass=trim(strtolower(fgets(STDIN)));
return;
}
system("$php5 /usr/share/artica-postfix/exec.artica-meta-client.php --ping --output --progress --force");
echo "Press Enter key to return to Menu:\n";
$pass=trim(strtolower(fgets(STDIN)));
}
function ACTION_JOIN(){
$tcp=new IP();
$DEFAULT=null;
$unix=new unix();
$sock=new sockets();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
$php5=$unix->LOCATE_PHP5_BIN();
$ArticaMetaUsername=$sock->GET_INFO("ArticaMetaUsername");
$ArticaMetaPassword=$sock->GET_INFO("ArticaMetaPassword");
$ArticaMetaHost=$sock->GET_INFO("ArticaMetaHost");
$ArticaMetaPort=intval($sock->GET_INFO("ArticaMetaPort"));
if($ArticaMetaPort==0){$ArticaMetaPort=9000;}
$EnableArticaMetaServer=intval($sock->GET_INFO("EnableArticaMetaServer"));
if($EnableArticaMetaServer==1){
echo "This is a Meta server, it cannot be a client\n";
echo "Press Enter key to return to Menu:\n";
$pass=trim(strtolower(fgets(STDIN)));
return;
}
echo "Give the address of the meta server [$ArticaMetaHost]:\n";
$ip=trim(strtolower(fgets(STDIN)));
if($ip==null){$ip=$ArticaMetaHost;}
echo "Give the Port of the meta server console [$ArticaMetaPort]:\n";
$port=trim(strtolower(fgets(STDIN)));
if($port==null){$port=$ArticaMetaPort;}
echo "Give the Meta account [$ArticaMetaUsername]:\n";
$user=trim(strtolower(fgets(STDIN)));
if($user==null){$port=$ArticaMetaUsername;}
echo "Give the Meta account password:\n";
$pass=trim(strtolower(fgets(STDIN)));
if($pass==null){$pass=$ArticaMetaPassword;}
if(is_file($clear)){system("$clear");}
$sock->SET_INFO("ArticaMetaUsername", $ArticaMetaUsername);
$sock->SET_INFO("ArticaMetaPassword", $ArticaMetaPassword);
$sock->SET_INFO("ArticaMetaHost", $ArticaMetaHost);
$sock->SET_INFO("ArticaMetaPort", $ArticaMetaPort);
$sock->SET_INFO("EnableArticaMetaClient",1);
echo "Join the Meta server...\n";
system("$php5 /usr/share/artica-postfix/exec.artica-meta-client.php --ping --output --progress --force");
echo "Press Enter key to return to Menu:\n";
$pass=trim(strtolower(fgets(STDIN)));
}
function ASK_NETMASK($NIC){
$tcp=new IP();
$DEFAULT=null;
$unix=new unix();
$sock=new sockets();
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
$DEFAULT="255.255.255.0";
if($NIC=="eth0"){
$savedsettings=unserialize(base64_decode($sock->GET_INFO("WizardSavedSettings")));
$DEFAULT=$savedsettings["NETMASK"];
}
if($DEFAULT==null){
$NETWORK_ALL_INTERFACES=$unix->NETWORK_ALL_INTERFACES();
$DEFAULT=$NETWORK_ALL_INTERFACES[$NIC]["NETMASK"];
}
echo "Netmask address:\n";
echo "Set here the Netmask of your $NIC interface (default $DEFAULT)\n";
$ip=trim(strtolower(fgets(STDIN)));
if($ip==null){$ip=$DEFAULT;}
if(!$tcp->isValid($ip)){
echo "`$ip` is not a valid IP address\n";
echo "Type q to exit or press key to retry\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
ASK_NETMASK($NIC);
return;
}
return $ip;
}
function ASK_DNS1($NIC=null){
$tcp=new IP();
$DEFAULT=null;
$unix=new unix();
$sock=new sockets();
$f=explode("\n",@file_get_contents("/etc/resolv.conf"));
while (list ($gpid, $val) = each ($f) ){
if(preg_match("#^nameserver\s+(.+)#", $val,$re)){
$DEFAULT=$re[1];
break;
}
}
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
if($NIC=="eth0"){
$savedsettings=unserialize(base64_decode($sock->GET_INFO("WizardSavedSettings")));
if($savedsettings["DNS1"]<>null){
$DEFAULT=$savedsettings["DNS1"];
}
}
echo "DNS address:\n";
echo "Set here the IP address of your first DNS server (default $DEFAULT)\n";
$ip=trim(strtolower(fgets(STDIN)));
if($ip==null){$ip=$DEFAULT;}
if(!$tcp->isValid($ip)){
echo "$ip is not a valid IP address\n";
echo "Type q to exit or press key to retry\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
return ASK_DNS1($NIC);
}
return $ip;
}
function ASK_DNS2(){
$tcp=new IP();
$DEFAULT=null;
$unix=new unix();
$sock=new sockets();
$f=explode("\n",@file_get_contents("/etc/resolv.conf"));
while (list ($gpid, $val) = each ($f) ){
if(preg_match("#^nameserver\s+(.+)#", $val,$re)){
$DNS[]=$re[1];
if(count($DNS)>1){break;}
}
}
$clear=$unix->find_program("clear");
if(is_file($clear)){system("$clear");}
if(count($DNS)>1){$DEFAULT=$DNS[1];}
echo "DNS address (2):\n";
echo "Set here the backup IP DNS server address (default $DEFAULT)\n";
$ip=trim(strtolower(fgets(STDIN)));
if($ip==null){$ip=$DEFAULT;}
if(!$tcp->isValid($ip)){
echo "$ip is not a valid IP address\n";
echo "Type q to exit or press key to retry\n";
$answer = trim(strtolower(fgets(STDIN)));
if($answer=="q"){return;}
return ASK_DNS2();
}
return $ip;
}