forked from oZONo32/EHCP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall_lib.php
executable file
·1799 lines (1419 loc) · 67.1 KB
/
install_lib.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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
$ehcpversion="0.38.b";
$emailenable=True;
$usePrompts=True;
# last modified by ehcpdeveloper on 5.7.2016 (d-m-y)
# include_once("config/dbutil.php"); # dbutil is being removed from project.
/*
notes to who want to change code, developers:
although this library uses functions and global variables,
this library is called by two different install routines, install_1.php and install_2.php
so, even global variables are not passed to install_2.php normal way.
you should put any variables that you want to pass to install_2.php in passvariablestoinstall2 function..
Todo:
php version will be detected, related commants will be adjusted to reflect php5, php7 (Ubuntu 16.04) or else..
*/
include_once("classapp.php");
require_once('console.php');
error_reporting (E_ALL ^ E_NOTICE);
$header="From: [email protected]";
if($installmode=='') $installmode='normal';
if(version_compare(phpversion(), '7.0.0', '<')) { # make compatible install in many versions of Ubuntu/Debian, Ubuntu 16.04 comes with php7, while olders are different
$php_version=5;
$php_version_tag="5";
$php_etc_dir="/etc/php5";
$php_fpm="php7.0-fpm";
} else {
$php_version=7;
$php_version_tag="";
$php_etc_dir="/etc/php/7.0";
$php_fpm="php5-fpm";
}
if(!function_exists("debugecho")){
function debugecho($str,$level=0) {
$currentlevel=4;
if($level>=$currentlevel) echo $str;
}
}
if(!function_exists("securefilename")){
function securefilename($fn){
$ret=trim($fn);
$ret=str_replace(['..','%','&'],['','',''],$fn);
#$ret=escapeshellarg($ret);
return $ret;
}
}
function installaptget(){
echo "now will try to install apt-get on your system. you need internet connection for this....\n";
echo "apt-get installation is not implemented yet \n\n";
}
function checkaptget(){
$cikti=system("which apt-get | wc -w");
echo "(Ehcp can only be installed on apt-get enabled Linux systems, such as Ubuntu, Debian, Mint etc.)\n";
if($cikti>0) {
echo "apt-get seems to be installed on your system. this is good. \n";
} else {
echo "apt-get is not installed..this is not good.. \n";
installaptget();
}
}
function log_to_file($str){
writeoutput("install_log.txt",$str."\n",'a',false);
}
function aptget($arr,$forceInteraction=False,$force_unattended=False){
/* this was like:
apt-get install build-essential dpkg-dev fakeroot debhelper libdb4.2-dev libgdbm-dev libldap2-dev libpcre3-dev libmysqlclient10-dev libssl-dev libsasl2-dev postgresql-dev po-debconf dpatch
but, when one package is not found, whole apt-get install was cancelling.
to avoid this, each is installed separately.
tr: herbirisi teker teker kuruluyor. yoksa hata verme ihtimali var.
iki tip kurulum uygulanabilir, biri hizli, tum apt ler tek seferde, digeri yavas, tek tek... ilk basta sorabilir..
* */
global $noapt, $unattended;
if(!is_array($arr)) $arr=[$arr]; # accept non-array
passthru("/usr/sbin/killall update-manager > /dev/null 2>&1");
passthru("/usr/sbin/killall update-notifier > /dev/null 2>&1"); # these cause other apt-get commands fail because of dpkg lock
if($noapt<>''){
echo "apt-get install of these skipped because of noapt parameter:";
print_r($arr);
return true;
}
foreach($arr as $prog) {
#
# first install try
# assumes yes, do not remove anything, allow any unauthenticated packages,
# do not remove: this is a security concern
$cmd="apt-get -y --no-remove --allow-unauthenticated install $prog";
# If unattended, don't show configuration options
if(($unattended && $forceInteraction == FALSE) or $force_unattended ){
$cmd = "DEBIAN_FRONTEND=noninteractive " . $cmd;
}
log_to_file($cmd);
cizgi();
echo "Starting apt-get install for: $prog\n(cmd: $cmd)\n\n";
passthru($cmd,$ret);
writeoutput("ehcp-apt-get-install.log",$cmd,"a",false);
if($ret==0) continue;
# second install try, if first fails :
# usefull if first one has failed, for reason such as a package has to be removed, if first apt-get exited for any reason, this one executes apt-get with not options, so that user can decide...
# if first is successfull, this actually does nothing... only prints that those packages are already installed...
# this way a bit slower, calls apt-get twice, but most "secure and avoids user intervention"
$cmd="apt-get install $prog";
echo "\nTrying second installation type for: $prog (cmd: $cmd)\n";
passthru($cmd);
writeoutput("ehcp-apt-get-install.log",$cmd,"a",false);
}
}//endfunc
function bosluk() {
echo "\n\n\n";
}
function cizgi() {
echo "\n---------------------------------------------------------------------\n";
}
function bosluk2() {
bosluk();
cizgi();
}
function ehcpheader() {
global $ehcpversion,$unattended;
cizgi();
echo "-----------------------EHCP MAIN INSTALLER---------------------------\n";
echo "------Easy Hosting Control Panel for Ubuntu, Debian and alikes ------\n";
echo "--------------------------www.ehcp.net-------------------------------\n";
cizgi();
echo "ehcp version $ehcpversion \n";
echo "ehcp installer version $ehcpversion\n";
echo "Unattended: $unattended\n";
}
function bekle($s='') { # wait
global $unattended;
if(!$unattended) getInput("press enter to continue: $s\n");
}
function getInput($prompt='',$default='',$allowempty=False) {
global $unattended;
if($unattended===True and $default<>'') return $default;
if($prompt<>'') echo $prompt;
$giris=trim(Console::GetLine());
if($giris=='') {
if($allowempty===True) return $giris;
else return $default; # return default, if input is empty and allowempty is not true
} else return $giris;
}
if(!function_exists("arraytofile")){
function arraytofile($file,$lines,$joinstr='') {
$new_content = join($joinstr,$lines);
$fp = fopen($file,'w');
$write = fwrite($fp, $new_content);
fclose($fp);
}
}
if(!function_exists("addifnotexists")){
function addifnotexists($what,$where) {
debugecho("\naddifnotexists: ($what) -> ($where) \n ",4);
#bekle(__FUNCTION__." basliyor..");
$what.="\n";
$filearr=@file($where);
if(!$filearr) {
echo "cannot open file, trying to setup: ($where)\n";
$fp = fopen($where,'w');
fclose($fp);
$filearr=file($where);
} //else print_r($file);
if(array_search($what,$filearr)===false) {
echo "dosyada bulamadı ekliyor: ($what) -> ($where)\n";
$filearr[]=$what;
arraytofile($where,$filearr);
} else {
//echo "buldu... sorun yok. \n";
// already found, so, do not add
}
#bekle(__FUNCTION__." bitti...");
}
}
function len($any){
return count($any);
}
function add_if_not_exists3($what,$where,$group) {
# digerinden farkı: istenilen my.cnf grubu altına ekler.
# especially for config files with [groups], such as my.cnf
$filearr=@file($where,FILE_IGNORE_NEW_LINES); # do not include \n character in strings
if(!$filearr) {
echo "cannot open file, trying to setup: ($where)\n";
$fp = fopen($where,'w');
fclose($fp);
$filearr=file($where);
}
# once group bulunacak:
$keys=array_keys($filearr,$group);
if(len($keys)==0) {
#echo "#group not found, add group&item \n";
$filearr[]=$group;
$filearr[]=$what;
} else {
$group_pos=$keys[0];
#echo "# group found.. at $group_pos \n";
$i=$group_pos+1; # start searching token from next line, until next group name
$found=False;
while($i<len($filearr) and $filearr[$i][0]!='[' and $filearr[$i]!=$what) $i++;
if($i>=len($filearr)) {
#echo "#dosya sonu geldi, sonuna ekle..\n";
$filearr[]=$what;
} elseif ($filearr[$i]==$what) {
#echo "# found, just return \n";
return ;
} else {
array_splice($filearr,$i,0,$what);
#echo "# insert item here: $i \n";
}
}
arraytofile($where,$filearr,"\n");
}
function add_if_not_exists2($what,$where,$addfile_if_not_exists=False) {
# add a string/config value onto (at the end of) a file...
# difference from addifnotexists: it uses arrays, this will use string, so, main.cf and similar config files will be handled better.. i hope..
# the $what should include newline too..
# may raise error if file too big for php strings..
# get file
$file=@file_get_contents($where);
if($file===false) {
if($addfile_if_not_exists) file_put_contents($where,'');
else {
echo __FUNCTION__.": cannot open file...($where ) \n";
return false;
}
}
# add if not exist:
$bul=strstr($file,$what);
if($bul===false) $file.=$what;
#write back
$ret=file_put_contents($where,$file);
if($ret===false){
echo __FUNCTION__.": cannot write file back: ($where) \n";
return false;
}
echo __FUNCTION__.": success add strings to $where \n";
return true;
}
function fail2ban_install(){ # thanks to [email protected]
global $installmode;
switch($installmode) {
case 'extra':
case 'normal':
echo "Starting fail2ban install \n";
aptget('fail2ban');
fail2ban_config();
echo "Finished fail2ban install \n";
break;
case 'light':
break;
default: echo "Unknown installmode parameter at ".__LINE__;
}
}
function fail2ban_config(){
global $ehcpinstalldir,$user_email;
copy("$ehcpinstalldir/fail2ban/ehcp.conf","/etc/fail2ban/filter.d/ehcp.conf");
$f="/etc/fail2ban/jail.local";
if(!file_exists($f)) copy("$ehcpinstalldir/fail2ban/jail.local",$f);
replacelineinfile("destemail","destemail = $user_email",$f);
$s=file_get_contents($f);
if(strstr($s,"[ehcp]")===false){ # if not already configured,
$ehcpF2Config="
[ehcp]
# fail2ban section for Easy Hosting Control Panel, ehcp.net
enabled = true
port = http,https
filter = ehcp
logpath = /var/www/new/ehcp/log/ehcp_failed_authentication.log
maxretry = 10";
file_put_contents($f,$ehcpF2Config,FILE_APPEND);
#append_to_file($f,$ehcpF2Config);
}
if(!file_exists("/etc/fail2ban/filter.d/apache-dos.conf")){
replacelineinfile("destemail[apache-dos]
enabled = true","[apache-dos]
enabled = false",$f);
}
}
function replace_in_file($find,$replace,$sourcefile,$targetfile){
# open source/sample file, find $find, replace it to $replace, write result to $target file
# especially for editing config files, like replacing {ehcppassword} to real passwords..
# get file
$file=file_get_contents($sourcefile);
if($file===false) {
echo __FUNCTION__.": cannot open file...($sourcefile ) \n";
return false;
}
# $find->$replace
$file=str_replace($find,$replace,$file);
#write back
$ret=file_put_contents($targetfile,$file);
if($ret===false){
echo __FUNCTION__.": cannot write file back: ($targetfile) \n";
return false;
}
echo __FUNCTION__.": success replace $find in $sourcefile -> $targetfile \n";
return true;
}
if(!function_exists('replacelineinfile')){
function replacelineinfile($find,$replace,$where) {
// edit a line starting with $find, to edit especially conf files..
debugecho("\nreplaceline: ($find -> $replace) in ($where) \n ");
$filearr=@file($where);
//if($find=='$dbrootpass=') print_r($filearr);
if(!$filearr) {
echo "cannot open file... returning...\n";
return false;
} //else print_r($file);
$len=strlen($find);
$newfile=[];
foreach($filearr as $line){
$line=trim($line)."\n";
$sub=substr($line,0,$len);
if($sub==$find) $line=$replace."\n";
$newfile[]=$line;
}
/*if($find=='$dbrootpass=') {
echo "yeni dosya:\n";
print_r($newfile);
}*/
arraytofile($where,$newfile);
}
}
if(!function_exists("editlineinfile")){
function editlineinfile($find,$replace,$where) {
// edit a line containing $find, replace it... to edit especially /etc/apt/sour/sources.list file
debugecho("\n replaceline: ($find -> $replace) in ($where) \n ");
$filearr=@file($where);
if(!$filearr) {
echo "cannot open file... returning...\n";
return false;
} //else print_r($file);
$newfile=[];
foreach($filearr as $line){
$line=trim($line)."\n";
$line=str_replace($find,$replace,$line);
$newfile[]=$line;
}
arraytofile($where,$newfile);
}
}
if(!function_exists("writeoutput")){
function writeoutput($file, $string, $mode="w",$log=true) {
if (!($fp = fopen($file, $mode))) {
echo "hata: dosya acilamadi: $file (writeoutput) !";
return false;
}
if (!fputs($fp, $string . "\n")) {
fclose($fp);
echo "hata: dosyaya yazilamadi: $file (writeoutput) !";
return false;
}
fclose($fp);
if($log) echo "\n(".__FILE__.") file written successfully: $file, mode:$mode \n";
return true;
}
}
if(!function_exists("getlocalip")){
function getlocalip($interface='eth0') {
global $localip;
$ipline=exec("ifconfig $interface 2>/dev/null | grep \"inet addr\" ");
$ipline=strstr($ipline,"addr:");
$pos=strpos($ipline," ");
$ipline=trim(substr($ipline,5,$pos-5));
$localip=$ipline;
return $ipline;
}
}
if(!function_exists("getlocalip2")){
function getlocalip2($interface='eth0') {
global $localip;
if($localip<>'') return $localip;
$ip='';
if($ip=='') $ip=getlocalip($interface);
if($ip=='') $ip=getlocalip('eth1');
if($ip=='') $ip=getlocalip('eth2');
if($ip=='') {
$ipline=exec("ifconfig | grep 'inet ' | grep 'addr' | grep 255.255 | grep -v '127.0.0' ");
$ipline=strstr($ipline,"addr:");
$pos=strpos($ipline," ");
$ip=trim(substr($ipline,5,$pos-5));
if($ip=='') {
echo "Your ip cannot be determined automatically... \nYour ip may be one of:\n";
system("ifconfig | grep 'inet addr:' 2>/dev/null ");
}
}
$localip=$ip;
return $ip;
}
}
function dovecot_install_configuration($params){
# use quide: http://workaround.org/articles/ispmail-etch/
# remove all courier
# install dovecot using apt-get
# configure dovecot using mysql auth....
}
function copyPostFixConfig(){
if(!file_exists('/etc/postfix/main.cf')) passthru2("cp ".$app->ehcpdir."/etc/postfix/main.cf.sample /etc/postfix/main.cf"); # on some systems, this is deleted somehow.
}
function mailNameFix(){
$mailname= @file_get_contents('/etc/mailname');
if(trim($mailname)==''){
$mailname="mail.".gethostname();
file_put_contents('/etc/mailname', $mailname);
}
}
function mailconfiguration($params) {
global $app,$ehcpinstalldir,$ip,$hostname,$user_email,$user_name,$ehcpmysqlpass,$rootpass,$newrootpass,$ehcpadminpass;
echo "configuring mail ... ".__FUNCTION__."\n";
# very similar to: https://help.ubuntu.com/community/PostfixCompleteVirtualMailSystemHowto
#print_r($params);
# echo 'var_dump($ehcpmysqlpass,$rootpass,$newrootpass,$ehcpadminpass);\n';
# var_dump($ehcpmysqlpass,$rootpass,$newrootpass,$ehcpadminpass);
/*
courier'e alternatif: dovecot:
*
http://www.opensourcehowto.org/how-to/mysql/mysql-users-postfixadmin-postfix-dovecot--squirrelmail-with-userprefs-stored-in-mysql.html
http://www.howtoforge.com/virtual-users-and-domains-postfix-dovecot-mysql-centos4.5
http://workaround.org/articles/ispmail-etch/
Gerekli arama: /etc/dovecot-mysql.conf
files to edit:
/etc/postfix/mysql-virtual_domains.cf
/etc/postfix/mysql-virtual_forwardings.cf
/etc/postfix/mysql-virtual_mailboxes.cf
/etc/postfix/mysql-virtual_email2email.cf
/etc/postfix/mysql-virtual_mailbox_limit_maps.cf
/etc/postfix/mysql-virtual_transports.cf
maybe we can switch to dovecot, if i can, a good start: http://workaround.org/ispmail/etch
*/
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = domains
select_field = 'virtual'
where_field = domainname
hosts = localhost
additional_conditions = and domainname in (select DISTINCT domainname from emailusers union select domainname from forwardings union select domainname from emailusers)
";
writeoutput("/etc/postfix/mysql-virtual_domains.cf",$filecontent,"w");
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = forwardings
select_field = destination
where_field = source
hosts = localhost
";
writeoutput("/etc/postfix/mysql-virtual_forwardings.cf",$filecontent,"w");
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = emailusers
select_field = CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/')
where_field = email
hosts = localhost
";
writeoutput("/etc/postfix/mysql-virtual_mailboxes.cf",$filecontent,"w");
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = emailusers
select_field = email
where_field = email
hosts = localhost
";
writeoutput("/etc/postfix/mysql-virtual_email2email.cf",$filecontent,"w");
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = emailusers
select_field = quota
where_field = email
hosts = localhost
";
writeoutput("/etc/postfix/mysql-virtual_mailbox_limit_maps.cf",$filecontent,"w");
# autoreply configuration: coded like: http://www.progression-asia.com/node/87
/*
I used ehcp's own php application, autoreply.php, instead of yaa.pl, since yaa.pl failed somehow, I wrote autoreply simply..
required db tables:(these will be setup in sql section)
CREATE TABLE transport (
domainname varchar(128) NOT NULL default '',
transport varchar(128) NOT NULL default '',
UNIQUE KEY domainname (domainname)
) TYPE=MyISAM;
*/
$filecontent="
user = ehcp
password = ".$params['ehcppass']."
dbname = ehcp
table = transport
select_field = transport
where_field = domainname
hosts = localhost
";
writeoutput("/etc/postfix/mysql-virtual_transports.cf",$filecontent,"w");
# edit main.cf:
$add="
# ehcp: autoresponder code:
ehcp_autoreply unix - n n - - pipe
user=vmail
argv=".$app->ehcpdir."/misc/autoreply.php \$sender \$recipient
";
add_if_not_exists2($add,'/etc/postfix/master.cf'); # this function may also be used to setup spamassassin and related stuff. soon to implement spamassassin support in ehcp automatically. (manually always possible..)
replace_in_file("#submission inet n - - - - smtpd","submission inet n - - - - smtpd",'/etc/postfix/master.cf','/etc/postfix/master.cf');
add_if_not_exists2("submission inet n - - - - smtpd",'/etc/postfix/master.cf'); # 587 kullanan yerler icin..
# classapp'da checktable yapılacak yenile..
# end autoreply configuration
copyPostFixConfig();
add_if_not_exists3("# lines added by ehcp ","/etc/mysql/my.cnf","[mysqld]");
add_if_not_exists3("bind-address=127.0.0.1","/etc/mysql/my.cnf","[mysqld]");
#add_if_not_exists3("skip-innodb","/etc/mysql/my.cnf","[mysqld]"); # disable innodb by default, because it consumes a lot of memory
# innodb is needed by some applications
add_if_not_exists3("character-set-server=utf8","/etc/mysql/my.cnf","[mysqld]");
add_if_not_exists3("collation-server=utf8_general_ci","/etc/mysql/my.cnf","[mysqld]");
add_if_not_exists3("default-storage-engine=myisam","/etc/mysql/my.cnf","[mysqld]");
add_if_not_exists3("# end lines added by ehcp ","/etc/mysql/my.cnf","[mysqld]");
passthru3("chmod o= /etc/postfix/mysql-virtual_*.cf");
passthru3("chgrp postfix /etc/postfix/mysql-virtual_*.cf");
#Now we setup a user and group called vmail with the home directory /home/vmail. This is where all mail boxes will be stored.
passthru3("groupdel vmail");
passthru3("userdel vmail");
echo "----------- Other user/group with uid/gid of 5000, you need to delete them, if any -----------";
passthru3("grep 5000 /etc/passwd ");
passthru3("grep 5000 /etc/group ");
echo "----------- ----------- ----------- ----------- ----------- ----------- ----------- ----------";
passthru3("groupadd -g 5000 vmail");
passthru3("useradd -g vmail -u 5000 vmail -d /home/vmail -m");
passthru3("chown -Rf vmail /home/vmail");
passthru3("adduser postfix sasl");
// burda input vardi... initialize a aktarildi..
$hostname=exec("hostname");
// ipnin ilk uc rakami alinip network alınacak
$ips=explode(".",$ip);
array_pop($ips);
$ips[]="0/24"; // calculate C class net number.
$net=implode(".",$ips);
passthru3("openssl req -new -config $ehcpinstalldir/LocalServer.cnf -outform PEM -out /etc/postfix/smtpd.cert -newkey rsa:2048 -nodes -keyout /etc/postfix/smtpd.key -keyform PEM -days 365 -x509");
passthru3("chmod o= /etc/postfix/smtpd.key");
passthru3("openssl req -passout pass:$ehcpmysqlpass -new -x509 -keyout /etc/postfix/cakey.pem -out /etc/postfix/cacert.pem -days 3650 -config $ehcpinstalldir/LocalServer.cnf"); ## yeni 13.6.2009
passthru3("postconf -e \"myhostname = $hostname\"");
passthru3("postconf -e \"relayhost = \"");
passthru3("postconf -e \"mydestination = localhost, $ip \"");
passthru3("postconf -e 'mynetworks = [::1]/128, 127.0.0.0/8, 192.168.0.0/16, 172.16.0.0/16, 10.0.0.0/8, $net '");
passthru3("postconf -e 'virtual_alias_domains ='");
passthru3("postconf -e 'virtual_alias_maps = proxy:mysql:/etc/postfix/mysql-virtual_forwardings.cf, proxy:mysql:/etc/postfix/mysql-virtual_email2email.cf'");
passthru3("postconf -e 'transport_maps = proxy:mysql:/etc/postfix/mysql-virtual_transports.cf'"); #autoresponder
passthru3("postconf -e 'virtual_mailbox_domains = proxy:mysql:/etc/postfix/mysql-virtual_domains.cf'");
passthru3("postconf -e 'virtual_mailbox_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailboxes.cf'");
passthru3("postconf -e 'virtual_mailbox_base = /home/vmail'");
passthru3("postconf -e 'virtual_uid_maps = static:5000'");
passthru3("postconf -e 'virtual_gid_maps = static:5000'");
passthru3("postconf -e 'smtpd_sasl_auth_enable = yes'");
passthru3("postconf -e 'smtpd_sasl_security_options = noanonymous'");
passthru3("postconf -e 'broken_sasl_auth_clients = yes'");
passthru3("postconf -e default_process_limit=10");
passthru3("postconf -e 'smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,check_client_access hash:/var/lib/pop-before-smtp/hosts,reject_unauth_destination'"); // this is used with pop-before-smtp
#passthru3("postconf -e 'smtpd_recipient_restrictions = permit_mynetworks,permit_sasl_authenticated,reject_unauth_destination'"); // this is used with sasl authenticated
passthru3("postconf -e 'smtp_use_tls = yes'"); ## yeni
passthru3("postconf -e 'smtpd_use_tls = yes'");
passthru3("postconf -e 'smtpd_tls_auth_only = no'"); ## yeni
passthru3("postconf -e 'smtpd_tls_CAfile = /etc/postfix/cacert.pem'"); ## yeni 13.6, dd.mm
passthru3("postconf -e 'smtpd_tls_cert_file = /etc/postfix/smtpd.cert'");
passthru3("postconf -e 'smtpd_tls_key_file = /etc/postfix/smtpd.key'");
# this is partially taken from https://help.ubuntu.com/8.04/serverguide/C/postfix.html
passthru3("postconf -e 'smtpd_tls_loglevel = 1'"); ## yeni 13.6, dd.mm
passthru3("postconf -e 'smtpd_tls_received_header = yes'"); ## yeni 13.6, dd.mm
passthru3("postconf -e 'smtpd_tls_session_cache_timeout = 3600s'"); ## yeni 13.6, dd.mm
passthru3("postconf -e 'tls_random_source = dev:/dev/urandom'"); ## yeni 13.6, dd.mm
# passthru3("postconf -e 'virtual_create_maildirsize = yes'");
# passthru3("postconf -e 'virtual_mailbox_extended = yes'");
passthru3("postconf -e 'virtual_mailbox_limit_maps = proxy:mysql:/etc/postfix/mysql-virtual_mailbox_limit_maps.cf'");
# passthru3("postconf -e 'virtual_mailbox_limit_override = yes'");
# passthru3("postconf -e 'virtual_maildir_limit_message = \"The user you are trying to reach is over quota.\"'");
# passthru3("postconf -e 'virtual_overquota_bounce = yes'"); # deprecated
passthru3("postconf -e 'debug_peer_list = '");
passthru3("postconf -e 'sender_canonical_maps = '");
passthru3("postconf -e 'debug_peer_level = 1'");
passthru3("postconf -e 'proxy_read_maps = \$local_recipient_maps \$mydestination \$virtual_alias_maps \$virtual_alias_domains \$virtual_mailbox_maps \$virtual_mailbox_domains \$relay_recipient_maps \$canonical_maps \$sender_canonical_maps \$recipient_canonical_maps \$relocated_maps \$mynetworks \$virtual_mailbox_limit_maps \$transport_maps'");
passthru3("postconf -e 'smtpd_banner =\$myhostname ESMTP \$mail_name powered by Easy Hosting Control Panel (ehcp) on Ubuntu, www.ehcp.net'");
passthru3("postconf -e 'default_process_limit = 3'"); # diğer türlü, onlarca proses açıyor, hiç gerek yok.
# many of above config became useless, for postfix. I will remove them later.
# passthru3("dpkg-statoverride --force --update --add root sasl 755 /var/run/saslauthd"); # may be required on some systems...
echo "configuring saslauthd \n";
passthru3("mkdir -p /var/spool/postfix/var/run/saslauthd");
# here, both params, options added, in case it may be changed.
$filecontent="
NAME=\"saslauthd\"
START=yes
MECHANISMS=\"pam\"
PARAMS=\"-n 1 -m /var/spool/postfix/var/run/saslauthd -r\"
OPTIONS=\"-n 1 -m /var/spool/postfix/var/run/saslauthd -r\"
";
# -n 1 : saslauthd process count
writeoutput("/etc/default/saslauthd",$filecontent,"w");
replacelineinfile("PIDFILE=","PIDFILE=\"/var/spool/postfix/var/run/\${NAME}/saslauthd.pid\"",'/etc/init.d/saslauthd');
configurepamsmtp(['ehcppass'=>$ehcpmysqlpass]);
echo "editing: /etc/postfix/sasl/smtpd.conf\n";
$filecontent="
pwcheck_method: saslauthd
mech_list: plain login
allow_plaintext: true
";
writeoutput("/etc/postfix/sasl/smtpd.conf",$filecontent,"w");
echo "Configuring Courier\n";
echo "Now configuring to tell Courier that it should authenticate against our MySQL database.";
addifnotexists("authmodulelist=\"authmysql\"","/etc/courier/authdaemonrc");
replacelineinfile("daemons=","daemons=1","/etc/courier/authdaemonrc");
//** tablo ismi degisirse, asagidaki emailusers da degismeli
configureauthmysql(['ehcppass'=>$ehcpmysqlpass]);
passthru("chown -Rvf postfix /var/lib/postfix/");
passthru("chmod -R 755 /var/spool/postfix");
passthru("chmod 1733 /var/spool/postfix/maildrop");
passthru2("newaliases"); # on some systems, aliases.db is deleted by user or somehow, this fixes that.
passthru("cp -vf pop-before-smtp.conf /etc/pop-before-smtp/");
if(!file_exists("/var/lib/pop-before-smtp/hosts.db")){
passthru2("mkdir /var/lib/pop-before-smtp");
passthru2("touch /var/lib/pop-before-smtp/hosts");
passthru2("postmap /var/lib/pop-before-smtp/hosts");
}
# adjust roundcube:
# adjust symlink for roundcube
passthru2("ln -s /usr/share/roundcube /var/www/new/ehcp/webmail2");
replacelineinfile("\$rcmail_config['default_host']","\$rcmail_config['default_host']='localhost';",'/etc/roundcube/main.inc.php');
# end adjust roundcube
foreach(['pop-before-smtp','postfix','saslauthd','courier-authdaemon','courier-imap','courier-imap-ssl','courier-pop','courier-pop-ssl'] as $service)
passthru("/usr/sbin/service $service restart");
passthru("postfix check");
}# end mailconfiguration
function configurepamsmtp($params){
echo "editing: /etc/pam.d/smtp (".__FUNCTION__.")\n";
$filecontent="
auth required pam_mysql.so user=ehcp passwd=".$params['ehcppass']." host=127.0.0.1 db=ehcp table=emailusers usercolumn=email passwdcolumn=password crypt=1
account sufficient pam_mysql.so user=ehcp passwd=".$params['ehcppass']." host=127.0.0.1 db=ehcp table=emailusers usercolumn=email passwdcolumn=password crypt=1
";
writeoutput("/etc/pam.d/smtp",$filecontent,"w");
}
function configureauthmysql($params){
echo "(".__FUNCTION__.")\n";
$filecontent="
MYSQL_SERVER localhost
MYSQL_USERNAME ehcp
MYSQL_PASSWORD ".$params['ehcppass']."
MYSQL_PORT 0
MYSQL_DATABASE ehcp
MYSQL_USER_TABLE emailusers
MYSQL_CRYPT_PWFIELD password
#MYSQL_CLEAR_PWFIELD password
MYSQL_UID_FIELD 5000
MYSQL_GID_FIELD 5000
MYSQL_LOGIN_FIELD email
MYSQL_HOME_FIELD \"/home/vmail\"
MYSQL_MAILDIR_FIELD CONCAT(SUBSTRING_INDEX(email,'@',-1),'/',SUBSTRING_INDEX(email,'@',1),'/')
#MYSQL_NAME_FIELD
MYSQL_QUOTA_FIELD quota
";
writeoutput("/etc/courier/authmysqlrc",$filecontent,"w");
replacelineinfile("MAXDAEMONS=","MAXDAEMONS=4","/etc/courier/imapd");
replacelineinfile("MAXPERIP=","MAXPERIP=2","/etc/courier/imapd");
}
# Returns just the release year of an Ubuntu distro
function getReleaseYear($ver){ #by [email protected]
if(isset($ver) && !empty($ver) && stripos($ver, '.') != FALSE){
$releaseYear = substr($ver, 0, stripos($ver, '.'));
return $releaseYear;
}
}
function mysqldebconf($rYear){ #by [email protected]
switch($rYear){
case "10":
$comms[] = "echo 'mysql-server-5.1 mysql-server/root_password password 1234' | debconf-set-selections";
$comms[] = "echo 'mysql-server-5.1 mysql-server/root_password_again password 1234' | debconf-set-selections";
break;
case "12":
case "13":
case "14":
case "15":
$comms[] = "echo 'mysql-server-5.5 mysql-server/root_password password 1234' | debconf-set-selections";
$comms[] = "echo 'mysql-server-5.5 mysql-server/root_password_again password 1234' | debconf-set-selections";
$comms[] = "echo 'mariadb-server-5.5 mysql-server/root_password password 1234' | debconf-set-selections";
$comms[] = "echo 'mariadb-server-5.5 mysql-server/root_password_again password 1234' | debconf-set-selections";
break;
}
# For all versions
$comms[] = "echo 'mysql-server mysql-server/root_password password 1234' | debconf-set-selections";
$comms[] = "echo 'mysql-server mysql-server/root_password_again password 1234' | debconf-set-selections";
return $comms;
}
function check_if_mysql_running(){
$out=executeProg3("ps aux | grep mysql | grep -v grep");
$ret=strlen($out)>20; # if running..
if($ret) echo "ehcp: Mysql/Mariadb seems running. this is good. \n\n";
return $ret;
}
function check_restart_mysql($checkpass=False){
# by ehcpdeveloper
global $rootpass;
$ret=check_if_mysql_running();
if($checkpass) {
if ($rootpass=='') echo "myql root pass seems empty. this is not normal/good.. \n\n";
$ret=$ret and checkmysqlpass("root",$rootpass);
}
$try=0;
while (!$ret) {
echo "mysql seems not running, trying to start it.. try: $try\n";
if($try<=3) {
passthru2("/usr/sbin/killall mysqld_safe"); # mysql hangs sometime.
passthru2("/usr/sbin/killall mysqld"); # mysql hangs sometime.
passthru2("/usr/sbin/killall mysqld");
passthru2("/usr/sbin/service mysql restart");
} elseif($try>3 and $try<=10) {
echo "# trying a hard kill, with signal 9 \n";
$cmd="ps aux | grep mysql | grep -v grep | awk '\{print \$2 \}' | xargs kill -9 ";
passthru($cmd);
} elseif($try>10) {
echo "Unfortunately, mysql has still not started after 10 tries. Check the problem on a separate command line.. sory.\n\n";
getInput();
return;
}
sleep(2);
$ret=check_if_mysql_running();
if($checkpass) {
$ret=$ret and checkmysqlpass("root",$rootpass);
}
$try++;
}
}
function installMySQLServ(){#by [email protected]
global $unattended, $distro, $version, $usePrompts, $php_version_tag;
# Get distro release year
$rYear = getReleaseYear($version);
# Get question answers for installer package
if($unattended && $distro == "ubuntu"){
$comms = mysqldebconf($rYear);
if(isset($comms) && is_array($comms)){
foreach($comms as $comm){
passthru3($comm);
}
}
}
aptget(['mariadb-server','mariadb-client']); # unattended install does not work for mariadb yet. I will do it later. This line is added to intall mariadb. Above mysql is kept, in case Ubuntu version is old, and if there is no maridb available.
# I am trying to switch to mariadb, to conform opensource software.
# I will try same for apache->nginx too.. Currently, ehcp supports nginx, but it is not default.
if(! file_exists("/etc/init.d/mysql") and !check_if_mysql_running() ) {
echo "Mariadb installation seems failed. continuing with normal mysql: \n";
# Install MySQL Server With Pre-Answered Prompts
aptget(['mysql-server','mysql-client'],$usePrompts);
} else {
print "installing php{$php_version_tag}-mysqlnd; because, php{$php_version_tag}-mysql may not work with mariadb \n";
# related error: mysqli_real_connect(): Headers and client library minor version mismatch. Headers:50538 Library:100010 in adodb
aptget("php{$php_version_tag}-mysqlnd",$usePrompts,True);
}
replacelineinfile("old_passwords","old_passwords=0","/etc/mysql/my.cnf"); # disable mysql old passwords... if enabled, vsftp auth cant work sometime.. changed 26.2.2008
# mariadb is new opensource drop-in replacement for mysql. hope this works with no problem.
check_restart_mysql();
}
function installPHPMYAdmin(){#by [email protected]
global $unattended, $usePrompts;
if($unattended){
# Answer automatic configuration questions
# http://gercogandia.blogspot.com/2012/11/automatic-unattended-install-of.html
passthru3("echo 'phpmyadmin phpmyadmin/dbconfig-install boolean true' | debconf-set-selections");
passthru3("echo 'phpmyadmin phpmyadmin/app-password-confirm password 1234' | debconf-set-selections");
passthru3("echo 'phpmyadmin phpmyadmin/mysql/admin-pass password 1234' | debconf-set-selections");
passthru3("echo 'phpmyadmin phpmyadmin/mysql/app-pass password 1234' | debconf-set-selections");
passthru3("echo 'phpmyadmin phpmyadmin/reconfigure-webserver multiselect apache2' | debconf-set-selections");
passthru3("echo 'phpmyadmin phpmyadmin/dbconfig-reinstall boolean true' | debconf-set-selections");
}
# Install PHPMyAdmin With Pre-Answered Prompts
aptget('phpmyadmin',$usePrompts);
}
function installRoundCube(){#by [email protected]
global $unattended, $usePrompts;
if($unattended){
# Answer automatic configuration questions
# http://gercogandia.blogspot.com/2012/11/automatic-unattended-install-of.html
passthru3("echo 'roundcube-core roundcube/password-confirm password 1234' | debconf-set-selections");
passthru3("echo 'roundcube-core roundcube/mysql/admin-pass password 1234' | debconf-set-selections");
passthru3("echo 'roundcube-core roundcube/mysql/app-pass password 1234' | debconf-set-selections");
passthru3("echo 'roundcube-core roundcube/app-password-confirm password 1234' | debconf-set-selections");
passthru3("echo 'roundcube-core roundcube/database-type select mysql' | debconf-set-selections");
passthru3("echo 'roundcube-core roundcube/dbconfig-install boolean true' | debconf-set-selections");
}
# Install Roundcube With Pre-Answered Prompts
aptget(['roundcube', 'roundcube-mysql','roundcube-plugins','roundcube-plugins-extra'],$usePrompts);
}
function installmailserver(){
global $app,$ehcpinstalldir,$ip,$hostname,$user_email,$user_name,$ehcpmysqlpass,$rootpass,$newrootpass,$ehcpadminpass,$installmode,$unattended;
echo "starting mail server installation (postfix and related programs)\n\n";
# If /etc/postfix/main.cf does not exist, it must exist before unattended install will work properly with PostFix
# See here:
# http://www.whatastruggle.com/postfix-non-interactive-install
# Added by Eric Arnol-Martin <[email protected]>
if($unattended) copyPostFixConfig();
# Install these packages and answer configuration questions if unattended
# Then install the rest of the packages
# Added by Eric Arnol-Martin <[email protected]>
# Place these functions wherever you want in your switch statement... they are currently here for testing
switch($installmode) {
case 'extra':
case 'normal': installRoundCube();installPHPMYAdmin();
case 'light':
aptget(['postfix','postfix-mysql'],False,True);