Mail-Postfixadmin

 view release on metacpan or  search on metacpan

lib/Mail/Postfixadmin.pm  view on Meta::CPAN

cleartext as its argument, returns the crypt.

=cut

sub cryptPassword(){
	my $self = shift;
	my $password = shift;
	my $cryptedPassword = Crypt::PasswdMD5::unix_md5_crypt($password);
	return $cryptedPassword;
}

=head3 changePassword() 

Changes the password of a user. Expects two arguments, a username and a new
password:

	$p->changePassword("user@domain.com", "password");

The salt is picked at pseudo-random; successive runs will (should) produce 
different results.

=cut

sub changePassword(){
	my $self = shift;
	my $user = shift;
	my $password = shift;
	if ($user eq ''){
		_error("No user passed to changePassword");
	}
	my $cryptedPassword = $self->cryptPassword($password);
	$self->changeCryptedPassword($user,$cryptedPassword,$password);
	return $cryptedPassword;
}

=head3 changeCryptedPassword()

changeCryptedPassword operates in exactly the same way as changePassword, but it 
expects to be passed an already-encrypted password, rather than a clear text 
one. It does no processing at all of its arguments, just writes it into the 
database.

=cut

sub changeCryptedPassword(){
	my $self = shift;
	my $user = shift;;

	if ($user eq ''){
		_error("No user passed to changeCryptedPassword");
	}
	my $cryptedPassword = shift;
	my $clearPassword = shift;

	my $query = "update $self->{'_tables'}->{'mailbox'} set ";
	$query.="`$self->{'_fields'}->{'mailbox'}->{'password'}`= '$cryptedPassword'";
	if($self->{'storeCleartextPassword'} > 0){
		$query.= ", `$self->{'_fields'}->{'mailbox'}->{'password_clear'}` = '$clearPassword'";
	}
	if($self->{'storeGPGPassword'} > 0){
		my $gpgPassword = $self->cryptPasswordGPG($clearPassword);
		$query.= ", `$self->{'_fields'}->{'mailbox'}->{'password_gpg'}` = '$gpgPassword'";
	}
	$query.="where `$self->{'_fields'}->{'mailbox'}->{'username'}` = '$user'";

	my $sth = $self->{'_dbi'}->prepare($query);
	$sth->execute();

	return $cryptedPassword;
}

=head2 Creating things

=head3 createDomain()

Expects to be passed a hash of options, with the keys being the same as those 
output by C<getDomainInfo()>. None are necessary except C<domain>.

Defaults are set as follows:

  domain       None; required.
  description  ""
  quota        MySQL's default
  transport    'virtual'
  active       1 (active)
  backupmx0    MySQL's default
  modified     now
  created      now
  aliases      MySQL's default
  maxquota     MySQL's default

Defaults are only set on keys that haven't been instantiated. If you set a key 
to an empty string, it will not be set to the default - null will be passed to 
the DB and it may set its own default.

On both success and failure the function will return a hash containing the 
options used to configure the domain - you can inspect this to see which 
defaults were used if you like.

If the domain already exists, it will not alter it, instead it will return '2' 
rather than a hash.

=cut

sub createDomain(){
	my $self = shift;
	my %opts = @_;
	my $fields;
	my $values;
	my $domain = $opts{'domain'};

	_error("No domain passed to createDomain") if $domain !~ /.+/;

	if($domain eq ''){
		_error("No domain passed to createDomain");
	}

	if ($self->domainExists($domain)){
		$self->{infostr} = "Domain '$domain' already exists";
		return 2;
	}

lib/Mail/Postfixadmin.pm  view on Meta::CPAN

	        'mailbox'       => 'mailbox',
	        'quota'         => 'quota',
	        'quota2'        => 'quota2',
	        'vacation'      => 'vacation',
	        'vacation_notification' => 'vacation_notification'
	);
	return \%tables;
}

=head3 _fields()

Returns a hashref describing the default field names. The keys are the names as used in this
module and the values should be the names of the fields themselves.

=cut

sub _fields(){
	my %fields;
	$fields{'admin'} = { 
	                        'domain'        => 'domain',
	                        'username'	=> 'username',
				'password'	=> 'password',
				'created'	=> 'created',
				'modified'	=> 'modified',
				'active'	=> 'active'
	};
	$fields{'alias'} = {
				'address'	=> 'address',
				'goto'		=> 'goto',	# Really should have been called 'target'
				'domain'	=> 'domain',
				'created'	=> 'created',
				'modified'	=> 'modified',
				'active'	=> 'active'

	};
	$fields{'domain'} = { 
	                        'domain'        => 'domain',
				'description'	=> 'description',
	                        'aliases'       => 'aliases',
	                        'mailboxes'     => 'mailboxes',
	                        'maxquota'      => 'maxquota',
	                        'quota'         => 'quota',
	                        'transport'     => 'transport',
	                        'backupmx'      => 'backupmx',
	                        'created'       => 'created',
	                        'modified'      => 'modified',
	                        'active'        => 'active'
	};
	$fields{'mailbox'} = { 
	                        'username'      => 'username',
				'password'	=> 'password',
				'name'		=> 'name',
				'maildir'	=> 'maildir',
				'quota'		=> 'quota',
				'local_part'	=> 'local_part',
				'domain'	=> 'domain',
				'created'	=> 'created',
				'modified'	=> 'modified',
				'active'	=> 'active',
				'password_clear'=> 'password_clear',
				'password_gpg'  => 'password_gpg',
	};
	$fields{'domain_admins'} = {
	                        'domain'        => 'domain',
	                        'username'      => 'username'
	};
	$fields{'alias_domain'} = {
				'alias_domain'	=> 'alias_domain',
				'target_domain' => 'target_domain',
				'created'	=> 'created',
				'modified'	=> 'modified',
				'active'	=> 'active'
	};
	return \%fields;
}


=head3 _dbCanStoreCleartestPasswords()

Attempts to ascertain whether the DB can store cleartext passwords. Basically
checks that whatever C<_fields()> reckons is the name of the field for storing
cleartext passwords in is the name of a column that exists in the db.

=cut

sub _dbCanStoreCleartextPasswords{
	my $self = shift;
	my $dbName = (split(/:/, $self->{'_params'}->{'_dbi'}))[2];
	my $tableName = $self->{'_tables'}->{'mailbox'};
	my $fieldName = $self->{'_fields'}->{'mailbox'}->{'password_clear'};
	if(_fieldExists($self->{'_dbi'}, $dbName, $tableName, $fieldName)){
		return;
	}
	return 1;
}

=head3 _createDBI()

Creates a DBI object. Called by the constructor and passed a reference
to the C<%conf> hash, containing the configuration and contructor
options.

=cut

sub _createDBI{
	my $conf = shift;
	my $dataSource = "DBI:".$conf->{'database_type'}.":".$conf->{'database_name'};
	my $username   = $conf->{'database_user'};
	my $password   = $conf->{'database_password'};
	my $dbi = DBI->connect($dataSource, $username, $password);	
	if (!$dbi){
		_warn("No dbi object created");
		return;
	}else{
		return $dbi;
	}
}

=head3 _dbInsert()

A generic sub to pawn all db inserts off onto:



( run in 0.691 second using v1.01-cache-2.11-cpan-df04353d9ac )