Marius Burkard
2016-07-01 49441bdd0f3ff75d5092d5b832b97ea722a66363
commit | author | age
e2d6ed 1 <?php
436ed8 2
e2d6ed 3 /*
436ed8 4 Copyright (c) 2007, Till Brehm, projektfarm Gmbh
e2d6ed 5 All rights reserved.
T 6
7 Redistribution and use in source and binary forms, with or without modification,
8 are permitted provided that the following conditions are met:
9
10     * Redistributions of source code must retain the above copyright notice,
11       this list of conditions and the following disclaimer.
12     * Redistributions in binary form must reproduce the above copyright notice,
13       this list of conditions and the following disclaimer in the documentation
14       and/or other materials provided with the distribution.
15     * Neither the name of ISPConfig nor the names of its contributors
16       may be used to endorse or promote products derived from this software without
17       specific prior written permission.
18
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
23 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
24 BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
26 OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 class system{
32
3b4c28 33     var $FILE = '/root/ispconfig/scripts/lib/classes/ispconfig_system.lib.php';
55da90 34     var $server_id;
B 35     var $server_conf;
36     var $data;
64ea56 37     var $min_uid = 500;
MC 38     var $min_gid = 500;
39     
55da90 40     /**
B 41      * Construct for this class
42      *
43      * @return system
44      */
7fe908 45
MC 46
f1926a 47     public function __construct(){
cea1e5 48         //global $go_info;
7fe908 49         //$this->server_id = $go_info['isp']['server_id'];
MC 50         //$this->server_conf = $go_info['isp']['server_conf'];
51         $this->server_conf['passwd_datei'] = '/etc/passwd';
52         $this->server_conf['shadow_datei'] = '/etc/shadow';
53         $this->server_conf['group_datei'] = '/etc/group';
55da90 54     }
7fe908 55
MC 56
57
55da90 58     /**
B 59      * Get the hostname from the server
60      *
61      * @return string
62      */
63     public function hostname(){
3b4c28 64         $dist = $this->server_conf['dist'];
7fe908 65
MC 66         ob_start();
67         passthru('hostname');
68         $hostname = ob_get_contents();
69         ob_end_clean();
55da90 70         $hostname = trim($hostname);
7fe908 71         ob_start();
MC 72         if(!strstr($dist, 'freebsd')){
73             passthru('dnsdomainname');
74         } else {
75             passthru('domainname');
76         }
77         $domainname = ob_get_contents();
78         ob_end_clean();
79         $domainname = trim($domainname);
80         if($domainname != ""){
81             if(!strstr($hostname, $domainname)) $hostname .= ".".$domainname;
82         }
83         return $hostname;
55da90 84     }
7fe908 85
MC 86
87
55da90 88     /**
B 89      * Add an user to the system
7fe908 90      *
55da90 91      */
B 92     public function adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
93         global $app;
7fe908 94         if($this->is_user($user_username)){
MC 95             return false;
96         } else {
97             if(trim($user_username) != '') {
98                 $user_datei = $this->server_conf['passwd_datei'];
99                 $shadow_datei = $this->server_conf['shadow_datei'];
100                 $shell = realpath($shell);
101                 if(trim($passwort) == '') $passwort = '*';
102                 $new_user = "\n$user_username:x:$uid:$gid:$username:$homedir:$shell\n";
103                 $app->log->msg('USER: '.$new_user);
104                 $app->file->af($user_datei, $new_user);
105                 if($shadow_datei == '/etc/shadow'){
106                     $datum = time();
107                     $tage = floor($datum/86400);
108                     $new_passwd = "\n$user_username:$passwort:$tage:0:99999:7:::\n";
109                 } else {
110                     $new_passwd = "\n$user_username:$passwort:$uid:$gid::0:0:$username:$homedir:$shell\n";
111                 }
112                 $app->file->af($shadow_datei, $new_passwd);
55da90 113                 // TB: leere Zeilen entfernen
7fe908 114                 $app->file->remove_blank_lines($shadow_datei);
MC 115                 $app->file->remove_blank_lines($user_datei);
116                 // TB: user Sortierung deaktiviert
117                 //$this->order_users_groups();
118                 if($shadow_datei != '/etc/shadow'){
119                     $app->file->af($shadow_datei, "\n");
55da90 120                     // TB: leere Zeilen entfernen
7fe908 121                     $app->file->remove_blank_lines($shadow_datei);
55da90 122                     $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
7fe908 123                 }
MC 124                 return true;
125             }
126         }
55da90 127     }
7fe908 128
MC 129
130
131
132
55da90 133     /**
B 134      * Update users when someone edit it
135      *
136      */
137     function updateuser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
138         //* First delete the users
139         $this->deluser($user_username);
140         //* Add the user again
7fe908 141         $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
55da90 142     }
7fe908 143
MC 144
145
146
147
55da90 148     /**
B 149      * Lock the user
150      *
151      */
152     function deactivateuser($user_username){
153         $passwort = str_rot13($this->getpasswd($user_username));
7fe908 154         $user_attr = $this->get_user_attributes($user_username);
MC 155         $uid = $user_attr['uid'];
156         $gid = $user_attr['gid'];
157         $username = $user_attr['name'];
158         $homedir = $user_attr['homedir'];
159         $shell = '/dev/null';
160         $this->deluser($user_username);
161         $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
55da90 162     }
7fe908 163
MC 164
55da90 165     /**
B 166      * Delete a user from the system
167      *
168      */
169     function deluser($user_username){
170         global $app;
7fe908 171         if($this->is_user($user_username)){
MC 172             $user_datei = $this->server_conf['passwd_datei'];
173             $shadow_datei = $this->server_conf['shadow_datei'];
174             $users = $app->file->rf($user_datei);
175             $lines = explode("\n", $users);
176             if(is_array($lines)){
177                 $num_lines = sizeof($lines);
178                 for($i=0;$i<$num_lines;$i++){
179                     if(trim($lines[$i]) != ''){
180                         list($f1, ) = explode(':', $lines[$i]);
181                         if($f1 != $user_username) $new_lines[] = $lines[$i];
182                     }
183                 }
184                 $new_users = implode("\n", $new_lines);
185                 $app->file->wf($user_datei, $new_users);
186                 unset($new_lines);
187                 unset($lines);
188                 unset($new_users);
189             }
190             $app->file->remove_blank_lines($user_datei);
191
192             $passwds = $app->file->rf($shadow_datei);
193             $lines = explode("\n", $passwds);
194             if(is_array($lines)){
195                 $num_lines = sizeof($lines);
196                 for($i=0;$i<$num_lines;$i++){
197                     if(trim($lines[$i]) != ''){
198                         list($f1, ) = explode(':', $lines[$i]);
199                         if($f1 != $user_username) $new_lines[] = $lines[$i];
200                     }
201                 }
202                 $new_passwds = implode("\n", $new_lines);
203                 $app->file->wf($shadow_datei, $new_passwds);
204                 unset($new_lines);
205                 unset($lines);
206                 unset($new_passwds);
207             }
208             $app->file->remove_blank_lines($shadow_datei);
209
210             $group_file = $app->file->rf($this->server_conf['group_datei']);
211             $group_file_lines = explode("\n", $group_file);
212             foreach($group_file_lines as $group_file_line){
213                 if(trim($group_file_line) != ''){
214                     list($f1, $f2, $f3, $f4) = explode(':', $group_file_line);
215                     $group_users = explode(',', str_replace(' ', '', $f4));
216                     if(in_array($user_username, $group_users)){
217                         $g_users = array();
218                         foreach($group_users as $group_user){
219                             if($group_user != $user_username) $g_users[] = $group_user;
220                         }
221                         $f4 = implode(',', $g_users);
222                     }
223                     $new_group_file[] = $f1.':'.$f2.':'.$f3.':'.$f4;
224                 }
225             }
226             $new_group_file = implode("\n", $new_group_file);
227             $app->file->wf($this->server_conf['group_datei'], $new_group_file);
228             // TB: auskommentiert
229             //$this->order_users_groups();
230
231             if($shadow_datei != '/etc/shadow'){
232                 $app->file->af($shadow_datei, "\n");
233                 $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
234             }
235             return true;
236         } else {
237             return false;
238         }
55da90 239     }
7fe908 240
MC 241
242
243
244
55da90 245     /**
B 246      * Add a usergroup to the system
247      *
248      */
249     function addgroup($group, $gid, $members = ''){
250         global $app;
7fe908 251         if($this->is_group($group)){
MC 252             return false;
253         } else {
254             $group_datei = $this->server_conf['group_datei'];
255             $shadow_datei = $this->server_conf['shadow_datei'];
256             $new_group = "\n$group:x:$gid:$members\n";
257             $app->file->af($group_datei, $new_group);
258
259             // TB: auskommentiert
260             //$this->order_users_groups();
261             if($shadow_datei != '/etc/shadow'){
262                 $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
263             }
264             return true;
265         }
55da90 266     }
7fe908 267
MC 268
269
270
271
55da90 272     /**
B 273      * Update usersgroup in way to delete and add it again
274      *
275      */
276     function updategroup($group, $gid, $members = ''){
277         $this->delgroup($group);
7fe908 278         $this->addgroup($group, $gid, $members);
55da90 279     }
7fe908 280
MC 281
282
283
284
55da90 285     /**
B 286      * Delete a usergroup from the system
287      *
288      */
289     function delgroup($group){
290         global $app;
7fe908 291         if($this->is_group($group)){
MC 292             $group_datei = $this->server_conf['group_datei'];
293             $shadow_datei = $this->server_conf['shadow_datei'];
294             $groups = $app->file->rf($group_datei);
295             $lines = explode("\n", $groups);
296             if(is_array($lines)){
297                 $num_lines = sizeof($lines);
298                 for($i=0;$i<$num_lines;$i++){
299                     if(trim($lines[$i]) != ''){
300                         list($f1, ) = explode(':', $lines[$i]);
301                         if($f1 != $group) $new_lines[] = $lines[$i];
302                     }
303                 }
304                 $new_groups = implode("\n", $new_lines);
305                 $app->file->wf($group_datei, $new_groups);
306                 unset($new_lines);
307                 unset($lines);
308                 unset($new_groups);
309             }
310             // TB: auskommentiert
311             //$this->order_users_groups();
312             if($shadow_datei != '/etc/shadow'){
313                 $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
314             }
315             return true;
316         } else {
317             return false;
318         }
55da90 319     }
7fe908 320
MC 321
55da90 322     /**
B 323      * Order usergroups
324      *
325      */
326     function order_users_groups(){
327         global $app;
7fe908 328         $user_datei = $this->server_conf['passwd_datei'];
MC 329         $shadow_datei = $this->server_conf['shadow_datei'];
330         $group_datei = $this->server_conf['group_datei'];
331
332         $groups = $app->file->no_comments($group_datei);
333         $lines = explode("\n", $groups);
334         if(is_array($lines)){
335             foreach($lines as $line){
336                 if(trim($line) != ''){
337                     list($f1, $f2, $f3, $f4) = explode(':', $line);
338                     $arr[$f3] = $line;
339                 }
340             }
341         }
342         ksort($arr);
343         reset($arr);
344         if($shadow_datei != '/etc/shadow'){
345             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0)."\n");
346         }else {
347             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
348         }
349         unset($arr);
350
351         $users = $app->file->no_comments($user_datei);
352         $lines = explode("\n", $users);
353         if(is_array($lines)){
354             foreach($lines as $line){
355                 if(trim($line) != ""){
356                     list($f1, $f2, $f3, ) = explode(':', $line);
357                     if($f1 != 'toor'){
358                         $arr[$f3] = $line;
359                     } else {
360                         $arr[70000] = $line;
361                     }
362                 }
363             }
364         }
365         ksort($arr);
366         reset($arr);
367         $app->file->wf($user_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
368         unset($arr);
369
370         $passwds = $app->file->no_comments($shadow_datei);
371         $lines = explode("\n", $passwds);
372         if(is_array($lines)){
373             foreach($lines as $line){
374                 if(trim($line) != ''){
375                     list($f1, $f2, $f3, ) = explode(':', $line);
376                     if($f1 != 'toor'){
377                         $uid = $this->getuid($f1);
378                         if(!is_bool($uid)) $arr[$uid] = $line;
379                     } else {
380                         $arr[70000] = $line;
381                     }
382                 }
383             }
384         }
385         ksort($arr);
386         reset($arr);
387         $app->file->wf($shadow_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
388         unset($arr);
55da90 389     }
7fe908 390
MC 391
392
393
394
55da90 395     /**
B 396      * Find a user / group id
397      *
398      */
399     function find_uid_gid($min, $max){
400         global $app;
7fe908 401         if($min < $max && $min >= 0 && $max >= 0 && $min <= 65536 && $max <= 65536 && is_int($min) && is_int($max)){
MC 402             for($i=$min;$i<=$max;$i++){
403                 $uid_arr[$i] = $gid_arr[$i] = 1;
404             }
405             $user_datei = $this->server_conf['passwd_datei'];
406             $group_datei = $this->server_conf['group_datei'];
407
408             $users = $app->file->no_comments($user_datei);
409             $lines = explode("\n", $users);
410             if(is_array($lines)){
411                 foreach($lines as $line){
412                     if(trim($line) != ''){
413                         list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
414                         if($f3 >= $min && $f3 <= $max) unset($uid_arr[$f3]);
415                     }
416                 }
417                 if(!empty($uid_arr)){
418                     foreach($uid_arr as $key => $val){
419                         $uids[] = $key;
420                     }
421                     $min_uid = min($uids);
422                     unset($uid_arr);
423                 } else {
424                     return false;
425                 }
426             }
427
428             $groups = $app->file->no_comments($group_datei);
429             $lines = explode("\n", $groups);
430             if(is_array($lines)){
431                 foreach($lines as $line){
432                     if(trim($line) != ''){
433                         list($f1, $f2, $f3, $f4) = explode(':', $line);
434                         if($f3 >= $min && $f3 <= $max) unset($gid_arr[$f3]);
435                     }
436                 }
437                 if(!empty($gid_arr)){
438                     foreach($gid_arr as $key => $val){
439                         $gids[] = $key;
440                     }
441                     $min_gid = min($gids);
442                     unset($gid_arr);
443                 } else {
444                     return false;
445                 }
446             }
447
448             $result = array_intersect($uids, $gids);
449             $new_id = (max($result));
450             unset($uids);
451             unset($gids);
452             unset($result);
453             if($new_id <= $max){
454                 return $new_id;
455             } else {
456                 return false;
457             }
458         } else {
459             return false;
460         }
55da90 461     }
7fe908 462
MC 463
464
465
466
55da90 467     /**
B 468      * Check if the users is really a user into the system
469      *
470      */
471     function is_user($user){
472         global $app;
7fe908 473         $user_datei = $this->server_conf['passwd_datei'];
MC 474         $users = $app->file->no_comments($user_datei);
475         $lines = explode("\n", $users);
476         if(is_array($lines)){
55da90 477             foreach($lines as $line){
7fe908 478                 if(trim($line) != ''){
MC 479                     list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
480                     if($f1 == $user) return true;
481                 }
482             }
483         }
484         return false;
55da90 485     }
7fe908 486
MC 487
488
489
490
55da90 491     /**
B 492      * Check if the group is on this system
493      *
494      */
495     function is_group($group){
496         global $app;
7fe908 497         $group_datei = $this->server_conf['group_datei'];
MC 498         $groups = $app->file->no_comments($group_datei);
499         $lines = explode("\n", $groups);
500         if(is_array($lines)){
501             foreach($lines as $line){
502                 if(trim($line) != ""){
503                     list($f1, $f2, $f3, $f4) = explode(':', $line);
504                     if($f1 == $group) return true;
505                 }
506             }
507         }
508         return false;
55da90 509     }
7fe908 510
89a57f 511     /*
T 512     // Alternative implementation of the is_group function. Should be faster then the old one To be tested.
513     function is_group($group) {
514     $groupfile = '/etc/group';
515     if(is_file($groupfile)) {
516         $handle = fopen ($groupfile, "r");
517         while (!feof($handle)) {
518             $line = trim(fgets($handle, 4096));
519             if($line != ""){
520                 $parts = explode(":", $line);
521                 if($parts[0] == $group) {
522                     fclose ($handle);
523                     return true;
524                 }
525             }
526         }
527         fclose ($handle);
528     }
529     return false;
530     }
531     */
7fe908 532
55da90 533     function root_group(){
B 534         global $app;
7fe908 535         $group_datei = $this->server_conf['group_datei'];
MC 536         $groups = $app->file->no_comments($group_datei);
537         $lines = explode("\n", $groups);
538         if(is_array($lines)){
539             foreach($lines as $line){
540                 if(trim($line) != ''){
541                     list($f1, $f2, $f3, $f4) = explode(':', $line);
542                     if($f3 == 0) return $f1;
543                 }
544             }
545         }
546         return false;
55da90 547     }
7fe908 548
MC 549
550
551
552
55da90 553     /**
B 554      * Get the groups of an user
555      *
556      */
557     function get_user_groups($username){
558         global $app;
7fe908 559         $user_groups = array();
MC 560         $group_datei = $this->server_conf['group_datei'];
561         $groups = $app->file->no_comments($group_datei);
562         $lines = explode("\n", $groups);
563         if(is_array($lines)){
564             foreach($lines as $line){
565                 if(trim($line) != ''){
566                     list($f1, $f2, $f3, $f4) = explode(':', $line);
567                     if(intval($f3) < intval($this->server_conf['groupid_von']) && trim($f1) != 'users'){
568                         $tmp_group_users = explode(',', str_replace(' ', '', $f4));
569                         if(in_array($username, $tmp_group_users) && trim($f1) != '') $user_groups[] = $f1;
570                         unset($tmp_group_users);
571                     }
572                 }
573             }
574         }
575         if(!empty($user_groups)) return implode(',', $user_groups);
576         return '';
55da90 577     }
7fe908 578
MC 579
580
581
582
55da90 583     /**
B 584      * Get a user password
585      *
586      */
587     function getpasswd($user){
588         global $app;
7fe908 589         if($this->is_user($user)){
MC 590             $shadow_datei = $this->server_conf['shadow_datei'];
591             $passwds = $app->file->no_comments($shadow_datei);
592             $lines = explode("\n", $passwds);
593             if(is_array($lines)){
594                 foreach($lines as $line){
595                     if(trim($line) != ''){
596                         list($f1, $f2, ) = explode(':', $line);
597                         if($f1 == $user) return $f2;
598                     }
599                 }
600             }
601         } else {
602             return false;
603         }
55da90 604     }
7fe908 605
MC 606
607
608
609
55da90 610     /**
0e2978 611      * Get the user from an user id
DM 612      *
613      */
614     function getuser($uid){
615         global $app;
616         $user_datei = $this->server_conf['passwd_datei'];
617         $users = $app->file->no_comments($user_datei);
618         $lines = explode("\n", $users);
619         if(is_array($lines)){
620             foreach($lines as $line){
621                 if(trim($line) != ''){
622                     list($f1, $f2, $f3,) = explode(':', $line);
623                     if($f3 == $uid) return $f1;
624                 }
625             }
626         }
627         return false;
628     }
629
630
631
632
633
634     /**
55da90 635      * Get the user id from an user
B 636      *
637      */
638     function getuid($user){
639         global $app;
7fe908 640         if($this->is_user($user)){
MC 641             $user_datei = $this->server_conf['passwd_datei'];
642             $users = $app->file->no_comments($user_datei);
643             $lines = explode("\n", $users);
644             if(is_array($lines)){
645                 foreach($lines as $line){
646                     if(trim($line) != ''){
647                         list($f1, $f2, $f3, ) = explode(':', $line);
648                         if($f1 == $user) return $f3;
649                     }
650                 }
651             }
652         } else {
653             return false;
654         }
0e2978 655     }
DM 656
657
658
659
660
661     /**
662      * Get the group from a group id
663      *
664      */
665     function getgroup($gid){
666         global $app;
667         $group_datei = $this->server_conf['group_datei'];
668         $groups = $app->file->no_comments($group_datei);
669         $lines = explode("\n", $groups);
670         if(is_array($lines)){
671             foreach($lines as $line){
672                 if(trim($line) != ""){
673                     list($f1, $f2, $f3, $f4) = explode(':', $line);
674                     if($f3 == $gid) return $f1;
675                 }
676             }
677         }
678         return false;
55da90 679     }
7fe908 680
MC 681
682
683
684
55da90 685     /**
3f478f 686      * Get the group id from an group
T 687      *
688      */
689     function getgid($group){
690         global $app;
7fe908 691         if($this->is_group($group)){
MC 692             $group_datei = $this->server_conf['group_datei'];
3f478f 693             $groups = $app->file->no_comments($group_datei);
T 694             $lines = explode("\n", $groups);
695             if(is_array($lines)){
7fe908 696                 foreach($lines as $line){
3f478f 697                     if(trim($line) != ""){
T 698                         list($f1, $f2, $f3, $f4) = explode(':', $line);
699                         if($f1 == $group) return $f3;
700                     }
701                 }
702             }
7fe908 703         } else {
MC 704             return false;
705         }
3f478f 706     }
7fe908 707
MC 708
709
710
711
3f478f 712     /**
7fe908 713      * Return info about a group by name
MC 714      *
715      */
3f478f 716     function posix_getgrnam($group) {
T 717         if(!function_exists('posix_getgrnam')){
718             $group_datei = $this->server_conf['group_datei'];
719             $cmd = 'grep -m 1 "^'.$group.':" '.$group_datei;
720             exec($cmd, $output, $return_var);
721             if($return_var != 0 || !$output[0]) return false;
722             list($f1, $f2, $f3, $f4) = explode(':', $output[0]);
723             $f2 = trim($f2);
724             $f3 = trim($f3);
725             $f4 = trim($f4);
726             if($f4 != ''){
727                 $members = explode(',', $f4);
728             } else {
729                 $members = array();
730             }
7fe908 731             $group_details = array( 'name' => $group,
MC 732                 'passwd' => $f2,
733                 'members' => $members,
734                 'gid' => $f3);
735             return $group_details;
3f478f 736         } else {
T 737             return posix_getgrnam($group);
738         }
7fe908 739     }
MC 740
741
742
743
744
3f478f 745     /**
55da90 746      * Get all information from a user
B 747      *
748      */
749     function get_user_attributes($user){
750         global $app;
7fe908 751         if($this->is_user($user)){
MC 752             $user_datei = $this->server_conf['passwd_datei'];
753             $users = $app->file->no_comments($user_datei);
754             $lines = explode("\n", $users);
755             if(is_array($lines)){
756                 foreach($lines as $line){
757                     if(trim($line) != ''){
758                         list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(':', $line);
759                         if($f1 == $user){
760                             $user_attr['username'] = $f1;
761                             $user_attr['x'] = $f2;
762                             $user_attr['uid'] = $f3;
763                             $user_attr['gid'] = $f4;
764                             $user_attr['name'] = $f5;
765                             $user_attr['homedir'] = $f6;
766                             $user_attr['shell'] = $f7;
767                             return $user_attr;
768                         }
769                     }
770                 }
771             }
772         } else {
773             return false;
774         }
55da90 775     }
7fe908 776
MC 777
778
779
780
55da90 781     /**
B 782      * Edit the owner of a file
783      *
784      */
c3ac0a 785     function chown($file, $owner, $allow_symlink = false){
7fe908 786         global $app;
MC 787         if($allow_symlink == false && $this->checkpath($file) == false) {
788             $app->log("Action aborted, file is a symlink: $file", LOGLEVEL_WARN);
d87f76 789             return false;
T 790         }
7fe908 791         if(file_exists($file)) {
MC 792             if(@chown($file, $owner)) {
793                 return true;
794             } else {
795                 $app->log("chown failed: $file : $owner", LOGLEVEL_DEBUG);
796                 return false;
797             }
798         }
c3ac0a 799     }
7fe908 800
c3ac0a 801     function chgrp($file, $group = '', $allow_symlink = false){
7fe908 802         global $app;
MC 803         if($allow_symlink == false && $this->checkpath($file) == false) {
804             $app->log("Action aborted, file is a symlink: $file", LOGLEVEL_WARN);
d87f76 805             return false;
T 806         }
7fe908 807         if(file_exists($file)) {
MC 808             if(@chgrp($file, $group)) {
809                 return true;
810             } else {
811                 $app->log("chgrp failed: $file : $group", LOGLEVEL_DEBUG);
812                 return false;
813             }
814         }
c3ac0a 815     }
7fe908 816
c3ac0a 817     //* Change the mode of a file
T 818     function chmod($file, $mode, $allow_symlink = false) {
c77103 819         global $app;
c3ac0a 820         if($allow_symlink == false && $this->checkpath($file) == false) {
7fe908 821             $app->log("Action aborted, file is a symlink: $file", LOGLEVEL_WARN);
c3ac0a 822             return false;
T 823         }
d87f76 824         if(@chmod($file, $mode)) {
T 825             return true;
826         } else {
7fe908 827             $app->log("chmod failed: $file : $mode", LOGLEVEL_DEBUG);
d87f76 828             return false;
T 829         }
c3ac0a 830     }
7fe908 831
c3ac0a 832     function file_put_contents($filename, $data, $allow_symlink = false) {
c77103 833         global $app;
c3ac0a 834         if($allow_symlink == false && $this->checkpath($filename) == false) {
7fe908 835             $app->log("Action aborted, file is a symlink: $filename", LOGLEVEL_WARN);
c3ac0a 836             return false;
T 837         }
c77103 838         if(file_exists($filename)) unlink($filename);
c3ac0a 839         return file_put_contents($filename, $data);
T 840     }
7fe908 841
c3ac0a 842     function file_get_contents($filename, $allow_symlink = false) {
c77103 843         global $app;
c3ac0a 844         if($allow_symlink == false && $this->checkpath($filename) == false) {
7fe908 845             $app->log("Action aborted, file is a symlink: $filename", LOGLEVEL_WARN);
c3ac0a 846             return false;
T 847         }
b889ed 848         return file_get_contents($filename, $data);
c3ac0a 849     }
7fe908 850
c3ac0a 851     function rename($filename, $new_filename, $allow_symlink = false) {
c77103 852         global $app;
c3ac0a 853         if($allow_symlink == false && $this->checkpath($filename) == false) {
7fe908 854             $app->log("Action aborted, file is a symlink: $filename", LOGLEVEL_WARN);
c3ac0a 855             return false;
T 856         }
857         return rename($filename, $new_filename);
858     }
7fe908 859
c3ac0a 860     function mkdir($dirname, $allow_symlink = false) {
c77103 861         global $app;
c3ac0a 862         if($allow_symlink == false && $this->checkpath($dirname) == false) {
7fe908 863             $app->log("Action aborted, file is a symlink: $dirname", LOGLEVEL_WARN);
c3ac0a 864             return false;
T 865         }
d87f76 866         if(@mkdir($dirname)) {
T 867             return true;
868         } else {
7fe908 869             $app->log("mkdir failed: $dirname", LOGLEVEL_DEBUG);
d87f76 870             return false;
T 871         }
c3ac0a 872     }
7fe908 873
58053e 874     function unlink($filename) {
b889ed 875         if(file_exists($filename) || is_link($filename)) {
c77103 876             return unlink($filename);
T 877         }
c3ac0a 878     }
7fe908 879
MC 880     function copy($file1, $file2) {
881         return copy($file1, $file2);
c3ac0a 882     }
7fe908 883
4bd960 884     function touch($file, $allow_symlink = false){
7fe908 885         global $app;
MC 886         if($allow_symlink == false && @file_exists($file) && $this->checkpath($file) == false) {
887             $this->unlink($file);
888         }
889         if(@touch($file)) {
4bd960 890             return true;
7fe908 891         } else {
MC 892             $app->log("touch failed: $file", LOGLEVEL_DEBUG);
4bd960 893             return false;
7fe908 894         }
4bd960 895     }
7fe908 896
c3ac0a 897     function checkpath($path) {
T 898         $path = trim($path);
899         //* We allow only absolute paths
7fe908 900         if(substr($path, 0, 1) != '/') return false;
MC 901
c3ac0a 902         //* We allow only some characters in the path
992797 903         // * is allowed, for example it is part of wildcard certificates/keys: *.example.com.crt
c6e989 904         if(!preg_match('@^/[-a-zA-Z0-9_/.*]{1,}[~]?$@', $path)) return false;
7fe908 905
c3ac0a 906         //* Check path for symlinks
7fe908 907         $path_parts = explode('/', $path);
c3ac0a 908         $testpath = '';
T 909         foreach($path_parts as $p) {
910             $testpath .= '/'.$p;
911             if(is_link($testpath)) return false;
912         }
7fe908 913
c3ac0a 914         return true;
55da90 915     }
7fe908 916
MC 917
6ff8d7 918     /**
FS 919      * This function checks the free space for a given directory
920      * @param path check path
921      * @param limit min. free space in bytes
852a25 922      * @return bool - true when the the free space is above limit ohterwise false, opt. available disk-space
6ff8d7 923     */
FS 924
852a25 925     function check_free_space($path, $limit = 0, &$free_space = 0) {
6ff8d7 926         $path = rtrim($path, '/');
FS 927
928         /**
929         * Make sure that we have only existing directories in the path.
930
931         * Given a file name instead of a directory, the behaviour of the disk_free_space
932         function is unspecified and may differ between operating systems and PHP versions.
933         */
852a25 934         while(!is_dir($path) && $path != '/') $path = realpath(dirname($path));
FS 935
936         $free_space = disk_free_space($out);
937
938         if (!$free_space) {
939             $free_space = 0;
940             return false;
6ff8d7 941         }
FS 942
852a25 943         if ($free_space >= $limit) {
6ff8d7 944             return true;
FS 945         } else {
946             return false;
947         }
948
949     }
7fe908 950
MC 951
952
55da90 953     /**
B 954      * Add an user to a specific group
955      *
956      */
957     function add_user_to_group($group, $user = 'admispconfig'){
958         global $app;
7fe908 959         $group_file = $app->file->rf($this->server_conf['group_datei']);
MC 960         $group_file_lines = explode("\n", $group_file);
961         foreach($group_file_lines as $group_file_line){
962             list($group_name, $group_x, $group_id, $group_users) = explode(':', $group_file_line);
963             if($group_name == $group){
964                 $group_users = explode(',', str_replace(' ', '', $group_users));
965                 if(!in_array($user, $group_users)){
966                     $group_users[] = $user;
967                 }
968                 $group_users = implode(',', $group_users);
969                 if(substr($group_users, 0, 1) == ',') $group_users = substr($group_users, 1);
970                 $group_file_line = $group_name.':'.$group_x.':'.$group_id.':'.$group_users;
971             }
972             $new_group_file[] = $group_file_line;
973         }
974         $new_group_file = implode("\n", $new_group_file);
975         $app->file->wf($this->server_conf['group_datei'], $new_group_file);
976         $app->file->remove_blank_lines($this->server_conf['group_datei']);
977         if($this->server_conf['shadow_datei'] != '/etc/shadow'){
978             $app->log->caselog('pwd_mkdb '.$this->server_conf['shadow_datei'].' &> /dev/null', $this->FILE, __LINE__);
979         }
55da90 980     }
7fe908 981
ff6a68 982     /*
55da90 983     function usermod($user, $groups){
B 984         global $app;
985           if($this->is_user($user)){
3b4c28 986             $groups = explode(',', str_replace(' ', '', $groups));
J 987             $group_file = $app->file->rf($this->server_conf['group_datei']);
55da90 988             $group_file_lines = explode("\n", $group_file);
B 989             foreach($group_file_lines as $group_file_line){
990                   if(trim($group_file_line) != ""){
3b4c28 991                     list($f1, $f2, $f3, $f4) = explode(':', $group_file_line);
J 992                     $group_users = explode(',', str_replace(' ', '', $f4));
55da90 993                     if(!in_array($f1, $groups)){
B 994                           if(in_array($user, $group_users)){
995                             $g_users = array();
996                             foreach($group_users as $group_user){
997                                   if($group_user != $user) $g_users[] = $group_user;
998                             }
3b4c28 999                             $f4 = implode(',', $g_users);
55da90 1000                           }
B 1001                     } else {
1002                           if(!in_array($user, $group_users)){
3b4c28 1003                             if(trim($group_users[0]) == '') unset($group_users);
55da90 1004                             $group_users[] = $user;
B 1005                           }
3b4c28 1006                           $f4 = implode(',', $group_users);
55da90 1007                     }
3b4c28 1008                     $new_group_file[] = $f1.':'.$f2.':'.$f3.':'.$f4;
55da90 1009                   }
B 1010             }
1011             $new_group_file = implode("\n", $new_group_file);
3b4c28 1012             $app->file->wf($this->server_conf['group_datei'], $new_group_file);
J 1013             $app->file->remove_blank_lines($this->server_conf['group_datei']);
1014             if($this->server_conf['shadow_datei'] != '/etc/shadow'){
1015                   $app->log->caselog('pwd_mkdb '.$this->server_conf['shadow_datei'].' &> /dev/null', $this->FILE, __LINE__);
55da90 1016             }
B 1017             return true;
1018           } else {
1019             return false;
1020           }
1021     }
ff6a68 1022     */
7fe908 1023
55da90 1024     /**boot autostart etc
B 1025      *
1026      */
1027     function rc_edit($service, $rl, $action){
1028         // $action = "on|off";
7fe908 1029         global $app;
MC 1030         $dist_init_scripts = $app->system->server_conf['dist_init_scripts'];
1031         $dist_runlevel = $app->system->server_conf['dist_runlevel'];
1032         $dist = $app->system->server_conf['dist'];
1033         if(trim($dist_runlevel) == ''){ // falls es keine runlevel gibt (FreeBSD)
1034             if($action == 'on'){
1035                 @symlink($dist_init_scripts.'/'.$service, $dist_init_scripts.'/'.$service.'.sh');
1036             }
1037             if($action == 'off'){
1038                 if(is_link($dist_init_scripts.'/'.$service.'.sh')){
1039                     unlink($dist_init_scripts.'/'.$service.'.sh');
1040                 } else {
1041                     rename($dist_init_scripts.'/'.$service.'.sh', $dist_init_scripts.'/'.$service);
1042                 }
1043             }
1044         } else { // Linux
1045             if(substr($dist, 0, 4) == 'suse'){
1046                 if($action == 'on'){
1047                     exec("chkconfig --add $service &> /dev/null");
1048                 }
1049                 if($action == 'off'){
1050                     exec("chkconfig --del $service &> /dev/null");
1051                 }
1052             } else {
1053                 $runlevels = explode(',', $rl);
1054                 foreach($runlevels as $runlevel){
1055                     $runlevel = trim($runlevel);
1056                     if($runlevel != '' && is_dir($dist_runlevel.'/rc'.$runlevel.'.d')){
1057                         $handle=opendir($dist_runlevel.'/rc'.$runlevel.'.d');
1058                         while($file = readdir($handle)){
1059                             if($file != '.' && $file != '..'){
1060                                 $target = @readlink($dist_runlevel.'/rc'.$runlevel.'.d/'.$file);
1061                                 if(strstr($file, $service) && strstr($target, $service) && substr($file, 0, 1) == 'S') $ln_arr[$runlevel][] = $dist_runlevel.'/rc'.$runlevel.'.d/'.$file;
1062                             }
1063                         }
1064                         closedir($handle);
1065                     }
1066                     if($action == 'on'){
1067                         if(!is_array($ln_arr[$runlevel])) @symlink($dist_init_scripts.'/'.$service, $dist_runlevel.'/rc'.$runlevel.'.d/S99'.$service);
1068                     }
1069                     if($action == 'off'){
1070                         if(is_array($ln_arr[$runlevel])){
1071                             foreach($ln_arr[$runlevel] as $link){
1072                                 unlink($link);
1073                             }
1074                         }
1075                     }
1076                 }
1077             }
1078         }
55da90 1079     }
7fe908 1080
MC 1081
1082
1083
1084
55da90 1085     /**
B 1086      * Filter information from the commands
1087      *
1088      */
1089     function grep($content, $string, $params = ''){
1090         global $app;
7fe908 1091         // params: i, v, w
MC 1092         $content = $app->file->unix_nl($content);
1093         $lines = explode("\n", $content);
1094         foreach($lines as $line){
1095             if(!strstr($params, 'w')){
1096                 if(strstr($params, 'i')){
1097                     if(strstr($params, 'v')){
1098                         if(!stristr($line, $string)) $find[] = $line;
1099                     } else {
1100                         if(stristr($line, $string)) $find[] = $line;
1101                     }
1102                 } else {
1103                     if(strstr($params, 'v')){
1104                         if(!strstr($line, $string)) $find[] = $line;
1105                     } else {
1106                         if(strstr($line, $string)) $find[] = $line;
1107                     }
1108                 }
1109             } else {
1110                 if(strstr($params, 'i')){
1111                     if(strstr($params, 'v')){
1112                         if(!$app->string->is_word($string, $line, 'i')) $find[] = $line;
1113                     } else {
1114                         if($app->string->is_word($string, $line, 'i')) $find[] = $line;
1115                     }
1116                 } else {
1117                     if(strstr($params, 'v')){
1118                         if(!$app->string->is_word($string, $line)) $find[] = $line;
1119                     } else {
1120                         if($app->string->is_word($string, $line)) $find[] = $line;
1121                     }
1122                 }
1123             }
1124         }
1125         if(is_array($find)){
1126             $ret_val = implode("\n", $find);
1127             if(substr($ret_val, -1) != "\n") $ret_val .= "\n";
1128             $find = NULL;
1129             return $ret_val;
1130         } else {
1131             return false;
1132         }
55da90 1133     }
7fe908 1134
MC 1135
1136
1137
1138
55da90 1139     /**
B 1140      * Strip content from fields
1141      *
1142      */
1143     function cut($content, $field, $delimiter = ':'){
1144         global $app;
7fe908 1145         $content = $app->file->unix_nl($content);
MC 1146         $lines = explode("\n", $content);
1147         foreach($lines as $line){
1148             $elms = explode($delimiter, $line);
1149             $find[] = $elms[($field-1)];
1150         }
1151         if(is_array($find)){
1152             $ret_val = implode("\n", $find);
1153             if(substr($ret_val, -1) != "\n") $ret_val .= "\n";
1154             $find = NULL;
1155             return $ret_val;
1156         } else {
1157             return false;
1158         }
55da90 1159     }
7fe908 1160
MC 1161
1162
1163
1164
55da90 1165     /**
B 1166      * Get the content off a file
1167      *
1168      */
1169     function cat($file){
1170         global $app;
7fe908 1171         return $app->file->rf($file);
55da90 1172     }
7fe908 1173
MC 1174
1175
1176
1177
55da90 1178     /**
B 1179      * Control services to restart etc
1180      *
1181      */
1182     function daemon_init($daemon, $action){
1183         //* $action = start|stop|restart|reload
7fe908 1184         global $app;
MC 1185         $dist = $this->server_conf['dist'];
1186         $dist_init_scripts = $this->server_conf['dist_init_scripts'];
1187         if(!strstr($dist, 'freebsd')){
1188             $app->log->caselog("$dist_init_scripts/$daemon $action &> /dev/null", $this->FILE, __LINE__);
1189         } else {
1190             if(is_file($dist_init_scripts.'/'.$daemon.'.sh') || is_link($dist_init_scripts.'/'.$daemon.'.sh')){
1191                 if($action == 'start' || $action == 'stop'){
1192                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh '.$action.' &> /dev/null', $this->FILE, __LINE__);
1193                 } else {
1194                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh stop &> /dev/null', $this->FILE, __LINE__);
1195                     sleep(3);
1196                     $app->log->caselog($dist_init_scripts.'/'.$daemon.'.sh start &> /dev/null', $this->FILE, __LINE__);
1197                 }
1198             } else {
1199                 if(is_file($dist_init_scripts.'/'.$daemon) || is_link($dist_init_scripts.'/'.$daemon)){
1200                     if($action == 'start' || $action == 'stop'){
1201                         $app->log->caselog($dist_init_scripts.'/'.$daemon.' '.$action.' &> /dev/null', $this->FILE, __LINE__);
1202                     } else {
1203                         $app->log->caselog($dist_init_scripts.'/'.$daemon.' stop &> /dev/null', $this->FILE, __LINE__);
1204                         sleep(3);
1205                         $app->log->caselog($dist_init_scripts.'/'.$daemon.' start &> /dev/null', $this->FILE, __LINE__);
1206                     }
1207                 } else {
1208                     if(is_file('/etc/rc.d/'.$daemon) || is_link('/etc/rc.d/'.$daemon)){
1209                         if($action == 'start' || $action == 'stop'){
1210                             $app->log->caselog('/etc/rc.d/'.$daemon.' '.$action.' &> /dev/null', $this->FILE, __LINE__);
1211                         } else {
1212                             $app->log->caselog('/etc/rc.d/'.$daemon.' stop &> /dev/null', $this->FILE, __LINE__);
1213                             sleep(3);
1214                             $app->log->caselog('/etc/rc.d/'.$daemon.' start &> /dev/null', $this->FILE, __LINE__);
1215                         }
1216                     }
1217                 }
1218             }
1219         }
55da90 1220     }
7fe908 1221
55da90 1222     function netmask($netmask){
7fe908 1223         list($f1, $f2, $f3, $f4) = explode('.', trim($netmask));
MC 1224         $bin = str_pad(decbin($f1), 8, '0', STR_PAD_LEFT).str_pad(decbin($f2), 8, '0', STR_PAD_LEFT).str_pad(decbin($f3), 8, '0', STR_PAD_LEFT).str_pad(decbin($f4), 8, '0', STR_PAD_LEFT);
1225         $parts = explode('0', $bin);
1226         $bin = str_pad($parts[0], 32, '0', STR_PAD_RIGHT);
1227         $bin = wordwrap($bin, 8, '.', 1);
1228         list($f1, $f2, $f3, $f4) = explode('.', trim($bin));
1229         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1230     }
7fe908 1231
55da90 1232     function binary_netmask($netmask){
7fe908 1233         list($f1, $f2, $f3, $f4) = explode('.', trim($netmask));
MC 1234         $bin = str_pad(decbin($f1), 8, '0', STR_PAD_LEFT).str_pad(decbin($f2), 8, '0', STR_PAD_LEFT).str_pad(decbin($f3), 8, '0', STR_PAD_LEFT).str_pad(decbin($f4), 8, '0', STR_PAD_LEFT);
1235         $parts = explode('0', $bin);
1236         return substr_count($parts[0], '1');
55da90 1237     }
7fe908 1238
55da90 1239     function network($ip, $netmask){
7fe908 1240         $netmask = $this->netmask($netmask);
MC 1241         list($f1, $f2, $f3, $f4) = explode('.', $netmask);
1242         $netmask_bin = str_pad(decbin($f1), 8, '0', STR_PAD_LEFT).str_pad(decbin($f2), 8, '0', STR_PAD_LEFT).str_pad(decbin($f3), 8, '0', STR_PAD_LEFT).str_pad(decbin($f4), 8, '0', STR_PAD_LEFT);
1243         list($f1, $f2, $f3, $f4) = explode('.', $ip);
1244         $ip_bin = str_pad(decbin($f1), 8, '0', STR_PAD_LEFT).str_pad(decbin($f2), 8, '0', STR_PAD_LEFT).str_pad(decbin($f3), 8, '0', STR_PAD_LEFT).str_pad(decbin($f4), 8, '0', STR_PAD_LEFT);
1245         for($i=0;$i<32;$i++){
1246             $network_bin .= substr($netmask_bin, $i, 1) * substr($ip_bin, $i, 1);
1247         }
1248         $network_bin = wordwrap($network_bin, 8, '.', 1);
1249         list($f1, $f2, $f3, $f4) = explode('.', trim($network_bin));
1250         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1251     }
7fe908 1252
MC 1253
1254
1255
1256
55da90 1257     /**
B 1258      * Make a broadcast address from an IP number in combination with netmask
1259      *
1260      */
1261     function broadcast($ip, $netmask){
1262         $netmask = $this->netmask($netmask);
7fe908 1263         $binary_netmask = $this->binary_netmask($netmask);
MC 1264         list($f1, $f2, $f3, $f4) = explode('.', $ip);
1265         $ip_bin = str_pad(decbin($f1), 8, '0', STR_PAD_LEFT).str_pad(decbin($f2), 8, '0', STR_PAD_LEFT).str_pad(decbin($f3), 8, '0', STR_PAD_LEFT).str_pad(decbin($f4), 8, '0', STR_PAD_LEFT);
1266         $broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask), 32, '1', STR_PAD_RIGHT);
1267         $broadcast_bin = wordwrap($broadcast_bin, 8, '.', 1);
1268         list($f1, $f2, $f3, $f4) = explode('.', trim($broadcast_bin));
1269         return bindec($f1).'.'.bindec($f2).'.'.bindec($f3).'.'.bindec($f4);
55da90 1270     }
7fe908 1271
MC 1272
1273
1274
1275
55da90 1276     /**
B 1277      * Get the network address information
1278      *
1279      */
1280     function network_info(){
458767 1281         $dist = $this->server_conf['dist'];
7fe908 1282         ob_start();
MC 1283         passthru('ifconfig');
1284         $output = ob_get_contents();
1285         ob_end_clean();
1286         $lines = explode("\n", $output);
1287         foreach($lines as $line){
1288             $elms = explode(' ', $line);
1289             if(trim($elms[0]) != '' && substr($elms[0], 0, 1) != "\t"){
1290                 $elms[0] = trim($elms[0]);
1291                 if(strstr($dist, 'freebsd')) $elms[0] = substr($elms[0], 0, -1);
1292                 $interfaces[] = $elms[0];
1293             }
1294         }
1295         if(!empty($interfaces)){
1296             foreach($interfaces as $interface){
1297                 ob_start();
1298                 if(!strstr($dist, 'freebsd')){
1299                     passthru('ifconfig '.$interface." | grep -iw 'inet' | cut -f2 -d: | cut -f1 -d' '");
1300                 } else {
1301                     passthru('ifconfig '.$interface." | grep -iw 'inet' | grep -iv 'inet6' | cut -f2 -d' '");
1302                 }
1303                 $output = trim(ob_get_contents());
1304                 ob_end_clean();
1305                 if($output != ''){
1306                     $ifconfig['INTERFACE'][$interface] = $output;
1307                     $ifconfig['IP'][$output] = $interface;
1308                 }
1309             }
1310             if(!empty($ifconfig)){
1311                 return $ifconfig;
1312             } else {
1313                 return false;
1314             }
1315         } else {
1316             return false;
1317         }
55da90 1318     }
7fe908 1319
MC 1320
1321
1322
1323
55da90 1324     /**
B 1325      * Configure the network settings from the system
1326      *
1327      */
1328     function network_config(){
1329         $ifconfig = $this->network_info();
7fe908 1330         if($ifconfig){
MC 1331             $main_interface = $ifconfig['IP'][$this->server_conf['server_ip']];
1332             if(strstr($main_interface, ':')){
1333                 $parts = explode(':', $main_interface);
1334                 $main_interface = trim($parts[0]);
1335             }
1336             if($main_interface != ''){
1337                 $ips = $this->data['isp_server_ip'];
1338                 if(!empty($ips)){
1339                     foreach($ips as $ip){
1340                         if(!isset($ifconfig['IP'][$ip['server_ip']])){
1341                             $to_set[] = $ip['server_ip'];
1342                         } else {
1343                             unset($ifconfig['IP'][$ip['server_ip']]);
1344                         }
1345                     }
1346                     if(!empty($ifconfig['IP'])){
1347                         foreach($ifconfig['IP'] as $key => $val){
1348                             if(!strstr($val, 'lo') && !strstr($val, 'lp') && strstr($val, $main_interface)){
1349                                 exec('ifconfig '.$val.' down &> /dev/null');
1350                                 unset($ifconfig['INTERFACE'][$val]);
1351                             }
1352                         }
1353                     }
1354                     if(!empty($to_set)){
1355                         foreach($to_set as $to){
1356                             $i = 0;
1357                             while($i >= 0){
1358                                 if(isset($ifconfig['INTERFACE'][$main_interface.':'.$i])){
1359                                     $i++;
1360                                 } else {
1361                                     $new_interface = $main_interface.':'.$i;
1362                                     $i = -1;
1363                                 }
1364                             }
1365                             exec('ifconfig '.$new_interface.' '.$to.' netmask '.$this->server_conf['server_netzmaske'].' up &> /dev/null');
1366                             $ifconfig['INTERFACE'][$new_interface] = $to;
1367                         }
1368                     }
1369                 }
1370             }
1371         }
55da90 1372     }
7fe908 1373
55da90 1374     function quota_dirs(){
7fe908 1375         global $app;
MC 1376         $content = $app->file->unix_nl($app->file->no_comments('/etc/fstab'));
1377         $lines = explode("\n", $content);
1378         foreach($lines as $line){
1379             $line = trim($line);
1380             if($line != ''){
1381                 $elms = explode("\t", $line);
1382                 foreach($elms as $elm){
1383                     if(trim($elm) != '') $f[] = $elm;
1384                 }
1385                 if(!empty($f) && stristr($f[3], 'userquota') && stristr($f[3], 'groupquota')){
1386                     $q_dirs[] = trim($f[1]);
1387                 }
1388                 unset($f);
1389             }
1390         }
1391         if(!empty($q_dirs)){
1392             return $q_dirs;
1393         } else {
1394             return false;
1395         }
55da90 1396     }
7fe908 1397
MC 1398
1399
1400
1401
55da90 1402     /**
B 1403      * Scan the trash for virusses infection
1404      *
1405      */
1406     function make_trashscan(){
1407         global $app;
7fe908 1408         //trashscan erstellen
MC 1409         // Template Ã–ffnen
1410         $app->tpl->clear_all();
1411         $app->tpl->define( array(table    => 'trashscan.master'));
1412
1413         if(!isset($this->server_conf['virusadmin']) || trim($this->server_conf['virusadmin']) == '') $this->server_conf['virusadmin'] = 'admispconfig@localhost';
1414         if(substr($this->server_conf['virusadmin'], 0, 1) == '#'){
1415             $notify = 'no';
1416         } else {
1417             $notify = 'yes';
1418         }
1419
1420         // Variablen zuweisen
1421         $app->tpl->assign( array(VIRUSADMIN => $this->server_conf['virusadmin'],
1422                 NOTIFICATION => $notify));
1423
1424         $app->tpl->parse(TABLE, table);
1425
1426         $trashscan_text = $app->tpl->fetch();
1427
1428         $datei = '/home/admispconfig/ispconfig/tools/clamav/bin/trashscan';
1429         $app->file->wf($datei, $trashscan_text);
1430
1431         chmod($datei, 0755);
1432         chown($datei, 'admispconfig');
1433         chgrp($datei, 'admispconfig');
55da90 1434     }
7fe908 1435
MC 1436
1437
1438
1439
55da90 1440     /**
B 1441      * Get the current time
1442      *
1443      */
1444     function get_time(){
7fe908 1445         $addr = 'http://www.ispconfig.org/';
MC 1446         $timeout = 1;
1447         $url_parts = parse_url($addr);
1448         $path = $url_parts['path'];
1449         $port = 80;
1450         $urlHandle = @fsockopen($url_parts['host'], $port, $errno, $errstr, $timeout);
1451         if ($urlHandle){
1452             socket_set_timeout($urlHandle, $timeout);
1453
1454             $urlString = 'GET '.$path." HTTP/1.0\r\nHost: ".$url_parts['host']."\r\nConnection: Keep-Alive\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)\r\n";
1455             if ($user) $urlString .= 'Authorization: Basic '.base64_encode($user.':'.$pass)."\r\n";
1456             $urlString .= "\r\n";
1457             fputs($urlHandle, $urlString);
1458
1459             $month['Jan'] = '01';
1460             $month['Feb'] = '02';
1461             $month['Mar'] = '03';
1462             $month['Apr'] = '04';
1463             $month['May'] = '05';
1464             $month['Jun'] = '06';
1465             $month['Jul'] = '07';
1466             $month['Aug'] = '08';
1467             $month['Sep'] = '09';
1468             $month['Oct'] = '10';
1469             $month['Nov'] = '11';
1470             $month['Dec'] = '12';
1471             $c = 0;
1472             $l = 0;
1473             $startzeit = time();
1474             while(!feof($urlHandle) && $c < 2 && $l == 0){
1475                 $line = trim(fgets($urlHandle, 128));
1476                 $response .= $line;
1477                 $c = time() - $startzeit;
1478                 if($line == '' || substr($line, 0, 5) == 'Date:') $l += 1; // nur den Header auslesen
1479                 if(substr($line, 0, 5) == 'Date:'){
1480                     $parts = explode(' ', $line);
1481                     $tag = $parts[2];
1482                     $monat = $month[$parts[3]];
1483                     $jahr = $parts[4];
1484                     list($stunde, $minute, $sekunde) = explode(':', $parts[5]);
1485                     $timestamp = mktime($stunde, $minute, $sekunde, $monat, $tag, $jahr);
1486                 }
1487             }
1488
1489             @fclose($urlHandle);
1490
1491             return $timestamp;
1492         } else {
1493             @fclose($urlHandle);
1494             return false;
1495         }
55da90 1496     }
7fe908 1497
MC 1498     function replaceLine($filename, $search_pattern, $new_line, $strict = 0, $append = 1) {
c77103 1499         global $app;
c3ac0a 1500         if($this->checkpath($filename) == false) {
7fe908 1501             $app->log("Action aborted, file is a symlink: $filename", LOGLEVEL_WARN);
c3ac0a 1502             return false;
T 1503         }
fdb514 1504         $lines = @file($filename);
T 1505         $out = '';
1506         $found = 0;
1507         if(is_array($lines)) {
1508             foreach($lines as $line) {
1509                 if($strict == 0) {
7fe908 1510                     if(stristr($line, $search_pattern)) {
fdb514 1511                         $out .= $new_line."\n";
T 1512                         $found = 1;
1513                     } else {
1514                         $out .= $line;
1515                     }
1516                 } else {
1517                     if(trim($line) == $search_pattern) {
1518                         $out .= $new_line."\n";
1519                         $found = 1;
1520                     } else {
1521                         $out .= $line;
1522                     }
1523                 }
1524             }
1525         }
7fe908 1526
fdb514 1527         if($found == 0) {
T 1528             //* add \n if the last line does not end with \n or \r
7fe908 1529             if(substr($out, -1) != "\n" && substr($out, -1) != "\r" && filesize($filename) > 0) $out .= "\n";
fdb514 1530             //* add the new line at the end of the file
9f56bd 1531             if($append == 1) {
T 1532                 $out .= $new_line."\n";
1533             }
fdb514 1534         }
7fe908 1535         file_put_contents($filename, $out);
fdb514 1536     }
7fe908 1537
MC 1538     function removeLine($filename, $search_pattern, $strict = 0) {
1539         global $app;
1540         if($this->checkpath($filename) == false) {
1541             $app->log("Action aborted, file is a symlink: $filename", LOGLEVEL_WARN);
1542             return false;
1543         }
1544         if($lines = @file($filename)) {
1545             $out = '';
1546             foreach($lines as $line) {
1547                 if($strict == 0) {
1548                     if(!stristr($line, $search_pattern)) {
1549                         $out .= $line;
1550                     }
1551                 } else {
1552                     if(!trim($line) == $search_pattern) {
1553                         $out .= $line;
1554                     }
fdb514 1555                 }
T 1556             }
7fe908 1557             file_put_contents($filename, $out);
fdb514 1558         }
T 1559     }
7fe908 1560
9ea57c 1561     function maildirmake($maildir_path, $user = '', $subfolder = '', $group = '') {
7fe908 1562
128d3b 1563         global $app, $conf;
TB 1564         
1565         // load the server configuration options
1566         $app->uses("getconf");
1567         $mail_config = $app->getconf->get_server_config($conf["server_id"], 'mail');
7fe908 1568
fdb514 1569         if($subfolder != '') {
d2212d 1570             $dir = escapeshellcmd($maildir_path.'/.'.$subfolder);
fdb514 1571         } else {
d2212d 1572             $dir = escapeshellcmd($maildir_path);
2ebadd 1573         }
7fe908 1574
fb3a98 1575         if(!is_dir($dir)) mkdir($dir, 0700, true);
2ebadd 1576
J 1577         if($user != '' && $user != 'root' && $this->is_user($user)) {
d2212d 1578             $user = escapeshellcmd($user);
7fe908 1579             if(is_dir($dir)) $this->chown($dir, $user);
2ebadd 1580
J 1581             $chown_mdsub = true;
0e2978 1582         }
DM 1583
1584         if($group != '' && $group != 'root' && $this->is_group($group)) {
1585             $group = escapeshellcmd($group);
1586             if(is_dir($dir)) $this->chgrp($dir, $group);
1587         
1588             $chgrp_mdsub = true;
fdb514 1589         }
7fe908 1590
MC 1591         $maildirsubs = array('cur', 'new', 'tmp');
2ebadd 1592
J 1593         foreach ($maildirsubs as $mdsub) {
9f56bd 1594             if(!is_dir($dir.'/'.$mdsub)) mkdir($dir.'/'.$mdsub, 0700, true);
0e2978 1595             if ($chown_mdsub) chown($dir.'/'.$mdsub, $user);
DM 1596             if ($chgrp_mdsub) chgrp($dir.'/'.$mdsub, $group);
2ebadd 1597         }
J 1598
1599         chmod($dir, 0700);
7fe908 1600
d2212d 1601         /*
fdb514 1602         if($user != '' && $this->is_user($user) && $user != 'root') {
d2212d 1603             $user = escapeshellcmd($user);
3b4c28 1604             // I assume that the name of the (vmail group) is the same as the name of the mail user in ISPConfig 3
a59ad3 1605             $group = $user;
T 1606             exec("chown $user:$group $dir $dir_cur $dir_new $dir_tmp");
fdb514 1607         }
d2212d 1608         */
7fe908 1609
fdb514 1610         //* Add the subfolder to the subscriptions and courierimapsubscribed files
T 1611         if($subfolder != '') {
128d3b 1612             
fdb514 1613             // Courier
128d3b 1614             if($mail_config['pop3_imap_daemon'] == 'courier') {
TB 1615                 if(!is_file($maildir_path.'/courierimapsubscribed')) {
1616                     $tmp_file = escapeshellcmd($maildir_path.'/courierimapsubscribed');
1617                     touch($tmp_file);
1618                     chmod($tmp_file, 0744);
1619                     chown($tmp_file, 'vmail');
1620                     chgrp($tmp_file, 'vmail');
1621                 }
1622                 $this->replaceLine($maildir_path.'/courierimapsubscribed', 'INBOX.'.$subfolder, 'INBOX.'.$subfolder, 1, 1);
fdb514 1623             }
7fe908 1624
fdb514 1625             // Dovecot
128d3b 1626             if($mail_config['pop3_imap_daemon'] == 'dovecot') {
TB 1627                 if(!is_file($maildir_path.'/subscriptions')) {
1628                     $tmp_file = escapeshellcmd($maildir_path.'/subscriptions');
1629                     touch($tmp_file);
1630                     chmod($tmp_file, 0744);
1631                     chown($tmp_file, 'vmail');
1632                     chgrp($tmp_file, 'vmail');
1633                 }
1634                 $this->replaceLine($maildir_path.'/subscriptions', $subfolder, $subfolder, 1, 1);
fdb514 1635             }
T 1636         }
7fe908 1637
MC 1638         $app->log('Created Maildir '.$maildir_path.' with subfolder: '.$subfolder, LOGLEVEL_DEBUG);
1639
fdb514 1640     }
7fe908 1641
355efb 1642     //* Function to create directory paths and chown them to a user and group
T 1643     function mkdirpath($path, $mode = 0755, $user = '', $group = '') {
7fe908 1644         $path_parts = explode('/', $path);
355efb 1645         $new_path = '';
T 1646         if(is_array($path_parts)) {
1647             foreach($path_parts as $part) {
1648                 $new_path .= '/'.$part;
1649                 if(!@is_dir($new_path)) {
c3ac0a 1650                     $this->mkdir($new_path);
7fe908 1651                     $this->chmod($new_path, $mode);
MC 1652                     if($user != '') $this->chown($new_path, $user);
1653                     if($group != '') $this->chgrp($new_path, $group);
355efb 1654                 }
T 1655             }
1656         }
7fe908 1657
355efb 1658     }
7fe908 1659
42f191 1660     //* Check if a application is installed
T 1661     function is_installed($appname) {
7fe908 1662         exec('which '.escapeshellcmd($appname).' 2> /dev/null', $out, $returncode);
MC 1663         if(isset($out[0]) && stristr($out[0], $appname) && $returncode == 0) {
42f191 1664             return true;
T 1665         } else {
1666             return false;
1667         }
1668     }
7fe908 1669
MC 1670     function web_folder_protection($document_root, $protect) {
1671         global $app, $conf;
1672
c3ac0a 1673         if($this->checkpath($document_root) == false) {
7fe908 1674             $app->log("Action aborted, target is a symlink: $document_root", LOGLEVEL_DEBUG);
c3ac0a 1675             return false;
T 1676         }
7fe908 1677
4b9329 1678         //* load the server configuration options
T 1679         $app->uses('getconf');
1680         $web_config = $app->getconf->get_server_config($conf['server_id'], 'web');
7fe908 1681
4b9329 1682         if($protect == true && $web_config['web_folder_protection'] == 'y') {
T 1683             //* Add protection
7fe908 1684             if($document_root != '' && $document_root != '/' && strlen($document_root) > 6 && !stristr($document_root, '..')) exec('chattr +i '.escapeshellcmd($document_root));
4b9329 1685         } else {
T 1686             //* Remove protection
7fe908 1687             if($document_root != '' && $document_root != '/' && strlen($document_root) > 6 && !stristr($document_root, '..')) exec('chattr -i '.escapeshellcmd($document_root));
4b9329 1688         }
T 1689     }
7fe908 1690
ff6a68 1691     function usermod($username, $uid = 0, $gid = 0, $home = '', $shell = '', $password = '', $login = '') {
T 1692         global $app;
7fe908 1693
ff6a68 1694         if($login == '') $login = $username;
7fe908 1695
ff6a68 1696         //* Change values in /etc/passwd
T 1697         $passwd_file_array = file('/etc/passwd');
1698         if(is_array($passwd_file_array)) {
1699             foreach($passwd_file_array as $line) {
1700                 $line = trim($line);
7fe908 1701                 $parts = explode(':', $line);
ff6a68 1702                 if($parts[0] == $username) {
T 1703                     if(trim($login) != '' && trim($login) != trim($username)) $parts[0] = trim($login);
1704                     if(!empty($uid)) $parts[2] = trim($uid);
1705                     if(!empty($gid)) $parts[3] = trim($gid);
1706                     if(trim($home) != '') $parts[5] = trim($home);
1707                     if(trim($shell) != '') $parts[6] = trim($shell);
7fe908 1708                     $new_line = implode(':', $parts);
MC 1709                     copy('/etc/passwd', '/etc/passwd~');
1710                     chmod('/etc/passwd~', 0600);
ff6a68 1711                     $app->uses('system');
7fe908 1712                     $app->system->replaceLine('/etc/passwd', $line, $new_line, 1, 0);
ff6a68 1713                 }
T 1714             }
1715             unset($passwd_file_array);
1716         }
7fe908 1717
ff6a68 1718         //* If username != login, change username in group and gshadow file
T 1719         if($username  != $login) {
1720             $group_file_array = file('/etc/group');
1721             if(is_array($group_file_array)) {
1722                 foreach($group_file_array as $line) {
1723                     $line = trim($line);
7fe908 1724                     $parts = explode(':', $line);
MC 1725                     if(strstr($parts[3], $username)) {
1726                         $uparts = explode(',', $parts[3]);
ff6a68 1727                         if(is_array($uparts)) {
T 1728                             foreach($uparts as $key => $val) {
1729                                 if($val == $username) $uparts[$key] = $login;
1730                             }
1731                         }
7fe908 1732                         $parts[3] = implode(',', $uparts);
MC 1733                         $new_line = implode(':', $parts);
1734                         copy('/etc/group', '/etc/group~');
1735                         chmod('/etc/group~', 0600);
1736                         $app->system->replaceLine('/etc/group', $line, $new_line, 1, 0);
ff6a68 1737                     }
T 1738                 }
1739             }
1740             unset($group_file_array);
7fe908 1741
ff6a68 1742             $gshadow_file_array = file('/etc/gshadow');
T 1743             if(is_array($gshadow_file_array)) {
1744                 foreach($gshadow_file_array as $line) {
1745                     $line = trim($line);
7fe908 1746                     $parts = explode(':', $line);
MC 1747                     if(strstr($parts[3], $username)) {
1748                         $uparts = explode(',', $parts[3]);
ff6a68 1749                         if(is_array($uparts)) {
T 1750                             foreach($uparts as $key => $val) {
1751                                 if($val == $username) $uparts[$key] = $login;
1752                             }
1753                         }
7fe908 1754                         $parts[3] = implode(',', $uparts);
MC 1755                         $new_line = implode(':', $parts);
1756                         copy('/etc/gshadow', '/etc/gshadow~');
1757                         chmod('/etc/gshadow~', 0600);
1758                         $app->system->replaceLine('/etc/gshadow', $line, $new_line, 1, 0);
ff6a68 1759                     }
T 1760                 }
1761             }
1762             unset($group_file_array);
1763         }
7fe908 1764
MC 1765
ff6a68 1766         //* When password or login name has been changed
T 1767         if($password != '' || $username  != $login) {
1768             $shadow_file_array = file('/etc/shadow');
1769             if(is_array($shadow_file_array)) {
1770                 foreach($shadow_file_array as $line) {
1771                     $line = trim($line);
7fe908 1772                     $parts = explode(':', $line);
ff6a68 1773                     if($parts[0] == $username) {
T 1774                         if(trim($login) != '' && trim($login) != trim($username)) $parts[0] = trim($login);
1775                         if(trim($password) != '') $parts[1] = trim($password);
7fe908 1776                         $new_line = implode(':', $parts);
MC 1777                         copy('/etc/shadow', '/etc/shadow~');
1778                         chmod('/etc/shadow~', 0600);
1779                         $app->system->replaceLine('/etc/shadow', $line, $new_line, 1, 0);
ff6a68 1780                     }
T 1781                 }
1782             }
1783             unset($shadow_file_array);
1784         }
1785     }
7fe908 1786
bfcdef 1787     function intval($string, $force_numeric = false) {
7fe908 1788         if(intval($string) == 2147483647) {
MC 1789             if($force_numeric == true) return floatval($string);
1790             elseif(preg_match('/^([-]?)[0]*([1-9][0-9]*)([^0-9].*)*$/', $string, $match)) return $match[1].$match[2];
1791             else return 0;
1792         } else {
1793             return intval($string);
1794         }
1795     }
1796
526b99 1797     function is_mounted($mountpoint){
cc6568 1798         //$cmd = 'df 2>/dev/null | grep " '.$mountpoint.'$"';
H 1799         $cmd = 'mount 2>/dev/null | grep " on '.$mountpoint.' type "';
526b99 1800         exec($cmd, $output, $return_var);
7fe908 1801         return $return_var == 0 ? true : false;
526b99 1802     }
7fe908 1803
990ca8 1804     function mount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_mount.sh'){
66fa9b 1805         global $app, $conf;
9f824a 1806         
MB 1807         if($this->is_mounted($backup_dir)) return true;
1808         
990ca8 1809         $mounted = true;
FS 1810         if (     is_file($mount_cmd) &&
1811                 is_executable($mount_cmd) &&
1812                 fileowner($mount_cmd) === 0
1813         ) {
1814             if (!$this->is_mounted($backup_dir)){
a8995c 1815                 exec($mount_cmd);
990ca8 1816                 sleep(1);
FS 1817                 if (!$this->is_mounted($backup_dir)) $mounted = false;
1818             }
1819         } else $mounted = false;
66fa9b 1820         if (!$mounted) {
FS 1821             //* send email to admin that backup directory could not be mounted
1822             $global_config = $app->getconf->get_global_config('mail');
1823             if($global_config['admin_mail'] != ''){
1824                 $subject = 'Backup directory '.$backup_dir.' could not be mounted';
1825                 $message = "Backup directory ".$backup_dir." could not be mounted.\n\nThe command\n\n".$mount_cmd."\n\nfailed.";
1826                 mail($global_config['admin_mail'], $subject, $message);
1827             }
1828         }
990ca8 1829
FS 1830         return $mounted;
1831     }
a8995c 1832
FS 1833     function umount_backup_dir($backup_dir, $mount_cmd = '/usr/local/ispconfig/server/scripts/backup_dir_umount.sh'){
66fa9b 1834         global $app, $conf;
FS 1835
a8995c 1836         if (     is_file($mount_cmd) &&
FS 1837                 is_executable($mount_cmd) &&
1838                 fileowner($mount_cmd) === 0
1839         ) {
1840             if ($this->is_mounted($backup_dir)){
1841                 exec($mount_cmd);
1842                 sleep(1);
1843             }
1844         }
1845
66fa9b 1846         $unmounted = $this->is_mounted($backup_dir) == 0 ? true : false;
FS 1847         if(!$unmounted) {
1848             //* send email to admin that backup directory could not be unmounted
1849             $global_config = $app->getconf->get_global_config('mail');
1850             if($global_config['admin_mail'] != ''){
1851                 $subject = 'Backup directory '.$backup_dir.' could not be unmounted';
1852                 $message = "Backup directory ".$backup_dir." could not be unmounted.\n\nThe command\n\n".$mount_cmd."\n\nfailed.";
1853                 mail($global_config['admin_mail'], $subject, $message);
1854             }
1855         }
1856
1857         return $unmounted;
1858
a8995c 1859     }
FS 1860
ea476a 1861     function _getinitcommand($servicename, $action, $init_script_directory = '', $check_service) {
33bcd0 1862         global $conf;
FT 1863         // upstart
1864         if(is_executable('/sbin/initctl')){
1865             exec('/sbin/initctl version 2>/dev/null | /bin/grep -q upstart', $retval['output'], $retval['retval']);
1866             if(intval($retval['retval']) == 0) return 'service '.$servicename.' '.$action;
1867         }
ea476a 1868
bc04c3 1869         // systemd
TB 1870         if(is_executable('/bin/systemd') || is_executable('/usr/bin/systemctl')){
ea476a 1871             if ($check_service) {
FS 1872                 exec("systemctl is-enabled ".$servicename." 2>&1", $out, $ret_val);
1873             }
1874             if ($ret_val == 0 || !$check_service) {
1875                 return 'systemctl '.$action.' '.$servicename.'.service';
1876             }
33bcd0 1877         }
ea476a 1878
33bcd0 1879         // sysvinit
FT 1880         if($init_script_directory == '') $init_script_directory = $conf['init_scripts'];
1881         if(substr($init_script_directory, -1) === '/') $init_script_directory = substr($init_script_directory, 0, -1);
ea476a 1882         if($check_service && is_executable($init_script_directory.'/'.$servicename)) {
FS 1883             return $init_script_directory.'/'.$servicename.' '.$action;
1884         }
1885         if (!$check_service) {
1886             return $init_script_directory.'/'.$servicename.' '.$action;
1887         }
33bcd0 1888     }
ea476a 1889
FS 1890     function getinitcommand($servicename, $action, $init_script_directory = '', $check_service=false) {
1891         if (is_array($servicename)) {
1892             foreach($servicename as $service) {
1893                 $out = $this->_getinitcommand($service, $action, $init_script_directory, true);
1894                 if ($out != '') return $out;
1895             }
1896         } else {
1897             return $this->_getinitcommand($servicename, $action, $init_script_directory, $check_service);
1898         }
1899     }
1900
8133de 1901     function getapacheversion($get_minor = false) {
MC 1902         global $app;
1903         
1904         $cmd = '';
1905         if($this->is_installed('apache2ctl')) $cmd = 'apache2ctl -v';
1906         elseif($this->is_installed('apachectl')) $cmd = 'apachectl -v';
1907         else {
1908             $app->log("Could not check apache version, apachectl not found.", LOGLEVEL_WARN);
1909             return '2.2';
1910         }
1911         
1912         exec($cmd, $output, $return_var);
1913         if($return_var != 0 || !$output[0]) {
1914             $app->log("Could not check apache version, apachectl did not return any data.", LOGLEVEL_WARN);
1915             return '2.2';
1916         }
1917         
1918         if(preg_match('/version:\s*Apache\/(\d+)(\.(\d+)(\.(\d+))*)?(\D|$)/i', $output[0], $matches)) {
1919             return $matches[1] . (isset($matches[3]) ? '.' . $matches[3] : '') . (isset($matches[5]) && $get_minor == true ? '.' . $matches[5] : '');
1920         } else {
1921             $app->log("Could not check apache version, did not find version string in apachectl output.", LOGLEVEL_WARN);
1922             return '2.2';
1923         }
1924     }
e2d6ed 1925
60b700 1926     function getapachemodules() {
MC 1927         global $app;
1928         
1929         $cmd = '';
1e67c1 1930         if($this->is_installed('apache2ctl')) $cmd = 'apache2ctl -t -D DUMP_MODULES';
TB 1931         elseif($this->is_installed('apachectl')) $cmd = 'apachectl -t -D DUMP_MODULES';
60b700 1932         else {
MC 1933             $app->log("Could not check apache modules, apachectl not found.", LOGLEVEL_WARN);
1934             return array();
1935         }
1936         
0a0ca4 1937         exec($cmd . ' 2>/dev/null', $output, $return_var);
60b700 1938         if($return_var != 0 || !$output[0]) {
MC 1939             $app->log("Could not check apache modules, apachectl did not return any data.", LOGLEVEL_WARN);
1940             return array();
1941         }
1942         
1943         $modules = array();
1944         for($i = 0; $i < count($output); $i++) {
1945             if(preg_match('/^\s*(\w+)\s+\((shared|static)\)\s*$/', $output[$i], $matches)) {
1946                 $modules[] = $matches[1];
1947             }
1948         }
1949         
1950         return $modules;
1951     }
d67883 1952     
TB 1953     //* ISPConfig mail function
1954     public function mail($to, $subject, $text, $from, $filepath = '', $filetype = 'application/pdf', $filename = '', $cc = '', $bcc = '', $from_name = '') {
1955         global $app, $conf;
1956
1957         if($conf['demo_mode'] == true) $app->error("Mail sending disabled in demo mode.");
1958
1959         $app->uses('getconf,ispcmail');
1960         $mail_config = $app->getconf->get_global_config('mail');
1961         if($mail_config['smtp_enabled'] == 'y') {
1962             $mail_config['use_smtp'] = true;
1963             $app->ispcmail->setOptions($mail_config);
1964         }
1965         $app->ispcmail->setSender($from, $from_name);
1966         $app->ispcmail->setSubject($subject);
1967         $app->ispcmail->setMailText($text);
1968
1969         if($filepath != '') {
1970             if(!file_exists($filepath)) $app->error("Mail attachement does not exist ".$filepath);
1971             $app->ispcmail->readAttachFile($filepath);
1972         }
1973
1974         if($cc != '') $app->ispcmail->setHeader('Cc', $cc);
1975         if($bcc != '') $app->ispcmail->setHeader('Bcc', $bcc);
1976
1977         $app->ispcmail->send($to);
1978         $app->ispcmail->finish();
1979         
1980         return true;
1981     }
1982     
64ea56 1983     public function is_allowed_user($username, $check_id = true, $restrict_names = false) {
MC 1984         global $app;
1985         
f2fc77 1986         $name_blacklist = array('root','ispconfig','vmail','getmail');
TB 1987         if(in_array($username,$name_blacklist)) return false;
1988         
373e88 1989         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $username) == false) return false;
f2fc77 1990         
64ea56 1991         if($check_id && intval($this->getuid($username)) < $this->min_uid) return false;
MC 1992         
1993         if($restrict_names == true && preg_match('/^web\d+$/', $username) == false) return false;
1994         
1995         return true;
1996     }
1997     
15687e 1998     public function is_allowed_group($groupname, $check_id = true, $restrict_names = false) {
64ea56 1999         global $app;
0cba4e 2000         
f2fc77 2001         $name_blacklist = array('root','ispconfig','vmail','getmail');
TB 2002         if(in_array($groupname,$name_blacklist)) return false;
0cba4e 2003         
373e88 2004         if(preg_match('/^[a-zA-Z0-9\.\-_]{1,32}$/', $groupname) == false) return false;
0cba4e 2005         
15687e 2006         if($check_id && intval($this->getgid($groupname)) < $this->min_gid) return false;
0cba4e 2007         
64ea56 2008         if($restrict_names == true && preg_match('/^client\d+$/', $groupname) == false) return false;
0cba4e 2009         
64ea56 2010         return true;
MC 2011     }
2012     
e2d6ed 2013 }
7fe908 2014
3b4c28 2015 ?>