summaryrefslogtreecommitdiff
path: root/FS/FS/part_export/ispconfig3.pm
blob: 9d22d1995c32691b9a74c092c810ae304e238c3c (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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
package FS::part_export::ispconfig3;

use strict;

use base qw( FS::part_export );

use Data::Dumper;
use SOAP::Lite;
use IO::Socket::SSL;

=pod

=head1 NAME

FS::part_export::ispconfig3

=head1 SYNOPSIS

ISPConfig 3 integration for Freeside

=head1 DESCRIPTION

This export offers basic svc_acct provisioning for ISPConfig 3.
All email accounts will be assigned to a single specified client.

This module also provides generic methods for working through the L</ISPConfig3 API>.

=cut

use vars qw( %info );

my @yesno = (
  options => ['y','n'],
  option_labels => { 'y' => 'yes', 'n' => 'no' },
);

tie my %options, 'Tie::IxHash',
  'soap_location'      => { label   => 'SOAP Location' },
  'username'           => { label   => 'User Name',
                            default => '' },
  'password'           => { label   => 'Password',
                            default => '' },
  'debug'              => { type    => 'checkbox',
                            label   => 'Enable debug warnings' },
  'subheading'         => { type    => 'title',
                            label   => 'Account defaults' },
  'client_id'          => { label   => 'Client ID' },
  'server_id'          => { label   => 'Server ID' },
  'maildir'            => { label   => 'Maildir (substitutions from svc_acct, e.g. /mail/$domain/$username)', },
  'cc'                 => { label   => 'Cc' },
  'autoresponder_text' => { label   => 'Autoresponder text', 
                            default => 'Out of Office Reply' },
  'move_junk'          => { type    => 'select',
                            options => ['y','n'],
                            option_labels => { 'y' => 'yes', 'n' => 'no' },
                            label   => 'Move junk' },
  'postfix'            => { type    => 'select',
                            @yesno,
                            label   => 'Postfix' },
  'access'             => { type    => 'select',
                            @yesno,
                            label   => 'Access' },
  'disableimap'        => { type    => 'select',
                            @yesno,
                            label   => 'Disable IMAP' },
  'disablepop3'        => { type    => 'select',
                            @yesno,
                            label   => 'Disable POP3' },
  'disabledeliver'     => { type    => 'select',
                            @yesno,
                            label   => 'Disable deliver' },
  'disablesmtp'        => { type    => 'select',
                            @yesno,
                            label   => 'Disable SMTP' },
;

%info = (
  'svc'             => 'svc_acct',
  'desc'            => 'Export email account to ISPConfig 3',
  'options'         => \%options,
  'no_machine'      => 1,
  'notes'           => <<'END',
All email accounts will be assigned to a single specified client and server.
END
);

sub _mail_user_params {
  my ($self, $svc_acct) = @_;
  # all available api fields are in comments below, even if we don't use them
  return {
    #server_id  (int(11))
    'server_id' => $self->option('server_id'),
    #email  (varchar(255))
    'email' => $svc_acct->username.'@'.$svc_acct->domain,
    #login  (varchar(255))
    'login' => $svc_acct->username.'@'.$svc_acct->domain,
    #password  (varchar(255))
    'password' => $svc_acct->_password,
    #name  (varchar(255))
    'name' => $svc_acct->finger,
    #uid  (int(11))
    'uid' => $svc_acct->uid,
    #gid  (int(11))
    'gid' => $svc_acct->gid,
    #maildir  (varchar(255))
    'maildir' => $self->_substitute($self->option('maildir'),$svc_acct),
    #quota  (bigint(20))
    'quota' => $svc_acct->quota,
    #cc  (varchar(255))
    'cc' => $self->option('cc'),
    #homedir  (varchar(255))
    'homedir' => $svc_acct->dir,

    ## initializing with autoresponder off, but this could become an export option...
    #autoresponder  (enum('n','y'))
    'autoresponder' => 'n',
    #autoresponder_start_date  (datetime)
    #autoresponder_end_date  (datetime)
    #autoresponder_text  (mediumtext)
    'autoresponder_text' => $self->option('autoresponder_text'),

    #move_junk  (enum('n','y'))
    'move_junk' => $self->option('move_junk'),
    #postfix  (enum('n','y'))
    'postfix' => $self->option('postfix'),
    #access  (enum('n','y'))
    'access' => $self->option('access'),

    ## not needed right now, not sure what it is
	#custom_mailfilter  (mediumtext)

    #disableimap  (enum('n','y'))
    'disableimap' => $self->option('disableimap'),
    #disablepop3  (enum('n','y'))
    'disablepop3' => $self->option('disablepop3'),
    #disabledeliver  (enum('n','y'))
    'disabledeliver' => $self->option('disabledeliver'),
    #disablesmtp  (enum('n','y'))
    'disablesmtp' => $self->option('disablesmtp'),
  };
}

sub _export_insert {
  my ($self, $svc_acct) = @_;
  return $self->api_error || 'Error logging in'
    unless $self->api_login;
  my $params = $self->_mail_user_params($svc_acct);
  my $remoteid = $self->api_call('mail_user_add',$self->option('client_id'),$params);
  return $self->api_error_logout if $self->api_error;
  my $error = $self->set_remoteid($svc_acct,$remoteid);
  $error = "Remote system updated, but error setting remoteid ($remoteid): $error"
    if $error;
  $self->api_logout;
  return $error;
}

sub _export_replace {
  my ($self, $svc_acct, $svc_acct_old) = @_;
  return $self->api_error || 'Error logging in'
    unless $self->api_login;
  my $remoteid = $self->get_remoteid($svc_acct_old);
  return "Could not load remoteid for old service" unless $remoteid;
  my $params = $self->_mail_user_params($svc_acct);
  #API docs claim "Returns the number of affected rows"
  my $success = $self->api_call('mail_user_update',$self->option('client_id'),$remoteid,$params);
  return $self->api_error_logout if $self->api_error;
  return "Server returned no rows updated, but no other error message" unless $success;
  my $error = '';
  unless ($svc_acct->svcnum eq $svc_acct_old->svcnum) { # are these ever not equal?
    $error = $self->set_remoteid($svc_acct,$remoteid);
    $error = "Remote system updated, but error setting remoteid ($remoteid): $error"
      if $error;
  }
  $self->api_logout;
  return $error;
}

sub _export_delete {
  my ($self, $svc_acct) = @_;
  return $self->api_error || 'Error logging in'
    unless $self->api_login;
  my $remoteid = $self->get_remoteid($svc_acct);
  #don't abort deletion--
  #  might have been provisioned before export was implemented,
  #  still need to be able to delete from freeside
  unless ($remoteid) {
    warn "Could not load remoteid for svcnum ".$svc_acct->svcnum.", unprovisioning anyway";
    return '';
  }
  #API docs claim "Returns the number of deleted records"
  my $success = $self->api_call('mail_user_delete',$remoteid);
  return $self->api_error_logout if $self->api_error;
  #don't abort deletion--
  #  if it's already been deleted remotely,
  #  still need to be able to delete from freeside
  warn "Server returned no records deleted for svcnum ".$svc_acct->svcnum.
       " remoteid $remoteid, unprovisioning anyway"
    unless $success;
  $self->api_logout;
  return '';
}

sub _export_suspend {
  my ($self, $svc_acct) = @_;
  return '';
}

sub _export_unsuspend {
  my ($self, $svc_acct) = @_;
  return '';
}

=head1 ISPConfig3 API

These methods allow access to the ISPConfig3 API using the credentials
set in the export options.

=cut

=head2 api_call

Accepts I<$method> and I<@params>.  Places an api call to the specified
method with the specified params.  Returns the result of that call
(empty on failure.)  Retrieve error messages using L</api_error>.

Do not include session id in list of params;  it will be included
automatically.  Must run L</api_login> first.

=cut

sub api_call {
  my ($self,$method,@params) = @_;
  # This does get used by api_login,
  # to retrieve the session id after it sets the client,
  # so we only check for existence of client,
  # and we only include session id if we have one
  my $client = $self->{'__ispconfig_client'};
  unless ($client) {
    $self->{'__ispconfig_error'} = 'Not logged in';
    return;
  }
  if ($self->{'__ispconfig_session'}) {
    unshift(@params,$self->{'__ispconfig_session'});
  }
  # Contact server in eval, to trap connection errors
  warn "Calling SOAP method $method with params:\n".Dumper(\@params)."\n"
    if $self->option('debug');
  my $response = eval { $client->$method(@params) };
  unless ($response) {
    $self->{'__ispconfig_error'} = "Error contacting server: $@";
    return;
  }
  # Set results and return
  $self->{'__ispconfig_error'} = $response->fault
                               ? "Error from server: " . $response->faultstring
                               : '';
  return $response->result;
}

=head2 api_error

Returns the error string set by L</ISPConfig3 API> methods,
or a blank string if most recent call produced no errors.

=cut

sub api_error {
  my $self = shift;
  return $self->{'__ispconfig_error'} || '';
}

=head2 api_error_logout

Attempts L</api_logout>, but returns L</api_error> message from
before logout was attempted.  Useful for logging out
properly after an error.

=cut

sub api_error_logout {
  my $self = shift;
  my $error = $self->api_error;
  $self->api_logout;
  return $error;
}

=head2 api_login

Initializes an api session using the credentials for this export.
Returns true on success, false on failure.
Retrieve error messages using L</api_error>.

=cut

sub api_login {
  my $self = shift;
  if ($self->{'__ispconfig_session'} || $self->{'__ispconfig_client'}) {
    $self->{'__ispconfig_error'} = 'Already logged in';
    return;
  }
  $self->{'__ispconfig_session'} = undef;
  $self->{'__ispconfig_client'} =
    SOAP::Lite->proxy( $self->option('soap_location'),
                         ssl_opts => [
                           verify_hostname => 0,
                           SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
                         ]
                     )
    || undef;
  unless ($self->{'__ispconfig_client'}) {
    $self->{'__ispconfig_error'} = 'Error creating SOAP client';
    return;
  }
  $self->{'__ispconfig_session'} = 
    $self->api_call('login',$self->option('username'),$self->option('password'))
    || undef;
  return unless $self->{'__ispconfig_session'};
  return 1;
}

=head2 api_logout

Ends the current api session established by L</api_login>.
Returns true on success, false on failure.

=cut

sub api_logout {
  my $self = shift;
  unless ($self->{'__ispconfig_session'}) {
    $self->{'__ispconfig_error'} = 'Not logged in';
    return;
  }
  my $result = $self->api_call('logout');
  # clear these even if there was a failure to logout
  $self->{'__ispconfig_client'} = undef;
  $self->{'__ispconfig_session'} = undef;
  return if $self->api_error;
  return 1;
}

# false laziness with portaone export
sub _substitute {
  my ($self, $string, @objects) = @_;
  return '' unless $string;
  foreach my $object (@objects) {
    next unless $object;
    my @fields = $object->fields;
    push(@fields,'domain') if $object->table eq 'svc_acct';
    foreach my $field (@fields) {
      next unless $field;
      my $value = $object->$field;
      $string =~ s/\$$field/$value/g;
    }
  }
  # strip leading/trailing whitespace
  $string =~ s/^\s//g;
  $string =~ s/\s$//g;
  return $string;
}

=head1 SEE ALSO

L<FS::part_export>

=head1 AUTHOR

Jonathan Prykop 
jonathan@freeside.biz

=cut

1;