trying to get HEAD building again. If you want the code
[Samba.git] / examples / LDAP / convertSambaAccount
blob223c43eadabc35afba84895cf0476bbf534b497f
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, $is_samba_group );
19 my ( %attr_map, %group_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 %group_attr_map = (
45 ntSid => 'sambaSID',
46 ntGroupType => 'sambaGroupType',
49 $domsid = $ARGV[0];
51 $ldif = Net::LDAP::LDIF->new ($ARGV[1], "r")
52 or die $!;
53 $ldif2 = Net::LDAP::LDIF->new ($ARGV[2], "w")
54 or die $!;
56 while ( !$ldif->eof ) {
57 undef ( $entry );
58 $entry = $ldif->read_entry();
60 ## skip entry if we find an error
61 if ( $ldif->error() ) {
62 print "Error msg: ",$ldif->error(),"\n";
63 print "Error lines:\n",$ldif->error_lines(),"\n";
64 next;
68 ## check to see if we have anything to do on this
69 ## entry. If not just write it out
71 @objclasses = $entry->get_value( "objectClass" );
72 undef ( $is_samba_account );
73 undef ( $is_samba_group );
74 foreach $obj ( @objclasses ) {
75 if ( "$obj" eq "sambaAccount" ) {
76 $is_samba_account = 1;
77 } elsif ( "$obj" eq "sambaGroupMapping" ) {
78 $is_samba_group = 1;
82 if ( defined ( $is_samba_account ) ) {
84 ## start editing the sambaAccount
87 $entry->delete( 'objectclass' => [ 'sambaAccount' ] );
88 $entry->add( 'objectclass' => 'sambaSamAccount' );
90 $entry->add( 'sambaSID' => $domsid."-".$entry->get_value( "rid" ) );
91 $entry->delete( 'rid' );
93 if ( $entry->get_value( "primaryGroupID" ) ) {
94 $entry->add( 'sambaPrimaryGroupSID' => $domsid."-".$entry->get_value( "primaryGroupID" ) );
95 $entry->delete( 'primaryGroupID' );
99 foreach $key ( keys %attr_map ) {
100 if ( defined($entry->get_value($key)) ) {
101 $entry->add( $attr_map{$key} => $entry->get_value($key) );
102 $entry->delete( $key );
105 } elsif ( defined ( $is_samba_group ) ) {
106 foreach $key ( keys %group_attr_map ) {
107 if ( defined($entry->get_value($key)) ) {
108 $entry->add( $group_attr_map{$key} => $entry->get_value($key) );
109 $entry->delete( $key );
114 $ldif2->write_entry( $entry );