summaryrefslogtreecommitdiff
path: root/tests/common/compare_file.pl
blob: eb498d3238ef881d477f0326981d436341e64ff2 (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
#!/usr/bin/perl

open (TEMPLATE, $ARGV[0]) or die "Cannot open '".$ARGV[0]."': $!";
my $template = join "", <TEMPLATE>;
open (FILE, $ARGV[1]) or die "Cannot open '".$ARGV[1]."': $!";
my $file = join "", <FILE>;

my $today = int(time()/(24*3600));
$template =~ s/\@TODAY\@/$today/g;

my $tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_DES ([^:]*)\@:(.*)$/s) {
	my $user = $2;
	my $pass = $3;
	$tmp = $4;
	if ($file =~ m/^$user:/m) {
		$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_DES $pass\@:$2/m;
		my $cryptpass = $1;
		# Check the password
		my $checkpass = qx|/usr/bin/openssl passwd -crypt -salt '$cryptpass' $pass 2>tmp/openssl.err|;
		chomp $checkpass;

		system "cat tmp/openssl.err"
			if ($checkpass ne $cryptpass);
		system "rm -f tmp/openssl.err";
		die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
			if ($checkpass ne $cryptpass);
	} else {
		die "No user '$user' in ".$ARGV[1].".\n";
	}
}

$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_MD5 ([^:]*)\@:(.*)$/s) {
	my $user = $2;
	my $pass = $3;
	$tmp = $4;
	if ($file =~ m/^$user:/m) {
		$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_MD5 $pass\@:$2/m;
		my $cryptpass = $1;
		# Check the password
		my $salt = $cryptpass;
		$salt =~ s/^\$1\$//;
		$salt =~ s/\$.*$//;
		my $checkpass = qx|/usr/bin/openssl passwd -1 -salt '$salt' '$pass'|;
		chomp $checkpass;

		die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
			if ($checkpass ne $cryptpass);
	} else {
		die "No user '$user' in ".$ARGV[1].".\n";
	}
}

$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_SHA256 ([^:]*)\@:(.*)$/s) {
	my $user = $2;
	my $pass = $3;
	$tmp = $4;
	if ($file =~ m/^$user:/m) {
		$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_SHA256 $pass\@:$2/m;
		my $cryptpass = $1;
		# Check the password
		my $salt = $cryptpass;
		$salt =~ s/^\$5\$//;
		my $rounds = "";
		if ($salt =~ s/^rounds=([0-9]*)\$//) {
			$rounds = "-R $1";
		}

		$salt =~ s/\$.*$//;
		my $checkpass = qx!echo '$pass' | /usr/bin/mkpasswd -m sha-256 --salt '$salt' $rounds --stdin!;
		chomp $checkpass;

		die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
			if ($checkpass ne $cryptpass);
	} else {
		die "No user '$user' in ".$ARGV[1].".\n";
	}
}

$tmp = $template;
while ($tmp =~ m/^(.*?)([^\n]*):\@PASS_SHA512 ([^:]*)\@:(.*)$/s) {
	my $user = $2;
	my $pass = $3;
	$tmp = $4;
	if ($file =~ m/^$user:/m) {
		$file =~ s/^$user:([^:]*):(.*)$/$user:\@PASS_SHA512 $pass\@:$2/m;
		my $cryptpass = $1;
		# Check the password
		my $salt = $cryptpass;
		$salt =~ s/^\$6\$//;
		my $rounds = "";
		if ($salt =~ s/^rounds=([0-9]*)\$//) {
			$rounds = "-R $1";
		}

		$salt =~ s/\$.*$//;
		my $checkpass = qx!echo '$pass' | /usr/bin/mkpasswd -m sha-512 --salt '$salt' $rounds --stdin!;
		chomp $checkpass;

		die "Wrong password for $user: '$cryptpass'. Expected password: '$checkpass'\n"
			if ($checkpass ne $cryptpass);
	} else {
		die "No user '$user' in ".$ARGV[1].".\n";
	}
}


exit 0 if ($file =~ m/^\Q$template\E$/s);

print "Files differ.\n";

system "diff", "-au", $ARGV[0], $ARGV[1];

exit 1