Code

Someone was so kind to believe that a 2.0 in a long list of 2.4 entries might be...
[gosa.git] / include / functions_setup.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 summary=\"\" 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>";
29   return $message;
30 }
33 function is_schema_readable($server, $admin, $password)
34 {
35   $ds= ldap_connect ($server);
36   if (!$ds) {
37     return (false);
38   }
39   ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
40   $r= ldap_bind ($ds, $admin, $password);
42   /* Get base to look for schema */
43   $sr  = @ldap_read ($ds, "", "objectClass=*", array("subschemaSubentry"));
44   $attr= @ldap_get_entries($ds,$sr);
45   if (!isset($attr[0]['subschemasubentry'][0])){
46     return (false);
47   }
49   $nb= $attr[0]['subschemasubentry'][0];
50   $objectclasses= array();
51   $sr= ldap_read ($ds, $nb, "objectClass=*", array("objectclasses"));
52   $attrs= ldap_get_entries($ds,$sr);
53   if (!isset($attrs[0])){
54     return (false);
55   }
56   return(true);
57
59 function schema_check($server, $admin, $password, $aff=0)
60 {
61   global $config;
63   $messages= array();
64   $required_classes= array(
65       "gosaObject"            => array("version" => "2.4"),
66       "gosaAccount"           => array("version" => "2.4"),
67       "gosaLockEntry"         => array("version" => "2.4"),
68       "gosaCacheEntry"        => array("version" => "2.4"),
69       "gosaDepartment"        => array("version" => "2.4"),
71       "goFaxAccount"          => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
72       "goFaxSBlock"           => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
73       "goFaxRBlock"           => array("version" => "1.0.4", "class" => "gofaxAccount","file" => "gofax.schema"),
75       "gosaUserTemplate"      => array("version" => "2.4", "class" => "posixAccount","file" => "nis.schema"),
76       "gosaMailAccount"       => array("version" => "2.4", "class" => "mailAccount","file" => "gosa+samba3.schema"),
77       "gosaProxyAccount"      => array("version" => "2.4", "class" => "proxyAccount","file" => "gosa+samba3.schema"),
78       "gosaApplication"       => array("version" => "2.4", "class" => "appgroup","file" => "gosa.schema"),
79       "gosaApplicationGroup"  => array("version" => "2.4", "class" => "appgroup","file" => "gosa.schema"),
81       "GOhard"                => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
82       "gotoTerminal"          => array("version" => "2.0", "class" => "terminals","file" => "goto.schema"),
83       "goServer"              => array("version" => "2.4","class" => "server","file" => "goserver.schema"),
84       "goTerminalServer"      => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
85       "goNfsServer"           => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
86       "goNtpServer"           => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
87       "goSyslogServer"        => array("version" => "2.4", "class" => "terminals","file" => "goto.schema"),
88       "goLdapServer"          => array("version" => "2.4"),
89       "goCupsServer"          => array("version" => "2.4", "class" => array("posixAccount", "terminals"),),
90       "goImapServer"          => array("version" => "2.4", "class" => array("mailAccount", "mailgroup"),"file" => "gosa+samba3.schema"),
91       "goKrbServer"           => array("version" => "2.4"),
92       "goFaxServer"           => array("version" => "2.4", "class" => "gofaxAccount","file" => "gofax.schema"),
93       );
95   /* Build LDAP connection */
96   $ds= ldap_connect ($server);
97   if (!$ds) {
98     return (array(array("msg" => _("Can't bind to LDAP. No schema check possible!"), "status" => FALSE)));
99   }
100   ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
101   $r= ldap_bind ($ds, $admin, $password);
103   /* Get base to look for schema */
104   $sr  = @ldap_read ($ds, "", "objectClass=*", array("subschemaSubentry"));
105   $attr= @ldap_get_entries($ds,$sr);
106   if (!isset($attr[0]['subschemasubentry'][0])){
107     return (array(array("msg" => _("Can't get schema information from server. No schema check possible!"), "status" => FALSE)));
108   }
110   /* Get list of objectclasses */
111   $nb= $attr[0]['subschemasubentry'][0];
112   $objectclasses= array();
113   $sr= ldap_read ($ds, $nb, "objectClass=*", array("objectclasses"));
114   $attrs= ldap_get_entries($ds,$sr);
115   if (!isset($attrs[0])){
116     return (array(array("msg" => _("Can't get schema information from server. No schema check possible!"), "status" => FALSE)));
117   }
118   foreach ($attrs[0]['objectclasses'] as $val){
119     $name= preg_replace("/^.* NAME\s+\(*\s*'([^']+)'\s*\)*.*$/", '\\1', $val);
120     if ($name != $val){
121       $objectclasses[$name]= $val;
122     }
123   }
124   /* Walk through objectclasses and check if they are needed or not */
125   foreach ($required_classes as $key => $value){
126     if (isset($value['class'])){
127       if (!is_array($value['class'])){
128         $classes= array($value['class']);
129       } else {
130         $classes= $value['class'];
131       }
133       /* Check if we are using the class that requires */
134       foreach($classes as $class){
135         if (!isset($objectclasses[$key])){
136           $messages[$key]['msg']= sprintf(_("Optional objectclass '%s' required by plugin '%s' is not present in LDAP setup"), $key, $class);
137           $messages[$key]['status'] = FALSE;
138         } else {
139           if (!check_schema_version($objectclasses[$key], $value['version'])){
140             $messages[$key]['msg']= sprintf(_("Optional objectclass '%s' required by plugin '%s' does not have version %s"), $key, $class, $value['version']);
141             $messages[$key]['needonstartup'] = TRUE;
142             $messages[$key]['status'] =FALSE;
143           }else {
144             if(!isset($affich2[$class])){
145               $affich2[$class]['msg']   = sprintf(_("Support for '%s' enabled"), $class)."<td class=\"check\"> ".$value['file']."</td>";
146               $affich2[$class]['status']= TRUE; 
147             }
148           }
149         }
151       }
152     } else {
153       /* Required class */
154       if (!isset($objectclasses[$key])){
155         $messages[$key]['msg']= sprintf(_("Required objectclass '%s' is not present in LDAP setup"), $key);
156         $messages[$key]['status'] = FALSE;  
157       } else {
158         if (!check_schema_version($objectclasses[$key], $value['version'])){
159           $messages[$key]['msg']= sprintf(_("Required objectclass '%s' does not have version %s"), $key, $value['version']);
160           $messages[$key]['status'] = FALSE;  
161           $messages[$key]['needonstartup'] = TRUE;
162         }
163     
164       }
165     }
166   }
168   /* Check for correct samba parameters */
169   if (!isset($objectclasses['sambaSamAccount'])){
170     $messages['samba3']['msg']= _("SAMBA 3 support disabled, no schema seems to be installed");
171     $affich['samba3']['msg']= $messages['samba3']['msg']."<td class=\"check\">gosa+samba3.schema</td>";
172     $messages['samba3']['status']= FALSE;
173     $affich['samba3']['status']= FALSE;
174   }else{
175     $affich['samba3']['msg']= _("SAMBA 3 support enabled")."<td class=\"check\">gosa+samba3.schema</td>";
176     $affich['samba3']['status']= TRUE;
177   }
179   if (!isset($objectclasses['sambaAccount'])){
180     $messages['samba2']['msg']= _("SAMBA 2 support disabled, no schema seems to be installed");
181     $affich['samba2']['msg']= $messages['samba2']['msg']."<td class=\"check\">samba.schema</td>";
182     $messages['samba2']['status']= FALSE;
183     $affich['samba2']['status']= FALSE;
184   }else{
185     $affich['samba2']['msg']= _("SAMBA 2 support enabled")."<td class=\"check\">samba.schema</td>";
186     $affich['samba2']['status']= TRUE;
187   }
189   /* Check pureftp/dns/ */
190   if (!isset($objectclasses['PureFTPdUser'])){
191     $messages['pureftp']['msg']= _("Support for pureftp disabled, no schema seems to be installed");
192     $affich['pureftp']['msg']= $messages['pureftp']['msg']."<td class=\"check\">pureftpd.schema</td>";
193     $messages['pureftp']['status']= FALSE;
194     $affich['pureftp']['status']= FALSE;
195   }else{
196     $affich['pureftp']['msg']= _("Support for pureftp enabled")."<td class=\"check\">pureftpd.schema</td>";
197     $affich['pureftp']['status']= TRUE;
198   }
200   if (!isset($objectclasses['gosaWebdavAccount'])){
201     $messages['webdav']['msg']= _("Support for WebDAV disabled, no schema seems to be installed");
202     $affich['webdav']['msg']= $messages['webdav']['msg']."<td class=\"check\"></td>";
203     $messages['webdav']['status']= FALSE;
204     $affich['webdav']['status']= FALSE;
205   }else{
206     $affich['webdav']['msg']=_("Support for WebDAV enabled")."<td class=\"check\">gosa+samba3.schema</td>";
207     $affich['webdav']['status']= TRUE;
208   }
210   if (!isset($objectclasses['phpgwAccount'])){
211     $messages['phpgroupware']['msg']= _("Support for phpgroupware disabled, no schema seems to be installed");
212     $affich['phpgroupware']['msg']= $messages['phpgroupware']['msg']."<td class=\"check\">phpgwaccount.schema</td>";
213     $messages['phpgroupware']['status']= FALSE;
214     $affich['phpgroupware']['status']= FALSE;
215   }else{
216     $affich['phpgroupware']['msg']= _("Support for phpgroupware enabled")."<td class=\"check\">phpgwaccount.schema</td>";
217     $affich['phpgroupware']['status']= TRUE;
218   }
220   if (!isset($objectclasses['goFonAccount'])){
221     $messages['phoneaccount']['msg']= _("Support for gofon disabled, no schema seems to be installed");
222     $affich['phoneaccount']['msg']= $messages['phoneaccount']['msg']."<td class=\"check\">gofon.schema</td>";
223     $messages['phoneaccount']['status']= FALSE;
224     $affich['phoneaccount']['status']= FALSE;
225   }else{
226     $affich['phoneaccount']['msg']= _("Support for gofon enabled")."<td class=\"check\">gofon.schema</td>";
227     $affich['phoneaccount']['status']= true;
228   }
230   if(($_SESSION['ldapconf']['mail_methods'][$_SESSION['ldapconf']['mail']] == "kolab")){
231     if(!isset($objectclasses['kolabInetOrgPerson']))
232     {
233       $messages['kolab']['msg']= _("Support for Kolab disabled, no schema seems to be installed, setting mail-method to cyrus");
234       $affich['kolab']['msg']=$messages['kolab']['msg']."<td class=\"check\">kolab2.schema</td>";
236       $tmp= array_flip($_SESSION['ldapconf']['mail_methods']);
237       $_SESSION['ldapconf']['mail']=$tmp['cyrus'];
238       $messages['kolab']['status']= FALSE;
239       $affich['kolab']['status']= FALSE;
240     }else{
241       $affich['kolab']['msg']=_("Support for Kolab enabled")."<td class=\"check\">gofon.schema</td>";
242       $affich['kolab']['status']= TRUE;
243     }
244   }
245   if($aff==0){
246     return ($messages);
247   } else {
248     return(array_merge($affich,$affich2));
249   }
253 function check(&$faults, $message, $description, $test, $required= TRUE)
255   $msg= "<table summary=\"\" class='check'><tr><td class='check' style='font-size:14px;'>$message</td>
256     <td rowspan=2 style='vertical-align:middle; text-align:center;width:45px;'>";
257   if ($test){
258     $msg.= _("OK")."<br>";
259   } else {
260     if (!$required){
261       $msg.="<font color=red>"._("Ignored")."</font><br>";
262     } else {
263       $msg.="<font color=red>"._("Failed")."</font><br>";
264       $faults++;
265     }
266   }
267   $msg.= "</td></tr><tr><td class='check' style='padding-left:20px;".
268     "background-color:#F0F0F0;'>$description</td></tr></table><br>";
270   return $msg;
273 function perform_php_checks(&$faults)
275   global $check_globals;
277   $faults= 0;
278   $msg= "";
280   $msg.= "<h1>"._("PHP setup inspection")."</h1>";
281   $msg.= check (        $faults, _("Checking for PHP version (>=4.1.0)"),
282       _("PHP must be of version 4.1.0 or above for some functions and known bugs in PHP language."),
283       version_compare(phpversion(), "4.1.0")>=0);
285   $msg.= check (        $faults, _("Checking if register_globals is set to 'off'"),
286       _("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."),
287       $check_globals == 0, FALSE);
289   $msg.= check (        $faults, _("Checking for ldap module"),
290       _("This is the main module used by GOsa and therefore really required."),
291       function_exists('ldap_bind'));
293   $msg.= check (  $faults, _("Checking for XML functions"),
294       _("XML functions are required to parse the configuration file."),
295       function_exists('xml_parser_create'));
297   $msg.= check (        $faults, _("Checking for gettext support"),
298       _("Gettext support is required for internationalized GOsa."), function_exists('bindtextdomain'));
300   $msg.= check (        $faults, _("Checking for iconv support"),
301       _("This module is used by GOsa to convert samba munged dial informations and is therefore required."),
302       function_exists('iconv'));
304   $msg.= check (        $faults, _("Checking for mhash module"),
305       _("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."),
306       function_exists('mhash'), FALSE);
308   $msg.= check (        $faults, _("Checking for imap module"),
309       _("The IMAP module is needed to communicate with the IMAP server. It gets status informations, creates and deletes mail users."),
310       function_exists('imap_open'));
312   $msg.= check (        $faults, _("Checking for getacl in imap"),
313       _("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."),
314       function_exists('imap_getacl'), FALSE);
316   $msg.= check (        $faults, _("Checking for mysql module"),
317       _("MySQL support is needed for reading GOfax reports from databases."),
318       function_exists('mysql_query'), FALSE);
320   $msg.= check (        $faults, _("Checking for cups module"),
321       _("In order to read available printers from IPP protocol instead of printcap files, you've to install the CUPS module."),
322       function_exists('cups_get_dest_list'), FALSE);
324   $msg.= check (        $faults, _("Checking for kadm5 module"),
325       _("Managing users in kerberos requires the kadm5 module which is downloadable via PEAR network."),
326       function_exists('kadm5_init_with_password'), FALSE);
328   $msg.= check (  $faults, _("Checking for snmp Module"),
329       _("Simple Network Management Protocol (SNMP) is required for client monitoring."),
330       function_exists('snmpget'), FALSE);
331   return ($msg);
335 function perform_additional_checks(&$faults)
337   /* Programm check */
338   $msg= "<h1>"._("Checking for some additional programms")."</h1>";
340   /* Image Magick */
341   $query= "LC_ALL=C LANG=C convert -help";
342   $output= shell_exec ($query);
343   if ($output != ""){
344     $lines= split ("\n", $output);
345     $version= preg_replace ("/^Version:.+Magick ([^\s]+).*/", "\\1", $lines[0]);
346     list($major, $minor)= split("\.", $version);
347     $msg.= check (      $faults, _("Checking for ImageMagick (>=5.4.0)"),
348         _("ImageMagick is used to convert user supplied images to fit the suggested size and the unified JPEG format."),
349         ($major > 5 || ($major == 5 && $minor >= 4)));
350   } else {
351     $msg.= check (      $faults, _("Checking imagick module for PHP"),
352         _("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);
353   }
355   /* Check for fping */
356   $query= "LC_ALL=C LANG=C fping -v 2>&1";
357   $output= shell_exec ($query);
358   $have_fping= preg_match("/^fping:/", $output);
359   $msg.= check (        $faults, _("Checking for fping utility"),
360       _("The fping utility is only used if you've got a thin client based terminal environment running."),
361       $have_fping, FALSE);
363   /* Check for smb hash generation tool */
364   $query= "mkntpwd 2>&1";
365   $output= shell_exec ($query);
366   $have_mkntpwd= preg_match("/^Usage: mkntpwd /", $output);
367   $alt = 0;
369   if (!$have_mkntpwd){
370     $query= "LC_ALL=C LANG=C perl -MCrypt::SmbHash -e 'ntlmgen \"PASSWD\", \$lm, \$nt; print \"\${lm}:\${nt}\\n\";' &>/dev/null";
371     system ($query, $ret);
372     $alt= ($ret == 0);
373   }
375   $msg.= check (        $faults, _("Checking for a way to generate LM/NT password hashes"),
376       _("In order to use SAMBA 2/3, you've to install some additional packages to generate password hashes."),
377       ($have_mkntpwd || $alt));
379   /* seesio.auto_start should be off, in order to without trouble*/
380   $session_auto_start = ini_get('session.auto_start');
381   $implicit_flush     = ini_get('implicit_flush');
382   $max_execution_time = ini_get('max_execution_time');
383   $memory_limit       = ini_get('memory_limit');
384   $expose_php         = ini_get('expose_php');
385   $magic_quotes_gpc   = ini_get('magic_quotes_gpc');
386   $register_globals   = ini_get('register_globals');
388   /* auto_register */
389   $msg.= check (  $faults, _("php.ini check -> session.auto_register"),
390       _("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']));
392   /* implicit_flush */
393   $msg.= check (  $faults, _("php.ini check -> implicit_flush"),
394       _("This option influences the Output handling. Turn this Option off, to increase performance."),
395       !$implicit_flush['local_value'],0,false);
397   /* max_execution_time */
398   if($max_execution_time['local_value'] < 30 ){
399     $max_execution_time['local_value']=false;
400   }
401   $msg.= check (  $faults, _("php.ini check -> max_execution_time"),
402       _("The Execution time should be at least 30 seconds, because some actions may consume more time."),
403       $max_execution_time['local_value'],0,false);
405   /* memory_limit */
406   if($memory_limit['local_value'] < 16 ){
407     $memory_limit['local_value']=false;
408   }
409   $msg.= check (  $faults, _("php.ini check -> memory_limit"),
410       _("GOsa needs at least 16MB of memory, less will cause unpredictable errors! Increase it for larger setups."),
411       !$implicit_flush['local_value'],0,false);
413   /* expose_php */
414   $msg.= check (  $faults, _("php.ini check -> expose_php"),
415       _("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."),
416       !$implicit_flush['local_value'],0,false);
418   /* magic_quotes_gpc */
419   $msg.= check (  $faults, _("php.ini check -> magic_quotes_gpc"),
420       _("Increase your server security by setting magic_quotes_gpc to 'on'. PHP will escape all quotes in strings in this case."),
421       $magic_quotes_gpc['local_value'],0,false);
423   return $msg;
427 function parse_contrib_conf()
430   $str                = "";
431   $used_samba_version = 0;
432   $query              = ""; 
433   $fp                 = false;
434   $output             = "";
435   $needridbase_sid    = false;
436   $pwdhash            = "";
437   $replacements       = array();
438   $ldapconf           = $_SESSION['ldapconf']; // The Installation information
439   $classes            = $_SESSION['classes'];  // Class information needed to define which features are enabled
440   $possible_plugins   = array();
442   /* Which samba version do we use? */
443   if(isset($classes['samba3'])){
444     $used_samba_version = 2;
445   } else {
446     $used_samba_version = 3;
447   }
449   /* Look for samba password generation method */
450   if(file_exists("/usr/lib/gosa/mkntpasswd")){
451     $pwdhash  = "/usr/lib/gosa/mkntpasswd";
452   } elseif (preg_match("/^Usage: mkntpwd /", shell_exec ("mkntpwd 2>&1"))){
453     $pwdhash= "mkntpwd";
454   } else {
455     $pwdhash=('perl -MCrypt::SmbHash -e "ntlmgen \"\$ARGV[0]\", \$lm, \$nt; print \"\${lm}:\${nt}\n\";" $1');
456   }
458   /* Define which variables will be replaced */
459   $replacements['{LOCATIONNAME}']  = $ldapconf['location'];
460   $replacements['{SAMBAVERSION}']  = $used_samba_version;
461   $replacements['{LDAPBASE}']      = $ldapconf['base'];
462   $replacements['{LDAPADMIN}']     = $ldapconf['admin'];
463   $replacements['{DNMODE}']        = $ldapconf['peopledn'];
464   $replacements['{LDAPHOST}']      = $ldapconf['uri'];
465   $replacements['{PASSWORD}']      = $ldapconf['password'];
466   $replacements['{CRYPT}']         = $ldapconf['arr_cryptkeys'][$ldapconf['arr_crypts']];
467   $replacements['{SID}']         = "";
468   $replacements['{RIDBASE}']     = "";
469   if($ldapconf['mail'] != "disabled"){
470     $replacements['{MAILMETHOD}']    = $ldapconf['mail_methods'][$ldapconf['mail']];
471   }   
472   $replacements['{SMBHASH}']       = $pwdhash;
473   $replacements['{GOVERNMENTMODE}']= "false"; 
474   $replacements['{kolabAccount}']  = "";
475   $replacements['{servKolab}']     = "";
476   $replacements['{errorlvl}']     = $ldapconf['errorlvl'];
478   /* This array contains all preg_replace syntax to delete all unused plugins
479      THE kEY MUST BE THE CLASSNAME so we can check it with $ldapconf['classes'] */
481   $possible_plugins['fonreport'][]   = "'\n.*<plugin.*fonreport+.*\n.*>.*\n'i";
482   $possible_plugins['phoneaccount'][]= "'\n.*<tab.*phoneAccount.*>.*\n'i";
483   $possible_plugins['logview'][]     = "'\n.*<plugin.*logview+.*\n.*>.*\n'i";
484   $possible_plugins['pureftp'][]     = "'\n.*<tab.*pureftp.*>.*\n'i";
485   $possible_plugins['webdav'][]      = "'\n.*<tab.*webdav.*>.*\n'i";
486   $possible_plugins['phpgroupware'][]= "'\n.*<tab.*phpgroupware.*>.*\n'i";
488   /*Header information
489      Needed to send the generated gosa.conf to the browser */
490   header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
491   header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
492   header("Cache-Control: no-cache");
493   header("Pragma: no-cache");
494   header("Cache-Control: post-check=0, pre-check=0");
495   header("Content-type: text/plain");
497   if (preg_match('/MSIE 5.5/', $_SERVER['HTTP_USER_AGENT']) ||
498       preg_match('/MSIE 6.0/', $_SERVER['HTTP_USER_AGENT'])){
499     header('Content-Disposition: filename="gosa.conf"');
500   } else {
501     header('Content-Disposition: attachment; filename="gosa.conf"');
502   }
504   if(!$fp=fopen(CONFIG_TEMPLATE_DIR."/gosa.conf","r")) {
505     echo "Can't open file ".CONFIG_TEMPLATE_DIR."/gosa.conf";
506   } else {
507     while(!feof($fp)) {
508       $str.= fread($fp,512);
509     }
511     if($ldapconf['mail_methods'][$ldapconf['mail']]=="kolab") {
512       $replacements['{kolabAccount}']  ="<tab class=\"kolabAccount\" />\n     ";
513       $replacements['{servKolab}']     ="<tab class=\"servkolab\" name=\"Kolab\" />";
514     }
516     if($used_samba_version == 2) {
517       /* Do nothing for samba 2... */
518     } else {
519       /* Create LDAP connection, to check if there's a domain
520          object defined in the LDAP schema */
521       $ldap= new LDAP($ldapconf['admin'], $ldapconf['password'], $ldapconf['uri']);
523       /* Try to find a Samba Domain Objekt */
524       $ldap->search("(objectClass=sambaDomain)");
525     
526       /* Something found ??? so we need to define ridbase an SID by ourselfs */
527       if($ldap->count()< 1) {
528         $replacements['{SID}']= "sid=\"123412-11\"";
529         $replacements['{RIDBASE}']= "ridbase=\"1000\"";  
530       } 
531     }
533     /* Data readed, types replaced, samba version detected and checked if
534        we need to add SID and RIDBASE. Check if there is an ivbbEntry in
535        the LDAP tree, in this case we will set the governmentmode to true.
536        Create LDAP connection, to check if theres a domain Objekt definen
537        in the LDAP schema. */
538     if(!isset($ldap)){
539       $ldap= new LDAP($ldapconf['admin'], $ldapconf['password'], $ldapconf['uri']);
540     }
542     /* Try to find a Samba Domain Objekt */
543     $ldap->search("(objectClass=ivbbEntry)");
545     /* Something found ??? so we need to define ridbase an SID by ourselfs */
546     if($ldap->count()> 0) {
547       $replacements['{GOVERNMENTMODE}']= "true";
548     }
550     /* Replace all colleted information with placeholder */
551     foreach($replacements as $key => $val) {
552       $str = preg_replace("/".$key."/",$val,$str);
553     }    
555     if($ldapconf['mail'] == "disabled"){
556       $str = str_replace("mailMethod=\"{MAILMETHOD}\"","",$str);
557     }
560     /* Remove all unused plugins */
561     foreach($possible_plugins as $key=> $plugin) {
562       foreach($plugin as $key=>$val) {
563         if(in_array($plugin,$classes)) {
564           $str = preg_replace($val,"\n",$str);
565         }
566       }
567     }
568   }
570   return ((($str)));
574 /* Show setup_page 1 */
575 function show_setup_page1($withoutput = true)
577   $smarty = get_smarty();  
578   $smarty->assign ("content", get_template_path('setup_introduction.tpl'));
579   $smarty->assign ("tests", perform_php_checks($faults));
581   /* This var is true if anything went wrong */
582   if ($faults){
583     $smarty->assign("mode", "disabled");
584   }
586   /* This line displays the template only if (withoutput is set) */
587   if($withoutput){
588     $smarty->display (get_template_path('headers.tpl'));
589   }
591   if (isset($_SESSION['errors'])){
592     $smarty->assign("errors", $_SESSION['errors']);
593   }
595   if($withoutput){
596     $smarty->display (get_template_path('setup.tpl'));
597   }
599   return (!$faults);
603 /* Show setup_page 2 */
604 function show_setup_page2($withoutput = true)
606   $smarty = get_smarty();
607   $smarty->assign ("content", get_template_path('setup_step2.tpl'));
608   $smarty->assign ("tests", perform_additional_checks($faults));
610   if ($faults) {
611     $smarty->assign("mode", "disabled");
612   }
613   if($withoutput){
614     $smarty->display (get_template_path('headers.tpl'));
615   }
616   if (isset($_SESSION['errors']))  {
617     $smarty->assign("errors", $_SESSION['errors']);
618   }
619   if($withoutput){
620     $smarty->display (get_template_path('setup.tpl'));
621   }
623   return (!$faults);                               
627 function show_setup_page3($withoutput = true)
629   $smarty = get_smarty();
631   /* Take the Post oder the Sessioin saved data */
632   if(isset($_POST['uri'])){
633     $uri = $_POST['uri'];
634   } elseif(isset($_SESSION['ldapconf']['uri'])){
635     $uri = $_SESSION['ldapconf']['uri'];
636   }
638   /* If Page called first time, field is empty */
639   if((!isset($uri))||(empty($uri))){
640     $uri = "ldap://localhost:389";
641   }
643   /* if isset $uri save it to session */
644   if(isset($uri)) {
645     $_SESSION['ldapconf']['uri'] = $uri;
646     $smarty->assign ("uri", validate($uri));
647   }
649   /* No error till now */
650   $fault = false;
652   /* If we pushed the Button continue */
653   if(isset($_POST['continue3'])){
654     if(!isset($uri)) {
655       $fault = true;
657       /* Output the Error */
658       if($withoutput) {
659         print_red (_("You've to specify an ldap server before continuing!"));
660         $smarty->assign ("content", get_template_path('setup_step3.tpl'));
661       }
662     }
663   } elseif (!$ds = @ldap_connect (validate($uri))) {
664     $fault =true;
666     /* Output the Error */
667     if($withoutput) {
668       print_red (_("Can't connect to the specified LDAP server! Please make sure that is reachable for GOsa."));
669       $smarty->assign ("uri", validate($uri));
670       $smarty->assign ("content", get_template_path('setup_step3.tpl'));
671     }
672   } else {
673     /* Try to bind the connection */    
674     ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
676     /* if we can't bind , print error */
677     if (!$r  =  @ldap_bind ($ds)) {
678       $fault = true;
680       /* Output the Error */
681       if($withoutput) {
682         print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
683         $smarty->assign ("content", get_template_path('setup_step3.tpl'));
684         $smarty->assign ("uri", validate($uri));
685       }
686     } else {
687       $fault = false;
688     }
689   }
691   $smarty->assign ("content", get_template_path('setup_step3.tpl'));
693   /* Load Header */
694   if($withoutput){
695     $smarty->display (get_template_path('headers.tpl'));
696   }
698   /* Set Errors to Smarty */
699   if (isset($_SESSION['errors'])) {
700     $smarty->assign("errors", $_SESSION['errors']);
701   }
703   /* Print out Template */ 
704   if($withoutput){
705     $smarty->display (get_template_path('setup.tpl'));
706   }
708   return (!$fault);                             
712 function show_setup_page4($withoutput = true)
714   $smarty= get_smarty();      
716   if(!isset($_SESSION['ldapconf']['base'])){
717     $_SESSION['ldapconf']['base']= $base;
718   }
720   if(!isset($_SESSION['ldapconf']['base'])){
721     $_SESSION['ldapconf']['base']= $base;
722   }
723   require_once("class_password-methods.inc");
725   $fault     = false;              
726   $uri       = $_SESSION['ldapconf']['uri'];
727   $ldapconf  = $_SESSION['ldapconf'];
728   $arr_crypts= array();
729   $temp      = "";
730   $checkvars = array("location", "admin", "password", "peopleou", "base",
731       "peopledn", "arr_crypts", "mail", "uidbase","errorlvl");
733   if(!isset($_SESSION['ldapconf']['arr_cryptkeys'])) {
734     require_once("class_password-methods.inc");
735     $tmp= passwordMethod::get_available_methods_if_not_loaded();
736     $_SESSION['ldapconf']['arr_cryptkeys']= $tmp['name'];
737   }
739   if(!isset($_SESSION['ldapconf']['mail_methods'])) {
740     $_SESSION['ldapconf']['mail_methods']=array();
741     $temp = get_available_mail_classes();
742     $_SESSION['ldapconf']['mail_methods']= $temp['name'];
743   }
745   /* If there are some empty vars in ldapconnect -
746      these values also represent out default values */
747   if(!$ds = @ldap_connect (validate($uri))){
748     $fault = true;
749     if($withoutput){
750       print_red (_("Can't connect to the specified LDAP server! Please make sure that is reachable for GOsa."));
751     }
752   } elseif(!@ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3)){
753     $fault = true;
754     if($withoutput){
755       print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
756     }
757   } elseif(!$r= @ldap_bind ($ds)){
758     $fault = true;
759     if($withoutput){
760       print_red (_("Can't bind to the specified LDAP server! Please make sure that it is reachable for GOsa."));
761     }
762   } else {
763     $sr=   @ldap_search ($ds, "", "objectClass=*", array("namingContexts"));
764     $attr= @ldap_get_entries($ds,$sr);
766     if((empty($attr))) {
767       $base= "dc=example,dc=net";
769       if($withoutput){
770         print_red(_("Bind to server successful, but the server seems to be completly empty, please check all informations twice"));
771       }
773     } else {
774       $base= $attr[0]['dn'];
775     }
776   }
778   if(!isset($_SESSION['ldapconf']['base'])){
779     $_SESSION['ldapconf']['base']= $base;
780   }
781   if(!isset($_SESSION['ldapconf']['admin'])){
782     $_SESSION['ldapconf']['admin']= "cn=ldapadmin,".$base;
783   }
784   if(!isset($_SESSION['ldapconf']['peopleou'])){
785     $_SESSION['ldapconf']['peopleou']= "ou=people";
786   }
787   if(!isset($_SESSION['ldapconf']['groupou'])){
788     $_SESSION['ldapconf']['groupou']= "ou=groups";
789   }
790   if(!isset($_SESSION['ldapconf']['peopledn'])){
791     $_SESSION['ldapconf']['peopledn']= "cn";
792   }
793   if(!isset($_SESSION['ldapconf']['password'])){
794     $_SESSION['ldapconf']['password']= "";
795   }
796   if(!isset($_SESSION['ldapconf']['location'])){
797     $_SESSION['ldapconf']['location']= "Example";
798   }
799   if(!isset($_SESSION['ldapconf']['uidbase'])){
800     $_SESSION['ldapconf']['uidbase']= "1000";
801   }
802   if(!isset($_SESSION['ldapconf']['mail'])){
803     $_SESSION['ldapconf']['mail']= 0;
804   }
805   $tmp= array_flip($_SESSION['ldapconf']['arr_cryptkeys']);
806   if(!isset($_SESSION['ldapconf']['arr_crypts'])){
807     $_SESSION['ldapconf']['arr_crypts']   = $tmp['md5'];
808   }
810   /* check POST data */
811   if(isset($_POST['check'])) {
813     /* Check if all needed vars are submitted */
814     foreach($checkvars as $key) {
815       if($key == "peopleou"){
816         continue;
817       }
818       if($key == "groupou"){
819         continue;
820       }
822       if((isset($_POST[$key]))&&($_POST[$key]!="")) {
823         $_SESSION['ldapconf'][$key] = $_POST[$key];
824       } else {
825         if($withoutput) {
826           print_red(sprintf(_("You're missing the required attribute '%s' from this formular. Please complete!"), $key));
827         }
828         $fault = true;
829       }
830     }
831   }
833   /* Transfer base */
834   if(isset($_POST['base'])){
835     $_SESSION['ldapconf']['base']= $_POST['base'];
836   }
838   $smarty->assign("arr_cryptkeys",$_SESSION['ldapconf']['arr_cryptkeys']);
839   $smarty->assign("mail_methods", $_SESSION['ldapconf']['mail_methods']);
841   foreach($_SESSION['ldapconf'] as $key => $val) {
842     $smarty->assign($key,$val);
843   }
845   if(isset($_POST['check'])) {
846     $ldap= new LDAP($_SESSION['ldapconf']['admin'],
847         $_SESSION['ldapconf']['password'],
848         $_SESSION['ldapconf']['uri']);
850     $m= schema_check($_SESSION['ldapconf']['uri'],
851         $_SESSION['ldapconf']['admin'],
852         $_SESSION['ldapconf']['password']);
853     $_SESSION['classes']= $m;
855     if(!is_schema_readable($ldapconf['uri'],$ldapconf['admin'],$ldapconf['password'])){
856       if($withoutput){
857         print_red(_("Can't read schema informations, GOsa needs to know your schema setup. Please verify that it is readable for GOsa"));
858       }
859       $fault=true;
860     }
864     if ($ldap->error != "Success") {
865       if($withoutput) {
866         print_red(sprintf(_("Can't log into LDAP server. Reason was: %s."), $ldap->get_error()));
867       }
868       $fault = true;
869     }
870   }
872   /* Set smarty output */
873   $smarty->assign ("content", get_template_path('setup_step4.tpl'));
874   $smarty->assign ("peopledns", array("cn", "uid"));
875   if($withoutput){
876     $smarty->display (get_template_path('headers.tpl'));
877   }
878   if(isset($_SESSION['errors'])) {
879     $smarty->assign("errors", $_SESSION['errors']);
880   }
881   if($withoutput){
882     $smarty->display (get_template_path('setup.tpl'));
883   }
884   return (!$fault);
888 function show_setup_page5($withoutput=true)
890   /* Get ldapconf */
891   $ldapconf= $_SESSION['ldapconf'];
893   /* get smarty */
894   $smarty = get_smarty();
896   if(isset($_SESSION['classes'])){
897     $classes = $_SESSION['classes'];
898   }
900   $info= posix_getgrgid(posix_getgid());
901   $smarty->assign("webgroup", $info['name']);
902   $smarty->assign("path", CONFIG_DIR);
903   $message= "<table summary=\"\" class=\"check\">";
904   $m= schema_check($ldapconf['uri'], $ldapconf['admin'], $ldapconf['password'],1);
906   if($withoutput) {
907     $smarty->assign ("schemas", view_schema_check($m));
908     $smarty->assign ("content", get_template_path('setup_finish.tpl'));
909   }
911   /* Output templates... */
912   if($withoutput){
913     $smarty->display (get_template_path('headers.tpl'));
914   }
915   if (isset($_SESSION['errors'])) {
916     $smarty->assign("errors", $_SESSION['errors']);
917   }
918   if($withoutput){
919     $smarty->display (get_template_path('setup.tpl'));
920   }
922   return(true);
926 function create_user_for_setup($withoutput=true)
928   global $samba;
930   $ldapconf = $_SESSION['ldapconf'];
931   $smarty = get_smarty();
932   
933   $need_to_create_group = false;
934   $need_to_create_user  = false;
936   $str_there="";
938   if(isset($_SESSION['classes'])){
939     $classes= $_SESSION['classes'];
940   }
942   /* Everything runns perfect ...
943      So we do a last test on this page
944      is there a user with ACLs :all which will be able to adminsitrate GOsa
945      We check that, if this user or group is missing we ask for creating them */
946   $ldap= new LDAP($_SESSION['ldapconf']['admin'],    $_SESSION['ldapconf']['password'],   $_SESSION['ldapconf']['uri']);
948   /* 
949   Now we are testing for a group, with the rights :all 
950   */
951   
952   $ldap->cd($ldapconf['base']);
953   $ldap->search("(&(objectClass=gosaObject)(gosaSubtreeACL=:all))");
955   $group_cnt  = $ldap->count();
956   $data       = $ldap->fetch();
958 //  $str_there  = "Searching for Aminitrative users <br><br>";
960   /* 
961   We need to create administrative user and group  because theres no group found 
962   */
963   if($group_cnt < 1) {
964     
965     /* 
966     Set var to create user 
967     */
968 //    $str_there  =   "no group found<br>";
970     $need_to_create_group = true;
971     $need_to_create_user  = true;
974     /* Output error */
975     if(($withoutput)&&(!isset($_POST['new_admin']))){
976       print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
977     }
978   } else {
979     
980 //    $str_there = "Group found <br>".$data['dn'];    
982     $need_to_create_group = false;
983  
984     $ldap->clearResult();
985    
986     /* We found an Administrative Group, is there a user, too */
987     if(isset($data['memberUid'][0])) {
988       $str = "uid=".$data['memberUid']['0'];
989       $ldap->search("(&(objectClass=gosaAccount)(objectClass=person)(".$str."))");
990       $data2   = $ldap->fetch();
991   
992       /* We must create a user */
993       if (($ldap->count() < 1)||(!isset($data2))) {
994 //        $str_there.="Missing user";
995         
996         $need_to_create_user = true;
997       
998         if(($withoutput)&&(!isset($_POST['new_admin']))){
999           print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
1000         }
1001       }else {
1002 //        $str_there.="<br>User found <br>".$data2['dn'];
1003         $need_to_create_user = false;
1004       }
1005     } else {
1006       $need_to_create_user=true;
1007       if(($withoutput)&&(!isset($_POST['new_admin']))){
1008           print_red(_("You're missing an administrative account for GOsa, you'll not be able to administrate anything!"));
1009         }   
1010 //      $str_there.="<br>No User found <br>";
1011     }
1012   }
1014   if(!($need_to_create_user&&$need_to_create_group))
1015     return(true);
1017   /* We need to create a new user with group */
1018   if(isset($_POST['new_admin']))
1019   {
1020   
1021     /* Adjust password attributes according to the samba version */
1022     if (isset($classes['samba3'])) {
1023       $samba= "2";
1024       $lmPassword = "lmPassword";
1025       $ntPassword = "ntPassword";
1026     } else {
1027       $samba= "3";
1028       $lmPassword = "sambaLMPassword";
1029       $ntPassword = "sambaNtPassword";
1030     }
1032     /* Nothing submitted */
1033     if(((empty($_POST['admin_name']))||(empty($_POST['admin_pass'])))) {
1034       return(true);
1035     }
1037     if($need_to_create_user) {
1038       /* We have the order to create an Admin */
1039       /* Define the user we are going to create */
1040       $dn= "cn=".$_POST['admin_name'].",".$ldapconf['peopleou'].",".$ldapconf['base'];
1041       $arr['objectClass'][0] ="person";
1042       $arr['objectClass'][1] ="organizationalPerson";
1043       $arr['objectClass'][2] ="inetOrgPerson";
1044       $arr['objectClass'][3] ="gosaAccount";
1045       $arr['uid']            = $_POST['admin_name'];
1046       $arr['cn']             = $_POST['admin_name'];
1047       $arr['sn']             = $_POST['admin_name'];
1048       $arr['givenName']      = "GOsa main administrator";
1049       $arr[$lmPassword]      = "10974C6EFC0AEE1917306D272A9441BB";
1050       $arr[$ntPassword]      = "38F3951141D0F71A039CFA9D1EC06378";
1051       $arr['userPassword']   = crypt_single($_POST['admin_pass'],"md5");
1052     
1054       if(!$ldap->dn_exists($dn)){ 
1055         $ldap->cd($dn); 
1056         $ldap->create_missing_trees($dn);
1057         $ldap->cd($dn);
1058         $ldap->add($arr);
1059         if($ldap->error!="Success"){
1060           print_red($ldap->error);
1061           print_red("Can't create user, and / or Group, possibly this problem  depends on an empty LDAP server. Check your configuration and try again!");
1062         }
1063       }    
1064     }
1066     /* There's already a group for administrator, so we only need to add the user */
1067     if(!$need_to_create_group) {
1068       if(!isset($data['memberUid'])) {
1069         $arrr['memberUid']= $_POST['admin_name'];
1070       } else {
1071         $data['memberUid'][$data['memberUid']['count']]=$_POST['admin_name'];
1072         $arrr['memberUid'] = $data['memberUid'];
1073         unset($arrr['memberUid']['count']);
1074   
1075         $tmp = array_reverse($arrr['memberUid']);    
1076         foreach($tmp as $tt){
1077           $tmp2[]=$tt;
1078         }
1079         $arrr['memberUid']= $tmp2;
1080 //        $str_there="Group found<br>".$data['dn'];
1081       }
1083       $ldap->cd($data['dn']);
1084       $ldap->modify($arrr);
1086     } else {
1087       $dn                    = "cn=administrators,".$ldapconf['groupou'].",".$ldapconf['base'];
1088       $arrr['objectClass'][0]= "gosaObject";
1089       $arrr['objectClass'][1]= "posixGroup";
1090       $arrr['gosaSubtreeACL']= ":all";
1091       $arrr['cn']            = "administrators";
1092       $arrr['gidNumber']     = "999";
1093       $arrr['memberUid']     = $_POST['admin_name'];
1095       $ldap->cd($dn);
1096       $ldap->create_missing_trees($dn);
1097       $ldap->cd($dn);
1099       $ldap->add($arrr);
1100     }
1101     return(true);
1102   } else {
1104     if(!($create_user)) {
1105       $smarty->assign ("content", get_template_path('setup_useradmin.tpl'));
1106       $smarty->assign("exists",true);
1107     } else {
1108       $smarty->assign ("content", get_template_path('setup_useradmin.tpl'));
1109       $smarty->assign("exists",false);
1110     }
1112   }
1114   /* Smarty output */ 
1115   if($withoutput){
1116     $smarty->display (get_template_path('headers.tpl'));
1117   }
1118   if (isset($_SESSION['errors'])) {
1119     $smarty->assign("errors", $_SESSION['errors']);
1120   }
1121   $smarty->assign("str_there",$str_there);
1122   if($withoutput){
1123     $smarty->display (get_template_path('setup.tpl'));
1124   }
1125   return(false);
1129 /* Returns the classnames auf the mail classes */
1130 function get_available_mail_classes()
1132   $dir = opendir( "../include");
1133   $methods = array();
1134   $suffix = "class_mail-methods-";
1135   $lensuf = strlen($suffix);
1136   $prefix = ".inc";
1137   $lenpre = strlen($prefix);
1139   $i = 0;
1140   while (($file = readdir($dir)) !== false){
1142     if(stristr($file,$suffix)) {
1143       $lenfile = strlen($file);
1144       $methods['name'][$i] = substr($file,$lensuf,($lenfile-$lensuf)-$lenpre);
1145       $methods['file'][$i] = $file;
1146       $methods[$i]['file'] = $file;
1147       $methods[$i]['name'] = substr($file,$lensuf,($lenfile-$lensuf)-$lenpre);
1148       $i++;
1149     }
1151   }
1153   return($methods);
1156 // vim:tabstop=2:expandtab:shiftwidth=2:filetype=php:syntax:ruler:
1157 ?>