applied patch from bug#140
[Samba/gebeck_regimport.git] / examples / LDAP / convertSambaAccount
blobf5b49ff095784812662b0af961614a92b9ce9fe6
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 ##
12 use strict;
13 use Net::LDAP::LDIF;
15 my ( $domain, $domsid );
16 my ( $ldif, $ldif2 );
17 my ( $entry, @objclasses, $obj );
18 my ( $is_samba_account );
19 my ( %attr_map, $key );
21 if ( $#ARGV != 2 ) {
22 print "Usage: convertSambaAccount domain_sid input_ldif output_ldif\n";
23 exit 1;
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',
44 $domsid = $ARGV[0];
46 $ldif = Net::LDAP::LDIF->new ($ARGV[1], "r")
47 or die $!;
48 $ldif2 = Net::LDAP::LDIF->new ($ARGV[2], "w")
49 or die $!;
51 while ( !$ldif->eof ) {
52 undef ( $entry );
53 $entry = $ldif->read_entry();
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;
63 ## check to see if we have anything to do on this
64 ## entry. If not just write it out
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;
74 if ( !defined ( $is_samba_account ) ) {
75 $ldif2->write_entry( $entry );
76 next;
80 ## start editing the sambaAccount
83 $entry->delete( 'objectclass' => [ 'sambaAccount' ] );
84 $entry->add( 'objectclass' => 'sambaSamAccount' );
86 $entry->add( 'sambaSID' => $domsid."-".$entry->get_value( "rid" ) );
87 $entry->delete( 'rid' );
89 if ( $entry->get_value( "primaryGroupID" ) ) {
90 $entry->add( 'sambaPrimaryGroupSID' => $domsid."-".$entry->get_value( "primaryGroupID" ) );
91 $entry->delete( 'primaryGroupID' );
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 );
102 $ldif2->write_entry( $entry );