summaryrefslogtreecommitdiff
path: root/source3/script/mysql_convert.pl
blob: e9c1b9de56b8279e674c2a2631ecc97436b995ff (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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
#!/usr/local/bin/perl

use Mysql;

$ACB_DISABLED=0x0001;
$ACB_HOMDIRREQ=0x0002;
$ACB_PWNOTREQ=0x0004;
$ACB_TEMPDUP=0x0008;
$ACB_NORMAL=0x0010;
$ACB_MNS=0x0020;
$ACB_DOMTRUST=0x0040;
$ACB_WSTRUST=0x0080;
$ACB_SVRTRUST=0x0100;
$ACB_PWNOEXP=0x0200;
$ACB_AUTOLOCK=0x0400;

sub getoptionval {
	my ($option) = @_;

	my ($value) = ($option =~ /^[^=]+=\s*(\S.*\S)/ );

	return $value;
}

sub usage {

print <<EOUSAGE;
$0 [options]
options:
   --infile=<filename>          # smbpasswd style file to read entries from
   --outfile=<filename>         # file to dump results to, format depending
				#   on --infile:
				# With --infile: Dump mysql script queries
				# Without --infile: Dump smbpasswd format
				#                from reading mysql database
   --host=<hostname>            # Mysql Server name (default: localhost)
   --db=<database>		# Mysql Database name
   --user=<user>                # Mysql User
   --password[=<password>]      # Mysql password for --user
   --table=<table>              # Mysql table
   --create			# Generate 'create table' query
   --file=[trash|append]	# Action to take if --outfile file exists
   --check                      # Do not alter or skip bad uids

EOUSAGE
exit 0;
}

sub getpass {
	my($prompt)=@_;
	my($ret);

	print $prompt;
	system "stty -echo";
	chomp($ret=<STDIN>);
	system "stty echo";
	print "\n";
	$ret;
}

sub next_entry {
	my ($name,$uid,$lm,$nt,$f,$lct) = ();

	$name="";
	if ( not $infile ) {
		($name,$uid,$lm,$nt,$f,$lct) = $mysqlquery->fetchrow();
	}
	else {
		my $line=<INFILE>;

		return () if ( not $line );

		chomp($line);

		next if ( $line !~ /^[^: ]+:\d+:/ );

		($name,$uid,$lm,$nt,$f,$lct) = split(/:/,$line);

		if ( $lct =~ /^LCT-/ ) {
			# New Format smbpasswd file
			my $flags=0;

			$flags |= $ACB_PWNOTREQ if ( $f =~ /N/ );
			$flags |= $ACB_DISABLED if ( $f =~ /D/ );
			$flags |= $ACB_HOMDIRREQ if ( $f =~ /H/ );
			$flags |= $ACB_TEMPDUP if ( $f =~ /T/ );
			$flags |= $ACB_NORMAL if ( $f =~ /U/ );
			$flags |= $ACB_MNS if ( $f =~ /M/ );
			$flags |= $ACB_WSTRUST if ( $f =~ /W/ );
			$flags |= $ACB_SVRTRUST if ( $f =~ /S/ );
			$flags |= $ACB_AUTOLOCK if ( $f =~ /L/ );
			$flags |= $ACB_PWNOEXP if ( $f =~ /X/ );
			$flags |= $ACB_DOMTRUST if ( $f =~ /I/ );

			$f = $flags;

			$f = $ACB_NORMAL if ( not $f );

			$lct =~ s/LCT-//;
			$lct = (unpack("L",pack("H8",$lct)))[0];
		}
		else {
			# Old Format smbpasswd file
			$f = 0;
			$lct = time();
			if ( $lm =~ /^NO PASS/ ) {
				$f |= $ACB_PWNOTREQ;
				$lm = "";
				$nt = "";
			}
			elsif ( $lm =~ /^XX/ ) {
				$f |= $ACB_DISABLED;

				$lm = "";
				$nt = "";
			}

			if ( $name =~ /\$$/ ) {
				$f |= $ACB_WSTRUST;
			}

			$f = $ACB_NORMAL if ( not $f );
		}
	}
	return () if ( not $name );
	($name,$uid,$lm,$nt,$f,$lct);
}

sub do_query {
	my ( $query ) = @_;

	chomp($query);
	if ( $outfile ) {
		print OUTFILE "$query;\n";
	}
	else {
		if ( not $mysqldb->query($query) ) {
			print "$query: $Mysql::db_errstr\n";
		}
	}
}

sub do_file {
	my ($file,$name,$uid,$lm,$nt,$f,$lct)=@_;

	my $strings = "";

	$strings .= "N" if ( $f & $ACB_PWNOTREQ );
	$strings .= "D" if ( $f & $ACB_DISABLED );
	$strings .= "H" if ( $f & $ACB_HOMDIRREQ );
	$strings .= "T" if ( $f & $ACB_TEMPDUP );
	$strings .= "U" if ( $f & $ACB_NORMAL );
	$strings .= "M" if ( $f & $ACB_MNS );
	$strings .= "W" if ( $f & $ACB_WSTRUST );
	$strings .= "S" if ( $f & $ACB_SVRTRUST );
	$strings .= "L" if ( $f & $ACB_AUTOLOCK );
	$strings .= "X" if ( $f & $ACB_PWNOEXP );
	$strings .= "I" if ( $f & $ACB_DOMTRUST );

	$f = sprintf( "[%-11s]", $strings );

	$lct=uc("LCT-".(unpack("H8",pack("L","$lct")))[0]);

	$lm = "X"x32 if ( not $lm );
	$nt = "X"x32 if ( not $nt );

	print $file "$name:$uid:$lm:$nt:$f:$lct\n";
}

$dbhost = "localhost";

for $option ( @ARGV ) {
	if ( $option =~ /--outfile=/ ) {
		$outfile = getoptionval($option);
	}
	elsif ( $option =~ /--infile=/ ) {
		$infile = getoptionval($option);
	}
	elsif ( $option =~ /--db=/ ) {
		$dbname = getoptionval($option);
	}
	elsif ( $option =~ /--user=/ ) {
		$dbuser = getoptionval($option);
	}
	elsif ( $option =~ /--host=/ ) {
		$dbhost = getoptionval($option);
	}
	elsif ( $option =~ /--password/ ) {
		$dbpasswd = getoptionval($option);
		$need_password = "yes"
	}
	elsif ( $option =~ /--table=/ ) {
		$dbtable = getoptionval($option);
	}
	elsif ( $option =~ /--create/ ) {
		$create_table = "yes";
	}
	elsif ( $option =~ /--file=/ ) {
		$file_action = getoptionval($option);
	}
	elsif ( $option =~ /--check/ ) {
		$check = "yes";
	}
	else {
		print "Unknown option: $option\n";
		$unknown = "yes";
	}
}

&usage if ( $unknown eq "yes" );

if ( ( not $infile ) && ( not $outfile ) && ( $create_table ne "yes" ) ) {
	print "Need file to read from or write to\n";
	&usage;
}
elsif ( $infile && $outfile ) {
	if ( not $dbtable ) {
		print "Need --table to create queries\n";
		exit 1;
	}

	# Reading a smbpasswd file, dumping queries into an file which
	# can be used for a mysql script
	# --db* options are ignored.

	$ignored = "";
	$ignored .= " --db" if ( $dbname );
	$ignored .= " --user" if ( $dbuser );
	$ignored .= " --password" if ( $dbuser );

	if ( $ignored ) {
		print "Ignoring options: $ignored\n";
	}
}
elsif ( (not $dbname) || (not $dbtable) || (not $dbuser) ) {
	print "Missing database particulars:\n";
	print "  --db=??\n" if ( not $dbname );
	print "  --user=??\n" if ( not $dbuser );
	print "  --table=??\n" if ( not $dbtable );
	&usage;
}
else {
	if ( ($need_password eq "yes") && ( not $dbpasswd )) {
		$dbpasswd = getpass("Enter MySQL password for $dbuser: ");
	}
	$mysqldb = Connect Mysql($dbhost,$dbname,$dbuser,$dbpasswd);

	if ( not $mysqldb ) {
		print "Cannot connect to database: $Mysql::db_errstr\n";
		exit 1;
	}

	if ( $outfile ) {
		$mysqlquery = $mysqldb->query("select unix_name,unix_uid,smb_passwd,smb_nt_passwd,acct_ctrl,pass_last_set_time from $dbtable");

		if ( not $mysqlquery ) {
			print "MySQL Query failed: $Mysql::db_errstr\n";
			exit 1;
		}
	}
}

if ( $create_table eq "yes" ) {
	$create_table_query=<<EOSQL;
create table $dbtable (
unix_name            char(20) not null,
unix_uid             int(10)  unsigned not null,
nt_name              char(20) not null,
user_rid             int(10)  unsigned not null,
smb_passwd           char(32),
smb_nt_passwd        char(32),
acct_ctrl            int(10) unsigned not null,
pass_last_set_time   int(10) unsigned not null,
unique (unix_name),
unique (unix_uid)
)
EOSQL
	print "$create_table_query\n";
}
if ( $infile ) {
	if ( not open(INFILE,$infile) ) {
		print "$infile: $!\n";
		exit 1;
	}
}

if ( $outfile ) {
	if ( ! -f $outfile ) {
		$open_string=">$outfile";
	}
	elsif ( not $file_action ) {
		print "File $outfile exists:\n";
		print "Please use --file=[trash|append] option to determine destiny of file\n";
		exit 1;
	}
	elsif ( $file_action eq "append" ) {
		$open_string = ">>$outfile";
	}
	else {
		$open_string = ">$outfile";
	}

	if ( not open(OUTFILE,$open_string) ) {
		print "$outfile: $!\n";
		exit 1;
	}
}

do_query($create_table_query) if ( $create_table_query );

$linenum=1;
while (($name,$uid,$lm,$nt,$f,$lct)=next_entry()) {
	
	$| = 1;
	print "\r$linenum ";
	$linenum++;

	$nuid = "";

	$nuid = (getpwnam(lc($name)))[2];

	if ( $check ) {
		if ( not $nuid ) {
			# print "Removing $name: Does not exist\n";
			push(@removed,[$name,$uid,$lm,$nt,$f,$lct]);
			next;
		}
		else {
			# print "Changing uid of $name\n";
			$uid = $nuid;
		}
	}

	if ( $infile ) {
		if ( $lm ) {
			$lm = "'$lm'";
		}
		else {
			$lm = "NULL";
		}
		if ( $nt ) {
			$nt = "'$nt'";
		}
		else {
			$nt = "NULL";
		}
		$rid=(4*$uid)+1000;
		do_query("insert into $dbtable (unix_name,unix_uid,smb_passwd,smb_nt_passwd,acct_ctrl,pass_last_set_time,nt_name,user_rid) values ('$name',$uid,$lm,$nt,$f,$lct,'$name',$rid)");
	}
	else {
		do_file(OUTFILE,$name,$uid,$lm,$nt,$f,$lct);
	}
}

if ( @removed ) {
	print "\n\nIgnored entries because usernames do not exist\n";
	foreach $line ( @removed ) {
		do_file(STDOUT,@{ $line });
	}
}

close (OUTFILE) if ( $outfile );
close (INFILE) if ( $infile );
print "\n";