turn off special characters in a string

turn off special characters in a string

am 18.09.2007 23:22:46 von wong_powah

My string has special characters '@' and '$'.
How to escape them (turn them off)?
e.g.
$newpw is entered as "1q@W3e$R";

chop($newpw = );
system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");

$newpw becomes "1q@W3e" (the "$R" is chopped).

Re: turn off special characters in a string

am 19.09.2007 00:01:27 von davidfilmer

On Sep 18, 2:22 pm, wong_po...@yahoo.ca wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>
> chop($newpw = );
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>
> $newpw becomes "1q@W3e" (the "$R" is chopped).

perldoc -f quotemeta

FWIW, You probably want to use backticks instead of system(), and you
should check your return code.


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: turn off special characters in a string

am 19.09.2007 04:11:24 von Tad McClellan

wong_powah@yahoo.ca wrote:


> chop($newpw = );


You should use chomp() to remove newlines nowadays.

Using chop() to remove newlines was how it was done 10 years ago.

Where are you learning your Perl from?


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: turn off special characters in a string

am 19.09.2007 08:39:41 von Mirco Wahab

wong_powah@yahoo.ca wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> $newpw is entered as "1q@W3e$R";
> chop($newpw = );
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
> $newpw becomes "1q@W3e" (the "$R" is chopped).

This would normally not happen. I don't know
what you did else, but maybe your "quoting interpolation"
will happen only in your "test example", see:

...
my $command = 'echo'; # change this to 'changepw'

my $oldpw = '1q@W3e$Q' . "\n"; # this is how input would come from
my $newpw = '1q@W3e$R' . "\n"; # outside (note the '' quote chars!)

chomp $oldpw; # don't use chop(), always use chomp()
chomp $newpw; # if possible (as Tad already said)

# use the qq{ .. } operator to evade quote masking errors
system qq{ $command -newpw "$newpw" -oldpw "$oldpw" };
...

If your program is larger, put a

use strict;
use warnings;
...

on top of it and Perl will tell you where
unwanted interpolation might happen.

Regards

M.

Re: turn off special characters in a string

am 19.09.2007 09:10:42 von Josef Moellers

wong_powah@yahoo.ca wrote:
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>=20
> chop($newpw =3D );
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>=20
> $newpw becomes "1q@W3e" (the "$R" is chopped).
>=20

No, it's not, at least not by perl.
However, your shell will most likely do this. Try to use single quotes=20
instead of the double quotes:

system("changepw -newpw '$newpw' -oldpw '$oldpw'");

Note that you don't need a LF at the end of a system() string.

--=20
These are my personal views and not those of Fujitsu Siemens Computers!
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize (T. Pratchett)
Company Details: http://www.fujitsu-siemens.com/imprint.html

Re: turn off special characters in a string

am 19.09.2007 09:47:04 von usenet

On Sep 19, 12:10 am, Josef Moellers siemens.com> wrote:
> Try to use single quotes instead of the double quotes:
> system("changepw -newpw '$newpw' -oldpw '$oldpw'");

Of course, if the password contains a single-quote, this fails. And
if a user is malicious and sets their password to something like this:

x'; rm -rf /;

then your script just deleted your whole system (assuming it runs as
root, which it probably does, since it is changing passwords). Guess
what? You're fired!


--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: turn off special characters in a string

am 19.09.2007 20:49:05 von dalessio

wrote in message
news:1190150566.038724.36690@d55g2000hsg.googlegroups.com...
> My string has special characters '@' and '$'.
> How to escape them (turn them off)?
> e.g.
> $newpw is entered as "1q@W3e$R";
>
> chop($newpw = );
> system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
>
> $newpw becomes "1q@W3e" (the "$R" is chopped).


To avoid escaping quotes, your system call could be (note that
you don't need the \n at the end):

system( qq( changepw -newpw "$newpw" -oldpw "$oldpw" ));

From Perl docs on "system" function: "If there is only one scalar argument,
the argument is checked for shell metacharacters, and if there are any, the
entire argument is passed to the system's command shell for parsing." So,
the command line you have is being passed to your shell for processing. Your
shell is probably interpreting the $... as variables, effectively stripping
them. To avoid this, pass multiple args to "system":

system( 'changepw', '-newpw', $newpw, '-oldpw', $oldpw );

Perl will now call changepw directly rather than using the shell.

Mario