summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/acct_sql.pm
blob: 9f1ae7b5c9e34840dde9ec2d1d4a0ed5acc121ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package FS::part_export::acct_sql;

use vars qw(@ISA %info);
use Tie::IxHash;
#use Digest::MD5 qw(md5_hex);
use FS::Record; #qw(qsearchs);
use FS::part_export;

@ISA = qw(FS::part_export);

tie my %options, 'Tie::IxHash',
  'datasrc'            => { 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' },
  'crypt'              => { label => 'Password encryption',
                            type=>'select', options=>[qw(crypt md5)],
                            default=>'crypt',
                          },
;

tie my %vpopmail_map, 'Tie::IxHash',
  'pw_name'   => 'username',
  'pw_domain' => 'domain',
  'pw_passwd' => 'crypt_password',
  'pw_uid'    => 'uid',
  'pw_gid'    => 'gid',
  'pw_gecos'  => 'finger',
  'pw_dir'    => 'dir',
  #'pw_shell'  => 'shell',
  'pw_shell'  => 'quota',
;
my $vpopmail_map = join('\n', map "$_ $vpopmail_map{$_}", keys %vpopmail_map );

tie my %postfix_courierimap_mailbox_map, 'Tie::IxHash',
  'username' => 'email',
  'password' => '_password',
  'crypt'    => 'crypt_password',
  'name'     => 'finger',
  'maildir'  => 'virtual_maildir',
  'domain'   => 'domain',
  'svcnum'   => 'svcnum',
;
my $postfix_courierimap_mailbox_map =
  join('\n', map "$_ $postfix_courierimap_mailbox_map{$_}",
                 keys %postfix_courierimap_mailbox_map      );

tie my %postfix_courierimap_alias_map, 'Tie::IxHash',
  'address' => 'email',
  'goto'    => 'email',
  'domain'  => 'domain',
  'svcnum'  => 'svcnum',
;
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',
  'mail'     => 'domain_slash_username',
;
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 '.
                '(vpopmail, Postfix+Courier IMAP, others?)',
  'options'  => \%options,
  'nodomain' => '',
  'notes'    => <<END
Export accounts (svc_acct records) to SQL databases.  Currently has default
configurations for vpopmail and Postfix+Courier IMAP but intended to be
configurable for other schemas as well.

<BR><BR>In contrast to sqlmail, this is intended to export just svc_acct
records only, rather than a single export for svc_acct, svc_forward and
svc_domain records, to export in "default" database schemas rather than
configure the MTA or POP/IMAP server for a Freeside-specific schema, and
to be configured for different mail server setups.

<BR><BR>Use these buttons for some useful presets:
<UL>
  <li><INPUT TYPE="button" VALUE="vpopmail" onClick='
    this.form.table.value = "vpopmail";
    this.form.schema.value = "$vpopmail_map";
    this.form.primary_key.value = "pw_name, pw_domain";
  '>
  <LI><INPUT TYPE="button" VALUE="postfix_courierimap_mailbox" onClick='
    this.form.table.value = "mailbox";
    this.form.schema.value = "$postfix_courierimap_mailbox_map";
    this.form.primary_key.value = "username";
  '>
  <LI><INPUT TYPE="button" VALUE="postfix_courierimap_alias" onClick='
    this.form.table.value = "alias";
    this.form.schema.value = "$postfix_courierimap_alias_map";
    this.form.primary_key.value = "address";
  '>
  <LI><INPUT TYPE="button" VALUE="postfix_native_mailbox" onClick='
    this.form.table.value = "users";
    this.form.schema.value = "$postfix_native_mailbox_map";
    this.form.primary_key.value = "userid";
  '>
</UL>
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 rebless { shift; }

sub _export_insert {
  my($self, $svc_acct) = (shift, shift);

  my %schema = $self->_schema_map;
  my %static = $self->_static_map;

  my %record = (

    ( map { $_ => $static{$_} } keys %static ),
  
    ( map { my $value = $schema{$_};
            my @arg = ();
            push @arg, $self->option('crypt')
              if $value eq 'crypt_password' && $self->option('crypt');
            $_ => $svc_acct->$value(@arg);
          } keys %schema
    ),

  );

  my $err_or_queue =
    $self->acct_sql_queue(
      $svc_acct->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 = $schema{$self->option('primary_key')};
    push @primary_key, $old->$keymap();
  }

  my %record = (

    ( map { $_ => $static{$_} } keys %static ),
  
    ( map { my $value = $schema{$_};
            my @arg = ();
            push @arg, $self->option('crypt')
              if $value eq 'crypt_password' && $self->option('crypt');
            $_ => $new->$value(@arg);
          } keys %schema
    ),

  );

  my $err_or_queue = $self->acct_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_acct ) = (shift, shift);

  my %schema = $self->_schema_map;

  my %primary_key = ();
  if ( $self->option('primary_key') =~ /,/ ) {
    foreach my $key ( split(/\s*,\s*/, $self->option('primary_key') ) ) {
      my $keymap = $schema{$key};
      $primary_key{ $key } = $svc_acct->$keymap();
    }
  } else {
    my $keymap = $schema{$self->option('primary_key')};
    $primary_key{ $self->option('primary_key') } = $svc_acct->$keymap(),
  }

  my $err_or_queue = $self->acct_sql_queue(
    $svc_acct->svcnum,
    'delete',
    $self->option('table'),
    %primary_key,
    #$self->option('primary_key') => $svc_acct->$keymap(),
  );
  return $err_or_queue unless ref($err_or_queue);
  '';
}

sub acct_sql_queue {
  my( $self, $svcnum, $method ) = (shift, shift, shift);
  my $queue = new FS::queue {
    'svcnum' => $svcnum,
    'job'    => "FS::part_export::acct_sql::acct_sql_$method",
  };
  $queue->insert(
    $self->option('datasrc'),
    $self->option('username'),
    $self->option('password'),
    @_,
  ) or $queue;
}

sub acct_sql_insert { #subroutine, not method
  my $dbh = acct_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 acct_sql_delete { #subroutine, not method
  my $dbh = acct_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 acct_sql_replace { #subroutine, not method
  my $dbh = acct_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 acct_sql_connect {
  #my($datasrc, $username, $password) = @_;
  #DBI->connect($datasrc, $username, $password) or die $DBI::errstr;
  DBI->connect(@_) or die $DBI::errstr;
}

1;