【问题标题】:htpasswd escaping "$" in perl variablehtpasswd 在 perl 变量中转义“$”
【发布时间】:2013-11-18 22:22:37
【问题描述】:

所以我们有一个脚本可以连接到数据库并下拉用户名密码列表。然后我们有一个子例程,它使用 /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"

【问题讨论】:

    标签: regex perl variables .htpasswd


    【解决方案1】:

    你正在形成一个错误的 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);
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-12-20
        • 1970-01-01
        • 1970-01-01
        • 2014-09-17
        • 2015-03-28
        • 1970-01-01
        • 2018-03-13
        相关资源
        最近更新 更多