trying to get HEAD building again. If you want the code
[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, $is_samba_group );
19 my ( %attr_map, %group_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 %group_attr_map = (
45         ntSid           => 'sambaSID',
46         ntGroupType     => 'sambaGroupType',
47 );
48
49 $domsid = $ARGV[0];
50
51 $ldif = Net::LDAP::LDIF->new ($ARGV[1], "r")
52         or die $!;
53 $ldif2 = Net::LDAP::LDIF->new ($ARGV[2], "w")
54         or die $!;
55
56 while ( !$ldif->eof ) {
57         undef ( $entry );
58         $entry = $ldif->read_entry();
59
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;
65         }
66
67         ##
68         ## check to see if we have anything to do on this
69         ## entry.  If not just write it out
70         ##
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;
79                 }
80         }
81
82         if ( defined ( $is_samba_account ) ) {
83                 ##
84                 ## start editing the sambaAccount
85                 ##
86
87                 $entry->delete( 'objectclass' => [ 'sambaAccount' ] );
88                 $entry->add( 'objectclass' => 'sambaSamAccount' );
89
90                 $entry->add( 'sambaSID' => $domsid."-".$entry->get_value( "rid" ) );
91                 $entry->delete( 'rid' );
92         
93                 if ( $entry->get_value( "primaryGroupID" ) ) {
94                         $entry->add( 'sambaPrimaryGroupSID' => $domsid."-".$entry->get_value( "primaryGroupID" ) );
95                         $entry->delete( 'primaryGroupID' );
96                 }
97         
98
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 );
103                         }
104                 }
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 );
110                         }
111                 }
112         }
113         
114         $ldif2->write_entry( $entry );
115 }
116
117