forked from epety/100-shell-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path080-htpasswd-b.pl
More file actions
31 lines (25 loc) · 724 Bytes
/
080-htpasswd-b.pl
File metadata and controls
31 lines (25 loc) · 724 Bytes
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
#!/usr/bin/perl
if (! scalar @ARGV ) {
print "Usage: htpasswd-b passwordfile user password\n";
print "(this program automatically creates the pw file if needed.)\n";
exit 0;
}
@saltsource = ('a'..'z', 'A'..'Z', '0'..'9','.','/');
$randum_num = int(rand(scalar @saltsource));
$salt = $saltsource[$randum_num];
$randum_num = int(rand(scalar @saltsource));
$salt .= $saltsource[$randum_num];
$outf=$ARGV[0];
$user=$ARGV[1];
$passwd=$ARGV[2];
if ($user && $passwd) {
$encrypted = crypt($passwd, "$salt");
if (-f $outf) {
open(OUT, ">>$outf") || die "htpasswd-b error: $!\n";
} else {
open(OUT, ">$outf") || die "htpasswd-b error: $!\n";
}
print OUT "$user:$encrypted\n";
close(OUT);
exit 0;
}