簡體   English   中英

htpasswd在perl變量中轉義“ $”

[英]htpasswd escaping “$” in perl variable

因此,我們有一個腳本可以連接到數據庫並下拉用戶名密碼列表。 然后,我們有一個子例程,該子例程使用/ usr / local / apache2 / bin / htpasswd -bc命中登錄名和密碼,然后將它們轉儲到/ tmp / file中。

有些用戶可能無法登錄

sub getUsers {
    $dbUser = shift; 
    $dbPass = shift;
    $dbSid  = shift;
    $dbh =  DBI->connect("dbi:Oracle:$dbSid","$dbUser","$dbPass") or die( "Couldn't connect: $!" );
    $sql = "SELECT user_name,passwd FROM login_tbl";
    $sth = $dbh->prepare($sql);
    $sth->execute();
    while ( ( $user_name,$passwd ) = $sth->fetchrow_array ) {
        $passwd = quotemeta($passwd);
        updatePasswdFile($user_name,$passwd); 
        }
        $dbh->disconnect();
}


sub updatePasswdFile {
    $file = "/tmp/tmpusers";
    $user = shift;
    $pass = shift;
    print "Adding user: $user\n";
    $cmd = "$prefs{htpasswd} -b $file $user $pass";
    system($cmd);
}

我發現htpasswd沒有處理帶有“ $”符號的登錄名或密碼。

bash-3.00$
bash-3.00$  /usr/local/apache2/bin/htpasswd -bc /tmp/error.htpasswd zions$lee test
Adding password for user zions
bash-3.00$
bash-3.00$ cat /tmp/error.htpasswd
zions:$apr1$2YZZsgEK$csp6L7vbO81YYNSaRCdZQ/
bash-3.00$

bash-3.00$  /usr/local/apache2/bin/htpasswd -bc /tmp/error.htpasswd "zions$lee" test
Adding password for user zions
bash-3.00$
bash-3.00$ cat /tmp/error.htpasswd
zions:$apr1$AQOnDHdP$xBdm0AB3WZN.1cIeNLXUw/
bash-3.00$
bash-3.00$  /usr/local/apache2/bin/htpasswd -bc /tmp/error.htpasswd "zions\$lee" test
Adding password for user zions$lee
bash-3.00$
bash-3.00$
bash-3.00$ cat /tmp/error.htpasswd
zions$lee:$apr1$kmnQqi6K$bNKp0Ly8Pn.dqk.gEPb2H.
bash-3.00$
bash-3.00$
bash-3.00$

我有一個正則表達式/ ^ \\ S + \\ s \\ S + $ /可以找到兩個單詞都帶有美元符號的情況-我很難更改正則表達式,例如僅登錄名帶有'$“字符的情況,反之亦然,只有密碼。

我正在用if語句轉義“ $”字符,但是我在把“”(引號)放在變量中時感到很費勁。 如何獲取和替換正則表達式一個,另一個或兩者,以及如何在$ user(或$ pass)變量中用美元符號轉義用戶名(或密碼或兩者)?

到目前為止,這是什么:

#!/usr/bin/perl
$escaping_file = $ARGV[0];
open( $filehandle, "<" , "$escaping_file" ) || die "can't access the file : $!";
while (<$filehandle>) {
    chomp;
    if ($_ =~ /^\$?\w+\$+\w+\$?$/g) {
    s/\$/\\\$/g ;
    $escaped_wquotes = qq("$_") ;
    print "$escaped_wquotes\n";
    }
}



[casper@casper]$ ./check_escapes 2013Nov14.logins
"uho\$test2"
"uho\$test3"
"ishi\$jg"
"bs\$test"
"uho\$test"
"boa\$jb"
"ishi\$b2"
"fb\$test"
"boston\$ny"
"stp\$test"
"tec\$stp3"
"bc\$test"

您正在形成錯誤的shell命令。

use String::ShellQuote qw( shell_quote );
my $shell_cmd = shell_quote($prefs{htpasswd}, '-b', $file, $user, $pass);
system($shell_cmd);

但是為什么要調用一個shell呢?

system($prefs{htpasswd}, '-b', $file, $user, $pass);

嘗試$cmd = "$prefs{htpasswd} -b '$file' '$user' '$pass'";

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM