perl script to convert from sambaAccount to sambaSamAccount; requires Net::LDAP:...
[ira/wip.git] / examples / LDAP / convertSambaAccount
1 #!/usr/bin/perl  -w
2 ##
3 ## Convert an LDIF file containing sambaAccount entries
4 ## to the new sambaSamAccount objectclass
5 ##
6 ## Copyright Gerald (Jerry) Carter      2003
7 ##
8 ## Usage: convertSambaAccount <Domain SID> <input ldif> <output ldif>
9 ##
10
11
12 use strict;
13 use Net::LDAP::LDIF;
14
15 my ( $domain, $domsid );
16 my ( $ldif, $ldif2 );
17 my ( $entry, @objclasses, $obj );
18 my ( $is_samba_account );
19 my ( %attr_map, $key );
20
21 if ( $#ARGV != 2 ) {
22         print "Usage: convertSambaAccount domain_sid input_ldif output_ldif\n";
23         exit 1;
24 }
25
26 %attr_map = ( 
27         lmPassword      => 'sambaLMPassword',
28         ntPassword      => 'sambaNTPassword',
29         pwdLastSet      => 'sambaPwdLastSet',
30         pwdMustChange   => 'sambaPwdMustChange',
31         pwdCanChange    => 'sambaPwdCanChange',
32         homeDrive       => 'sambaHomeDrive',
33         smbHome         => 'sambaHomePath',
34         scriptPath      => 'sambaLogonScript',
35         profilePath     => 'sambaProfilePath',
36         kickoffTime     => 'sambaKickoffTime',
37         logonTime       => 'sambaLogonTime',
38         logoffTime      => 'sambaLogoffTime',
39         userWorkstations        => 'sambaUserWorkstations',
40         domain          => 'sambaDomainName',
41         acctFlags       => 'sambaAcctFlags',
42 );
43
44 $domsid = $ARGV[0];
45
46 $ldif = Net::LDAP::LDIF->new ($ARGV[1], "r")
47         or die $!;
48 $ldif2 = Net::LDAP::LDIF->new ($ARGV[2], "w")
49         or die $!;
50
51 while ( !$ldif->eof ) {
52         undef ( $entry );
53         $entry = $ldif->read_entry();
54
55         ## skip entry if we find an error
56         if ( $ldif->error() ) {
57                 print "Error msg: ",$ldif->error(),"\n";
58                 print "Error lines:\n",$ldif->error_lines(),"\n";
59                 next;
60         }
61
62         ##
63         ## check to see if we have anything to do on this
64         ## entry.  If not just write it out
65         ##
66         @objclasses = $entry->get_value( "objectClass" );
67         undef ( $is_samba_account );
68         foreach $obj ( @objclasses ) {
69                 if ( "$obj" eq "sambaAccount" ) {
70                         $is_samba_account = 1;
71                 }
72         }
73
74         if ( !defined ( $is_samba_account ) ) {
75                 $ldif2->write_entry( $entry );
76                 next;
77         }
78
79         ##
80         ## start editing the sambaAccount
81         ##
82
83         $entry->delete( 'objectclass' => [ 'sambaAccount' ] );
84         $entry->add( 'objectclass' => 'sambaSamAccount' );
85
86         $entry->add( 'sambaSID' => $domsid."-".$entry->get_value( "rid" ) );
87         $entry->delete( 'rid' );
88         
89         if ( $entry->get_value( "primaryGroupID" ) ) {
90                 $entry->add( 'primaryGroupSID' => $domsid."-".$entry->get_value( "primaryGroupID" ) );
91                 $entry->delete( 'primaryGroupID' );
92         }
93         
94
95         foreach $key ( keys %attr_map ) {
96                 if ( $entry->get_value($key) ) {
97                         $entry->add( $attr_map{$key} => $entry->get_value($key) );
98                         $entry->delete( $key );
99                 }
100         }
101         
102         $ldif2->write_entry( $entry );
103 }
104
105