Ticket #39615 Fix versions for upgrades
[freeside.git] / bin / ispman.ldap.import
1 #!/usr/bin/perl -w
2
3 use strict;
4 use Net::LDAP::LDIF;
5
6 use FS::UID qw(adminsuidsetup);
7 use FS::Record qw(qsearchs);
8 use FS::svc_domain;
9 use FS::svc_acct;
10
11 my $user = shift or die;
12 adminsuidsetup($user);
13
14 $FS::svc_Common::noexport_hack = 1;
15 $FS::svc_domain::whois_hack = 1;
16
17 my $domain_svcpart = 1;
18 my $account_svcpart = 2;
19 my $mailbox_svcpart = 3;
20 my $fedweeknet_svcpart = 4;
21
22 #my $ldif =
23 #  Net::LDAP::LDIF->new( "ispman-06-23-04.ldif", "r", onerror => 'undef' );
24 my $ldif =
25   Net::LDAP::LDIF->new( "ispman-06-23-04.ldif", "r", onerror => 'warn' );
26
27 #my %objectclass;
28
29 my $acct = 0;
30 my $imported = 0;
31
32 my $entry;
33 while ( $entry = $ldif->read_entry ) {
34   #warn "$entry\n";
35   my %attributes = map { $_ => [ $entry->get_value( $_ ) ]  } $entry->attributes;
36
37   my $objectclass = join('/', @{$attributes{'objectclass'}} );
38
39   next unless $objectclass eq 'posixAccount/ispmanDomainUser/radiusprofile';
40
41   foreach my $attr ( keys %attributes ) {
42     print join( " => ", substr($attr.' 'x30,0,30), @{$attributes{ $attr }} ), "\n";
43     #if ( $attr eq 'objectclass' ) {
44     #  $objectclass{ join('/', @{$attributes{$attr}} ) }++;
45     #}
46   }
47   print "\n";
48
49   $acct++;
50
51   my $email = $attributes{'maillocaladdress'}->[0];
52   $email =~ /^(\w+)\@([\w\.\-]+)$/ or die $email;
53   die "$1 ne ". $attributes{'ispmanuserid'}->[0]. "\n"
54     unless lc($1) eq $attributes{'ispmanuserid'}->[0];
55   my $username = lc($1);
56   my $domain = lc($2);
57
58   my $svc_domain = qsearchs('svc_domain', { 'domain' => $domain } )
59                  || new FS::svc_domain { 'svcpart' => $domain_svcpart,
60                                          'domain'  => $domain,
61                                          'action'  => 'N',
62                                        };
63
64   unless ( $svc_domain->svcnum ) {
65     my $error = $svc_domain->insert;
66     if ( $error ) {
67       die "inserting domain: $error\n";
68     }
69   }
70
71   ( my $password = $attributes{'userpassword'}->[0] ) =~ s/^\{crypt\}//;
72
73   # pick svcpart
74   my $svcpart = $account_svcpart;
75   if ( $domain eq 'fedweeknet.com' ) {
76     $svcpart = $fedweeknet_svcpart;
77   } elsif ( $attributes{'dialupaccess'}->[0] =~ /(false|no)/i ) {
78     $svcpart = $mailbox_svcpart;
79   }
80
81   my $dir = $attributes{'homedirectory'}->[0];
82   $dir =~ s/\s+//g;
83   $dir =~ s/\@/_/;
84
85   my $svc_acct = new FS::svc_acct {
86     'svcpart'   => $svcpart,
87     'username'  => $username,
88     '_password' => $password,
89     'finger'    => $attributes{'cn'}->[0],
90     'domsvc'    => $svc_domain->svcnum,
91     'shell'     => $attributes{'loginshell'}->[0],
92     'uid'       => $attributes{'uidnumber'}->[0],
93     'gid'       => $attributes{'gidnumber'}->[0],
94     'dir'       => $dir,
95     'quota'     => $attributes{'mailquota'}->[0],
96   };
97   my $error = $svc_acct->insert;
98   #my $error = $svc_acct->check;
99
100   if ( $error ) { 
101     warn "$error\n";
102   } else {
103     $imported++;
104   }
105
106 }
107
108 print "$imported of $acct imported\n";
109
110 #print "\n\n";
111
112 #foreach ( sort { $objectclass{$b} <=> $objectclass{$a} } keys %objectclass ) {
113 #  print "$objectclass{$_}: $_\n";
114 #}