From be6d221200eda1ad6de73a32fc20ddac8d5151ff Mon Sep 17 00:00:00 2001 From: ivan Date: Tue, 19 Sep 2006 16:49:24 +0000 Subject: [PATCH] add domain_sql export for new mailserver config and modify acct_sql export for same --- FS/FS/part_export/acct_sql.pm | 14 +++ FS/FS/part_export/domain_sql.pm | 228 ++++++++++++++++++++++++++++++++++++++++ FS/FS/svc_acct.pm | 43 +++++++- 3 files changed, 283 insertions(+), 2 deletions(-) create mode 100644 FS/FS/part_export/domain_sql.pm diff --git a/FS/FS/part_export/acct_sql.pm b/FS/FS/part_export/acct_sql.pm index 4b92e80f1..e2282372f 100644 --- a/FS/FS/part_export/acct_sql.pm +++ b/FS/FS/part_export/acct_sql.pm @@ -60,6 +60,16 @@ my $postfix_courierimap_alias_map = join('\n', map "$_ $postfix_courierimap_alias_map{$_}", keys %postfix_courierimap_alias_map ); +tie my %postfix_native_mailbox_map, 'Tie::IxHash', + 'userid' => 'email', + 'uid' => 'uid', + 'gid' => 'gid', + 'password' => 'ldap_password', +; +my $postfix_native_mailbox_map = + join('\n', map "$_ $postfix_native_mailbox_map{$_}", + keys %postfix_native_mailbox_map ); + %info = ( 'svc' => 'svc_acct', 'desc' => 'Real-time export of accounts to SQL databases '. @@ -94,6 +104,10 @@ to be configured for different mail server setups. this.form.schema.value = "$postfix_courierimap_alias_map"; this.form.primary_key.value = "address"; '> +
  • { label => 'DBI data source' }, + 'username' => { label => 'Database username' }, + 'password' => { label => 'Database password' }, + 'table' => { label => 'Database table' }, + 'schema' => { label => + 'Database schema mapping to Freeside methods.', + type => 'textarea', + }, + 'static' => { label => + 'Database schema mapping to static values.', + type => 'textarea', + }, + 'primary_key' => { label => 'Database primary key' }, +; + +tie my %postfix_transport_map, + 'domain' => 'domain' +; +my $postfix_transport_map = + join('\n', map "$_ $postfix_transport_map{$_}", + keys %postfix_transport_map ); +tie my %postfix_transport_static, + 'transport' => 'virtual:', +; +my $postfix_transport_static = + join('\n', map "$_ $postfix_transport_static{$_}", + keys %postfix_transport_static ); + +%info = ( + 'svc' => 'svc_domain', + 'desc' => 'Real time export of domains to SQL databases '. + '(postfix, others?)', + 'options' => \%options, + 'notes' => <
    Use these buttons for useful presets: +
      +
    • +
    +END +); + +sub _schema_map { shift->_map('schema'); } +sub _static_map { shift->_map('static'); } + +sub _map { + my $self = shift; + map { /^\s*(\S+)\s*(\S+)\s*$/ } split("\n", $self->option(shift) ); +} + +sub _export_insert { + my($self, $svc_domain) = (shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my %record = ( map { $_ => $static{$_} } keys %static ), + ( map { $_ => $svc_domain->$_() } keys %schema ); + + my $err_or_queue = + $self->domain_sql_queue( + $svc_domain->svcnum, + 'insert', + $self->option('table'), + %record + ); + return $err_or_queue unless ref($err_or_queue); + + ''; +} + +sub _export_replace { + my($self, $new, $old) = (shift, shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my @primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $schema{$key}; + push @primary_key, $old->$keymap(); + } + } else { + my $keymap = $map{$self->option('primary_key')}; + push @primary_key, $old->$keymap(); + } + + my %record = ( map { $_ => $static{$_} } keys %static ), + ( map { $_ => $svc_domain->$_() } keys %schema ); + + my $err_or_queue = $self->domain_sql_queue( + $new->svcnum, + 'replace', + $self->option('table'), + $self->option('primary_key'), @primary_key, + %record, + ); + return $err_or_queue unless ref($err_or_queue); + ''; +} + +sub _export_delete { + my ( $self, $svc_domain ) = (shift, shift); + + my %schema = $self->_schema_map; + my %static = $self->_static_map; + + my %primary_key = (); + if ( $self->option('primary_key') =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) { + my $keymap = $map{$key}; + $primary_key{ $key } = $svc_domain->$keymap(); + } + } else { + my $keymap = $map{$self->option('primary_key')}; + $primary_key{ $self->option('primary_key') } = $svc_domain->$keymap(), + } + + my $err_or_queue = $self->domain_sql_queue( + $svc_domain->svcnum, + 'delete', + $self->option('table'), + %primary_key, + #$self->option('primary_key') => $svc_domain->$keymap(), + ); + return $err_or_queue unless ref($err_or_queue); + ''; +} + +sub domain_sql_queue { + my( $self, $svcnum, $method ) = (shift, shift, shift); + my $queue = new FS::queue { + 'svcnum' => $svcnum, + 'job' => "FS::part_export::domain_sql::domain_sql_$method", + }; + $queue->insert( + $self->option('datasrc'), + $self->option('username'), + $self->option('password'), + @_, + ) or $queue; +} + +sub domain_sql_insert { #subroutine, not method + my $dbh = domain_sql_connect(shift, shift, shift); + my( $table, %record ) = @_; + + my $sth = $dbh->prepare( + "INSERT INTO $table ( ". join(", ", keys %record). + " ) VALUES ( ". join(", ", map '?', keys %record ). " )" + ) or die $dbh->errstr; + + $sth->execute( values(%record) ) + or die "can't insert into $table table: ". $sth->errstr; + + $dbh->disconnect; +} + +sub domain_sql_delete { #subroutine, not method + my $dbh = domain_sql_connect(shift, shift, shift); + my( $table, %record ) = @_; + + my $sth = $dbh->prepare( + "DELETE FROM $table WHERE ". join(' AND ', map "$_ = ? ", keys %record ) + ) or die $dbh->errstr; + + $sth->execute( map $record{$_}, keys %record ) + or die "can't delete from $table table: ". $sth->errstr; + + $dbh->disconnect; +} + +sub domain_sql_replace { #subroutine, not method + my $dbh = domain_sql_connect(shift, shift, shift); + + my( $table, $pkey ) = ( shift, shift ); + + my %primary_key = (); + if ( $pkey =~ /,/ ) { + foreach my $key ( split(/\s*,\s*/, $pkey ) ) { + $primary_key{$key} = shift; + } + } else { + $primary_key{$pkey} = shift; + } + + my %record = @_; + + my $sth = $dbh->prepare( + "UPDATE $table". + ' SET '. join(', ', map "$_ = ?", keys %record ). + ' WHERE '. join(' AND ', map "$_ = ?", keys %primary_key ) + ) or die $dbh->errstr; + + $sth->execute( values(%record), values(%primary_key) ); + + $dbh->disconnect; +} + +sub domain_sql_connect { + #my($datasrc, $username, $password) = @_; + #DBI->connect($datasrc, $username, $password) or die $DBI::errstr; + DBI->connect(@_) or die $DBI::errstr; +} + +1; + diff --git a/FS/FS/svc_acct.pm b/FS/FS/svc_acct.pm index b201f2353..003ad90c2 100644 --- a/FS/FS/svc_acct.pm +++ b/FS/FS/svc_acct.pm @@ -1551,13 +1551,52 @@ sub crypt_password { } elsif ( $encryption eq 'md5' ) { unix_md5_crypt( $self->_password ); } elsif ( $encryption eq 'blowfish' ) { - die "unknown encryption method $encryption"; + croak "unknown encryption method $encryption"; } else { - die "unknown encryption method $encryption"; + croak "unknown encryption method $encryption"; } } } +=item ldap_password [ DEFAULT_ENCRYPTION_TYPE ] + +Returns an encrypted password in "LDAP" format, with a curly-bracked prefix +describing the format, for example, "{CRYPT}94pAVyK/4oIBk" or +"{PLAIN-MD5}5426824942db4253f87a1009fd5d2d4f". + +The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it +to work the same as the B method. + +=cut + +sub ldap_password { + my $self = shift; + #eventually should check a "password-encoding" field + if ( length($self->_password) == 13 ) { #crypt + return '{CRYPT}'. $self->_password; + } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5 + return '{MD5}'. $1; + } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish + die "Blowfish encryption not supported in this context, svcnum ". + $self->svcnum. "\n"; + } else { #plaintext + return '{PLAIN}'. self->_password; + #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt'; + #if ( $encryption eq 'crypt' ) { + # return '{CRYPT}'. crypt( + # $self->_password, + # $saltset[int(rand(64))].$saltset[int(rand(64))] + # ); + #} elsif ( $encryption eq 'md5' ) { + # unix_md5_crypt( $self->_password ); + #} elsif ( $encryption eq 'blowfish' ) { + # croak "unknown encryption method $encryption"; + #} else { + # croak "unknown encryption method $encryption"; + #} + } +} + =item virtual_maildir Returns $domain/maildirs/$username/ -- 2.11.0