redray
2008-12-01 436ed820192d05299a639b45ade96f3916bb51f2
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
55da90 33     var $FILE = "/root/ispconfig/scripts/lib/classes/ispconfig_system.lib.php";
B 34     var $server_id;
35     var $server_conf;
36     var $data;
37     
38     /**
39      * Construct for this class
40      *
41      * @return system
42      */
43     public function system(){
44         global $go_info;
45           $this->server_id = $go_info["isp"]["server_id"];
46           $this->server_conf = $go_info["isp"]["server_conf"];
47           $this->server_conf["passwd_datei"] = '/etc/passwd';
48           $this->server_conf["shadow_datei"] = '/etc/shadow';
49           $this->server_conf["group_datei"] = '/etc/group';
50     }
51     
52     /**
53      * Get the hostname from the server
54      *
55      * @return string
56      */
57     public function hostname(){
58         $dist = $this->server_conf["dist"];
59     
60          ob_start();
61           passthru("hostname");
62           $hostname = ob_get_contents();
63           ob_end_clean();
64         $hostname = trim($hostname);
65           ob_start();
66           if(!strstr($dist, "freebsd")){
67             passthru("dnsdomainname");
68           } else {
69             passthru("domainname");
70           }
71           $domainname = ob_get_contents();
72           ob_end_clean();
73           $domainname = trim($domainname);
74           if($domainname != ""){
75             if(!strstr($hostname, $domainname)) $hostname .= ".".$domainname;
76           }
77           return $hostname;
78     }
79     
80     /**
81      * Add an user to the system
82      * 
83      */
84     public function adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
85         global $app;
86           if($this->is_user($user_username)){
87             return false;
88           } else {
89             if(trim($user_username) != '') {
90                 $user_datei = $this->server_conf["passwd_datei"];
91                 $shadow_datei = $this->server_conf["shadow_datei"];
92                 $shell = realpath($shell);
93                 if(trim($passwort) == "") $passwort = '*';
94                 $new_user = "\n$user_username:x:$uid:$gid:$username:$homedir:$shell\n";
95                 $app->log->msg("USER: $new_user");
96                 $app->file->af($user_datei, $new_user);
97                 if($shadow_datei == "/etc/shadow"){
98                     $datum = time();
99                     $tage = floor($datum/86400);
100                     $new_passwd = "\n$user_username:$passwort:$tage:0:99999:7:::\n";
101                 } else {
102                     $new_passwd = "\n$user_username:$passwort:$uid:$gid::0:0:$username:$homedir:$shell\n";
103                 }
104                 $app->file->af($shadow_datei, $new_passwd);
105                 // TB: leere Zeilen entfernen
106                 $app->file->remove_blank_lines($shadow_datei);
107                 $app->file->remove_blank_lines($user_datei);
108                 // TB: user Sortierung deaktiviert
109                 //$this->order_users_groups();
110                 if($shadow_datei != "/etc/shadow"){
111                     $app->file->af($shadow_datei, "\n");
112                     // TB: leere Zeilen entfernen
113                     $app->file->remove_blank_lines($shadow_datei);
114                     $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
115                 }
116                 return true;
117             }
118       }
119     }
120     
121     /**
122      * Update users when someone edit it
123      *
124      */
125     function updateuser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort = '*'){
126         //* First delete the users
127         $this->deluser($user_username);
128         //* Add the user again
129           $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
130     }
131     
132     /**
133      * Lock the user
134      *
135      */
136     function deactivateuser($user_username){
137         $passwort = str_rot13($this->getpasswd($user_username));
138           $user_attr = $this->get_user_attributes($user_username);
139           $uid = $user_attr["uid"];
140           $gid = $user_attr["gid"];
141           $username = $user_attr["name"];
142           $homedir = $user_attr["homedir"];
143           $shell = "/dev/null";
144           $this->deluser($user_username);
145           $this->adduser($user_username, $uid, $gid, $username, $homedir, $shell, $passwort);
146     }
147     /**
148      * Delete a user from the system
149      *
150      */
151     function deluser($user_username){
152         global $app;
153           if($this->is_user($user_username)){
154             $user_datei = $this->server_conf["passwd_datei"];
155             $shadow_datei = $this->server_conf["shadow_datei"];
156             $users = $app->file->rf($user_datei);
157             $lines = explode("\n", $users);
158             if(is_array($lines)){
159                   $num_lines = sizeof($lines);
160                   for($i=0;$i<$num_lines;$i++){
161                     if(trim($lines[$i]) != ""){
162                           list($f1,) = explode(":", $lines[$i]);
163                           if($f1 != $user_username) $new_lines[] = $lines[$i];
164                     }
165                   }
166                   $new_users = implode("\n", $new_lines);
167                   $app->file->wf($user_datei, $new_users);
168                   unset($new_lines);
169                   unset($lines);
170                   unset($new_users);
171             }
172             $app->file->remove_blank_lines($user_datei);
173     
174             $passwds = $app->file->rf($shadow_datei);
175             $lines = explode("\n", $passwds);
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_passwds = implode("\n", $new_lines);
185                   $app->file->wf($shadow_datei, $new_passwds);
186                   unset($new_lines);
187                   unset($lines);
188                   unset($new_passwds);
189             }
190             $app->file->remove_blank_lines($shadow_datei);
191     
192             $group_file = $app->file->rf($this->server_conf["group_datei"]);
193             $group_file_lines = explode("\n", $group_file);
194             foreach($group_file_lines as $group_file_line){
195                   if(trim($group_file_line) != ""){
196                     list($f1, $f2, $f3, $f4) = explode(":", $group_file_line);
197                     $group_users = explode(",", str_replace(" ", "", $f4));
198                     if(in_array($user_username, $group_users)){
199                           $g_users = array();
200                           foreach($group_users as $group_user){
201                             if($group_user != $user_username) $g_users[] = $group_user;
202                           }
203                           $f4 = implode(",", $g_users);
204                     }
205                     $new_group_file[] = $f1.":".$f2.":".$f3.":".$f4;
206                   }
207             }
208             $new_group_file = implode("\n", $new_group_file);
209             $app->file->wf($this->server_conf["group_datei"], $new_group_file);
210             // TB: auskommentiert
211             //$this->order_users_groups();
212     
213             if($shadow_datei != "/etc/shadow"){
214                   $app->file->af($shadow_datei, "\n");
215                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
216             }
217             return true;
218           } else {
219             return false;
220           }
221     }
222     
223     /**
224      * Add a usergroup to the system
225      *
226      */
227     function addgroup($group, $gid, $members = ''){
228         global $app;
229           if($this->is_group($group)){
230             return false;
231           } else {
232             $group_datei = $this->server_conf["group_datei"];
233             $shadow_datei = $this->server_conf["shadow_datei"];
234             $new_group = "\n$group:x:$gid:$members\n";
235             $app->file->af($group_datei, $new_group);
236     
237             // TB: auskommentiert
238             //$this->order_users_groups();
239             if($shadow_datei != "/etc/shadow"){
240                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
241             }
242             return true;
243           }
244     }
245     
246     /**
247      * Update usersgroup in way to delete and add it again
248      *
249      */
250     function updategroup($group, $gid, $members = ''){
251         $this->delgroup($group);
252           $this->addgroup($group, $gid, $members);
253     }
254     
255     /**
256      * Delete a usergroup from the system
257      *
258      */
259     function delgroup($group){
260         global $app;
261           if($this->is_group($group)){
262             $group_datei = $this->server_conf["group_datei"];
263             $shadow_datei = $this->server_conf["shadow_datei"];
264             $groups = $app->file->rf($group_datei);
265             $lines = explode("\n", $groups);
266             if(is_array($lines)){
267                   $num_lines = sizeof($lines);
268                   for($i=0;$i<$num_lines;$i++){
269                     if(trim($lines[$i]) != ""){
270                           list($f1,) = explode(":", $lines[$i]);
271                           if($f1 != $group) $new_lines[] = $lines[$i];
272                     }
273                   }
274                   $new_groups = implode("\n", $new_lines);
275                   $app->file->wf($group_datei, $new_groups);
276                   unset($new_lines);
277                   unset($lines);
278                   unset($new_groups);
279             }
280             // TB: auskommentiert
281             //$this->order_users_groups();
282             if($shadow_datei != "/etc/shadow"){
283                   $app->log->caselog("pwd_mkdb $shadow_datei &> /dev/null", $this->FILE, __LINE__);
284             }
285             return true;
286           } else {
287             return false;
288           }
289     }
290     /**
291      * Order usergroups
292      *
293      */
294     function order_users_groups(){
295         global $app;
296           $user_datei = $this->server_conf["passwd_datei"];
297           $shadow_datei = $this->server_conf["shadow_datei"];
298           $group_datei = $this->server_conf["group_datei"];
299     
300           $groups = $app->file->no_comments($group_datei);
301           $lines = explode("\n", $groups);
302           if(is_array($lines)){
303             foreach($lines as $line){
304                   if(trim($line) != ""){
305                     list($f1, $f2, $f3, $f4) = explode(":", $line);
306                     $arr[$f3] = $line;
307                   }
308             }
309           }
310           ksort($arr);
311           reset($arr);
312           if($shadow_datei != "/etc/shadow"){
313             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0)."\n");
314           }else {
315             $app->file->wf($group_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
316           }
317           unset($arr);
318     
319           $users = $app->file->no_comments($user_datei);
320           $lines = explode("\n", $users);
321           if(is_array($lines)){
322             foreach($lines as $line){
323                   if(trim($line) != ""){
324                     list($f1, $f2, $f3,) = explode(":", $line);
325                     if($f1 != "toor"){
326                           $arr[$f3] = $line;
327                     } else {
328                           $arr[70000] = $line;
329                     }
330                   }
331             }
332           }
333           ksort($arr);
334           reset($arr);
335           $app->file->wf($user_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
336           unset($arr);
337     
338           $passwds = $app->file->no_comments($shadow_datei);
339           $lines = explode("\n", $passwds);
340           if(is_array($lines)){
341             foreach($lines as $line){
342                 if(trim($line) != ""){
343                     list($f1, $f2, $f3,) = explode(":", $line);
344                     if($f1 != "toor"){
345                           $uid = $this->getuid($f1);
346                           if(!is_bool($uid)) $arr[$uid] = $line;
347                     } else {
348                           $arr[70000] = $line;
349                     }
350                   }
351             }
352           }
353           ksort($arr);
354           reset($arr);
355           $app->file->wf($shadow_datei, $app->file->remove_blank_lines(implode("\n", $arr), 0));
356           unset($arr);
357     }
358     
359     /**
360      * Find a user / group id
361      *
362      */
363     function find_uid_gid($min, $max){
364         global $app;
365           if($min < $max && $min >= 0 && $max >= 0 && $min <= 65536 && $max <= 65536 && is_int($min) && is_int($max)){
366             for($i=$min;$i<=$max;$i++){
367                   $uid_arr[$i] = $gid_arr[$i] = 1;
368             }
369             $user_datei = $this->server_conf["passwd_datei"];
370             $group_datei = $this->server_conf["group_datei"];
371     
372             $users = $app->file->no_comments($user_datei);
373             $lines = explode("\n", $users);
374             if(is_array($lines)){
375                   foreach($lines as $line){
376                     if(trim($line) != ""){
377                           list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(":", $line);
378                           if($f3 >= $min && $f3 <= $max) unset($uid_arr[$f3]);
379                     }
380                   }
381                   if(!empty($uid_arr)){
382                     foreach($uid_arr as $key => $val){
383                           $uids[] = $key;
384                     }
385                     $min_uid = min($uids);
386                     unset($uid_arr);
387                   } else {
388                     return false;
389                   }
390             }
391     
392             $groups = $app->file->no_comments($group_datei);
393             $lines = explode("\n", $groups);
394             if(is_array($lines)){
395                   foreach($lines as $line){
396                     if(trim($line) != ""){
397                           list($f1, $f2, $f3, $f4) = explode(":", $line);
398                           if($f3 >= $min && $f3 <= $max) unset($gid_arr[$f3]);
399                     }
400                   }
401                   if(!empty($gid_arr)){
402                     foreach($gid_arr as $key => $val){
403                           $gids[] = $key;
404                     }
405                     $min_gid = min($gids);
406                     unset($gid_arr);
407                   } else {
408                     return false;
409                   }
410             }
411     
412             $result = array_intersect($uids, $gids);
413             $new_id = (max($result));
414             unset($uids);
415             unset($gids);
416             unset($result);
417             if($new_id <= $max){
418                   return $new_id;
419             } else {
420                   return false;
421             }
422           } else {
423             return false;
424           }
425     }
426     
427     /**
428      * Check if the users is really a user into the system
429      *
430      */
431     function is_user($user){
432         global $app;
433           $user_datei = $this->server_conf["passwd_datei"];
434           $users = $app->file->no_comments($user_datei);
435           $lines = explode("\n", $users);
436           if(is_array($lines)){
437             foreach($lines as $line){
438                   if(trim($line) != ""){
439                     list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(":", $line);
440                     if($f1 == $user) return true;
441                   }
442             }
443           }
444           return false;
445     }
446     
447     /**
448      * Check if the group is on this system
449      *
450      */
451     function is_group($group){
452         global $app;
453           $group_datei = $this->server_conf["group_datei"];
454           $groups = $app->file->no_comments($group_datei);
455           $lines = explode("\n", $groups);
456           if(is_array($lines)){
457             foreach($lines as $line){
458                   if(trim($line) != ""){
459                     list($f1, $f2, $f3, $f4) = explode(":", $line);
460                     if($f1 == $group) return true;
461                   }
462             }
463           }
464           return false;
465     }
466     
467     function root_group(){
468         global $app;
469           $group_datei = $this->server_conf["group_datei"];
470           $groups = $app->file->no_comments($group_datei);
471           $lines = explode("\n", $groups);
472           if(is_array($lines)){
473             foreach($lines as $line){
474                   if(trim($line) != ""){
475                     list($f1, $f2, $f3, $f4) = explode(":", $line);
476                     if($f3 == 0) return $f1;
477                   }
478             }
479           }
480           return false;
481     }
482     
483     /**
484      * Get the groups of an user
485      *
486      */
487     function get_user_groups($username){
488         global $app;
489           $user_groups = array();
490           $group_datei = $this->server_conf["group_datei"];
491           $groups = $app->file->no_comments($group_datei);
492           $lines = explode("\n", $groups);
493           if(is_array($lines)){
494             foreach($lines as $line){
495                   if(trim($line) != ""){
496                     list($f1, $f2, $f3, $f4) = explode(":", $line);
497                     if(intval($f3) < intval($this->server_conf["groupid_von"]) && trim($f1) != 'users'){
498                           $tmp_group_users = explode(',', str_replace(' ', '', $f4));
499                           if(in_array($username, $tmp_group_users) && trim($f1) != '') $user_groups[] = $f1;
500                           unset($tmp_group_users);
501                     }
502                   }
503             }
504           }
505           if(!empty($user_groups)) return implode(',', $user_groups);
506           return '';
507     }
508     
509     /**
510      * Get a user password
511      *
512      */
513     function getpasswd($user){
514         global $app;
515           if($this->is_user($user)){
516             $shadow_datei = $this->server_conf["shadow_datei"];
517             $passwds = $app->file->no_comments($shadow_datei);
518             $lines = explode("\n", $passwds);
519             if(is_array($lines)){
520                   foreach($lines as $line){
521                     if(trim($line) != ""){
522                           list($f1, $f2,) = explode(":", $line);
523                           if($f1 == $user) return $f2;
524                     }
525                   }
526             }
527           } else {
528             return false;
529           }
530     }
531     
532     /**
533      * Get the user id from an user
534      *
535      */
536     function getuid($user){
537         global $app;
538           if($this->is_user($user)){
539             $user_datei = $this->server_conf["passwd_datei"];
540             $users = $app->file->no_comments($user_datei);
541             $lines = explode("\n", $users);
542             if(is_array($lines)){
543                   foreach($lines as $line){
544                     if(trim($line) != ""){
545                           list($f1, $f2, $f3,) = explode(":", $line);
546                           if($f1 == $user) return $f3;
547                     }
548                   }
549             }
550           } else {
551             return false;
552           }
553     }
554     
555     /**
556      * Get all information from a user
557      *
558      */
559     function get_user_attributes($user){
560         global $app;
561           if($this->is_user($user)){
562             $user_datei = $this->server_conf["passwd_datei"];
563             $users = $app->file->no_comments($user_datei);
564             $lines = explode("\n", $users);
565             if(is_array($lines)){
566                   foreach($lines as $line){
567                     if(trim($line) != ""){
568                           list($f1, $f2, $f3, $f4, $f5, $f6, $f7) = explode(":", $line);
569                           if($f1 == $user){
570                             $user_attr["username"] = $f1;
571                             $user_attr["x"] = $f2;
572                             $user_attr["uid"] = $f3;
573                             $user_attr["gid"] = $f4;
574                             $user_attr["name"] = $f5;
575                             $user_attr["homedir"] = $f6;
576                             $user_attr["shell"] = $f7;
577                             return $user_attr;
578                           }
579                     }
580                   }
581             }
582           } else {
583             return false;
584           }
585     }
586     
587     /**
588      * Edit the owner of a file
589      *
590      */
591     function chown($file, $owner, $group = ''){
592       $owner_change = @chown($file, $owner);
593       if($group != ""){
594         $group_change = @chgrp($file, $group);
595       } else {
596         $group_change = 1;
597       }
598       if($owner_change && $group_change){
599         return true;
600       } else {
601         return false;
602       }
603     }
604     
605     /**
606      * Add an user to a specific group
607      *
608      */
609     function add_user_to_group($group, $user = 'admispconfig'){
610         global $app;
611           $group_file = $app->file->rf($this->server_conf["group_datei"]);
612           $group_file_lines = explode("\n", $group_file);
613           foreach($group_file_lines as $group_file_line){
614             list($group_name,$group_x,$group_id,$group_users) = explode(":",$group_file_line);
615             if($group_name == $group){
616                   $group_users = explode(",", str_replace(" ", "", $group_users));
617                   if(!in_array($user, $group_users)){
618                     $group_users[] = $user;
619                   }
620                   $group_users = implode(",", $group_users);
621                   if(substr($group_users,0,1) == ",") $group_users = substr($group_users,1);
622                   $group_file_line = $group_name.":".$group_x.":".$group_id.":".$group_users;
623             }
624             $new_group_file[] = $group_file_line;
625           }
626           $new_group_file = implode("\n", $new_group_file);
627           $app->file->wf($this->server_conf["group_datei"], $new_group_file);
628           $app->file->remove_blank_lines($this->server_conf["group_datei"]);
629           if($this->server_conf["shadow_datei"] != "/etc/shadow"){
630             $app->log->caselog("pwd_mkdb ".$this->server_conf["shadow_datei"]." &> /dev/null", $this->FILE, __LINE__);
631           }
632     }
633     
634     function usermod($user, $groups){
635         global $app;
636           if($this->is_user($user)){
637             $groups = explode(",", str_replace(" ", "", $groups));
638             $group_file = $app->file->rf($this->server_conf["group_datei"]);
639             $group_file_lines = explode("\n", $group_file);
640             foreach($group_file_lines as $group_file_line){
641                   if(trim($group_file_line) != ""){
642                     list($f1, $f2, $f3, $f4) = explode(":", $group_file_line);
643                     $group_users = explode(",", str_replace(" ", "", $f4));
644                     if(!in_array($f1, $groups)){
645                           if(in_array($user, $group_users)){
646                             $g_users = array();
647                             foreach($group_users as $group_user){
648                                   if($group_user != $user) $g_users[] = $group_user;
649                             }
650                             $f4 = implode(",", $g_users);
651                           }
652                     } else {
653                           if(!in_array($user, $group_users)){
654                             if(trim($group_users[0]) == "") unset($group_users);
655                             $group_users[] = $user;
656                           }
657                           $f4 = implode(",", $group_users);
658                     }
659                     $new_group_file[] = $f1.":".$f2.":".$f3.":".$f4;
660                   }
661             }
662             $new_group_file = implode("\n", $new_group_file);
663             $app->file->wf($this->server_conf["group_datei"], $new_group_file);
664             $app->file->remove_blank_lines($this->server_conf["group_datei"]);
665             if($this->server_conf["shadow_datei"] != "/etc/shadow"){
666                   $app->log->caselog("pwd_mkdb ".$this->server_conf["shadow_datei"]." &> /dev/null", $this->FILE, __LINE__);
667             }
668             return true;
669           } else {
670             return false;
671           }
672     }
673     
674     /**boot autostart etc
675      *
676      */
677     function rc_edit($service, $rl, $action){
678         // $action = "on|off";
679           global $app;
680           $dist_init_scripts = $app->system->server_conf["dist_init_scripts"];
681           $dist_runlevel = $app->system->server_conf["dist_runlevel"];
682           $dist = $app->system->server_conf["dist"];
683           if(trim($dist_runlevel) == ""){ // falls es keine runlevel gibt (FreeBSD)
684             if($action == "on"){
685                 @symlink($dist_init_scripts."/".$service, $dist_init_scripts."/".$service.".sh");
686             }
687             if($action == "off"){
688                   if(is_link($dist_init_scripts."/".$service.".sh")){
689                     unlink($dist_init_scripts."/".$service.".sh");
690                   } else {
691                     exec("mv -f ".$dist_init_scripts."/".$service.".sh ".$dist_init_scripts."/".$service." &> /dev/null");
692                   }
693             }
694           } else { // Linux
695             if(substr($dist, 0,4) == 'suse'){
696                   if($action == "on"){
697                     exec("chkconfig --add $service &> /dev/null");
698                   }
699                   if($action == "off"){
700                     exec("chkconfig --del $service &> /dev/null");
701                   }
702             } else {
703                   $runlevels = explode(",", $rl);
704                   foreach($runlevels as $runlevel){
705                     $runlevel = trim($runlevel);
706                     if($runlevel != "" && is_dir($dist_runlevel."/rc".$runlevel.".d")){
707                           $handle=opendir($dist_runlevel."/rc".$runlevel.".d");
708                           while($file = readdir($handle)){
709                             if($file != "." && $file != ".."){
710                                   $target = @readlink($dist_runlevel."/rc".$runlevel.".d/".$file);
711                                   if(strstr($file, $service) && strstr($target, $service) && substr($file,0,1) == "S") $ln_arr[$runlevel][] = $dist_runlevel."/rc".$runlevel.".d/".$file;
712                             }
713                           }
714                           closedir($handle);
715                     }
716                     if($action == "on"){
717                           if(!is_array($ln_arr[$runlevel])) @symlink($dist_init_scripts."/".$service, $dist_runlevel."/rc".$runlevel.".d/S99".$service);
718                     }
719                     if($action == "off"){
720                           if(is_array($ln_arr[$runlevel])){
721                             foreach($ln_arr[$runlevel] as $link){
722                                   unlink($link);
723                             }
724                           }
725                     }
726                   }
727             }
728           }
729     }
730     
731     /**
732      * Filter information from the commands
733      *
734      */
735     function grep($content, $string, $params = ''){
736         global $app;
737           // params: i, v, w
738           $content = $app->file->unix_nl($content);
739           $lines = explode("\n", $content);
740           foreach($lines as $line){
741             if(!strstr($params, 'w')){
742                   if(strstr($params, 'i')){
743                     if(strstr($params, 'v')){
744                           if(!stristr($line, $string)) $find[] = $line;
745                     } else {
746                           if(stristr($line, $string)) $find[] = $line;
747                     }
748                   } else {
749                     if(strstr($params, 'v')){
750                           if(!strstr($line, $string)) $find[] = $line;
751                     } else {
752                           if(strstr($line, $string)) $find[] = $line;
753                     }
754                   }
755             } else {
756                   if(strstr($params, 'i')){
757                     if(strstr($params, 'v')){
758                           if(!$app->string->is_word($string, $line, 'i')) $find[] = $line;
759                     } else {
760                           if($app->string->is_word($string, $line, 'i')) $find[] = $line;
761                     }
762                   } else {
763                     if(strstr($params, 'v')){
764                           if(!$app->string->is_word($string, $line)) $find[] = $line;
765                     } else {
766                           if($app->string->is_word($string, $line)) $find[] = $line;
767                     }
768                   }
769             }
770           }
771           if(is_array($find)){
772             $ret_val = implode("\n", $find);
773             if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
774             $find = NULL;
775             return $ret_val;
776           } else {
777             return false;
778           }
779     }
780     
781     /**
782      * Strip content from fields
783      *
784      */
785     function cut($content, $field, $delimiter = ':'){
786         global $app;
787           $content = $app->file->unix_nl($content);
788           $lines = explode("\n", $content);
789           foreach($lines as $line){
790             $elms = explode($delimiter, $line);
791             $find[] = $elms[($field-1)];
792           }
793           if(is_array($find)){
794             $ret_val = implode("\n", $find);
795             if(substr($ret_val,-1) != "\n") $ret_val .= "\n";
796             $find = NULL;
797             return $ret_val;
798           } else {
799             return false;
800           }
801     }
802     
803     /**
804      * Get the content off a file
805      *
806      */
807     function cat($file){
808         global $app;
809           return $app->file->rf($file);
810     }
811     
812     /**
813      * Control services to restart etc
814      *
815      */
816     function daemon_init($daemon, $action){
817         //* $action = start|stop|restart|reload
818           global $app;
819           $dist = $this->server_conf["dist"];
820           $dist_init_scripts = $this->server_conf["dist_init_scripts"];
821           if(!strstr($dist, "freebsd")){
822             $app->log->caselog("$dist_init_scripts/$daemon $action &> /dev/null", $this->FILE, __LINE__);
823           } else {
824             if(is_file($dist_init_scripts."/".$daemon.".sh") || is_link($dist_init_scripts."/".$daemon.".sh")){
825                   if($action == "start" || $action == "stop"){
826                     $app->log->caselog($dist_init_scripts."/".$daemon.".sh ".$action." &> /dev/null", $this->FILE, __LINE__);
827                   } else {
828                     $app->log->caselog($dist_init_scripts."/".$daemon.".sh stop &> /dev/null", $this->FILE, __LINE__);
829                     sleep(3);
830                     $app->log->caselog($dist_init_scripts."/".$daemon.".sh start &> /dev/null", $this->FILE, __LINE__);
831                   }
832             } else {
833                   if(is_file($dist_init_scripts."/".$daemon) || is_link($dist_init_scripts."/".$daemon)){
834                     if($action == "start" || $action == "stop"){
835                           $app->log->caselog($dist_init_scripts."/".$daemon." ".$action." &> /dev/null", $this->FILE, __LINE__);
836                     } else {
837                           $app->log->caselog($dist_init_scripts."/".$daemon." stop &> /dev/null", $this->FILE, __LINE__);
838                           sleep(3);
839                           $app->log->caselog($dist_init_scripts."/".$daemon." start &> /dev/null", $this->FILE, __LINE__);
840                     }
841                   } else {
842                     if(is_file("/etc/rc.d/".$daemon) || is_link("/etc/rc.d/".$daemon)){
843                           if($action == "start" || $action == "stop"){
844                             $app->log->caselog("/etc/rc.d/".$daemon." ".$action." &> /dev/null", $this->FILE, __LINE__);
845                           } else {
846                             $app->log->caselog("/etc/rc.d/".$daemon." stop &> /dev/null", $this->FILE, __LINE__);
847                             sleep(3);
848                             $app->log->caselog("/etc/rc.d/".$daemon." start &> /dev/null", $this->FILE, __LINE__);
849                           }
850                     }
851                   }
852             }
853           }
854     }
855     
856     function netmask($netmask){
857       list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
858       $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);
859       $parts = explode("0", $bin);
860       $bin = str_pad($parts[0], 32, "0", STR_PAD_RIGHT);
861       $bin = wordwrap($bin, 8, ".", 1);
862       list($f1,$f2,$f3,$f4) = explode(".", trim($bin));
863       return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
864     }
865     
866     function binary_netmask($netmask){
867       list($f1,$f2,$f3,$f4) = explode(".", trim($netmask));
868       $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);
869       $parts = explode("0", $bin);
870       return substr_count($parts[0], "1");
871     }
872     
873     function network($ip, $netmask){
874       $netmask = $this->netmask($netmask);
875       list($f1,$f2,$f3,$f4) = explode(".", $netmask);
876       $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);
877       list($f1,$f2,$f3,$f4) = explode(".", $ip);
878       $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);
879       for($i=0;$i<32;$i++){
880         $network_bin .= substr($netmask_bin,$i,1) * substr($ip_bin,$i,1);
881       }
882       $network_bin = wordwrap($network_bin, 8, ".", 1);
883       list($f1,$f2,$f3,$f4) = explode(".", trim($network_bin));
884       return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
885     }
886     
887     /**
888      * Make a broadcast address from an IP number in combination with netmask
889      *
890      */
891     function broadcast($ip, $netmask){
892         $netmask = $this->netmask($netmask);
893           $binary_netmask = $this->binary_netmask($netmask);
894           list($f1,$f2,$f3,$f4) = explode(".", $ip);
895           $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);
896           $broadcast_bin = str_pad(substr($ip_bin, 0, $binary_netmask),32,"1",STR_PAD_RIGHT);
897           $broadcast_bin = wordwrap($broadcast_bin, 8, ".", 1);
898           list($f1,$f2,$f3,$f4) = explode(".", trim($broadcast_bin));
899           return bindec($f1).".".bindec($f2).".".bindec($f3).".".bindec($f4);
900     }
901     
902     /**
903      * Get the network address information
904      *
905      */
906     function network_info(){
907         $dist = $this->server_conf["dist"];
908           ob_start();
909           passthru("ifconfig");
910           $output = ob_get_contents();
911           ob_end_clean();
912           $lines = explode("\n", $output);
913           foreach($lines as $line){
914             $elms = explode(" ", $line);
915             if(trim($elms[0]) != "" && substr($elms[0],0,1) != "\t"){
916                   $elms[0] = trim($elms[0]);
917                   if(strstr($dist, "freebsd")) $elms[0] = substr($elms[0],0,-1);
918                   $interfaces[] = $elms[0];
919             }
920           }
921           if(!empty($interfaces)){
922             foreach($interfaces as $interface){
923                 ob_start();
924                   if(!strstr($dist, "freebsd")){
925                     passthru("ifconfig ".$interface." | grep -iw 'inet' | cut -f2 -d: | cut -f1 -d' '");
926                   } else {
927                     passthru("ifconfig ".$interface." | grep -iw 'inet' | grep -iv 'inet6' | cut -f2 -d' '");
928                   }
929                   $output = trim(ob_get_contents());
930                   ob_end_clean();
931                   if($output != ""){
932                     $ifconfig["INTERFACE"][$interface] = $output;
933                     $ifconfig["IP"][$output] = $interface;
934                   }
935             }
936             if(!empty($ifconfig)){
937                   return $ifconfig;
938             } else {
939                   return false;
940             }
941           } else {
942             return false;
943           }
944     }
945     
946     /**
947      * Configure the network settings from the system
948      *
949      */
950     function network_config(){
951         $ifconfig = $this->network_info();
952           if($ifconfig){
953             $main_interface = $ifconfig["IP"][$this->server_conf["server_ip"]];
954             if(strstr($main_interface, ":")){
955                   $parts = explode(":", $main_interface);
956                   $main_interface = trim($parts[0]);
957             }
958             if($main_interface != ""){
959                   $ips = $this->data["isp_server_ip"];
960                   if(!empty($ips)){
961                     foreach($ips as $ip){
962                           if(!isset($ifconfig["IP"][$ip["server_ip"]])){
963                             $to_set[] = $ip["server_ip"];
964                           } else {
965                             unset($ifconfig["IP"][$ip["server_ip"]]);
966                           }
967                     }
968                     if(!empty($ifconfig["IP"])){
969                           foreach($ifconfig["IP"] as $key => $val){
970                             if(!strstr($val, "lo") && !strstr($val, "lp") && strstr($val, $main_interface)){
971                                   exec("ifconfig ".$val." down &> /dev/null");
972                                   unset($ifconfig["INTERFACE"][$val]);
973                             }
974                           }
975                     }
976                     if(!empty($to_set)){
977                          foreach($to_set as $to){
978                                $i = 0;
979                                while($i >= 0){
980                                  if(isset($ifconfig["INTERFACE"][$main_interface.":".$i])){
981                                        $i++;
982                                  } else {
983                                        $new_interface = $main_interface.":".$i;
984                                        $i = -1;
985                                  }
986                                }
987                                exec("ifconfig ".$new_interface." ".$to." netmask ".$this->server_conf["server_netzmaske"]." up &> /dev/null");
988                                $ifconfig["INTERFACE"][$new_interface] = $to;
989                           }
990                     }
991                   }
992             }
993           }
994     }
995     
996     function quota_dirs(){
997       global $app;
998       $content = $app->file->unix_nl($app->file->no_comments("/etc/fstab"));
999       $lines = explode("\n", $content);
1000       foreach($lines as $line){
1001         $line = trim($line);
1002         if($line != ""){
1003           $elms = explode("\t", $line);
1004           foreach($elms as $elm){
1005             if(trim($elm) != "") $f[] = $elm;
1006           }
1007           if(!empty($f) && stristr($f[3], "userquota") && stristr($f[3], "groupquota")){
1008             $q_dirs[] = trim($f[1]);
1009           }
1010           unset($f);
1011         }
1012       }
1013       if(!empty($q_dirs)){
1014         return $q_dirs;
1015       } else {
1016         return false;
1017       }
1018     }
1019     
1020     /**
1021      * Scan the trash for virusses infection
1022      *
1023      */
1024     function make_trashscan(){
1025         global $app;
1026           //trashscan erstellen
1027           // Template Ã–ffnen
1028           $app->tpl->clear_all();
1029           $app->tpl->define( array(table    => "trashscan.master"));
1030     
1031           if(!isset($this->server_conf["virusadmin"]) || trim($this->server_conf["virusadmin"]) == "") $this->server_conf["virusadmin"] = "admispconfig@localhost";
1032           if(substr($this->server_conf["virusadmin"],0,1) == "#"){
1033             $notify = "no";
1034           } else {
1035             $notify = "yes";
1036           }
1037     
1038           // Variablen zuweisen
1039           $app->tpl->assign( array(VIRUSADMIN => $this->server_conf["virusadmin"],
1040                                NOTIFICATION => $notify));
1041     
1042           $app->tpl->parse(TABLE, table);
1043     
1044           $trashscan_text = $app->tpl->fetch();
1045     
1046           $datei = "/home/admispconfig/ispconfig/tools/clamav/bin/trashscan";
1047           $app->file->wf($datei, $trashscan_text);
1048     
1049           exec("chown admispconfig:admispconfig $datei &> /dev/null");
1050           exec("chmod 755 $datei");
1051     }
1052     
1053     /**
1054      * Get the current time
1055      *
1056      */
1057     function get_time(){
1058       $addr = "http://www.ispconfig.org/";
1059       $timeout = 1;
1060       $url_parts = parse_url($addr);
1061       $path = $url_parts["path"];
1062       $port = 80;
1063       $urlHandle = @fsockopen($url_parts["host"], $port, $errno, $errstr, $timeout);
1064       if ($urlHandle){
1065         socket_set_timeout($urlHandle, $timeout);
1066     
1067         $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";
1068         if ($user) $urlString .= "Authorization: Basic ".base64_encode("$user:$pass")."\r\n";
1069         $urlString .= "\r\n";
1070         fputs($urlHandle, $urlString);
1071     
1072         $month["Jan"] = "01";
1073         $month["Feb"] = "02";
1074         $month["Mar"] = "03";
1075         $month["Apr"] = "04";
1076         $month["May"] = "05";
1077         $month["Jun"] = "06";
1078         $month["Jul"] = "07";
1079         $month["Aug"] = "08";
1080         $month["Sep"] = "09";
1081         $month["Oct"] = "10";
1082         $month["Nov"] = "11";
1083         $month["Dec"] = "12";
1084         $c = 0;
1085         $l = 0;
1086         $startzeit = time();
1087         while(!feof($urlHandle) && $c < 2 && $l == 0){
1088           $line = trim(fgets($urlHandle,128));
1089           $response .= $line;
1090           $c = time() - $startzeit;
1091           if($line == "" || substr($line, 0, 5) == "Date:") $l += 1; // nur den Header auslesen
1092           if(substr($line, 0, 5) == "Date:"){
1093             $parts = explode(" ", $line);
1094             $tag = $parts[2];
1095             $monat = $month[$parts[3]];
1096             $jahr = $parts[4];
1097             list($stunde, $minute, $sekunde) = explode(":", $parts[5]);
1098             $timestamp = mktime($stunde,$minute,$sekunde,$monat,$tag,$jahr);
1099           }
1100         }
1101     
1102         @fclose($urlHandle);
1103     
1104         return $timestamp;
1105       } else {
1106         @fclose($urlHandle);
1107         return false;
1108       }
1109     }
e2d6ed 1110
T 1111 }
1112 ?>