next step config is now also a command object and added better&shorter passwords

This commit is contained in:
Ralf Becker 2007-12-11 01:28:07 +00:00
parent ab3c470388
commit f0e0d84e55
6 changed files with 453 additions and 146 deletions

View File

@ -180,10 +180,6 @@ abstract class setup_cmd extends admin_cmd
}
}
}
/* if ($domain)
{
self::$egw_setup->ConfigDomain = $domain;
}*/
}
/**
@ -335,4 +331,29 @@ abstract class setup_cmd extends admin_cmd
return $msg;
}
/**
* Return a rand string, eg. to generate passwords
*
* @param int $len=16
* @return string
*/
static function randomstring($len=16)
{
static $usedchars = array(
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
'w','x','y','z','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'@','!','$','%','&','/','(',')','=','?',';',':','#','_','-','<',
'>','|','{','[',']','}', // dont add ,'" as we have problems dealing with them
);
$str = '';
for($i=0; $i < $len; $i++)
{
$str .= $usedchars[mt_rand(0,count($usedchars)-1)];
}
return $str;
}
}

View File

@ -0,0 +1,384 @@
<?php
/**
* eGgroupWare setup - create / change eGW configuration
*
* @link http://www.egroupware.org
* @author Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @package setup
* @copyright (c) 2007 by Ralf Becker <RalfBecker-AT-outdoor-training.de>
* @license http://opensource.org/licenses/gpl-license.php GPL - GNU General Public License
* @version $Id$
*/
/**
* setup command: create / change eGW configuration
*/
class setup_cmd_config extends setup_cmd
{
/**
* Constructor
*
* @param string $domain string with domain-name or array with all arguments
* @param string $config_user=null user to config the domain (or header_admin_user)
* @param string $config_passwd=null pw of above user
* @param string $arguments=null array with command line argruments
* @param boolean $verbose=false if true, echos out some status information during the run
*/
function __construct($domain,$config_user=null,$config_passwd=null,$arguments=null,$verbose=false)
{
if (!is_array($domain))
{
$domain = array(
'domain' => $domain,
'config_user' => $config_user,
'config_passwd' => $config_passwd,
'arguments' => $arguments,
'verbose' => $verbose,
);
}
//echo __CLASS__.'::__construct()'; _debug_array($domain);
admin_cmd::__construct($domain);
}
/**
* test or create database
*
* @param boolean $check_only=false only run the checks (and throw the exceptions), but not the command itself
* @return string serialized $GLOBALS defined in the header.inc.php
* @throws Exception(lang('Wrong credentials to access the header.inc.php file!'),2);
* @throws Exception('header.inc.php not found!');
*/
protected function exec($check_only=false)
{
if ($check_only && $this->remote_id)
{
return true; // can only check locally
}
// instanciate setup object and check authorisation
$this->check_setup_auth($this->config_user,$this->config_passwd,$this->domain);
$this->check_installed($this->domain,15,$this->verbose);
$values = array();
if ($this->arguments) // we have command line arguments
{
$save_ea_profile = $this->_parse_cli_arguments($values);
}
else
{
$save_ea_profile = $this->_parse_properties($values);
}
// store the config
foreach($values as $name => $value)
{
self::$egw_setup->db->insert(self::$egw_setup->config_table,array(
'config_value' => $value,
),array(
'config_app' => 'phpgwapi',
'config_name' => $name,
),__LINE__,__FILE__);
}
if (count($values))
{
if ($save_ea_profile) $this->_save_ea_profile();
return lang('Configuration changed.');
}
return lang('Nothing to change.');
}
/**
* Return or echo the most common config options
*
* @param boolean $echoit=false if true the config is additionally echo'ed out
* @return array with name => value pairs
*/
static function get_config($echoit=false)
{
self::$egw_setup->db->select(self::$egw_setup->config_table,'config_name,config_value',array(
'config_app' => 'phpgwapi',
"(config_name LIKE '%\\_dir' OR (config_name LIKE 'mail%' AND config_name != 'mail_footer') OR config_name LIKE 'smtp\\_%' OR config_name LIKE 'ldap%' OR config_name IN ('webserver_url','system_charset','auth_type','account_repository'))",
),__LINE__,__FILE__);
$config = array();
while (($row = self::$egw_setup->db->row(true)))
{
$config[$row['config_name']] = $row['config_value'];
}
if ($echoit)
{
echo lang('Current configuration:')."\n";
foreach($config as $name => $value)
{
echo str_pad($name.':',22).$value."\n";
}
}
return $config;
}
/**
* Available options and allowed arguments
*
* @var array
*/
static $options = array(
'--config' => array(), // name=value,...
'--files-dir' => 'files_dir',
'--backup-dir' => 'backup_dir',
'--temp-dir' => 'temp_dir',
'--webserver-url' => 'webserver_url',
'--mailserver' => array( //server,{IMAP|IMAPS|POP|POPS},[domain],[{standard(default)|vmailmgr = add domain for mailserver login}]
'mail_server',
array('name' => 'mail_server_type','allowed' => array('imap','imaps','pop3','pop3s'),'default'=>'imap'),
'mail_suffix',
array('name' => 'mail_login_type','allowed' => array(
'username (standard)' => 'standard','username@domain (virtual mail manager)' => 'vmailmgr',
),'default'=>'standard'),
),
'--cyrus' => array(
'imapAdminUsername',
'imapAdminPW',
array('name' => 'imapType','default' => 3),
array('name' => 'imapEnableCyrusAdmin','default' => 'yes'),
),
'--sieve' => array(
array('name' => 'imapSieveServer','default' => 'localhost'),
array('name' => 'imapSievePort','default' => 2000),
array('name' => 'imapEnableSieve','default' => 'yes'), // null or yes
),
'--postfix' => array(
array('name' => 'editforwardingaddress','allowed' => array('yes',null)),
array('name' => 'smtpType','default' => 2),
),
'--smtpserver' => array( //smtp server,[smtp port],[smtp user],[smtp password]
'smtp_server',array('name' => 'smtp_port','default' => 25),'smtp_auth_user','smtp_auth_passwd',''
),
'--account-auth' => array(
array('name' => 'account_repository','allowed' => array('sql','ldap'),'default'=>'sql'),
array('name' => 'auth_type','allowed' => array('sql','ldap','mail','ads','http','sqlssl','nis','pam'),'default'=>'sql'),
array('name' => 'sql_encryption','allowed' => array('md5','blowfish_crypt','md5_crypt','crypt'),'default'=>'md5'),
'check_save_password','allow_cookie_auth'),
'--ldap-host' => 'ldap_host',
'--ldap-root-dn' => 'ldap_root_dn',
'--ldap-root-pw' => 'ldap_root_pw',
'--ldap-context' => 'ldap_context',
'--ldap-search-filter' => 'ldap_search_filter',
'--ldap-group-context' => 'ldap_group_context',
);
/**
* Parses properties from this object
*
* @param array &$value contains set values on return
* @return boolean do we need to save the emailadmin profile
*/
private function _parse_properties(&$values)
{
$this->_merge_defaults();
$save_ea_profile = false;
$values = array();
foreach(self::$options as $arg => $option)
{
foreach(is_array($option) ? $option : array($option) as $n => $data)
{
$name = is_array($data) ? $data['name'] : $data;
if (isset($this->$name))
{
$save_ea_profile |= $this->_parse_value($arg,$n,$option,$this->$name,$values);
}
}
}
return $save_ea_profile;
}
/**
* Parses command line arguments in $this->arguments
*
* @param array &$value contains set values on return
* @return boolean do we need to save the emailadmin profile
*/
private function _parse_cli_arguments(&$values)
{
$arguments = $this->arguments;
$values = array();
$save_ea_profile = false;
$args = $this->arguments;
while(($arg = array_shift($args)))
{
if (!isset(self::$options[$arg]))
{
throw new egw_exception_wrong_userinput(lang("Unknown option '%1' !!!",$arg),90);
}
$options = is_array(self::$options[$arg]) ? explode(',',array_shift($args)) : array(array_shift($args));
if ($arg == '--config')
{
foreach($options as $option)
{
list($name,$value) = explode('=',$option,2);
$values[$name] = $value;
}
continue;
}
$options[] = ''; $options[] = '';
foreach($options as $n => $value)
{
$save_ea_profile |= $this->_parse_value($arg,$n,self::$options[$arg],$value,$values);
}
}
return $save_ea_profile;
}
/**
* Parses a single value
*
* @param string $arg current cli argument processed
* @param int $n number of the property
* @param array/string $data string with type or array containing values for type, allowed
* @param mixed $value value to set
* @param array &$values where the values get set
*/
private function _parse_value($arg,$n,$data,$value,array &$values)
{
if ($value === '' && is_array($data) && !isset($data[$n]['default'])) return false;
$name = is_array($data) || $n ? $data[$n] : $data;
if (is_array($name))
{
if (!$value && isset($name['default'])) $value = $name['default'];
if (isset($name['allowed']) && !in_array($value,$name['allowed']))
{
throw new egw_exception_wrong_userinput(lang("'%1' is not allowed as %2. arguments of option %3 !!!",$value,1+$n,$arg)." ($name[name])",91);
}
$name = $name['name'];
}
$values[$name] = $value;
return in_array($arg,array('--mailserver','--smtpserver','--cyrus','--postfix','--sieve'));
}
/**
* Updates the default EMailAdmin profile from the eGW config
*/
function _save_ea_profile($config=array())
{
self::$egw_setup->db->select(self::$egw_setup->config_table,'config_name,config_value',array(
'config_app' => 'phpgwapi',
"((config_name LIKE 'mail%' AND config_name != 'mail_footer') OR config_name LIKE 'smtp%' OR config_name LIKE 'imap%' OR config_name='editforwardingaddress')",
),__LINE__,__FILE__);
while (($row = self::$egw_setup->db->row(true)))
{
$config[$row['config_name']] = $row['config_value'];
}
$config['smtpAuth'] = $config['smtp_auth_user'] ? 'yes' : null;
require_once(EGW_INCLUDE_ROOT.'/emailadmin/inc/class.bo.inc.php');
$emailadmin = new bo(-1,false); // false=no session stuff
$emailadmin->setDefaultProfile($config);
if ($this->verbose)
{
echo "\n".lang('EMailAdmin profile updated:')."\n";
foreach($config as $name => $value)
{
echo str_pad($name.':',22).$value."\n";
}
}
}
/**
* Return the options from the $options array
*
* @return array with name => array(value=>label,...) pairs
*/
static function options()
{
$options = array();
foreach(self::$options as $option)
{
if (is_array($option))
{
foreach($option as $n => $data)
{
if (is_array($data) && isset($data['allowed']))
{
foreach($data['allowed'] as $label => $value)
{
if (is_int($label))
{
$label = (string) $value === '' ? 'No' : strtoupper($value);
}
$options[$data['name']][$value] = lang($label);
}
}
}
}
}
return $options;
}
/**
* Return the defaults from the $options array
*
* @return array with name => $value pairs
*/
static function defaults()
{
$defaults = array();
// fetch the default from the cli options
foreach(self::$options as $option)
{
if (is_array($option))
{
foreach($option as $n => $data)
{
if (is_array($data) && isset($data['default']))
{
$defaults[$data['name']] = $data['default'];
}
}
}
}
// some extra defaults for non-cli operation
$defaults['files_dir'] = '/var/lib/egroupware/$domain/files';
$defaults['backup_dir'] = '/var/lib/egroupware/$domain/backup';
$defaults['temp_dir'] = '/tmp';
$defaults['webserver_url'] = '/egroupware';
$defaults['smtp_server'] = $defaults['mail_server'] = 'localhost';
$defaults['mail_suffix'] = '$domain';
$defaults['imapAdminUsername'] = 'cyrus@$domain';
$defaults['imapAdminPW'] = self::randomstring();
return $defaults;
}
/**
* Merges the default into the current properties, if they are empty or contain placeholders
*
* Replacements like $domain, only work for values listed by self::defaults()
*/
private function _merge_defaults()
{
foreach(self::defaults() as $name => $default)
{
if (!$this->$name)
{
//echo "<p>setting $name='{$this->$name}' to it's default='$default'</p>\n";
$this->set_defaults[$name] = $this->$name = $default;
}
if (strpos($this->$name,'$') !== false)
{
$this->$name = str_replace(array(
'$domain',
),array(
$this->domain,
),$this->$name);
}
}
}
}

View File

@ -203,7 +203,7 @@ class setup_cmd_database extends setup_cmd
'db_port' => 3306,
'db_name' => 'egw_$domain',
'db_user' => 'egw_$domain',
'db_pass' => md5(microtime(true).$domain.session_id()),
'db_pass' => self::randomstring(),
'db_root' => 'root',
'db_root_pw' => '', // not really a default
'db_meta' => $meta_db,
@ -216,7 +216,7 @@ class setup_cmd_database extends setup_cmd
*/
private function _merge_defaults()
{
foreach(self::defaults($this->domain) as $name => $default)
foreach(self::defaults() as $name => $default)
{
if (!$this->$name)
{

View File

@ -23,6 +23,7 @@ class setup_cmd_install extends setup_cmd
* @param string $config_passwd=null pw of above user
* @param string $backup=null filename of backup to use instead of new install, default new install
* @param string $charset='utf-8' charset for the install, default utf-8 now
* @param boolean $verbose=false if true, echos out some status information during the run
*/
function __construct($domain,$config_user=null,$config_passwd=null,$backup=null,$charset='utf-8',$verbose=false)
{

View File

@ -32,16 +32,16 @@ class setup_cmd_ldap extends setup_cmd
* @param string $ldap_admin=null root-dn needed to create new entries in the suffix
* @param string $ldap_admin_pw=null
* @param string $ldap_base=null base of the instance, default "o=$domain,$suffix"
* @param string $ldap_root=null root-dn used for the instance, default "cn=admin,$base"
* @param string $ldap_root_dn=null root-dn used for the instance, default "cn=admin,$base"
* @param string $ldap_root_pw=null
* @param string $ldap_context=null ou for accounts, default "ou=accounts,$base"
* @param string $ldap_search_filter=null search-filter for accounts, default "(uid=%user)"
* @param string $ldap_context_group=null ou for groups, default "ou=groups,$base"
* @param string $ldap_group_context=null ou for groups, default "ou=groups,$base"
* @param string $sub_command='create_ldap' 'create_ldap', 'test_ldap', 'test_ldap_root'
*/
function __construct($domain,$ldap_host=null,$ldap_suffix=null,$ldap_admin=null,$ldap_admin_pw=null,
$ldap_base=null,$ldap_root=null,$ldap_root_pw=null,$ldap_context=null,$ldap_search_filter=null,
$ldap_context_group=null,$sub_command='create_ldap')
$ldap_base=null,$ldap_root_dn=null,$ldap_root_pw=null,$ldap_context=null,$ldap_search_filter=null,
$ldap_group_context=null,$sub_command='create_ldap')
{
if (!is_array($domain))
{
@ -52,11 +52,11 @@ class setup_cmd_ldap extends setup_cmd
'ldap_admin' => $ldap_admin,
'ldap_admin_pw' => $ldap_admin_pw,
'ldap_base' => $ldap_base,
'ldap_root' => $ldap_root,
'ldap_root_dn' => $ldap_root_dn,
'ldap_root_pw' => $ldap_root_pw,
'ldap_context' => $ldap_context,
'ldap_search_filter' => $ldap_search_filter,
'ldap_context_group' => $ldap_context_group,
'ldap_group_context' => $ldap_group_context,
'sub_command' => $sub_command
);
}
@ -83,7 +83,6 @@ class setup_cmd_ldap extends setup_cmd
$this->_merge_defaults();
//_debug_array($this->as_array());
try {
switch($this->sub_command)
{
case 'test_ldap_root':
@ -97,29 +96,19 @@ class setup_cmd_ldap extends setup_cmd
$msg = $this->create();
break;
}
}
catch (Exception $e) {
// we catch the exception to properly restore the db
}
$this->restore_db();
if ($e)
{
throw $e;
}
return $msg;
}
/**
* Connect to ldap server
*
* @param string $dn=null default $this->ldap_root
* @param string $dn=null default $this->ldap_root_dn
* @param string $pw=null default $this->ldap_root_pw
* @throws egw_exception_wrong_userinput Can not connect to ldap ...
*/
private function connect($dn=null,$pw=null)
{
if (is_null($dn)) $dn = $this->ldap_root;
if (is_null($dn)) $dn = $this->ldap_root_dn;
if (is_null($pw)) $pw = $this->ldap_root_pw;
if (!$pw) // ldap::ldapConnect use the current eGW's pw otherwise
@ -158,11 +147,11 @@ class setup_cmd_ldap extends setup_cmd
foreach(array(
$this->ldap_base => array(),
$this->ldap_context => array(),
$this->ldap_context_group => array(),
$this->ldap_root => array('userPassword' => '{crypt}'.crypt($this->ldap_root_pw)),
$this->ldap_group_context => array(),
$this->ldap_root_dn => array('userPassword' => '{crypt}'.crypt($this->ldap_root_pw)),
) as $dn => $extra)
{
if (!$this->_create_node($dn,$extra,$check_only) && $dn == $this->ldap_root)
if (!$this->_create_node($dn,$extra,$check_only) && $dn == $this->ldap_root_dn)
{
// ldap_root already existed, lets check the pw is correct
$this->connect();
@ -240,11 +229,11 @@ class setup_cmd_ldap extends setup_cmd
'ldap_admin' => 'cn=admin,$suffix',
'ldap_admin_pw' => '',
'ldap_base' => 'o=$domain,$suffix',
'ldap_root' => 'cn=admin,$base',
'ldap_root_pw' => md5(microtime(true).$domain.session_id()),
'ldap_root_dn' => 'cn=admin,$base',
'ldap_root_pw' => self::randomstring(),
'ldap_context' => 'ou=accounts,$base',
'ldap_search_filter' => '(uid=%user)',
'ldap_context_group' => 'ou=groups,$base',
'ldap_group_context' => 'ou=groups,$base',
);
}
@ -253,7 +242,7 @@ class setup_cmd_ldap extends setup_cmd
*/
private function _merge_defaults()
{
foreach(self::defaults($this->domain) as $name => $default)
foreach(self::defaults() as $name => $default)
{
if (!$this->$name)
{

View File

@ -120,110 +120,22 @@ exit(0);
*/
function do_config($args)
{
$options = _check_auth_config(array_shift($args),15);
$arg0 = explode(',',array_shift($args));
if (!($domain = @array_shift($arg0))) $domain = 'default';
$user = @array_shift($arg0);
$password = @array_shift($arg0);
_fetch_user_password($user,$password);
$values = array();
foreach($options as $option)
if ($arg0) // direct assignments (name=value,...) left
{
list($name,$value) = explode('=',$option,2);
$values[$name] = $value;
array_unshift($args,implode(',',$arg0));
array_unshift($args,'--config');
}
static $config = array(
'--files-dir' => 'files_dir',
'--backup-dir' => 'backup_dir',
'--temp-dir' => 'temp_dir',
'--webserver-url' => 'webserver_url',
'--mailserver' => array( //server,{IMAP|IMAPS|POP|POPS},[domain],[{standard(default)|vmailmgr = add domain for mailserver login}]
'mail_server',
array('name' => 'mail_server_type','allowed' => array('imap','imaps','pop3','pop3s')),
'mail_suffix',
array('name' => 'mail_login_type','allowed' => array('standard','vmailmgr')),
),
'--cyrus' => array(
'imapAdminUsername',
'imapAdminPW',
array('name' => 'imapType','default' => 3),
array('name' => 'imapEnableCyrusAdmin','default' => 'yes'),
),
'--sieve' => array(
array('name' => 'imapSieveServer','default' => 'localhost'),
array('name' => 'imapSievePort','default' => 2000),
array('name' => 'imapEnableSieve','default' => 'yes'), // null or yes
),
'--postfix' => array(
array('name' => 'editforwardingaddress','allowed' => array('yes',null)),
array('name' => 'smtpType','default' => 2),
),
'--smtpserver' => array( //smtp server,[smtp port],[smtp user],[smtp password]
'smtp_server','smtp_port','smtp_auth_user','smtp_auth_passwd',''
),
'--account-auth' => array(
array('name' => 'account_repository','allowed' => array('sql','ldap')),
array('name' => 'auth_type','allowed' => array('sql','ldap','mail','ads','http','sqlssl','nis','pam')),
array('name' => 'sql_encryption','allowed' => array('md5','blowfish_crypt','md5_crypt','crypt')),
'check_save_password','allow_cookie_auth'),
'--ldap-host' => 'ldap_host',
'--ldap-root-dn' => 'ldap_root_dn',
'--ldap-root-pw' => 'ldap_root_pw',
'--ldap-context' => 'ldap_context',
'--ldap-group-context' => 'ldap_group_context',
);
$do_ea_profile = false;
while (($arg = array_shift($args)))
{
if (!isset($config[$arg])) fail(90,lang("Unknown option '%1' !!!",$arg));
$options = array();
if (substr($args[0],0,2) !== '--')
{
$options = is_array($config[$arg]) ? explode(',',array_shift($args)) : array(array_shift($args));
}
$options[] = ''; $options[] = '';
foreach($options as $n => $value)
{
if ($value === '' && is_array($config[$arg]) && !isset($config[$arg][$n]['default'])) continue;
$cmd = new setup_cmd_config($domain,$user,$password,$args,true);
echo $cmd->run()."\n\n";
$name = is_array($config[$arg]) || $n ? $config[$arg][$n] : $config[$arg];
if (is_array($name))
{
if (isset($name['allowed']) && !in_array($value,$name['allowed']))
{
fail(91,lang("'%1' is not allowed as %2. arguments of option %3 !!!",$value,1+$n,$arg));
}
if (!$value && isset($name['default'])) $value = $name['default'];
$name = $name['name'];
}
$values[$name] = $value;
}
if (in_array($arg,array('--mailserver','--smtpserver','--cyrus','--postfix','--sieve')))
{
$do_ea_profile = true;
}
}
foreach($values as $name => $value)
{
$GLOBALS['egw_setup']->db->insert($GLOBALS['egw_setup']->config_table,array(
'config_value' => $value,
),array(
'config_app' => 'phpgwapi',
'config_name' => $name,
),__LINE__,__FILE__);
}
if (count($values))
{
echo lang('Configuration changed.')."\n";
if ($do_ea_profile) do_emailadmin($values);
}
echo "\n".lang('Current configuration:')."\n";
$GLOBALS['egw_setup']->db->select($GLOBALS['egw_setup']->config_table,'config_name,config_value',array(
'config_app' => 'phpgwapi',
"(config_name LIKE '%\\_dir' OR (config_name LIKE 'mail%' AND config_name != 'mail_footer') OR config_name LIKE 'smtp\\_%' OR config_name LIKE 'ldap%' OR config_name IN ('webserver_url','system_charset','auth_type','account_repository'))",
),__LINE__,__FILE__);
while (($row = $GLOBALS['egw_setup']->db->row(true)))
{
echo str_pad($row['config_name'].':',22).$row['config_value']."\n";
}
$cmd->get_config(true);
}
/**
@ -458,8 +370,6 @@ function _check_auth_config($arg,$stop,$set_lang=true)
*/
function do_install($args)
{
global $setup_info;
list($domain,$user,$password,$backup,$charset) = explode(',',$args);
_fetch_user_password($user,$password);
@ -642,6 +552,8 @@ function fail($exit_code,$message)
*
* The list is generated by "greping" this file for calls to the fail() function.
* Calls to fail() have to be in one line, to be recogniced!
*
* @todo we need to grep for the exceptions too!
*/
function list_exit_codes()
{