Code

Fixed errors that appear to happen when specifiying a non existant DN and
[gosa.git] / include / setup_checks.inc
1 <?php
3 function check_schema_version($description, $version)
4 {
5   $desc= preg_replace("/^.* DESC\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $description);
7   return preg_match("/\(v$version\)/", $desc);
8 }
11 function view_schema_check($table)
12 {
13   $message="<table class=\"check\">";
15   foreach ($table as $key => $values){
16     $msg = $values['msg'];
17     $message.= "<tr><td class=\"check\">$msg";
19     if($values['status']) {
20       $message.="</td><td style='text-align:center' >
21         <img src=images/true.png alt='true' /></td></tr>";
22     } else {
23       $message.="</td><td style='text-align:center' >
24         <img src=images/button_cancel.png alt='false' /></td></tr>";
25     }
26   }
27   $message.="</table>";
28   
29   return $message;
30 }
33 function schema_check($server, $admin, $password, $aff=0)
34 {
35   global $config;
37   $messages= array();
38   $required_classes= array(
39       "gosaObject"            => array("version" => "2.4"),
40       "gosaAccount"           => array("version" => "2.4"),
41       "gosaLockEntry"         => array("version" => "2.4"),
42       "gosaCacheEntry"        => array("version" => "2.4"),
43       "gosaDepartment"        => array("version" => "2.4"),
45       "goFaxAccount"          => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
46       "goFaxSBlock"           => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
47       "goFaxRBlock"           => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
49       "gosaUserTemplate"      => array("version" => "2.4", "class" => "posixAccount","file" => "nis.schema"),
50       "gosaMailAccount"       => array("version" => "2.4", "class" => "mailAccount","file" => "gosa+samba3.schema"),
51       "gosaProxyAccount"      => array("version" => "2.4", "class" => "proxyAccount","file" => "gosa+samba3.schema"),
52       "gosaApplication"       => array("version" => "2.4", "class" => "appgroup","file" => "gosa.schema"),
53       "gosaApplicationGroup"  => array("version" => "2.4", "class" => "appgroup","file" => "gosa.schema"),
55       "GOhard"                => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
56       "gotoTerminal"          => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
57       "goServer"              => array("version" => "2.4","class" => "server","file" => "goserver.schema"),
58       "goTerminalServer"      => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
59       "goNfsServer"           => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
60       "goNtpServer"           => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
61       "goSyslogServer"        => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
62       "goLdapServer"          => array("version" => "2.4"),
63       "goCupsServer"          => array("version" => "2.4", "class" => array("posixAccount", "terminals"),),
64       "goImapServer"          => array("version" => "2.4", "class" => array("mailAccount", "mailgroup"),"file" => "gosa+samba3.schema"),
65       "goKrbServer"           => array("version" => "2.4"),
66       "goFaxServer"           => array("version" => "2.4", "class" => "gofaxAccount","file" => "gofax.schema"),
67       );
69   /* Build LDAP connection */
70   $ds= ldap_connect ($server);
71   if (!$ds) {
72     return (array(array("msg" => _("Can't bind to LDAP. No schema check possible!"), "status" => FALSE)));
73   }
74   ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
75   $r= ldap_bind ($ds, $admin, $password);
77   /* Get base to look for schema */
78   $sr  = @ldap_read ($ds, "", "objectClass=*", array("subschemaSubentry"));
79   $attr= @ldap_get_entries($ds,$sr);
80   if (!isset($attr[0]['subschemasubentry'][0])){
81     return (array(array("msg" => _("Can't get schema information from server. No schema check possible!"), "status" => FALSE)));
82   }
84   /* Get list of objectclasses */
85   $nb= $attr[0]['subschemasubentry'][0];
86   $objectclasses= array();
87   $sr= ldap_read ($ds, $nb, "objectClass=*", array("objectclasses"));
88   $attrs= ldap_get_entries($ds,$sr);
89   if (!isset($attrs[0])){
90     return (array(array("msg" => _("Can't get schema information from server. No schema check possible!"), "status" => FALSE)));
91   }
92   foreach ($attrs[0]['objectclasses'] as $val){
93     $name= preg_replace("/^.* NAME\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $val);
94     if ($name != $val){
95       $objectclasses[$name]= $val;
96     }
97   }
98   /* Walk through objectclasses and check if they are needed or not */
99   foreach ($required_classes as $key => $value){
100     if (isset($value['class'])){
101       if (!is_array($value['class'])){
102         $classes= array($value['class']);
103       } else {
104         $classes= $value['class'];
105       }
107       /* Check if we are using the class that requires */
108       foreach($classes as $class){
109         if (!isset($objectclasses[$key])){
110           $messages[$key]['msg']= sprintf(_("Optional objectclass '%s' required by plugin '%s' is not present in LDAP setup"), $key, $class);
111           $messages[$key]['status'] = FALSE;
112         } else {
113           if (!check_schema_version($objectclasses[$key], $value['version'])){
114             $messages[$key]['msg']= sprintf(_("Optional objectclass '%s' required by plugin '%s' does not have version %s"), $key, $class, $value['version']);
115             $messages[$key]['status'] =FALSE;
116           }else {
117             if(!isset($affich2[$class])){
118               $affich2[$class]['msg']   = sprintf(_("Support for '%s' enabled"), $class)."<td class=\"check\"> ".$value['file']."</td>";
119               $affich2[$class]['status']= TRUE; 
120             }
121           }
122         }
124       }
125     } else {
126       /* Required class */
127       if (!isset($objectclasses[$key])){
128         $messages[$key]['msg']= sprintf(_("Required objectclass '%s' is not present in LDAP setup"), $key);
129         $messages[$key]['status'] = FALSE;  
130       } else {
131         if (!check_schema_version($objectclasses[$key], $value['version'])){
132           $messages[$key]['msg']= sprintf(_("Required objectclass '%s' does not have version %s"), $key, $value['version']);
133           $messages[$key]['status'] = FALSE;  
134         }
135       }
136     }
137   }
139   /* Check for correct samba parameters */
140   if (!isset($objectclasses['sambaSamAccount'])){
141     $messages['samba3']['msg']= _("SAMBA 3 support disabled, no schema seems to be installed");
142     $affich['samba3']['msg']= $messages['samba3']['msg']."<td class=\"check\">gosa+samba3.schema</td>";
143     $messages['samba3']['status']= FALSE;
144     $affich['samba3']['status']= FALSE;
145   }else{
146     $affich['samba3']['msg']= _("SAMBA 3 support enabled")."<td class=\"check\">gosa+samba3.schema</td>";
147     $affich['samba3']['status']= TRUE;
148   }
150   if (!isset($objectclasses['sambaAccount'])){
151     $messages['samba2']['msg']= _("SAMBA 2 support disabled, no schema seems to be installed");
152     $affich['samba2']['msg']= $messages['samba2']['msg']."<td class=\"check\">samba.schema</td>";
153     $messages['samba2']['status']= FALSE;
154     $affich['samba2']['status']= FALSE;
155   }else{
156     $affich['samba2']['msg']= _("SAMBA 2 support enabled")."<td class=\"check\">samba.schema</td>";
157     $affich['samba2']['status']= TRUE;
158   }
160   /* Check pureftp/dns/ */
161   if (!isset($objectclasses['PureFTPdUser'])){
162     $messages['pureftp']['msg']= _("Support for pureftp disabled, no schema seems to be installed");
163     $affich['pureftp']['msg']= $messages['pureftp']['msg']."<td class=\"check\">pureftpd.schema</td>";
164     $messages['pureftp']['status']= FALSE;
165     $affich['pureftp']['status']= FALSE;
166   }else{
167     $affich['pureftp']['msg']= _("Support for pureftp enabled")."<td class=\"check\">pureftpd.schema</td>";
168     $affich['pureftp']['status']= TRUE;
169   }
171   if (!isset($objectclasses['gosaWebdavAccount'])){
172     $messages['webdav']['msg']= _("Support for WebDAV disabled, no schema seems to be installed");
173     $affich['webdav']['msg']= $messages['webdav']['msg']."<td class=\"check\"></td>";
174     $messages['webdav']['status']= FALSE;
175     $affich['webdav']['status']= FALSE;
176   }else{
177     $affich['webdav']['msg']=_("Support for WebDAV enabled")."<td class=\"check\">gosa+samba3.schema</td>";
178     $affich['webdav']['status']= TRUE;
179   }
181   if (!isset($objectclasses['phpgwAccount'])){
182     $messages['phpgroupware']['msg']= _("Support for phpgroupware disabled, no schema seems to be installed");
183     $affich['phpgroupware']['msg']= $messages['phpgroupware']['msg']."<td class=\"check\">phpgwaccount.schema</td>";
184     $messages['phpgroupware']['status']= FALSE;
185     $affich['phpgroupware']['status']= FALSE;
186   }else{
187     $affich['phpgroupware']['msg']= _("Support for phpgroupware enabled")."<td class=\"check\">phpgwaccount.schema</td>";
188     $affich['phpgroupware']['status']= TRUE;
189   }
191   if (!isset($objectclasses['goFonAccount'])){
192     $messages['phoneaccount']['msg']= _("Support for gofon disabled, no schema seems to be installed");
193     $affich['phoneaccount']['msg']= $messages['phoneaccount']['msg']."<td class=\"check\">gofon.schema</td>";
194     $messages['phoneaccount']['status']= FALSE;
195     $affich['phoneaccount']['status']= FALSE;
196   }else{
197     $affich['phoneaccount']['msg']= _("Support for gofon enabled")."<td class=\"check\">gofon.schema</td>";
198     $affich['phoneaccount']['status']= true;
199   }
201   if(($_SESSION['ldapconf']['mail_methods'][$_SESSION['ldapconf']['mail']] == "kolab")){
202     if(!isset($objectclasses['kolabInetOrgPerson']))
203     {
204       $messages['kolab']['msg']= _("Support for Kolab disabled, no schema seems to be installed, setting mail-method to cyrus");
205       $affich['kolab']['msg']=$messages['kolab']['msg']."<td class=\"check\">kolab2.schema</td>";
207       $tmp= array_flip($_SESSION['ldapconf']['mail_methods']);
208       $_SESSION['ldapconf']['mail']=$tmp['cyrus'];
209       $messages['kolab']['status']= FALSE;
210       $affich['kolab']['status']= FALSE;
211     }else{
212       $affich['kolab']['msg']=_("Support for Kolab enabled")."<td class=\"check\">gofon.schema</td>";
213       $affich['kolab']['status']= TRUE;
214     }
215   }
216   if($aff==0){
217     return ($messages);
218   } else {
219     return(array_merge($affich,$affich2));
220   }
224 function check(&$faults, $message, $description, $test, $required= TRUE)
226   $msg= "<table class='check'><tr><td class='check' style='font-size:14px;'>$message</td>
227     <td rowspan=2 style='vertical-align:middle; text-align:center;width:45px;'>";
228   if ($test){
229     $msg.= _("OK")."<br>";
230   } else {
231     if (!$required){
232       $msg.="<font color=red>"._("Ignored")."</font><br>";
233     } else {
234       $msg.="<font color=red>"._("Failed")."</font><br>";
235       $faults++;
236     }
237   }
238   $msg.= "</td></tr><tr><td class='check' style='padding-left:20px;".
239          "background-color:#F0F0F0;'>$description</td></tr></table><br>";
241   return $msg;
244 function perform_php_checks(&$faults)
246   global $check_globals;
248   $faults= 0;
249   $msg= "";
251   $msg.= "<h1>"._("PHP setup inspection")."</h1>";
252   $msg.= check (        $faults, _("Checking for PHP version (>=4.1.0)"),
253       _("PHP must be of version 4.1.0 or above for some functions and known bugs in PHP language."),
254       version_compare(phpversion(), "4.1.0")>=0);
256   $msg.= check (        $faults, _("Checking if register_globals is set to 'off'"),
257       _("register_globals is a PHP mechanism to register all global varibales to be accessible from scripts without changing the scope. This may be a security risk. GOsa will run in both modes."),
258       $check_globals == 0, FALSE);
260   $msg.= check (        $faults, _("Checking for ldap module"),
261       _("This is the main module used by GOsa and therefore really required."),
262       function_exists('ldap_bind'));
264   $msg.= check (        $faults, _("Checking for gettext support"),
265       _("Gettext support is required for internationalized GOsa."), function_exists('bindtextdomain'));
267   $msg.= check (        $faults, _("Checking for iconv support"),
268       _("This module is used by GOsa to convert samba munged dial informations and is therefore required."),
269       function_exists('iconv'));
271   $msg.= check (        $faults, _("Checking for mhash module"),
272       _("To use SSHA encryption, you'll need this module. If you are just using crypt or md5 encryption, ignore this message. GOsa will run without it."),
273       function_exists('mhash'), FALSE);
275   $msg.= check (        $faults, _("Checking for imap module"),
276       _("The IMAP module is needed to communicate with the IMAP server. It gets status informations, creates and deletes mail users."),
277       function_exists('imap_open'));
278   $msg.= check (        $faults, _("Checking for getacl in imap"),
279       _("The getacl support is needed for shared folder permissions. The standard IMAP module is not capable of reading acl's. You need a recend PHP version for this feature."),
280       function_exists('imap_getacl'), FALSE);
281   $msg.= check (        $faults, _("Checking for mysql module"),
282       _("MySQL support is needed for reading GOfax reports from databases."),
283       function_exists('mysql_query'), FALSE);
284   $msg.= check (        $faults, _("Checking for cups module"),
285       _("In order to read available printers from IPP protocol instead of printcap files, you've to install the CUPS module."),
286       function_exists('cups_get_dest_list'), FALSE);
287   $msg.= check (        $faults, _("Checking for kadm5 module"),
288       _("Managing users in kerberos requires the kadm5 module which is downloadable via PEAR network."),
289       function_exists('kadm5_init_with_password'), FALSE);
290   return ($msg);
294 function perform_additional_checks(&$faults)
296   /* Programm check */
297   $msg= "<h1>"._("Checking for some additional programms")."</h1>";
299   /* Image Magick */
300   $query= "LC_ALL=C LANG=C convert -help";
301   $output= shell_exec ($query);
302   if ($output != ""){
303     $lines= split ("\n", $output);
304     $version= preg_replace ("/^Version:[^I]+ImageMagick ([^\s]+).*/", "\\1", $lines[0]);
305     list($major, $minor)= split("\.", $version);
306     $msg.= check (      $faults, _("Checking for ImageMagick (>=5.4.0)"),
307         _("ImageMagick is used to convert user supplied images to fit the suggested size and the unified JPEG format."),
308         ($major > 5 || ($major == 5 && $minor >= 4)));
309   } else {
310     $msg.= check (      $faults, _("Checking imagick module for PHP"),
311         _("Imagick is used to convert user supplied images to fit the suggested size and the unified JPEG format from PHP script."), function_exists('imagick_blob2image'), TRUE);
312   }
314   /* Check for fping */
315   $query= "LC_ALL=C LANG=C fping -v 2>&1";
316   $output= shell_exec ($query);
317   $have_fping= preg_match("/^fping:/", $output);
318   $msg.= check (        $faults, _("Checking for fping utility"),
319       _("The fping utility is only used if you've got a thin client based terminal environment running."),
320       $have_fping, FALSE);
322   /* Check for smb hash generation tool */
323   $query= "mkntpwd 2>&1";
324   $output= shell_exec ($query);
325   $have_mkntpwd= preg_match("/^Usage: mkntpwd /", $output);
326   $alt = 0;
328   if (!$have_mkntpwd){
329     $query= "LC_ALL=C LANG=C perl -MCrypt::SmbHash -e 'ntlmgen \"PASSWD\", \$lm, \$nt; print \"\${lm}:\${nt}\\n\";' &>/dev/null";
330     system ($query, $ret);
331     $alt= ($ret == 0);
332   }
334   $msg.= check (        $faults, _("Checking for a way to generate LM/NT password hashes"),
335       _("In order to use SAMBA 2/3, you've to install some additional packages to generate password hashes."),
336       ($have_mkntpwd || $alt));
338   /* seesio.auto_start should be off, in order to without trouble*/
339   $arra = ini_get_all();
341   $session_auto_start = $arra['session.auto_start'];
342   $implicit_flush     = $arra['implicit_flush'];
343   $max_execution_time = $arra['max_execution_time'];
344   $memory_limit       = $arra['memory_limit'];
345   $expose_php         = $arra['expose_php'];
346   $magic_quotes_gpc   = $arra['magic_quotes_gpc'];
347   $register_globals   = $arra['register_globals'];
349   /* auto_register */
350   $msg.= check (  $faults, _("php.ini check -> session.auto_register"),
351       _("In Order to use GOsa without any trouble, the session.auto_register option in your php.ini must be set to 'Off'."), (!$session_auto_start['local_value']));
353   /* implicit_flush */
354   $msg.= check (  $faults, _("php.ini check -> implicit_flush"),
355       _("This option influences the Output handling. Turn this Option off, to increase performance."),
356       !$implicit_flush['local_value'],0,false);
358   /* max_execution_time */
359   if($max_execution_time['local_value'] < 30 ){
360     $max_execution_time['local_value']=false;
361   }
362   $msg.= check (  $faults, _("php.ini check -> max_execution_time"),
363       _("The Execution time should be at least 30 seconds, because some actions may consume more time."),
364       $max_execution_time['local_value'],0,false);
366   /* memory_limit */
367   if($memory_limit['local_value'] < 16 ){
368     $memory_limit['local_value']=false;
369   }
370   $msg.= check (  $faults, _("php.ini check -> memory_limit"),
371       _("GOsa needs at least 16MB of memory, less will cause unpredictable errors! Increase it for larger setups."),
372       !$implicit_flush['local_value'],0,false);
374   /* expose_php */
375   $msg.= check (  $faults, _("php.ini check -> expose_php"),
376       _("Increase the server security by setting expose_php to 'off'. PHP won't send any Information about the server you are running in this case."),
377       !$implicit_flush['local_value'],0,false);
379   /* magic_quotes_gpc */
380   $msg.= check (  $faults, _("php.ini check -> magic_quotes_gpc"),
381       _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."),
382       $magic_quotes_gpc['local_value'],0,false);
384   return $msg;
388 function parse_contrib_conf()
390   $str                = "";
391   $used_samba_version = 0;
392   $query              = ""; 
393   $fp                 = false;
394   $output             = "";
395   $needridbase_sid    = false;
396   $pwdhash            = "";
397   $replacements       = array();
398   $ldapconf           = $_SESSION['ldapconf']; // The Installation information
399   $classes            = $_SESSION['classes'];  // Class information needed to define which features are enabled
400   $possible_plugins   = array();
402   /* Which samba version do we use? */
403   if(isset($classes['samba3'])){
404     $used_samba_version = 2;
405   } else {
406     $used_samba_version = 3;
407   }
409   /* Look for samba password generation method */
410   if(file_exists("/usr/lib/gosa/mkntpasswd")){
411     $pwdhash  = "/usr/lib/gosa/mkntpasswd";
412   } elseif (preg_match("/^Usage: mkntpwd /", shell_exec ("mkntpwd 2>&1"))){
413     $pwdhash= "mkntpwd";
414   } else {
415     $pwdhash=('perl -MCrypt::SmbHash -e "ntlmgen \"\$ARGV[0]\", \$lm, \$nt; print \"\${lm}:\${nt}\n\";" $1');
416   }
418   /* Define which variables will be replaced */
419   $replacements['{LOCATIONNAME}']  = $ldapconf['location'];
420   $replacements['{SAMBAVERSION}']  = $used_samba_version;
421   $replacements['{LDAPBASE}']      = $ldapconf['base'];
422   $replacements['{LDAPADMIN}']     = $ldapconf['admin'];
423   $replacements['{DNMODE}']        = $ldapconf['peopledn'];
424   $replacements['{LDAPHOST}']      = $ldapconf['uri'];
425   $replacements['{PASSWORD}']      = $ldapconf['password'];
426   $replacements['{CRYPT}']         = $ldapconf['arr_cryptkeys'][$ldapconf['arr_crypts']];
427   $replacements['{SID}\n']         = "";
428   $replacements['{RIDBASE}\n']     = "";
429   $replacements['{MAILMETHOD}']    = $ldapconf['mail_methods'][$ldapconf['mail']];
430   $replacements['{SMBHASH}']       = $pwdhash;
431   $replacements['{GOVERNMENTMODE}']= "false"; 
432   $replacements['{kolabAccount}']  = "";
433   $replacements['{servKolab}']     = "";
435   /* This array contains all preg_replace syntax to delete all unused plugins
436      THE kEY MUST BE THE CLASSNAME so we can check it with $ldapconf['classes'] */
437   $possible_plugins['fonreport'][]   = "'\n.*<plugin.*fonreport+.*\n.*>.*\n'i";
438   $possible_plugins['phoneaccount'][]= "'\n.*<tab.*phoneAccount.*>.*\n'i";
439   $possible_plugins['logview'][]     = "'\n.*<plugin.*logview+.*\n.*>.*\n'i";
440   $possible_plugins['pureftp'][]     = "'\n.*<tab.*pureftp.*>.*\n'i";
441   $possible_plugins['webdav'][]      = "'\n.*<tab.*webdav.*>.*\n'i";
442   $possible_plugins['phpgroupware'][]= "'\n.*<tab.*phpgroupware.*>.*\n'i";
444   /* Header information
445      Needed to send the generated gosa.conf to the browser */
446   header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
447   header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
448   header("Cache-Control: no-cache");
449   header("Pragma: no-cache");
450   header("Cache-Control: post-check=0, pre-check=0");
451   header("Content-type: text/plain");
453   if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
454       preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
455     header('Content-Disposition: filename="gosa.conf"');
456   } else {
457     header('Content-Disposition: attachment; filename="gosa.conf"');
458   }
460   if(!$fp=fopen(CONFIG_TEMPLATE_DIR."/gosa.conf","r")) {
461     echo "Can't open file ".CONFIG_TEMPLATE_DIR."/gosa.conf";
462   } else {
463     while(!feof($fp)) {
464       $str.= fread($fp,512);
465     }
467     if($ldapconf['mail_methods'][$ldapconf['mail']]=="kolab") {
468       $replacements['{kolabAccount}']  ="<tab class=\"kolabAccount\" />\n     ";
469       $replacements['{servKolab}']     ="<tab class=\"servkolab\" name=\"Kolab\" />";
470     }
472     if($used_samba_version == 2) {
473       /* Do nothing for samba 2... */
474     } else {
475       /* Create LDAP connection, to check if there's a domain
476          object defined in the LDAP schema */
477       $ldap= new LDAP($ldapconf['admin'], $ldapconf['password'], $ldapconf['uri']);
479       /* Try to find a Samba Domain Objekt */
480       $ldap->search("(objectClass=sambaDomain)");
482       /* Something found ??? so we need to define ridbase an SID by ourselfs */
483       if($ldap->count()< 1) {
484         $replacements['{SID}']= "sid=\"123412-11\"";
485         $replacements['{RIDBASE}']= "ridbase=\"1000\"";  
486       } 
487     }
489     /* Data readed, types replaced, samba version detected and checked if
490        we need to add SID and RIDBASE. Check if there is an ivbbEntry in
491        the LDAP tree, in this case we will set the governmentmode to true.
492        Create LDAP connection, to check if theres a domain Objekt definen
493        in the LDAP schema. */
494     if(!isset($ldap)){
495       $ldap= new LDAP($ldapconf['admin'], $ldapconf['password'], $ldapconf['uri']);
496     }
498     /* Try to find a Samba Domain Objekt */
499     $ldap->search("(objectClass=ivbbEntry)");
501     /* Something found ??? so we need to define ridbase an SID by ourselfs */
502     if($ldap->count()> 0) {
503       $replacements['{GOVERNMENTMODE}']= "true";
504     }
506     /* Replace all colleted information with placeholder */
507     foreach($replacements as $key => $val) {
508       $str = preg_replace("/".$key."/",$val,$str);
509     }    
511     /* Remove all unused plugins */
512     foreach($possible_plugins as $plugin) {
513       foreach($plugin as $key=>$val) {
514         if(in_array($plugin,$classes)) {
515           $str = preg_replace($val,"\n",$str);
516         }
517       }
518     }
519   }
521   return ((($str)));
525 /* Show setup_page 1 */
526 function show_setup_page1($withoutput = true)
528   $smarty = get_smarty();  
529   $smarty->assign ("content", get_template_path('setup_introduction.tpl'));
530   $smarty->assign ("tests", perform_php_checks($faults));
532   /* This var is true if anything went wrong */
533   if ($faults){
534     $smarty->assign("mode", "disabled");
535   }
537   /* This line displays the template only if (withoutput is set) */
538   if($withoutput){
539     $smarty->display (get_template_path('headers.tpl'));
540   }
542   if (isset($_SESSION['errors'])){
543     $smarty->assign("errors", $_SESSION['errors']);
544   }
546   if($withoutput){
547     $smarty->display (get_template_path('setup.tpl'));
548   }
550   return (!$faults);
554 /* Show setup_page 2 */
555 function show_setup_page2($withoutput = true)
557   $smarty = get_smarty();
558   $smarty->assign ("content", get_template_path('setup_step2.tpl'));
559   $smarty->assign ("tests", perform_additional_checks($faults));
561   if ($faults) {
562     $smarty->assign("mode", "disabled");
563   }
564   if($withoutput){
565     $smarty->display (get_template_path('headers.tpl'));
566   }
567   if (isset($_SESSION['errors']))  {
568     $smarty->assign("errors", $_SESSION['errors']);
569   }
570   if($withoutput){
571     $smarty->display (get_template_path('setup.tpl'));
572   }
574   return (!$faults);                               
578 function show_setup_page3($withoutput = true)
580   $smarty = get_smarty();
582   /* Take the Post oder the Sessioin saved data */
583   if(isset($_POST['uri'])){
584     $uri = $_POST['uri'];
585   } elseif(isset($_SESSION['ldapconf']['uri'])){
586     $uri = $_SESSION['ldapconf']['uri'];
587   }
589   /* If Page called first time, field is empty */
590   if((!isset($uri))||(empty($uri))){
591     $uri = "ldap://localhost:389";
592   }
594   /* if isset $uri save it to session */
595   if(isset($uri)) {
596     $_SESSION['ldapconf']['uri'] = $uri;
597     $smarty->assign ("uri", validate($uri));
598   }
600   /* No error till now */
601   $fault = false;
603   /* If we pushed the Button continue */
604   if(isset($_POST['continue3'])){
605     if(!isset($uri)) {
606       $fault = true;
607       
608       /* Output the Error */
609       if($withoutput) {
610         print_red (_("You've to specify an ldap server before continuing!"));
611         $smarty->assign ("content", get_template_path('setup_step3.tpl'));
612       }
613     }
614   } elseif (!$ds = @ldap_connect (validate($uri))) {
615     $fault =true;
616     
617     /* Output the Error */
618     if($withoutput) {
619       print_red (_("Can't connect to the specified LDAP server! Please make sure that is reachable for GOsa."));
620       $smarty->assign ("uri", validate($uri));
621       $smarty->assign ("content", get_template_path('setup_step3.tpl'));
622     }
623   } else {
624     /* Try to bind the connection */    
625     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
627     /* if we can't bind , print error */
628     if (!$r  =  @ldap_bind ($ds)) {
629       $fault = true;
630     
631       /* Output the Error */
632       if($withoutput) {
633         print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
634         $smarty->assign ("content", get_template_path('setup_step3.tpl'));
635         $smarty->assign ("uri", validate($uri));
636       }
637     } else {
638       $fault = false;
639     }
640   }
642   $smarty->assign ("content", get_template_path('setup_step3.tpl'));
644   /* Load Header */
645   if($withoutput){
646     $smarty->display (get_template_path('headers.tpl'));
647   }
649   /* Set Errors to Smarty */
650   if (isset($_SESSION['errors'])) {
651     $smarty->assign("errors", $_SESSION['errors']);
652   }
654   /* Print out Template */ 
655   if($withoutput){
656     $smarty->display (get_template_path('setup.tpl'));
657   }
659   return (!$fault);                             
663 function show_setup_page4($withoutput = true)
665   $smarty= get_smarty();      
667   if(!isset($_SESSION['ldapconf']['base'])){
668     $_SESSION['ldapconf']['base']= $base;
669   }
671   if(!isset($_SESSION['ldapconf']['base'])){
672     $_SESSION['ldapconf']['base']= $base;
673   }
674   require_once("class_password-methods.inc");
676   $fault     = false;              
677   $uri       = $_SESSION['ldapconf']['uri'];
678   $ldapconf  = $_SESSION['ldapconf'];
679   $arr_crypts= array();
680   $temp      = "";
681   $checkvars = array("location", "admin", "password", "peopleou", "base",
682                      "peopledn", "arr_crypts", "mail", "uidbase");
684   if(!isset($_SESSION['ldapconf']['arr_cryptkeys'])) {
685     require_once("class_password-methods.inc");
686     $tmp= passwordMethod::get_available_methods_if_not_loaded();
687     $_SESSION['ldapconf']['arr_cryptkeys']= $tmp['name'];
688   }
690   if(!isset($_SESSION['ldapconf']['mail_methods'])) {
691     $_SESSION['ldapconf']['mail_methods']=array();
692     $temp = get_available_mail_classes();
693     $_SESSION['ldapconf']['mail_methods']= $temp['name'];
694   }
696   /* If there are some empty vars in ldapconnect -
697      these values also represent out default values */
698   if(!$ds = @ldap_connect (validate($uri))){
699     $fault = true;
700     if($withoutput){
701       print_red (_("Can't connect to the specified LDAP server! Please make sure that is reachable for GOsa."));
702     }
703   } elseif(!@ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)){
704     $fault = true;
705     if($withoutput){
706       print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
707     }
708   } elseif(!$r= @ldap_bind ($ds)){
709     $fault = true;
710     if($withoutput){
711       print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
712     }
713   } else {
714     $sr=   @ldap_search ($ds, "", "objectClass=*", array("namingContexts"));
715     $attr= @ldap_get_entries($ds,$sr);
717     if((empty($attr))) {
718       $base= "dc=example,dc=net";
720       if($withoutput){
721         print_red(_("Bind to server successful, but the server seems to be completly empty, please check all informations twice"));
722       }
723       
724     } else {
725       $base= $attr[0]['dn'];
726     }
727   }
729   if(!isset($_SESSION['ldapconf']['base'])){
730     $_SESSION['ldapconf']['base']= $base;
731   }
732   if(!isset($_SESSION['ldapconf']['admin'])){
733     $_SESSION['ldapconf']['admin']= "cn=ldapadmin,".$base;
734   }
735   if(!isset($_SESSION['ldapconf']['peopleou'])){
736     $_SESSION['ldapconf']['peopleou']= "ou=people";
737   }
738   if(!isset($_SESSION['ldapconf']['groupou'])){
739     $_SESSION['ldapconf']['groupou']= "ou=groups";
740   }
741   if(!isset($_SESSION['ldapconf']['peopledn'])){
742     $_SESSION['ldapconf']['peopledn']= "cn";
743   }
744   if(!isset($_SESSION['ldapconf']['password'])){
745     $_SESSION['ldapconf']['password']= "";
746   }
747   if(!isset($_SESSION['ldapconf']['location'])){
748     $_SESSION['ldapconf']['location']= "Example";
749   }
750   if(!isset($_SESSION['ldapconf']['uidbase'])){
751     $_SESSION['ldapconf']['uidbase']= "1000";
752   }
753   if(!isset($_SESSION['ldapconf']['mail'])){
754     $_SESSION['ldapconf']['mail']= 0;
755   }
756   $tmp= array_flip($_SESSION['ldapconf']['arr_cryptkeys']);
757   if(!isset($_SESSION['ldapconf']['arr_crypts'])){
758     $_SESSION['ldapconf']['arr_crypts']   = $tmp['md5'];
759   }
761   /* check POST data */
762   if(isset($_POST['check'])) {
764     /* Check if all needed vars are submitted */
765     foreach($checkvars as $key) {
766       if($key == "peopleou"){
767         continue;
768       }
769       if($key == "groupou"){
770         continue;
771       }
773       if((isset($_POST[$key]))&&($_POST[$key]!="")) {
774         $_SESSION['ldapconf'][$key] = $_POST[$key];
775       } else {
776         if($withoutput) {
777           print_red(sprintf(_("You're missing the required attribute '%s' from this formular. Please complete!"), $key));
778         }
779         $fault = true;
780       }
781     }
782   }
784   /* Transfer base */
785   if(isset($_POST['base'])){
786     $_SESSION['ldapconf']['base']= $_POST['base'];
787   }
789   $smarty->assign("arr_cryptkeys",$_SESSION['ldapconf']['arr_cryptkeys']);
790   $smarty->assign("mail_methods", $_SESSION['ldapconf']['mail_methods']);
792   foreach($_SESSION['ldapconf'] as $key => $val) {
793     $smarty->assign($key,$val);
794   }
796   if(isset($_POST['check'])) {
797     $ldap= new LDAP($_SESSION['ldapconf']['admin'],
798                     $_SESSION['ldapconf']['password'],
799                     $_SESSION['ldapconf']['uri']);
801     $m= schema_check($_SESSION['ldapconf']['uri'],
802                     $_SESSION['ldapconf']['admin'],
803                     $_SESSION['ldapconf']['password']);
804     $_SESSION['classes']= $m;
806     if ($ldap->error != "Success") {
807       if($withoutput) {
808         print_red(sprintf(_("Can't log into LDAP server. Reason was: %s."), $ldap->get_error()));
809       }
810       $fault = true;
811     }
812   }
814   /* Set smarty output */
815   $smarty->assign ("content", get_template_path('setup_step4.tpl'));
816   $smarty->assign ("peopledns", array("cn", "uid"));
817   if($withoutput){
818     $smarty->display (get_template_path('headers.tpl'));
819   }
820   if(isset($_SESSION['errors'])) {
821     $smarty->assign("errors", $_SESSION['errors']);
822   }
823   if($withoutput){
824     $smarty->display (get_template_path('setup.tpl'));
825   }
827   return (!$fault);
831 function show_setup_page5($withoutput=true)
833   /* Get ldapconf */
834   $ldapconf= $_SESSION['ldapconf'];
836   /* get smarty */
837   $smarty = get_smarty();
839   if(isset($_SESSION['classes'])){
840     $classes = $_SESSION['classes'];
841   }
843   $info= posix_getgrgid(posix_getgid());
844   $smarty->assign("webgroup", $info['name']);
845   $smarty->assign("path", CONFIG_DIR);
846   $message= "<table class=\"check\">";
847   $m= schema_check($ldapconf['uri'], $ldapconf['admin'], $ldapconf['password'],1);
849   if($withoutput) {
850     $smarty->assign ("schemas", view_schema_check($m));
851     $smarty->assign ("content", get_template_path('setup_finish.tpl'));
852   }
853   
854   /* Output templates... */
855   if($withoutput){
856     $smarty->display (get_template_path('headers.tpl'));
857   }
858   if (isset($_SESSION['errors'])) {
859     $smarty->assign("errors", $_SESSION['errors']);
860   }
861   if($withoutput){
862     $smarty->display (get_template_path('setup.tpl'));
863   }
864   
865   return(true);
869 function create_user_for_setup($withoutput=true)
871   global $samba;
873   $ldapconf = $_SESSION['ldapconf'];
874   $smarty = get_smarty();
876   if(isset($_SESSION['classes'])){
877     $classes= $_SESSION['classes'];
878   }
880   /* Everything runns perfect ...
881      So we do a last test on this page
882      is there a user with ACLs :all which will be able to adminsitrate GOsa
883      We check that, if this user or group is missing we ask for creating them */
884   $ldap= new LDAP($_SESSION['ldapconf']['admin'],
885                   $_SESSION['ldapconf']['password'],
886                   $_SESSION['ldapconf']['uri']);
888   /* Now we are testing for a group, with the rights :all */
889   $ldap->cd($ldapconf['base']);
890   $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
892   $group_cnt  = $ldap->count();
893   $data       = $ldap->fetch();
894   $create_user= false;
896   /* We need to create administrative user and group
897      because theres no group found */
898   if($group_cnt < 1) {
899     
900     /* Set var to create user */
901     $create_user=   true;
903     /* Output error */
904     if(($withoutput)&&(!isset($_POST['new_admin']))){
905       print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
906     }
908   } else {
910     /* We found an Administrative Group, is there a user, too */
911     if(isset($data['memberUid'][0])) {
912       $ldap->search("(&(objectClass=gosaAccount)(objectClass=person))",array("uid=".$data['memberUid'][0]));
913       $data2   = $ldap->fetch();
914     }
916     /* We must create a user */
917     if (($ldap->count() < 1)||(!isset($data2))) {
918       $create_user = true;
919       if(($withoutput)&&(!isset($_POST['new_admin']))){
920         print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
921       }
922     } else {
923       /* We don't need to add a user */
924       return(true);
925     }
927   }
929   /* We need to create a new user with group */
930   if(isset($_POST['new_admin']))
931   {
932     /* Adjust password attributes according to the samba version */
933     if (isset($classes['samba3'])) {
934       $samba= "2";
935       $lmPassword = "lmPassword";
936       $ntPassword = "ntPassword";
937     } else {
938       $samba= "3";
939       $lmPassword = "sambaLMPassword";
940       $ntPassword = "sambaNtPassword";
941     }
943     /* Nothing submitted */
944     if(( (empty($_POST['admin_name']))||(empty($_POST['admin_pass'])) )&&(!$create_user)) {
945       return(true);
946     }
948     /* We have the order to create on Admin ^^
949        Detect Samba version to define the Attribute names shown below */
950     $ldap->cd($ldapconf['base']);
952     /* Define the user we are going to create */
953     $dn= "cn=".$_POST['admin_name'].",".$ldapconf['peopleou'].",".$ldapconf['base'];
954     $arr['objectClass'][0] ="person";
955     $arr['objectClass'][1] ="organizationalPerson";
956     $arr['objectClass'][2] ="inetOrgPerson";
957     $arr['objectClass'][3] ="gosaAccount";
958     $arr['uid']            = $_POST['admin_name'];
959     $arr['cn']             = $_POST['admin_name'];
960     $arr['sn']             = $_POST['admin_name'];
961     $arr['givenName']      = "GOsa main administrator";
962     $arr[$lmPassword]      = "10974C6EFC0AEE1917306D272A9441BB";
963     $arr[$ntPassword]      = "38F3951141D0F71A039CFA9D1EC06378";
964     $arr['userPassword']   = crypt_single($_POST['admin_pass'],"md5");
966     if( !$ldap->dn_exists ( $dn )){ 
967       $ldap->cd($dn); 
968       $ldap->create_missing_trees($dn);
969       $ldap->add($arr);
970       if($ldap->error!="Success"){
971         print_red("Can't create user, and / or Group, possibly this problem  depends on an empty LDAP server. Check your configuration and try again!");
972       }
973     }
975     /* There's already a group for administrator, so we only need to add the user */
976     if($group_cnt) {
978       if(!isset($data['memberUid'])) {
979         $arrr['memberUid']= $_POST['admin_name'];
980       } else {
981         $data['memberUid'][$data['memberUid']['count']]=$_POST['admin_name'];
982         $arrr['memberUid'] = $data['memberUid'];
983         unset($arrr['memberUid']['count']);
984       }
986       $ldap->cd($data['dn']);
987       $ldap->modify($arrr);
989     } else {
990       
991       $dn                    = "cn=administrators,".$ldapconf['groupou'].",".$ldapconf['base'];
992       $arrr['objectClass'][0]= "gosaObject";
993       $arrr['objectClass'][1]= "posixGroup";
994       $arrr['gosaSubtreeACL']= ":all";
995       $arrr['cn']            = "administrators";
996       $arrr['gidNumber']     = "999";
997       $arrr['memberUid']     = $_POST['admin_name'];
998       $ldap->cd($dn);
999       $ldap->add($arrr);
1000     }
1002     return(true);
1003   } else {
1005     if(!($create_user)) {
1006       $smarty->assign ("content", get_template_path('setup_useradmin.tpl'));
1007       $smarty->assign("exists",true);
1008     } else {
1009       $smarty->assign ("content", get_template_path('setup_useradmin.tpl'));
1010       $smarty->assign("exists",false);
1011     }
1013   }
1015   /* Smarty output */ 
1016   if($withoutput){
1017     $smarty->display (get_template_path('headers.tpl'));
1018   }
1019   if (isset($_SESSION['errors'])) {
1020     $smarty->assign("errors", $_SESSION['errors']);
1021   }
1022   if($withoutput){
1023     $smarty->display (get_template_path('setup.tpl'));
1024   }
1026   return(false);
1030 /* Returns the classnames auf the mail classes */
1031 function get_available_mail_classes()
1033   $dir = opendir( "../include");
1034   $methods = array();
1035   $suffix = "class_mail-methods-";
1036   $lensuf = strlen($suffix);
1037   $prefix = ".inc";
1038   $lenpre = strlen($prefix);
1040   $i = 0;
1041   while (($file = readdir($dir)) !== false){
1043     if(stristr($file,$suffix)) {
1044       $lenfile = strlen($file);
1045       $methods['name'][$i] = substr($file,$lensuf,($lenfile-$lensuf)-$lenpre);
1046       $methods['file'][$i] = $file;
1047       $methods[$i]['file'] = $file;
1048       $methods[$i]['name'] = substr($file,$lensuf,($lenfile-$lensuf)-$lenpre);
1049       $i++;
1050     }
1052   }
1053   
1054   return($methods);
1057 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1058 ?>