summaryrefslogtreecommitdiff
path: root/examples/LDAP/convertSambaAccount
blob: 61e80e1b7b6f313b7380c3482cb1b0f64431f800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/usr/bin/perl  -w
##
## Convert an LDIF file containing sambaAccount entries
## to the new sambaSamAccount objectclass
##
## Copyright Gerald (Jerry) Carter	2003
##
## Usage: convertSambaAccount --sid=<Domain SID> \
##       --input=<input ldif> --output=<output ldif> \
##       --changetype=[modify|add]
##


use strict;
use Net::LDAP::LDIF;
use Getopt::Long;


##############################################################################
## local variables

my ( $domain, $domsid, $changetype );
my ( $ldif, $ldif2 );
my ( $entry, @objclasses, $obj );
my ( $is_samba_account, $is_samba_group );
my ( %attr_map, %group_attr_map, $key );
my ( @dels, $deletion, @adds, $addition );
my ( $result, %options );


##############################################################################
## Print the option usage

sub usage {

	print "convertSambaAccount <options>\n";
	print "Options:\n";
	print "  --input        input LDIF filename\n";
	print "  --output       output LDIF filename\n";
	print "  --sid          domain SID\n";
	print "  --changetype   [modify|add] (default is 'add')\n";
}


##############################################################################
##                               MAIN DRIVER                                ##
##############################################################################

##
## hashes to map old attribute names to new ones 
##

%attr_map = ( 
	lmPassword	=> 'sambaLMPassword',
	ntPassword	=> 'sambaNTPassword',
	pwdLastSet	=> 'sambaPwdLastSet',
	pwdMustChange	=> 'sambaPwdMustChange',
	pwdCanChange	=> 'sambaPwdCanChange',
	homeDrive	=> 'sambaHomeDrive',
	smbHome		=> 'sambaHomePath',
	scriptPath	=> 'sambaLogonScript',
	profilePath	=> 'sambaProfilePath',
	kickoffTime	=> 'sambaKickoffTime',
	logonTime	=> 'sambaLogonTime',
	logoffTime	=> 'sambaLogoffTime',
	userWorkstations	=> 'sambaUserWorkstations',
	domain		=> 'sambaDomainName',
	acctFlags	=> 'sambaAcctFlags',
);

%group_attr_map = (
	ntSid		=> 'sambaSID',
	ntGroupType	=> 'sambaGroupType',
);

##
## process command line args
##

$result = GetOptions(\%options, 
			"input=s", 
			"output=s", 
			"sid=s",
			"changetype=s");

if (!$result && ($#ARGV != -1)) {
	usage();
	exit 1;
}


if ( !defined( $options{'sid'} ) ) {
	print "You must provide a domain sid\n";
	exit 1;
}

$domsid = $options{'sid'};

$changetype = 'add';
if ( defined( $options{'changetype'} ) ) {
	$changetype = $options{'changetype'};
}

##
## open files
##

$ldif = Net::LDAP::LDIF->new ($options{'input'}, "r") or die $!;

if ( "$changetype" eq "add" ) {
	$ldif2 = Net::LDAP::LDIF->new ($options{'output'}, "w") or die $!;
}
elsif ( "$changetype" eq "modify" ) {
	open( OUTPUT, ">$options{'output'}" ) or die $!;
}
else {
	print "Bad changetype!\n";
	exit 1;
}

##
## process LDIF 
##

while ( !$ldif->eof ) {
	undef ( $entry );
	$entry = $ldif->read_entry();

	## skip entry if we find an error
	if ( $ldif->error() ) {
		print "Error msg: ",$ldif->error(),"\n";
		print "Error lines:\n",$ldif->error_lines(),"\n";
		next;
	}

	##
	## check to see if we have anything to do on this
	## entry.  If not just write it out
	##
	@objclasses = $entry->get_value( "objectClass" );
	undef ( $is_samba_account );
	undef ( $is_samba_group );
	@adds = ();
	@dels = ();
	foreach $obj ( @objclasses ) {
		if ( "$obj" eq "sambaAccount" ) {
			$is_samba_account = 1;
		} elsif ( "$obj" eq "sambaGroupMapping" ) {
			$is_samba_group = 1;
		}
	}

	if ( defined ( $is_samba_account ) ) {
		##
		## start editing the sambaAccount
		##

		@dels = ( 'objectclass: sambaAccount', 'rid' );
		@adds = ('objectclass: sambaSamAccount', "sambaSID: " .  ${domsid} . "-" . ${entry}->get_value( 'rid' ) );
		$entry->delete( 'objectclass' => [ 'sambaAccount' ] );
		$entry->add( 'objectclass' => 'sambaSamAccount' );

		$entry->add( 'sambaSID' => $domsid."-".$entry->get_value( "rid" ) );
		$entry->delete( 'rid' );
	
		if ( $entry->get_value( "primaryGroupID" ) ) {
			push @adds, "sambaPrimaryGroupSID: " . $domsid."-".$entry->get_value( "primaryGroupID" );
			push @dels, "primaryGroupID";
			$entry->add( 'sambaPrimaryGroupSID' => $domsid."-".$entry->get_value( "primaryGroupID" ) );
			$entry->delete( 'primaryGroupID' );
		}
	

		foreach $key ( keys %attr_map ) {
			if ( defined($entry->get_value($key)) ) {
				push @adds, "$attr_map{$key}: " . $entry->get_value($key);
				push @dels, "$key";
				$entry->add( $attr_map{$key} => $entry->get_value($key) );
				$entry->delete( $key );
			}
		}
	} elsif ( defined ( $is_samba_group ) ) {
		foreach $key ( keys %group_attr_map ) {
			if ( defined($entry->get_value($key)) ) {
				push @adds, "$group_attr_map{$key}: " . $entry->get_value($key);
				push @dels, "$key";
				$entry->add( $group_attr_map{$key} => $entry->get_value($key) );
				$entry->delete( $key );
			}
		}
	}
	
	## see if we should write full entries or only the changes
	
	if ( "$changetype" eq "add" ) {
		$ldif2->write_entry( $entry );
	}
	else {
		if ( defined ( $is_samba_account ) || defined ( $is_samba_group ) ){
			if ( @adds + @dels > 0 ) {
				print OUTPUT "dn: " . $entry->dn . "\n";
				foreach $addition (@adds) {
					$addition =~ /(^\w+):/;
					print OUTPUT "add: " . $1  . "\n";
					print OUTPUT "$addition\n-\n";
				}
				foreach $deletion (@dels) {
					if ( $deletion =~ /^(\w+):\s(.*)/ ) {
						print OUTPUT "delete: $1\n$1: $2\n-\n";
					} else {
						print OUTPUT "delete: $deletion\n-\n"
					}
				}
				print OUTPUT "\n"
			}
		}
	}
}