summaryrefslogtreecommitdiff
path: root/bin/svc_acct.export
blob: 0bc370fc0130fbdf7b8b420357f0e1656084644d (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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
#!/usr/bin/perl -w
#
# $Id: svc_acct.export,v 1.36 2002-05-16 14:28:35 ivan Exp $
#
# Create and export password, radius and vpopmail password files:
# passwd, passwd.adjunct, shadow, acp_passwd, acp_userinfo, acp_dialup
# users/assign, domains/vdomain/vpasswd
# Also export sendmail and qmail config files.

use strict;
use vars qw($conf);
use Fcntl qw(:flock);
use File::Path;
use IO::Handle;
use FS::Conf;
use Net::SSH qw(ssh);
use Net::SCP qw(scp);
use FS::UID qw(adminsuidsetup datasrc dbh);
use FS::Record qw(qsearch qsearchs fields);
use FS::svc_acct;
use FS::svc_domain;
use FS::svc_forward;

my $ssh='ssh';
my $rsync='rsync';

my $user = shift or die &usage;
adminsuidsetup $user;

$conf = new FS::Conf;

my $userpolicy = $conf->config('username_policy')
  if $conf->exists('username_policy');

my @shellmachines = $conf->config('shellmachines')
  if $conf->exists('shellmachines');

my @bsdshellmachines = $conf->config('bsdshellmachines')
  if $conf->exists('bsdshellmachines');

my @nismachines = $conf->config('nismachines')
  if $conf->exists('nismachines');

my @erpcdmachines = $conf->config('erpcdmachines')
  if $conf->exists('erpcdmachines');

my @radiusmachines = $conf->config('radiusmachines')
  if $conf->exists('radiusmachines');

my $textradiusprepend =
  $conf->exists('textradiusprepend')
    ? $conf->config('textradiusprepend')
    : '';

warn "using depriciated textradiusprepend file" if $textradiusprepend;


my $radiusprepend =
  $conf->exists('radiusprepend')
    ? join("\n", $conf->config('radiusprepend'))
    : '';

my @vpopmailmachines = $conf->config('vpopmailmachines')
  if $conf->exists('vpopmailmachines');
my $vpopmailrestart = '';
$vpopmailrestart = $conf->config('vpopmailrestart')
  if $conf->exists('vpopmailrestart');

my ($machine, $vpopdir, $vpopuid, $vpopgid) = split (/\s+/, $vpopmailmachines[0]) if $vpopmailmachines[0];

my($shellmachine, @qmailmachines);
if ( $conf->exists('qmailmachines') ) {
  $shellmachine = $conf->config('shellmachine');
  @qmailmachines = $conf->config('qmailmachines');
}

my(@sendmailmachines, $sendmailconfigpath, $sendmailrestart);
if ( $conf->exists('sendmailmachines') ) {
  @sendmailmachines = $conf->config('sendmailmachines');
  $sendmailconfigpath = $conf->config('sendmailconfigpath') || '/etc';
  $sendmailrestart = $conf->config('sendmailrestart');
}

my $mydomain = $conf->config('domain') if $conf->exists('domain');




my(@saltset)= ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
require 5.004; #srand(time|$$);

my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
my $spoollock = "/usr/local/etc/freeside/svc_acct.export.lock.". datasrc;

open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
select(EXPORT); $|=1; select(STDOUT);
unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
  seek(EXPORT,0,0);
  my($pid)=<EXPORT>;
  chop($pid);
  #no reason to start lots of blocking processes
  die "Is another export process running under pid $pid?\n";
}
seek(EXPORT,0,0);
print EXPORT $$,"\n";

my(@svc_domain)=qsearch('svc_domain',{});

( open(MASTER,">$spooldir/master.passwd")
  and flock(MASTER,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/.master.passwd: $!";
( open(PASSWD,">$spooldir/passwd")
  and flock(PASSWD,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/passwd: $!";
( open(SHADOW,">$spooldir/shadow")
  and flock(SHADOW,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/shadow: $!";
( open(ACP_PASSWD,">$spooldir/acp_passwd")
  and flock(ACP_PASSWD,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/acp_passwd: $!";
( open(ACP_DIALUP,">$spooldir/acp_dialup")
  and flock(ACP_DIALUP,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/acp_dialup: $!";
( open(USERS,">$spooldir/users")
  and flock(USERS,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/users: $!";

( open(ASSIGN,">$spooldir/assign")
  and flock(ASSIGN,LOCK_EX|LOCK_NB)  
) or die "Can't open $spooldir/assign: $!";
( open(RCPTHOSTS,">$spooldir/rcpthosts")
  and flock(RCPTHOSTS,LOCK_EX|LOCK_NB) 
) or die "Can't open $spooldir/rcpthosts: $!";
( open(VPOPRCPTHOSTS,">$spooldir/vpoprcpthosts")
  and flock(VPOPRCPTHOSTS,LOCK_EX|LOCK_NB) 
) or die "Can't open $spooldir/rcpthosts: $!";
( open(RECIPIENTMAP,">$spooldir/recipientmap") 
  and flock(RECIPIENTMAP,LOCK_EX|LOCK_NB) 
) or die "Can't open $spooldir/recipientmap: $!";
( open(VIRTUALDOMAINS,">$spooldir/virtualdomains") 
  and flock(VIRTUALDOMAINS,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/virtualdomains: $!";
( open(VPOPVIRTUALDOMAINS,">$spooldir/vpopvirtualdomains") 
  and flock(VPOPVIRTUALDOMAINS,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/virtualdomains: $!";
( open(VIRTUSERTABLE,">$spooldir/virtusertable")
  and flock(VIRTUSERTABLE,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/virtusertable: $!";
( open(SENDMAIL_CW,">$spooldir/sendmail.cw")
  and flock(SENDMAIL_CW,LOCK_EX|LOCK_NB)
) or die "Can't open $spooldir/sendmail.cw: $!";



chmod 0644, "$spooldir/passwd",
            "$spooldir/acp_dialup",
            "$spooldir/assign",
            "$spooldir/sendmail.cw",
            "$spooldir/virtusertable",
            "$spooldir/rcpthosts",
            "$spooldir/vpoprcpthosts",
            "$spooldir/recipientmap",
            "$spooldir/virtualdomains",
            "$spooldir/vpopvirtualdomains",

;
chmod 0600, "$spooldir/master.passwd",
            "$spooldir/acp_passwd",
            "$spooldir/shadow",
            "$spooldir/users",
;

rmtree"$spooldir/domains", 0, 1;
mkdir "$spooldir/domains", 0700;

setpriority(0,0,10);

print USERS "$radiusprepend\n";

my %usernames;  ## this hack helps keep the passwd files sane
my @sendmail;

my $svc_domain;
foreach $svc_domain (sort {$a->domain cmp $b->domain} @svc_domain) {

  my($domain)=$svc_domain->domain;
  print RCPTHOSTS "$domain\n.$domain\n";
  print VPOPRCPTHOSTS "$domain\n";
  print SENDMAIL_CW "$domain\n";

  ###
  # FORMAT OF THE ASSIGN/USERS FILE HERE
  print ASSIGN join(":",
    "+" . $domain . "-",
    $domain,
    $vpopuid,
    $vpopgid,
    $vpopdir . "/domains/" . $domain,
    "-",
    "",
    "",
  ), "\n" if $vpopmailmachines[0];

  (mkdir "$spooldir/domains/" . $domain, 0700)
    or die "Can't create $spooldir/domains/" . $domain .": $!";

  ( open(QMAILDEFAULT,">$spooldir/domains/" . $domain . "/.qmail-default")
    and flock(QMAILDEFAULT,LOCK_EX|LOCK_NB)  
  ) or die "Can't open $spooldir/domains/" . $domain . "/.qmail-default: $!";

  ( open(VPASSWD,">$spooldir/domains/" . $domain . "/vpasswd")
    and flock(VPASSWD,LOCK_EX|LOCK_NB)  
  ) or die "Can't open $spooldir/domains/" . $domain . "/vpasswd: $!";

  my ($svc_acct);

  if ($svc_domain->getfield('catchall')) {
    $svc_acct = qsearchs('svc_acct', {'svcnum' => $svc_domain->catchall});
    die "Cannot find catchall account for domain $domain\n" unless $svc_acct;

    my $username = $svc_acct->username;
    push @sendmail, "\@$domain\t$username\n";
    print VIRTUALDOMAINS "$domain:$username-$domain\n",
                         ".$domain:$username-$domain\n",
    ;

    ###
    # FORMAT OF THE .QMAIL-DEFAULT FILE HERE
    print QMAILDEFAULT "| $vpopdir/bin/vdelivermail \"\" " . $svc_acct->email . "\n"
      if $vpopmailmachines[0];

  }else{
    ###
    # FORMAT OF THE .QMAIL-DEFAULT FILE HERE
    print QMAILDEFAULT "| $vpopdir/bin/vdelivermail \"\" bounce-no-mailbox\n"
      if $vpopmailmachines[0];
  }

  print VPOPVIRTUALDOMAINS "$domain:$domain\n";

  foreach $svc_acct (qsearch('svc_acct', {'domsvc' => $svc_domain->svcnum})) {
    my($password)=$svc_acct->getfield('_password');
    my($cpassword,$rpassword);
    #if ( ( length($password) <= 8 )
    if ( ( length($password) <= 12 )
         && ( $password ne '*' )
         && ( $password ne '!!' )
         && ( $password ne '' )
       ) {
      $cpassword=crypt($password,
                       $saltset[int(rand(64))].$saltset[int(rand(64))]
      );
      $rpassword=$password;
    } else {
      $cpassword=$password;
      $rpassword='UNIX';
    }

    my $username;

    if ($mydomain && ($mydomain eq $svc_domain->domain)) {
      $username=$svc_acct->username;
    } elsif ($userpolicy =~ /^prepend domsvc$/) {
      $username=$svc_acct->domsvc . $svc_acct->username;
    } elsif ($userpolicy =~ /^append domsvc$/) {
      $username=$svc_acct->username . $svc_acct->domsvc;
    } elsif ($userpolicy =~ /^append domain$/) {
      $username=$svc_acct->username . $svc_domain->domain;
    } elsif ($userpolicy =~ /^append domain$/) {
      $username=$svc_acct->username . $svc_domain->domain;
    } elsif ($userpolicy =~ /^append \@domain$/) {
      $username=$svc_acct->username . '@'. $svc_domain->domain;
    } else {
      die "Unknown policy in username_policy\n";
    }

    if ($svc_acct->dir ne '/dev/null' || $svc_acct->slipip ne '') {
      if ($usernames{$username}++) {
        die "Duplicate username detected: $username\n";
      }
    }
            
    if ( $svc_acct->uid  =~ /^(\d+)$/ ) {

      die "Non-root user ". $svc_acct->username. " has 0 UID!"
        if $svc_acct->uid == 0 && $svc_acct->username ne 'root';

      if ( $svc_acct->dir ne "/dev/null") {

        ###
        # FORMAT OF FreeBSD MASTER PASSWD FILE HERE
        print MASTER join(":",
          $username,                        # User name
          $cpassword,                       # Encrypted password
          $svc_acct->uid,                   # User ID
          $svc_acct->gid,                   # Group ID
          "",                               # Login Class
          "0",                              # Password Change Time
          "0",                              # Password Expiration Time
          $svc_acct->finger,                # Users name
          $svc_acct->dir,                   # Users home directory
          $svc_acct->shell,                 # shell
        ), "\n" ;


        ###
        # FORMAT OF THE PASSWD FILE HERE
        print PASSWD join(":",
          $username,
          'x', # "##". $username,
          $svc_acct->uid,
          $svc_acct->gid,
          $svc_acct->finger,
          $svc_acct->dir,
          $svc_acct->shell,
        ), "\n";

        ###
        # FORMAT OF THE SHADOW FILE HERE
        print SHADOW join(":",
          $username,
          $cpassword,
          '',
          '',
          '',
          '',
          '',
          '',
          '',
        ), "\n";
      }
    }

    ###
    # FORMAT OF THE VPASSWD FILE HERE
    print VPASSWD join(":",
      $svc_acct->username,
      $cpassword,
      '1',
      '0',
      $svc_acct->username,
      "$vpopdir/domains/" . $svc_domain->domain ."/" . $svc_acct->username,
      'NOQUOTA',
    ), "\n";


    if ( $svc_acct->slipip ne '' ) {

      ###
      # FORMAT OF THE ACP_* FILES HERE
      print ACP_PASSWD join(":",
        $username,
        $cpassword,
        "0",
        "0",
        "",
        "",
        "",
      ), "\n";

      my($ip)=$svc_acct->slipip;

      unless ( $ip eq '0.0.0.0' || $svc_acct->slipip eq '0e0' ) {
        print ACP_DIALUP $username, "\t*\t", $svc_acct->slipip, "\n";
      }

      my %radreply = $svc_acct->radius_reply;
      my %radcheck = $svc_acct->radius_check;

      my $radcheck = join ", ", map { qq($_ = "$radcheck{$_}") } keys %radcheck;
      $radcheck .= ", " if $radcheck;

      ###
      # FORMAT OF THE USERS FILE HERE
      print USERS
        $username,
        qq(\t${textradiusprepend}),
        $radcheck,
#        qq(Password = "$rpassword"\n\t),
        join ",\n\t", map { qq($_ = "$radreply{$_}") } keys %radreply;

      #if ( $ip && $ip ne '0e0' ) {
      #  #print USERS qq(,\n\tFramed-Address = "$ip"\n\n);
      #  print USERS qq(,\n\tFramed-IP-Address = "$ip"\n\n);
      #} else {
        print USERS qq(\n\n);
      #}

    }
  
    ###
    # vpopmail directory structure creation

    (mkdir "$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username, 0700)
      or die "Can't create $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . ": $!";
    (mkdir "$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/Maildir", 0700)
      or die "Can't create $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . " /Maildir: $!";
    (mkdir "$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/Maildir/cur", 0700)
      or die "Can't create $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . " /Maildir/cur: $!";
    (mkdir "$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/Maildir/new", 0700)
      or die "Can't create $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . " /Maildir/new: $!";
    (mkdir "$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/Maildir/tmp", 0700)
      or die "Can't create $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . " /Maildir/tmp: $!";

    ( open(DOTQMAIL,">$spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/.qmail")
      and flock(DOTQMAIL,LOCK_EX|LOCK_NB)  
    ) or die "Can't open $spooldir/domains/" . $svc_domain->domain . "/" . $svc_acct->username . "/.qmail: $!";

    my($svc_forward);
    foreach $svc_forward (qsearch('svc_forward', {'srcsvc' => $svc_acct->svcnum})) {
      my($destination);
      if ($svc_forward->dstsvc) {
        my $dst_acct = qsearchs('svc_acct', {'svcnum' => $svc_forward->dstsvc});
        my $dst_domain = qsearchs('svc_domain', {'svcnum' => $dst_acct->domsvc});
        $destination = $dst_acct->username . '@' . $dst_domain->domain;

        if ($dst_domain->domain eq $mydomain) {
          print VIRTUSERTABLE $svc_acct->username . "@" . $svc_domain->domain .
            "\t" . $dst_acct->username . "\n";
          print RECIPIENTMAP $svc_acct->username . "@" . $svc_domain->domain .
            ":$destination\n";
        }
      } else {
        $destination = $svc_forward->dst;
      }
    
      ###
      # FORMAT OF .QMAIL FILES HERE
      print DOTQMAIL "$destination\n";
    }

    flock(DOTQMAIL,LOCK_UN);
    close DOTQMAIL;

  }

  flock(VPASSWD,LOCK_UN);
  flock(QMAILDEFAULT,LOCK_UN);
  close VPASSWD;
  close QMAILDEFAULT;

}

###
# FORMAT OF THE ASSIGN/USERS FILE FINAL LINE HERE
print ASSIGN ".\n";

print VIRTUSERTABLE @sendmail;

flock(MASTER,LOCK_UN);
flock(PASSWD,LOCK_UN);
flock(SHADOW,LOCK_UN);
flock(ACP_DIALUP,LOCK_UN);
flock(ACP_PASSWD,LOCK_UN);
flock(USERS,LOCK_UN);
flock(ASSIGN,LOCK_UN);
flock(SENDMAIL_CW,LOCK_UN);
flock(VIRTUSERTABLE,LOCK_UN);
flock(RCPTHOSTS,LOCK_UN);
flock(VPOPRCPTHOSTS,LOCK_UN);
flock(RECIPIENTMAP,LOCK_UN);
flock(VPOPVIRTUALDOMAINS,LOCK_UN);

close MASTER;
close PASSWD;
close SHADOW;
close ACP_DIALUP;
close ACP_PASSWD;
close USERS;
close ASSIGN;
close SENDMAIL_CW;
close VIRTUSERTABLE;
close RCPTHOSTS;
close VPOPRCPTHOSTS;
close RECIPIENTMAP;
close VPOPVIRTUALDOMAINS;

###
# export stuff
#

my($ashellmachine);
foreach $ashellmachine (@shellmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/passwd","root\@$ashellmachine:/etc/passwd.new")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/shadow","root\@$ashellmachine:/etc/shadow.new")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$ashellmachine",
    "( ".
      "mv /etc/passwd.new /etc/passwd; ".
      "mv /etc/shadow.new /etc/shadow; ".
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($bsdshellmachine);
foreach $bsdshellmachine (@bsdshellmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/passwd","root\@$bsdshellmachine:/etc/passwd.new")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/master.passwd","root\@$bsdshellmachine:/etc/master.passwd.new")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$bsdshellmachine",
    "( ".
      "mv /etc/passwd.new /etc/passwd; ".
      #"mv /etc/master.passwd.new /etc/master.passwd; ".
      "pwd_mkdb /etc/master.passwd.new; ".
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($nismachine);
foreach $nismachine (@nismachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/passwd","root\@$nismachine:/etc/global/passwd")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/shadow","root\@$nismachine:/etc/global/shadow")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$nismachine",
    "( ".
      "cd /var/yp; make; ".
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($erpcdmachine);
foreach $erpcdmachine (@erpcdmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/acp_passwd","root\@$erpcdmachine:/usr/annex/acp_passwd")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/acp_dialup","root\@$erpcdmachine:/usr/annex/acp_dialup")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$erpcdmachine",
    "( ".
      "kill -USR1 \`cat /usr/annex/erpcd.pid\'".
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($radiusmachine);
foreach $radiusmachine (@radiusmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/users","root\@$radiusmachine:/etc/raddb/users")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$radiusmachine",
    "( ".
      "builddbm".
    " )"
  )
    == 0 or die "ssh error: $!";
}

#my @args = ("/bin/tar", "c", "--force-local", "-C", "$spooldir", "-f", "$spooldir/vpoptarball", "domains");

#system {$args[0]} @args;

my($vpopmailmachine);
foreach $vpopmailmachine (@vpopmailmachines) {
  my ($machine, $vpopdir, $vpopuid, $vpopgid) = split (/\s+/, $vpopmailmachine);
  my $scp = new Net::SCP;
#  $scp->scp("$spooldir/vpoptarball","root\@$machine:vpoptarball")
#    or die "scp error: ". $scp->{errstr};
#  ssh("root\@$machine",
#    "( ".
#      "rm -rf domains; ".
#      "tar xf vpoptarball; ".
#      "chown -R $vpopuid:$vpopgid domains; ".
#      "tar cf vpoptarball domains; ".
#      "cd $vpopdir; ".
#      "tar xf ~/vpoptarball; ".
#    " )"
#  )
#    == 0 or die "ssh error: $!";

  chdir $spooldir;
  my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");

  system {$args[0]} @args;

  $scp->scp("$spooldir/assign","root\@$machine:/var/qmail/users/assign")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/vpopvirtualdomains","root\@$machine:/var/qmail/control/virtualdomains")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/vpoprcpthosts","root\@$machine:/var/qmail/control/rcpthosts")
    or die "scp error: ". $scp->{errstr};

  ssh("root\@$machine",
    "( ".
      $vpopmailrestart .
    " )"
  )
    == 0 or die "ssh error: $!";


}

my($sendmailmachine);
foreach $sendmailmachine (@sendmailmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/sendmail.cw","root\@$sendmailmachine:$sendmailconfigpath/sendmail.cw.new")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/virtusertable","root\@$sendmailmachine:$sendmailconfigpath/virtusertable.new")
    or die "scp error: ". $scp->{errstr};
  ssh("root\@$sendmailmachine",
    "( ".
      "mv $sendmailconfigpath/sendmail.cw.new $sendmailconfigpath/sendmail.cw; ".
      "mv $sendmailconfigpath/virtusertable.new $sendmailconfigpath/virtusertable; ".
      $sendmailrestart.
    " )"
  )
    == 0 or die "ssh error: $!";
}

my($qmailmachine);
foreach $qmailmachine (@qmailmachines) {
  my $scp = new Net::SCP;
  $scp->scp("$spooldir/recipientmap","root\@$qmailmachine:/var/qmail/control/recipientmap")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/virtualdomains","root\@$qmailmachine:/var/qmail/control/virtualdomains")
    or die "scp error: ". $scp->{errstr};
  $scp->scp("$spooldir/rcpthosts","root\@$qmailmachine:/var/qmail/control/rcpthosts")
    or die "scp error: ". $scp->{errstr};
  #ssh("root\@$qmailmachine","/etc/init.d/qmail restart")
  #  == 0 or die "ssh error: $!";
}

unlink $spoollock;
flock(EXPORT,LOCK_UN);
close EXPORT;

#

sub usage {
  die "Usage:\n\n  svc_acct.export user\n";
}