summaryrefslogtreecommitdiff
path: root/bin/ispman.ldap.import
blob: 7495f47f8019e06f49a97b42a3ed5fea916283f2 (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
#!/usr/bin/perl -w

use strict;
use Net::LDAP::LDIF;

use FS::UID qw(adminsuidsetup);
use FS::Record qw(qsearchs);
use FS::svc_domain;
use FS::svc_acct;

my $user = shift or die;
adminsuidsetup($user);

$FS::svc_Common::noexport_hack = 1;
$FS::svc_domain::whois_hack = 1;

my $domain_svcpart = 1;
my $account_svcpart = 2;
my $mailbox_svcpart = 3;
my $fedweeknet_svcpart = 4;

#my $ldif =
#  Net::LDAP::LDIF->new( "ispman-06-23-04.ldif", "r", onerror => 'undef' );
my $ldif =
  Net::LDAP::LDIF->new( "ispman-06-23-04.ldif", "r", onerror => 'warn' );

#my %objectclass;

my $acct = 0;
my $imported = 0;

my $entry;
while ( $entry = $ldif->read_entry ) {
  #warn "$entry\n";
  my %attributes = map { $_ => [ $entry->get_value( $_ ) ]  } $entry->attributes;

  my $objectclass = join('/', @{$attributes{'objectclass'}} );

  next unless $objectclass eq 'posixAccount/ispmanDomainUser/radiusprofile';

  foreach my $attr ( keys %attributes ) {
    print join( " => ", substr($attr.' 'x30,0,30), @{$attributes{ $attr }} ), "\n";
    #if ( $attr eq 'objectclass' ) {
    #  $objectclass{ join('/', @{$attributes{$attr}} ) }++;
    #}
  }
  print "\n";

  $acct++;

  my $email = $attributes{'maillocaladdress'}->[0];
  $email =~ /^(\w+)\@([\w\.\-]+)$/ or die $email;
  die "$1 ne ". $attributes{'ispmanuserid'}->[0]. "\n"
    unless lc($1) eq $attributes{'ispmanuserid'}->[0];
  my $username = lc($1);
  my $domain = lc($2);

  my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
                 || new FS::svc_domain { 'svcpart' => $domain_svcpart,
                                         'domain'  => $domain,
                                         'action'  => 'N',
                                       };

  unless ( $svc_domain->svcnum ) {
    my $error = $svc_domain->insert;
    if ( $error ) {
      die "inserting domain: $error\n";
    }
  }

  ( my $password = $attributes{'userpassword'}->[0] ) =~ s/^\{crypt\}//;

  # pick svcpart
  my $svcpart = $account_svcpart;
  if ( $domain eq 'fedweeknet.com' ) {
    $svcpart = $fedweeknet_svcpart;
  } elsif ( $attributes{'dialupaccess'}->[0] =~ /(false|no)/i ) {
    $svcpart = $mailbox_svcpart;
  }

  my $dir = $attributes{'homedirectory'}->[0];
  $dir =~ s/\s+//g;
  $dir =~ s/\@/_/;

  my $svc_acct = new FS::svc_acct {
    'svcpart'   => $svcpart,
    'username'  => $username,
    '_password' => $password,
    'finger'    => $attributes{'cn'}->[0],
    'domsvc'    => $svc_domain->svcnum,
    'shell'     => $attributes{'loginshell'}->[0],
    'uid'       => $attributes{'uidnumber'}->[0],
    'gid'       => $attributes{'gidnumber'}->[0],
    'dir'       => $dir,
    'quota'     => $attributes{'mailquota'}->[0],
  };
  my $error = $svc_acct->insert;
  #my $error = $svc_acct->check;

  if ( $error ) { 
    warn "$error\n";
  } else {
    $imported++;
  }

}

print "$imported of $acct imported\n";

#print "\n\n";

#foreach ( sort { $objectclass{$b} <=> $objectclass{$a} } keys %objectclass ) {
#  print "$objectclass{$_}: $_\n";
#}