ini_set('error_reporting', E_ALL); } if (posix_getuid() != 0) { die("Cannot be used in web server mode\n\n"); } include_once dirname(__FILE__) . '/ressources/class.ldap.inc'; include_once dirname(__FILE__) . "/framework/frame.class.inc"; if ($argv[1] == "--start") { start(); exit; } if ($argv[1] == "--stop") { stop(); exit; } buildscript(); function buildscript() { $unix = new unix(); $php = $unix->LOCATE_PHP5_BIN(); $f[] = "#!/bin/sh"; $f[] = "### BEGIN INIT INFO"; $f[] = "# Provides: artica-swap"; $f[] = "# Required-Start: \$local_fs"; $f[] = "# Required-Stop: \$local_fs"; $f[] = "# Should-Start:"; $f[] = "# Should-Stop:"; $f[] = "# Default-Start: 2 3 4 5"; $f[] = "# Default-Stop: 0 1 6"; $f[] = "# Short-Description: Start SWAP config server"; $f[] = "# chkconfig: 2345 11 89";
function start_ldap($aspid = false) { $sock = new sockets(); $ldaps = array(); $unix = new unix(); $kill = $unix->find_program("kill"); if (!$GLOBALS["FORCE"]) { $pid = $unix->get_pid_from_file('/etc/artica-postfix/pids/exec.backup.artica.php.restore.pid'); if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); if ($pidtime < 15) { echo "slapd: [INFO] Artica restore task already running pid {$pid} since {$pidtime}mn\n"; restart_ldap_progress("{success}", 100); return; } } } $MYPID_FILE = "/etc/artica-postfix/pids/start_ldap.pid"; if (!$aspid) { $pid = $unix->get_pid_from_file($MYPID_FILE); if ($unix->process_exists($pid, basename(__FILE__))) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); $unix->ToSyslog("Artica task already running pid {$pid} since {$pidtime}mn", false, basename(__FILE__)); echo "slapd: [INFO] Artica task already running pid {$pid} since {$pidtime}mn\n"; if ($pidtime > 10) { echo "slapd: [INFO] Killing this Artica task...\n"; unix_system_kill_force($pid); } else { die; } } $MYPID_FILE_TIME = $unix->file_time_min($MYPID_FILE); if (!$GLOBALS["FORCE"]) { if ($MYPID_FILE_TIME < 1) { echo "slapd: [INFO] Task must be executed only each 1mn (use --force to by pass)\n"; die; } } @unlink($MYPID_FILE); @file_put_contents($MYPID_FILE, getmypid()); } $slapd = $unix->find_program("slapd"); $SLAPD_PID_FILE = $unix->SLAPD_PID_PATH(); $pid = $unix->get_pid_from_file($SLAPD_PID_FILE); if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); restart_ldap_progress("{success}", 100); echo "slapd: [INFO] slapd already running pid {$pid} since {$pidtime}mn\n"; @file_put_contents($SLAPD_PID_FILE, $pid); return; } $pid = $unix->PIDOF_PATTERN($slapd); echo "slapd: [INFO] detecting presence of `{$slapd}`:{$pid}...\n"; if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); restart_ldap_progress("{success}", 100); echo "slapd: [INFO] slapd already running pid {$pid} since {$pidtime}mn\n"; @file_put_contents($SLAPD_PID_FILE, $pid); return; } echo "slapd: [INFO] slapd loading required values...\n"; if (!is_file($slapd)) { if (is_file('/usr/lib/openldap/slapd')) { $slapd = '/usr/lib/openldap/slapd'; } } $OpenLDAPLogLevel = $sock->GET_INFO("OpenLDAPLogLevel"); $OpenLDAPDisableSSL = $sock->GET_INFO("OpenLDAPDisableSSL"); $EnableNonEncryptedLdapSession = $sock->GET_INFO("EnableNonEncryptedLdapSession"); $EnableipV6 = $sock->GET_INFO("EnableipV6"); if (!is_numeric($EnableipV6)) { $EnableipV6 = 0; } if (!is_numeric($EnableNonEncryptedLdapSession)) { $EnableNonEncryptedLdapSession = 1; } $phpldapadmin = null; if (!is_numeric($OpenLDAPDisableSSL)) { $OpenLDAPDisableSSL = 0; } $ZARAFA_INSTALLED = 0; if ($GLOBALS["VERBOSE"]) { echo "users=new usersMenus();\n"; } $users = new usersMenus(); if ($GLOBALS["VERBOSE"]) { echo "users=new usersMenus() done...;\n"; } if (!is_dir("/var/lib/ldap")) { @mkdir("/var/lib/ldap", 0755, true); } if (!is_dir("/var/run/slapd")) { @mkdir("/var/run/slapd", 0755, true); } if (!is_numeric($OpenLDAPLogLevel)) { $OpenLDAPLogLevel = 0; } if ($OpenLDAPLogLevel != 0) { $OpenLDAPLogLevelCmdline = " -d {$OpenLDAPLogLevel}"; } $ifconfig = $unix->find_program("ifconfig"); echo "slapd: [INFO] start looback address...\n"; shell_exec("{$ifconfig} lo 127.0.0.1 netmask 255.255.255.0 up >/dev/null 2>&1"); $ldap[] = "ldapi://" . urlencode("/var/run/slapd/slapd.sock"); $ldap[] = "ldap://127.0.0.1:389/"; if (is_file("/etc/artica-postfix/settings/Daemons/LdapListenIPAddr")) { $LdapListenIPAddr = explode("\n", @file_get_contents("/etc/artica-postfix/settings/Daemons/LdapListenIPAddr")); while (list($num, $ipaddr) = each($LdapListenIPAddr)) { $ipaddr = trim($ipaddr); if ($ipaddr == null) { continue; } echo "slapd: [INFO] slapd listen `{$ipaddr}`\n"; if (!$unix->IS_IPADDR_EXISTS($ipaddr)) { echo "slapd: [INFO] slapd `{$ipaddr}` does not exists\n"; continue; } if ($EnableNonEncryptedLdapSession == 0) { $ldaps[] = "ldaps://{$ipaddr}/"; } $ldap[] = "ldap://{$ipaddr}:389/"; } } if (count($ldaps) > 0) { $SLAPD_SERVICESSSL = " " . @implode(" ", $ldaps); } $SLAPD_SERVICES = @implode(" ", $ldap) . $SLAPD_SERVICESSSL; if ($users->ZARAFA_INSTALLED) { $ZARAFA_INSTALLED = 1; } $DB_RECOVER_BIN = $unix->LOCATE_DB_RECOVER(); $DB_ARCHIVE_BIN = $unix->LOCATE_DB_ARCHIVE(); $LDAP_SCHEMA_PATH = $unix->LDAP_SCHEMA_PATH(); $rm = $unix->find_program("rm"); $SLAPD_CONF = $unix->SLAPD_CONF_PATH(); $php5 = $unix->LOCATE_PHP5_BIN(); $tar = $unix->find_program("tar"); $pidofbin = $unix->find_program("pidof"); $ulimit = $unix->find_program("ulimit"); $nohup = $unix->find_program("nohup"); $mebin = __FILE__; $suffix = @trim(@file_get_contents("/etc/artica-postfix/ldap_settings/suffix")); shell_exec("{$nohup} /usr/share/artica-postfix/exec.virtuals-ip.php --resolvconf >/dev/null 2>&1 &"); echo "slapd: [INFO] slapd `{$slapd}`\n"; echo "slapd: [INFO] db_recover `{$DB_RECOVER_BIN}`\n"; echo "slapd: [INFO] db_archive `{$DB_ARCHIVE_BIN}`\n"; echo "slapd: [INFO] config `{$SLAPD_CONF}`\n"; echo "slapd: [INFO] pid `{$SLAPD_PID_FILE}`\n"; echo "slapd: [INFO] services `{$SLAPD_SERVICES}`\n"; echo "slapd: [INFO] pidof `{$pidofbin}`\n"; if ($EnableipV6 == 0) { echo "slapd: [INFO] ipv4 only...\n"; $v4 = " -4"; } if ($GLOBALS["VERBOSE"]) { echo "-> ARRAY;\n"; } $shemas[] = "core.schema"; $shemas[] = "cosine.schema"; $shemas[] = "mod_vhost_ldap.schema"; $shemas[] = "nis.schema"; $shemas[] = "inetorgperson.schema"; $shemas[] = "evolutionperson.schema"; $shemas[] = "postfix.schema"; $shemas[] = "dhcp.schema"; $shemas[] = "samba.schema"; $shemas[] = "ISPEnv.schema"; $shemas[] = "mozilla-thunderbird.schema"; $shemas[] = "officeperson.schema"; $shemas[] = "pureftpd.schema"; $shemas[] = "joomla.schema"; $shemas[] = "autofs.schema"; $shemas[] = "dnsdomain2.schema"; $shemas[] = "zarafa.schema"; restart_ldap_progress("{starting_service}", 50); while (list($num, $file) = each($shemas)) { if (is_file("/usr/share/artica-postfix/bin/install/{$file}")) { if (is_file("{$LDAP_SCHEMA_PATH}/{$file}")) { @unlink("{$LDAP_SCHEMA_PATH}/{$file}"); } @copy("/usr/share/artica-postfix/bin/install/{$file}", "{$LDAP_SCHEMA_PATH}/{$file}"); echo "slapd: [INFO] installing `{$file}` schema\n"; $unix->chmod_func(0777, "{$LDAP_SCHEMA_PATH}/{$file}"); } } if (file_exists($ulimit)) { shell_exec("{$ulimit} -HSd unlimited"); } restart_ldap_progress("{starting_service}", 60); if (is_dir("/usr/share/phpldapadmin/config")) { $phpldapadmin = "{$php5} " . dirname(__FILE__) . "/exec.phpldapadmin.php --build >/dev/null 2>&1"; echo "slapd: [INFO] please wait, configuring PHPLdapAdminservice... \n"; shell_exec($phpldapadmin); } echo "slapd: [INFO] please wait, configuring the daemon...\n"; if ($unix->MEM_TOTAL_INSTALLEE() < 624288) { $sock = new sockets(); $sock->SET_INFO("SlapdThreads", 2); } restart_ldap_progress("{starting_service}", 70); @chmod("/usr/share/artica-postfix/bin/artica-install", 0755); shell_exec("/usr/share/artica-postfix/bin/artica-install --slapdconf"); echo "slapd: [INFO] please wait, building the start script...\n"; buildscript(); $unix->ToSyslog("Launching the OpenLDAP daemon ", false, basename(__FILE__)); echo "slapd: [INFO] please wait, Launching the daemon...\n"; if (!$unix->NETWORK_INTERFACE_OK("lo")) { $ifconfig = $unix->find_program("ifconfig"); shell_exec("{$ifconfig} lo 127.0.0.1 netmask 255.255.255.0 up >/dev/null 2>&1"); } restart_ldap_progress("{starting_service}", 80); $cdmline = "{$nohup} {$slapd}{$v4} -h \"{$SLAPD_SERVICES}\" -f {$SLAPD_CONF} -u root -g root -l local4{$OpenLDAPLogLevelCmdline} >/dev/null 2>&1 &"; shell_exec($cdmline); sleep(1); for ($i = 0; $i < 5; $i++) { $pid = $unix->get_pid_from_file($SLAPD_PID_FILE); if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); echo "slapd: [INFO] slapd success Running pid {$pid}\n"; restart_ldap_progress("{success}", 100); if ($users->ZARAFA_INSTALLED) { start_zarafa(); } return; } $pid = $unix->PIDOF($slapd); if ($unix->process_exists($pid)) { $pidtime = $unix->PROCCESS_TIME_MIN($pid); echo "slapd: [INFO] slapd success Running pid {$pid}\n"; if ($users->ZARAFA_INSTALLED) { start_zarafa(); } restart_ldap_progress("{success}", 100); return; } echo "slapd: [INFO] please wait, waiting service to start...\n"; sleep(1); } restart_ldap_progress("{failed}", 110); echo "slapd: [ERR ] Failed to start the service with `{$cdmline}`\n"; }